blob: 898601accabbf3b007f23e134db5c647838deeab [file] [log] [blame]
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001/*
2 * RTL8XXXU mac80211 USB driver
3 *
Jes Sorenseneb188062016-04-14 16:37:14 -04004 * Copyright (c) 2014 - 2016 Jes Sorensen <Jes.Sorensen@redhat.com>
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005 *
6 * Portions, notably calibration code:
7 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
8 *
9 * This driver was written as a replacement for the vendor provided
10 * rtl8723au driver. As the Realtek 8xxx chips are very similar in
11 * their programming interface, I have started adding support for
12 * additional 8xxx chips like the 8192cu, 8188cus, etc.
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of version 2 of the GNU General Public License as
16 * published by the Free Software Foundation.
17 *
18 * This program is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 * more details.
22 */
23
24#include <linux/init.h>
25#include <linux/kernel.h>
26#include <linux/sched.h>
27#include <linux/errno.h>
28#include <linux/slab.h>
29#include <linux/module.h>
30#include <linux/spinlock.h>
31#include <linux/list.h>
32#include <linux/usb.h>
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/ethtool.h>
36#include <linux/wireless.h>
37#include <linux/firmware.h>
38#include <linux/moduleparam.h>
39#include <net/mac80211.h>
40#include "rtl8xxxu.h"
41#include "rtl8xxxu_regs.h"
42
43#define DRIVER_NAME "rtl8xxxu"
44
Jes Sorensen599119f2016-04-28 15:19:06 -040045int rtl8xxxu_debug = RTL8XXXU_DEBUG_EFUSE;
Jes Sorensen26f1fad2015-10-14 20:44:51 -040046static bool rtl8xxxu_ht40_2g;
47
48MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@redhat.com>");
49MODULE_DESCRIPTION("RTL8XXXu USB mac80211 Wireless LAN Driver");
50MODULE_LICENSE("GPL");
51MODULE_FIRMWARE("rtlwifi/rtl8723aufw_A.bin");
52MODULE_FIRMWARE("rtlwifi/rtl8723aufw_B.bin");
53MODULE_FIRMWARE("rtlwifi/rtl8723aufw_B_NoBT.bin");
54MODULE_FIRMWARE("rtlwifi/rtl8192cufw_A.bin");
55MODULE_FIRMWARE("rtlwifi/rtl8192cufw_B.bin");
56MODULE_FIRMWARE("rtlwifi/rtl8192cufw_TMSC.bin");
Jes Sorensenb001e082016-02-29 17:04:02 -050057MODULE_FIRMWARE("rtlwifi/rtl8192eu_nic.bin");
Jes Sorensen35a741f2016-02-29 17:04:10 -050058MODULE_FIRMWARE("rtlwifi/rtl8723bu_nic.bin");
59MODULE_FIRMWARE("rtlwifi/rtl8723bu_bt.bin");
Jes Sorensen26f1fad2015-10-14 20:44:51 -040060
61module_param_named(debug, rtl8xxxu_debug, int, 0600);
62MODULE_PARM_DESC(debug, "Set debug mask");
63module_param_named(ht40_2g, rtl8xxxu_ht40_2g, bool, 0600);
64MODULE_PARM_DESC(ht40_2g, "Enable HT40 support on the 2.4GHz band");
65
66#define USB_VENDOR_ID_REALTEK 0x0bda
Jes Sorensen26f1fad2015-10-14 20:44:51 -040067#define RTL8XXXU_RX_URBS 32
68#define RTL8XXXU_RX_URB_PENDING_WATER 8
69#define RTL8XXXU_TX_URBS 64
70#define RTL8XXXU_TX_URB_LOW_WATER 25
71#define RTL8XXXU_TX_URB_HIGH_WATER 32
72
73static int rtl8xxxu_submit_rx_urb(struct rtl8xxxu_priv *priv,
74 struct rtl8xxxu_rx_urb *rx_urb);
75
76static struct ieee80211_rate rtl8xxxu_rates[] = {
77 { .bitrate = 10, .hw_value = DESC_RATE_1M, .flags = 0 },
78 { .bitrate = 20, .hw_value = DESC_RATE_2M, .flags = 0 },
79 { .bitrate = 55, .hw_value = DESC_RATE_5_5M, .flags = 0 },
80 { .bitrate = 110, .hw_value = DESC_RATE_11M, .flags = 0 },
81 { .bitrate = 60, .hw_value = DESC_RATE_6M, .flags = 0 },
82 { .bitrate = 90, .hw_value = DESC_RATE_9M, .flags = 0 },
83 { .bitrate = 120, .hw_value = DESC_RATE_12M, .flags = 0 },
84 { .bitrate = 180, .hw_value = DESC_RATE_18M, .flags = 0 },
85 { .bitrate = 240, .hw_value = DESC_RATE_24M, .flags = 0 },
86 { .bitrate = 360, .hw_value = DESC_RATE_36M, .flags = 0 },
87 { .bitrate = 480, .hw_value = DESC_RATE_48M, .flags = 0 },
88 { .bitrate = 540, .hw_value = DESC_RATE_54M, .flags = 0 },
89};
90
91static struct ieee80211_channel rtl8xxxu_channels_2g[] = {
Johannes Berg57fbcce2016-04-12 15:56:15 +020092 { .band = NL80211_BAND_2GHZ, .center_freq = 2412,
Jes Sorensen26f1fad2015-10-14 20:44:51 -040093 .hw_value = 1, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +020094 { .band = NL80211_BAND_2GHZ, .center_freq = 2417,
Jes Sorensen26f1fad2015-10-14 20:44:51 -040095 .hw_value = 2, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +020096 { .band = NL80211_BAND_2GHZ, .center_freq = 2422,
Jes Sorensen26f1fad2015-10-14 20:44:51 -040097 .hw_value = 3, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +020098 { .band = NL80211_BAND_2GHZ, .center_freq = 2427,
Jes Sorensen26f1fad2015-10-14 20:44:51 -040099 .hw_value = 4, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200100 { .band = NL80211_BAND_2GHZ, .center_freq = 2432,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400101 .hw_value = 5, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200102 { .band = NL80211_BAND_2GHZ, .center_freq = 2437,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400103 .hw_value = 6, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200104 { .band = NL80211_BAND_2GHZ, .center_freq = 2442,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400105 .hw_value = 7, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200106 { .band = NL80211_BAND_2GHZ, .center_freq = 2447,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400107 .hw_value = 8, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200108 { .band = NL80211_BAND_2GHZ, .center_freq = 2452,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400109 .hw_value = 9, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200110 { .band = NL80211_BAND_2GHZ, .center_freq = 2457,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400111 .hw_value = 10, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200112 { .band = NL80211_BAND_2GHZ, .center_freq = 2462,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400113 .hw_value = 11, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200114 { .band = NL80211_BAND_2GHZ, .center_freq = 2467,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400115 .hw_value = 12, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200116 { .band = NL80211_BAND_2GHZ, .center_freq = 2472,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400117 .hw_value = 13, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200118 { .band = NL80211_BAND_2GHZ, .center_freq = 2484,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400119 .hw_value = 14, .max_power = 30 }
120};
121
122static struct ieee80211_supported_band rtl8xxxu_supported_band = {
123 .channels = rtl8xxxu_channels_2g,
124 .n_channels = ARRAY_SIZE(rtl8xxxu_channels_2g),
125 .bitrates = rtl8xxxu_rates,
126 .n_bitrates = ARRAY_SIZE(rtl8xxxu_rates),
127};
128
Jes Sorensen20e3b2e2016-04-28 15:19:08 -0400129struct rtl8xxxu_reg8val rtl8xxxu_gen1_mac_init_table[] = {
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400130 {0x420, 0x80}, {0x423, 0x00}, {0x430, 0x00}, {0x431, 0x00},
131 {0x432, 0x00}, {0x433, 0x01}, {0x434, 0x04}, {0x435, 0x05},
132 {0x436, 0x06}, {0x437, 0x07}, {0x438, 0x00}, {0x439, 0x00},
133 {0x43a, 0x00}, {0x43b, 0x01}, {0x43c, 0x04}, {0x43d, 0x05},
134 {0x43e, 0x06}, {0x43f, 0x07}, {0x440, 0x5d}, {0x441, 0x01},
135 {0x442, 0x00}, {0x444, 0x15}, {0x445, 0xf0}, {0x446, 0x0f},
136 {0x447, 0x00}, {0x458, 0x41}, {0x459, 0xa8}, {0x45a, 0x72},
137 {0x45b, 0xb9}, {0x460, 0x66}, {0x461, 0x66}, {0x462, 0x08},
138 {0x463, 0x03}, {0x4c8, 0xff}, {0x4c9, 0x08}, {0x4cc, 0xff},
139 {0x4cd, 0xff}, {0x4ce, 0x01}, {0x500, 0x26}, {0x501, 0xa2},
140 {0x502, 0x2f}, {0x503, 0x00}, {0x504, 0x28}, {0x505, 0xa3},
141 {0x506, 0x5e}, {0x507, 0x00}, {0x508, 0x2b}, {0x509, 0xa4},
142 {0x50a, 0x5e}, {0x50b, 0x00}, {0x50c, 0x4f}, {0x50d, 0xa4},
143 {0x50e, 0x00}, {0x50f, 0x00}, {0x512, 0x1c}, {0x514, 0x0a},
144 {0x515, 0x10}, {0x516, 0x0a}, {0x517, 0x10}, {0x51a, 0x16},
145 {0x524, 0x0f}, {0x525, 0x4f}, {0x546, 0x40}, {0x547, 0x00},
146 {0x550, 0x10}, {0x551, 0x10}, {0x559, 0x02}, {0x55a, 0x02},
147 {0x55d, 0xff}, {0x605, 0x30}, {0x608, 0x0e}, {0x609, 0x2a},
148 {0x652, 0x20}, {0x63c, 0x0a}, {0x63d, 0x0a}, {0x63e, 0x0e},
149 {0x63f, 0x0e}, {0x66e, 0x05}, {0x700, 0x21}, {0x701, 0x43},
150 {0x702, 0x65}, {0x703, 0x87}, {0x708, 0x21}, {0x709, 0x43},
151 {0x70a, 0x65}, {0x70b, 0x87}, {0xffff, 0xff},
152};
153
154static struct rtl8xxxu_reg32val rtl8723a_phy_1t_init_table[] = {
155 {0x800, 0x80040000}, {0x804, 0x00000003},
156 {0x808, 0x0000fc00}, {0x80c, 0x0000000a},
157 {0x810, 0x10001331}, {0x814, 0x020c3d10},
158 {0x818, 0x02200385}, {0x81c, 0x00000000},
159 {0x820, 0x01000100}, {0x824, 0x00390004},
160 {0x828, 0x00000000}, {0x82c, 0x00000000},
161 {0x830, 0x00000000}, {0x834, 0x00000000},
162 {0x838, 0x00000000}, {0x83c, 0x00000000},
163 {0x840, 0x00010000}, {0x844, 0x00000000},
164 {0x848, 0x00000000}, {0x84c, 0x00000000},
165 {0x850, 0x00000000}, {0x854, 0x00000000},
166 {0x858, 0x569a569a}, {0x85c, 0x001b25a4},
167 {0x860, 0x66f60110}, {0x864, 0x061f0130},
168 {0x868, 0x00000000}, {0x86c, 0x32323200},
169 {0x870, 0x07000760}, {0x874, 0x22004000},
170 {0x878, 0x00000808}, {0x87c, 0x00000000},
171 {0x880, 0xc0083070}, {0x884, 0x000004d5},
172 {0x888, 0x00000000}, {0x88c, 0xccc000c0},
173 {0x890, 0x00000800}, {0x894, 0xfffffffe},
174 {0x898, 0x40302010}, {0x89c, 0x00706050},
175 {0x900, 0x00000000}, {0x904, 0x00000023},
176 {0x908, 0x00000000}, {0x90c, 0x81121111},
177 {0xa00, 0x00d047c8}, {0xa04, 0x80ff000c},
178 {0xa08, 0x8c838300}, {0xa0c, 0x2e68120f},
179 {0xa10, 0x9500bb78}, {0xa14, 0x11144028},
180 {0xa18, 0x00881117}, {0xa1c, 0x89140f00},
181 {0xa20, 0x1a1b0000}, {0xa24, 0x090e1317},
182 {0xa28, 0x00000204}, {0xa2c, 0x00d30000},
183 {0xa70, 0x101fbf00}, {0xa74, 0x00000007},
184 {0xa78, 0x00000900},
185 {0xc00, 0x48071d40}, {0xc04, 0x03a05611},
186 {0xc08, 0x000000e4}, {0xc0c, 0x6c6c6c6c},
187 {0xc10, 0x08800000}, {0xc14, 0x40000100},
188 {0xc18, 0x08800000}, {0xc1c, 0x40000100},
189 {0xc20, 0x00000000}, {0xc24, 0x00000000},
190 {0xc28, 0x00000000}, {0xc2c, 0x00000000},
191 {0xc30, 0x69e9ac44}, {0xc34, 0x469652af},
192 {0xc38, 0x49795994}, {0xc3c, 0x0a97971c},
193 {0xc40, 0x1f7c403f}, {0xc44, 0x000100b7},
194 {0xc48, 0xec020107}, {0xc4c, 0x007f037f},
195 {0xc50, 0x69543420}, {0xc54, 0x43bc0094},
196 {0xc58, 0x69543420}, {0xc5c, 0x433c0094},
197 {0xc60, 0x00000000}, {0xc64, 0x7112848b},
198 {0xc68, 0x47c00bff}, {0xc6c, 0x00000036},
199 {0xc70, 0x2c7f000d}, {0xc74, 0x018610db},
200 {0xc78, 0x0000001f}, {0xc7c, 0x00b91612},
201 {0xc80, 0x40000100}, {0xc84, 0x20f60000},
202 {0xc88, 0x40000100}, {0xc8c, 0x20200000},
203 {0xc90, 0x00121820}, {0xc94, 0x00000000},
204 {0xc98, 0x00121820}, {0xc9c, 0x00007f7f},
205 {0xca0, 0x00000000}, {0xca4, 0x00000080},
206 {0xca8, 0x00000000}, {0xcac, 0x00000000},
207 {0xcb0, 0x00000000}, {0xcb4, 0x00000000},
208 {0xcb8, 0x00000000}, {0xcbc, 0x28000000},
209 {0xcc0, 0x00000000}, {0xcc4, 0x00000000},
210 {0xcc8, 0x00000000}, {0xccc, 0x00000000},
211 {0xcd0, 0x00000000}, {0xcd4, 0x00000000},
212 {0xcd8, 0x64b22427}, {0xcdc, 0x00766932},
213 {0xce0, 0x00222222}, {0xce4, 0x00000000},
214 {0xce8, 0x37644302}, {0xcec, 0x2f97d40c},
215 {0xd00, 0x00080740}, {0xd04, 0x00020401},
216 {0xd08, 0x0000907f}, {0xd0c, 0x20010201},
217 {0xd10, 0xa0633333}, {0xd14, 0x3333bc43},
218 {0xd18, 0x7a8f5b6b}, {0xd2c, 0xcc979975},
219 {0xd30, 0x00000000}, {0xd34, 0x80608000},
220 {0xd38, 0x00000000}, {0xd3c, 0x00027293},
221 {0xd40, 0x00000000}, {0xd44, 0x00000000},
222 {0xd48, 0x00000000}, {0xd4c, 0x00000000},
223 {0xd50, 0x6437140a}, {0xd54, 0x00000000},
224 {0xd58, 0x00000000}, {0xd5c, 0x30032064},
225 {0xd60, 0x4653de68}, {0xd64, 0x04518a3c},
226 {0xd68, 0x00002101}, {0xd6c, 0x2a201c16},
227 {0xd70, 0x1812362e}, {0xd74, 0x322c2220},
228 {0xd78, 0x000e3c24}, {0xe00, 0x2a2a2a2a},
229 {0xe04, 0x2a2a2a2a}, {0xe08, 0x03902a2a},
230 {0xe10, 0x2a2a2a2a}, {0xe14, 0x2a2a2a2a},
231 {0xe18, 0x2a2a2a2a}, {0xe1c, 0x2a2a2a2a},
232 {0xe28, 0x00000000}, {0xe30, 0x1000dc1f},
233 {0xe34, 0x10008c1f}, {0xe38, 0x02140102},
234 {0xe3c, 0x681604c2}, {0xe40, 0x01007c00},
235 {0xe44, 0x01004800}, {0xe48, 0xfb000000},
236 {0xe4c, 0x000028d1}, {0xe50, 0x1000dc1f},
237 {0xe54, 0x10008c1f}, {0xe58, 0x02140102},
238 {0xe5c, 0x28160d05}, {0xe60, 0x00000008},
239 {0xe68, 0x001b25a4}, {0xe6c, 0x631b25a0},
240 {0xe70, 0x631b25a0}, {0xe74, 0x081b25a0},
241 {0xe78, 0x081b25a0}, {0xe7c, 0x081b25a0},
242 {0xe80, 0x081b25a0}, {0xe84, 0x631b25a0},
243 {0xe88, 0x081b25a0}, {0xe8c, 0x631b25a0},
244 {0xed0, 0x631b25a0}, {0xed4, 0x631b25a0},
245 {0xed8, 0x631b25a0}, {0xedc, 0x001b25a0},
246 {0xee0, 0x001b25a0}, {0xeec, 0x6b1b25a0},
247 {0xf14, 0x00000003}, {0xf4c, 0x00000000},
248 {0xf00, 0x00000300},
249 {0xffff, 0xffffffff},
250};
251
252static struct rtl8xxxu_reg32val rtl8192cu_phy_2t_init_table[] = {
253 {0x024, 0x0011800f}, {0x028, 0x00ffdb83},
254 {0x800, 0x80040002}, {0x804, 0x00000003},
255 {0x808, 0x0000fc00}, {0x80c, 0x0000000a},
256 {0x810, 0x10000330}, {0x814, 0x020c3d10},
257 {0x818, 0x02200385}, {0x81c, 0x00000000},
258 {0x820, 0x01000100}, {0x824, 0x00390004},
259 {0x828, 0x01000100}, {0x82c, 0x00390004},
260 {0x830, 0x27272727}, {0x834, 0x27272727},
261 {0x838, 0x27272727}, {0x83c, 0x27272727},
262 {0x840, 0x00010000}, {0x844, 0x00010000},
263 {0x848, 0x27272727}, {0x84c, 0x27272727},
264 {0x850, 0x00000000}, {0x854, 0x00000000},
265 {0x858, 0x569a569a}, {0x85c, 0x0c1b25a4},
266 {0x860, 0x66e60230}, {0x864, 0x061f0130},
267 {0x868, 0x27272727}, {0x86c, 0x2b2b2b27},
268 {0x870, 0x07000700}, {0x874, 0x22184000},
269 {0x878, 0x08080808}, {0x87c, 0x00000000},
270 {0x880, 0xc0083070}, {0x884, 0x000004d5},
271 {0x888, 0x00000000}, {0x88c, 0xcc0000c0},
272 {0x890, 0x00000800}, {0x894, 0xfffffffe},
273 {0x898, 0x40302010}, {0x89c, 0x00706050},
274 {0x900, 0x00000000}, {0x904, 0x00000023},
275 {0x908, 0x00000000}, {0x90c, 0x81121313},
276 {0xa00, 0x00d047c8}, {0xa04, 0x80ff000c},
277 {0xa08, 0x8c838300}, {0xa0c, 0x2e68120f},
278 {0xa10, 0x9500bb78}, {0xa14, 0x11144028},
279 {0xa18, 0x00881117}, {0xa1c, 0x89140f00},
280 {0xa20, 0x1a1b0000}, {0xa24, 0x090e1317},
281 {0xa28, 0x00000204}, {0xa2c, 0x00d30000},
282 {0xa70, 0x101fbf00}, {0xa74, 0x00000007},
283 {0xc00, 0x48071d40}, {0xc04, 0x03a05633},
284 {0xc08, 0x000000e4}, {0xc0c, 0x6c6c6c6c},
285 {0xc10, 0x08800000}, {0xc14, 0x40000100},
286 {0xc18, 0x08800000}, {0xc1c, 0x40000100},
287 {0xc20, 0x00000000}, {0xc24, 0x00000000},
288 {0xc28, 0x00000000}, {0xc2c, 0x00000000},
289 {0xc30, 0x69e9ac44}, {0xc34, 0x469652cf},
290 {0xc38, 0x49795994}, {0xc3c, 0x0a97971c},
291 {0xc40, 0x1f7c403f}, {0xc44, 0x000100b7},
292 {0xc48, 0xec020107}, {0xc4c, 0x007f037f},
293 {0xc50, 0x69543420}, {0xc54, 0x43bc0094},
294 {0xc58, 0x69543420}, {0xc5c, 0x433c0094},
295 {0xc60, 0x00000000}, {0xc64, 0x5116848b},
296 {0xc68, 0x47c00bff}, {0xc6c, 0x00000036},
297 {0xc70, 0x2c7f000d}, {0xc74, 0x2186115b},
298 {0xc78, 0x0000001f}, {0xc7c, 0x00b99612},
299 {0xc80, 0x40000100}, {0xc84, 0x20f60000},
300 {0xc88, 0x40000100}, {0xc8c, 0xa0e40000},
301 {0xc90, 0x00121820}, {0xc94, 0x00000000},
302 {0xc98, 0x00121820}, {0xc9c, 0x00007f7f},
303 {0xca0, 0x00000000}, {0xca4, 0x00000080},
304 {0xca8, 0x00000000}, {0xcac, 0x00000000},
305 {0xcb0, 0x00000000}, {0xcb4, 0x00000000},
306 {0xcb8, 0x00000000}, {0xcbc, 0x28000000},
307 {0xcc0, 0x00000000}, {0xcc4, 0x00000000},
308 {0xcc8, 0x00000000}, {0xccc, 0x00000000},
309 {0xcd0, 0x00000000}, {0xcd4, 0x00000000},
310 {0xcd8, 0x64b22427}, {0xcdc, 0x00766932},
311 {0xce0, 0x00222222}, {0xce4, 0x00000000},
312 {0xce8, 0x37644302}, {0xcec, 0x2f97d40c},
313 {0xd00, 0x00080740}, {0xd04, 0x00020403},
314 {0xd08, 0x0000907f}, {0xd0c, 0x20010201},
315 {0xd10, 0xa0633333}, {0xd14, 0x3333bc43},
316 {0xd18, 0x7a8f5b6b}, {0xd2c, 0xcc979975},
317 {0xd30, 0x00000000}, {0xd34, 0x80608000},
318 {0xd38, 0x00000000}, {0xd3c, 0x00027293},
319 {0xd40, 0x00000000}, {0xd44, 0x00000000},
320 {0xd48, 0x00000000}, {0xd4c, 0x00000000},
321 {0xd50, 0x6437140a}, {0xd54, 0x00000000},
322 {0xd58, 0x00000000}, {0xd5c, 0x30032064},
323 {0xd60, 0x4653de68}, {0xd64, 0x04518a3c},
324 {0xd68, 0x00002101}, {0xd6c, 0x2a201c16},
325 {0xd70, 0x1812362e}, {0xd74, 0x322c2220},
326 {0xd78, 0x000e3c24}, {0xe00, 0x2a2a2a2a},
327 {0xe04, 0x2a2a2a2a}, {0xe08, 0x03902a2a},
328 {0xe10, 0x2a2a2a2a}, {0xe14, 0x2a2a2a2a},
329 {0xe18, 0x2a2a2a2a}, {0xe1c, 0x2a2a2a2a},
330 {0xe28, 0x00000000}, {0xe30, 0x1000dc1f},
331 {0xe34, 0x10008c1f}, {0xe38, 0x02140102},
332 {0xe3c, 0x681604c2}, {0xe40, 0x01007c00},
333 {0xe44, 0x01004800}, {0xe48, 0xfb000000},
334 {0xe4c, 0x000028d1}, {0xe50, 0x1000dc1f},
335 {0xe54, 0x10008c1f}, {0xe58, 0x02140102},
336 {0xe5c, 0x28160d05}, {0xe60, 0x00000010},
337 {0xe68, 0x001b25a4}, {0xe6c, 0x63db25a4},
338 {0xe70, 0x63db25a4}, {0xe74, 0x0c1b25a4},
339 {0xe78, 0x0c1b25a4}, {0xe7c, 0x0c1b25a4},
340 {0xe80, 0x0c1b25a4}, {0xe84, 0x63db25a4},
341 {0xe88, 0x0c1b25a4}, {0xe8c, 0x63db25a4},
342 {0xed0, 0x63db25a4}, {0xed4, 0x63db25a4},
343 {0xed8, 0x63db25a4}, {0xedc, 0x001b25a4},
344 {0xee0, 0x001b25a4}, {0xeec, 0x6fdb25a4},
345 {0xf14, 0x00000003}, {0xf4c, 0x00000000},
346 {0xf00, 0x00000300},
347 {0xffff, 0xffffffff},
348};
349
350static struct rtl8xxxu_reg32val rtl8188ru_phy_1t_highpa_table[] = {
351 {0x024, 0x0011800f}, {0x028, 0x00ffdb83},
352 {0x040, 0x000c0004}, {0x800, 0x80040000},
353 {0x804, 0x00000001}, {0x808, 0x0000fc00},
354 {0x80c, 0x0000000a}, {0x810, 0x10005388},
355 {0x814, 0x020c3d10}, {0x818, 0x02200385},
356 {0x81c, 0x00000000}, {0x820, 0x01000100},
357 {0x824, 0x00390204}, {0x828, 0x00000000},
358 {0x82c, 0x00000000}, {0x830, 0x00000000},
359 {0x834, 0x00000000}, {0x838, 0x00000000},
360 {0x83c, 0x00000000}, {0x840, 0x00010000},
361 {0x844, 0x00000000}, {0x848, 0x00000000},
362 {0x84c, 0x00000000}, {0x850, 0x00000000},
363 {0x854, 0x00000000}, {0x858, 0x569a569a},
364 {0x85c, 0x001b25a4}, {0x860, 0x66e60230},
365 {0x864, 0x061f0130}, {0x868, 0x00000000},
366 {0x86c, 0x20202000}, {0x870, 0x03000300},
367 {0x874, 0x22004000}, {0x878, 0x00000808},
368 {0x87c, 0x00ffc3f1}, {0x880, 0xc0083070},
369 {0x884, 0x000004d5}, {0x888, 0x00000000},
370 {0x88c, 0xccc000c0}, {0x890, 0x00000800},
371 {0x894, 0xfffffffe}, {0x898, 0x40302010},
372 {0x89c, 0x00706050}, {0x900, 0x00000000},
373 {0x904, 0x00000023}, {0x908, 0x00000000},
374 {0x90c, 0x81121111}, {0xa00, 0x00d047c8},
375 {0xa04, 0x80ff000c}, {0xa08, 0x8c838300},
376 {0xa0c, 0x2e68120f}, {0xa10, 0x9500bb78},
377 {0xa14, 0x11144028}, {0xa18, 0x00881117},
378 {0xa1c, 0x89140f00}, {0xa20, 0x15160000},
379 {0xa24, 0x070b0f12}, {0xa28, 0x00000104},
380 {0xa2c, 0x00d30000}, {0xa70, 0x101fbf00},
381 {0xa74, 0x00000007}, {0xc00, 0x48071d40},
382 {0xc04, 0x03a05611}, {0xc08, 0x000000e4},
383 {0xc0c, 0x6c6c6c6c}, {0xc10, 0x08800000},
384 {0xc14, 0x40000100}, {0xc18, 0x08800000},
385 {0xc1c, 0x40000100}, {0xc20, 0x00000000},
386 {0xc24, 0x00000000}, {0xc28, 0x00000000},
387 {0xc2c, 0x00000000}, {0xc30, 0x69e9ac44},
388 {0xc34, 0x469652cf}, {0xc38, 0x49795994},
389 {0xc3c, 0x0a97971c}, {0xc40, 0x1f7c403f},
390 {0xc44, 0x000100b7}, {0xc48, 0xec020107},
391 {0xc4c, 0x007f037f}, {0xc50, 0x6954342e},
392 {0xc54, 0x43bc0094}, {0xc58, 0x6954342f},
393 {0xc5c, 0x433c0094}, {0xc60, 0x00000000},
394 {0xc64, 0x5116848b}, {0xc68, 0x47c00bff},
395 {0xc6c, 0x00000036}, {0xc70, 0x2c46000d},
396 {0xc74, 0x018610db}, {0xc78, 0x0000001f},
397 {0xc7c, 0x00b91612}, {0xc80, 0x24000090},
398 {0xc84, 0x20f60000}, {0xc88, 0x24000090},
399 {0xc8c, 0x20200000}, {0xc90, 0x00121820},
400 {0xc94, 0x00000000}, {0xc98, 0x00121820},
401 {0xc9c, 0x00007f7f}, {0xca0, 0x00000000},
402 {0xca4, 0x00000080}, {0xca8, 0x00000000},
403 {0xcac, 0x00000000}, {0xcb0, 0x00000000},
404 {0xcb4, 0x00000000}, {0xcb8, 0x00000000},
405 {0xcbc, 0x28000000}, {0xcc0, 0x00000000},
406 {0xcc4, 0x00000000}, {0xcc8, 0x00000000},
407 {0xccc, 0x00000000}, {0xcd0, 0x00000000},
408 {0xcd4, 0x00000000}, {0xcd8, 0x64b22427},
409 {0xcdc, 0x00766932}, {0xce0, 0x00222222},
410 {0xce4, 0x00000000}, {0xce8, 0x37644302},
411 {0xcec, 0x2f97d40c}, {0xd00, 0x00080740},
412 {0xd04, 0x00020401}, {0xd08, 0x0000907f},
413 {0xd0c, 0x20010201}, {0xd10, 0xa0633333},
414 {0xd14, 0x3333bc43}, {0xd18, 0x7a8f5b6b},
415 {0xd2c, 0xcc979975}, {0xd30, 0x00000000},
416 {0xd34, 0x80608000}, {0xd38, 0x00000000},
417 {0xd3c, 0x00027293}, {0xd40, 0x00000000},
418 {0xd44, 0x00000000}, {0xd48, 0x00000000},
419 {0xd4c, 0x00000000}, {0xd50, 0x6437140a},
420 {0xd54, 0x00000000}, {0xd58, 0x00000000},
421 {0xd5c, 0x30032064}, {0xd60, 0x4653de68},
422 {0xd64, 0x04518a3c}, {0xd68, 0x00002101},
423 {0xd6c, 0x2a201c16}, {0xd70, 0x1812362e},
424 {0xd74, 0x322c2220}, {0xd78, 0x000e3c24},
425 {0xe00, 0x24242424}, {0xe04, 0x24242424},
426 {0xe08, 0x03902024}, {0xe10, 0x24242424},
427 {0xe14, 0x24242424}, {0xe18, 0x24242424},
428 {0xe1c, 0x24242424}, {0xe28, 0x00000000},
429 {0xe30, 0x1000dc1f}, {0xe34, 0x10008c1f},
430 {0xe38, 0x02140102}, {0xe3c, 0x681604c2},
431 {0xe40, 0x01007c00}, {0xe44, 0x01004800},
432 {0xe48, 0xfb000000}, {0xe4c, 0x000028d1},
433 {0xe50, 0x1000dc1f}, {0xe54, 0x10008c1f},
434 {0xe58, 0x02140102}, {0xe5c, 0x28160d05},
435 {0xe60, 0x00000008}, {0xe68, 0x001b25a4},
436 {0xe6c, 0x631b25a0}, {0xe70, 0x631b25a0},
437 {0xe74, 0x081b25a0}, {0xe78, 0x081b25a0},
438 {0xe7c, 0x081b25a0}, {0xe80, 0x081b25a0},
439 {0xe84, 0x631b25a0}, {0xe88, 0x081b25a0},
440 {0xe8c, 0x631b25a0}, {0xed0, 0x631b25a0},
441 {0xed4, 0x631b25a0}, {0xed8, 0x631b25a0},
442 {0xedc, 0x001b25a0}, {0xee0, 0x001b25a0},
443 {0xeec, 0x6b1b25a0}, {0xee8, 0x31555448},
444 {0xf14, 0x00000003}, {0xf4c, 0x00000000},
445 {0xf00, 0x00000300},
446 {0xffff, 0xffffffff},
447};
448
449static struct rtl8xxxu_reg32val rtl8xxx_agc_standard_table[] = {
450 {0xc78, 0x7b000001}, {0xc78, 0x7b010001},
451 {0xc78, 0x7b020001}, {0xc78, 0x7b030001},
452 {0xc78, 0x7b040001}, {0xc78, 0x7b050001},
453 {0xc78, 0x7a060001}, {0xc78, 0x79070001},
454 {0xc78, 0x78080001}, {0xc78, 0x77090001},
455 {0xc78, 0x760a0001}, {0xc78, 0x750b0001},
456 {0xc78, 0x740c0001}, {0xc78, 0x730d0001},
457 {0xc78, 0x720e0001}, {0xc78, 0x710f0001},
458 {0xc78, 0x70100001}, {0xc78, 0x6f110001},
459 {0xc78, 0x6e120001}, {0xc78, 0x6d130001},
460 {0xc78, 0x6c140001}, {0xc78, 0x6b150001},
461 {0xc78, 0x6a160001}, {0xc78, 0x69170001},
462 {0xc78, 0x68180001}, {0xc78, 0x67190001},
463 {0xc78, 0x661a0001}, {0xc78, 0x651b0001},
464 {0xc78, 0x641c0001}, {0xc78, 0x631d0001},
465 {0xc78, 0x621e0001}, {0xc78, 0x611f0001},
466 {0xc78, 0x60200001}, {0xc78, 0x49210001},
467 {0xc78, 0x48220001}, {0xc78, 0x47230001},
468 {0xc78, 0x46240001}, {0xc78, 0x45250001},
469 {0xc78, 0x44260001}, {0xc78, 0x43270001},
470 {0xc78, 0x42280001}, {0xc78, 0x41290001},
471 {0xc78, 0x402a0001}, {0xc78, 0x262b0001},
472 {0xc78, 0x252c0001}, {0xc78, 0x242d0001},
473 {0xc78, 0x232e0001}, {0xc78, 0x222f0001},
474 {0xc78, 0x21300001}, {0xc78, 0x20310001},
475 {0xc78, 0x06320001}, {0xc78, 0x05330001},
476 {0xc78, 0x04340001}, {0xc78, 0x03350001},
477 {0xc78, 0x02360001}, {0xc78, 0x01370001},
478 {0xc78, 0x00380001}, {0xc78, 0x00390001},
479 {0xc78, 0x003a0001}, {0xc78, 0x003b0001},
480 {0xc78, 0x003c0001}, {0xc78, 0x003d0001},
481 {0xc78, 0x003e0001}, {0xc78, 0x003f0001},
482 {0xc78, 0x7b400001}, {0xc78, 0x7b410001},
483 {0xc78, 0x7b420001}, {0xc78, 0x7b430001},
484 {0xc78, 0x7b440001}, {0xc78, 0x7b450001},
485 {0xc78, 0x7a460001}, {0xc78, 0x79470001},
486 {0xc78, 0x78480001}, {0xc78, 0x77490001},
487 {0xc78, 0x764a0001}, {0xc78, 0x754b0001},
488 {0xc78, 0x744c0001}, {0xc78, 0x734d0001},
489 {0xc78, 0x724e0001}, {0xc78, 0x714f0001},
490 {0xc78, 0x70500001}, {0xc78, 0x6f510001},
491 {0xc78, 0x6e520001}, {0xc78, 0x6d530001},
492 {0xc78, 0x6c540001}, {0xc78, 0x6b550001},
493 {0xc78, 0x6a560001}, {0xc78, 0x69570001},
494 {0xc78, 0x68580001}, {0xc78, 0x67590001},
495 {0xc78, 0x665a0001}, {0xc78, 0x655b0001},
496 {0xc78, 0x645c0001}, {0xc78, 0x635d0001},
497 {0xc78, 0x625e0001}, {0xc78, 0x615f0001},
498 {0xc78, 0x60600001}, {0xc78, 0x49610001},
499 {0xc78, 0x48620001}, {0xc78, 0x47630001},
500 {0xc78, 0x46640001}, {0xc78, 0x45650001},
501 {0xc78, 0x44660001}, {0xc78, 0x43670001},
502 {0xc78, 0x42680001}, {0xc78, 0x41690001},
503 {0xc78, 0x406a0001}, {0xc78, 0x266b0001},
504 {0xc78, 0x256c0001}, {0xc78, 0x246d0001},
505 {0xc78, 0x236e0001}, {0xc78, 0x226f0001},
506 {0xc78, 0x21700001}, {0xc78, 0x20710001},
507 {0xc78, 0x06720001}, {0xc78, 0x05730001},
508 {0xc78, 0x04740001}, {0xc78, 0x03750001},
509 {0xc78, 0x02760001}, {0xc78, 0x01770001},
510 {0xc78, 0x00780001}, {0xc78, 0x00790001},
511 {0xc78, 0x007a0001}, {0xc78, 0x007b0001},
512 {0xc78, 0x007c0001}, {0xc78, 0x007d0001},
513 {0xc78, 0x007e0001}, {0xc78, 0x007f0001},
514 {0xc78, 0x3800001e}, {0xc78, 0x3801001e},
515 {0xc78, 0x3802001e}, {0xc78, 0x3803001e},
516 {0xc78, 0x3804001e}, {0xc78, 0x3805001e},
517 {0xc78, 0x3806001e}, {0xc78, 0x3807001e},
518 {0xc78, 0x3808001e}, {0xc78, 0x3c09001e},
519 {0xc78, 0x3e0a001e}, {0xc78, 0x400b001e},
520 {0xc78, 0x440c001e}, {0xc78, 0x480d001e},
521 {0xc78, 0x4c0e001e}, {0xc78, 0x500f001e},
522 {0xc78, 0x5210001e}, {0xc78, 0x5611001e},
523 {0xc78, 0x5a12001e}, {0xc78, 0x5e13001e},
524 {0xc78, 0x6014001e}, {0xc78, 0x6015001e},
525 {0xc78, 0x6016001e}, {0xc78, 0x6217001e},
526 {0xc78, 0x6218001e}, {0xc78, 0x6219001e},
527 {0xc78, 0x621a001e}, {0xc78, 0x621b001e},
528 {0xc78, 0x621c001e}, {0xc78, 0x621d001e},
529 {0xc78, 0x621e001e}, {0xc78, 0x621f001e},
530 {0xffff, 0xffffffff}
531};
532
533static struct rtl8xxxu_reg32val rtl8xxx_agc_highpa_table[] = {
534 {0xc78, 0x7b000001}, {0xc78, 0x7b010001},
535 {0xc78, 0x7b020001}, {0xc78, 0x7b030001},
536 {0xc78, 0x7b040001}, {0xc78, 0x7b050001},
537 {0xc78, 0x7b060001}, {0xc78, 0x7b070001},
538 {0xc78, 0x7b080001}, {0xc78, 0x7a090001},
539 {0xc78, 0x790a0001}, {0xc78, 0x780b0001},
540 {0xc78, 0x770c0001}, {0xc78, 0x760d0001},
541 {0xc78, 0x750e0001}, {0xc78, 0x740f0001},
542 {0xc78, 0x73100001}, {0xc78, 0x72110001},
543 {0xc78, 0x71120001}, {0xc78, 0x70130001},
544 {0xc78, 0x6f140001}, {0xc78, 0x6e150001},
545 {0xc78, 0x6d160001}, {0xc78, 0x6c170001},
546 {0xc78, 0x6b180001}, {0xc78, 0x6a190001},
547 {0xc78, 0x691a0001}, {0xc78, 0x681b0001},
548 {0xc78, 0x671c0001}, {0xc78, 0x661d0001},
549 {0xc78, 0x651e0001}, {0xc78, 0x641f0001},
550 {0xc78, 0x63200001}, {0xc78, 0x62210001},
551 {0xc78, 0x61220001}, {0xc78, 0x60230001},
552 {0xc78, 0x46240001}, {0xc78, 0x45250001},
553 {0xc78, 0x44260001}, {0xc78, 0x43270001},
554 {0xc78, 0x42280001}, {0xc78, 0x41290001},
555 {0xc78, 0x402a0001}, {0xc78, 0x262b0001},
556 {0xc78, 0x252c0001}, {0xc78, 0x242d0001},
557 {0xc78, 0x232e0001}, {0xc78, 0x222f0001},
558 {0xc78, 0x21300001}, {0xc78, 0x20310001},
559 {0xc78, 0x06320001}, {0xc78, 0x05330001},
560 {0xc78, 0x04340001}, {0xc78, 0x03350001},
561 {0xc78, 0x02360001}, {0xc78, 0x01370001},
562 {0xc78, 0x00380001}, {0xc78, 0x00390001},
563 {0xc78, 0x003a0001}, {0xc78, 0x003b0001},
564 {0xc78, 0x003c0001}, {0xc78, 0x003d0001},
565 {0xc78, 0x003e0001}, {0xc78, 0x003f0001},
566 {0xc78, 0x7b400001}, {0xc78, 0x7b410001},
567 {0xc78, 0x7b420001}, {0xc78, 0x7b430001},
568 {0xc78, 0x7b440001}, {0xc78, 0x7b450001},
569 {0xc78, 0x7b460001}, {0xc78, 0x7b470001},
570 {0xc78, 0x7b480001}, {0xc78, 0x7a490001},
571 {0xc78, 0x794a0001}, {0xc78, 0x784b0001},
572 {0xc78, 0x774c0001}, {0xc78, 0x764d0001},
573 {0xc78, 0x754e0001}, {0xc78, 0x744f0001},
574 {0xc78, 0x73500001}, {0xc78, 0x72510001},
575 {0xc78, 0x71520001}, {0xc78, 0x70530001},
576 {0xc78, 0x6f540001}, {0xc78, 0x6e550001},
577 {0xc78, 0x6d560001}, {0xc78, 0x6c570001},
578 {0xc78, 0x6b580001}, {0xc78, 0x6a590001},
579 {0xc78, 0x695a0001}, {0xc78, 0x685b0001},
580 {0xc78, 0x675c0001}, {0xc78, 0x665d0001},
581 {0xc78, 0x655e0001}, {0xc78, 0x645f0001},
582 {0xc78, 0x63600001}, {0xc78, 0x62610001},
583 {0xc78, 0x61620001}, {0xc78, 0x60630001},
584 {0xc78, 0x46640001}, {0xc78, 0x45650001},
585 {0xc78, 0x44660001}, {0xc78, 0x43670001},
586 {0xc78, 0x42680001}, {0xc78, 0x41690001},
587 {0xc78, 0x406a0001}, {0xc78, 0x266b0001},
588 {0xc78, 0x256c0001}, {0xc78, 0x246d0001},
589 {0xc78, 0x236e0001}, {0xc78, 0x226f0001},
590 {0xc78, 0x21700001}, {0xc78, 0x20710001},
591 {0xc78, 0x06720001}, {0xc78, 0x05730001},
592 {0xc78, 0x04740001}, {0xc78, 0x03750001},
593 {0xc78, 0x02760001}, {0xc78, 0x01770001},
594 {0xc78, 0x00780001}, {0xc78, 0x00790001},
595 {0xc78, 0x007a0001}, {0xc78, 0x007b0001},
596 {0xc78, 0x007c0001}, {0xc78, 0x007d0001},
597 {0xc78, 0x007e0001}, {0xc78, 0x007f0001},
598 {0xc78, 0x3800001e}, {0xc78, 0x3801001e},
599 {0xc78, 0x3802001e}, {0xc78, 0x3803001e},
600 {0xc78, 0x3804001e}, {0xc78, 0x3805001e},
601 {0xc78, 0x3806001e}, {0xc78, 0x3807001e},
602 {0xc78, 0x3808001e}, {0xc78, 0x3c09001e},
603 {0xc78, 0x3e0a001e}, {0xc78, 0x400b001e},
604 {0xc78, 0x440c001e}, {0xc78, 0x480d001e},
605 {0xc78, 0x4c0e001e}, {0xc78, 0x500f001e},
606 {0xc78, 0x5210001e}, {0xc78, 0x5611001e},
607 {0xc78, 0x5a12001e}, {0xc78, 0x5e13001e},
608 {0xc78, 0x6014001e}, {0xc78, 0x6015001e},
609 {0xc78, 0x6016001e}, {0xc78, 0x6217001e},
610 {0xc78, 0x6218001e}, {0xc78, 0x6219001e},
611 {0xc78, 0x621a001e}, {0xc78, 0x621b001e},
612 {0xc78, 0x621c001e}, {0xc78, 0x621d001e},
613 {0xc78, 0x621e001e}, {0xc78, 0x621f001e},
614 {0xffff, 0xffffffff}
615};
616
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400617static struct rtl8xxxu_rfregs rtl8xxxu_rfregs[] = {
618 { /* RF_A */
619 .hssiparm1 = REG_FPGA0_XA_HSSI_PARM1,
620 .hssiparm2 = REG_FPGA0_XA_HSSI_PARM2,
621 .lssiparm = REG_FPGA0_XA_LSSI_PARM,
622 .hspiread = REG_HSPI_XA_READBACK,
623 .lssiread = REG_FPGA0_XA_LSSI_READBACK,
624 .rf_sw_ctrl = REG_FPGA0_XA_RF_SW_CTRL,
625 },
626 { /* RF_B */
627 .hssiparm1 = REG_FPGA0_XB_HSSI_PARM1,
628 .hssiparm2 = REG_FPGA0_XB_HSSI_PARM2,
629 .lssiparm = REG_FPGA0_XB_LSSI_PARM,
630 .hspiread = REG_HSPI_XB_READBACK,
631 .lssiread = REG_FPGA0_XB_LSSI_READBACK,
632 .rf_sw_ctrl = REG_FPGA0_XB_RF_SW_CTRL,
633 },
634};
635
Jes Sorensen599119f2016-04-28 15:19:06 -0400636const u32 rtl8xxxu_iqk_phy_iq_bb_reg[RTL8XXXU_BB_REGS] = {
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400637 REG_OFDM0_XA_RX_IQ_IMBALANCE,
638 REG_OFDM0_XB_RX_IQ_IMBALANCE,
639 REG_OFDM0_ENERGY_CCA_THRES,
640 REG_OFDM0_AGCR_SSI_TABLE,
641 REG_OFDM0_XA_TX_IQ_IMBALANCE,
642 REG_OFDM0_XB_TX_IQ_IMBALANCE,
643 REG_OFDM0_XC_TX_AFE,
644 REG_OFDM0_XD_TX_AFE,
645 REG_OFDM0_RX_IQ_EXT_ANTA
646};
647
Jes Sorensen599119f2016-04-28 15:19:06 -0400648u8 rtl8xxxu_read8(struct rtl8xxxu_priv *priv, u16 addr)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400649{
650 struct usb_device *udev = priv->udev;
651 int len;
652 u8 data;
653
654 mutex_lock(&priv->usb_buf_mutex);
655 len = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
656 REALTEK_USB_CMD_REQ, REALTEK_USB_READ,
657 addr, 0, &priv->usb_buf.val8, sizeof(u8),
658 RTW_USB_CONTROL_MSG_TIMEOUT);
659 data = priv->usb_buf.val8;
660 mutex_unlock(&priv->usb_buf_mutex);
661
662 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
663 dev_info(&udev->dev, "%s(%04x) = 0x%02x, len %i\n",
664 __func__, addr, data, len);
665 return data;
666}
667
Jes Sorensen599119f2016-04-28 15:19:06 -0400668u16 rtl8xxxu_read16(struct rtl8xxxu_priv *priv, u16 addr)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400669{
670 struct usb_device *udev = priv->udev;
671 int len;
672 u16 data;
673
674 mutex_lock(&priv->usb_buf_mutex);
675 len = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
676 REALTEK_USB_CMD_REQ, REALTEK_USB_READ,
677 addr, 0, &priv->usb_buf.val16, sizeof(u16),
678 RTW_USB_CONTROL_MSG_TIMEOUT);
679 data = le16_to_cpu(priv->usb_buf.val16);
680 mutex_unlock(&priv->usb_buf_mutex);
681
682 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
683 dev_info(&udev->dev, "%s(%04x) = 0x%04x, len %i\n",
684 __func__, addr, data, len);
685 return data;
686}
687
Jes Sorensen599119f2016-04-28 15:19:06 -0400688u32 rtl8xxxu_read32(struct rtl8xxxu_priv *priv, u16 addr)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400689{
690 struct usb_device *udev = priv->udev;
691 int len;
692 u32 data;
693
694 mutex_lock(&priv->usb_buf_mutex);
695 len = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
696 REALTEK_USB_CMD_REQ, REALTEK_USB_READ,
697 addr, 0, &priv->usb_buf.val32, sizeof(u32),
698 RTW_USB_CONTROL_MSG_TIMEOUT);
699 data = le32_to_cpu(priv->usb_buf.val32);
700 mutex_unlock(&priv->usb_buf_mutex);
701
702 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
703 dev_info(&udev->dev, "%s(%04x) = 0x%08x, len %i\n",
704 __func__, addr, data, len);
705 return data;
706}
707
Jes Sorensen599119f2016-04-28 15:19:06 -0400708int rtl8xxxu_write8(struct rtl8xxxu_priv *priv, u16 addr, u8 val)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400709{
710 struct usb_device *udev = priv->udev;
711 int ret;
712
713 mutex_lock(&priv->usb_buf_mutex);
714 priv->usb_buf.val8 = val;
715 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
716 REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
717 addr, 0, &priv->usb_buf.val8, sizeof(u8),
718 RTW_USB_CONTROL_MSG_TIMEOUT);
719
720 mutex_unlock(&priv->usb_buf_mutex);
721
722 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
723 dev_info(&udev->dev, "%s(%04x) = 0x%02x\n",
724 __func__, addr, val);
725 return ret;
726}
727
Jes Sorensen599119f2016-04-28 15:19:06 -0400728int rtl8xxxu_write16(struct rtl8xxxu_priv *priv, u16 addr, u16 val)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400729{
730 struct usb_device *udev = priv->udev;
731 int ret;
732
733 mutex_lock(&priv->usb_buf_mutex);
734 priv->usb_buf.val16 = cpu_to_le16(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.val16, sizeof(u16),
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%04x\n",
743 __func__, addr, val);
744 return ret;
745}
746
Jes Sorensen599119f2016-04-28 15:19:06 -0400747int rtl8xxxu_write32(struct rtl8xxxu_priv *priv, u16 addr, u32 val)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400748{
749 struct usb_device *udev = priv->udev;
750 int ret;
751
752 mutex_lock(&priv->usb_buf_mutex);
753 priv->usb_buf.val32 = cpu_to_le32(val);
754 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
755 REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
756 addr, 0, &priv->usb_buf.val32, sizeof(u32),
757 RTW_USB_CONTROL_MSG_TIMEOUT);
758 mutex_unlock(&priv->usb_buf_mutex);
759
760 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
761 dev_info(&udev->dev, "%s(%04x) = 0x%08x\n",
762 __func__, addr, val);
763 return ret;
764}
765
766static int
767rtl8xxxu_writeN(struct rtl8xxxu_priv *priv, u16 addr, u8 *buf, u16 len)
768{
769 struct usb_device *udev = priv->udev;
770 int blocksize = priv->fops->writeN_block_size;
771 int ret, i, count, remainder;
772
773 count = len / blocksize;
774 remainder = len % blocksize;
775
776 for (i = 0; i < count; i++) {
777 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
778 REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
779 addr, 0, buf, blocksize,
780 RTW_USB_CONTROL_MSG_TIMEOUT);
781 if (ret != blocksize)
782 goto write_error;
783
784 addr += blocksize;
785 buf += blocksize;
786 }
787
788 if (remainder) {
789 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
790 REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
791 addr, 0, buf, remainder,
792 RTW_USB_CONTROL_MSG_TIMEOUT);
793 if (ret != remainder)
794 goto write_error;
795 }
796
797 return len;
798
799write_error:
800 dev_info(&udev->dev,
801 "%s: Failed to write block at addr: %04x size: %04x\n",
802 __func__, addr, blocksize);
803 return -EAGAIN;
804}
805
Jes Sorensen599119f2016-04-28 15:19:06 -0400806u32 rtl8xxxu_read_rfreg(struct rtl8xxxu_priv *priv,
807 enum rtl8xxxu_rfpath path, u8 reg)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400808{
809 u32 hssia, val32, retval;
810
811 hssia = rtl8xxxu_read32(priv, REG_FPGA0_XA_HSSI_PARM2);
812 if (path != RF_A)
813 val32 = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].hssiparm2);
814 else
815 val32 = hssia;
816
817 val32 &= ~FPGA0_HSSI_PARM2_ADDR_MASK;
818 val32 |= (reg << FPGA0_HSSI_PARM2_ADDR_SHIFT);
819 val32 |= FPGA0_HSSI_PARM2_EDGE_READ;
820 hssia &= ~FPGA0_HSSI_PARM2_EDGE_READ;
821 rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM2, hssia);
822
823 udelay(10);
824
825 rtl8xxxu_write32(priv, rtl8xxxu_rfregs[path].hssiparm2, val32);
826 udelay(100);
827
828 hssia |= FPGA0_HSSI_PARM2_EDGE_READ;
829 rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM2, hssia);
830 udelay(10);
831
832 val32 = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].hssiparm1);
833 if (val32 & FPGA0_HSSI_PARM1_PI)
834 retval = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].hspiread);
835 else
836 retval = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].lssiread);
837
838 retval &= 0xfffff;
839
840 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_RFREG_READ)
841 dev_info(&priv->udev->dev, "%s(%02x) = 0x%06x\n",
842 __func__, reg, retval);
843 return retval;
844}
845
Jes Sorensen22a31d42016-02-29 17:04:15 -0500846/*
847 * The RTL8723BU driver indicates that registers 0xb2 and 0xb6 can
848 * have write issues in high temperature conditions. We may have to
849 * retry writing them.
850 */
Jes Sorensen599119f2016-04-28 15:19:06 -0400851int rtl8xxxu_write_rfreg(struct rtl8xxxu_priv *priv,
852 enum rtl8xxxu_rfpath path, u8 reg, u32 data)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400853{
854 int ret, retval;
Jes Sorensen2949b9e2016-04-07 14:19:25 -0400855 u32 dataaddr, val32;
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400856
857 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_RFREG_WRITE)
858 dev_info(&priv->udev->dev, "%s(%02x) = 0x%06x\n",
859 __func__, reg, data);
860
861 data &= FPGA0_LSSI_PARM_DATA_MASK;
862 dataaddr = (reg << FPGA0_LSSI_PARM_ADDR_SHIFT) | data;
863
Jes Sorensen2949b9e2016-04-07 14:19:25 -0400864 if (priv->rtl_chip == RTL8192E) {
865 val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
866 val32 &= ~0x20000;
867 rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
868 }
869
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400870 /* Use XB for path B */
871 ret = rtl8xxxu_write32(priv, rtl8xxxu_rfregs[path].lssiparm, dataaddr);
872 if (ret != sizeof(dataaddr))
873 retval = -EIO;
874 else
875 retval = 0;
876
877 udelay(1);
878
Jes Sorensen2949b9e2016-04-07 14:19:25 -0400879 if (priv->rtl_chip == RTL8192E) {
880 val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
881 val32 |= 0x20000;
882 rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
883 }
884
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400885 return retval;
886}
887
Jes Sorensen9c0343d2016-04-28 15:19:13 -0400888int
889rtl8xxxu_gen1_h2c_cmd(struct rtl8xxxu_priv *priv, struct h2c_cmd *h2c, int len)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400890{
891 struct device *dev = &priv->udev->dev;
892 int mbox_nr, retry, retval = 0;
893 int mbox_reg, mbox_ext_reg;
894 u8 val8;
895
896 mutex_lock(&priv->h2c_mutex);
897
898 mbox_nr = priv->next_mbox;
899 mbox_reg = REG_HMBOX_0 + (mbox_nr * 4);
Jes Sorensen9c0343d2016-04-28 15:19:13 -0400900 mbox_ext_reg = REG_HMBOX_EXT_0 + (mbox_nr * 2);
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400901
902 /*
903 * MBOX ready?
904 */
905 retry = 100;
906 do {
907 val8 = rtl8xxxu_read8(priv, REG_HMTFR);
908 if (!(val8 & BIT(mbox_nr)))
909 break;
910 } while (retry--);
911
912 if (!retry) {
Jes Sorensenc7a5a192016-02-29 17:04:30 -0500913 dev_info(dev, "%s: Mailbox busy\n", __func__);
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400914 retval = -EBUSY;
915 goto error;
916 }
917
918 /*
919 * Need to swap as it's being swapped again by rtl8xxxu_write16/32()
920 */
Jes Sorensen8da91572016-02-29 17:04:29 -0500921 if (len > sizeof(u32)) {
Jes Sorensen9c0343d2016-04-28 15:19:13 -0400922 rtl8xxxu_write16(priv, mbox_ext_reg, le16_to_cpu(h2c->raw.ext));
923 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
924 dev_info(dev, "H2C_EXT %04x\n",
925 le16_to_cpu(h2c->raw.ext));
926 }
927 rtl8xxxu_write32(priv, mbox_reg, le32_to_cpu(h2c->raw.data));
928 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
929 dev_info(dev, "H2C %08x\n", le32_to_cpu(h2c->raw.data));
930
931 priv->next_mbox = (mbox_nr + 1) % H2C_MAX_MBOX;
932
933error:
934 mutex_unlock(&priv->h2c_mutex);
935 return retval;
936}
937
938int
939rtl8xxxu_gen2_h2c_cmd(struct rtl8xxxu_priv *priv, struct h2c_cmd *h2c, int len)
940{
941 struct device *dev = &priv->udev->dev;
942 int mbox_nr, retry, retval = 0;
943 int mbox_reg, mbox_ext_reg;
944 u8 val8;
945
946 mutex_lock(&priv->h2c_mutex);
947
948 mbox_nr = priv->next_mbox;
949 mbox_reg = REG_HMBOX_0 + (mbox_nr * 4);
950 mbox_ext_reg = REG_HMBOX_EXT0_8723B + (mbox_nr * 4);
951
952 /*
953 * MBOX ready?
954 */
955 retry = 100;
956 do {
957 val8 = rtl8xxxu_read8(priv, REG_HMTFR);
958 if (!(val8 & BIT(mbox_nr)))
959 break;
960 } while (retry--);
961
962 if (!retry) {
963 dev_info(dev, "%s: Mailbox busy\n", __func__);
964 retval = -EBUSY;
965 goto error;
966 }
967
968 /*
969 * Need to swap as it's being swapped again by rtl8xxxu_write16/32()
970 */
971 if (len > sizeof(u32)) {
972 rtl8xxxu_write32(priv, mbox_ext_reg,
973 le32_to_cpu(h2c->raw_wide.ext));
974 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
975 dev_info(dev, "H2C_EXT %08x\n",
976 le32_to_cpu(h2c->raw_wide.ext));
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400977 }
978 rtl8xxxu_write32(priv, mbox_reg, le32_to_cpu(h2c->raw.data));
979 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
980 dev_info(dev, "H2C %08x\n", le32_to_cpu(h2c->raw.data));
981
982 priv->next_mbox = (mbox_nr + 1) % H2C_MAX_MBOX;
983
984error:
985 mutex_unlock(&priv->h2c_mutex);
986 return retval;
987}
988
Jes Sorensen20e3b2e2016-04-28 15:19:08 -0400989void rtl8xxxu_gen1_enable_rf(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400990{
991 u8 val8;
992 u32 val32;
993
994 val8 = rtl8xxxu_read8(priv, REG_SPS0_CTRL);
995 val8 |= BIT(0) | BIT(3);
996 rtl8xxxu_write8(priv, REG_SPS0_CTRL, val8);
997
998 val32 = rtl8xxxu_read32(priv, REG_FPGA0_XAB_RF_PARM);
999 val32 &= ~(BIT(4) | BIT(5));
1000 val32 |= BIT(3);
1001 if (priv->rf_paths == 2) {
1002 val32 &= ~(BIT(20) | BIT(21));
1003 val32 |= BIT(19);
1004 }
1005 rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_PARM, val32);
1006
1007 val32 = rtl8xxxu_read32(priv, REG_OFDM0_TRX_PATH_ENABLE);
1008 val32 &= ~OFDM_RF_PATH_TX_MASK;
1009 if (priv->tx_paths == 2)
1010 val32 |= OFDM_RF_PATH_TX_A | OFDM_RF_PATH_TX_B;
Jes Sorensenba17d822016-03-31 17:08:39 -04001011 else if (priv->rtl_chip == RTL8192C || priv->rtl_chip == RTL8191C)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001012 val32 |= OFDM_RF_PATH_TX_B;
1013 else
1014 val32 |= OFDM_RF_PATH_TX_A;
1015 rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, val32);
1016
1017 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1018 val32 &= ~FPGA_RF_MODE_JAPAN;
1019 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1020
1021 if (priv->rf_paths == 2)
1022 rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x63db25a0);
1023 else
1024 rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x631b25a0);
1025
1026 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC, 0x32d95);
1027 if (priv->rf_paths == 2)
1028 rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC, 0x32d95);
1029
1030 rtl8xxxu_write8(priv, REG_TXPAUSE, 0x00);
1031}
1032
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04001033void rtl8xxxu_gen1_disable_rf(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001034{
1035 u8 sps0;
1036 u32 val32;
1037
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001038 sps0 = rtl8xxxu_read8(priv, REG_SPS0_CTRL);
1039
1040 /* RF RX code for preamble power saving */
1041 val32 = rtl8xxxu_read32(priv, REG_FPGA0_XAB_RF_PARM);
1042 val32 &= ~(BIT(3) | BIT(4) | BIT(5));
1043 if (priv->rf_paths == 2)
1044 val32 &= ~(BIT(19) | BIT(20) | BIT(21));
1045 rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_PARM, val32);
1046
1047 /* Disable TX for four paths */
1048 val32 = rtl8xxxu_read32(priv, REG_OFDM0_TRX_PATH_ENABLE);
1049 val32 &= ~OFDM_RF_PATH_TX_MASK;
1050 rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, val32);
1051
1052 /* Enable power saving */
1053 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1054 val32 |= FPGA_RF_MODE_JAPAN;
1055 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1056
1057 /* AFE control register to power down bits [30:22] */
1058 if (priv->rf_paths == 2)
1059 rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x00db25a0);
1060 else
1061 rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x001b25a0);
1062
1063 /* Power down RF module */
1064 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC, 0);
1065 if (priv->rf_paths == 2)
1066 rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC, 0);
1067
1068 sps0 &= ~(BIT(0) | BIT(3));
1069 rtl8xxxu_write8(priv, REG_SPS0_CTRL, sps0);
1070}
1071
Jes Sorensen97db5a82016-04-28 15:19:10 -04001072static void rtl8xxxu_stop_tx_beacon(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001073{
1074 u8 val8;
1075
1076 val8 = rtl8xxxu_read8(priv, REG_FWHW_TXQ_CTRL + 2);
1077 val8 &= ~BIT(6);
1078 rtl8xxxu_write8(priv, REG_FWHW_TXQ_CTRL + 2, val8);
1079
1080 rtl8xxxu_write8(priv, REG_TBTT_PROHIBIT + 1, 0x64);
1081 val8 = rtl8xxxu_read8(priv, REG_TBTT_PROHIBIT + 2);
1082 val8 &= ~BIT(0);
1083 rtl8xxxu_write8(priv, REG_TBTT_PROHIBIT + 2, val8);
1084}
1085
1086
1087/*
1088 * The rtl8723a has 3 channel groups for it's efuse settings. It only
1089 * supports the 2.4GHz band, so channels 1 - 14:
1090 * group 0: channels 1 - 3
1091 * group 1: channels 4 - 9
1092 * group 2: channels 10 - 14
1093 *
1094 * Note: We index from 0 in the code
1095 */
Jes Sorensena46b0992016-04-28 15:19:11 -04001096static int rtl8xxxu_gen1_channel_to_group(int channel)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001097{
1098 int group;
1099
1100 if (channel < 4)
1101 group = 0;
1102 else if (channel < 10)
1103 group = 1;
1104 else
1105 group = 2;
1106
1107 return group;
1108}
1109
Jes Sorensen9e247722016-04-07 14:19:23 -04001110/*
1111 * Valid for rtl8723bu and rtl8192eu
1112 */
Jes Sorensen599119f2016-04-28 15:19:06 -04001113int rtl8xxxu_gen2_channel_to_group(int channel)
Jes Sorensene796dab2016-02-29 17:05:19 -05001114{
1115 int group;
1116
1117 if (channel < 3)
1118 group = 0;
1119 else if (channel < 6)
1120 group = 1;
1121 else if (channel < 9)
1122 group = 2;
1123 else if (channel < 12)
1124 group = 3;
1125 else
1126 group = 4;
1127
1128 return group;
1129}
1130
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04001131void rtl8xxxu_gen1_config_channel(struct ieee80211_hw *hw)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001132{
1133 struct rtl8xxxu_priv *priv = hw->priv;
1134 u32 val32, rsr;
1135 u8 val8, opmode;
1136 bool ht = true;
1137 int sec_ch_above, channel;
1138 int i;
1139
1140 opmode = rtl8xxxu_read8(priv, REG_BW_OPMODE);
1141 rsr = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
1142 channel = hw->conf.chandef.chan->hw_value;
1143
1144 switch (hw->conf.chandef.width) {
1145 case NL80211_CHAN_WIDTH_20_NOHT:
1146 ht = false;
1147 case NL80211_CHAN_WIDTH_20:
1148 opmode |= BW_OPMODE_20MHZ;
1149 rtl8xxxu_write8(priv, REG_BW_OPMODE, opmode);
1150
1151 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1152 val32 &= ~FPGA_RF_MODE;
1153 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1154
1155 val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
1156 val32 &= ~FPGA_RF_MODE;
1157 rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
1158
1159 val32 = rtl8xxxu_read32(priv, REG_FPGA0_ANALOG2);
1160 val32 |= FPGA0_ANALOG2_20MHZ;
1161 rtl8xxxu_write32(priv, REG_FPGA0_ANALOG2, val32);
1162 break;
1163 case NL80211_CHAN_WIDTH_40:
1164 if (hw->conf.chandef.center_freq1 >
1165 hw->conf.chandef.chan->center_freq) {
1166 sec_ch_above = 1;
1167 channel += 2;
1168 } else {
1169 sec_ch_above = 0;
1170 channel -= 2;
1171 }
1172
1173 opmode &= ~BW_OPMODE_20MHZ;
1174 rtl8xxxu_write8(priv, REG_BW_OPMODE, opmode);
1175 rsr &= ~RSR_RSC_BANDWIDTH_40M;
1176 if (sec_ch_above)
1177 rsr |= RSR_RSC_UPPER_SUB_CHANNEL;
1178 else
1179 rsr |= RSR_RSC_LOWER_SUB_CHANNEL;
1180 rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, rsr);
1181
1182 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1183 val32 |= FPGA_RF_MODE;
1184 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1185
1186 val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
1187 val32 |= FPGA_RF_MODE;
1188 rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
1189
1190 /*
1191 * Set Control channel to upper or lower. These settings
1192 * are required only for 40MHz
1193 */
1194 val32 = rtl8xxxu_read32(priv, REG_CCK0_SYSTEM);
1195 val32 &= ~CCK0_SIDEBAND;
1196 if (!sec_ch_above)
1197 val32 |= CCK0_SIDEBAND;
1198 rtl8xxxu_write32(priv, REG_CCK0_SYSTEM, val32);
1199
1200 val32 = rtl8xxxu_read32(priv, REG_OFDM1_LSTF);
1201 val32 &= ~OFDM_LSTF_PRIME_CH_MASK; /* 0xc00 */
1202 if (sec_ch_above)
1203 val32 |= OFDM_LSTF_PRIME_CH_LOW;
1204 else
1205 val32 |= OFDM_LSTF_PRIME_CH_HIGH;
1206 rtl8xxxu_write32(priv, REG_OFDM1_LSTF, val32);
1207
1208 val32 = rtl8xxxu_read32(priv, REG_FPGA0_ANALOG2);
1209 val32 &= ~FPGA0_ANALOG2_20MHZ;
1210 rtl8xxxu_write32(priv, REG_FPGA0_ANALOG2, val32);
1211
1212 val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
1213 val32 &= ~(FPGA0_PS_LOWER_CHANNEL | FPGA0_PS_UPPER_CHANNEL);
1214 if (sec_ch_above)
1215 val32 |= FPGA0_PS_UPPER_CHANNEL;
1216 else
1217 val32 |= FPGA0_PS_LOWER_CHANNEL;
1218 rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
1219 break;
1220
1221 default:
1222 break;
1223 }
1224
1225 for (i = RF_A; i < priv->rf_paths; i++) {
1226 val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
1227 val32 &= ~MODE_AG_CHANNEL_MASK;
1228 val32 |= channel;
1229 rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
1230 }
1231
1232 if (ht)
1233 val8 = 0x0e;
1234 else
1235 val8 = 0x0a;
1236
1237 rtl8xxxu_write8(priv, REG_SIFS_CCK + 1, val8);
1238 rtl8xxxu_write8(priv, REG_SIFS_OFDM + 1, val8);
1239
1240 rtl8xxxu_write16(priv, REG_R2T_SIFS, 0x0808);
1241 rtl8xxxu_write16(priv, REG_T2T_SIFS, 0x0a0a);
1242
1243 for (i = RF_A; i < priv->rf_paths; i++) {
1244 val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
1245 if (hw->conf.chandef.width == NL80211_CHAN_WIDTH_40)
1246 val32 &= ~MODE_AG_CHANNEL_20MHZ;
1247 else
1248 val32 |= MODE_AG_CHANNEL_20MHZ;
1249 rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
1250 }
1251}
1252
Jes Sorensen599119f2016-04-28 15:19:06 -04001253void rtl8xxxu_gen2_config_channel(struct ieee80211_hw *hw)
Jes Sorensenc3f95062016-02-29 17:04:40 -05001254{
1255 struct rtl8xxxu_priv *priv = hw->priv;
1256 u32 val32, rsr;
Jes Sorensen368633c2016-02-29 17:04:42 -05001257 u8 val8, subchannel;
Jes Sorensenc3f95062016-02-29 17:04:40 -05001258 u16 rf_mode_bw;
1259 bool ht = true;
1260 int sec_ch_above, channel;
1261 int i;
1262
1263 rf_mode_bw = rtl8xxxu_read16(priv, REG_WMAC_TRXPTCL_CTL);
1264 rf_mode_bw &= ~WMAC_TRXPTCL_CTL_BW_MASK;
1265 rsr = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
1266 channel = hw->conf.chandef.chan->hw_value;
1267
1268/* Hack */
1269 subchannel = 0;
1270
1271 switch (hw->conf.chandef.width) {
1272 case NL80211_CHAN_WIDTH_20_NOHT:
1273 ht = false;
1274 case NL80211_CHAN_WIDTH_20:
1275 rf_mode_bw |= WMAC_TRXPTCL_CTL_BW_20;
1276 subchannel = 0;
1277
1278 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1279 val32 &= ~FPGA_RF_MODE;
1280 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1281
1282 val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
1283 val32 &= ~FPGA_RF_MODE;
1284 rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
1285
1286 val32 = rtl8xxxu_read32(priv, REG_OFDM0_TX_PSDO_NOISE_WEIGHT);
1287 val32 &= ~(BIT(30) | BIT(31));
1288 rtl8xxxu_write32(priv, REG_OFDM0_TX_PSDO_NOISE_WEIGHT, val32);
1289
1290 break;
1291 case NL80211_CHAN_WIDTH_40:
1292 rf_mode_bw |= WMAC_TRXPTCL_CTL_BW_40;
1293
1294 if (hw->conf.chandef.center_freq1 >
1295 hw->conf.chandef.chan->center_freq) {
1296 sec_ch_above = 1;
1297 channel += 2;
1298 } else {
1299 sec_ch_above = 0;
1300 channel -= 2;
1301 }
1302
1303 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1304 val32 |= FPGA_RF_MODE;
1305 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1306
1307 val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
1308 val32 |= FPGA_RF_MODE;
1309 rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
1310
1311 /*
1312 * Set Control channel to upper or lower. These settings
1313 * are required only for 40MHz
1314 */
1315 val32 = rtl8xxxu_read32(priv, REG_CCK0_SYSTEM);
1316 val32 &= ~CCK0_SIDEBAND;
1317 if (!sec_ch_above)
1318 val32 |= CCK0_SIDEBAND;
1319 rtl8xxxu_write32(priv, REG_CCK0_SYSTEM, val32);
1320
1321 val32 = rtl8xxxu_read32(priv, REG_OFDM1_LSTF);
1322 val32 &= ~OFDM_LSTF_PRIME_CH_MASK; /* 0xc00 */
1323 if (sec_ch_above)
1324 val32 |= OFDM_LSTF_PRIME_CH_LOW;
1325 else
1326 val32 |= OFDM_LSTF_PRIME_CH_HIGH;
1327 rtl8xxxu_write32(priv, REG_OFDM1_LSTF, val32);
1328
1329 val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
1330 val32 &= ~(FPGA0_PS_LOWER_CHANNEL | FPGA0_PS_UPPER_CHANNEL);
1331 if (sec_ch_above)
1332 val32 |= FPGA0_PS_UPPER_CHANNEL;
1333 else
1334 val32 |= FPGA0_PS_LOWER_CHANNEL;
1335 rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
1336 break;
1337 case NL80211_CHAN_WIDTH_80:
1338 rf_mode_bw |= WMAC_TRXPTCL_CTL_BW_80;
1339 break;
1340 default:
1341 break;
1342 }
1343
1344 for (i = RF_A; i < priv->rf_paths; i++) {
1345 val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
1346 val32 &= ~MODE_AG_CHANNEL_MASK;
1347 val32 |= channel;
1348 rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
1349 }
1350
1351 rtl8xxxu_write16(priv, REG_WMAC_TRXPTCL_CTL, rf_mode_bw);
1352 rtl8xxxu_write8(priv, REG_DATA_SUBCHANNEL, subchannel);
1353
1354 if (ht)
1355 val8 = 0x0e;
1356 else
1357 val8 = 0x0a;
1358
1359 rtl8xxxu_write8(priv, REG_SIFS_CCK + 1, val8);
1360 rtl8xxxu_write8(priv, REG_SIFS_OFDM + 1, val8);
1361
1362 rtl8xxxu_write16(priv, REG_R2T_SIFS, 0x0808);
1363 rtl8xxxu_write16(priv, REG_T2T_SIFS, 0x0a0a);
1364
1365 for (i = RF_A; i < priv->rf_paths; i++) {
1366 val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
1367 val32 &= ~MODE_AG_BW_MASK;
1368 switch(hw->conf.chandef.width) {
1369 case NL80211_CHAN_WIDTH_80:
1370 val32 |= MODE_AG_BW_80MHZ_8723B;
1371 break;
1372 case NL80211_CHAN_WIDTH_40:
1373 val32 |= MODE_AG_BW_40MHZ_8723B;
1374 break;
1375 default:
1376 val32 |= MODE_AG_BW_20MHZ_8723B;
1377 break;
1378 }
1379 rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
1380 }
1381}
1382
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04001383void
Jes Sorensen42a3bc72016-04-18 11:49:31 -04001384rtl8xxxu_gen1_set_tx_power(struct rtl8xxxu_priv *priv, int channel, bool ht40)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001385{
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001386 struct rtl8xxxu_power_base *power_base = priv->power_base;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001387 u8 cck[RTL8723A_MAX_RF_PATHS], ofdm[RTL8723A_MAX_RF_PATHS];
1388 u8 ofdmbase[RTL8723A_MAX_RF_PATHS], mcsbase[RTL8723A_MAX_RF_PATHS];
1389 u32 val32, ofdm_a, ofdm_b, mcs_a, mcs_b;
1390 u8 val8;
1391 int group, i;
1392
Jes Sorensena46b0992016-04-28 15:19:11 -04001393 group = rtl8xxxu_gen1_channel_to_group(channel);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001394
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001395 cck[0] = priv->cck_tx_power_index_A[group] - 1;
1396 cck[1] = priv->cck_tx_power_index_B[group] - 1;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001397
Jes Sorensenb591e982016-04-14 16:37:09 -04001398 if (priv->hi_pa) {
1399 if (cck[0] > 0x20)
1400 cck[0] = 0x20;
1401 if (cck[1] > 0x20)
1402 cck[1] = 0x20;
1403 }
1404
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001405 ofdm[0] = priv->ht40_1s_tx_power_index_A[group];
1406 ofdm[1] = priv->ht40_1s_tx_power_index_B[group];
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001407 if (ofdm[0])
1408 ofdm[0] -= 1;
1409 if (ofdm[1])
1410 ofdm[1] -= 1;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001411
1412 ofdmbase[0] = ofdm[0] + priv->ofdm_tx_power_index_diff[group].a;
1413 ofdmbase[1] = ofdm[1] + priv->ofdm_tx_power_index_diff[group].b;
1414
1415 mcsbase[0] = ofdm[0];
1416 mcsbase[1] = ofdm[1];
1417 if (!ht40) {
1418 mcsbase[0] += priv->ht20_tx_power_index_diff[group].a;
1419 mcsbase[1] += priv->ht20_tx_power_index_diff[group].b;
1420 }
1421
1422 if (priv->tx_paths > 1) {
1423 if (ofdm[0] > priv->ht40_2s_tx_power_index_diff[group].a)
1424 ofdm[0] -= priv->ht40_2s_tx_power_index_diff[group].a;
1425 if (ofdm[1] > priv->ht40_2s_tx_power_index_diff[group].b)
1426 ofdm[1] -= priv->ht40_2s_tx_power_index_diff[group].b;
1427 }
1428
1429 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_CHANNEL)
1430 dev_info(&priv->udev->dev,
1431 "%s: Setting TX power CCK A: %02x, "
1432 "CCK B: %02x, OFDM A: %02x, OFDM B: %02x\n",
1433 __func__, cck[0], cck[1], ofdm[0], ofdm[1]);
1434
1435 for (i = 0; i < RTL8723A_MAX_RF_PATHS; i++) {
1436 if (cck[i] > RF6052_MAX_TX_PWR)
1437 cck[i] = RF6052_MAX_TX_PWR;
1438 if (ofdm[i] > RF6052_MAX_TX_PWR)
1439 ofdm[i] = RF6052_MAX_TX_PWR;
1440 }
1441
1442 val32 = rtl8xxxu_read32(priv, REG_TX_AGC_A_CCK1_MCS32);
1443 val32 &= 0xffff00ff;
1444 val32 |= (cck[0] << 8);
1445 rtl8xxxu_write32(priv, REG_TX_AGC_A_CCK1_MCS32, val32);
1446
1447 val32 = rtl8xxxu_read32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11);
1448 val32 &= 0xff;
1449 val32 |= ((cck[0] << 8) | (cck[0] << 16) | (cck[0] << 24));
1450 rtl8xxxu_write32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11, val32);
1451
1452 val32 = rtl8xxxu_read32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11);
1453 val32 &= 0xffffff00;
1454 val32 |= cck[1];
1455 rtl8xxxu_write32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11, val32);
1456
1457 val32 = rtl8xxxu_read32(priv, REG_TX_AGC_B_CCK1_55_MCS32);
1458 val32 &= 0xff;
1459 val32 |= ((cck[1] << 8) | (cck[1] << 16) | (cck[1] << 24));
1460 rtl8xxxu_write32(priv, REG_TX_AGC_B_CCK1_55_MCS32, val32);
1461
1462 ofdm_a = ofdmbase[0] | ofdmbase[0] << 8 |
1463 ofdmbase[0] << 16 | ofdmbase[0] << 24;
1464 ofdm_b = ofdmbase[1] | ofdmbase[1] << 8 |
1465 ofdmbase[1] << 16 | ofdmbase[1] << 24;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001466
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001467 rtl8xxxu_write32(priv, REG_TX_AGC_A_RATE18_06,
1468 ofdm_a + power_base->reg_0e00);
1469 rtl8xxxu_write32(priv, REG_TX_AGC_B_RATE18_06,
1470 ofdm_b + power_base->reg_0830);
1471
1472 rtl8xxxu_write32(priv, REG_TX_AGC_A_RATE54_24,
1473 ofdm_a + power_base->reg_0e04);
1474 rtl8xxxu_write32(priv, REG_TX_AGC_B_RATE54_24,
1475 ofdm_b + power_base->reg_0834);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001476
1477 mcs_a = mcsbase[0] | mcsbase[0] << 8 |
1478 mcsbase[0] << 16 | mcsbase[0] << 24;
1479 mcs_b = mcsbase[1] | mcsbase[1] << 8 |
1480 mcsbase[1] << 16 | mcsbase[1] << 24;
1481
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001482 rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS03_MCS00,
1483 mcs_a + power_base->reg_0e10);
1484 rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS03_MCS00,
1485 mcs_b + power_base->reg_083c);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001486
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001487 rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS07_MCS04,
1488 mcs_a + power_base->reg_0e14);
1489 rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS07_MCS04,
1490 mcs_b + power_base->reg_0848);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001491
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001492 rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS11_MCS08,
1493 mcs_a + power_base->reg_0e18);
1494 rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS11_MCS08,
1495 mcs_b + power_base->reg_084c);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001496
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001497 rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS15_MCS12,
1498 mcs_a + power_base->reg_0e1c);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001499 for (i = 0; i < 3; i++) {
1500 if (i != 2)
1501 val8 = (mcsbase[0] > 8) ? (mcsbase[0] - 8) : 0;
1502 else
1503 val8 = (mcsbase[0] > 6) ? (mcsbase[0] - 6) : 0;
1504 rtl8xxxu_write8(priv, REG_OFDM0_XC_TX_IQ_IMBALANCE + i, val8);
1505 }
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001506 rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS15_MCS12,
1507 mcs_b + power_base->reg_0868);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001508 for (i = 0; i < 3; i++) {
1509 if (i != 2)
1510 val8 = (mcsbase[1] > 8) ? (mcsbase[1] - 8) : 0;
1511 else
1512 val8 = (mcsbase[1] > 6) ? (mcsbase[1] - 6) : 0;
1513 rtl8xxxu_write8(priv, REG_OFDM0_XD_TX_IQ_IMBALANCE + i, val8);
1514 }
1515}
1516
1517static void rtl8xxxu_set_linktype(struct rtl8xxxu_priv *priv,
1518 enum nl80211_iftype linktype)
1519{
Jes Sorensena26703f2016-02-03 13:39:56 -05001520 u8 val8;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001521
Jes Sorensena26703f2016-02-03 13:39:56 -05001522 val8 = rtl8xxxu_read8(priv, REG_MSR);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001523 val8 &= ~MSR_LINKTYPE_MASK;
1524
1525 switch (linktype) {
1526 case NL80211_IFTYPE_UNSPECIFIED:
1527 val8 |= MSR_LINKTYPE_NONE;
1528 break;
1529 case NL80211_IFTYPE_ADHOC:
1530 val8 |= MSR_LINKTYPE_ADHOC;
1531 break;
1532 case NL80211_IFTYPE_STATION:
1533 val8 |= MSR_LINKTYPE_STATION;
1534 break;
1535 case NL80211_IFTYPE_AP:
1536 val8 |= MSR_LINKTYPE_AP;
1537 break;
1538 default:
1539 goto out;
1540 }
1541
1542 rtl8xxxu_write8(priv, REG_MSR, val8);
1543out:
1544 return;
1545}
1546
1547static void
1548rtl8xxxu_set_retry(struct rtl8xxxu_priv *priv, u16 short_retry, u16 long_retry)
1549{
1550 u16 val16;
1551
1552 val16 = ((short_retry << RETRY_LIMIT_SHORT_SHIFT) &
1553 RETRY_LIMIT_SHORT_MASK) |
1554 ((long_retry << RETRY_LIMIT_LONG_SHIFT) &
1555 RETRY_LIMIT_LONG_MASK);
1556
1557 rtl8xxxu_write16(priv, REG_RETRY_LIMIT, val16);
1558}
1559
1560static void
1561rtl8xxxu_set_spec_sifs(struct rtl8xxxu_priv *priv, u16 cck, u16 ofdm)
1562{
1563 u16 val16;
1564
1565 val16 = ((cck << SPEC_SIFS_CCK_SHIFT) & SPEC_SIFS_CCK_MASK) |
1566 ((ofdm << SPEC_SIFS_OFDM_SHIFT) & SPEC_SIFS_OFDM_MASK);
1567
1568 rtl8xxxu_write16(priv, REG_SPEC_SIFS, val16);
1569}
1570
1571static void rtl8xxxu_print_chipinfo(struct rtl8xxxu_priv *priv)
1572{
1573 struct device *dev = &priv->udev->dev;
1574 char *cut;
1575
1576 switch (priv->chip_cut) {
1577 case 0:
1578 cut = "A";
1579 break;
1580 case 1:
1581 cut = "B";
1582 break;
Jes Sorensen0e5d4352016-02-29 17:04:00 -05001583 case 2:
1584 cut = "C";
1585 break;
1586 case 3:
1587 cut = "D";
1588 break;
1589 case 4:
1590 cut = "E";
1591 break;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001592 default:
1593 cut = "unknown";
1594 }
1595
1596 dev_info(dev,
1597 "RTL%s rev %s (%s) %iT%iR, TX queues %i, WiFi=%i, BT=%i, GPS=%i, HI PA=%i\n",
Jes Sorensen0e5d4352016-02-29 17:04:00 -05001598 priv->chip_name, cut, priv->chip_vendor, priv->tx_paths,
1599 priv->rx_paths, priv->ep_tx_count, priv->has_wifi,
1600 priv->has_bluetooth, priv->has_gps, priv->hi_pa);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001601
1602 dev_info(dev, "RTL%s MAC: %pM\n", priv->chip_name, priv->mac_addr);
1603}
1604
1605static int rtl8xxxu_identify_chip(struct rtl8xxxu_priv *priv)
1606{
1607 struct device *dev = &priv->udev->dev;
1608 u32 val32, bonding;
1609 u16 val16;
1610
1611 val32 = rtl8xxxu_read32(priv, REG_SYS_CFG);
1612 priv->chip_cut = (val32 & SYS_CFG_CHIP_VERSION_MASK) >>
1613 SYS_CFG_CHIP_VERSION_SHIFT;
1614 if (val32 & SYS_CFG_TRP_VAUX_EN) {
1615 dev_info(dev, "Unsupported test chip\n");
1616 return -ENOTSUPP;
1617 }
1618
1619 if (val32 & SYS_CFG_BT_FUNC) {
Jes Sorensen35a741f2016-02-29 17:04:10 -05001620 if (priv->chip_cut >= 3) {
1621 sprintf(priv->chip_name, "8723BU");
Jes Sorensenba17d822016-03-31 17:08:39 -04001622 priv->rtl_chip = RTL8723B;
Jes Sorensen35a741f2016-02-29 17:04:10 -05001623 } else {
1624 sprintf(priv->chip_name, "8723AU");
Jes Sorensen0e28b972016-02-29 17:04:13 -05001625 priv->usb_interrupts = 1;
Jes Sorensenba17d822016-03-31 17:08:39 -04001626 priv->rtl_chip = RTL8723A;
Jes Sorensen35a741f2016-02-29 17:04:10 -05001627 }
1628
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001629 priv->rf_paths = 1;
1630 priv->rx_paths = 1;
1631 priv->tx_paths = 1;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001632
1633 val32 = rtl8xxxu_read32(priv, REG_MULTI_FUNC_CTRL);
1634 if (val32 & MULTI_WIFI_FUNC_EN)
1635 priv->has_wifi = 1;
1636 if (val32 & MULTI_BT_FUNC_EN)
1637 priv->has_bluetooth = 1;
1638 if (val32 & MULTI_GPS_FUNC_EN)
1639 priv->has_gps = 1;
Jakub Sitnicki38451992016-02-03 13:39:49 -05001640 priv->is_multi_func = 1;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001641 } else if (val32 & SYS_CFG_TYPE_ID) {
1642 bonding = rtl8xxxu_read32(priv, REG_HPON_FSM);
1643 bonding &= HPON_FSM_BONDING_MASK;
Jes Sorensen55c0b6a2016-04-14 14:58:50 -04001644 if (priv->fops->tx_desc_size ==
1645 sizeof(struct rtl8xxxu_txdesc40)) {
Jes Sorensen0e5d4352016-02-29 17:04:00 -05001646 if (bonding == HPON_FSM_BONDING_1T2R) {
1647 sprintf(priv->chip_name, "8191EU");
1648 priv->rf_paths = 2;
1649 priv->rx_paths = 2;
1650 priv->tx_paths = 1;
Jes Sorensenba17d822016-03-31 17:08:39 -04001651 priv->rtl_chip = RTL8191E;
Jes Sorensen0e5d4352016-02-29 17:04:00 -05001652 } else {
1653 sprintf(priv->chip_name, "8192EU");
1654 priv->rf_paths = 2;
1655 priv->rx_paths = 2;
1656 priv->tx_paths = 2;
Jes Sorensenba17d822016-03-31 17:08:39 -04001657 priv->rtl_chip = RTL8192E;
Jes Sorensen0e5d4352016-02-29 17:04:00 -05001658 }
1659 } else if (bonding == HPON_FSM_BONDING_1T2R) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001660 sprintf(priv->chip_name, "8191CU");
1661 priv->rf_paths = 2;
1662 priv->rx_paths = 2;
1663 priv->tx_paths = 1;
Jes Sorensen0e28b972016-02-29 17:04:13 -05001664 priv->usb_interrupts = 1;
Jes Sorensenba17d822016-03-31 17:08:39 -04001665 priv->rtl_chip = RTL8191C;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001666 } else {
1667 sprintf(priv->chip_name, "8192CU");
1668 priv->rf_paths = 2;
1669 priv->rx_paths = 2;
1670 priv->tx_paths = 2;
Jes Sorensen0e28b972016-02-29 17:04:13 -05001671 priv->usb_interrupts = 1;
Jes Sorensenba17d822016-03-31 17:08:39 -04001672 priv->rtl_chip = RTL8192C;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001673 }
1674 priv->has_wifi = 1;
1675 } else {
1676 sprintf(priv->chip_name, "8188CU");
1677 priv->rf_paths = 1;
1678 priv->rx_paths = 1;
1679 priv->tx_paths = 1;
Jes Sorensenba17d822016-03-31 17:08:39 -04001680 priv->rtl_chip = RTL8188C;
Jes Sorensen0e28b972016-02-29 17:04:13 -05001681 priv->usb_interrupts = 1;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001682 priv->has_wifi = 1;
1683 }
1684
Jes Sorensenba17d822016-03-31 17:08:39 -04001685 switch (priv->rtl_chip) {
1686 case RTL8188E:
1687 case RTL8192E:
1688 case RTL8723B:
Jes Sorensen0e5d4352016-02-29 17:04:00 -05001689 switch (val32 & SYS_CFG_VENDOR_EXT_MASK) {
1690 case SYS_CFG_VENDOR_ID_TSMC:
1691 sprintf(priv->chip_vendor, "TSMC");
1692 break;
1693 case SYS_CFG_VENDOR_ID_SMIC:
1694 sprintf(priv->chip_vendor, "SMIC");
1695 priv->vendor_smic = 1;
1696 break;
1697 case SYS_CFG_VENDOR_ID_UMC:
1698 sprintf(priv->chip_vendor, "UMC");
1699 priv->vendor_umc = 1;
1700 break;
1701 default:
1702 sprintf(priv->chip_vendor, "unknown");
1703 }
1704 break;
1705 default:
1706 if (val32 & SYS_CFG_VENDOR_ID) {
1707 sprintf(priv->chip_vendor, "UMC");
1708 priv->vendor_umc = 1;
1709 } else {
1710 sprintf(priv->chip_vendor, "TSMC");
1711 }
1712 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001713
1714 val32 = rtl8xxxu_read32(priv, REG_GPIO_OUTSTS);
1715 priv->rom_rev = (val32 & GPIO_RF_RL_ID) >> 28;
1716
1717 val16 = rtl8xxxu_read16(priv, REG_NORMAL_SIE_EP_TX);
1718 if (val16 & NORMAL_SIE_EP_TX_HIGH_MASK) {
1719 priv->ep_tx_high_queue = 1;
1720 priv->ep_tx_count++;
1721 }
1722
1723 if (val16 & NORMAL_SIE_EP_TX_NORMAL_MASK) {
1724 priv->ep_tx_normal_queue = 1;
1725 priv->ep_tx_count++;
1726 }
1727
1728 if (val16 & NORMAL_SIE_EP_TX_LOW_MASK) {
1729 priv->ep_tx_low_queue = 1;
1730 priv->ep_tx_count++;
1731 }
1732
1733 /*
1734 * Fallback for devices that do not provide REG_NORMAL_SIE_EP_TX
1735 */
1736 if (!priv->ep_tx_count) {
1737 switch (priv->nr_out_eps) {
Jes Sorensen35a741f2016-02-29 17:04:10 -05001738 case 4:
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001739 case 3:
1740 priv->ep_tx_low_queue = 1;
1741 priv->ep_tx_count++;
1742 case 2:
1743 priv->ep_tx_normal_queue = 1;
1744 priv->ep_tx_count++;
1745 case 1:
1746 priv->ep_tx_high_queue = 1;
1747 priv->ep_tx_count++;
1748 break;
1749 default:
1750 dev_info(dev, "Unsupported USB TX end-points\n");
1751 return -ENOTSUPP;
1752 }
1753 }
1754
1755 return 0;
1756}
1757
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001758static int
1759rtl8xxxu_read_efuse8(struct rtl8xxxu_priv *priv, u16 offset, u8 *data)
1760{
1761 int i;
1762 u8 val8;
1763 u32 val32;
1764
1765 /* Write Address */
1766 rtl8xxxu_write8(priv, REG_EFUSE_CTRL + 1, offset & 0xff);
1767 val8 = rtl8xxxu_read8(priv, REG_EFUSE_CTRL + 2);
1768 val8 &= 0xfc;
1769 val8 |= (offset >> 8) & 0x03;
1770 rtl8xxxu_write8(priv, REG_EFUSE_CTRL + 2, val8);
1771
1772 val8 = rtl8xxxu_read8(priv, REG_EFUSE_CTRL + 3);
1773 rtl8xxxu_write8(priv, REG_EFUSE_CTRL + 3, val8 & 0x7f);
1774
1775 /* Poll for data read */
1776 val32 = rtl8xxxu_read32(priv, REG_EFUSE_CTRL);
1777 for (i = 0; i < RTL8XXXU_MAX_REG_POLL; i++) {
1778 val32 = rtl8xxxu_read32(priv, REG_EFUSE_CTRL);
1779 if (val32 & BIT(31))
1780 break;
1781 }
1782
1783 if (i == RTL8XXXU_MAX_REG_POLL)
1784 return -EIO;
1785
1786 udelay(50);
1787 val32 = rtl8xxxu_read32(priv, REG_EFUSE_CTRL);
1788
1789 *data = val32 & 0xff;
1790 return 0;
1791}
1792
1793static int rtl8xxxu_read_efuse(struct rtl8xxxu_priv *priv)
1794{
1795 struct device *dev = &priv->udev->dev;
1796 int i, ret = 0;
1797 u8 val8, word_mask, header, extheader;
1798 u16 val16, efuse_addr, offset;
1799 u32 val32;
1800
1801 val16 = rtl8xxxu_read16(priv, REG_9346CR);
1802 if (val16 & EEPROM_ENABLE)
1803 priv->has_eeprom = 1;
1804 if (val16 & EEPROM_BOOT)
1805 priv->boot_eeprom = 1;
1806
Jakub Sitnicki38451992016-02-03 13:39:49 -05001807 if (priv->is_multi_func) {
1808 val32 = rtl8xxxu_read32(priv, REG_EFUSE_TEST);
1809 val32 = (val32 & ~EFUSE_SELECT_MASK) | EFUSE_WIFI_SELECT;
1810 rtl8xxxu_write32(priv, REG_EFUSE_TEST, val32);
1811 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001812
1813 dev_dbg(dev, "Booting from %s\n",
1814 priv->boot_eeprom ? "EEPROM" : "EFUSE");
1815
1816 rtl8xxxu_write8(priv, REG_EFUSE_ACCESS, EFUSE_ACCESS_ENABLE);
1817
1818 /* 1.2V Power: From VDDON with Power Cut(0x0000[15]), default valid */
1819 val16 = rtl8xxxu_read16(priv, REG_SYS_ISO_CTRL);
1820 if (!(val16 & SYS_ISO_PWC_EV12V)) {
1821 val16 |= SYS_ISO_PWC_EV12V;
1822 rtl8xxxu_write16(priv, REG_SYS_ISO_CTRL, val16);
1823 }
1824 /* Reset: 0x0000[28], default valid */
1825 val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
1826 if (!(val16 & SYS_FUNC_ELDR)) {
1827 val16 |= SYS_FUNC_ELDR;
1828 rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
1829 }
1830
1831 /*
1832 * Clock: Gated(0x0008[5]) 8M(0x0008[1]) clock from ANA, default valid
1833 */
1834 val16 = rtl8xxxu_read16(priv, REG_SYS_CLKR);
1835 if (!(val16 & SYS_CLK_LOADER_ENABLE) || !(val16 & SYS_CLK_ANA8M)) {
1836 val16 |= (SYS_CLK_LOADER_ENABLE | SYS_CLK_ANA8M);
1837 rtl8xxxu_write16(priv, REG_SYS_CLKR, val16);
1838 }
1839
1840 /* Default value is 0xff */
Jes Sorensen3307d842016-02-29 17:03:59 -05001841 memset(priv->efuse_wifi.raw, 0xff, EFUSE_MAP_LEN);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001842
1843 efuse_addr = 0;
1844 while (efuse_addr < EFUSE_REAL_CONTENT_LEN_8723A) {
Jakub Sitnickif6c47702016-02-29 17:04:23 -05001845 u16 map_addr;
1846
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001847 ret = rtl8xxxu_read_efuse8(priv, efuse_addr++, &header);
1848 if (ret || header == 0xff)
1849 goto exit;
1850
1851 if ((header & 0x1f) == 0x0f) { /* extended header */
1852 offset = (header & 0xe0) >> 5;
1853
1854 ret = rtl8xxxu_read_efuse8(priv, efuse_addr++,
1855 &extheader);
1856 if (ret)
1857 goto exit;
1858 /* All words disabled */
1859 if ((extheader & 0x0f) == 0x0f)
1860 continue;
1861
1862 offset |= ((extheader & 0xf0) >> 1);
1863 word_mask = extheader & 0x0f;
1864 } else {
1865 offset = (header >> 4) & 0x0f;
1866 word_mask = header & 0x0f;
1867 }
1868
Jakub Sitnickif6c47702016-02-29 17:04:23 -05001869 /* Get word enable value from PG header */
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001870
Jakub Sitnickif6c47702016-02-29 17:04:23 -05001871 /* We have 8 bits to indicate validity */
1872 map_addr = offset * 8;
1873 if (map_addr >= EFUSE_MAP_LEN) {
1874 dev_warn(dev, "%s: Illegal map_addr (%04x), "
1875 "efuse corrupt!\n",
1876 __func__, map_addr);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001877 ret = -EINVAL;
1878 goto exit;
1879 }
Jakub Sitnickif6c47702016-02-29 17:04:23 -05001880 for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
1881 /* Check word enable condition in the section */
Jakub Sitnicki32a39dd2016-02-29 17:04:24 -05001882 if (word_mask & BIT(i)) {
Jakub Sitnickif6c47702016-02-29 17:04:23 -05001883 map_addr += 2;
Jakub Sitnicki32a39dd2016-02-29 17:04:24 -05001884 continue;
1885 }
1886
1887 ret = rtl8xxxu_read_efuse8(priv, efuse_addr++, &val8);
1888 if (ret)
1889 goto exit;
1890 priv->efuse_wifi.raw[map_addr++] = val8;
1891
1892 ret = rtl8xxxu_read_efuse8(priv, efuse_addr++, &val8);
1893 if (ret)
1894 goto exit;
1895 priv->efuse_wifi.raw[map_addr++] = val8;
Jakub Sitnickif6c47702016-02-29 17:04:23 -05001896 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001897 }
1898
1899exit:
1900 rtl8xxxu_write8(priv, REG_EFUSE_ACCESS, EFUSE_ACCESS_DISABLE);
1901
1902 return ret;
1903}
1904
Jes Sorensen599119f2016-04-28 15:19:06 -04001905void rtl8xxxu_reset_8051(struct rtl8xxxu_priv *priv)
Jes Sorensend48fe602016-02-03 13:39:44 -05001906{
1907 u8 val8;
1908 u16 sys_func;
1909
1910 val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL + 1);
Jes Sorensen53b381c2016-02-03 13:39:57 -05001911 val8 &= ~BIT(0);
Jes Sorensend48fe602016-02-03 13:39:44 -05001912 rtl8xxxu_write8(priv, REG_RSV_CTRL + 1, val8);
Jes Sorensen7d4ccb82016-02-29 17:05:50 -05001913
Jes Sorensend48fe602016-02-03 13:39:44 -05001914 sys_func = rtl8xxxu_read16(priv, REG_SYS_FUNC);
1915 sys_func &= ~SYS_FUNC_CPU_ENABLE;
1916 rtl8xxxu_write16(priv, REG_SYS_FUNC, sys_func);
Jes Sorensen7d4ccb82016-02-29 17:05:50 -05001917
Jes Sorensend48fe602016-02-03 13:39:44 -05001918 val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL + 1);
Jes Sorensen53b381c2016-02-03 13:39:57 -05001919 val8 |= BIT(0);
Jes Sorensend48fe602016-02-03 13:39:44 -05001920 rtl8xxxu_write8(priv, REG_RSV_CTRL + 1, val8);
Jes Sorensen7d4ccb82016-02-29 17:05:50 -05001921
1922 sys_func |= SYS_FUNC_CPU_ENABLE;
1923 rtl8xxxu_write16(priv, REG_SYS_FUNC, sys_func);
1924}
1925
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001926static int rtl8xxxu_start_firmware(struct rtl8xxxu_priv *priv)
1927{
1928 struct device *dev = &priv->udev->dev;
1929 int ret = 0, i;
1930 u32 val32;
1931
1932 /* Poll checksum report */
1933 for (i = 0; i < RTL8XXXU_FIRMWARE_POLL_MAX; i++) {
1934 val32 = rtl8xxxu_read32(priv, REG_MCU_FW_DL);
1935 if (val32 & MCU_FW_DL_CSUM_REPORT)
1936 break;
1937 }
1938
1939 if (i == RTL8XXXU_FIRMWARE_POLL_MAX) {
1940 dev_warn(dev, "Firmware checksum poll timed out\n");
1941 ret = -EAGAIN;
1942 goto exit;
1943 }
1944
1945 val32 = rtl8xxxu_read32(priv, REG_MCU_FW_DL);
1946 val32 |= MCU_FW_DL_READY;
1947 val32 &= ~MCU_WINT_INIT_READY;
1948 rtl8xxxu_write32(priv, REG_MCU_FW_DL, val32);
1949
Jes Sorensend48fe602016-02-03 13:39:44 -05001950 /*
1951 * Reset the 8051 in order for the firmware to start running,
1952 * otherwise it won't come up on the 8192eu
1953 */
Jes Sorensen7d4ccb82016-02-29 17:05:50 -05001954 priv->fops->reset_8051(priv);
Jes Sorensend48fe602016-02-03 13:39:44 -05001955
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001956 /* Wait for firmware to become ready */
1957 for (i = 0; i < RTL8XXXU_FIRMWARE_POLL_MAX; i++) {
1958 val32 = rtl8xxxu_read32(priv, REG_MCU_FW_DL);
1959 if (val32 & MCU_WINT_INIT_READY)
1960 break;
1961
1962 udelay(100);
1963 }
1964
1965 if (i == RTL8XXXU_FIRMWARE_POLL_MAX) {
1966 dev_warn(dev, "Firmware failed to start\n");
1967 ret = -EAGAIN;
1968 goto exit;
1969 }
1970
Jes Sorensen3a4be6a2016-02-29 17:04:58 -05001971 /*
1972 * Init H2C command
1973 */
Jes Sorensenba17d822016-03-31 17:08:39 -04001974 if (priv->rtl_chip == RTL8723B)
Jes Sorensen3a4be6a2016-02-29 17:04:58 -05001975 rtl8xxxu_write8(priv, REG_HMTFR, 0x0f);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001976exit:
1977 return ret;
1978}
1979
1980static int rtl8xxxu_download_firmware(struct rtl8xxxu_priv *priv)
1981{
1982 int pages, remainder, i, ret;
Jes Sorensend48fe602016-02-03 13:39:44 -05001983 u8 val8;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001984 u16 val16;
1985 u32 val32;
1986 u8 *fwptr;
1987
1988 val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC + 1);
1989 val8 |= 4;
1990 rtl8xxxu_write8(priv, REG_SYS_FUNC + 1, val8);
1991
1992 /* 8051 enable */
1993 val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
Jes Sorensen43154f62016-02-03 13:39:35 -05001994 val16 |= SYS_FUNC_CPU_ENABLE;
1995 rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001996
Jes Sorensen216202a2016-02-03 13:39:37 -05001997 val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL);
1998 if (val8 & MCU_FW_RAM_SEL) {
1999 pr_info("do the RAM reset\n");
2000 rtl8xxxu_write8(priv, REG_MCU_FW_DL, 0x00);
Jes Sorensen7d4ccb82016-02-29 17:05:50 -05002001 priv->fops->reset_8051(priv);
Jes Sorensen216202a2016-02-03 13:39:37 -05002002 }
2003
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002004 /* MCU firmware download enable */
2005 val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL);
Jes Sorensenef1c0492016-02-03 13:39:36 -05002006 val8 |= MCU_FW_DL_ENABLE;
2007 rtl8xxxu_write8(priv, REG_MCU_FW_DL, val8);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002008
2009 /* 8051 reset */
2010 val32 = rtl8xxxu_read32(priv, REG_MCU_FW_DL);
Jes Sorensenef1c0492016-02-03 13:39:36 -05002011 val32 &= ~BIT(19);
2012 rtl8xxxu_write32(priv, REG_MCU_FW_DL, val32);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002013
2014 /* Reset firmware download checksum */
2015 val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL);
Jes Sorensenef1c0492016-02-03 13:39:36 -05002016 val8 |= MCU_FW_DL_CSUM_REPORT;
2017 rtl8xxxu_write8(priv, REG_MCU_FW_DL, val8);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002018
2019 pages = priv->fw_size / RTL_FW_PAGE_SIZE;
2020 remainder = priv->fw_size % RTL_FW_PAGE_SIZE;
2021
2022 fwptr = priv->fw_data->data;
2023
2024 for (i = 0; i < pages; i++) {
2025 val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL + 2) & 0xF8;
Jes Sorensenef1c0492016-02-03 13:39:36 -05002026 val8 |= i;
2027 rtl8xxxu_write8(priv, REG_MCU_FW_DL + 2, val8);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002028
2029 ret = rtl8xxxu_writeN(priv, REG_FW_START_ADDRESS,
2030 fwptr, RTL_FW_PAGE_SIZE);
2031 if (ret != RTL_FW_PAGE_SIZE) {
2032 ret = -EAGAIN;
2033 goto fw_abort;
2034 }
2035
2036 fwptr += RTL_FW_PAGE_SIZE;
2037 }
2038
2039 if (remainder) {
2040 val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL + 2) & 0xF8;
Jes Sorensenef1c0492016-02-03 13:39:36 -05002041 val8 |= i;
2042 rtl8xxxu_write8(priv, REG_MCU_FW_DL + 2, val8);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002043 ret = rtl8xxxu_writeN(priv, REG_FW_START_ADDRESS,
2044 fwptr, remainder);
2045 if (ret != remainder) {
2046 ret = -EAGAIN;
2047 goto fw_abort;
2048 }
2049 }
2050
2051 ret = 0;
2052fw_abort:
2053 /* MCU firmware download disable */
2054 val16 = rtl8xxxu_read16(priv, REG_MCU_FW_DL);
Jes Sorensenef1c0492016-02-03 13:39:36 -05002055 val16 &= ~MCU_FW_DL_ENABLE;
2056 rtl8xxxu_write16(priv, REG_MCU_FW_DL, val16);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002057
2058 return ret;
2059}
2060
Jes Sorensen599119f2016-04-28 15:19:06 -04002061int rtl8xxxu_load_firmware(struct rtl8xxxu_priv *priv, char *fw_name)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002062{
2063 struct device *dev = &priv->udev->dev;
2064 const struct firmware *fw;
2065 int ret = 0;
2066 u16 signature;
2067
2068 dev_info(dev, "%s: Loading firmware %s\n", DRIVER_NAME, fw_name);
2069 if (request_firmware(&fw, fw_name, &priv->udev->dev)) {
2070 dev_warn(dev, "request_firmware(%s) failed\n", fw_name);
2071 ret = -EAGAIN;
2072 goto exit;
2073 }
2074 if (!fw) {
2075 dev_warn(dev, "Firmware data not available\n");
2076 ret = -EINVAL;
2077 goto exit;
2078 }
2079
2080 priv->fw_data = kmemdup(fw->data, fw->size, GFP_KERNEL);
Tobias Klauser98e27cb2016-02-03 13:39:43 -05002081 if (!priv->fw_data) {
2082 ret = -ENOMEM;
2083 goto exit;
2084 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002085 priv->fw_size = fw->size - sizeof(struct rtl8xxxu_firmware_header);
2086
2087 signature = le16_to_cpu(priv->fw_data->signature);
2088 switch (signature & 0xfff0) {
Jes Sorensen0e5d4352016-02-29 17:04:00 -05002089 case 0x92e0:
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002090 case 0x92c0:
2091 case 0x88c0:
Jes Sorensen35a741f2016-02-29 17:04:10 -05002092 case 0x5300:
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002093 case 0x2300:
2094 break;
2095 default:
2096 ret = -EINVAL;
2097 dev_warn(dev, "%s: Invalid firmware signature: 0x%04x\n",
2098 __func__, signature);
2099 }
2100
2101 dev_info(dev, "Firmware revision %i.%i (signature 0x%04x)\n",
2102 le16_to_cpu(priv->fw_data->major_version),
2103 priv->fw_data->minor_version, signature);
2104
2105exit:
2106 release_firmware(fw);
2107 return ret;
2108}
2109
Jes Sorensen6c46ca32016-04-28 15:19:07 -04002110void rtl8xxxu_firmware_self_reset(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002111{
2112 u16 val16;
2113 int i = 100;
2114
2115 /* Inform 8051 to perform reset */
2116 rtl8xxxu_write8(priv, REG_HMTFR + 3, 0x20);
2117
2118 for (i = 100; i > 0; i--) {
2119 val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
2120
2121 if (!(val16 & SYS_FUNC_CPU_ENABLE)) {
2122 dev_dbg(&priv->udev->dev,
2123 "%s: Firmware self reset success!\n", __func__);
2124 break;
2125 }
2126 udelay(50);
2127 }
2128
2129 if (!i) {
2130 /* Force firmware reset */
2131 val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
2132 val16 &= ~SYS_FUNC_CPU_ENABLE;
2133 rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
2134 }
2135}
2136
2137static int
Jes Sorensenc606e662016-04-07 14:19:16 -04002138rtl8xxxu_init_mac(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002139{
Jes Sorensenc606e662016-04-07 14:19:16 -04002140 struct rtl8xxxu_reg8val *array = priv->fops->mactable;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002141 int i, ret;
2142 u16 reg;
2143 u8 val;
2144
2145 for (i = 0; ; i++) {
2146 reg = array[i].reg;
2147 val = array[i].val;
2148
2149 if (reg == 0xffff && val == 0xff)
2150 break;
2151
2152 ret = rtl8xxxu_write8(priv, reg, val);
2153 if (ret != 1) {
2154 dev_warn(&priv->udev->dev,
Jes Sorensenc606e662016-04-07 14:19:16 -04002155 "Failed to initialize MAC "
2156 "(reg: %04x, val %02x)\n", reg, val);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002157 return -EAGAIN;
2158 }
2159 }
2160
Jes Sorensen8a594852016-04-07 14:19:26 -04002161 if (priv->rtl_chip != RTL8723B && priv->rtl_chip != RTL8192E)
Jes Sorensen8baf6702016-02-29 17:04:54 -05002162 rtl8xxxu_write8(priv, REG_MAX_AGGR_NUM, 0x0a);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002163
2164 return 0;
2165}
2166
Jes Sorensen599119f2016-04-28 15:19:06 -04002167int rtl8xxxu_init_phy_regs(struct rtl8xxxu_priv *priv,
2168 struct rtl8xxxu_reg32val *array)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002169{
2170 int i, ret;
2171 u16 reg;
2172 u32 val;
2173
2174 for (i = 0; ; i++) {
2175 reg = array[i].reg;
2176 val = array[i].val;
2177
2178 if (reg == 0xffff && val == 0xffffffff)
2179 break;
2180
2181 ret = rtl8xxxu_write32(priv, reg, val);
2182 if (ret != sizeof(val)) {
2183 dev_warn(&priv->udev->dev,
2184 "Failed to initialize PHY\n");
2185 return -EAGAIN;
2186 }
2187 udelay(1);
2188 }
2189
2190 return 0;
2191}
2192
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04002193void rtl8xxxu_gen1_init_phy_bb(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002194{
Jes Sorensenb84cac12016-04-14 14:59:00 -04002195 u8 val8, ldoa15, ldov12d, lpldo, ldohci12;
Jes Sorensen04313eb2016-02-29 17:04:51 -05002196 u16 val16;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002197 u32 val32;
2198
Jes Sorensencb877252016-04-14 14:58:57 -04002199 val8 = rtl8xxxu_read8(priv, REG_AFE_PLL_CTRL);
2200 udelay(2);
2201 val8 |= AFE_PLL_320_ENABLE;
2202 rtl8xxxu_write8(priv, REG_AFE_PLL_CTRL, val8);
2203 udelay(2);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002204
Jes Sorensencb877252016-04-14 14:58:57 -04002205 rtl8xxxu_write8(priv, REG_AFE_PLL_CTRL + 1, 0xff);
2206 udelay(2);
Jes Sorensen8baf6702016-02-29 17:04:54 -05002207
Jes Sorensencb877252016-04-14 14:58:57 -04002208 val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
2209 val16 |= SYS_FUNC_BB_GLB_RSTN | SYS_FUNC_BBRSTB;
2210 rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002211
Jes Sorensencb877252016-04-14 14:58:57 -04002212 val32 = rtl8xxxu_read32(priv, REG_AFE_XTAL_CTRL);
2213 val32 &= ~AFE_XTAL_RF_GATE;
2214 if (priv->has_bluetooth)
2215 val32 &= ~AFE_XTAL_BT_GATE;
2216 rtl8xxxu_write32(priv, REG_AFE_XTAL_CTRL, val32);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002217
2218 /* 6. 0x1f[7:0] = 0x07 */
2219 val8 = RF_ENABLE | RF_RSTB | RF_SDMRSTB;
2220 rtl8xxxu_write8(priv, REG_RF_CTRL, val8);
2221
Jes Sorensencb877252016-04-14 14:58:57 -04002222 if (priv->hi_pa)
Jes Sorensenabd71bd2016-04-07 14:19:22 -04002223 rtl8xxxu_init_phy_regs(priv, rtl8188ru_phy_1t_highpa_table);
2224 else if (priv->tx_paths == 2)
2225 rtl8xxxu_init_phy_regs(priv, rtl8192cu_phy_2t_init_table);
2226 else
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002227 rtl8xxxu_init_phy_regs(priv, rtl8723a_phy_1t_init_table);
2228
Jes Sorensen78a84212016-04-14 16:37:10 -04002229 if (priv->rtl_chip == RTL8188R && priv->hi_pa &&
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002230 priv->vendor_umc && priv->chip_cut == 1)
2231 rtl8xxxu_write8(priv, REG_OFDM0_AGC_PARM1 + 2, 0x50);
Jes Sorensenc82f8d12016-04-14 14:58:59 -04002232
2233 if (priv->hi_pa)
2234 rtl8xxxu_init_phy_regs(priv, rtl8xxx_agc_highpa_table);
2235 else
2236 rtl8xxxu_init_phy_regs(priv, rtl8xxx_agc_standard_table);
Jes Sorensenb84cac12016-04-14 14:59:00 -04002237
2238 ldoa15 = LDOA15_ENABLE | LDOA15_OBUF;
2239 ldov12d = LDOV12D_ENABLE | BIT(2) | (2 << LDOV12D_VADJ_SHIFT);
2240 ldohci12 = 0x57;
2241 lpldo = 1;
2242 val32 = (lpldo << 24) | (ldohci12 << 16) | (ldov12d << 8) | ldoa15;
2243 rtl8xxxu_write32(priv, REG_LDOA15_CTRL, val32);
Jes Sorensencb877252016-04-14 14:58:57 -04002244}
2245
Jes Sorensencb877252016-04-14 14:58:57 -04002246/*
2247 * Most of this is black magic retrieved from the old rtl8723au driver
2248 */
2249static int rtl8xxxu_init_phy_bb(struct rtl8xxxu_priv *priv)
2250{
Jes Sorensenb84cac12016-04-14 14:59:00 -04002251 u8 val8;
Jes Sorensencb877252016-04-14 14:58:57 -04002252 u32 val32;
2253
2254 priv->fops->init_phy_bb(priv);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002255
2256 if (priv->tx_paths == 1 && priv->rx_paths == 2) {
2257 /*
2258 * For 1T2R boards, patch the registers.
2259 *
2260 * It looks like 8191/2 1T2R boards use path B for TX
2261 */
2262 val32 = rtl8xxxu_read32(priv, REG_FPGA0_TX_INFO);
2263 val32 &= ~(BIT(0) | BIT(1));
2264 val32 |= BIT(1);
2265 rtl8xxxu_write32(priv, REG_FPGA0_TX_INFO, val32);
2266
2267 val32 = rtl8xxxu_read32(priv, REG_FPGA1_TX_INFO);
2268 val32 &= ~0x300033;
2269 val32 |= 0x200022;
2270 rtl8xxxu_write32(priv, REG_FPGA1_TX_INFO, val32);
2271
2272 val32 = rtl8xxxu_read32(priv, REG_CCK0_AFE_SETTING);
Jes Sorensenbd8fe402016-04-14 14:58:56 -04002273 val32 &= ~CCK0_AFE_RX_MASK;
Jes Sorensen90683082016-04-14 14:58:55 -04002274 val32 &= 0x00ffffff;
Jes Sorensenbd8fe402016-04-14 14:58:56 -04002275 val32 |= 0x40000000;
2276 val32 |= CCK0_AFE_RX_ANT_B;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002277 rtl8xxxu_write32(priv, REG_CCK0_AFE_SETTING, val32);
2278
2279 val32 = rtl8xxxu_read32(priv, REG_OFDM0_TRX_PATH_ENABLE);
2280 val32 &= ~(OFDM_RF_PATH_RX_MASK | OFDM_RF_PATH_TX_MASK);
2281 val32 |= (OFDM_RF_PATH_RX_A | OFDM_RF_PATH_RX_B |
2282 OFDM_RF_PATH_TX_B);
2283 rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, val32);
2284
2285 val32 = rtl8xxxu_read32(priv, REG_OFDM0_AGC_PARM1);
2286 val32 &= ~(BIT(4) | BIT(5));
2287 val32 |= BIT(4);
2288 rtl8xxxu_write32(priv, REG_OFDM0_AGC_PARM1, val32);
2289
2290 val32 = rtl8xxxu_read32(priv, REG_TX_CCK_RFON);
2291 val32 &= ~(BIT(27) | BIT(26));
2292 val32 |= BIT(27);
2293 rtl8xxxu_write32(priv, REG_TX_CCK_RFON, val32);
2294
2295 val32 = rtl8xxxu_read32(priv, REG_TX_CCK_BBON);
2296 val32 &= ~(BIT(27) | BIT(26));
2297 val32 |= BIT(27);
2298 rtl8xxxu_write32(priv, REG_TX_CCK_BBON, val32);
2299
2300 val32 = rtl8xxxu_read32(priv, REG_TX_OFDM_RFON);
2301 val32 &= ~(BIT(27) | BIT(26));
2302 val32 |= BIT(27);
2303 rtl8xxxu_write32(priv, REG_TX_OFDM_RFON, val32);
2304
2305 val32 = rtl8xxxu_read32(priv, REG_TX_OFDM_BBON);
2306 val32 &= ~(BIT(27) | BIT(26));
2307 val32 |= BIT(27);
2308 rtl8xxxu_write32(priv, REG_TX_OFDM_BBON, val32);
2309
2310 val32 = rtl8xxxu_read32(priv, REG_TX_TO_TX);
2311 val32 &= ~(BIT(27) | BIT(26));
2312 val32 |= BIT(27);
2313 rtl8xxxu_write32(priv, REG_TX_TO_TX, val32);
2314 }
2315
Jes Sorensen4ef22eb2016-02-29 17:04:55 -05002316 if (priv->has_xtalk) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002317 val32 = rtl8xxxu_read32(priv, REG_MAC_PHY_CTRL);
2318
Jes Sorensen4ef22eb2016-02-29 17:04:55 -05002319 val8 = priv->xtalk;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002320 val32 &= 0xff000fff;
2321 val32 |= ((val8 | (val8 << 6)) << 12);
2322
2323 rtl8xxxu_write32(priv, REG_MAC_PHY_CTRL, val32);
2324 }
2325
Jes Sorensen8a594852016-04-07 14:19:26 -04002326 if (priv->rtl_chip == RTL8192E)
2327 rtl8xxxu_write32(priv, REG_AFE_XTAL_CTRL, 0x000f81fb);
2328
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002329 return 0;
2330}
2331
2332static int rtl8xxxu_init_rf_regs(struct rtl8xxxu_priv *priv,
2333 struct rtl8xxxu_rfregval *array,
2334 enum rtl8xxxu_rfpath path)
2335{
2336 int i, ret;
2337 u8 reg;
2338 u32 val;
2339
2340 for (i = 0; ; i++) {
2341 reg = array[i].reg;
2342 val = array[i].val;
2343
2344 if (reg == 0xff && val == 0xffffffff)
2345 break;
2346
2347 switch (reg) {
2348 case 0xfe:
2349 msleep(50);
2350 continue;
2351 case 0xfd:
2352 mdelay(5);
2353 continue;
2354 case 0xfc:
2355 mdelay(1);
2356 continue;
2357 case 0xfb:
2358 udelay(50);
2359 continue;
2360 case 0xfa:
2361 udelay(5);
2362 continue;
2363 case 0xf9:
2364 udelay(1);
2365 continue;
2366 }
2367
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002368 ret = rtl8xxxu_write_rfreg(priv, path, reg, val);
2369 if (ret) {
2370 dev_warn(&priv->udev->dev,
2371 "Failed to initialize RF\n");
2372 return -EAGAIN;
2373 }
2374 udelay(1);
2375 }
2376
2377 return 0;
2378}
2379
Jes Sorensen599119f2016-04-28 15:19:06 -04002380int rtl8xxxu_init_phy_rf(struct rtl8xxxu_priv *priv,
2381 struct rtl8xxxu_rfregval *table,
2382 enum rtl8xxxu_rfpath path)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002383{
2384 u32 val32;
2385 u16 val16, rfsi_rfenv;
2386 u16 reg_sw_ctrl, reg_int_oe, reg_hssi_parm2;
2387
2388 switch (path) {
2389 case RF_A:
2390 reg_sw_ctrl = REG_FPGA0_XA_RF_SW_CTRL;
2391 reg_int_oe = REG_FPGA0_XA_RF_INT_OE;
2392 reg_hssi_parm2 = REG_FPGA0_XA_HSSI_PARM2;
2393 break;
2394 case RF_B:
2395 reg_sw_ctrl = REG_FPGA0_XB_RF_SW_CTRL;
2396 reg_int_oe = REG_FPGA0_XB_RF_INT_OE;
2397 reg_hssi_parm2 = REG_FPGA0_XB_HSSI_PARM2;
2398 break;
2399 default:
2400 dev_err(&priv->udev->dev, "%s:Unsupported RF path %c\n",
2401 __func__, path + 'A');
2402 return -EINVAL;
2403 }
2404 /* For path B, use XB */
2405 rfsi_rfenv = rtl8xxxu_read16(priv, reg_sw_ctrl);
2406 rfsi_rfenv &= FPGA0_RF_RFENV;
2407
2408 /*
2409 * These two we might be able to optimize into one
2410 */
2411 val32 = rtl8xxxu_read32(priv, reg_int_oe);
2412 val32 |= BIT(20); /* 0x10 << 16 */
2413 rtl8xxxu_write32(priv, reg_int_oe, val32);
2414 udelay(1);
2415
2416 val32 = rtl8xxxu_read32(priv, reg_int_oe);
2417 val32 |= BIT(4);
2418 rtl8xxxu_write32(priv, reg_int_oe, val32);
2419 udelay(1);
2420
2421 /*
2422 * These two we might be able to optimize into one
2423 */
2424 val32 = rtl8xxxu_read32(priv, reg_hssi_parm2);
2425 val32 &= ~FPGA0_HSSI_3WIRE_ADDR_LEN;
2426 rtl8xxxu_write32(priv, reg_hssi_parm2, val32);
2427 udelay(1);
2428
2429 val32 = rtl8xxxu_read32(priv, reg_hssi_parm2);
2430 val32 &= ~FPGA0_HSSI_3WIRE_DATA_LEN;
2431 rtl8xxxu_write32(priv, reg_hssi_parm2, val32);
2432 udelay(1);
2433
2434 rtl8xxxu_init_rf_regs(priv, table, path);
2435
2436 /* For path B, use XB */
2437 val16 = rtl8xxxu_read16(priv, reg_sw_ctrl);
2438 val16 &= ~FPGA0_RF_RFENV;
2439 val16 |= rfsi_rfenv;
2440 rtl8xxxu_write16(priv, reg_sw_ctrl, val16);
2441
2442 return 0;
2443}
2444
2445static int rtl8xxxu_llt_write(struct rtl8xxxu_priv *priv, u8 address, u8 data)
2446{
2447 int ret = -EBUSY;
2448 int count = 0;
2449 u32 value;
2450
2451 value = LLT_OP_WRITE | address << 8 | data;
2452
2453 rtl8xxxu_write32(priv, REG_LLT_INIT, value);
2454
2455 do {
2456 value = rtl8xxxu_read32(priv, REG_LLT_INIT);
2457 if ((value & LLT_OP_MASK) == LLT_OP_INACTIVE) {
2458 ret = 0;
2459 break;
2460 }
2461 } while (count++ < 20);
2462
2463 return ret;
2464}
2465
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04002466int rtl8xxxu_init_llt_table(struct rtl8xxxu_priv *priv, u8 last_tx_page)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002467{
2468 int ret;
2469 int i;
2470
2471 for (i = 0; i < last_tx_page; i++) {
2472 ret = rtl8xxxu_llt_write(priv, i, i + 1);
2473 if (ret)
2474 goto exit;
2475 }
2476
2477 ret = rtl8xxxu_llt_write(priv, last_tx_page, 0xff);
2478 if (ret)
2479 goto exit;
2480
2481 /* Mark remaining pages as a ring buffer */
2482 for (i = last_tx_page + 1; i < 0xff; i++) {
2483 ret = rtl8xxxu_llt_write(priv, i, (i + 1));
2484 if (ret)
2485 goto exit;
2486 }
2487
2488 /* Let last entry point to the start entry of ring buffer */
2489 ret = rtl8xxxu_llt_write(priv, 0xff, last_tx_page + 1);
2490 if (ret)
2491 goto exit;
2492
2493exit:
2494 return ret;
2495}
2496
Jes Sorensen599119f2016-04-28 15:19:06 -04002497int rtl8xxxu_auto_llt_table(struct rtl8xxxu_priv *priv, u8 last_tx_page)
Jes Sorensen74b99be2016-02-29 17:04:04 -05002498{
2499 u32 val32;
2500 int ret = 0;
2501 int i;
2502
2503 val32 = rtl8xxxu_read32(priv, REG_AUTO_LLT);
Jes Sorensen74b99be2016-02-29 17:04:04 -05002504 val32 |= AUTO_LLT_INIT_LLT;
2505 rtl8xxxu_write32(priv, REG_AUTO_LLT, val32);
2506
2507 for (i = 500; i; i--) {
2508 val32 = rtl8xxxu_read32(priv, REG_AUTO_LLT);
2509 if (!(val32 & AUTO_LLT_INIT_LLT))
2510 break;
2511 usleep_range(2, 4);
2512 }
2513
Jes Sorensen4de24812016-02-29 17:04:07 -05002514 if (!i) {
Jes Sorensen74b99be2016-02-29 17:04:04 -05002515 ret = -EBUSY;
2516 dev_warn(&priv->udev->dev, "LLT table init failed\n");
2517 }
Jes Sorensen74b99be2016-02-29 17:04:04 -05002518
2519 return ret;
2520}
2521
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002522static int rtl8xxxu_init_queue_priority(struct rtl8xxxu_priv *priv)
2523{
2524 u16 val16, hi, lo;
2525 u16 hiq, mgq, bkq, beq, viq, voq;
2526 int hip, mgp, bkp, bep, vip, vop;
2527 int ret = 0;
2528
2529 switch (priv->ep_tx_count) {
2530 case 1:
2531 if (priv->ep_tx_high_queue) {
2532 hi = TRXDMA_QUEUE_HIGH;
2533 } else if (priv->ep_tx_low_queue) {
2534 hi = TRXDMA_QUEUE_LOW;
2535 } else if (priv->ep_tx_normal_queue) {
2536 hi = TRXDMA_QUEUE_NORMAL;
2537 } else {
2538 hi = 0;
2539 ret = -EINVAL;
2540 }
2541
2542 hiq = hi;
2543 mgq = hi;
2544 bkq = hi;
2545 beq = hi;
2546 viq = hi;
2547 voq = hi;
2548
2549 hip = 0;
2550 mgp = 0;
2551 bkp = 0;
2552 bep = 0;
2553 vip = 0;
2554 vop = 0;
2555 break;
2556 case 2:
2557 if (priv->ep_tx_high_queue && priv->ep_tx_low_queue) {
2558 hi = TRXDMA_QUEUE_HIGH;
2559 lo = TRXDMA_QUEUE_LOW;
2560 } else if (priv->ep_tx_normal_queue && priv->ep_tx_low_queue) {
2561 hi = TRXDMA_QUEUE_NORMAL;
2562 lo = TRXDMA_QUEUE_LOW;
2563 } else if (priv->ep_tx_high_queue && priv->ep_tx_normal_queue) {
2564 hi = TRXDMA_QUEUE_HIGH;
2565 lo = TRXDMA_QUEUE_NORMAL;
2566 } else {
2567 ret = -EINVAL;
2568 hi = 0;
2569 lo = 0;
2570 }
2571
2572 hiq = hi;
2573 mgq = hi;
2574 bkq = lo;
2575 beq = lo;
2576 viq = hi;
2577 voq = hi;
2578
2579 hip = 0;
2580 mgp = 0;
2581 bkp = 1;
2582 bep = 1;
2583 vip = 0;
2584 vop = 0;
2585 break;
2586 case 3:
2587 beq = TRXDMA_QUEUE_LOW;
2588 bkq = TRXDMA_QUEUE_LOW;
2589 viq = TRXDMA_QUEUE_NORMAL;
2590 voq = TRXDMA_QUEUE_HIGH;
2591 mgq = TRXDMA_QUEUE_HIGH;
2592 hiq = TRXDMA_QUEUE_HIGH;
2593
2594 hip = hiq ^ 3;
2595 mgp = mgq ^ 3;
2596 bkp = bkq ^ 3;
2597 bep = beq ^ 3;
2598 vip = viq ^ 3;
2599 vop = viq ^ 3;
2600 break;
2601 default:
2602 ret = -EINVAL;
2603 }
2604
2605 /*
2606 * None of the vendor drivers are configuring the beacon
2607 * queue here .... why?
2608 */
2609 if (!ret) {
2610 val16 = rtl8xxxu_read16(priv, REG_TRXDMA_CTRL);
2611 val16 &= 0x7;
2612 val16 |= (voq << TRXDMA_CTRL_VOQ_SHIFT) |
2613 (viq << TRXDMA_CTRL_VIQ_SHIFT) |
2614 (beq << TRXDMA_CTRL_BEQ_SHIFT) |
2615 (bkq << TRXDMA_CTRL_BKQ_SHIFT) |
2616 (mgq << TRXDMA_CTRL_MGQ_SHIFT) |
2617 (hiq << TRXDMA_CTRL_HIQ_SHIFT);
2618 rtl8xxxu_write16(priv, REG_TRXDMA_CTRL, val16);
2619
2620 priv->pipe_out[TXDESC_QUEUE_VO] =
2621 usb_sndbulkpipe(priv->udev, priv->out_ep[vop]);
2622 priv->pipe_out[TXDESC_QUEUE_VI] =
2623 usb_sndbulkpipe(priv->udev, priv->out_ep[vip]);
2624 priv->pipe_out[TXDESC_QUEUE_BE] =
2625 usb_sndbulkpipe(priv->udev, priv->out_ep[bep]);
2626 priv->pipe_out[TXDESC_QUEUE_BK] =
2627 usb_sndbulkpipe(priv->udev, priv->out_ep[bkp]);
2628 priv->pipe_out[TXDESC_QUEUE_BEACON] =
2629 usb_sndbulkpipe(priv->udev, priv->out_ep[0]);
2630 priv->pipe_out[TXDESC_QUEUE_MGNT] =
2631 usb_sndbulkpipe(priv->udev, priv->out_ep[mgp]);
2632 priv->pipe_out[TXDESC_QUEUE_HIGH] =
2633 usb_sndbulkpipe(priv->udev, priv->out_ep[hip]);
2634 priv->pipe_out[TXDESC_QUEUE_CMD] =
2635 usb_sndbulkpipe(priv->udev, priv->out_ep[0]);
2636 }
2637
2638 return ret;
2639}
2640
Jes Sorensen599119f2016-04-28 15:19:06 -04002641void rtl8xxxu_fill_iqk_matrix_a(struct rtl8xxxu_priv *priv, bool iqk_ok,
2642 int result[][8], int candidate, bool tx_only)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002643{
2644 u32 oldval, x, tx0_a, reg;
2645 int y, tx0_c;
2646 u32 val32;
2647
2648 if (!iqk_ok)
2649 return;
2650
2651 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE);
2652 oldval = val32 >> 22;
2653
2654 x = result[candidate][0];
2655 if ((x & 0x00000200) != 0)
2656 x = x | 0xfffffc00;
2657 tx0_a = (x * oldval) >> 8;
2658
2659 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE);
2660 val32 &= ~0x3ff;
2661 val32 |= tx0_a;
2662 rtl8xxxu_write32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE, val32);
2663
2664 val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
2665 val32 &= ~BIT(31);
2666 if ((x * oldval >> 7) & 0x1)
2667 val32 |= BIT(31);
2668 rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
2669
2670 y = result[candidate][1];
2671 if ((y & 0x00000200) != 0)
2672 y = y | 0xfffffc00;
2673 tx0_c = (y * oldval) >> 8;
2674
2675 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XC_TX_AFE);
2676 val32 &= ~0xf0000000;
2677 val32 |= (((tx0_c & 0x3c0) >> 6) << 28);
2678 rtl8xxxu_write32(priv, REG_OFDM0_XC_TX_AFE, val32);
2679
2680 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE);
2681 val32 &= ~0x003f0000;
2682 val32 |= ((tx0_c & 0x3f) << 16);
2683 rtl8xxxu_write32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE, val32);
2684
2685 val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
2686 val32 &= ~BIT(29);
2687 if ((y * oldval >> 7) & 0x1)
2688 val32 |= BIT(29);
2689 rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
2690
2691 if (tx_only) {
2692 dev_dbg(&priv->udev->dev, "%s: only TX\n", __func__);
2693 return;
2694 }
2695
2696 reg = result[candidate][2];
2697
2698 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE);
2699 val32 &= ~0x3ff;
2700 val32 |= (reg & 0x3ff);
2701 rtl8xxxu_write32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE, val32);
2702
2703 reg = result[candidate][3] & 0x3F;
2704
2705 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE);
2706 val32 &= ~0xfc00;
2707 val32 |= ((reg << 10) & 0xfc00);
2708 rtl8xxxu_write32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE, val32);
2709
2710 reg = (result[candidate][3] >> 6) & 0xF;
2711
2712 val32 = rtl8xxxu_read32(priv, REG_OFDM0_RX_IQ_EXT_ANTA);
2713 val32 &= ~0xf0000000;
2714 val32 |= (reg << 28);
2715 rtl8xxxu_write32(priv, REG_OFDM0_RX_IQ_EXT_ANTA, val32);
2716}
2717
Jes Sorensen599119f2016-04-28 15:19:06 -04002718void rtl8xxxu_fill_iqk_matrix_b(struct rtl8xxxu_priv *priv, bool iqk_ok,
2719 int result[][8], int candidate, bool tx_only)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002720{
2721 u32 oldval, x, tx1_a, reg;
2722 int y, tx1_c;
2723 u32 val32;
2724
2725 if (!iqk_ok)
2726 return;
2727
2728 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE);
2729 oldval = val32 >> 22;
2730
2731 x = result[candidate][4];
2732 if ((x & 0x00000200) != 0)
2733 x = x | 0xfffffc00;
2734 tx1_a = (x * oldval) >> 8;
2735
2736 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE);
2737 val32 &= ~0x3ff;
2738 val32 |= tx1_a;
2739 rtl8xxxu_write32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE, val32);
2740
2741 val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
2742 val32 &= ~BIT(27);
2743 if ((x * oldval >> 7) & 0x1)
2744 val32 |= BIT(27);
2745 rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
2746
2747 y = result[candidate][5];
2748 if ((y & 0x00000200) != 0)
2749 y = y | 0xfffffc00;
2750 tx1_c = (y * oldval) >> 8;
2751
2752 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XD_TX_AFE);
2753 val32 &= ~0xf0000000;
2754 val32 |= (((tx1_c & 0x3c0) >> 6) << 28);
2755 rtl8xxxu_write32(priv, REG_OFDM0_XD_TX_AFE, val32);
2756
2757 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE);
2758 val32 &= ~0x003f0000;
2759 val32 |= ((tx1_c & 0x3f) << 16);
2760 rtl8xxxu_write32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE, val32);
2761
2762 val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
2763 val32 &= ~BIT(25);
2764 if ((y * oldval >> 7) & 0x1)
2765 val32 |= BIT(25);
2766 rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
2767
2768 if (tx_only) {
2769 dev_dbg(&priv->udev->dev, "%s: only TX\n", __func__);
2770 return;
2771 }
2772
2773 reg = result[candidate][6];
2774
2775 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE);
2776 val32 &= ~0x3ff;
2777 val32 |= (reg & 0x3ff);
2778 rtl8xxxu_write32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE, val32);
2779
2780 reg = result[candidate][7] & 0x3f;
2781
2782 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE);
2783 val32 &= ~0xfc00;
2784 val32 |= ((reg << 10) & 0xfc00);
2785 rtl8xxxu_write32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE, val32);
2786
2787 reg = (result[candidate][7] >> 6) & 0xf;
2788
2789 val32 = rtl8xxxu_read32(priv, REG_OFDM0_AGCR_SSI_TABLE);
2790 val32 &= ~0x0000f000;
2791 val32 |= (reg << 12);
2792 rtl8xxxu_write32(priv, REG_OFDM0_AGCR_SSI_TABLE, val32);
2793}
2794
2795#define MAX_TOLERANCE 5
2796
2797static bool rtl8xxxu_simularity_compare(struct rtl8xxxu_priv *priv,
2798 int result[][8], int c1, int c2)
2799{
2800 u32 i, j, diff, simubitmap, bound = 0;
2801 int candidate[2] = {-1, -1}; /* for path A and path B */
2802 bool retval = true;
2803
2804 if (priv->tx_paths > 1)
2805 bound = 8;
2806 else
2807 bound = 4;
2808
2809 simubitmap = 0;
2810
2811 for (i = 0; i < bound; i++) {
2812 diff = (result[c1][i] > result[c2][i]) ?
2813 (result[c1][i] - result[c2][i]) :
2814 (result[c2][i] - result[c1][i]);
2815 if (diff > MAX_TOLERANCE) {
2816 if ((i == 2 || i == 6) && !simubitmap) {
2817 if (result[c1][i] + result[c1][i + 1] == 0)
2818 candidate[(i / 4)] = c2;
2819 else if (result[c2][i] + result[c2][i + 1] == 0)
2820 candidate[(i / 4)] = c1;
2821 else
2822 simubitmap = simubitmap | (1 << i);
2823 } else {
2824 simubitmap = simubitmap | (1 << i);
2825 }
2826 }
2827 }
2828
2829 if (simubitmap == 0) {
2830 for (i = 0; i < (bound / 4); i++) {
2831 if (candidate[i] >= 0) {
2832 for (j = i * 4; j < (i + 1) * 4 - 2; j++)
2833 result[3][j] = result[candidate[i]][j];
2834 retval = false;
2835 }
2836 }
2837 return retval;
2838 } else if (!(simubitmap & 0x0f)) {
2839 /* path A OK */
2840 for (i = 0; i < 4; i++)
2841 result[3][i] = result[c1][i];
2842 } else if (!(simubitmap & 0xf0) && priv->tx_paths > 1) {
2843 /* path B OK */
2844 for (i = 4; i < 8; i++)
2845 result[3][i] = result[c1][i];
2846 }
2847
2848 return false;
2849}
2850
Jes Sorensen599119f2016-04-28 15:19:06 -04002851bool rtl8xxxu_gen2_simularity_compare(struct rtl8xxxu_priv *priv,
2852 int result[][8], int c1, int c2)
Jes Sorensene1547c52016-02-29 17:04:35 -05002853{
2854 u32 i, j, diff, simubitmap, bound = 0;
2855 int candidate[2] = {-1, -1}; /* for path A and path B */
2856 int tmp1, tmp2;
2857 bool retval = true;
2858
2859 if (priv->tx_paths > 1)
2860 bound = 8;
2861 else
2862 bound = 4;
2863
2864 simubitmap = 0;
2865
2866 for (i = 0; i < bound; i++) {
2867 if (i & 1) {
2868 if ((result[c1][i] & 0x00000200))
2869 tmp1 = result[c1][i] | 0xfffffc00;
2870 else
2871 tmp1 = result[c1][i];
2872
2873 if ((result[c2][i]& 0x00000200))
2874 tmp2 = result[c2][i] | 0xfffffc00;
2875 else
2876 tmp2 = result[c2][i];
2877 } else {
2878 tmp1 = result[c1][i];
2879 tmp2 = result[c2][i];
2880 }
2881
2882 diff = (tmp1 > tmp2) ? (tmp1 - tmp2) : (tmp2 - tmp1);
2883
2884 if (diff > MAX_TOLERANCE) {
2885 if ((i == 2 || i == 6) && !simubitmap) {
2886 if (result[c1][i] + result[c1][i + 1] == 0)
2887 candidate[(i / 4)] = c2;
2888 else if (result[c2][i] + result[c2][i + 1] == 0)
2889 candidate[(i / 4)] = c1;
2890 else
2891 simubitmap = simubitmap | (1 << i);
2892 } else {
2893 simubitmap = simubitmap | (1 << i);
2894 }
2895 }
2896 }
2897
2898 if (simubitmap == 0) {
2899 for (i = 0; i < (bound / 4); i++) {
2900 if (candidate[i] >= 0) {
2901 for (j = i * 4; j < (i + 1) * 4 - 2; j++)
2902 result[3][j] = result[candidate[i]][j];
2903 retval = false;
2904 }
2905 }
2906 return retval;
2907 } else {
2908 if (!(simubitmap & 0x03)) {
2909 /* path A TX OK */
2910 for (i = 0; i < 2; i++)
2911 result[3][i] = result[c1][i];
2912 }
2913
2914 if (!(simubitmap & 0x0c)) {
2915 /* path A RX OK */
2916 for (i = 2; i < 4; i++)
2917 result[3][i] = result[c1][i];
2918 }
2919
2920 if (!(simubitmap & 0x30) && priv->tx_paths > 1) {
2921 /* path B RX OK */
2922 for (i = 4; i < 6; i++)
2923 result[3][i] = result[c1][i];
2924 }
2925
2926 if (!(simubitmap & 0x30) && priv->tx_paths > 1) {
2927 /* path B RX OK */
2928 for (i = 6; i < 8; i++)
2929 result[3][i] = result[c1][i];
2930 }
2931 }
2932
2933 return false;
2934}
2935
Jes Sorensen599119f2016-04-28 15:19:06 -04002936void
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002937rtl8xxxu_save_mac_regs(struct rtl8xxxu_priv *priv, const u32 *reg, u32 *backup)
2938{
2939 int i;
2940
2941 for (i = 0; i < (RTL8XXXU_MAC_REGS - 1); i++)
2942 backup[i] = rtl8xxxu_read8(priv, reg[i]);
2943
2944 backup[i] = rtl8xxxu_read32(priv, reg[i]);
2945}
2946
Jes Sorensen599119f2016-04-28 15:19:06 -04002947void rtl8xxxu_restore_mac_regs(struct rtl8xxxu_priv *priv,
2948 const u32 *reg, u32 *backup)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002949{
2950 int i;
2951
2952 for (i = 0; i < (RTL8XXXU_MAC_REGS - 1); i++)
2953 rtl8xxxu_write8(priv, reg[i], backup[i]);
2954
2955 rtl8xxxu_write32(priv, reg[i], backup[i]);
2956}
2957
Jes Sorensen599119f2016-04-28 15:19:06 -04002958void rtl8xxxu_save_regs(struct rtl8xxxu_priv *priv, const u32 *regs,
2959 u32 *backup, int count)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002960{
2961 int i;
2962
2963 for (i = 0; i < count; i++)
2964 backup[i] = rtl8xxxu_read32(priv, regs[i]);
2965}
2966
Jes Sorensen599119f2016-04-28 15:19:06 -04002967void rtl8xxxu_restore_regs(struct rtl8xxxu_priv *priv, const u32 *regs,
2968 u32 *backup, int count)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002969{
2970 int i;
2971
2972 for (i = 0; i < count; i++)
2973 rtl8xxxu_write32(priv, regs[i], backup[i]);
2974}
2975
2976
Jes Sorensen599119f2016-04-28 15:19:06 -04002977void rtl8xxxu_path_adda_on(struct rtl8xxxu_priv *priv, const u32 *regs,
2978 bool path_a_on)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002979{
2980 u32 path_on;
2981 int i;
2982
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002983 if (priv->tx_paths == 1) {
Jes Sorensen8634af52016-02-29 17:04:33 -05002984 path_on = priv->fops->adda_1t_path_on;
2985 rtl8xxxu_write32(priv, regs[0], priv->fops->adda_1t_init);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002986 } else {
Jes Sorensen8634af52016-02-29 17:04:33 -05002987 path_on = path_a_on ? priv->fops->adda_2t_path_on_a :
2988 priv->fops->adda_2t_path_on_b;
2989
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002990 rtl8xxxu_write32(priv, regs[0], path_on);
2991 }
2992
2993 for (i = 1 ; i < RTL8XXXU_ADDA_REGS ; i++)
2994 rtl8xxxu_write32(priv, regs[i], path_on);
2995}
2996
Jes Sorensen599119f2016-04-28 15:19:06 -04002997void rtl8xxxu_mac_calibration(struct rtl8xxxu_priv *priv,
2998 const u32 *regs, u32 *backup)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002999{
3000 int i = 0;
3001
3002 rtl8xxxu_write8(priv, regs[i], 0x3f);
3003
3004 for (i = 1 ; i < (RTL8XXXU_MAC_REGS - 1); i++)
3005 rtl8xxxu_write8(priv, regs[i], (u8)(backup[i] & ~BIT(3)));
3006
3007 rtl8xxxu_write8(priv, regs[i], (u8)(backup[i] & ~BIT(5)));
3008}
3009
3010static int rtl8xxxu_iqk_path_a(struct rtl8xxxu_priv *priv)
3011{
3012 u32 reg_eac, reg_e94, reg_e9c, reg_ea4, val32;
3013 int result = 0;
3014
3015 /* path-A IQK setting */
3016 rtl8xxxu_write32(priv, REG_TX_IQK_TONE_A, 0x10008c1f);
3017 rtl8xxxu_write32(priv, REG_RX_IQK_TONE_A, 0x10008c1f);
3018 rtl8xxxu_write32(priv, REG_TX_IQK_PI_A, 0x82140102);
3019
3020 val32 = (priv->rf_paths > 1) ? 0x28160202 :
3021 /*IS_81xxC_VENDOR_UMC_B_CUT(pHalData->VersionID)?0x28160202: */
3022 0x28160502;
3023 rtl8xxxu_write32(priv, REG_RX_IQK_PI_A, val32);
3024
3025 /* path-B IQK setting */
3026 if (priv->rf_paths > 1) {
3027 rtl8xxxu_write32(priv, REG_TX_IQK_TONE_B, 0x10008c22);
3028 rtl8xxxu_write32(priv, REG_RX_IQK_TONE_B, 0x10008c22);
3029 rtl8xxxu_write32(priv, REG_TX_IQK_PI_B, 0x82140102);
3030 rtl8xxxu_write32(priv, REG_RX_IQK_PI_B, 0x28160202);
3031 }
3032
3033 /* LO calibration setting */
3034 rtl8xxxu_write32(priv, REG_IQK_AGC_RSP, 0x001028d1);
3035
3036 /* One shot, path A LOK & IQK */
3037 rtl8xxxu_write32(priv, REG_IQK_AGC_PTS, 0xf9000000);
3038 rtl8xxxu_write32(priv, REG_IQK_AGC_PTS, 0xf8000000);
3039
3040 mdelay(1);
3041
3042 /* Check failed */
3043 reg_eac = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_A_2);
3044 reg_e94 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_A);
3045 reg_e9c = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_A);
3046 reg_ea4 = rtl8xxxu_read32(priv, REG_RX_POWER_BEFORE_IQK_A_2);
3047
3048 if (!(reg_eac & BIT(28)) &&
3049 ((reg_e94 & 0x03ff0000) != 0x01420000) &&
3050 ((reg_e9c & 0x03ff0000) != 0x00420000))
3051 result |= 0x01;
3052 else /* If TX not OK, ignore RX */
3053 goto out;
3054
3055 /* If TX is OK, check whether RX is OK */
3056 if (!(reg_eac & BIT(27)) &&
3057 ((reg_ea4 & 0x03ff0000) != 0x01320000) &&
3058 ((reg_eac & 0x03ff0000) != 0x00360000))
3059 result |= 0x02;
3060 else
3061 dev_warn(&priv->udev->dev, "%s: Path A RX IQK failed!\n",
3062 __func__);
3063out:
3064 return result;
3065}
3066
3067static int rtl8xxxu_iqk_path_b(struct rtl8xxxu_priv *priv)
3068{
3069 u32 reg_eac, reg_eb4, reg_ebc, reg_ec4, reg_ecc;
3070 int result = 0;
3071
3072 /* One shot, path B LOK & IQK */
3073 rtl8xxxu_write32(priv, REG_IQK_AGC_CONT, 0x00000002);
3074 rtl8xxxu_write32(priv, REG_IQK_AGC_CONT, 0x00000000);
3075
3076 mdelay(1);
3077
3078 /* Check failed */
3079 reg_eac = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_A_2);
3080 reg_eb4 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_B);
3081 reg_ebc = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_B);
3082 reg_ec4 = rtl8xxxu_read32(priv, REG_RX_POWER_BEFORE_IQK_B_2);
3083 reg_ecc = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_B_2);
3084
3085 if (!(reg_eac & BIT(31)) &&
3086 ((reg_eb4 & 0x03ff0000) != 0x01420000) &&
3087 ((reg_ebc & 0x03ff0000) != 0x00420000))
3088 result |= 0x01;
3089 else
3090 goto out;
3091
3092 if (!(reg_eac & BIT(30)) &&
3093 (((reg_ec4 & 0x03ff0000) >> 16) != 0x132) &&
3094 (((reg_ecc & 0x03ff0000) >> 16) != 0x36))
3095 result |= 0x02;
3096 else
3097 dev_warn(&priv->udev->dev, "%s: Path B RX IQK failed!\n",
3098 __func__);
3099out:
3100 return result;
3101}
3102
3103static void rtl8xxxu_phy_iqcalibrate(struct rtl8xxxu_priv *priv,
3104 int result[][8], int t)
3105{
3106 struct device *dev = &priv->udev->dev;
3107 u32 i, val32;
3108 int path_a_ok, path_b_ok;
3109 int retry = 2;
3110 const u32 adda_regs[RTL8XXXU_ADDA_REGS] = {
3111 REG_FPGA0_XCD_SWITCH_CTRL, REG_BLUETOOTH,
3112 REG_RX_WAIT_CCA, REG_TX_CCK_RFON,
3113 REG_TX_CCK_BBON, REG_TX_OFDM_RFON,
3114 REG_TX_OFDM_BBON, REG_TX_TO_RX,
3115 REG_TX_TO_TX, REG_RX_CCK,
3116 REG_RX_OFDM, REG_RX_WAIT_RIFS,
3117 REG_RX_TO_RX, REG_STANDBY,
3118 REG_SLEEP, REG_PMPD_ANAEN
3119 };
3120 const u32 iqk_mac_regs[RTL8XXXU_MAC_REGS] = {
3121 REG_TXPAUSE, REG_BEACON_CTRL,
3122 REG_BEACON_CTRL_1, REG_GPIO_MUXCFG
3123 };
3124 const u32 iqk_bb_regs[RTL8XXXU_BB_REGS] = {
3125 REG_OFDM0_TRX_PATH_ENABLE, REG_OFDM0_TR_MUX_PAR,
3126 REG_FPGA0_XCD_RF_SW_CTRL, REG_CONFIG_ANT_A, REG_CONFIG_ANT_B,
3127 REG_FPGA0_XAB_RF_SW_CTRL, REG_FPGA0_XA_RF_INT_OE,
3128 REG_FPGA0_XB_RF_INT_OE, REG_FPGA0_RF_MODE
3129 };
3130
3131 /*
3132 * Note: IQ calibration must be performed after loading
3133 * PHY_REG.txt , and radio_a, radio_b.txt
3134 */
3135
3136 if (t == 0) {
3137 /* Save ADDA parameters, turn Path A ADDA on */
3138 rtl8xxxu_save_regs(priv, adda_regs, priv->adda_backup,
3139 RTL8XXXU_ADDA_REGS);
3140 rtl8xxxu_save_mac_regs(priv, iqk_mac_regs, priv->mac_backup);
3141 rtl8xxxu_save_regs(priv, iqk_bb_regs,
3142 priv->bb_backup, RTL8XXXU_BB_REGS);
3143 }
3144
3145 rtl8xxxu_path_adda_on(priv, adda_regs, true);
3146
3147 if (t == 0) {
3148 val32 = rtl8xxxu_read32(priv, REG_FPGA0_XA_HSSI_PARM1);
3149 if (val32 & FPGA0_HSSI_PARM1_PI)
3150 priv->pi_enabled = 1;
3151 }
3152
3153 if (!priv->pi_enabled) {
3154 /* Switch BB to PI mode to do IQ Calibration. */
3155 rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM1, 0x01000100);
3156 rtl8xxxu_write32(priv, REG_FPGA0_XB_HSSI_PARM1, 0x01000100);
3157 }
3158
3159 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
3160 val32 &= ~FPGA_RF_MODE_CCK;
3161 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
3162
3163 rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, 0x03a05600);
3164 rtl8xxxu_write32(priv, REG_OFDM0_TR_MUX_PAR, 0x000800e4);
3165 rtl8xxxu_write32(priv, REG_FPGA0_XCD_RF_SW_CTRL, 0x22204000);
3166
Jes Sorensencabb5502016-04-14 16:37:17 -04003167 if (!priv->no_pape) {
3168 val32 = rtl8xxxu_read32(priv, REG_FPGA0_XAB_RF_SW_CTRL);
3169 val32 |= (FPGA0_RF_PAPE |
3170 (FPGA0_RF_PAPE << FPGA0_RF_BD_CTRL_SHIFT));
3171 rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_SW_CTRL, val32);
3172 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003173
3174 val32 = rtl8xxxu_read32(priv, REG_FPGA0_XA_RF_INT_OE);
3175 val32 &= ~BIT(10);
3176 rtl8xxxu_write32(priv, REG_FPGA0_XA_RF_INT_OE, val32);
3177 val32 = rtl8xxxu_read32(priv, REG_FPGA0_XB_RF_INT_OE);
3178 val32 &= ~BIT(10);
3179 rtl8xxxu_write32(priv, REG_FPGA0_XB_RF_INT_OE, val32);
3180
3181 if (priv->tx_paths > 1) {
3182 rtl8xxxu_write32(priv, REG_FPGA0_XA_LSSI_PARM, 0x00010000);
3183 rtl8xxxu_write32(priv, REG_FPGA0_XB_LSSI_PARM, 0x00010000);
3184 }
3185
3186 /* MAC settings */
3187 rtl8xxxu_mac_calibration(priv, iqk_mac_regs, priv->mac_backup);
3188
3189 /* Page B init */
3190 rtl8xxxu_write32(priv, REG_CONFIG_ANT_A, 0x00080000);
3191
3192 if (priv->tx_paths > 1)
3193 rtl8xxxu_write32(priv, REG_CONFIG_ANT_B, 0x00080000);
3194
3195 /* IQ calibration setting */
3196 rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0x80800000);
3197 rtl8xxxu_write32(priv, REG_TX_IQK, 0x01007c00);
3198 rtl8xxxu_write32(priv, REG_RX_IQK, 0x01004800);
3199
3200 for (i = 0; i < retry; i++) {
3201 path_a_ok = rtl8xxxu_iqk_path_a(priv);
3202 if (path_a_ok == 0x03) {
3203 val32 = rtl8xxxu_read32(priv,
3204 REG_TX_POWER_BEFORE_IQK_A);
3205 result[t][0] = (val32 >> 16) & 0x3ff;
3206 val32 = rtl8xxxu_read32(priv,
3207 REG_TX_POWER_AFTER_IQK_A);
3208 result[t][1] = (val32 >> 16) & 0x3ff;
3209 val32 = rtl8xxxu_read32(priv,
3210 REG_RX_POWER_BEFORE_IQK_A_2);
3211 result[t][2] = (val32 >> 16) & 0x3ff;
3212 val32 = rtl8xxxu_read32(priv,
3213 REG_RX_POWER_AFTER_IQK_A_2);
3214 result[t][3] = (val32 >> 16) & 0x3ff;
3215 break;
3216 } else if (i == (retry - 1) && path_a_ok == 0x01) {
3217 /* TX IQK OK */
3218 dev_dbg(dev, "%s: Path A IQK Only Tx Success!!\n",
3219 __func__);
3220
3221 val32 = rtl8xxxu_read32(priv,
3222 REG_TX_POWER_BEFORE_IQK_A);
3223 result[t][0] = (val32 >> 16) & 0x3ff;
3224 val32 = rtl8xxxu_read32(priv,
3225 REG_TX_POWER_AFTER_IQK_A);
3226 result[t][1] = (val32 >> 16) & 0x3ff;
3227 }
3228 }
3229
3230 if (!path_a_ok)
3231 dev_dbg(dev, "%s: Path A IQK failed!\n", __func__);
3232
3233 if (priv->tx_paths > 1) {
3234 /*
3235 * Path A into standby
3236 */
3237 rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0x0);
3238 rtl8xxxu_write32(priv, REG_FPGA0_XA_LSSI_PARM, 0x00010000);
3239 rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0x80800000);
3240
3241 /* Turn Path B ADDA on */
3242 rtl8xxxu_path_adda_on(priv, adda_regs, false);
3243
3244 for (i = 0; i < retry; i++) {
3245 path_b_ok = rtl8xxxu_iqk_path_b(priv);
3246 if (path_b_ok == 0x03) {
3247 val32 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_B);
3248 result[t][4] = (val32 >> 16) & 0x3ff;
3249 val32 = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_B);
3250 result[t][5] = (val32 >> 16) & 0x3ff;
3251 val32 = rtl8xxxu_read32(priv, REG_RX_POWER_BEFORE_IQK_B_2);
3252 result[t][6] = (val32 >> 16) & 0x3ff;
3253 val32 = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_B_2);
3254 result[t][7] = (val32 >> 16) & 0x3ff;
3255 break;
3256 } else if (i == (retry - 1) && path_b_ok == 0x01) {
3257 /* TX IQK OK */
3258 val32 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_B);
3259 result[t][4] = (val32 >> 16) & 0x3ff;
3260 val32 = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_B);
3261 result[t][5] = (val32 >> 16) & 0x3ff;
3262 }
3263 }
3264
3265 if (!path_b_ok)
3266 dev_dbg(dev, "%s: Path B IQK failed!\n", __func__);
3267 }
3268
3269 /* Back to BB mode, load original value */
3270 rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0);
3271
3272 if (t) {
3273 if (!priv->pi_enabled) {
3274 /*
3275 * Switch back BB to SI mode after finishing
3276 * IQ Calibration
3277 */
3278 val32 = 0x01000000;
3279 rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM1, val32);
3280 rtl8xxxu_write32(priv, REG_FPGA0_XB_HSSI_PARM1, val32);
3281 }
3282
3283 /* Reload ADDA power saving parameters */
3284 rtl8xxxu_restore_regs(priv, adda_regs, priv->adda_backup,
3285 RTL8XXXU_ADDA_REGS);
3286
3287 /* Reload MAC parameters */
3288 rtl8xxxu_restore_mac_regs(priv, iqk_mac_regs, priv->mac_backup);
3289
3290 /* Reload BB parameters */
3291 rtl8xxxu_restore_regs(priv, iqk_bb_regs,
3292 priv->bb_backup, RTL8XXXU_BB_REGS);
3293
3294 /* Restore RX initial gain */
3295 rtl8xxxu_write32(priv, REG_FPGA0_XA_LSSI_PARM, 0x00032ed3);
3296
3297 if (priv->tx_paths > 1) {
3298 rtl8xxxu_write32(priv, REG_FPGA0_XB_LSSI_PARM,
3299 0x00032ed3);
3300 }
3301
3302 /* Load 0xe30 IQC default value */
3303 rtl8xxxu_write32(priv, REG_TX_IQK_TONE_A, 0x01008c00);
3304 rtl8xxxu_write32(priv, REG_RX_IQK_TONE_A, 0x01008c00);
3305 }
3306}
3307
Jes Sorensen27c7e892016-04-28 15:19:14 -04003308void rtl8xxxu_gen2_prepare_calibrate(struct rtl8xxxu_priv *priv, u8 start)
Jes Sorensenc7a5a192016-02-29 17:04:30 -05003309{
3310 struct h2c_cmd h2c;
3311
Jes Sorensenc7a5a192016-02-29 17:04:30 -05003312 memset(&h2c, 0, sizeof(struct h2c_cmd));
3313 h2c.bt_wlan_calibration.cmd = H2C_8723B_BT_WLAN_CALIBRATION;
3314 h2c.bt_wlan_calibration.data = start;
3315
Jes Sorensen9c0343d2016-04-28 15:19:13 -04003316 rtl8xxxu_gen2_h2c_cmd(priv, &h2c, sizeof(h2c.bt_wlan_calibration));
Jes Sorensenc7a5a192016-02-29 17:04:30 -05003317}
3318
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04003319void rtl8xxxu_gen1_phy_iq_calibrate(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003320{
3321 struct device *dev = &priv->udev->dev;
3322 int result[4][8]; /* last is final result */
3323 int i, candidate;
3324 bool path_a_ok, path_b_ok;
3325 u32 reg_e94, reg_e9c, reg_ea4, reg_eac;
3326 u32 reg_eb4, reg_ebc, reg_ec4, reg_ecc;
3327 s32 reg_tmp = 0;
3328 bool simu;
3329
3330 memset(result, 0, sizeof(result));
3331 candidate = -1;
3332
3333 path_a_ok = false;
3334 path_b_ok = false;
3335
3336 rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
3337
3338 for (i = 0; i < 3; i++) {
3339 rtl8xxxu_phy_iqcalibrate(priv, result, i);
3340
3341 if (i == 1) {
3342 simu = rtl8xxxu_simularity_compare(priv, result, 0, 1);
3343 if (simu) {
3344 candidate = 0;
3345 break;
3346 }
3347 }
3348
3349 if (i == 2) {
3350 simu = rtl8xxxu_simularity_compare(priv, result, 0, 2);
3351 if (simu) {
3352 candidate = 0;
3353 break;
3354 }
3355
3356 simu = rtl8xxxu_simularity_compare(priv, result, 1, 2);
3357 if (simu) {
3358 candidate = 1;
3359 } else {
3360 for (i = 0; i < 8; i++)
3361 reg_tmp += result[3][i];
3362
3363 if (reg_tmp)
3364 candidate = 3;
3365 else
3366 candidate = -1;
3367 }
3368 }
3369 }
3370
3371 for (i = 0; i < 4; i++) {
3372 reg_e94 = result[i][0];
3373 reg_e9c = result[i][1];
3374 reg_ea4 = result[i][2];
3375 reg_eac = result[i][3];
3376 reg_eb4 = result[i][4];
3377 reg_ebc = result[i][5];
3378 reg_ec4 = result[i][6];
3379 reg_ecc = result[i][7];
3380 }
3381
3382 if (candidate >= 0) {
3383 reg_e94 = result[candidate][0];
3384 priv->rege94 = reg_e94;
3385 reg_e9c = result[candidate][1];
3386 priv->rege9c = reg_e9c;
3387 reg_ea4 = result[candidate][2];
3388 reg_eac = result[candidate][3];
3389 reg_eb4 = result[candidate][4];
3390 priv->regeb4 = reg_eb4;
3391 reg_ebc = result[candidate][5];
3392 priv->regebc = reg_ebc;
3393 reg_ec4 = result[candidate][6];
3394 reg_ecc = result[candidate][7];
3395 dev_dbg(dev, "%s: candidate is %x\n", __func__, candidate);
3396 dev_dbg(dev,
3397 "%s: e94 =%x e9c=%x ea4=%x eac=%x eb4=%x ebc=%x ec4=%x "
3398 "ecc=%x\n ", __func__, reg_e94, reg_e9c,
3399 reg_ea4, reg_eac, reg_eb4, reg_ebc, reg_ec4, reg_ecc);
3400 path_a_ok = true;
3401 path_b_ok = true;
3402 } else {
3403 reg_e94 = reg_eb4 = priv->rege94 = priv->regeb4 = 0x100;
3404 reg_e9c = reg_ebc = priv->rege9c = priv->regebc = 0x0;
3405 }
3406
3407 if (reg_e94 && candidate >= 0)
3408 rtl8xxxu_fill_iqk_matrix_a(priv, path_a_ok, result,
3409 candidate, (reg_ea4 == 0));
3410
3411 if (priv->tx_paths > 1 && reg_eb4)
3412 rtl8xxxu_fill_iqk_matrix_b(priv, path_b_ok, result,
3413 candidate, (reg_ec4 == 0));
3414
Jes Sorensen04a74a92016-04-18 11:49:36 -04003415 rtl8xxxu_save_regs(priv, rtl8xxxu_iqk_phy_iq_bb_reg,
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003416 priv->bb_recovery_backup, RTL8XXXU_BB_REGS);
3417}
3418
3419static void rtl8723a_phy_lc_calibrate(struct rtl8xxxu_priv *priv)
3420{
3421 u32 val32;
3422 u32 rf_amode, rf_bmode = 0, lstf;
3423
3424 /* Check continuous TX and Packet TX */
3425 lstf = rtl8xxxu_read32(priv, REG_OFDM1_LSTF);
3426
3427 if (lstf & OFDM_LSTF_MASK) {
3428 /* Disable all continuous TX */
3429 val32 = lstf & ~OFDM_LSTF_MASK;
3430 rtl8xxxu_write32(priv, REG_OFDM1_LSTF, val32);
3431
3432 /* Read original RF mode Path A */
3433 rf_amode = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_AC);
3434
3435 /* Set RF mode to standby Path A */
3436 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC,
3437 (rf_amode & 0x8ffff) | 0x10000);
3438
3439 /* Path-B */
3440 if (priv->tx_paths > 1) {
3441 rf_bmode = rtl8xxxu_read_rfreg(priv, RF_B,
3442 RF6052_REG_AC);
3443
3444 rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC,
3445 (rf_bmode & 0x8ffff) | 0x10000);
3446 }
3447 } else {
3448 /* Deal with Packet TX case */
3449 /* block all queues */
3450 rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
3451 }
3452
3453 /* Start LC calibration */
Jes Sorensen0d698de2016-02-29 17:04:36 -05003454 if (priv->fops->has_s0s1)
3455 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_S0S1, 0xdfbe0);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003456 val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_MODE_AG);
3457 val32 |= 0x08000;
3458 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_MODE_AG, val32);
3459
3460 msleep(100);
3461
Jes Sorensen0d698de2016-02-29 17:04:36 -05003462 if (priv->fops->has_s0s1)
3463 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_S0S1, 0xdffe0);
3464
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003465 /* Restore original parameters */
3466 if (lstf & OFDM_LSTF_MASK) {
3467 /* Path-A */
3468 rtl8xxxu_write32(priv, REG_OFDM1_LSTF, lstf);
3469 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC, rf_amode);
3470
3471 /* Path-B */
3472 if (priv->tx_paths > 1)
3473 rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC,
3474 rf_bmode);
3475 } else /* Deal with Packet TX case */
3476 rtl8xxxu_write8(priv, REG_TXPAUSE, 0x00);
3477}
3478
3479static int rtl8xxxu_set_mac(struct rtl8xxxu_priv *priv)
3480{
3481 int i;
3482 u16 reg;
3483
3484 reg = REG_MACID;
3485
3486 for (i = 0; i < ETH_ALEN; i++)
3487 rtl8xxxu_write8(priv, reg + i, priv->mac_addr[i]);
3488
3489 return 0;
3490}
3491
3492static int rtl8xxxu_set_bssid(struct rtl8xxxu_priv *priv, const u8 *bssid)
3493{
3494 int i;
3495 u16 reg;
3496
3497 dev_dbg(&priv->udev->dev, "%s: (%pM)\n", __func__, bssid);
3498
3499 reg = REG_BSSID;
3500
3501 for (i = 0; i < ETH_ALEN; i++)
3502 rtl8xxxu_write8(priv, reg + i, bssid[i]);
3503
3504 return 0;
3505}
3506
3507static void
3508rtl8xxxu_set_ampdu_factor(struct rtl8xxxu_priv *priv, u8 ampdu_factor)
3509{
3510 u8 vals[4] = { 0x41, 0xa8, 0x72, 0xb9 };
3511 u8 max_agg = 0xf;
3512 int i;
3513
3514 ampdu_factor = 1 << (ampdu_factor + 2);
3515 if (ampdu_factor > max_agg)
3516 ampdu_factor = max_agg;
3517
3518 for (i = 0; i < 4; i++) {
3519 if ((vals[i] & 0xf0) > (ampdu_factor << 4))
3520 vals[i] = (vals[i] & 0x0f) | (ampdu_factor << 4);
3521
3522 if ((vals[i] & 0x0f) > ampdu_factor)
3523 vals[i] = (vals[i] & 0xf0) | ampdu_factor;
3524
3525 rtl8xxxu_write8(priv, REG_AGGLEN_LMT + i, vals[i]);
3526 }
3527}
3528
3529static void rtl8xxxu_set_ampdu_min_space(struct rtl8xxxu_priv *priv, u8 density)
3530{
3531 u8 val8;
3532
3533 val8 = rtl8xxxu_read8(priv, REG_AMPDU_MIN_SPACE);
3534 val8 &= 0xf8;
3535 val8 |= density;
3536 rtl8xxxu_write8(priv, REG_AMPDU_MIN_SPACE, val8);
3537}
3538
3539static int rtl8xxxu_active_to_emu(struct rtl8xxxu_priv *priv)
3540{
3541 u8 val8;
Colin Ian King37ba4b62016-04-14 16:37:07 -04003542 int count, ret = 0;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003543
3544 /* Start of rtl8723AU_card_enable_flow */
3545 /* Act to Cardemu sequence*/
3546 /* Turn off RF */
3547 rtl8xxxu_write8(priv, REG_RF_CTRL, 0);
3548
3549 /* 0x004E[7] = 0, switch DPDT_SEL_P output from register 0x0065[2] */
3550 val8 = rtl8xxxu_read8(priv, REG_LEDCFG2);
3551 val8 &= ~LEDCFG2_DPDT_SELECT;
3552 rtl8xxxu_write8(priv, REG_LEDCFG2, val8);
3553
3554 /* 0x0005[1] = 1 turn off MAC by HW state machine*/
3555 val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
3556 val8 |= BIT(1);
3557 rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
3558
3559 for (count = RTL8XXXU_MAX_REG_POLL; count; count--) {
3560 val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
3561 if ((val8 & BIT(1)) == 0)
3562 break;
3563 udelay(10);
3564 }
3565
3566 if (!count) {
3567 dev_warn(&priv->udev->dev, "%s: Disabling MAC timed out\n",
3568 __func__);
3569 ret = -EBUSY;
3570 goto exit;
3571 }
3572
3573 /* 0x0000[5] = 1 analog Ips to digital, 1:isolation */
3574 val8 = rtl8xxxu_read8(priv, REG_SYS_ISO_CTRL);
3575 val8 |= SYS_ISO_ANALOG_IPS;
3576 rtl8xxxu_write8(priv, REG_SYS_ISO_CTRL, val8);
3577
3578 /* 0x0020[0] = 0 disable LDOA12 MACRO block*/
3579 val8 = rtl8xxxu_read8(priv, REG_LDOA15_CTRL);
3580 val8 &= ~LDOA15_ENABLE;
3581 rtl8xxxu_write8(priv, REG_LDOA15_CTRL, val8);
3582
3583exit:
3584 return ret;
3585}
3586
Jes Sorensen6c46ca32016-04-28 15:19:07 -04003587int rtl8xxxu_active_to_lps(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003588{
3589 u8 val8;
3590 u8 val32;
Colin Ian King37ba4b62016-04-14 16:37:07 -04003591 int count, ret = 0;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003592
3593 rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
3594
3595 /*
3596 * Poll - wait for RX packet to complete
3597 */
3598 for (count = RTL8XXXU_MAX_REG_POLL; count; count--) {
3599 val32 = rtl8xxxu_read32(priv, 0x5f8);
3600 if (!val32)
3601 break;
3602 udelay(10);
3603 }
3604
3605 if (!count) {
3606 dev_warn(&priv->udev->dev,
3607 "%s: RX poll timed out (0x05f8)\n", __func__);
3608 ret = -EBUSY;
3609 goto exit;
3610 }
3611
3612 /* Disable CCK and OFDM, clock gated */
3613 val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC);
3614 val8 &= ~SYS_FUNC_BBRSTB;
3615 rtl8xxxu_write8(priv, REG_SYS_FUNC, val8);
3616
3617 udelay(2);
3618
3619 /* Reset baseband */
3620 val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC);
3621 val8 &= ~SYS_FUNC_BB_GLB_RSTN;
3622 rtl8xxxu_write8(priv, REG_SYS_FUNC, val8);
3623
3624 /* Reset MAC TRX */
3625 val8 = rtl8xxxu_read8(priv, REG_CR);
3626 val8 = CR_HCI_TXDMA_ENABLE | CR_HCI_RXDMA_ENABLE;
3627 rtl8xxxu_write8(priv, REG_CR, val8);
3628
3629 /* Reset MAC TRX */
3630 val8 = rtl8xxxu_read8(priv, REG_CR + 1);
3631 val8 &= ~BIT(1); /* CR_SECURITY_ENABLE */
3632 rtl8xxxu_write8(priv, REG_CR + 1, val8);
3633
3634 /* Respond TX OK to scheduler */
3635 val8 = rtl8xxxu_read8(priv, REG_DUAL_TSF_RST);
3636 val8 |= DUAL_TSF_TX_OK;
3637 rtl8xxxu_write8(priv, REG_DUAL_TSF_RST, val8);
3638
3639exit:
3640 return ret;
3641}
3642
Jes Sorensen993dd9b2016-04-28 15:19:12 -04003643void rtl8xxxu_disabled_to_emu(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003644{
3645 u8 val8;
3646
3647 /* Clear suspend enable and power down enable*/
3648 val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
3649 val8 &= ~(BIT(3) | BIT(7));
3650 rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
3651
3652 /* 0x48[16] = 0 to disable GPIO9 as EXT WAKEUP*/
3653 val8 = rtl8xxxu_read8(priv, REG_GPIO_INTM + 2);
3654 val8 &= ~BIT(0);
3655 rtl8xxxu_write8(priv, REG_GPIO_INTM + 2, val8);
3656
3657 /* 0x04[12:11] = 11 enable WL suspend*/
3658 val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
3659 val8 &= ~(BIT(3) | BIT(4));
3660 rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
3661}
3662
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003663static int rtl8xxxu_emu_to_disabled(struct rtl8xxxu_priv *priv)
3664{
3665 u8 val8;
3666
3667 /* 0x0007[7:0] = 0x20 SOP option to disable BG/MB */
3668 rtl8xxxu_write8(priv, REG_APS_FSMCO + 3, 0x20);
3669
3670 /* 0x04[12:11] = 01 enable WL suspend */
3671 val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
3672 val8 &= ~BIT(4);
3673 val8 |= BIT(3);
3674 rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
3675
3676 val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
3677 val8 |= BIT(7);
3678 rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
3679
3680 /* 0x48[16] = 1 to enable GPIO9 as EXT wakeup */
3681 val8 = rtl8xxxu_read8(priv, REG_GPIO_INTM + 2);
3682 val8 |= BIT(0);
3683 rtl8xxxu_write8(priv, REG_GPIO_INTM + 2, val8);
3684
3685 return 0;
3686}
3687
Jes Sorensen6c46ca32016-04-28 15:19:07 -04003688int rtl8xxxu_flush_fifo(struct rtl8xxxu_priv *priv)
Jes Sorensen430b4542016-02-29 17:05:48 -05003689{
Jes Sorensen145428e2016-02-29 17:05:49 -05003690 struct device *dev = &priv->udev->dev;
Jes Sorensen430b4542016-02-29 17:05:48 -05003691 u32 val32;
3692 int retry, retval;
3693
3694 rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
3695
3696 val32 = rtl8xxxu_read32(priv, REG_RXPKT_NUM);
3697 val32 |= RXPKT_NUM_RW_RELEASE_EN;
3698 rtl8xxxu_write32(priv, REG_RXPKT_NUM, val32);
3699
3700 retry = 100;
3701 retval = -EBUSY;
3702
3703 do {
3704 val32 = rtl8xxxu_read32(priv, REG_RXPKT_NUM);
3705 if (val32 & RXPKT_NUM_RXDMA_IDLE) {
3706 retval = 0;
3707 break;
3708 }
3709 } while (retry--);
3710
3711 rtl8xxxu_write16(priv, REG_RQPN_NPQ, 0);
3712 rtl8xxxu_write32(priv, REG_RQPN, 0x80000000);
3713 mdelay(2);
Jes Sorensen145428e2016-02-29 17:05:49 -05003714
3715 if (!retry)
3716 dev_warn(dev, "Failed to flush FIFO\n");
Jes Sorensen430b4542016-02-29 17:05:48 -05003717
3718 return retval;
3719}
3720
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04003721void rtl8xxxu_gen1_usb_quirks(struct rtl8xxxu_priv *priv)
Jes Sorensen747bf232016-04-14 14:59:04 -04003722{
3723 /* Fix USB interface interference issue */
3724 rtl8xxxu_write8(priv, 0xfe40, 0xe0);
3725 rtl8xxxu_write8(priv, 0xfe41, 0x8d);
3726 rtl8xxxu_write8(priv, 0xfe42, 0x80);
3727 /*
3728 * This sets TXDMA_OFFSET_DROP_DATA_EN (bit 9) as well as bits
3729 * 8 and 5, for which I have found no documentation.
3730 */
3731 rtl8xxxu_write32(priv, REG_TXDMA_OFFSET_CHK, 0xfd0320);
3732
3733 /*
3734 * Solve too many protocol error on USB bus.
3735 * Can't do this for 8188/8192 UMC A cut parts
3736 */
3737 if (!(!priv->chip_cut && priv->vendor_umc)) {
3738 rtl8xxxu_write8(priv, 0xfe40, 0xe6);
3739 rtl8xxxu_write8(priv, 0xfe41, 0x94);
3740 rtl8xxxu_write8(priv, 0xfe42, 0x80);
3741
3742 rtl8xxxu_write8(priv, 0xfe40, 0xe0);
3743 rtl8xxxu_write8(priv, 0xfe41, 0x19);
3744 rtl8xxxu_write8(priv, 0xfe42, 0x80);
3745
3746 rtl8xxxu_write8(priv, 0xfe40, 0xe5);
3747 rtl8xxxu_write8(priv, 0xfe41, 0x91);
3748 rtl8xxxu_write8(priv, 0xfe42, 0x80);
3749
3750 rtl8xxxu_write8(priv, 0xfe40, 0xe2);
3751 rtl8xxxu_write8(priv, 0xfe41, 0x81);
3752 rtl8xxxu_write8(priv, 0xfe42, 0x80);
3753 }
3754}
3755
Jes Sorensen599119f2016-04-28 15:19:06 -04003756void rtl8xxxu_gen2_usb_quirks(struct rtl8xxxu_priv *priv)
Jes Sorensen747bf232016-04-14 14:59:04 -04003757{
3758 u32 val32;
3759
3760 val32 = rtl8xxxu_read32(priv, REG_TXDMA_OFFSET_CHK);
3761 val32 |= TXDMA_OFFSET_DROP_DATA_EN;
3762 rtl8xxxu_write32(priv, REG_TXDMA_OFFSET_CHK, val32);
3763}
3764
Jes Sorensen599119f2016-04-28 15:19:06 -04003765void rtl8xxxu_power_off(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003766{
3767 u8 val8;
3768 u16 val16;
3769 u32 val32;
3770
3771 /*
3772 * Workaround for 8188RU LNA power leakage problem.
3773 */
Jes Sorensen8d95c802016-04-14 16:37:11 -04003774 if (priv->rtl_chip == RTL8188R) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003775 val32 = rtl8xxxu_read32(priv, REG_FPGA0_XCD_RF_PARM);
3776 val32 |= BIT(1);
3777 rtl8xxxu_write32(priv, REG_FPGA0_XCD_RF_PARM, val32);
3778 }
3779
Jes Sorensen430b4542016-02-29 17:05:48 -05003780 rtl8xxxu_flush_fifo(priv);
3781
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003782 rtl8xxxu_active_to_lps(priv);
3783
3784 /* Turn off RF */
3785 rtl8xxxu_write8(priv, REG_RF_CTRL, 0x00);
3786
3787 /* Reset Firmware if running in RAM */
3788 if (rtl8xxxu_read8(priv, REG_MCU_FW_DL) & MCU_FW_RAM_SEL)
3789 rtl8xxxu_firmware_self_reset(priv);
3790
3791 /* Reset MCU */
3792 val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
3793 val16 &= ~SYS_FUNC_CPU_ENABLE;
3794 rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
3795
3796 /* Reset MCU ready status */
3797 rtl8xxxu_write8(priv, REG_MCU_FW_DL, 0x00);
3798
3799 rtl8xxxu_active_to_emu(priv);
3800 rtl8xxxu_emu_to_disabled(priv);
3801
3802 /* Reset MCU IO Wrapper */
3803 val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL + 1);
3804 val8 &= ~BIT(0);
3805 rtl8xxxu_write8(priv, REG_RSV_CTRL + 1, val8);
3806
3807 val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL + 1);
3808 val8 |= BIT(0);
3809 rtl8xxxu_write8(priv, REG_RSV_CTRL + 1, val8);
3810
3811 /* RSV_CTRL 0x1C[7:0] = 0x0e lock ISO/CLK/Power control register */
3812 rtl8xxxu_write8(priv, REG_RSV_CTRL, 0x0e);
3813}
3814
Jes Sorensena3a5dac2016-02-29 17:05:16 -05003815#ifdef NEED_PS_TDMA
Jes Sorensen3ca7b322016-02-29 17:04:43 -05003816static void rtl8723bu_set_ps_tdma(struct rtl8xxxu_priv *priv,
3817 u8 arg1, u8 arg2, u8 arg3, u8 arg4, u8 arg5)
3818{
3819 struct h2c_cmd h2c;
3820
3821 memset(&h2c, 0, sizeof(struct h2c_cmd));
3822 h2c.b_type_dma.cmd = H2C_8723B_B_TYPE_TDMA;
3823 h2c.b_type_dma.data1 = arg1;
3824 h2c.b_type_dma.data2 = arg2;
3825 h2c.b_type_dma.data3 = arg3;
3826 h2c.b_type_dma.data4 = arg4;
3827 h2c.b_type_dma.data5 = arg5;
Jes Sorensen9c0343d2016-04-28 15:19:13 -04003828 rtl8xxxu_gen2_h2c_cmd(priv, &h2c, sizeof(h2c.b_type_dma));
Jes Sorensen3ca7b322016-02-29 17:04:43 -05003829}
Jes Sorensena3a5dac2016-02-29 17:05:16 -05003830#endif
Jes Sorensen3ca7b322016-02-29 17:04:43 -05003831
Jes Sorensen599119f2016-04-28 15:19:06 -04003832void rtl8xxxu_gen2_disable_rf(struct rtl8xxxu_priv *priv)
Jes Sorensenfc89a412016-02-29 17:05:46 -05003833{
3834 u32 val32;
3835
Jes Sorensenfc89a412016-02-29 17:05:46 -05003836 val32 = rtl8xxxu_read32(priv, REG_RX_WAIT_CCA);
3837 val32 &= ~(BIT(22) | BIT(23));
3838 rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, val32);
3839}
3840
Jes Sorensen89c2a092016-04-14 14:58:44 -04003841static void rtl8xxxu_old_init_queue_reserved_page(struct rtl8xxxu_priv *priv)
3842{
3843 u8 val8;
3844 u32 val32;
3845
3846 if (priv->ep_tx_normal_queue)
3847 val8 = TX_PAGE_NUM_NORM_PQ;
3848 else
3849 val8 = 0;
3850
3851 rtl8xxxu_write8(priv, REG_RQPN_NPQ, val8);
3852
3853 val32 = (TX_PAGE_NUM_PUBQ << RQPN_PUB_PQ_SHIFT) | RQPN_LOAD;
3854
3855 if (priv->ep_tx_high_queue)
3856 val32 |= (TX_PAGE_NUM_HI_PQ << RQPN_HI_PQ_SHIFT);
3857 if (priv->ep_tx_low_queue)
3858 val32 |= (TX_PAGE_NUM_LO_PQ << RQPN_LO_PQ_SHIFT);
3859
3860 rtl8xxxu_write32(priv, REG_RQPN, val32);
3861}
3862
3863static void rtl8xxxu_init_queue_reserved_page(struct rtl8xxxu_priv *priv)
3864{
3865 struct rtl8xxxu_fileops *fops = priv->fops;
3866 u32 hq, lq, nq, eq, pubq;
3867 u32 val32;
3868
3869 hq = 0;
3870 lq = 0;
3871 nq = 0;
3872 eq = 0;
3873 pubq = 0;
3874
3875 if (priv->ep_tx_high_queue)
3876 hq = fops->page_num_hi;
3877 if (priv->ep_tx_low_queue)
3878 lq = fops->page_num_lo;
3879 if (priv->ep_tx_normal_queue)
3880 nq = fops->page_num_norm;
3881
3882 val32 = (nq << RQPN_NPQ_SHIFT) | (eq << RQPN_EPQ_SHIFT);
3883 rtl8xxxu_write32(priv, REG_RQPN_NPQ, val32);
3884
3885 pubq = fops->total_page_num - hq - lq - nq;
3886
3887 val32 = RQPN_LOAD;
3888 val32 |= (hq << RQPN_HI_PQ_SHIFT);
3889 val32 |= (lq << RQPN_LO_PQ_SHIFT);
3890 val32 |= (pubq << RQPN_PUB_PQ_SHIFT);
3891
3892 rtl8xxxu_write32(priv, REG_RQPN, val32);
3893}
3894
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003895static int rtl8xxxu_init_device(struct ieee80211_hw *hw)
3896{
3897 struct rtl8xxxu_priv *priv = hw->priv;
3898 struct device *dev = &priv->udev->dev;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003899 bool macpower;
3900 int ret;
3901 u8 val8;
3902 u16 val16;
3903 u32 val32;
3904
3905 /* Check if MAC is already powered on */
3906 val8 = rtl8xxxu_read8(priv, REG_CR);
3907
3908 /*
3909 * Fix 92DU-VC S3 hang with the reason is that secondary mac is not
3910 * initialized. First MAC returns 0xea, second MAC returns 0x00
3911 */
3912 if (val8 == 0xea)
3913 macpower = false;
3914 else
3915 macpower = true;
3916
3917 ret = priv->fops->power_on(priv);
3918 if (ret < 0) {
3919 dev_warn(dev, "%s: Failed power on\n", __func__);
3920 goto exit;
3921 }
3922
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003923 if (!macpower) {
Jes Sorensen89c2a092016-04-14 14:58:44 -04003924 if (priv->fops->total_page_num)
3925 rtl8xxxu_init_queue_reserved_page(priv);
Jes Sorensen59b24da2016-04-14 14:58:43 -04003926 else
Jes Sorensen89c2a092016-04-14 14:58:44 -04003927 rtl8xxxu_old_init_queue_reserved_page(priv);
Jes Sorensen07bb46b2016-02-29 17:04:05 -05003928 }
3929
Jes Sorensen59b24da2016-04-14 14:58:43 -04003930 ret = rtl8xxxu_init_queue_priority(priv);
3931 dev_dbg(dev, "%s: init_queue_priority %i\n", __func__, ret);
3932 if (ret)
3933 goto exit;
3934
3935 /*
3936 * Set RX page boundary
3937 */
Jes Sorensen24e8e7e2016-04-14 14:59:01 -04003938 rtl8xxxu_write16(priv, REG_TRXFF_BNDY + 2, priv->fops->trxff_boundary);
Jes Sorensen59b24da2016-04-14 14:58:43 -04003939
Jes Sorensena47b9d42016-02-29 17:04:06 -05003940 ret = rtl8xxxu_download_firmware(priv);
3941 dev_dbg(dev, "%s: download_fiwmare %i\n", __func__, ret);
3942 if (ret)
3943 goto exit;
3944 ret = rtl8xxxu_start_firmware(priv);
3945 dev_dbg(dev, "%s: start_fiwmare %i\n", __func__, ret);
3946 if (ret)
3947 goto exit;
3948
Jes Sorensenf0d9f5e2016-02-29 17:04:16 -05003949 if (priv->fops->phy_init_antenna_selection)
3950 priv->fops->phy_init_antenna_selection(priv);
3951
Jes Sorensenc606e662016-04-07 14:19:16 -04003952 ret = rtl8xxxu_init_mac(priv);
Jes Sorensenb7dd8ff2016-02-29 17:04:17 -05003953
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003954 dev_dbg(dev, "%s: init_mac %i\n", __func__, ret);
3955 if (ret)
3956 goto exit;
3957
3958 ret = rtl8xxxu_init_phy_bb(priv);
3959 dev_dbg(dev, "%s: init_phy_bb %i\n", __func__, ret);
3960 if (ret)
3961 goto exit;
3962
Jes Sorensen4062b8f2016-04-14 16:37:08 -04003963 ret = priv->fops->init_phy_rf(priv);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003964 if (ret)
3965 goto exit;
3966
Jes Sorensenc1578632016-04-14 14:58:42 -04003967 /* RFSW Control - clear bit 14 ?? */
Jes Sorensenb8169012016-04-14 14:58:47 -04003968 if (priv->rtl_chip != RTL8723B && priv->rtl_chip != RTL8192E)
Jes Sorensenc1578632016-04-14 14:58:42 -04003969 rtl8xxxu_write32(priv, REG_FPGA0_TX_INFO, 0x00000003);
Jes Sorensen31133da2016-04-14 14:59:05 -04003970
3971 val32 = FPGA0_RF_TRSW | FPGA0_RF_TRSWB | FPGA0_RF_ANTSW |
Jes Sorensencabb5502016-04-14 16:37:17 -04003972 FPGA0_RF_ANTSWB |
3973 ((FPGA0_RF_ANTSW | FPGA0_RF_ANTSWB) << FPGA0_RF_BD_CTRL_SHIFT);
3974 if (!priv->no_pape) {
3975 val32 |= (FPGA0_RF_PAPE |
3976 (FPGA0_RF_PAPE << FPGA0_RF_BD_CTRL_SHIFT));
3977 }
Jes Sorensenc1578632016-04-14 14:58:42 -04003978 rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_SW_CTRL, val32);
Jes Sorensencabb5502016-04-14 16:37:17 -04003979
Jes Sorensenc1578632016-04-14 14:58:42 -04003980 /* 0x860[6:5]= 00 - why? - this sets antenna B */
3981 if (priv->rtl_chip != RTL8192E)
3982 rtl8xxxu_write32(priv, REG_FPGA0_XA_RF_INT_OE, 0x66f60210);
3983
Jes Sorensenf2a41632016-02-29 17:05:09 -05003984 if (!macpower) {
Jes Sorensen1f1b20f2016-02-29 17:05:00 -05003985 /*
3986 * Set TX buffer boundary
3987 */
Jes Sorensen80805aa2016-04-07 14:19:18 -04003988 if (priv->rtl_chip == RTL8192E)
3989 val8 = TX_TOTAL_PAGE_NUM_8192E + 1;
3990 else
3991 val8 = TX_TOTAL_PAGE_NUM + 1;
Jes Sorensen1f1b20f2016-02-29 17:05:00 -05003992
Jes Sorensenba17d822016-03-31 17:08:39 -04003993 if (priv->rtl_chip == RTL8723B)
Jes Sorensen1f1b20f2016-02-29 17:05:00 -05003994 val8 -= 1;
3995
3996 rtl8xxxu_write8(priv, REG_TXPKTBUF_BCNQ_BDNY, val8);
3997 rtl8xxxu_write8(priv, REG_TXPKTBUF_MGQ_BDNY, val8);
3998 rtl8xxxu_write8(priv, REG_TXPKTBUF_WMAC_LBK_BF_HD, val8);
3999 rtl8xxxu_write8(priv, REG_TRXFF_BNDY, val8);
4000 rtl8xxxu_write8(priv, REG_TDECTRL + 1, val8);
4001 }
4002
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004003 /*
Jes Sorensen9b323ee2016-04-14 14:59:03 -04004004 * The vendor drivers set PBP for all devices, except 8192e.
4005 * There is no explanation for this in any of the sources.
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004006 */
Jes Sorensen9b323ee2016-04-14 14:59:03 -04004007 val8 = (priv->fops->pbp_rx << PBP_PAGE_SIZE_RX_SHIFT) |
4008 (priv->fops->pbp_tx << PBP_PAGE_SIZE_TX_SHIFT);
Jes Sorensen2e7c7b32016-04-14 14:58:46 -04004009 if (priv->rtl_chip != RTL8192E)
4010 rtl8xxxu_write8(priv, REG_PBP, val8);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004011
Jes Sorensen59b24da2016-04-14 14:58:43 -04004012 dev_dbg(dev, "%s: macpower %i\n", __func__, macpower);
4013 if (!macpower) {
4014 ret = priv->fops->llt_init(priv, TX_TOTAL_PAGE_NUM);
4015 if (ret) {
4016 dev_warn(dev, "%s: LLT table init failed\n", __func__);
4017 goto exit;
4018 }
4019
4020 /*
Jes Sorensen0486e802016-04-14 14:58:45 -04004021 * Chip specific quirks
4022 */
Jes Sorensen747bf232016-04-14 14:59:04 -04004023 priv->fops->usb_quirks(priv);
Jes Sorensen0486e802016-04-14 14:58:45 -04004024
4025 /*
Jes Sorensen59b24da2016-04-14 14:58:43 -04004026 * Presumably this is for 8188EU as well
4027 * Enable TX report and TX report timer
4028 */
4029 if (priv->rtl_chip == RTL8723B) {
4030 val8 = rtl8xxxu_read8(priv, REG_TX_REPORT_CTRL);
4031 val8 |= TX_REPORT_CTRL_TIMER_ENABLE;
4032 rtl8xxxu_write8(priv, REG_TX_REPORT_CTRL, val8);
4033 /* Set MAX RPT MACID */
4034 rtl8xxxu_write8(priv, REG_TX_REPORT_CTRL + 1, 0x02);
4035 /* TX report Timer. Unit: 32us */
4036 rtl8xxxu_write16(priv, REG_TX_REPORT_TIME, 0xcdf0);
4037
4038 /* tmp ps ? */
4039 val8 = rtl8xxxu_read8(priv, 0xa3);
4040 val8 &= 0xf8;
4041 rtl8xxxu_write8(priv, 0xa3, val8);
4042 }
4043 }
4044
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004045 /*
4046 * Unit in 8 bytes, not obvious what it is used for
4047 */
4048 rtl8xxxu_write8(priv, REG_RX_DRVINFO_SZ, 4);
4049
Jes Sorensen57e5e2e2016-04-07 14:19:27 -04004050 if (priv->rtl_chip == RTL8192E) {
4051 rtl8xxxu_write32(priv, REG_HIMR0, 0x00);
4052 rtl8xxxu_write32(priv, REG_HIMR1, 0x00);
4053 } else {
4054 /*
4055 * Enable all interrupts - not obvious USB needs to do this
4056 */
4057 rtl8xxxu_write32(priv, REG_HISR, 0xffffffff);
4058 rtl8xxxu_write32(priv, REG_HIMR, 0xffffffff);
4059 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004060
4061 rtl8xxxu_set_mac(priv);
4062 rtl8xxxu_set_linktype(priv, NL80211_IFTYPE_STATION);
4063
4064 /*
4065 * Configure initial WMAC settings
4066 */
4067 val32 = RCR_ACCEPT_PHYS_MATCH | RCR_ACCEPT_MCAST | RCR_ACCEPT_BCAST |
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004068 RCR_ACCEPT_MGMT_FRAME | RCR_HTC_LOC_CTRL |
4069 RCR_APPEND_PHYSTAT | RCR_APPEND_ICV | RCR_APPEND_MIC;
4070 rtl8xxxu_write32(priv, REG_RCR, val32);
4071
4072 /*
4073 * Accept all multicast
4074 */
4075 rtl8xxxu_write32(priv, REG_MAR, 0xffffffff);
4076 rtl8xxxu_write32(priv, REG_MAR + 4, 0xffffffff);
4077
4078 /*
4079 * Init adaptive controls
4080 */
4081 val32 = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
4082 val32 &= ~RESPONSE_RATE_BITMAP_ALL;
4083 val32 |= RESPONSE_RATE_RRSR_CCK_ONLY_1M;
4084 rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, val32);
4085
4086 /* CCK = 0x0a, OFDM = 0x10 */
4087 rtl8xxxu_set_spec_sifs(priv, 0x10, 0x10);
4088 rtl8xxxu_set_retry(priv, 0x30, 0x30);
4089 rtl8xxxu_set_spec_sifs(priv, 0x0a, 0x10);
4090
4091 /*
4092 * Init EDCA
4093 */
4094 rtl8xxxu_write16(priv, REG_MAC_SPEC_SIFS, 0x100a);
4095
4096 /* Set CCK SIFS */
4097 rtl8xxxu_write16(priv, REG_SIFS_CCK, 0x100a);
4098
4099 /* Set OFDM SIFS */
4100 rtl8xxxu_write16(priv, REG_SIFS_OFDM, 0x100a);
4101
4102 /* TXOP */
4103 rtl8xxxu_write32(priv, REG_EDCA_BE_PARAM, 0x005ea42b);
4104 rtl8xxxu_write32(priv, REG_EDCA_BK_PARAM, 0x0000a44f);
4105 rtl8xxxu_write32(priv, REG_EDCA_VI_PARAM, 0x005ea324);
4106 rtl8xxxu_write32(priv, REG_EDCA_VO_PARAM, 0x002fa226);
4107
4108 /* Set data auto rate fallback retry count */
4109 rtl8xxxu_write32(priv, REG_DARFRC, 0x00000000);
4110 rtl8xxxu_write32(priv, REG_DARFRC + 4, 0x10080404);
4111 rtl8xxxu_write32(priv, REG_RARFRC, 0x04030201);
4112 rtl8xxxu_write32(priv, REG_RARFRC + 4, 0x08070605);
4113
4114 val8 = rtl8xxxu_read8(priv, REG_FWHW_TXQ_CTRL);
4115 val8 |= FWHW_TXQ_CTRL_AMPDU_RETRY;
4116 rtl8xxxu_write8(priv, REG_FWHW_TXQ_CTRL, val8);
4117
4118 /* Set ACK timeout */
4119 rtl8xxxu_write8(priv, REG_ACKTO, 0x40);
4120
4121 /*
4122 * Initialize beacon parameters
4123 */
4124 val16 = BEACON_DISABLE_TSF_UPDATE | (BEACON_DISABLE_TSF_UPDATE << 8);
4125 rtl8xxxu_write16(priv, REG_BEACON_CTRL, val16);
4126 rtl8xxxu_write16(priv, REG_TBTT_PROHIBIT, 0x6404);
4127 rtl8xxxu_write8(priv, REG_DRIVER_EARLY_INT, DRIVER_EARLY_INT_TIME);
4128 rtl8xxxu_write8(priv, REG_BEACON_DMA_TIME, BEACON_DMA_ATIME_INT_TIME);
4129 rtl8xxxu_write16(priv, REG_BEACON_TCFG, 0x660F);
4130
4131 /*
Jes Sorensenc3690602016-02-29 17:05:03 -05004132 * Initialize burst parameters
4133 */
Jes Sorensenba17d822016-03-31 17:08:39 -04004134 if (priv->rtl_chip == RTL8723B) {
Jes Sorensenc3690602016-02-29 17:05:03 -05004135 /*
4136 * For USB high speed set 512B packets
4137 */
4138 val8 = rtl8xxxu_read8(priv, REG_RXDMA_PRO_8723B);
4139 val8 &= ~(BIT(4) | BIT(5));
4140 val8 |= BIT(4);
4141 val8 |= BIT(1) | BIT(2) | BIT(3);
4142 rtl8xxxu_write8(priv, REG_RXDMA_PRO_8723B, val8);
4143
4144 /*
4145 * For USB high speed set 512B packets
4146 */
4147 val8 = rtl8xxxu_read8(priv, REG_HT_SINGLE_AMPDU_8723B);
4148 val8 |= BIT(7);
4149 rtl8xxxu_write8(priv, REG_HT_SINGLE_AMPDU_8723B, val8);
4150
4151 rtl8xxxu_write16(priv, REG_MAX_AGGR_NUM, 0x0c14);
4152 rtl8xxxu_write8(priv, REG_AMPDU_MAX_TIME_8723B, 0x5e);
4153 rtl8xxxu_write32(priv, REG_AGGLEN_LMT, 0xffffffff);
4154 rtl8xxxu_write8(priv, REG_RX_PKT_LIMIT, 0x18);
4155 rtl8xxxu_write8(priv, REG_PIFS, 0x00);
4156 rtl8xxxu_write8(priv, REG_USTIME_TSF_8723B, 0x50);
4157 rtl8xxxu_write8(priv, REG_USTIME_EDCA, 0x50);
4158
4159 val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL);
4160 val8 |= BIT(5) | BIT(6);
4161 rtl8xxxu_write8(priv, REG_RSV_CTRL, val8);
4162 }
4163
Jes Sorensen3e88ca42016-02-29 17:05:08 -05004164 if (priv->fops->init_aggregation)
4165 priv->fops->init_aggregation(priv);
4166
Jes Sorensenc3690602016-02-29 17:05:03 -05004167 /*
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004168 * Enable CCK and OFDM block
4169 */
4170 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
4171 val32 |= (FPGA_RF_MODE_CCK | FPGA_RF_MODE_OFDM);
4172 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
4173
4174 /*
4175 * Invalidate all CAM entries - bit 30 is undocumented
4176 */
4177 rtl8xxxu_write32(priv, REG_CAM_CMD, CAM_CMD_POLLING | BIT(30));
4178
4179 /*
4180 * Start out with default power levels for channel 6, 20MHz
4181 */
Jes Sorensene796dab2016-02-29 17:05:19 -05004182 priv->fops->set_tx_power(priv, 1, false);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004183
4184 /* Let the 8051 take control of antenna setting */
Jes Sorensen5bdb6b02016-04-14 14:58:48 -04004185 if (priv->rtl_chip != RTL8192E) {
4186 val8 = rtl8xxxu_read8(priv, REG_LEDCFG2);
4187 val8 |= LEDCFG2_DPDT_SELECT;
4188 rtl8xxxu_write8(priv, REG_LEDCFG2, val8);
4189 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004190
4191 rtl8xxxu_write8(priv, REG_HWSEQ_CTRL, 0xff);
4192
4193 /* Disable BAR - not sure if this has any effect on USB */
4194 rtl8xxxu_write32(priv, REG_BAR_MODE_CTRL, 0x0201ffff);
4195
4196 rtl8xxxu_write16(priv, REG_FAST_EDCA_CTRL, 0);
4197
Jes Sorensen9c79bf92016-02-29 17:05:10 -05004198 if (priv->fops->init_statistics)
4199 priv->fops->init_statistics(priv);
4200
Jes Sorensenb052b7f2016-04-07 14:19:30 -04004201 if (priv->rtl_chip == RTL8192E) {
4202 /*
4203 * 0x4c6[3] 1: RTS BW = Data BW
4204 * 0: RTS BW depends on CCA / secondary CCA result.
4205 */
4206 val8 = rtl8xxxu_read8(priv, REG_QUEUE_CTRL);
4207 val8 &= ~BIT(3);
4208 rtl8xxxu_write8(priv, REG_QUEUE_CTRL, val8);
4209 /*
4210 * Reset USB mode switch setting
4211 */
4212 rtl8xxxu_write8(priv, REG_ACLK_MON, 0x00);
4213 }
4214
Jes Sorensenfa0f2d42016-02-29 17:04:37 -05004215 rtl8723a_phy_lc_calibrate(priv);
4216
Jes Sorensene1547c52016-02-29 17:04:35 -05004217 priv->fops->phy_iq_calibrate(priv);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004218
4219 /*
4220 * This should enable thermal meter
4221 */
Jes Sorensen55c0b6a2016-04-14 14:58:50 -04004222 if (priv->fops->tx_desc_size == sizeof(struct rtl8xxxu_txdesc40))
Jes Sorensen72143b92016-02-29 17:05:25 -05004223 rtl8xxxu_write_rfreg(priv,
4224 RF_A, RF6052_REG_T_METER_8723B, 0x37cf8);
4225 else
4226 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_T_METER, 0x60);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004227
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004228 /* Set NAV_UPPER to 30000us */
4229 val8 = ((30000 + NAV_UPPER_UNIT - 1) / NAV_UPPER_UNIT);
4230 rtl8xxxu_write8(priv, REG_NAV_UPPER, val8);
4231
Jes Sorensenba17d822016-03-31 17:08:39 -04004232 if (priv->rtl_chip == RTL8723A) {
Jes Sorensen4042e612016-02-03 13:40:01 -05004233 /*
4234 * 2011/03/09 MH debug only, UMC-B cut pass 2500 S5 test,
4235 * but we need to find root cause.
4236 * This is 8723au only.
4237 */
4238 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
4239 if ((val32 & 0xff000000) != 0x83000000) {
4240 val32 |= FPGA_RF_MODE_CCK;
4241 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
4242 }
Jes Sorensen3021e512016-04-07 14:19:28 -04004243 } else if (priv->rtl_chip == RTL8192E) {
4244 rtl8xxxu_write8(priv, REG_USB_HRPWM, 0x00);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004245 }
4246
4247 val32 = rtl8xxxu_read32(priv, REG_FWHW_TXQ_CTRL);
4248 val32 |= FWHW_TXQ_CTRL_XMIT_MGMT_ACK;
4249 /* ack for xmit mgmt frames. */
4250 rtl8xxxu_write32(priv, REG_FWHW_TXQ_CTRL, val32);
4251
Jes Sorensene1394fe2016-04-07 14:19:29 -04004252 if (priv->rtl_chip == RTL8192E) {
4253 /*
4254 * Fix LDPC rx hang issue.
4255 */
4256 val32 = rtl8xxxu_read32(priv, REG_AFE_MISC);
4257 rtl8xxxu_write8(priv, REG_8192E_LDOV12_CTRL, 0x75);
4258 val32 &= 0xfff00fff;
4259 val32 |= 0x0007e000;
Jes Sorensen46b37832016-04-14 14:59:06 -04004260 rtl8xxxu_write32(priv, REG_AFE_MISC, val32);
Jes Sorensene1394fe2016-04-07 14:19:29 -04004261 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004262exit:
4263 return ret;
4264}
4265
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004266static void rtl8xxxu_cam_write(struct rtl8xxxu_priv *priv,
4267 struct ieee80211_key_conf *key, const u8 *mac)
4268{
4269 u32 cmd, val32, addr, ctrl;
4270 int j, i, tmp_debug;
4271
4272 tmp_debug = rtl8xxxu_debug;
4273 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_KEY)
4274 rtl8xxxu_debug |= RTL8XXXU_DEBUG_REG_WRITE;
4275
4276 /*
4277 * This is a bit of a hack - the lower bits of the cipher
4278 * suite selector happens to match the cipher index in the CAM
4279 */
4280 addr = key->keyidx << CAM_CMD_KEY_SHIFT;
4281 ctrl = (key->cipher & 0x0f) << 2 | key->keyidx | CAM_WRITE_VALID;
4282
4283 for (j = 5; j >= 0; j--) {
4284 switch (j) {
4285 case 0:
4286 val32 = ctrl | (mac[0] << 16) | (mac[1] << 24);
4287 break;
4288 case 1:
4289 val32 = mac[2] | (mac[3] << 8) |
4290 (mac[4] << 16) | (mac[5] << 24);
4291 break;
4292 default:
4293 i = (j - 2) << 2;
4294 val32 = key->key[i] | (key->key[i + 1] << 8) |
4295 key->key[i + 2] << 16 | key->key[i + 3] << 24;
4296 break;
4297 }
4298
4299 rtl8xxxu_write32(priv, REG_CAM_WRITE, val32);
4300 cmd = CAM_CMD_POLLING | CAM_CMD_WRITE | (addr + j);
4301 rtl8xxxu_write32(priv, REG_CAM_CMD, cmd);
4302 udelay(100);
4303 }
4304
4305 rtl8xxxu_debug = tmp_debug;
4306}
4307
4308static void rtl8xxxu_sw_scan_start(struct ieee80211_hw *hw,
Jes Sorensen56e43742016-02-03 13:39:50 -05004309 struct ieee80211_vif *vif, const u8 *mac)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004310{
4311 struct rtl8xxxu_priv *priv = hw->priv;
4312 u8 val8;
4313
4314 val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
4315 val8 |= BEACON_DISABLE_TSF_UPDATE;
4316 rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
4317}
4318
4319static void rtl8xxxu_sw_scan_complete(struct ieee80211_hw *hw,
4320 struct ieee80211_vif *vif)
4321{
4322 struct rtl8xxxu_priv *priv = hw->priv;
4323 u8 val8;
4324
4325 val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
4326 val8 &= ~BEACON_DISABLE_TSF_UPDATE;
4327 rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
4328}
4329
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04004330void rtl8xxxu_update_rate_mask(struct rtl8xxxu_priv *priv, u32 ramask, int sgi)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004331{
4332 struct h2c_cmd h2c;
4333
Jes Sorensenf653e692016-02-29 17:05:38 -05004334 memset(&h2c, 0, sizeof(struct h2c_cmd));
4335
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004336 h2c.ramask.cmd = H2C_SET_RATE_MASK;
4337 h2c.ramask.mask_lo = cpu_to_le16(ramask & 0xffff);
4338 h2c.ramask.mask_hi = cpu_to_le16(ramask >> 16);
4339
4340 h2c.ramask.arg = 0x80;
4341 if (sgi)
4342 h2c.ramask.arg |= 0x20;
4343
Jes Sorensen7ff8c1a2016-02-29 17:04:32 -05004344 dev_dbg(&priv->udev->dev, "%s: rate mask %08x, arg %02x, size %zi\n",
Jes Sorensen8da91572016-02-29 17:04:29 -05004345 __func__, ramask, h2c.ramask.arg, sizeof(h2c.ramask));
Jes Sorensen9c0343d2016-04-28 15:19:13 -04004346 rtl8xxxu_gen1_h2c_cmd(priv, &h2c, sizeof(h2c.ramask));
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004347}
4348
Jes Sorensen599119f2016-04-28 15:19:06 -04004349void rtl8xxxu_gen2_update_rate_mask(struct rtl8xxxu_priv *priv,
4350 u32 ramask, int sgi)
Jes Sorensenf653e692016-02-29 17:05:38 -05004351{
4352 struct h2c_cmd h2c;
4353 u8 bw = 0;
4354
4355 memset(&h2c, 0, sizeof(struct h2c_cmd));
4356
4357 h2c.b_macid_cfg.cmd = H2C_8723B_MACID_CFG_RAID;
4358 h2c.b_macid_cfg.ramask0 = ramask & 0xff;
4359 h2c.b_macid_cfg.ramask1 = (ramask >> 8) & 0xff;
4360 h2c.b_macid_cfg.ramask2 = (ramask >> 16) & 0xff;
4361 h2c.b_macid_cfg.ramask3 = (ramask >> 24) & 0xff;
4362
4363 h2c.ramask.arg = 0x80;
4364 h2c.b_macid_cfg.data1 = 0;
4365 if (sgi)
4366 h2c.b_macid_cfg.data1 |= BIT(7);
4367
4368 h2c.b_macid_cfg.data2 = bw;
4369
4370 dev_dbg(&priv->udev->dev, "%s: rate mask %08x, arg %02x, size %zi\n",
4371 __func__, ramask, h2c.ramask.arg, sizeof(h2c.b_macid_cfg));
Jes Sorensen9c0343d2016-04-28 15:19:13 -04004372 rtl8xxxu_gen2_h2c_cmd(priv, &h2c, sizeof(h2c.b_macid_cfg));
Jes Sorensenf653e692016-02-29 17:05:38 -05004373}
4374
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04004375void rtl8xxxu_gen1_report_connect(struct rtl8xxxu_priv *priv,
4376 u8 macid, bool connect)
Jes Sorensen7d794ea2016-02-29 17:05:39 -05004377{
4378 struct h2c_cmd h2c;
4379
4380 memset(&h2c, 0, sizeof(struct h2c_cmd));
4381
4382 h2c.joinbss.cmd = H2C_JOIN_BSS_REPORT;
4383
4384 if (connect)
4385 h2c.joinbss.data = H2C_JOIN_BSS_CONNECT;
4386 else
4387 h2c.joinbss.data = H2C_JOIN_BSS_DISCONNECT;
4388
Jes Sorensen9c0343d2016-04-28 15:19:13 -04004389 rtl8xxxu_gen1_h2c_cmd(priv, &h2c, sizeof(h2c.joinbss));
Jes Sorensen7d794ea2016-02-29 17:05:39 -05004390}
4391
Jes Sorensen599119f2016-04-28 15:19:06 -04004392void rtl8xxxu_gen2_report_connect(struct rtl8xxxu_priv *priv,
4393 u8 macid, bool connect)
Jes Sorensen7d794ea2016-02-29 17:05:39 -05004394{
4395 struct h2c_cmd h2c;
4396
4397 memset(&h2c, 0, sizeof(struct h2c_cmd));
4398
4399 h2c.media_status_rpt.cmd = H2C_8723B_MEDIA_STATUS_RPT;
4400 if (connect)
4401 h2c.media_status_rpt.parm |= BIT(0);
4402 else
4403 h2c.media_status_rpt.parm &= ~BIT(0);
4404
Jes Sorensen9c0343d2016-04-28 15:19:13 -04004405 rtl8xxxu_gen2_h2c_cmd(priv, &h2c, sizeof(h2c.media_status_rpt));
Jes Sorensen7d794ea2016-02-29 17:05:39 -05004406}
4407
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004408static void rtl8xxxu_set_basic_rates(struct rtl8xxxu_priv *priv, u32 rate_cfg)
4409{
4410 u32 val32;
4411 u8 rate_idx = 0;
4412
4413 rate_cfg &= RESPONSE_RATE_BITMAP_ALL;
4414
4415 val32 = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
4416 val32 &= ~RESPONSE_RATE_BITMAP_ALL;
4417 val32 |= rate_cfg;
4418 rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, val32);
4419
4420 dev_dbg(&priv->udev->dev, "%s: rates %08x\n", __func__, rate_cfg);
4421
4422 while (rate_cfg) {
4423 rate_cfg = (rate_cfg >> 1);
4424 rate_idx++;
4425 }
4426 rtl8xxxu_write8(priv, REG_INIRTS_RATE_SEL, rate_idx);
4427}
4428
4429static void
4430rtl8xxxu_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4431 struct ieee80211_bss_conf *bss_conf, u32 changed)
4432{
4433 struct rtl8xxxu_priv *priv = hw->priv;
4434 struct device *dev = &priv->udev->dev;
4435 struct ieee80211_sta *sta;
4436 u32 val32;
4437 u8 val8;
4438
4439 if (changed & BSS_CHANGED_ASSOC) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004440 dev_dbg(dev, "Changed ASSOC: %i!\n", bss_conf->assoc);
4441
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004442 rtl8xxxu_set_linktype(priv, vif->type);
4443
4444 if (bss_conf->assoc) {
4445 u32 ramask;
4446 int sgi = 0;
4447
4448 rcu_read_lock();
4449 sta = ieee80211_find_sta(vif, bss_conf->bssid);
4450 if (!sta) {
4451 dev_info(dev, "%s: ASSOC no sta found\n",
4452 __func__);
4453 rcu_read_unlock();
4454 goto error;
4455 }
4456
4457 if (sta->ht_cap.ht_supported)
4458 dev_info(dev, "%s: HT supported\n", __func__);
4459 if (sta->vht_cap.vht_supported)
4460 dev_info(dev, "%s: VHT supported\n", __func__);
4461
4462 /* TODO: Set bits 28-31 for rate adaptive id */
4463 ramask = (sta->supp_rates[0] & 0xfff) |
4464 sta->ht_cap.mcs.rx_mask[0] << 12 |
4465 sta->ht_cap.mcs.rx_mask[1] << 20;
4466 if (sta->ht_cap.cap &
4467 (IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_SGI_20))
4468 sgi = 1;
4469 rcu_read_unlock();
4470
Jes Sorensenf653e692016-02-29 17:05:38 -05004471 priv->fops->update_rate_mask(priv, ramask, sgi);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004472
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004473 rtl8xxxu_write8(priv, REG_BCN_MAX_ERR, 0xff);
4474
Jes Sorensen97db5a82016-04-28 15:19:10 -04004475 rtl8xxxu_stop_tx_beacon(priv);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004476
4477 /* joinbss sequence */
4478 rtl8xxxu_write16(priv, REG_BCN_PSR_RPT,
4479 0xc000 | bss_conf->aid);
4480
Jes Sorensen7d794ea2016-02-29 17:05:39 -05004481 priv->fops->report_connect(priv, 0, true);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004482 } else {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004483 val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
4484 val8 |= BEACON_DISABLE_TSF_UPDATE;
4485 rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
4486
Jes Sorensen7d794ea2016-02-29 17:05:39 -05004487 priv->fops->report_connect(priv, 0, false);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004488 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004489 }
4490
4491 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4492 dev_dbg(dev, "Changed ERP_PREAMBLE: Use short preamble %i\n",
4493 bss_conf->use_short_preamble);
4494 val32 = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
4495 if (bss_conf->use_short_preamble)
4496 val32 |= RSR_ACK_SHORT_PREAMBLE;
4497 else
4498 val32 &= ~RSR_ACK_SHORT_PREAMBLE;
4499 rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, val32);
4500 }
4501
4502 if (changed & BSS_CHANGED_ERP_SLOT) {
4503 dev_dbg(dev, "Changed ERP_SLOT: short_slot_time %i\n",
4504 bss_conf->use_short_slot);
4505
4506 if (bss_conf->use_short_slot)
4507 val8 = 9;
4508 else
4509 val8 = 20;
4510 rtl8xxxu_write8(priv, REG_SLOT, val8);
4511 }
4512
4513 if (changed & BSS_CHANGED_BSSID) {
4514 dev_dbg(dev, "Changed BSSID!\n");
4515 rtl8xxxu_set_bssid(priv, bss_conf->bssid);
4516 }
4517
4518 if (changed & BSS_CHANGED_BASIC_RATES) {
4519 dev_dbg(dev, "Changed BASIC_RATES!\n");
4520 rtl8xxxu_set_basic_rates(priv, bss_conf->basic_rates);
4521 }
4522error:
4523 return;
4524}
4525
4526static u32 rtl8xxxu_80211_to_rtl_queue(u32 queue)
4527{
4528 u32 rtlqueue;
4529
4530 switch (queue) {
4531 case IEEE80211_AC_VO:
4532 rtlqueue = TXDESC_QUEUE_VO;
4533 break;
4534 case IEEE80211_AC_VI:
4535 rtlqueue = TXDESC_QUEUE_VI;
4536 break;
4537 case IEEE80211_AC_BE:
4538 rtlqueue = TXDESC_QUEUE_BE;
4539 break;
4540 case IEEE80211_AC_BK:
4541 rtlqueue = TXDESC_QUEUE_BK;
4542 break;
4543 default:
4544 rtlqueue = TXDESC_QUEUE_BE;
4545 }
4546
4547 return rtlqueue;
4548}
4549
4550static u32 rtl8xxxu_queue_select(struct ieee80211_hw *hw, struct sk_buff *skb)
4551{
4552 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4553 u32 queue;
4554
4555 if (ieee80211_is_mgmt(hdr->frame_control))
4556 queue = TXDESC_QUEUE_MGNT;
4557 else
4558 queue = rtl8xxxu_80211_to_rtl_queue(skb_get_queue_mapping(skb));
4559
4560 return queue;
4561}
4562
Jes Sorensen179e1742016-02-29 17:05:27 -05004563/*
4564 * Despite newer chips 8723b/8812/8821 having a larger TX descriptor
4565 * format. The descriptor checksum is still only calculated over the
4566 * initial 32 bytes of the descriptor!
4567 */
Jes Sorensendbb28962016-03-31 17:08:33 -04004568static void rtl8xxxu_calc_tx_desc_csum(struct rtl8xxxu_txdesc32 *tx_desc)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004569{
4570 __le16 *ptr = (__le16 *)tx_desc;
4571 u16 csum = 0;
4572 int i;
4573
4574 /*
4575 * Clear csum field before calculation, as the csum field is
4576 * in the middle of the struct.
4577 */
4578 tx_desc->csum = cpu_to_le16(0);
4579
Jes Sorensendbb28962016-03-31 17:08:33 -04004580 for (i = 0; i < (sizeof(struct rtl8xxxu_txdesc32) / sizeof(u16)); i++)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004581 csum = csum ^ le16_to_cpu(ptr[i]);
4582
4583 tx_desc->csum |= cpu_to_le16(csum);
4584}
4585
4586static void rtl8xxxu_free_tx_resources(struct rtl8xxxu_priv *priv)
4587{
4588 struct rtl8xxxu_tx_urb *tx_urb, *tmp;
4589 unsigned long flags;
4590
4591 spin_lock_irqsave(&priv->tx_urb_lock, flags);
4592 list_for_each_entry_safe(tx_urb, tmp, &priv->tx_urb_free_list, list) {
4593 list_del(&tx_urb->list);
4594 priv->tx_urb_free_count--;
4595 usb_free_urb(&tx_urb->urb);
4596 }
4597 spin_unlock_irqrestore(&priv->tx_urb_lock, flags);
4598}
4599
4600static struct rtl8xxxu_tx_urb *
4601rtl8xxxu_alloc_tx_urb(struct rtl8xxxu_priv *priv)
4602{
4603 struct rtl8xxxu_tx_urb *tx_urb;
4604 unsigned long flags;
4605
4606 spin_lock_irqsave(&priv->tx_urb_lock, flags);
4607 tx_urb = list_first_entry_or_null(&priv->tx_urb_free_list,
4608 struct rtl8xxxu_tx_urb, list);
4609 if (tx_urb) {
4610 list_del(&tx_urb->list);
4611 priv->tx_urb_free_count--;
4612 if (priv->tx_urb_free_count < RTL8XXXU_TX_URB_LOW_WATER &&
4613 !priv->tx_stopped) {
4614 priv->tx_stopped = true;
4615 ieee80211_stop_queues(priv->hw);
4616 }
4617 }
4618
4619 spin_unlock_irqrestore(&priv->tx_urb_lock, flags);
4620
4621 return tx_urb;
4622}
4623
4624static void rtl8xxxu_free_tx_urb(struct rtl8xxxu_priv *priv,
4625 struct rtl8xxxu_tx_urb *tx_urb)
4626{
4627 unsigned long flags;
4628
4629 INIT_LIST_HEAD(&tx_urb->list);
4630
4631 spin_lock_irqsave(&priv->tx_urb_lock, flags);
4632
4633 list_add(&tx_urb->list, &priv->tx_urb_free_list);
4634 priv->tx_urb_free_count++;
4635 if (priv->tx_urb_free_count > RTL8XXXU_TX_URB_HIGH_WATER &&
4636 priv->tx_stopped) {
4637 priv->tx_stopped = false;
4638 ieee80211_wake_queues(priv->hw);
4639 }
4640
4641 spin_unlock_irqrestore(&priv->tx_urb_lock, flags);
4642}
4643
4644static void rtl8xxxu_tx_complete(struct urb *urb)
4645{
4646 struct sk_buff *skb = (struct sk_buff *)urb->context;
4647 struct ieee80211_tx_info *tx_info;
4648 struct ieee80211_hw *hw;
Jes Sorensen179e1742016-02-29 17:05:27 -05004649 struct rtl8xxxu_priv *priv;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004650 struct rtl8xxxu_tx_urb *tx_urb =
4651 container_of(urb, struct rtl8xxxu_tx_urb, urb);
4652
4653 tx_info = IEEE80211_SKB_CB(skb);
4654 hw = tx_info->rate_driver_data[0];
Jes Sorensen179e1742016-02-29 17:05:27 -05004655 priv = hw->priv;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004656
Jes Sorensen179e1742016-02-29 17:05:27 -05004657 skb_pull(skb, priv->fops->tx_desc_size);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004658
4659 ieee80211_tx_info_clear_status(tx_info);
4660 tx_info->status.rates[0].idx = -1;
4661 tx_info->status.rates[0].count = 0;
4662
4663 if (!urb->status)
4664 tx_info->flags |= IEEE80211_TX_STAT_ACK;
4665
4666 ieee80211_tx_status_irqsafe(hw, skb);
4667
Jes Sorensen179e1742016-02-29 17:05:27 -05004668 rtl8xxxu_free_tx_urb(priv, tx_urb);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004669}
4670
4671static void rtl8xxxu_dump_action(struct device *dev,
4672 struct ieee80211_hdr *hdr)
4673{
4674 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)hdr;
4675 u16 cap, timeout;
4676
4677 if (!(rtl8xxxu_debug & RTL8XXXU_DEBUG_ACTION))
4678 return;
4679
4680 switch (mgmt->u.action.u.addba_resp.action_code) {
4681 case WLAN_ACTION_ADDBA_RESP:
4682 cap = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
4683 timeout = le16_to_cpu(mgmt->u.action.u.addba_resp.timeout);
4684 dev_info(dev, "WLAN_ACTION_ADDBA_RESP: "
4685 "timeout %i, tid %02x, buf_size %02x, policy %02x, "
4686 "status %02x\n",
4687 timeout,
4688 (cap & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2,
4689 (cap & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6,
4690 (cap >> 1) & 0x1,
4691 le16_to_cpu(mgmt->u.action.u.addba_resp.status));
4692 break;
4693 case WLAN_ACTION_ADDBA_REQ:
4694 cap = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
4695 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
4696 dev_info(dev, "WLAN_ACTION_ADDBA_REQ: "
4697 "timeout %i, tid %02x, buf_size %02x, policy %02x\n",
4698 timeout,
4699 (cap & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2,
4700 (cap & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6,
4701 (cap >> 1) & 0x1);
4702 break;
4703 default:
4704 dev_info(dev, "action frame %02x\n",
4705 mgmt->u.action.u.addba_resp.action_code);
4706 break;
4707 }
4708}
4709
4710static void rtl8xxxu_tx(struct ieee80211_hw *hw,
4711 struct ieee80211_tx_control *control,
4712 struct sk_buff *skb)
4713{
4714 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4715 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
4716 struct ieee80211_rate *tx_rate = ieee80211_get_tx_rate(hw, tx_info);
4717 struct rtl8xxxu_priv *priv = hw->priv;
Jes Sorensendbb28962016-03-31 17:08:33 -04004718 struct rtl8xxxu_txdesc32 *tx_desc;
4719 struct rtl8xxxu_txdesc40 *tx_desc40;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004720 struct rtl8xxxu_tx_urb *tx_urb;
4721 struct ieee80211_sta *sta = NULL;
4722 struct ieee80211_vif *vif = tx_info->control.vif;
4723 struct device *dev = &priv->udev->dev;
4724 u32 queue, rate;
4725 u16 pktlen = skb->len;
4726 u16 seq_number;
4727 u16 rate_flag = tx_info->control.rates[0].flags;
Jes Sorensen179e1742016-02-29 17:05:27 -05004728 int tx_desc_size = priv->fops->tx_desc_size;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004729 int ret;
Jes Sorensencc2646d2016-02-29 17:05:32 -05004730 bool usedesc40, ampdu_enable;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004731
Jes Sorensen179e1742016-02-29 17:05:27 -05004732 if (skb_headroom(skb) < tx_desc_size) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004733 dev_warn(dev,
4734 "%s: Not enough headroom (%i) for tx descriptor\n",
4735 __func__, skb_headroom(skb));
4736 goto error;
4737 }
4738
Jes Sorensen179e1742016-02-29 17:05:27 -05004739 if (unlikely(skb->len > (65535 - tx_desc_size))) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004740 dev_warn(dev, "%s: Trying to send over-sized skb (%i)\n",
4741 __func__, skb->len);
4742 goto error;
4743 }
4744
4745 tx_urb = rtl8xxxu_alloc_tx_urb(priv);
4746 if (!tx_urb) {
4747 dev_warn(dev, "%s: Unable to allocate tx urb\n", __func__);
4748 goto error;
4749 }
4750
4751 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_TX)
4752 dev_info(dev, "%s: TX rate: %d (%d), pkt size %d\n",
4753 __func__, tx_rate->bitrate, tx_rate->hw_value, pktlen);
4754
4755 if (ieee80211_is_action(hdr->frame_control))
4756 rtl8xxxu_dump_action(dev, hdr);
4757
Jes Sorensencc2646d2016-02-29 17:05:32 -05004758 usedesc40 = (tx_desc_size == 40);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004759 tx_info->rate_driver_data[0] = hw;
4760
4761 if (control && control->sta)
4762 sta = control->sta;
4763
Jes Sorensendbb28962016-03-31 17:08:33 -04004764 tx_desc = (struct rtl8xxxu_txdesc32 *)skb_push(skb, tx_desc_size);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004765
Jes Sorensen179e1742016-02-29 17:05:27 -05004766 memset(tx_desc, 0, tx_desc_size);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004767 tx_desc->pkt_size = cpu_to_le16(pktlen);
Jes Sorensen179e1742016-02-29 17:05:27 -05004768 tx_desc->pkt_offset = tx_desc_size;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004769
4770 tx_desc->txdw0 =
4771 TXDESC_OWN | TXDESC_FIRST_SEGMENT | TXDESC_LAST_SEGMENT;
4772 if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
4773 is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
4774 tx_desc->txdw0 |= TXDESC_BROADMULTICAST;
4775
4776 queue = rtl8xxxu_queue_select(hw, skb);
4777 tx_desc->txdw1 = cpu_to_le32(queue << TXDESC_QUEUE_SHIFT);
4778
4779 if (tx_info->control.hw_key) {
4780 switch (tx_info->control.hw_key->cipher) {
4781 case WLAN_CIPHER_SUITE_WEP40:
4782 case WLAN_CIPHER_SUITE_WEP104:
4783 case WLAN_CIPHER_SUITE_TKIP:
4784 tx_desc->txdw1 |= cpu_to_le32(TXDESC_SEC_RC4);
4785 break;
4786 case WLAN_CIPHER_SUITE_CCMP:
4787 tx_desc->txdw1 |= cpu_to_le32(TXDESC_SEC_AES);
4788 break;
4789 default:
4790 break;
4791 }
4792 }
4793
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004794 /* (tx_info->flags & IEEE80211_TX_CTL_AMPDU) && */
Jes Sorensena40ace42016-02-29 17:05:31 -05004795 ampdu_enable = false;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004796 if (ieee80211_is_data_qos(hdr->frame_control) && sta) {
4797 if (sta->ht_cap.ht_supported) {
4798 u32 ampdu, val32;
4799
4800 ampdu = (u32)sta->ht_cap.ampdu_density;
4801 val32 = ampdu << TXDESC_AMPDU_DENSITY_SHIFT;
4802 tx_desc->txdw2 |= cpu_to_le32(val32);
Jes Sorensence2d1db2016-02-29 17:05:30 -05004803
Jes Sorensena40ace42016-02-29 17:05:31 -05004804 ampdu_enable = true;
4805 }
4806 }
4807
Jes Sorensen4c683602016-02-29 17:05:35 -05004808 if (rate_flag & IEEE80211_TX_RC_MCS)
4809 rate = tx_info->control.rates[0].idx + DESC_RATE_MCS0;
4810 else
4811 rate = tx_rate->hw_value;
4812
Jes Sorensencc2646d2016-02-29 17:05:32 -05004813 seq_number = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
4814 if (!usedesc40) {
Jes Sorensen4c683602016-02-29 17:05:35 -05004815 tx_desc->txdw5 = cpu_to_le32(rate);
4816
4817 if (ieee80211_is_data(hdr->frame_control))
4818 tx_desc->txdw5 |= cpu_to_le32(0x0001ff00);
4819
Jes Sorensencc2646d2016-02-29 17:05:32 -05004820 tx_desc->txdw3 =
Jes Sorensen33f37242016-03-31 17:08:34 -04004821 cpu_to_le32((u32)seq_number << TXDESC32_SEQ_SHIFT);
Jes Sorensencc2646d2016-02-29 17:05:32 -05004822
Jes Sorensena40ace42016-02-29 17:05:31 -05004823 if (ampdu_enable)
Jes Sorensen33f37242016-03-31 17:08:34 -04004824 tx_desc->txdw1 |= cpu_to_le32(TXDESC32_AGG_ENABLE);
Jes Sorensena40ace42016-02-29 17:05:31 -05004825 else
Jes Sorensen33f37242016-03-31 17:08:34 -04004826 tx_desc->txdw1 |= cpu_to_le32(TXDESC32_AGG_BREAK);
Jes Sorensen4c683602016-02-29 17:05:35 -05004827
4828 if (ieee80211_is_mgmt(hdr->frame_control)) {
4829 tx_desc->txdw5 = cpu_to_le32(tx_rate->hw_value);
4830 tx_desc->txdw4 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004831 cpu_to_le32(TXDESC32_USE_DRIVER_RATE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004832 tx_desc->txdw5 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004833 cpu_to_le32(6 << TXDESC32_RETRY_LIMIT_SHIFT);
Jes Sorensen4c683602016-02-29 17:05:35 -05004834 tx_desc->txdw5 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004835 cpu_to_le32(TXDESC32_RETRY_LIMIT_ENABLE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004836 }
4837
4838 if (ieee80211_is_data_qos(hdr->frame_control))
Jes Sorensen33f37242016-03-31 17:08:34 -04004839 tx_desc->txdw4 |= cpu_to_le32(TXDESC32_QOS);
Jes Sorensen4c683602016-02-29 17:05:35 -05004840
4841 if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ||
4842 (sta && vif && vif->bss_conf.use_short_preamble))
Jes Sorensen33f37242016-03-31 17:08:34 -04004843 tx_desc->txdw4 |= cpu_to_le32(TXDESC32_SHORT_PREAMBLE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004844
4845 if (rate_flag & IEEE80211_TX_RC_SHORT_GI ||
4846 (ieee80211_is_data_qos(hdr->frame_control) &&
4847 sta && sta->ht_cap.cap &
4848 (IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_SGI_20))) {
Jes Sorensen1df1de32016-03-31 17:08:36 -04004849 tx_desc->txdw5 |= cpu_to_le32(TXDESC32_SHORT_GI);
Jes Sorensen4c683602016-02-29 17:05:35 -05004850 }
4851
4852 if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
4853 /*
4854 * Use RTS rate 24M - does the mac80211 tell
4855 * us which to use?
4856 */
4857 tx_desc->txdw4 |=
4858 cpu_to_le32(DESC_RATE_24M <<
Jes Sorensen33f37242016-03-31 17:08:34 -04004859 TXDESC32_RTS_RATE_SHIFT);
Jes Sorensen4c683602016-02-29 17:05:35 -05004860 tx_desc->txdw4 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004861 cpu_to_le32(TXDESC32_RTS_CTS_ENABLE);
4862 tx_desc->txdw4 |= cpu_to_le32(TXDESC32_HW_RTS_ENABLE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004863 }
Jes Sorensena40ace42016-02-29 17:05:31 -05004864 } else {
Jes Sorensendbb28962016-03-31 17:08:33 -04004865 tx_desc40 = (struct rtl8xxxu_txdesc40 *)tx_desc;
Jes Sorensencc2646d2016-02-29 17:05:32 -05004866
Jes Sorensen4c683602016-02-29 17:05:35 -05004867 tx_desc40->txdw4 = cpu_to_le32(rate);
4868 if (ieee80211_is_data(hdr->frame_control)) {
4869 tx_desc->txdw4 |=
4870 cpu_to_le32(0x1f <<
Jes Sorensen33f37242016-03-31 17:08:34 -04004871 TXDESC40_DATA_RATE_FB_SHIFT);
Jes Sorensen4c683602016-02-29 17:05:35 -05004872 }
4873
Jes Sorensencc2646d2016-02-29 17:05:32 -05004874 tx_desc40->txdw9 =
Jes Sorensen33f37242016-03-31 17:08:34 -04004875 cpu_to_le32((u32)seq_number << TXDESC40_SEQ_SHIFT);
Jes Sorensencc2646d2016-02-29 17:05:32 -05004876
Jes Sorensena40ace42016-02-29 17:05:31 -05004877 if (ampdu_enable)
Jes Sorensen33f37242016-03-31 17:08:34 -04004878 tx_desc40->txdw2 |= cpu_to_le32(TXDESC40_AGG_ENABLE);
Jes Sorensena40ace42016-02-29 17:05:31 -05004879 else
Jes Sorensen33f37242016-03-31 17:08:34 -04004880 tx_desc40->txdw2 |= cpu_to_le32(TXDESC40_AGG_BREAK);
Jes Sorensen4c683602016-02-29 17:05:35 -05004881
4882 if (ieee80211_is_mgmt(hdr->frame_control)) {
4883 tx_desc40->txdw4 = cpu_to_le32(tx_rate->hw_value);
4884 tx_desc40->txdw3 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004885 cpu_to_le32(TXDESC40_USE_DRIVER_RATE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004886 tx_desc40->txdw4 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004887 cpu_to_le32(6 << TXDESC40_RETRY_LIMIT_SHIFT);
Jes Sorensen4c683602016-02-29 17:05:35 -05004888 tx_desc40->txdw4 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004889 cpu_to_le32(TXDESC40_RETRY_LIMIT_ENABLE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004890 }
4891
4892 if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ||
4893 (sta && vif && vif->bss_conf.use_short_preamble))
4894 tx_desc40->txdw5 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004895 cpu_to_le32(TXDESC40_SHORT_PREAMBLE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004896
4897 if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
4898 /*
4899 * Use RTS rate 24M - does the mac80211 tell
4900 * us which to use?
4901 */
4902 tx_desc->txdw4 |=
4903 cpu_to_le32(DESC_RATE_24M <<
Jes Sorensen33f37242016-03-31 17:08:34 -04004904 TXDESC40_RTS_RATE_SHIFT);
4905 tx_desc->txdw3 |= cpu_to_le32(TXDESC40_RTS_CTS_ENABLE);
4906 tx_desc->txdw3 |= cpu_to_le32(TXDESC40_HW_RTS_ENABLE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004907 }
Jes Sorensen69794942016-02-29 17:05:43 -05004908 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004909
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004910 rtl8xxxu_calc_tx_desc_csum(tx_desc);
4911
4912 usb_fill_bulk_urb(&tx_urb->urb, priv->udev, priv->pipe_out[queue],
4913 skb->data, skb->len, rtl8xxxu_tx_complete, skb);
4914
4915 usb_anchor_urb(&tx_urb->urb, &priv->tx_anchor);
4916 ret = usb_submit_urb(&tx_urb->urb, GFP_ATOMIC);
4917 if (ret) {
4918 usb_unanchor_urb(&tx_urb->urb);
4919 rtl8xxxu_free_tx_urb(priv, tx_urb);
4920 goto error;
4921 }
4922 return;
4923error:
4924 dev_kfree_skb(skb);
4925}
4926
4927static void rtl8xxxu_rx_parse_phystats(struct rtl8xxxu_priv *priv,
4928 struct ieee80211_rx_status *rx_status,
Jes Sorensen87957082016-02-29 17:05:42 -05004929 struct rtl8723au_phy_stats *phy_stats,
4930 u32 rxmcs)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004931{
4932 if (phy_stats->sgi_en)
4933 rx_status->flag |= RX_FLAG_SHORT_GI;
4934
Jes Sorensen87957082016-02-29 17:05:42 -05004935 if (rxmcs < DESC_RATE_6M) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004936 /*
4937 * Handle PHY stats for CCK rates
4938 */
4939 u8 cck_agc_rpt = phy_stats->cck_agc_rpt_ofdm_cfosho_a;
4940
4941 switch (cck_agc_rpt & 0xc0) {
4942 case 0xc0:
4943 rx_status->signal = -46 - (cck_agc_rpt & 0x3e);
4944 break;
4945 case 0x80:
4946 rx_status->signal = -26 - (cck_agc_rpt & 0x3e);
4947 break;
4948 case 0x40:
4949 rx_status->signal = -12 - (cck_agc_rpt & 0x3e);
4950 break;
4951 case 0x00:
4952 rx_status->signal = 16 - (cck_agc_rpt & 0x3e);
4953 break;
4954 }
4955 } else {
4956 rx_status->signal =
4957 (phy_stats->cck_sig_qual_ofdm_pwdb_all >> 1) - 110;
4958 }
4959}
4960
4961static void rtl8xxxu_free_rx_resources(struct rtl8xxxu_priv *priv)
4962{
4963 struct rtl8xxxu_rx_urb *rx_urb, *tmp;
4964 unsigned long flags;
4965
4966 spin_lock_irqsave(&priv->rx_urb_lock, flags);
4967
4968 list_for_each_entry_safe(rx_urb, tmp,
4969 &priv->rx_urb_pending_list, list) {
4970 list_del(&rx_urb->list);
4971 priv->rx_urb_pending_count--;
4972 usb_free_urb(&rx_urb->urb);
4973 }
4974
4975 spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
4976}
4977
4978static void rtl8xxxu_queue_rx_urb(struct rtl8xxxu_priv *priv,
4979 struct rtl8xxxu_rx_urb *rx_urb)
4980{
4981 struct sk_buff *skb;
4982 unsigned long flags;
4983 int pending = 0;
4984
4985 spin_lock_irqsave(&priv->rx_urb_lock, flags);
4986
4987 if (!priv->shutdown) {
4988 list_add_tail(&rx_urb->list, &priv->rx_urb_pending_list);
4989 priv->rx_urb_pending_count++;
4990 pending = priv->rx_urb_pending_count;
4991 } else {
4992 skb = (struct sk_buff *)rx_urb->urb.context;
4993 dev_kfree_skb(skb);
4994 usb_free_urb(&rx_urb->urb);
4995 }
4996
4997 spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
4998
4999 if (pending > RTL8XXXU_RX_URB_PENDING_WATER)
5000 schedule_work(&priv->rx_urb_wq);
5001}
5002
5003static void rtl8xxxu_rx_urb_work(struct work_struct *work)
5004{
5005 struct rtl8xxxu_priv *priv;
5006 struct rtl8xxxu_rx_urb *rx_urb, *tmp;
5007 struct list_head local;
5008 struct sk_buff *skb;
5009 unsigned long flags;
5010 int ret;
5011
5012 priv = container_of(work, struct rtl8xxxu_priv, rx_urb_wq);
5013 INIT_LIST_HEAD(&local);
5014
5015 spin_lock_irqsave(&priv->rx_urb_lock, flags);
5016
5017 list_splice_init(&priv->rx_urb_pending_list, &local);
5018 priv->rx_urb_pending_count = 0;
5019
5020 spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
5021
5022 list_for_each_entry_safe(rx_urb, tmp, &local, list) {
5023 list_del_init(&rx_urb->list);
5024 ret = rtl8xxxu_submit_rx_urb(priv, rx_urb);
5025 /*
5026 * If out of memory or temporary error, put it back on the
5027 * queue and try again. Otherwise the device is dead/gone
5028 * and we should drop it.
5029 */
5030 switch (ret) {
5031 case 0:
5032 break;
5033 case -ENOMEM:
5034 case -EAGAIN:
5035 rtl8xxxu_queue_rx_urb(priv, rx_urb);
5036 break;
5037 default:
5038 pr_info("failed to requeue urb %i\n", ret);
5039 skb = (struct sk_buff *)rx_urb->urb.context;
5040 dev_kfree_skb(skb);
5041 usb_free_urb(&rx_urb->urb);
5042 }
5043 }
5044}
5045
Jes Sorensena635df82016-06-27 12:32:00 -04005046static void rtl8723bu_handle_c2h(struct rtl8xxxu_priv *priv,
5047 struct sk_buff *skb)
5048{
5049 struct rtl8723bu_c2h *c2h = (struct rtl8723bu_c2h *)skb->data;
5050 struct device *dev = &priv->udev->dev;
5051 int len;
5052
5053 len = skb->len - 2;
5054
5055 dev_dbg(dev, "C2H ID %02x seq %02x, len %02x source %02x\n",
5056 c2h->id, c2h->seq, len, c2h->bt_info.response_source);
5057
5058 switch(c2h->id) {
5059 case C2H_8723B_BT_INFO:
5060 if (c2h->bt_info.response_source >
5061 BT_INFO_SRC_8723B_BT_ACTIVE_SEND)
5062 dev_dbg(dev, "C2H_BT_INFO WiFi only firmware\n");
5063 else
5064 dev_dbg(dev, "C2H_BT_INFO BT/WiFi coexist firmware\n");
5065
5066 if (c2h->bt_info.bt_has_reset)
5067 dev_dbg(dev, "BT has been reset\n");
5068 if (c2h->bt_info.tx_rx_mask)
5069 dev_dbg(dev, "BT TRx mask\n");
5070
5071 break;
5072 case C2H_8723B_BT_MP_INFO:
5073 dev_dbg(dev, "C2H_MP_INFO ext ID %02x, status %02x\n",
5074 c2h->bt_mp_info.ext_id, c2h->bt_mp_info.status);
5075 break;
5076 case C2H_8723B_RA_REPORT:
5077 dev_dbg(dev,
5078 "C2H RA RPT: rate %02x, unk %i, macid %02x, noise %i\n",
5079 c2h->ra_report.rate, c2h->ra_report.dummy0_0,
5080 c2h->ra_report.macid, c2h->ra_report.noisy_state);
5081 break;
5082 default:
5083 dev_info(dev, "Unhandled C2H event %02x seq %02x\n",
5084 c2h->id, c2h->seq);
5085 print_hex_dump(KERN_INFO, "C2H content: ", DUMP_PREFIX_NONE,
5086 16, 1, c2h->raw.payload, len, false);
5087 break;
5088 }
5089}
5090
Jes Sorensen2db125d2016-06-27 12:32:01 -04005091int rtl8xxxu_parse_rxdesc16(struct rtl8xxxu_priv *priv, struct sk_buff *skb)
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005092{
Jes Sorensenc14ee432016-06-27 12:31:59 -04005093 struct ieee80211_hw *hw = priv->hw;
Jes Sorensen040b97b2016-06-27 12:32:03 -04005094 struct ieee80211_rx_status *rx_status;
5095 struct rtl8xxxu_rxdesc16 *rx_desc;
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005096 struct rtl8723au_phy_stats *phy_stats;
Jes Sorensen040b97b2016-06-27 12:32:03 -04005097 struct sk_buff *next_skb = NULL;
5098 __le32 *_rx_desc_le;
5099 u32 *_rx_desc;
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005100 int drvinfo_sz, desc_shift;
Jes Sorensen040b97b2016-06-27 12:32:03 -04005101 int i, pkt_cnt, pkt_len, urb_len, pkt_offset;
Jes Sorensen2cb79eb2016-04-14 14:58:51 -04005102
Jes Sorensen040b97b2016-06-27 12:32:03 -04005103 urb_len = skb->len;
5104 pkt_cnt = 0;
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005105
Jes Sorensen040b97b2016-06-27 12:32:03 -04005106 do {
5107 rx_desc = (struct rtl8xxxu_rxdesc16 *)skb->data;
5108 _rx_desc_le = (__le32 *)skb->data;
5109 _rx_desc = (u32 *)skb->data;
Jes Sorensenc14ee432016-06-27 12:31:59 -04005110
Jes Sorensen040b97b2016-06-27 12:32:03 -04005111 for (i = 0;
5112 i < (sizeof(struct rtl8xxxu_rxdesc16) / sizeof(u32)); i++)
5113 _rx_desc[i] = le32_to_cpu(_rx_desc_le[i]);
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005114
Jes Sorensen040b97b2016-06-27 12:32:03 -04005115 /*
5116 * Only read pkt_cnt from the header if we're parsing the
5117 * first packet
5118 */
5119 if (!pkt_cnt)
5120 pkt_cnt = rx_desc->pkt_cnt;
5121 pkt_len = rx_desc->pktlen;
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005122
Jes Sorensen040b97b2016-06-27 12:32:03 -04005123 drvinfo_sz = rx_desc->drvinfo_sz * 8;
5124 desc_shift = rx_desc->shift;
5125 pkt_offset = roundup(pkt_len + drvinfo_sz + desc_shift +
5126 sizeof(struct rtl8xxxu_rxdesc16), 128);
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005127
Jes Sorensen040b97b2016-06-27 12:32:03 -04005128 if (pkt_cnt > 1)
5129 next_skb = skb_clone(skb, GFP_ATOMIC);
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005130
Jes Sorensen040b97b2016-06-27 12:32:03 -04005131 rx_status = IEEE80211_SKB_RXCB(skb);
5132 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005133
Jes Sorensen040b97b2016-06-27 12:32:03 -04005134 skb_pull(skb, sizeof(struct rtl8xxxu_rxdesc16));
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005135
Jes Sorensen040b97b2016-06-27 12:32:03 -04005136 phy_stats = (struct rtl8723au_phy_stats *)skb->data;
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005137
Jes Sorensen040b97b2016-06-27 12:32:03 -04005138 skb_pull(skb, drvinfo_sz + desc_shift);
Jes Sorensenc14ee432016-06-27 12:31:59 -04005139
Jes Sorensen040b97b2016-06-27 12:32:03 -04005140 skb_trim(skb, pkt_len);
5141
5142 if (rx_desc->phy_stats)
5143 rtl8xxxu_rx_parse_phystats(priv, rx_status, phy_stats,
5144 rx_desc->rxmcs);
5145
5146 rx_status->mactime = le32_to_cpu(rx_desc->tsfl);
5147 rx_status->flag |= RX_FLAG_MACTIME_START;
5148
5149 if (!rx_desc->swdec)
5150 rx_status->flag |= RX_FLAG_DECRYPTED;
5151 if (rx_desc->crc32)
5152 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
5153 if (rx_desc->bw)
5154 rx_status->flag |= RX_FLAG_40MHZ;
5155
5156 if (rx_desc->rxht) {
5157 rx_status->flag |= RX_FLAG_HT;
5158 rx_status->rate_idx = rx_desc->rxmcs - DESC_RATE_MCS0;
5159 } else {
5160 rx_status->rate_idx = rx_desc->rxmcs;
5161 }
5162
5163 rx_status->freq = hw->conf.chandef.chan->center_freq;
5164 rx_status->band = hw->conf.chandef.chan->band;
5165
5166 ieee80211_rx_irqsafe(hw, skb);
5167
5168 skb = next_skb;
5169 if (skb)
5170 skb_pull(next_skb, pkt_offset);
5171
5172 pkt_cnt--;
5173 urb_len -= pkt_offset;
5174 } while (skb && urb_len > 0 && pkt_cnt > 0);
5175
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005176 return RX_TYPE_DATA_PKT;
5177}
5178
Jes Sorensen2db125d2016-06-27 12:32:01 -04005179int rtl8xxxu_parse_rxdesc24(struct rtl8xxxu_priv *priv, struct sk_buff *skb)
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005180{
Jes Sorensenc14ee432016-06-27 12:31:59 -04005181 struct ieee80211_hw *hw = priv->hw;
Jes Sorensen2db125d2016-06-27 12:32:01 -04005182 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
Jes Sorensena49c7ce2016-04-14 14:58:52 -04005183 struct rtl8xxxu_rxdesc24 *rx_desc =
5184 (struct rtl8xxxu_rxdesc24 *)skb->data;
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005185 struct rtl8723au_phy_stats *phy_stats;
Jes Sorensen2cb79eb2016-04-14 14:58:51 -04005186 __le32 *_rx_desc_le = (__le32 *)skb->data;
5187 u32 *_rx_desc = (u32 *)skb->data;
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005188 int drvinfo_sz, desc_shift;
Jes Sorensen2cb79eb2016-04-14 14:58:51 -04005189 int i;
5190
Jes Sorensena49c7ce2016-04-14 14:58:52 -04005191 for (i = 0; i < (sizeof(struct rtl8xxxu_rxdesc24) / sizeof(u32)); i++)
Jes Sorensen2cb79eb2016-04-14 14:58:51 -04005192 _rx_desc[i] = le32_to_cpu(_rx_desc_le[i]);
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005193
Jes Sorensenc14ee432016-06-27 12:31:59 -04005194 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
5195
Jes Sorensena49c7ce2016-04-14 14:58:52 -04005196 skb_pull(skb, sizeof(struct rtl8xxxu_rxdesc24));
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005197
5198 phy_stats = (struct rtl8723au_phy_stats *)skb->data;
5199
5200 drvinfo_sz = rx_desc->drvinfo_sz * 8;
5201 desc_shift = rx_desc->shift;
5202 skb_pull(skb, drvinfo_sz + desc_shift);
5203
Jes Sorensene975b872016-02-29 17:05:36 -05005204 if (rx_desc->rpt_sel) {
5205 struct device *dev = &priv->udev->dev;
5206 dev_dbg(dev, "%s: C2H packet\n", __func__);
Jes Sorensena635df82016-06-27 12:32:00 -04005207 rtl8723bu_handle_c2h(priv, skb);
5208 dev_kfree_skb(skb);
Jes Sorensene975b872016-02-29 17:05:36 -05005209 return RX_TYPE_C2H;
5210 }
5211
Jes Sorensen87957082016-02-29 17:05:42 -05005212 if (rx_desc->phy_stats)
5213 rtl8xxxu_rx_parse_phystats(priv, rx_status, phy_stats,
5214 rx_desc->rxmcs);
5215
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005216 rx_status->mactime = le32_to_cpu(rx_desc->tsfl);
5217 rx_status->flag |= RX_FLAG_MACTIME_START;
5218
5219 if (!rx_desc->swdec)
5220 rx_status->flag |= RX_FLAG_DECRYPTED;
5221 if (rx_desc->crc32)
5222 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
5223 if (rx_desc->bw)
5224 rx_status->flag |= RX_FLAG_40MHZ;
5225
5226 if (rx_desc->rxmcs >= DESC_RATE_MCS0) {
5227 rx_status->flag |= RX_FLAG_HT;
5228 rx_status->rate_idx = rx_desc->rxmcs - DESC_RATE_MCS0;
5229 } else {
5230 rx_status->rate_idx = rx_desc->rxmcs;
5231 }
5232
Jes Sorensenc14ee432016-06-27 12:31:59 -04005233 rx_status->freq = hw->conf.chandef.chan->center_freq;
5234 rx_status->band = hw->conf.chandef.chan->band;
5235
Jes Sorensena635df82016-06-27 12:32:00 -04005236 ieee80211_rx_irqsafe(hw, skb);
Jes Sorensene975b872016-02-29 17:05:36 -05005237 return RX_TYPE_DATA_PKT;
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005238}
5239
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005240static void rtl8xxxu_rx_complete(struct urb *urb)
5241{
5242 struct rtl8xxxu_rx_urb *rx_urb =
5243 container_of(urb, struct rtl8xxxu_rx_urb, urb);
5244 struct ieee80211_hw *hw = rx_urb->hw;
5245 struct rtl8xxxu_priv *priv = hw->priv;
5246 struct sk_buff *skb = (struct sk_buff *)urb->context;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005247 struct device *dev = &priv->udev->dev;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005248
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005249 skb_put(skb, urb->actual_length);
5250
5251 if (urb->status == 0) {
Jes Sorensen2db125d2016-06-27 12:32:01 -04005252 priv->fops->parse_rx_desc(priv, skb);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005253
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005254 skb = NULL;
5255 rx_urb->urb.context = NULL;
5256 rtl8xxxu_queue_rx_urb(priv, rx_urb);
5257 } else {
5258 dev_dbg(dev, "%s: status %i\n", __func__, urb->status);
5259 goto cleanup;
5260 }
5261 return;
5262
5263cleanup:
5264 usb_free_urb(urb);
5265 dev_kfree_skb(skb);
5266 return;
5267}
5268
5269static int rtl8xxxu_submit_rx_urb(struct rtl8xxxu_priv *priv,
5270 struct rtl8xxxu_rx_urb *rx_urb)
5271{
Jes Sorensen04319ae2016-06-27 12:32:04 -04005272 struct rtl8xxxu_fileops *fops = priv->fops;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005273 struct sk_buff *skb;
5274 int skb_size;
Jes Sorensena49c7ce2016-04-14 14:58:52 -04005275 int ret, rx_desc_sz;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005276
Jes Sorensen04319ae2016-06-27 12:32:04 -04005277 rx_desc_sz = fops->rx_desc_size;
5278
Jes Sorensen1e5b3b32016-06-27 12:32:05 -04005279 if (priv->rx_buf_aggregation && fops->rx_agg_buf_size) {
Jes Sorensen04319ae2016-06-27 12:32:04 -04005280 skb_size = fops->rx_agg_buf_size;
Jes Sorensen1e5b3b32016-06-27 12:32:05 -04005281 skb_size += (rx_desc_sz + sizeof(struct rtl8723au_phy_stats));
5282 } else {
Jes Sorensen04319ae2016-06-27 12:32:04 -04005283 skb_size = IEEE80211_MAX_FRAME_LEN;
Jes Sorensen1e5b3b32016-06-27 12:32:05 -04005284 }
Jes Sorensen04319ae2016-06-27 12:32:04 -04005285
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005286 skb = __netdev_alloc_skb(NULL, skb_size, GFP_KERNEL);
5287 if (!skb)
5288 return -ENOMEM;
5289
Jes Sorensena49c7ce2016-04-14 14:58:52 -04005290 memset(skb->data, 0, rx_desc_sz);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005291 usb_fill_bulk_urb(&rx_urb->urb, priv->udev, priv->pipe_in, skb->data,
5292 skb_size, rtl8xxxu_rx_complete, skb);
5293 usb_anchor_urb(&rx_urb->urb, &priv->rx_anchor);
5294 ret = usb_submit_urb(&rx_urb->urb, GFP_ATOMIC);
5295 if (ret)
5296 usb_unanchor_urb(&rx_urb->urb);
5297 return ret;
5298}
5299
5300static void rtl8xxxu_int_complete(struct urb *urb)
5301{
5302 struct rtl8xxxu_priv *priv = (struct rtl8xxxu_priv *)urb->context;
5303 struct device *dev = &priv->udev->dev;
5304 int ret;
5305
5306 dev_dbg(dev, "%s: status %i\n", __func__, urb->status);
5307 if (urb->status == 0) {
5308 usb_anchor_urb(urb, &priv->int_anchor);
5309 ret = usb_submit_urb(urb, GFP_ATOMIC);
5310 if (ret)
5311 usb_unanchor_urb(urb);
5312 } else {
Jes Sorensen78383ac2016-06-23 14:35:53 -04005313 dev_dbg(dev, "%s: Error %i\n", __func__, urb->status);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005314 }
5315}
5316
5317
5318static int rtl8xxxu_submit_int_urb(struct ieee80211_hw *hw)
5319{
5320 struct rtl8xxxu_priv *priv = hw->priv;
5321 struct urb *urb;
5322 u32 val32;
5323 int ret;
5324
5325 urb = usb_alloc_urb(0, GFP_KERNEL);
5326 if (!urb)
5327 return -ENOMEM;
5328
5329 usb_fill_int_urb(urb, priv->udev, priv->pipe_interrupt,
5330 priv->int_buf, USB_INTR_CONTENT_LENGTH,
5331 rtl8xxxu_int_complete, priv, 1);
5332 usb_anchor_urb(urb, &priv->int_anchor);
5333 ret = usb_submit_urb(urb, GFP_KERNEL);
5334 if (ret) {
5335 usb_unanchor_urb(urb);
5336 goto error;
5337 }
5338
5339 val32 = rtl8xxxu_read32(priv, REG_USB_HIMR);
5340 val32 |= USB_HIMR_CPWM;
5341 rtl8xxxu_write32(priv, REG_USB_HIMR, val32);
5342
5343error:
5344 return ret;
5345}
5346
5347static int rtl8xxxu_add_interface(struct ieee80211_hw *hw,
5348 struct ieee80211_vif *vif)
5349{
5350 struct rtl8xxxu_priv *priv = hw->priv;
5351 int ret;
5352 u8 val8;
5353
5354 switch (vif->type) {
5355 case NL80211_IFTYPE_STATION:
Jes Sorensen97db5a82016-04-28 15:19:10 -04005356 rtl8xxxu_stop_tx_beacon(priv);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005357
5358 val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
5359 val8 |= BEACON_ATIM | BEACON_FUNCTION_ENABLE |
5360 BEACON_DISABLE_TSF_UPDATE;
5361 rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
5362 ret = 0;
5363 break;
5364 default:
5365 ret = -EOPNOTSUPP;
5366 }
5367
5368 rtl8xxxu_set_linktype(priv, vif->type);
5369
5370 return ret;
5371}
5372
5373static void rtl8xxxu_remove_interface(struct ieee80211_hw *hw,
5374 struct ieee80211_vif *vif)
5375{
5376 struct rtl8xxxu_priv *priv = hw->priv;
5377
5378 dev_dbg(&priv->udev->dev, "%s\n", __func__);
5379}
5380
5381static int rtl8xxxu_config(struct ieee80211_hw *hw, u32 changed)
5382{
5383 struct rtl8xxxu_priv *priv = hw->priv;
5384 struct device *dev = &priv->udev->dev;
5385 u16 val16;
5386 int ret = 0, channel;
5387 bool ht40;
5388
5389 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_CHANNEL)
5390 dev_info(dev,
5391 "%s: channel: %i (changed %08x chandef.width %02x)\n",
5392 __func__, hw->conf.chandef.chan->hw_value,
5393 changed, hw->conf.chandef.width);
5394
5395 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
5396 val16 = ((hw->conf.long_frame_max_tx_count <<
5397 RETRY_LIMIT_LONG_SHIFT) & RETRY_LIMIT_LONG_MASK) |
5398 ((hw->conf.short_frame_max_tx_count <<
5399 RETRY_LIMIT_SHORT_SHIFT) & RETRY_LIMIT_SHORT_MASK);
5400 rtl8xxxu_write16(priv, REG_RETRY_LIMIT, val16);
5401 }
5402
5403 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
5404 switch (hw->conf.chandef.width) {
5405 case NL80211_CHAN_WIDTH_20_NOHT:
5406 case NL80211_CHAN_WIDTH_20:
5407 ht40 = false;
5408 break;
5409 case NL80211_CHAN_WIDTH_40:
5410 ht40 = true;
5411 break;
5412 default:
5413 ret = -ENOTSUPP;
5414 goto exit;
5415 }
5416
5417 channel = hw->conf.chandef.chan->hw_value;
5418
Jes Sorensene796dab2016-02-29 17:05:19 -05005419 priv->fops->set_tx_power(priv, channel, ht40);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005420
Jes Sorensen1ea8e842016-02-29 17:05:04 -05005421 priv->fops->config_channel(hw);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005422 }
5423
5424exit:
5425 return ret;
5426}
5427
5428static int rtl8xxxu_conf_tx(struct ieee80211_hw *hw,
5429 struct ieee80211_vif *vif, u16 queue,
5430 const struct ieee80211_tx_queue_params *param)
5431{
5432 struct rtl8xxxu_priv *priv = hw->priv;
5433 struct device *dev = &priv->udev->dev;
5434 u32 val32;
5435 u8 aifs, acm_ctrl, acm_bit;
5436
5437 aifs = param->aifs;
5438
5439 val32 = aifs |
5440 fls(param->cw_min) << EDCA_PARAM_ECW_MIN_SHIFT |
5441 fls(param->cw_max) << EDCA_PARAM_ECW_MAX_SHIFT |
5442 (u32)param->txop << EDCA_PARAM_TXOP_SHIFT;
5443
5444 acm_ctrl = rtl8xxxu_read8(priv, REG_ACM_HW_CTRL);
5445 dev_dbg(dev,
5446 "%s: IEEE80211 queue %02x val %08x, acm %i, acm_ctrl %02x\n",
5447 __func__, queue, val32, param->acm, acm_ctrl);
5448
5449 switch (queue) {
5450 case IEEE80211_AC_VO:
5451 acm_bit = ACM_HW_CTRL_VO;
5452 rtl8xxxu_write32(priv, REG_EDCA_VO_PARAM, val32);
5453 break;
5454 case IEEE80211_AC_VI:
5455 acm_bit = ACM_HW_CTRL_VI;
5456 rtl8xxxu_write32(priv, REG_EDCA_VI_PARAM, val32);
5457 break;
5458 case IEEE80211_AC_BE:
5459 acm_bit = ACM_HW_CTRL_BE;
5460 rtl8xxxu_write32(priv, REG_EDCA_BE_PARAM, val32);
5461 break;
5462 case IEEE80211_AC_BK:
5463 acm_bit = ACM_HW_CTRL_BK;
5464 rtl8xxxu_write32(priv, REG_EDCA_BK_PARAM, val32);
5465 break;
5466 default:
5467 acm_bit = 0;
5468 break;
5469 }
5470
5471 if (param->acm)
5472 acm_ctrl |= acm_bit;
5473 else
5474 acm_ctrl &= ~acm_bit;
5475 rtl8xxxu_write8(priv, REG_ACM_HW_CTRL, acm_ctrl);
5476
5477 return 0;
5478}
5479
5480static void rtl8xxxu_configure_filter(struct ieee80211_hw *hw,
5481 unsigned int changed_flags,
5482 unsigned int *total_flags, u64 multicast)
5483{
5484 struct rtl8xxxu_priv *priv = hw->priv;
Bruno Randolf3bed4bf2016-02-03 13:39:51 -05005485 u32 rcr = rtl8xxxu_read32(priv, REG_RCR);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005486
5487 dev_dbg(&priv->udev->dev, "%s: changed_flags %08x, total_flags %08x\n",
5488 __func__, changed_flags, *total_flags);
5489
Bruno Randolf3bed4bf2016-02-03 13:39:51 -05005490 /*
5491 * FIF_ALLMULTI ignored as all multicast frames are accepted (REG_MAR)
5492 */
5493
5494 if (*total_flags & FIF_FCSFAIL)
5495 rcr |= RCR_ACCEPT_CRC32;
5496 else
5497 rcr &= ~RCR_ACCEPT_CRC32;
5498
5499 /*
5500 * FIF_PLCPFAIL not supported?
5501 */
5502
5503 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
5504 rcr &= ~RCR_CHECK_BSSID_BEACON;
5505 else
5506 rcr |= RCR_CHECK_BSSID_BEACON;
5507
5508 if (*total_flags & FIF_CONTROL)
5509 rcr |= RCR_ACCEPT_CTRL_FRAME;
5510 else
5511 rcr &= ~RCR_ACCEPT_CTRL_FRAME;
5512
5513 if (*total_flags & FIF_OTHER_BSS) {
5514 rcr |= RCR_ACCEPT_AP;
5515 rcr &= ~RCR_CHECK_BSSID_MATCH;
5516 } else {
5517 rcr &= ~RCR_ACCEPT_AP;
5518 rcr |= RCR_CHECK_BSSID_MATCH;
5519 }
5520
5521 if (*total_flags & FIF_PSPOLL)
5522 rcr |= RCR_ACCEPT_PM;
5523 else
5524 rcr &= ~RCR_ACCEPT_PM;
5525
5526 /*
5527 * FIF_PROBE_REQ ignored as probe requests always seem to be accepted
5528 */
5529
5530 rtl8xxxu_write32(priv, REG_RCR, rcr);
5531
Jes Sorensen755bda12016-02-03 13:39:54 -05005532 *total_flags &= (FIF_ALLMULTI | FIF_FCSFAIL | FIF_BCN_PRBRESP_PROMISC |
5533 FIF_CONTROL | FIF_OTHER_BSS | FIF_PSPOLL |
5534 FIF_PROBE_REQ);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005535}
5536
5537static int rtl8xxxu_set_rts_threshold(struct ieee80211_hw *hw, u32 rts)
5538{
5539 if (rts > 2347)
5540 return -EINVAL;
5541
5542 return 0;
5543}
5544
5545static int rtl8xxxu_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5546 struct ieee80211_vif *vif,
5547 struct ieee80211_sta *sta,
5548 struct ieee80211_key_conf *key)
5549{
5550 struct rtl8xxxu_priv *priv = hw->priv;
5551 struct device *dev = &priv->udev->dev;
5552 u8 mac_addr[ETH_ALEN];
5553 u8 val8;
5554 u16 val16;
5555 u32 val32;
5556 int retval = -EOPNOTSUPP;
5557
5558 dev_dbg(dev, "%s: cmd %02x, cipher %08x, index %i\n",
5559 __func__, cmd, key->cipher, key->keyidx);
5560
5561 if (vif->type != NL80211_IFTYPE_STATION)
5562 return -EOPNOTSUPP;
5563
5564 if (key->keyidx > 3)
5565 return -EOPNOTSUPP;
5566
5567 switch (key->cipher) {
5568 case WLAN_CIPHER_SUITE_WEP40:
5569 case WLAN_CIPHER_SUITE_WEP104:
5570
5571 break;
5572 case WLAN_CIPHER_SUITE_CCMP:
5573 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
5574 break;
5575 case WLAN_CIPHER_SUITE_TKIP:
5576 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
5577 default:
5578 return -EOPNOTSUPP;
5579 }
5580
5581 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
5582 dev_dbg(dev, "%s: pairwise key\n", __func__);
5583 ether_addr_copy(mac_addr, sta->addr);
5584 } else {
5585 dev_dbg(dev, "%s: group key\n", __func__);
5586 eth_broadcast_addr(mac_addr);
5587 }
5588
5589 val16 = rtl8xxxu_read16(priv, REG_CR);
5590 val16 |= CR_SECURITY_ENABLE;
5591 rtl8xxxu_write16(priv, REG_CR, val16);
5592
5593 val8 = SEC_CFG_TX_SEC_ENABLE | SEC_CFG_TXBC_USE_DEFKEY |
5594 SEC_CFG_RX_SEC_ENABLE | SEC_CFG_RXBC_USE_DEFKEY;
5595 val8 |= SEC_CFG_TX_USE_DEFKEY | SEC_CFG_RX_USE_DEFKEY;
5596 rtl8xxxu_write8(priv, REG_SECURITY_CFG, val8);
5597
5598 switch (cmd) {
5599 case SET_KEY:
5600 key->hw_key_idx = key->keyidx;
5601 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
5602 rtl8xxxu_cam_write(priv, key, mac_addr);
5603 retval = 0;
5604 break;
5605 case DISABLE_KEY:
5606 rtl8xxxu_write32(priv, REG_CAM_WRITE, 0x00000000);
5607 val32 = CAM_CMD_POLLING | CAM_CMD_WRITE |
5608 key->keyidx << CAM_CMD_KEY_SHIFT;
5609 rtl8xxxu_write32(priv, REG_CAM_CMD, val32);
5610 retval = 0;
5611 break;
5612 default:
5613 dev_warn(dev, "%s: Unsupported command %02x\n", __func__, cmd);
5614 }
5615
5616 return retval;
5617}
5618
5619static int
5620rtl8xxxu_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Sara Sharon50ea05e2015-12-30 16:06:04 +02005621 struct ieee80211_ampdu_params *params)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005622{
5623 struct rtl8xxxu_priv *priv = hw->priv;
5624 struct device *dev = &priv->udev->dev;
5625 u8 ampdu_factor, ampdu_density;
Sara Sharon50ea05e2015-12-30 16:06:04 +02005626 struct ieee80211_sta *sta = params->sta;
5627 enum ieee80211_ampdu_mlme_action action = params->action;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005628
5629 switch (action) {
5630 case IEEE80211_AMPDU_TX_START:
5631 dev_info(dev, "%s: IEEE80211_AMPDU_TX_START\n", __func__);
5632 ampdu_factor = sta->ht_cap.ampdu_factor;
5633 ampdu_density = sta->ht_cap.ampdu_density;
5634 rtl8xxxu_set_ampdu_factor(priv, ampdu_factor);
5635 rtl8xxxu_set_ampdu_min_space(priv, ampdu_density);
5636 dev_dbg(dev,
5637 "Changed HT: ampdu_factor %02x, ampdu_density %02x\n",
5638 ampdu_factor, ampdu_density);
5639 break;
5640 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5641 dev_info(dev, "%s: IEEE80211_AMPDU_TX_STOP_FLUSH\n", __func__);
5642 rtl8xxxu_set_ampdu_factor(priv, 0);
5643 rtl8xxxu_set_ampdu_min_space(priv, 0);
5644 break;
5645 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5646 dev_info(dev, "%s: IEEE80211_AMPDU_TX_STOP_FLUSH_CONT\n",
5647 __func__);
5648 rtl8xxxu_set_ampdu_factor(priv, 0);
5649 rtl8xxxu_set_ampdu_min_space(priv, 0);
5650 break;
5651 case IEEE80211_AMPDU_RX_START:
5652 dev_info(dev, "%s: IEEE80211_AMPDU_RX_START\n", __func__);
5653 break;
5654 case IEEE80211_AMPDU_RX_STOP:
5655 dev_info(dev, "%s: IEEE80211_AMPDU_RX_STOP\n", __func__);
5656 break;
5657 default:
5658 break;
5659 }
5660 return 0;
5661}
5662
5663static int rtl8xxxu_start(struct ieee80211_hw *hw)
5664{
5665 struct rtl8xxxu_priv *priv = hw->priv;
5666 struct rtl8xxxu_rx_urb *rx_urb;
5667 struct rtl8xxxu_tx_urb *tx_urb;
5668 unsigned long flags;
5669 int ret, i;
5670
5671 ret = 0;
5672
5673 init_usb_anchor(&priv->rx_anchor);
5674 init_usb_anchor(&priv->tx_anchor);
5675 init_usb_anchor(&priv->int_anchor);
5676
Jes Sorensendb08de92016-02-29 17:05:17 -05005677 priv->fops->enable_rf(priv);
Jes Sorensen0e28b972016-02-29 17:04:13 -05005678 if (priv->usb_interrupts) {
5679 ret = rtl8xxxu_submit_int_urb(hw);
5680 if (ret)
5681 goto exit;
5682 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005683
5684 for (i = 0; i < RTL8XXXU_TX_URBS; i++) {
5685 tx_urb = kmalloc(sizeof(struct rtl8xxxu_tx_urb), GFP_KERNEL);
5686 if (!tx_urb) {
5687 if (!i)
5688 ret = -ENOMEM;
5689
5690 goto error_out;
5691 }
5692 usb_init_urb(&tx_urb->urb);
5693 INIT_LIST_HEAD(&tx_urb->list);
5694 tx_urb->hw = hw;
5695 list_add(&tx_urb->list, &priv->tx_urb_free_list);
5696 priv->tx_urb_free_count++;
5697 }
5698
5699 priv->tx_stopped = false;
5700
5701 spin_lock_irqsave(&priv->rx_urb_lock, flags);
5702 priv->shutdown = false;
5703 spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
5704
5705 for (i = 0; i < RTL8XXXU_RX_URBS; i++) {
5706 rx_urb = kmalloc(sizeof(struct rtl8xxxu_rx_urb), GFP_KERNEL);
5707 if (!rx_urb) {
5708 if (!i)
5709 ret = -ENOMEM;
5710
5711 goto error_out;
5712 }
5713 usb_init_urb(&rx_urb->urb);
5714 INIT_LIST_HEAD(&rx_urb->list);
5715 rx_urb->hw = hw;
5716
5717 ret = rtl8xxxu_submit_rx_urb(priv, rx_urb);
5718 }
5719exit:
5720 /*
Bruno Randolfc85ea112016-02-03 13:39:55 -05005721 * Accept all data and mgmt frames
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005722 */
Bruno Randolfc85ea112016-02-03 13:39:55 -05005723 rtl8xxxu_write16(priv, REG_RXFLTMAP2, 0xffff);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005724 rtl8xxxu_write16(priv, REG_RXFLTMAP0, 0xffff);
5725
5726 rtl8xxxu_write32(priv, REG_OFDM0_XA_AGC_CORE1, 0x6954341e);
5727
5728 return ret;
5729
5730error_out:
5731 rtl8xxxu_free_tx_resources(priv);
5732 /*
5733 * Disable all data and mgmt frames
5734 */
5735 rtl8xxxu_write16(priv, REG_RXFLTMAP2, 0x0000);
5736 rtl8xxxu_write16(priv, REG_RXFLTMAP0, 0x0000);
5737
5738 return ret;
5739}
5740
5741static void rtl8xxxu_stop(struct ieee80211_hw *hw)
5742{
5743 struct rtl8xxxu_priv *priv = hw->priv;
5744 unsigned long flags;
5745
5746 rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
5747
5748 rtl8xxxu_write16(priv, REG_RXFLTMAP0, 0x0000);
5749 rtl8xxxu_write16(priv, REG_RXFLTMAP2, 0x0000);
5750
5751 spin_lock_irqsave(&priv->rx_urb_lock, flags);
5752 priv->shutdown = true;
5753 spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
5754
5755 usb_kill_anchored_urbs(&priv->rx_anchor);
5756 usb_kill_anchored_urbs(&priv->tx_anchor);
Jes Sorensen0e28b972016-02-29 17:04:13 -05005757 if (priv->usb_interrupts)
5758 usb_kill_anchored_urbs(&priv->int_anchor);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005759
Jes Sorensen265697e2016-04-14 16:37:20 -04005760 rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
5761
Jes Sorensenfc89a412016-02-29 17:05:46 -05005762 priv->fops->disable_rf(priv);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005763
5764 /*
5765 * Disable interrupts
5766 */
Jes Sorensen0e28b972016-02-29 17:04:13 -05005767 if (priv->usb_interrupts)
5768 rtl8xxxu_write32(priv, REG_USB_HIMR, 0);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005769
5770 rtl8xxxu_free_rx_resources(priv);
5771 rtl8xxxu_free_tx_resources(priv);
5772}
5773
5774static const struct ieee80211_ops rtl8xxxu_ops = {
5775 .tx = rtl8xxxu_tx,
5776 .add_interface = rtl8xxxu_add_interface,
5777 .remove_interface = rtl8xxxu_remove_interface,
5778 .config = rtl8xxxu_config,
5779 .conf_tx = rtl8xxxu_conf_tx,
5780 .bss_info_changed = rtl8xxxu_bss_info_changed,
5781 .configure_filter = rtl8xxxu_configure_filter,
5782 .set_rts_threshold = rtl8xxxu_set_rts_threshold,
5783 .start = rtl8xxxu_start,
5784 .stop = rtl8xxxu_stop,
5785 .sw_scan_start = rtl8xxxu_sw_scan_start,
5786 .sw_scan_complete = rtl8xxxu_sw_scan_complete,
5787 .set_key = rtl8xxxu_set_key,
5788 .ampdu_action = rtl8xxxu_ampdu_action,
5789};
5790
5791static int rtl8xxxu_parse_usb(struct rtl8xxxu_priv *priv,
5792 struct usb_interface *interface)
5793{
5794 struct usb_interface_descriptor *interface_desc;
5795 struct usb_host_interface *host_interface;
5796 struct usb_endpoint_descriptor *endpoint;
5797 struct device *dev = &priv->udev->dev;
5798 int i, j = 0, endpoints;
5799 u8 dir, xtype, num;
5800 int ret = 0;
5801
5802 host_interface = &interface->altsetting[0];
5803 interface_desc = &host_interface->desc;
5804 endpoints = interface_desc->bNumEndpoints;
5805
5806 for (i = 0; i < endpoints; i++) {
5807 endpoint = &host_interface->endpoint[i].desc;
5808
5809 dir = endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
5810 num = usb_endpoint_num(endpoint);
5811 xtype = usb_endpoint_type(endpoint);
5812 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
5813 dev_dbg(dev,
5814 "%s: endpoint: dir %02x, # %02x, type %02x\n",
5815 __func__, dir, num, xtype);
5816 if (usb_endpoint_dir_in(endpoint) &&
5817 usb_endpoint_xfer_bulk(endpoint)) {
5818 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
5819 dev_dbg(dev, "%s: in endpoint num %i\n",
5820 __func__, num);
5821
5822 if (priv->pipe_in) {
5823 dev_warn(dev,
5824 "%s: Too many IN pipes\n", __func__);
5825 ret = -EINVAL;
5826 goto exit;
5827 }
5828
5829 priv->pipe_in = usb_rcvbulkpipe(priv->udev, num);
5830 }
5831
5832 if (usb_endpoint_dir_in(endpoint) &&
5833 usb_endpoint_xfer_int(endpoint)) {
5834 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
5835 dev_dbg(dev, "%s: interrupt endpoint num %i\n",
5836 __func__, num);
5837
5838 if (priv->pipe_interrupt) {
5839 dev_warn(dev, "%s: Too many INTERRUPT pipes\n",
5840 __func__);
5841 ret = -EINVAL;
5842 goto exit;
5843 }
5844
5845 priv->pipe_interrupt = usb_rcvintpipe(priv->udev, num);
5846 }
5847
5848 if (usb_endpoint_dir_out(endpoint) &&
5849 usb_endpoint_xfer_bulk(endpoint)) {
5850 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
5851 dev_dbg(dev, "%s: out endpoint num %i\n",
5852 __func__, num);
5853 if (j >= RTL8XXXU_OUT_ENDPOINTS) {
5854 dev_warn(dev,
5855 "%s: Too many OUT pipes\n", __func__);
5856 ret = -EINVAL;
5857 goto exit;
5858 }
5859 priv->out_ep[j++] = num;
5860 }
5861 }
5862exit:
5863 priv->nr_out_eps = j;
5864 return ret;
5865}
5866
5867static int rtl8xxxu_probe(struct usb_interface *interface,
5868 const struct usb_device_id *id)
5869{
5870 struct rtl8xxxu_priv *priv;
5871 struct ieee80211_hw *hw;
5872 struct usb_device *udev;
5873 struct ieee80211_supported_band *sband;
5874 int ret = 0;
5875 int untested = 1;
5876
5877 udev = usb_get_dev(interface_to_usbdev(interface));
5878
5879 switch (id->idVendor) {
5880 case USB_VENDOR_ID_REALTEK:
5881 switch(id->idProduct) {
5882 case 0x1724:
5883 case 0x8176:
5884 case 0x8178:
5885 case 0x817f:
5886 untested = 0;
5887 break;
5888 }
5889 break;
5890 case 0x7392:
5891 if (id->idProduct == 0x7811)
5892 untested = 0;
5893 break;
Jes Sorensene1d70c92016-04-14 16:37:06 -04005894 case 0x050d:
5895 if (id->idProduct == 0x1004)
5896 untested = 0;
5897 break;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005898 default:
5899 break;
5900 }
5901
5902 if (untested) {
Jes Sorenseneaa4d142016-02-29 17:04:31 -05005903 rtl8xxxu_debug |= RTL8XXXU_DEBUG_EFUSE;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005904 dev_info(&udev->dev,
5905 "This Realtek USB WiFi dongle (0x%04x:0x%04x) is untested!\n",
5906 id->idVendor, id->idProduct);
5907 dev_info(&udev->dev,
5908 "Please report results to Jes.Sorensen@gmail.com\n");
5909 }
5910
5911 hw = ieee80211_alloc_hw(sizeof(struct rtl8xxxu_priv), &rtl8xxxu_ops);
5912 if (!hw) {
5913 ret = -ENOMEM;
5914 goto exit;
5915 }
5916
5917 priv = hw->priv;
5918 priv->hw = hw;
5919 priv->udev = udev;
5920 priv->fops = (struct rtl8xxxu_fileops *)id->driver_info;
5921 mutex_init(&priv->usb_buf_mutex);
5922 mutex_init(&priv->h2c_mutex);
5923 INIT_LIST_HEAD(&priv->tx_urb_free_list);
5924 spin_lock_init(&priv->tx_urb_lock);
5925 INIT_LIST_HEAD(&priv->rx_urb_pending_list);
5926 spin_lock_init(&priv->rx_urb_lock);
5927 INIT_WORK(&priv->rx_urb_wq, rtl8xxxu_rx_urb_work);
5928
5929 usb_set_intfdata(interface, hw);
5930
5931 ret = rtl8xxxu_parse_usb(priv, interface);
5932 if (ret)
5933 goto exit;
5934
5935 ret = rtl8xxxu_identify_chip(priv);
5936 if (ret) {
5937 dev_err(&udev->dev, "Fatal - failed to identify chip\n");
5938 goto exit;
5939 }
5940
5941 ret = rtl8xxxu_read_efuse(priv);
5942 if (ret) {
5943 dev_err(&udev->dev, "Fatal - failed to read EFuse\n");
5944 goto exit;
5945 }
5946
5947 ret = priv->fops->parse_efuse(priv);
5948 if (ret) {
5949 dev_err(&udev->dev, "Fatal - failed to parse EFuse\n");
5950 goto exit;
5951 }
5952
5953 rtl8xxxu_print_chipinfo(priv);
5954
5955 ret = priv->fops->load_firmware(priv);
5956 if (ret) {
5957 dev_err(&udev->dev, "Fatal - failed to load firmware\n");
5958 goto exit;
5959 }
5960
5961 ret = rtl8xxxu_init_device(hw);
5962
5963 hw->wiphy->max_scan_ssids = 1;
5964 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
5965 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
5966 hw->queues = 4;
5967
5968 sband = &rtl8xxxu_supported_band;
5969 sband->ht_cap.ht_supported = true;
5970 sband->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5971 sband->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
5972 sband->ht_cap.cap = IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40;
5973 memset(&sband->ht_cap.mcs, 0, sizeof(sband->ht_cap.mcs));
5974 sband->ht_cap.mcs.rx_mask[0] = 0xff;
5975 sband->ht_cap.mcs.rx_mask[4] = 0x01;
5976 if (priv->rf_paths > 1) {
5977 sband->ht_cap.mcs.rx_mask[1] = 0xff;
5978 sband->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5979 }
5980 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
5981 /*
5982 * Some APs will negotiate HT20_40 in a noisy environment leading
5983 * to miserable performance. Rather than defaulting to this, only
5984 * enable it if explicitly requested at module load time.
5985 */
5986 if (rtl8xxxu_ht40_2g) {
5987 dev_info(&udev->dev, "Enabling HT_20_40 on the 2.4GHz band\n");
5988 sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5989 }
Johannes Berg57fbcce2016-04-12 15:56:15 +02005990 hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005991
5992 hw->wiphy->rts_threshold = 2347;
5993
5994 SET_IEEE80211_DEV(priv->hw, &interface->dev);
5995 SET_IEEE80211_PERM_ADDR(hw, priv->mac_addr);
5996
Jes Sorensen179e1742016-02-29 17:05:27 -05005997 hw->extra_tx_headroom = priv->fops->tx_desc_size;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005998 ieee80211_hw_set(hw, SIGNAL_DBM);
5999 /*
6000 * The firmware handles rate control
6001 */
6002 ieee80211_hw_set(hw, HAS_RATE_CONTROL);
6003 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
6004
6005 ret = ieee80211_register_hw(priv->hw);
6006 if (ret) {
6007 dev_err(&udev->dev, "%s: Failed to register: %i\n",
6008 __func__, ret);
6009 goto exit;
6010 }
6011
6012exit:
6013 if (ret < 0)
6014 usb_put_dev(udev);
6015 return ret;
6016}
6017
6018static void rtl8xxxu_disconnect(struct usb_interface *interface)
6019{
6020 struct rtl8xxxu_priv *priv;
6021 struct ieee80211_hw *hw;
6022
6023 hw = usb_get_intfdata(interface);
6024 priv = hw->priv;
6025
Jes Sorensen8cae2f12016-04-14 16:37:13 -04006026 ieee80211_unregister_hw(hw);
6027
6028 priv->fops->power_off(priv);
6029
Jes Sorensen26f1fad2015-10-14 20:44:51 -04006030 usb_set_intfdata(interface, NULL);
6031
6032 dev_info(&priv->udev->dev, "disconnecting\n");
6033
Jes Sorensen26f1fad2015-10-14 20:44:51 -04006034 kfree(priv->fw_data);
6035 mutex_destroy(&priv->usb_buf_mutex);
6036 mutex_destroy(&priv->h2c_mutex);
6037
6038 usb_put_dev(priv->udev);
6039 ieee80211_free_hw(hw);
6040}
6041
Jes Sorensen26f1fad2015-10-14 20:44:51 -04006042static struct usb_device_id dev_table[] = {
6043{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8724, 0xff, 0xff, 0xff),
6044 .driver_info = (unsigned long)&rtl8723au_fops},
6045{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x1724, 0xff, 0xff, 0xff),
6046 .driver_info = (unsigned long)&rtl8723au_fops},
6047{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x0724, 0xff, 0xff, 0xff),
6048 .driver_info = (unsigned long)&rtl8723au_fops},
Jes Sorensen3307d842016-02-29 17:03:59 -05006049{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x818b, 0xff, 0xff, 0xff),
6050 .driver_info = (unsigned long)&rtl8192eu_fops},
Jes Sorensen35a741f2016-02-29 17:04:10 -05006051{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0xb720, 0xff, 0xff, 0xff),
6052 .driver_info = (unsigned long)&rtl8723bu_fops},
Kalle Valo033695b2015-10-23 20:27:58 +03006053#ifdef CONFIG_RTL8XXXU_UNTESTED
6054/* Still supported by rtlwifi */
Jes Sorensen26f1fad2015-10-14 20:44:51 -04006055{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8176, 0xff, 0xff, 0xff),
6056 .driver_info = (unsigned long)&rtl8192cu_fops},
6057{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8178, 0xff, 0xff, 0xff),
6058 .driver_info = (unsigned long)&rtl8192cu_fops},
6059{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817f, 0xff, 0xff, 0xff),
6060 .driver_info = (unsigned long)&rtl8192cu_fops},
6061/* Tested by Larry Finger */
6062{USB_DEVICE_AND_INTERFACE_INFO(0x7392, 0x7811, 0xff, 0xff, 0xff),
6063 .driver_info = (unsigned long)&rtl8192cu_fops},
Jes Sorensene1d70c92016-04-14 16:37:06 -04006064/* Tested by Andrea Merello */
6065{USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x1004, 0xff, 0xff, 0xff),
6066 .driver_info = (unsigned long)&rtl8192cu_fops},
Jes Sorensen26f1fad2015-10-14 20:44:51 -04006067/* Currently untested 8188 series devices */
6068{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8191, 0xff, 0xff, 0xff),
6069 .driver_info = (unsigned long)&rtl8192cu_fops},
6070{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8170, 0xff, 0xff, 0xff),
6071 .driver_info = (unsigned long)&rtl8192cu_fops},
6072{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8177, 0xff, 0xff, 0xff),
6073 .driver_info = (unsigned long)&rtl8192cu_fops},
6074{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817a, 0xff, 0xff, 0xff),
6075 .driver_info = (unsigned long)&rtl8192cu_fops},
6076{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817b, 0xff, 0xff, 0xff),
6077 .driver_info = (unsigned long)&rtl8192cu_fops},
6078{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817d, 0xff, 0xff, 0xff),
6079 .driver_info = (unsigned long)&rtl8192cu_fops},
6080{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817e, 0xff, 0xff, 0xff),
6081 .driver_info = (unsigned long)&rtl8192cu_fops},
6082{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x818a, 0xff, 0xff, 0xff),
6083 .driver_info = (unsigned long)&rtl8192cu_fops},
6084{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x317f, 0xff, 0xff, 0xff),
6085 .driver_info = (unsigned long)&rtl8192cu_fops},
6086{USB_DEVICE_AND_INTERFACE_INFO(0x1058, 0x0631, 0xff, 0xff, 0xff),
6087 .driver_info = (unsigned long)&rtl8192cu_fops},
6088{USB_DEVICE_AND_INTERFACE_INFO(0x04bb, 0x094c, 0xff, 0xff, 0xff),
6089 .driver_info = (unsigned long)&rtl8192cu_fops},
6090{USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x1102, 0xff, 0xff, 0xff),
6091 .driver_info = (unsigned long)&rtl8192cu_fops},
6092{USB_DEVICE_AND_INTERFACE_INFO(0x06f8, 0xe033, 0xff, 0xff, 0xff),
6093 .driver_info = (unsigned long)&rtl8192cu_fops},
6094{USB_DEVICE_AND_INTERFACE_INFO(0x07b8, 0x8189, 0xff, 0xff, 0xff),
6095 .driver_info = (unsigned long)&rtl8192cu_fops},
6096{USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0x9041, 0xff, 0xff, 0xff),
6097 .driver_info = (unsigned long)&rtl8192cu_fops},
6098{USB_DEVICE_AND_INTERFACE_INFO(0x0b05, 0x17ba, 0xff, 0xff, 0xff),
6099 .driver_info = (unsigned long)&rtl8192cu_fops},
6100{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x1e1e, 0xff, 0xff, 0xff),
6101 .driver_info = (unsigned long)&rtl8192cu_fops},
6102{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x5088, 0xff, 0xff, 0xff),
6103 .driver_info = (unsigned long)&rtl8192cu_fops},
6104{USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x0052, 0xff, 0xff, 0xff),
6105 .driver_info = (unsigned long)&rtl8192cu_fops},
6106{USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x005c, 0xff, 0xff, 0xff),
6107 .driver_info = (unsigned long)&rtl8192cu_fops},
6108{USB_DEVICE_AND_INTERFACE_INFO(0x0eb0, 0x9071, 0xff, 0xff, 0xff),
6109 .driver_info = (unsigned long)&rtl8192cu_fops},
6110{USB_DEVICE_AND_INTERFACE_INFO(0x103c, 0x1629, 0xff, 0xff, 0xff),
6111 .driver_info = (unsigned long)&rtl8192cu_fops},
6112{USB_DEVICE_AND_INTERFACE_INFO(0x13d3, 0x3357, 0xff, 0xff, 0xff),
6113 .driver_info = (unsigned long)&rtl8192cu_fops},
6114{USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3308, 0xff, 0xff, 0xff),
6115 .driver_info = (unsigned long)&rtl8192cu_fops},
6116{USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x330b, 0xff, 0xff, 0xff),
6117 .driver_info = (unsigned long)&rtl8192cu_fops},
6118{USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0x4902, 0xff, 0xff, 0xff),
6119 .driver_info = (unsigned long)&rtl8192cu_fops},
6120{USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xab2a, 0xff, 0xff, 0xff),
6121 .driver_info = (unsigned long)&rtl8192cu_fops},
6122{USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xab2e, 0xff, 0xff, 0xff),
6123 .driver_info = (unsigned long)&rtl8192cu_fops},
6124{USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xed17, 0xff, 0xff, 0xff),
6125 .driver_info = (unsigned long)&rtl8192cu_fops},
6126{USB_DEVICE_AND_INTERFACE_INFO(0x20f4, 0x648b, 0xff, 0xff, 0xff),
6127 .driver_info = (unsigned long)&rtl8192cu_fops},
6128{USB_DEVICE_AND_INTERFACE_INFO(0x4855, 0x0090, 0xff, 0xff, 0xff),
6129 .driver_info = (unsigned long)&rtl8192cu_fops},
6130{USB_DEVICE_AND_INTERFACE_INFO(0x4856, 0x0091, 0xff, 0xff, 0xff),
6131 .driver_info = (unsigned long)&rtl8192cu_fops},
6132{USB_DEVICE_AND_INTERFACE_INFO(0xcdab, 0x8010, 0xff, 0xff, 0xff),
6133 .driver_info = (unsigned long)&rtl8192cu_fops},
Jes Sorensen26f1fad2015-10-14 20:44:51 -04006134{USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaff7, 0xff, 0xff, 0xff),
6135 .driver_info = (unsigned long)&rtl8192cu_fops},
6136{USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaff9, 0xff, 0xff, 0xff),
6137 .driver_info = (unsigned long)&rtl8192cu_fops},
6138{USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaffa, 0xff, 0xff, 0xff),
6139 .driver_info = (unsigned long)&rtl8192cu_fops},
6140{USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaff8, 0xff, 0xff, 0xff),
6141 .driver_info = (unsigned long)&rtl8192cu_fops},
6142{USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaffb, 0xff, 0xff, 0xff),
6143 .driver_info = (unsigned long)&rtl8192cu_fops},
6144{USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaffc, 0xff, 0xff, 0xff),
6145 .driver_info = (unsigned long)&rtl8192cu_fops},
6146{USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0x1201, 0xff, 0xff, 0xff),
6147 .driver_info = (unsigned long)&rtl8192cu_fops},
6148/* Currently untested 8192 series devices */
6149{USB_DEVICE_AND_INTERFACE_INFO(0x04bb, 0x0950, 0xff, 0xff, 0xff),
6150 .driver_info = (unsigned long)&rtl8192cu_fops},
Jes Sorensen26f1fad2015-10-14 20:44:51 -04006151{USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x2102, 0xff, 0xff, 0xff),
6152 .driver_info = (unsigned long)&rtl8192cu_fops},
6153{USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x2103, 0xff, 0xff, 0xff),
6154 .driver_info = (unsigned long)&rtl8192cu_fops},
6155{USB_DEVICE_AND_INTERFACE_INFO(0x0586, 0x341f, 0xff, 0xff, 0xff),
6156 .driver_info = (unsigned long)&rtl8192cu_fops},
6157{USB_DEVICE_AND_INTERFACE_INFO(0x06f8, 0xe035, 0xff, 0xff, 0xff),
6158 .driver_info = (unsigned long)&rtl8192cu_fops},
6159{USB_DEVICE_AND_INTERFACE_INFO(0x0b05, 0x17ab, 0xff, 0xff, 0xff),
6160 .driver_info = (unsigned long)&rtl8192cu_fops},
6161{USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x0061, 0xff, 0xff, 0xff),
6162 .driver_info = (unsigned long)&rtl8192cu_fops},
6163{USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x0070, 0xff, 0xff, 0xff),
6164 .driver_info = (unsigned long)&rtl8192cu_fops},
6165{USB_DEVICE_AND_INTERFACE_INFO(0x0789, 0x016d, 0xff, 0xff, 0xff),
6166 .driver_info = (unsigned long)&rtl8192cu_fops},
6167{USB_DEVICE_AND_INTERFACE_INFO(0x07aa, 0x0056, 0xff, 0xff, 0xff),
6168 .driver_info = (unsigned long)&rtl8192cu_fops},
6169{USB_DEVICE_AND_INTERFACE_INFO(0x07b8, 0x8178, 0xff, 0xff, 0xff),
6170 .driver_info = (unsigned long)&rtl8192cu_fops},
6171{USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0x9021, 0xff, 0xff, 0xff),
6172 .driver_info = (unsigned long)&rtl8192cu_fops},
6173{USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0xf001, 0xff, 0xff, 0xff),
6174 .driver_info = (unsigned long)&rtl8192cu_fops},
6175{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x2e2e, 0xff, 0xff, 0xff),
6176 .driver_info = (unsigned long)&rtl8192cu_fops},
6177{USB_DEVICE_AND_INTERFACE_INFO(0x0e66, 0x0019, 0xff, 0xff, 0xff),
6178 .driver_info = (unsigned long)&rtl8192cu_fops},
6179{USB_DEVICE_AND_INTERFACE_INFO(0x0e66, 0x0020, 0xff, 0xff, 0xff),
6180 .driver_info = (unsigned long)&rtl8192cu_fops},
6181{USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3307, 0xff, 0xff, 0xff),
6182 .driver_info = (unsigned long)&rtl8192cu_fops},
6183{USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3309, 0xff, 0xff, 0xff),
6184 .driver_info = (unsigned long)&rtl8192cu_fops},
6185{USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x330a, 0xff, 0xff, 0xff),
6186 .driver_info = (unsigned long)&rtl8192cu_fops},
6187{USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xab2b, 0xff, 0xff, 0xff),
6188 .driver_info = (unsigned long)&rtl8192cu_fops},
6189{USB_DEVICE_AND_INTERFACE_INFO(0x20f4, 0x624d, 0xff, 0xff, 0xff),
6190 .driver_info = (unsigned long)&rtl8192cu_fops},
6191{USB_DEVICE_AND_INTERFACE_INFO(0x2357, 0x0100, 0xff, 0xff, 0xff),
6192 .driver_info = (unsigned long)&rtl8192cu_fops},
6193{USB_DEVICE_AND_INTERFACE_INFO(0x4855, 0x0091, 0xff, 0xff, 0xff),
6194 .driver_info = (unsigned long)&rtl8192cu_fops},
6195{USB_DEVICE_AND_INTERFACE_INFO(0x7392, 0x7822, 0xff, 0xff, 0xff),
6196 .driver_info = (unsigned long)&rtl8192cu_fops},
6197#endif
6198{ }
6199};
6200
6201static struct usb_driver rtl8xxxu_driver = {
6202 .name = DRIVER_NAME,
6203 .probe = rtl8xxxu_probe,
6204 .disconnect = rtl8xxxu_disconnect,
6205 .id_table = dev_table,
Jes Sorensen6a62f9d2016-04-14 16:37:18 -04006206 .no_dynamic_id = 1,
Jes Sorensen26f1fad2015-10-14 20:44:51 -04006207 .disable_hub_initiated_lpm = 1,
6208};
6209
6210static int __init rtl8xxxu_module_init(void)
6211{
6212 int res;
6213
6214 res = usb_register(&rtl8xxxu_driver);
6215 if (res < 0)
6216 pr_err(DRIVER_NAME ": usb_register() failed (%i)\n", res);
6217
6218 return res;
6219}
6220
6221static void __exit rtl8xxxu_module_exit(void)
6222{
6223 usb_deregister(&rtl8xxxu_driver);
6224}
6225
6226
6227MODULE_DEVICE_TABLE(usb, dev_table);
6228
6229module_init(rtl8xxxu_module_init);
6230module_exit(rtl8xxxu_module_exit);