18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright(c) 2009-2012  Realtek Corporation.*/
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include "wifi.h"
58c2ecf20Sopenharmony_ci#include "cam.h"
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
88c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifdef CONFIG_RTLWIFI_DEBUG
118c2ecf20Sopenharmony_civoid _rtl_dbg_print(struct rtl_priv *rtlpriv, u64 comp, int level,
128c2ecf20Sopenharmony_ci		    const char *fmt, ...)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	if (unlikely((comp & rtlpriv->cfg->mod_params->debug_mask) &&
158c2ecf20Sopenharmony_ci		     level <= rtlpriv->cfg->mod_params->debug_level)) {
168c2ecf20Sopenharmony_ci		struct va_format vaf;
178c2ecf20Sopenharmony_ci		va_list args;
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci		va_start(args, fmt);
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci		vaf.fmt = fmt;
228c2ecf20Sopenharmony_ci		vaf.va = &args;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci		pr_info("%pV", &vaf);
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci		va_end(args);
278c2ecf20Sopenharmony_ci	}
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(_rtl_dbg_print);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_civoid _rtl_dbg_print_data(struct rtl_priv *rtlpriv, u64 comp, int level,
328c2ecf20Sopenharmony_ci			 const char *titlestring,
338c2ecf20Sopenharmony_ci			 const void *hexdata, int hexdatalen)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	if (unlikely(((comp) & rtlpriv->cfg->mod_params->debug_mask) &&
368c2ecf20Sopenharmony_ci		     ((level) <= rtlpriv->cfg->mod_params->debug_level))) {
378c2ecf20Sopenharmony_ci		pr_info("In process \"%s\" (pid %i): %s\n",
388c2ecf20Sopenharmony_ci			current->comm, current->pid, titlestring);
398c2ecf20Sopenharmony_ci		print_hex_dump_bytes("", DUMP_PREFIX_NONE,
408c2ecf20Sopenharmony_ci				     hexdata, hexdatalen);
418c2ecf20Sopenharmony_ci	}
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(_rtl_dbg_print_data);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistruct rtl_debugfs_priv {
468c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv;
478c2ecf20Sopenharmony_ci	int (*cb_read)(struct seq_file *m, void *v);
488c2ecf20Sopenharmony_ci	ssize_t (*cb_write)(struct file *filp, const char __user *buffer,
498c2ecf20Sopenharmony_ci			    size_t count, loff_t *loff);
508c2ecf20Sopenharmony_ci	u32 cb_data;
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic struct dentry *debugfs_topdir;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic int rtl_debug_get_common(struct seq_file *m, void *v)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	struct rtl_debugfs_priv *debugfs_priv = m->private;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	return debugfs_priv->cb_read(m, v);
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic int dl_debug_open_common(struct inode *inode, struct file *file)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	return single_open(file, rtl_debug_get_common, inode->i_private);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic const struct file_operations file_ops_common = {
688c2ecf20Sopenharmony_ci	.open = dl_debug_open_common,
698c2ecf20Sopenharmony_ci	.read = seq_read,
708c2ecf20Sopenharmony_ci	.llseek = seq_lseek,
718c2ecf20Sopenharmony_ci	.release = single_release,
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic int rtl_debug_get_mac_page(struct seq_file *m, void *v)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	struct rtl_debugfs_priv *debugfs_priv = m->private;
778c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = debugfs_priv->rtlpriv;
788c2ecf20Sopenharmony_ci	u32 page = debugfs_priv->cb_data;
798c2ecf20Sopenharmony_ci	int i, n;
808c2ecf20Sopenharmony_ci	int max = 0xff;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	for (n = 0; n <= max; ) {
838c2ecf20Sopenharmony_ci		seq_printf(m, "\n%8.8x  ", n + page);
848c2ecf20Sopenharmony_ci		for (i = 0; i < 4 && n <= max; i++, n += 4)
858c2ecf20Sopenharmony_ci			seq_printf(m, "%8.8x    ",
868c2ecf20Sopenharmony_ci				   rtl_read_dword(rtlpriv, (page | n)));
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci	seq_puts(m, "\n");
898c2ecf20Sopenharmony_ci	return 0;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci#define RTL_DEBUG_IMPL_MAC_SERIES(page, addr)			\
938c2ecf20Sopenharmony_cistatic struct rtl_debugfs_priv rtl_debug_priv_mac_ ##page = {	\
948c2ecf20Sopenharmony_ci	.cb_read = rtl_debug_get_mac_page,			\
958c2ecf20Sopenharmony_ci	.cb_data = addr,					\
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(0, 0x0000);
998c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(1, 0x0100);
1008c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(2, 0x0200);
1018c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(3, 0x0300);
1028c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(4, 0x0400);
1038c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(5, 0x0500);
1048c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(6, 0x0600);
1058c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(7, 0x0700);
1068c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(10, 0x1000);
1078c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(11, 0x1100);
1088c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(12, 0x1200);
1098c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(13, 0x1300);
1108c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(14, 0x1400);
1118c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(15, 0x1500);
1128c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(16, 0x1600);
1138c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_MAC_SERIES(17, 0x1700);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic int rtl_debug_get_bb_page(struct seq_file *m, void *v)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	struct rtl_debugfs_priv *debugfs_priv = m->private;
1188c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = debugfs_priv->rtlpriv;
1198c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw = rtlpriv->hw;
1208c2ecf20Sopenharmony_ci	u32 page = debugfs_priv->cb_data;
1218c2ecf20Sopenharmony_ci	int i, n;
1228c2ecf20Sopenharmony_ci	int max = 0xff;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	for (n = 0; n <= max; ) {
1258c2ecf20Sopenharmony_ci		seq_printf(m, "\n%8.8x  ", n + page);
1268c2ecf20Sopenharmony_ci		for (i = 0; i < 4 && n <= max; i++, n += 4)
1278c2ecf20Sopenharmony_ci			seq_printf(m, "%8.8x    ",
1288c2ecf20Sopenharmony_ci				   rtl_get_bbreg(hw, (page | n), 0xffffffff));
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci	seq_puts(m, "\n");
1318c2ecf20Sopenharmony_ci	return 0;
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#define RTL_DEBUG_IMPL_BB_SERIES(page, addr)			\
1358c2ecf20Sopenharmony_cistatic struct rtl_debugfs_priv rtl_debug_priv_bb_ ##page = {	\
1368c2ecf20Sopenharmony_ci	.cb_read = rtl_debug_get_bb_page,			\
1378c2ecf20Sopenharmony_ci	.cb_data = addr,					\
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(8, 0x0800);
1418c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(9, 0x0900);
1428c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(a, 0x0a00);
1438c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(b, 0x0b00);
1448c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(c, 0x0c00);
1458c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(d, 0x0d00);
1468c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(e, 0x0e00);
1478c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(f, 0x0f00);
1488c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(18, 0x1800);
1498c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(19, 0x1900);
1508c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(1a, 0x1a00);
1518c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(1b, 0x1b00);
1528c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(1c, 0x1c00);
1538c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(1d, 0x1d00);
1548c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(1e, 0x1e00);
1558c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_BB_SERIES(1f, 0x1f00);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic int rtl_debug_get_reg_rf(struct seq_file *m, void *v)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	struct rtl_debugfs_priv *debugfs_priv = m->private;
1608c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = debugfs_priv->rtlpriv;
1618c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw = rtlpriv->hw;
1628c2ecf20Sopenharmony_ci	enum radio_path rfpath = debugfs_priv->cb_data;
1638c2ecf20Sopenharmony_ci	int i, n;
1648c2ecf20Sopenharmony_ci	int max = 0x40;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	if (IS_HARDWARE_TYPE_8822B(rtlpriv))
1678c2ecf20Sopenharmony_ci		max = 0xff;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	seq_printf(m, "\nPATH(%d)", rfpath);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	for (n = 0; n <= max; ) {
1728c2ecf20Sopenharmony_ci		seq_printf(m, "\n%8.8x  ", n);
1738c2ecf20Sopenharmony_ci		for (i = 0; i < 4 && n <= max; n += 1, i++)
1748c2ecf20Sopenharmony_ci			seq_printf(m, "%8.8x    ",
1758c2ecf20Sopenharmony_ci				   rtl_get_rfreg(hw, rfpath, n, 0xffffffff));
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci	seq_puts(m, "\n");
1788c2ecf20Sopenharmony_ci	return 0;
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci#define RTL_DEBUG_IMPL_RF_SERIES(page, addr)			\
1828c2ecf20Sopenharmony_cistatic struct rtl_debugfs_priv rtl_debug_priv_rf_ ##page = {	\
1838c2ecf20Sopenharmony_ci	.cb_read = rtl_debug_get_reg_rf,			\
1848c2ecf20Sopenharmony_ci	.cb_data = addr,					\
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_RF_SERIES(a, RF90_PATH_A);
1888c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_RF_SERIES(b, RF90_PATH_B);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic int rtl_debug_get_cam_register(struct seq_file *m, void *v)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	struct rtl_debugfs_priv *debugfs_priv = m->private;
1938c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = debugfs_priv->rtlpriv;
1948c2ecf20Sopenharmony_ci	int start = debugfs_priv->cb_data;
1958c2ecf20Sopenharmony_ci	u32 target_cmd = 0;
1968c2ecf20Sopenharmony_ci	u32 target_val = 0;
1978c2ecf20Sopenharmony_ci	u8 entry_i = 0;
1988c2ecf20Sopenharmony_ci	u32 ulstatus;
1998c2ecf20Sopenharmony_ci	int i = 100, j = 0;
2008c2ecf20Sopenharmony_ci	int end = (start + 11 > TOTAL_CAM_ENTRY ? TOTAL_CAM_ENTRY : start + 11);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	/* This dump the current register page */
2038c2ecf20Sopenharmony_ci	seq_printf(m,
2048c2ecf20Sopenharmony_ci		   "\n#################### SECURITY CAM (%d-%d) ##################\n",
2058c2ecf20Sopenharmony_ci		   start, end - 1);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	for (j = start; j < end; j++) {
2088c2ecf20Sopenharmony_ci		seq_printf(m, "\nD:  %2x > ", j);
2098c2ecf20Sopenharmony_ci		for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
2108c2ecf20Sopenharmony_ci			/* polling bit, and No Write enable, and address  */
2118c2ecf20Sopenharmony_ci			target_cmd = entry_i + CAM_CONTENT_COUNT * j;
2128c2ecf20Sopenharmony_ci			target_cmd = target_cmd | BIT(31);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci			/* Check polling bit is clear */
2158c2ecf20Sopenharmony_ci			while ((i--) >= 0) {
2168c2ecf20Sopenharmony_ci				ulstatus =
2178c2ecf20Sopenharmony_ci				    rtl_read_dword(rtlpriv,
2188c2ecf20Sopenharmony_ci						   rtlpriv->cfg->maps[RWCAM]);
2198c2ecf20Sopenharmony_ci				if (ulstatus & BIT(31))
2208c2ecf20Sopenharmony_ci					continue;
2218c2ecf20Sopenharmony_ci				else
2228c2ecf20Sopenharmony_ci					break;
2238c2ecf20Sopenharmony_ci			}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci			rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
2268c2ecf20Sopenharmony_ci					target_cmd);
2278c2ecf20Sopenharmony_ci			target_val = rtl_read_dword(rtlpriv,
2288c2ecf20Sopenharmony_ci						    rtlpriv->cfg->maps[RCAMO]);
2298c2ecf20Sopenharmony_ci			seq_printf(m, "%8.8x ", target_val);
2308c2ecf20Sopenharmony_ci		}
2318c2ecf20Sopenharmony_ci	}
2328c2ecf20Sopenharmony_ci	seq_puts(m, "\n");
2338c2ecf20Sopenharmony_ci	return 0;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci#define RTL_DEBUG_IMPL_CAM_SERIES(page, addr)			\
2378c2ecf20Sopenharmony_cistatic struct rtl_debugfs_priv rtl_debug_priv_cam_ ##page = {	\
2388c2ecf20Sopenharmony_ci	.cb_read = rtl_debug_get_cam_register,			\
2398c2ecf20Sopenharmony_ci	.cb_data = addr,					\
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_CAM_SERIES(1, 0);
2438c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_CAM_SERIES(2, 11);
2448c2ecf20Sopenharmony_ciRTL_DEBUG_IMPL_CAM_SERIES(3, 22);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic int rtl_debug_get_btcoex(struct seq_file *m, void *v)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	struct rtl_debugfs_priv *debugfs_priv = m->private;
2498c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = debugfs_priv->rtlpriv;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	if (rtlpriv->cfg->ops->get_btc_status())
2528c2ecf20Sopenharmony_ci		rtlpriv->btcoexist.btc_ops->btc_display_bt_coex_info(rtlpriv,
2538c2ecf20Sopenharmony_ci								     m);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	seq_puts(m, "\n");
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	return 0;
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cistatic struct rtl_debugfs_priv rtl_debug_priv_btcoex = {
2618c2ecf20Sopenharmony_ci	.cb_read = rtl_debug_get_btcoex,
2628c2ecf20Sopenharmony_ci	.cb_data = 0,
2638c2ecf20Sopenharmony_ci};
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic ssize_t rtl_debugfs_set_write_reg(struct file *filp,
2668c2ecf20Sopenharmony_ci					 const char __user *buffer,
2678c2ecf20Sopenharmony_ci					 size_t count, loff_t *loff)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	struct rtl_debugfs_priv *debugfs_priv = filp->private_data;
2708c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = debugfs_priv->rtlpriv;
2718c2ecf20Sopenharmony_ci	char tmp[32 + 1];
2728c2ecf20Sopenharmony_ci	int tmp_len;
2738c2ecf20Sopenharmony_ci	u32 addr, val, len;
2748c2ecf20Sopenharmony_ci	int num;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	if (count < 3)
2778c2ecf20Sopenharmony_ci		return -EFAULT;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	tmp_len = (count > sizeof(tmp) - 1 ? sizeof(tmp) - 1 : count);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	if (copy_from_user(tmp, buffer, tmp_len))
2828c2ecf20Sopenharmony_ci		return -EFAULT;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	tmp[tmp_len] = '\0';
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	/* write BB/MAC register */
2878c2ecf20Sopenharmony_ci	num = sscanf(tmp, "%x %x %x", &addr, &val, &len);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	if (num !=  3)
2908c2ecf20Sopenharmony_ci		return -EINVAL;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	switch (len) {
2938c2ecf20Sopenharmony_ci	case 1:
2948c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, addr, (u8)val);
2958c2ecf20Sopenharmony_ci		break;
2968c2ecf20Sopenharmony_ci	case 2:
2978c2ecf20Sopenharmony_ci		rtl_write_word(rtlpriv, addr, (u16)val);
2988c2ecf20Sopenharmony_ci		break;
2998c2ecf20Sopenharmony_ci	case 4:
3008c2ecf20Sopenharmony_ci		rtl_write_dword(rtlpriv, addr, val);
3018c2ecf20Sopenharmony_ci		break;
3028c2ecf20Sopenharmony_ci	default:
3038c2ecf20Sopenharmony_ci		/*printk("error write length=%d", len);*/
3048c2ecf20Sopenharmony_ci		break;
3058c2ecf20Sopenharmony_ci	}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	return count;
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic struct rtl_debugfs_priv rtl_debug_priv_write_reg = {
3118c2ecf20Sopenharmony_ci	.cb_write = rtl_debugfs_set_write_reg,
3128c2ecf20Sopenharmony_ci};
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic ssize_t rtl_debugfs_set_write_h2c(struct file *filp,
3158c2ecf20Sopenharmony_ci					 const char __user *buffer,
3168c2ecf20Sopenharmony_ci					 size_t count, loff_t *loff)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	struct rtl_debugfs_priv *debugfs_priv = filp->private_data;
3198c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = debugfs_priv->rtlpriv;
3208c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw = rtlpriv->hw;
3218c2ecf20Sopenharmony_ci	char tmp[32 + 1];
3228c2ecf20Sopenharmony_ci	int tmp_len;
3238c2ecf20Sopenharmony_ci	u8 h2c_len, h2c_data_packed[8];
3248c2ecf20Sopenharmony_ci	int h2c_data[8];	/* idx 0: cmd */
3258c2ecf20Sopenharmony_ci	int i;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	if (count < 3)
3288c2ecf20Sopenharmony_ci		return -EFAULT;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	tmp_len = (count > sizeof(tmp) - 1 ? sizeof(tmp) - 1 : count);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	if (copy_from_user(tmp, buffer, tmp_len))
3338c2ecf20Sopenharmony_ci		return -EFAULT;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	tmp[tmp_len] = '\0';
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	h2c_len = sscanf(tmp, "%X %X %X %X %X %X %X %X",
3388c2ecf20Sopenharmony_ci			 &h2c_data[0], &h2c_data[1],
3398c2ecf20Sopenharmony_ci			 &h2c_data[2], &h2c_data[3],
3408c2ecf20Sopenharmony_ci			 &h2c_data[4], &h2c_data[5],
3418c2ecf20Sopenharmony_ci			 &h2c_data[6], &h2c_data[7]);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	if (h2c_len == 0)
3448c2ecf20Sopenharmony_ci		return -EINVAL;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	for (i = 0; i < h2c_len; i++)
3478c2ecf20Sopenharmony_ci		h2c_data_packed[i] = (u8)h2c_data[i];
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	rtlpriv->cfg->ops->fill_h2c_cmd(hw, h2c_data_packed[0],
3508c2ecf20Sopenharmony_ci					h2c_len - 1,
3518c2ecf20Sopenharmony_ci					&h2c_data_packed[1]);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	return count;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic struct rtl_debugfs_priv rtl_debug_priv_write_h2c = {
3578c2ecf20Sopenharmony_ci	.cb_write = rtl_debugfs_set_write_h2c,
3588c2ecf20Sopenharmony_ci};
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cistatic ssize_t rtl_debugfs_set_write_rfreg(struct file *filp,
3618c2ecf20Sopenharmony_ci					   const char __user *buffer,
3628c2ecf20Sopenharmony_ci					    size_t count, loff_t *loff)
3638c2ecf20Sopenharmony_ci{
3648c2ecf20Sopenharmony_ci	struct rtl_debugfs_priv *debugfs_priv = filp->private_data;
3658c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = debugfs_priv->rtlpriv;
3668c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw = rtlpriv->hw;
3678c2ecf20Sopenharmony_ci	char tmp[32 + 1];
3688c2ecf20Sopenharmony_ci	int tmp_len;
3698c2ecf20Sopenharmony_ci	int num;
3708c2ecf20Sopenharmony_ci	int path;
3718c2ecf20Sopenharmony_ci	u32 addr, bitmask, data;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	if (count < 3)
3748c2ecf20Sopenharmony_ci		return -EFAULT;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	tmp_len = (count > sizeof(tmp) - 1 ? sizeof(tmp) - 1 : count);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	if (copy_from_user(tmp, buffer, tmp_len))
3798c2ecf20Sopenharmony_ci		return -EFAULT;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	tmp[tmp_len] = '\0';
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	num = sscanf(tmp, "%X %X %X %X",
3848c2ecf20Sopenharmony_ci		     &path, &addr, &bitmask, &data);
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	if (num != 4) {
3878c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_ERR, DBG_DMESG,
3888c2ecf20Sopenharmony_ci			"Format is <path> <addr> <mask> <data>\n");
3898c2ecf20Sopenharmony_ci		return -EINVAL;
3908c2ecf20Sopenharmony_ci	}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, path, addr, bitmask, data);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	return count;
3958c2ecf20Sopenharmony_ci}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_cistatic struct rtl_debugfs_priv rtl_debug_priv_write_rfreg = {
3988c2ecf20Sopenharmony_ci	.cb_write = rtl_debugfs_set_write_rfreg,
3998c2ecf20Sopenharmony_ci};
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic int rtl_debugfs_close(struct inode *inode, struct file *filp)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	return 0;
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cistatic ssize_t rtl_debugfs_common_write(struct file *filp,
4078c2ecf20Sopenharmony_ci					const char __user *buffer,
4088c2ecf20Sopenharmony_ci					size_t count, loff_t *loff)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	struct rtl_debugfs_priv *debugfs_priv = filp->private_data;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	return debugfs_priv->cb_write(filp, buffer, count, loff);
4138c2ecf20Sopenharmony_ci}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_cistatic const struct file_operations file_ops_common_write = {
4168c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
4178c2ecf20Sopenharmony_ci	.write = rtl_debugfs_common_write,
4188c2ecf20Sopenharmony_ci	.open = simple_open,
4198c2ecf20Sopenharmony_ci	.release = rtl_debugfs_close,
4208c2ecf20Sopenharmony_ci};
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci#define RTL_DEBUGFS_ADD_CORE(name, mode, fopname)			   \
4238c2ecf20Sopenharmony_ci	do {								   \
4248c2ecf20Sopenharmony_ci		rtl_debug_priv_ ##name.rtlpriv = rtlpriv;		   \
4258c2ecf20Sopenharmony_ci		debugfs_create_file(#name, mode, parent,		   \
4268c2ecf20Sopenharmony_ci				    &rtl_debug_priv_ ##name,		   \
4278c2ecf20Sopenharmony_ci				    &file_ops_ ##fopname);		   \
4288c2ecf20Sopenharmony_ci	} while (0)
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci#define RTL_DEBUGFS_ADD(name)						   \
4318c2ecf20Sopenharmony_ci		RTL_DEBUGFS_ADD_CORE(name, S_IFREG | 0444, common)
4328c2ecf20Sopenharmony_ci#define RTL_DEBUGFS_ADD_W(name)						   \
4338c2ecf20Sopenharmony_ci		RTL_DEBUGFS_ADD_CORE(name, S_IFREG | 0222, common_write)
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_civoid rtl_debug_add_one(struct ieee80211_hw *hw)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
4388c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
4398c2ecf20Sopenharmony_ci	struct dentry *parent;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	snprintf(rtlpriv->dbg.debugfs_name, 18, "%pMF", rtlefuse->dev_addr);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	rtlpriv->dbg.debugfs_dir =
4448c2ecf20Sopenharmony_ci		debugfs_create_dir(rtlpriv->dbg.debugfs_name, debugfs_topdir);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	parent = rtlpriv->dbg.debugfs_dir;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_0);
4498c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_1);
4508c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_2);
4518c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_3);
4528c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_4);
4538c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_5);
4548c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_6);
4558c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_7);
4568c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_8);
4578c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_9);
4588c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_a);
4598c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_b);
4608c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_c);
4618c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_d);
4628c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_e);
4638c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_f);
4648c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_10);
4658c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_11);
4668c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_12);
4678c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_13);
4688c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_14);
4698c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_15);
4708c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_16);
4718c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(mac_17);
4728c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_18);
4738c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_19);
4748c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_1a);
4758c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_1b);
4768c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_1c);
4778c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_1d);
4788c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_1e);
4798c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(bb_1f);
4808c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(rf_a);
4818c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(rf_b);
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(cam_1);
4848c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(cam_2);
4858c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(cam_3);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD(btcoex);
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD_W(write_reg);
4908c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD_W(write_h2c);
4918c2ecf20Sopenharmony_ci	RTL_DEBUGFS_ADD_W(write_rfreg);
4928c2ecf20Sopenharmony_ci}
4938c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rtl_debug_add_one);
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_civoid rtl_debug_remove_one(struct ieee80211_hw *hw)
4968c2ecf20Sopenharmony_ci{
4978c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	debugfs_remove_recursive(rtlpriv->dbg.debugfs_dir);
5008c2ecf20Sopenharmony_ci	rtlpriv->dbg.debugfs_dir = NULL;
5018c2ecf20Sopenharmony_ci}
5028c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rtl_debug_remove_one);
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_civoid rtl_debugfs_add_topdir(void)
5058c2ecf20Sopenharmony_ci{
5068c2ecf20Sopenharmony_ci	debugfs_topdir = debugfs_create_dir("rtlwifi", NULL);
5078c2ecf20Sopenharmony_ci}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_civoid rtl_debugfs_remove_topdir(void)
5108c2ecf20Sopenharmony_ci{
5118c2ecf20Sopenharmony_ci	debugfs_remove_recursive(debugfs_topdir);
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci#endif
515