1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 
4 #include <linux/device.h>
5 #include <linux/pci.h>
6 
7 #include "ath5k.h"
8 #include "reg.h"
9 
10 #define SIMPLE_SHOW_STORE(name, get, set)				\
11 static ssize_t ath5k_attr_show_##name(struct device *dev,		\
12 			struct device_attribute *attr,			\
13 			char *buf)					\
14 {									\
15 	struct ieee80211_hw *hw = dev_get_drvdata(dev);			\
16 	struct ath5k_hw *ah = hw->priv;				\
17 	return sysfs_emit(buf, "%d\n", get);			\
18 }									\
19 									\
20 static ssize_t ath5k_attr_store_##name(struct device *dev,		\
21 			struct device_attribute *attr,			\
22 			const char *buf, size_t count)			\
23 {									\
24 	struct ieee80211_hw *hw = dev_get_drvdata(dev);			\
25 	struct ath5k_hw *ah = hw->priv;				\
26 	int val, ret;							\
27 									\
28 	ret = kstrtoint(buf, 10, &val);					\
29 	if (ret < 0)							\
30 		return ret;						\
31 	set(ah, val);						\
32 	return count;							\
33 }									\
34 static DEVICE_ATTR(name, 0644,						\
35 		   ath5k_attr_show_##name, ath5k_attr_store_##name)
36 
37 #define SIMPLE_SHOW(name, get)						\
38 static ssize_t ath5k_attr_show_##name(struct device *dev,		\
39 			struct device_attribute *attr,			\
40 			char *buf)					\
41 {									\
42 	struct ieee80211_hw *hw = dev_get_drvdata(dev);			\
43 	struct ath5k_hw *ah = hw->priv;				\
44 	return sysfs_emit(buf, "%d\n", get);			\
45 }									\
46 static DEVICE_ATTR(name, 0444, ath5k_attr_show_##name, NULL)
47 
48 /*** ANI ***/
49 
50 SIMPLE_SHOW_STORE(ani_mode, ah->ani_state.ani_mode, ath5k_ani_init);
51 SIMPLE_SHOW_STORE(noise_immunity_level, ah->ani_state.noise_imm_level,
52 			ath5k_ani_set_noise_immunity_level);
53 SIMPLE_SHOW_STORE(spur_level, ah->ani_state.spur_level,
54 			ath5k_ani_set_spur_immunity_level);
55 SIMPLE_SHOW_STORE(firstep_level, ah->ani_state.firstep_level,
56 			ath5k_ani_set_firstep_level);
57 SIMPLE_SHOW_STORE(ofdm_weak_signal_detection, ah->ani_state.ofdm_weak_sig,
58 			ath5k_ani_set_ofdm_weak_signal_detection);
59 SIMPLE_SHOW_STORE(cck_weak_signal_detection, ah->ani_state.cck_weak_sig,
60 			ath5k_ani_set_cck_weak_signal_detection);
61 SIMPLE_SHOW(spur_level_max, ah->ani_state.max_spur_level);
62 
ath5k_attr_show_noise_immunity_level_max(struct device * dev,struct device_attribute * attr,char * buf)63 static ssize_t ath5k_attr_show_noise_immunity_level_max(struct device *dev,
64 			struct device_attribute *attr,
65 			char *buf)
66 {
67 	return sysfs_emit(buf, "%d\n", ATH5K_ANI_MAX_NOISE_IMM_LVL);
68 }
69 static DEVICE_ATTR(noise_immunity_level_max, 0444,
70 		   ath5k_attr_show_noise_immunity_level_max, NULL);
71 
ath5k_attr_show_firstep_level_max(struct device * dev,struct device_attribute * attr,char * buf)72 static ssize_t ath5k_attr_show_firstep_level_max(struct device *dev,
73 			struct device_attribute *attr,
74 			char *buf)
75 {
76 	return sysfs_emit(buf, "%d\n", ATH5K_ANI_MAX_FIRSTEP_LVL);
77 }
78 static DEVICE_ATTR(firstep_level_max, 0444,
79 		   ath5k_attr_show_firstep_level_max, NULL);
80 
81 static struct attribute *ath5k_sysfs_entries_ani[] = {
82 	&dev_attr_ani_mode.attr,
83 	&dev_attr_noise_immunity_level.attr,
84 	&dev_attr_spur_level.attr,
85 	&dev_attr_firstep_level.attr,
86 	&dev_attr_ofdm_weak_signal_detection.attr,
87 	&dev_attr_cck_weak_signal_detection.attr,
88 	&dev_attr_noise_immunity_level_max.attr,
89 	&dev_attr_spur_level_max.attr,
90 	&dev_attr_firstep_level_max.attr,
91 	NULL
92 };
93 
94 static struct attribute_group ath5k_attribute_group_ani = {
95 	.name = "ani",
96 	.attrs = ath5k_sysfs_entries_ani,
97 };
98 
99 
100 /*** register / unregister ***/
101 
102 int
ath5k_sysfs_register(struct ath5k_hw * ah)103 ath5k_sysfs_register(struct ath5k_hw *ah)
104 {
105 	struct device *dev = ah->dev;
106 	int err;
107 
108 	err = sysfs_create_group(&dev->kobj, &ath5k_attribute_group_ani);
109 	if (err) {
110 		ATH5K_ERR(ah, "failed to create sysfs group\n");
111 		return err;
112 	}
113 
114 	return 0;
115 }
116 
117 void
ath5k_sysfs_unregister(struct ath5k_hw * ah)118 ath5k_sysfs_unregister(struct ath5k_hw *ah)
119 {
120 	struct device *dev = ah->dev;
121 
122 	sysfs_remove_group(&dev->kobj, &ath5k_attribute_group_ani);
123 }
124