xref: /linux/drivers/net/wireless/realtek/rtlwifi/rtl8192du/sw.c (revision 90602c251cda8a1e526efb250f28c1ea3f87cd78)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2024  Realtek Corporation.*/
3 
4 #include "../wifi.h"
5 #include "../core.h"
6 #include "../usb.h"
7 #include "../base.h"
8 #include "../rtl8192d/reg.h"
9 #include "../rtl8192d/def.h"
10 #include "../rtl8192d/fw_common.h"
11 #include "../rtl8192d/hw_common.h"
12 #include "../rtl8192d/phy_common.h"
13 #include "../rtl8192d/trx_common.h"
14 #include "phy.h"
15 #include "dm.h"
16 #include "hw.h"
17 #include "trx.h"
18 #include "led.h"
19 
20 #include <linux/module.h>
21 
rtl92du_get_other_intf(struct ieee80211_hw * hw)22 static struct usb_interface *rtl92du_get_other_intf(struct ieee80211_hw *hw)
23 {
24 	struct usb_interface *intf;
25 	struct usb_device *udev;
26 	u8 other_interfaceindex;
27 
28 	/* See SET_IEEE80211_DEV(hw, &intf->dev); in usb.c */
29 	intf = container_of_const(wiphy_dev(hw->wiphy), struct usb_interface, dev);
30 
31 	if (intf->altsetting[0].desc.bInterfaceNumber == 0)
32 		other_interfaceindex = 1;
33 	else
34 		other_interfaceindex = 0;
35 
36 	udev = interface_to_usbdev(intf);
37 
38 	return usb_ifnum_to_if(udev, other_interfaceindex);
39 }
40 
rtl92du_init_shared_data(struct ieee80211_hw * hw)41 static int rtl92du_init_shared_data(struct ieee80211_hw *hw)
42 {
43 	struct usb_interface *other_intf = rtl92du_get_other_intf(hw);
44 	struct rtl_priv *rtlpriv = rtl_priv(hw);
45 	struct rtl_priv *other_rtlpriv = NULL;
46 	struct ieee80211_hw *other_hw = NULL;
47 
48 	if (other_intf)
49 		other_hw = usb_get_intfdata(other_intf);
50 
51 	if (other_hw) {
52 		/* The other interface was already probed. */
53 		other_rtlpriv = rtl_priv(other_hw);
54 		rtlpriv->curveindex_2g = other_rtlpriv->curveindex_2g;
55 		rtlpriv->curveindex_5g = other_rtlpriv->curveindex_5g;
56 		rtlpriv->mutex_for_power_on_off = other_rtlpriv->mutex_for_power_on_off;
57 		rtlpriv->mutex_for_hw_init = other_rtlpriv->mutex_for_hw_init;
58 
59 		if (!rtlpriv->curveindex_2g || !rtlpriv->curveindex_5g ||
60 		    !rtlpriv->mutex_for_power_on_off || !rtlpriv->mutex_for_hw_init)
61 			return -ENOMEM;
62 
63 		return 0;
64 	}
65 
66 	/* The other interface doesn't exist or was not probed yet. */
67 	rtlpriv->curveindex_2g = kcalloc(TARGET_CHNL_NUM_2G,
68 					 sizeof(*rtlpriv->curveindex_2g),
69 					 GFP_KERNEL);
70 	rtlpriv->curveindex_5g = kcalloc(TARGET_CHNL_NUM_5G,
71 					 sizeof(*rtlpriv->curveindex_5g),
72 					 GFP_KERNEL);
73 	rtlpriv->mutex_for_power_on_off =
74 		kzalloc(sizeof(*rtlpriv->mutex_for_power_on_off), GFP_KERNEL);
75 	rtlpriv->mutex_for_hw_init =
76 		kzalloc(sizeof(*rtlpriv->mutex_for_hw_init), GFP_KERNEL);
77 
78 	if (!rtlpriv->curveindex_2g || !rtlpriv->curveindex_5g ||
79 	    !rtlpriv->mutex_for_power_on_off || !rtlpriv->mutex_for_hw_init) {
80 		kfree(rtlpriv->curveindex_2g);
81 		kfree(rtlpriv->curveindex_5g);
82 		kfree(rtlpriv->mutex_for_power_on_off);
83 		kfree(rtlpriv->mutex_for_hw_init);
84 		rtlpriv->curveindex_2g = NULL;
85 		rtlpriv->curveindex_5g = NULL;
86 		rtlpriv->mutex_for_power_on_off = NULL;
87 		rtlpriv->mutex_for_hw_init = NULL;
88 		return -ENOMEM;
89 	}
90 
91 	mutex_init(rtlpriv->mutex_for_power_on_off);
92 	mutex_init(rtlpriv->mutex_for_hw_init);
93 
94 	return 0;
95 }
96 
rtl92du_deinit_shared_data(struct ieee80211_hw * hw)97 static void rtl92du_deinit_shared_data(struct ieee80211_hw *hw)
98 {
99 	struct usb_interface *other_intf = rtl92du_get_other_intf(hw);
100 	struct rtl_priv *rtlpriv = rtl_priv(hw);
101 
102 	if (!other_intf || !usb_get_intfdata(other_intf)) {
103 		/* The other interface doesn't exist or was already disconnected. */
104 		kfree(rtlpriv->curveindex_2g);
105 		kfree(rtlpriv->curveindex_5g);
106 		if (rtlpriv->mutex_for_power_on_off)
107 			mutex_destroy(rtlpriv->mutex_for_power_on_off);
108 		if (rtlpriv->mutex_for_hw_init)
109 			mutex_destroy(rtlpriv->mutex_for_hw_init);
110 		kfree(rtlpriv->mutex_for_power_on_off);
111 		kfree(rtlpriv->mutex_for_hw_init);
112 	}
113 }
114 
rtl92du_init_sw_vars(struct ieee80211_hw * hw)115 static int rtl92du_init_sw_vars(struct ieee80211_hw *hw)
116 {
117 	const char *fw_name = "rtlwifi/rtl8192dufw.bin";
118 	struct rtl_priv *rtlpriv = rtl_priv(hw);
119 	int err;
120 
121 	err = rtl92du_init_shared_data(hw);
122 	if (err)
123 		return err;
124 
125 	rtlpriv->dm.dm_initialgain_enable = true;
126 	rtlpriv->dm.dm_flag = 0;
127 	rtlpriv->dm.disable_framebursting = false;
128 	rtlpriv->dm.thermalvalue = 0;
129 	rtlpriv->dm.useramask = true;
130 
131 	/* dual mac */
132 	if (rtlpriv->rtlhal.current_bandtype == BAND_ON_5G)
133 		rtlpriv->phy.current_channel = 36;
134 	else
135 		rtlpriv->phy.current_channel = 1;
136 
137 	if (rtlpriv->rtlhal.macphymode != SINGLEMAC_SINGLEPHY)
138 		rtlpriv->rtlhal.disable_amsdu_8k = true;
139 
140 	/* for LPS & IPS */
141 	rtlpriv->psc.inactiveps = rtlpriv->cfg->mod_params->inactiveps;
142 	rtlpriv->psc.swctrl_lps = rtlpriv->cfg->mod_params->swctrl_lps;
143 	rtlpriv->psc.fwctrl_lps = rtlpriv->cfg->mod_params->fwctrl_lps;
144 
145 	/* for early mode */
146 	rtlpriv->rtlhal.earlymode_enable = false;
147 
148 	/* for firmware buf */
149 	rtlpriv->rtlhal.pfirmware = kmalloc(0x8000, GFP_KERNEL);
150 	if (!rtlpriv->rtlhal.pfirmware)
151 		return -ENOMEM;
152 
153 	rtlpriv->max_fw_size = 0x8000;
154 	pr_info("Driver for Realtek RTL8192DU WLAN interface\n");
155 	pr_info("Loading firmware file %s\n", fw_name);
156 
157 	/* request fw */
158 	err = request_firmware_nowait(THIS_MODULE, 1, fw_name,
159 				      rtlpriv->io.dev, GFP_KERNEL, hw,
160 				      rtl_fw_cb);
161 	if (err) {
162 		pr_err("Failed to request firmware!\n");
163 		kfree(rtlpriv->rtlhal.pfirmware);
164 		rtlpriv->rtlhal.pfirmware = NULL;
165 		return err;
166 	}
167 
168 	return 0;
169 }
170 
rtl92du_deinit_sw_vars(struct ieee80211_hw * hw)171 static void rtl92du_deinit_sw_vars(struct ieee80211_hw *hw)
172 {
173 	struct rtl_priv *rtlpriv = rtl_priv(hw);
174 
175 	kfree(rtlpriv->rtlhal.pfirmware);
176 	rtlpriv->rtlhal.pfirmware = NULL;
177 
178 	rtl92du_deinit_shared_data(hw);
179 }
180 
181 static const struct rtl_hal_ops rtl8192du_hal_ops = {
182 	.init_sw_vars = rtl92du_init_sw_vars,
183 	.deinit_sw_vars = rtl92du_deinit_sw_vars,
184 	.read_chip_version = rtl92du_read_chip_version,
185 	.read_eeprom_info = rtl92d_read_eeprom_info,
186 	.hw_init = rtl92du_hw_init,
187 	.hw_disable = rtl92du_card_disable,
188 	.enable_interrupt = rtl92du_enable_interrupt,
189 	.disable_interrupt = rtl92du_disable_interrupt,
190 	.set_network_type = rtl92du_set_network_type,
191 	.set_chk_bssid = rtl92du_set_check_bssid,
192 	.set_qos = rtl92d_set_qos,
193 	.set_bcn_reg = rtl92du_set_beacon_related_registers,
194 	.set_bcn_intv = rtl92du_set_beacon_interval,
195 	.update_interrupt_mask = rtl92du_update_interrupt_mask,
196 	.get_hw_reg = rtl92du_get_hw_reg,
197 	.set_hw_reg = rtl92du_set_hw_reg,
198 	.update_rate_tbl = rtl92d_update_hal_rate_tbl,
199 	.fill_tx_desc = rtl92du_tx_fill_desc,
200 	.query_rx_desc = rtl92d_rx_query_desc,
201 	.set_channel_access = rtl92d_update_channel_access_setting,
202 	.radio_onoff_checking = rtl92d_gpio_radio_on_off_checking,
203 	.set_bw_mode = rtl92du_phy_set_bw_mode,
204 	.switch_channel = rtl92du_phy_sw_chnl,
205 	.dm_watchdog = rtl92du_dm_watchdog,
206 	.scan_operation_backup = rtl_phy_scan_operation_backup,
207 	.set_rf_power_state = rtl92du_phy_set_rf_power_state,
208 	.led_control = rtl92du_led_control,
209 	.set_desc = rtl92d_set_desc,
210 	.get_desc = rtl92d_get_desc,
211 	.enable_hw_sec = rtl92d_enable_hw_security_config,
212 	.set_key = rtl92d_set_key,
213 	.get_bbreg = rtl92du_phy_query_bb_reg,
214 	.set_bbreg = rtl92du_phy_set_bb_reg,
215 	.get_rfreg = rtl92d_phy_query_rf_reg,
216 	.set_rfreg = rtl92d_phy_set_rf_reg,
217 	.linked_set_reg = rtl92du_linked_set_reg,
218 	.fill_h2c_cmd = rtl92d_fill_h2c_cmd,
219 	.get_btc_status = rtl_btc_status_false,
220 	.phy_iq_calibrate = rtl92du_phy_iq_calibrate,
221 	.phy_lc_calibrate = rtl92du_phy_lc_calibrate,
222 };
223 
224 static struct rtl_mod_params rtl92du_mod_params = {
225 	.sw_crypto = false,
226 	.inactiveps = false,
227 	.swctrl_lps = false,
228 	.debug_level = 0,
229 	.debug_mask = 0,
230 };
231 
232 static const struct rtl_hal_usbint_cfg rtl92du_interface_cfg = {
233 	/* rx */
234 	.rx_urb_num = 8,
235 	.rx_max_size = 15360,
236 	.usb_rx_hdl = NULL,
237 	.usb_rx_segregate_hdl = NULL,
238 	/* tx */
239 	.usb_tx_cleanup = rtl92du_tx_cleanup,
240 	.usb_tx_post_hdl = rtl92du_tx_post_hdl,
241 	.usb_tx_aggregate_hdl = rtl92du_tx_aggregate_hdl,
242 	.usb_endpoint_mapping = rtl92du_endpoint_mapping,
243 	.usb_mq_to_hwq = rtl92du_mq_to_hwq,
244 };
245 
246 static const struct rtl_hal_cfg rtl92du_hal_cfg = {
247 	.name = "rtl8192du",
248 	.ops = &rtl8192du_hal_ops,
249 	.mod_params = &rtl92du_mod_params,
250 	.usb_interface_cfg = &rtl92du_interface_cfg,
251 
252 	.maps[SYS_ISO_CTRL] = REG_SYS_ISO_CTRL,
253 	.maps[SYS_FUNC_EN] = REG_SYS_FUNC_EN,
254 	.maps[SYS_CLK] = REG_SYS_CLKR,
255 	.maps[MAC_RCR_AM] = RCR_AM,
256 	.maps[MAC_RCR_AB] = RCR_AB,
257 	.maps[MAC_RCR_ACRC32] = RCR_ACRC32,
258 	.maps[MAC_RCR_ACF] = RCR_ACF,
259 	.maps[MAC_RCR_AAP] = RCR_AAP,
260 
261 	.maps[EFUSE_TEST] = REG_EFUSE_TEST,
262 	.maps[EFUSE_ACCESS] = REG_EFUSE_ACCESS,
263 	.maps[EFUSE_CTRL] = REG_EFUSE_CTRL,
264 	.maps[EFUSE_CLK] = 0,	/* just for 92se */
265 	.maps[EFUSE_CLK_CTRL] = REG_EFUSE_CTRL,
266 	.maps[EFUSE_PWC_EV12V] = PWC_EV12V,
267 	.maps[EFUSE_FEN_ELDR] = FEN_ELDR,
268 	.maps[EFUSE_LOADER_CLK_EN] = 0,
269 	.maps[EFUSE_ANA8M] = 0,	/* just for 92se */
270 	.maps[EFUSE_HWSET_MAX_SIZE] = HWSET_MAX_SIZE,
271 	.maps[EFUSE_MAX_SECTION_MAP] = EFUSE_MAX_SECTION,
272 	.maps[EFUSE_REAL_CONTENT_SIZE] = EFUSE_REAL_CONTENT_LEN,
273 
274 	.maps[RWCAM] = REG_CAMCMD,
275 	.maps[WCAMI] = REG_CAMWRITE,
276 	.maps[RCAMO] = REG_CAMREAD,
277 	.maps[CAMDBG] = REG_CAMDBG,
278 	.maps[SECR] = REG_SECCFG,
279 	.maps[SEC_CAM_NONE] = CAM_NONE,
280 	.maps[SEC_CAM_WEP40] = CAM_WEP40,
281 	.maps[SEC_CAM_TKIP] = CAM_TKIP,
282 	.maps[SEC_CAM_AES] = CAM_AES,
283 	.maps[SEC_CAM_WEP104] = CAM_WEP104,
284 
285 	.maps[RTL_IMR_BCNDMAINT6] = IMR_BCNDMAINT6,
286 	.maps[RTL_IMR_BCNDMAINT5] = IMR_BCNDMAINT5,
287 	.maps[RTL_IMR_BCNDMAINT4] = IMR_BCNDMAINT4,
288 	.maps[RTL_IMR_BCNDMAINT3] = IMR_BCNDMAINT3,
289 	.maps[RTL_IMR_BCNDMAINT2] = IMR_BCNDMAINT2,
290 	.maps[RTL_IMR_BCNDMAINT1] = IMR_BCNDMAINT1,
291 	.maps[RTL_IMR_BCNDOK8] = IMR_BCNDOK8,
292 	.maps[RTL_IMR_BCNDOK7] = IMR_BCNDOK7,
293 	.maps[RTL_IMR_BCNDOK6] = IMR_BCNDOK6,
294 	.maps[RTL_IMR_BCNDOK5] = IMR_BCNDOK5,
295 	.maps[RTL_IMR_BCNDOK4] = IMR_BCNDOK4,
296 	.maps[RTL_IMR_BCNDOK3] = IMR_BCNDOK3,
297 	.maps[RTL_IMR_BCNDOK2] = IMR_BCNDOK2,
298 	.maps[RTL_IMR_BCNDOK1] = IMR_BCNDOK1,
299 	.maps[RTL_IMR_TIMEOUT2] = IMR_TIMEOUT2,
300 	.maps[RTL_IMR_TIMEOUT1] = IMR_TIMEOUT1,
301 
302 	.maps[RTL_IMR_TXFOVW] = IMR_TXFOVW,
303 	.maps[RTL_IMR_PSTIMEOUT] = IMR_PSTIMEOUT,
304 	.maps[RTL_IMR_BCNINT] = IMR_BCNINT,
305 	.maps[RTL_IMR_RXFOVW] = IMR_RXFOVW,
306 	.maps[RTL_IMR_RDU] = IMR_RDU,
307 	.maps[RTL_IMR_ATIMEND] = IMR_ATIMEND,
308 	.maps[RTL_IMR_BDOK] = IMR_BDOK,
309 	.maps[RTL_IMR_MGNTDOK] = IMR_MGNTDOK,
310 	.maps[RTL_IMR_TBDER] = IMR_TBDER,
311 	.maps[RTL_IMR_HIGHDOK] = IMR_HIGHDOK,
312 	.maps[RTL_IMR_TBDOK] = IMR_TBDOK,
313 	.maps[RTL_IMR_BKDOK] = IMR_BKDOK,
314 	.maps[RTL_IMR_BEDOK] = IMR_BEDOK,
315 	.maps[RTL_IMR_VIDOK] = IMR_VIDOK,
316 	.maps[RTL_IMR_VODOK] = IMR_VODOK,
317 	.maps[RTL_IMR_ROK] = IMR_ROK,
318 	.maps[RTL_IBSS_INT_MASKS] = (IMR_BCNINT | IMR_TBDOK | IMR_TBDER),
319 
320 	.maps[RTL_RC_CCK_RATE1M] = DESC_RATE1M,
321 	.maps[RTL_RC_CCK_RATE2M] = DESC_RATE2M,
322 	.maps[RTL_RC_CCK_RATE5_5M] = DESC_RATE5_5M,
323 	.maps[RTL_RC_CCK_RATE11M] = DESC_RATE11M,
324 	.maps[RTL_RC_OFDM_RATE6M] = DESC_RATE6M,
325 	.maps[RTL_RC_OFDM_RATE9M] = DESC_RATE9M,
326 	.maps[RTL_RC_OFDM_RATE12M] = DESC_RATE12M,
327 	.maps[RTL_RC_OFDM_RATE18M] = DESC_RATE18M,
328 	.maps[RTL_RC_OFDM_RATE24M] = DESC_RATE24M,
329 	.maps[RTL_RC_OFDM_RATE36M] = DESC_RATE36M,
330 	.maps[RTL_RC_OFDM_RATE48M] = DESC_RATE48M,
331 	.maps[RTL_RC_OFDM_RATE54M] = DESC_RATE54M,
332 
333 	.maps[RTL_RC_HT_RATEMCS7] = DESC_RATEMCS7,
334 	.maps[RTL_RC_HT_RATEMCS15] = DESC_RATEMCS15,
335 };
336 
337 module_param_named(swenc, rtl92du_mod_params.sw_crypto, bool, 0444);
338 module_param_named(debug_level, rtl92du_mod_params.debug_level, int, 0644);
339 module_param_named(ips, rtl92du_mod_params.inactiveps, bool, 0444);
340 module_param_named(swlps, rtl92du_mod_params.swctrl_lps, bool, 0444);
341 module_param_named(debug_mask, rtl92du_mod_params.debug_mask, ullong, 0644);
342 MODULE_PARM_DESC(swenc, "Set to 1 for software crypto (default 0)\n");
343 MODULE_PARM_DESC(ips, "Set to 0 to not use link power save (default 0)\n");
344 MODULE_PARM_DESC(swlps, "Set to 1 to use SW control power save (default 0)\n");
345 MODULE_PARM_DESC(debug_level, "Set debug level (0-5) (default 0)");
346 MODULE_PARM_DESC(debug_mask, "Set debug mask (default 0)");
347 
348 #define USB_VENDOR_ID_REALTEK		0x0bda
349 
350 static const struct usb_device_id rtl8192d_usb_ids[] = {
351 	{RTL_USB_DEVICE(USB_VENDOR_ID_REALTEK, 0x8193, rtl92du_hal_cfg)},
352 	{RTL_USB_DEVICE(USB_VENDOR_ID_REALTEK, 0x8194, rtl92du_hal_cfg)},
353 	{RTL_USB_DEVICE(USB_VENDOR_ID_REALTEK, 0x8111, rtl92du_hal_cfg)},
354 	{RTL_USB_DEVICE(USB_VENDOR_ID_REALTEK, 0x0193, rtl92du_hal_cfg)},
355 	{RTL_USB_DEVICE(USB_VENDOR_ID_REALTEK, 0xe194, rtl92du_hal_cfg)},
356 	{RTL_USB_DEVICE(0x2019, 0xab2c, rtl92du_hal_cfg)},
357 	{RTL_USB_DEVICE(0x2019, 0xab2d, rtl92du_hal_cfg)},
358 	{RTL_USB_DEVICE(0x2019, 0x4903, rtl92du_hal_cfg)},
359 	{RTL_USB_DEVICE(0x2019, 0x4904, rtl92du_hal_cfg)},
360 	{RTL_USB_DEVICE(0x07b8, 0x8193, rtl92du_hal_cfg)},
361 	{RTL_USB_DEVICE(0x20f4, 0x664b, rtl92du_hal_cfg)},
362 	{RTL_USB_DEVICE(0x04dd, 0x954f, rtl92du_hal_cfg)},
363 	{RTL_USB_DEVICE(0x04dd, 0x96a6, rtl92du_hal_cfg)},
364 	{RTL_USB_DEVICE(0x050d, 0x110a, rtl92du_hal_cfg)},
365 	{RTL_USB_DEVICE(0x050d, 0x1105, rtl92du_hal_cfg)},
366 	{RTL_USB_DEVICE(0x050d, 0x120a, rtl92du_hal_cfg)},
367 	{RTL_USB_DEVICE(0x1668, 0x8102, rtl92du_hal_cfg)},
368 	{RTL_USB_DEVICE(0x0930, 0x0a0a, rtl92du_hal_cfg)},
369 	{RTL_USB_DEVICE(0x2001, 0x330c, rtl92du_hal_cfg)},
370 	{}
371 };
372 
373 MODULE_DEVICE_TABLE(usb, rtl8192d_usb_ids);
374 
rtl8192du_probe(struct usb_interface * intf,const struct usb_device_id * id)375 static int rtl8192du_probe(struct usb_interface *intf,
376 			   const struct usb_device_id *id)
377 {
378 	return rtl_usb_probe(intf, id, &rtl92du_hal_cfg);
379 }
380 
381 static struct usb_driver rtl8192du_driver = {
382 	.name = "rtl8192du",
383 	.probe = rtl8192du_probe,
384 	.disconnect = rtl_usb_disconnect,
385 	.id_table = rtl8192d_usb_ids,
386 	.disable_hub_initiated_lpm = 1,
387 };
388 
389 module_usb_driver(rtl8192du_driver);
390 
391 MODULE_AUTHOR("Bitterblue Smith	<rtl8821cerfe2@gmail.com>");
392 MODULE_LICENSE("GPL");
393 MODULE_DESCRIPTION("Realtek 8192DU 802.11n Dual Mac USB wireless");
394 MODULE_FIRMWARE("rtlwifi/rtl8192dufw.bin");
395