xref: /openbmc/linux/drivers/platform/x86/dell/dell-wmi-sysman/int-attributes.c (revision 4ebdac060e5e24a89a7b3ec33ec46a41621e57fe)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Functions corresponding to integer type attributes under BIOS Integer GUID for use with
4  * dell-wmi-sysman
5  *
6  *  Copyright (c) 2020 Dell Inc.
7  */
8 
9 #include "dell-wmi-sysman.h"
10 
11 enum int_properties {MIN_VALUE = 6, MAX_VALUE, SCALAR_INCR};
12 
13 get_instance_id(integer);
14 
current_value_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)15 static ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
16 {
17 	int instance_id = get_integer_instance_id(kobj);
18 	union acpi_object *obj;
19 	ssize_t ret;
20 
21 	if (instance_id < 0)
22 		return instance_id;
23 
24 	/* need to use specific instance_id and guid combination to get right data */
25 	obj = get_wmiobj_pointer(instance_id, DELL_WMI_BIOS_INTEGER_ATTRIBUTE_GUID);
26 	if (!obj)
27 		return -EIO;
28 	if (obj->type != ACPI_TYPE_PACKAGE || obj->package.count < INT_MIN_ELEMENTS ||
29 	    obj->package.elements[CURRENT_VAL].type != ACPI_TYPE_INTEGER) {
30 		kfree(obj);
31 		return -EIO;
32 	}
33 	ret = snprintf(buf, PAGE_SIZE, "%lld\n", obj->package.elements[CURRENT_VAL].integer.value);
34 	kfree(obj);
35 	return ret;
36 }
37 
38 /**
39  * validate_integer_input() - Validate input of current_value against lower and upper bound
40  * @instance_id: The instance on which input is validated
41  * @buf: Input value
42  */
validate_integer_input(int instance_id,char * buf)43 static int validate_integer_input(int instance_id, char *buf)
44 {
45 	int in_val;
46 	int ret;
47 
48 	ret = kstrtoint(buf, 0, &in_val);
49 	if (ret)
50 		return ret;
51 	if (in_val < wmi_priv.integer_data[instance_id].min_value ||
52 			in_val > wmi_priv.integer_data[instance_id].max_value)
53 		return -EINVAL;
54 
55 	/* workaround for BIOS error.
56 	 * validate input to avoid setting 0 when integer input passed with + sign
57 	 */
58 	if (*buf == '+')
59 		memmove(buf, (buf + 1), strlen(buf + 1) + 1);
60 
61 	return ret;
62 }
63 
64 attribute_s_property_show(display_name_language_code, integer);
65 static struct kobj_attribute integer_displ_langcode =
66 	__ATTR_RO(display_name_language_code);
67 
68 attribute_s_property_show(display_name, integer);
69 static struct kobj_attribute integer_displ_name =
70 	__ATTR_RO(display_name);
71 
72 attribute_n_property_show(default_value, integer);
73 static struct kobj_attribute integer_default_val =
74 	__ATTR_RO(default_value);
75 
76 attribute_property_store(current_value, integer);
77 static struct kobj_attribute integer_current_val =
78 	__ATTR_RW_MODE(current_value, 0600);
79 
80 attribute_s_property_show(dell_modifier, integer);
81 static struct kobj_attribute integer_modifier =
82 	__ATTR_RO(dell_modifier);
83 
84 attribute_n_property_show(min_value, integer);
85 static struct kobj_attribute integer_lower_bound =
86 	__ATTR_RO(min_value);
87 
88 attribute_n_property_show(max_value, integer);
89 static struct kobj_attribute integer_upper_bound =
90 	__ATTR_RO(max_value);
91 
92 attribute_n_property_show(scalar_increment, integer);
93 static struct kobj_attribute integer_scalar_increment =
94 	__ATTR_RO(scalar_increment);
95 
type_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)96 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
97 			 char *buf)
98 {
99 	return sprintf(buf, "integer\n");
100 }
101 static struct kobj_attribute integer_type =
102 	__ATTR_RO(type);
103 
104 static struct attribute *integer_attrs[] = {
105 	&integer_displ_langcode.attr,
106 	&integer_displ_name.attr,
107 	&integer_default_val.attr,
108 	&integer_current_val.attr,
109 	&integer_modifier.attr,
110 	&integer_lower_bound.attr,
111 	&integer_upper_bound.attr,
112 	&integer_scalar_increment.attr,
113 	&integer_type.attr,
114 	NULL,
115 };
116 
117 static const struct attribute_group integer_attr_group = {
118 	.attrs = integer_attrs,
119 };
120 
alloc_int_data(void)121 int alloc_int_data(void)
122 {
123 	int ret = 0;
124 
125 	wmi_priv.integer_instances_count = get_instance_count(DELL_WMI_BIOS_INTEGER_ATTRIBUTE_GUID);
126 	wmi_priv.integer_data = kcalloc(wmi_priv.integer_instances_count,
127 					sizeof(struct integer_data), GFP_KERNEL);
128 	if (!wmi_priv.integer_data) {
129 		wmi_priv.integer_instances_count = 0;
130 		ret = -ENOMEM;
131 	}
132 	return ret;
133 }
134 
135 /**
136  * populate_int_data() - Populate all properties of an instance under integer attribute
137  * @integer_obj: ACPI object with integer data
138  * @instance_id: The instance to enumerate
139  * @attr_name_kobj: The parent kernel object
140  */
populate_int_data(union acpi_object * integer_obj,int instance_id,struct kobject * attr_name_kobj)141 int populate_int_data(union acpi_object *integer_obj, int instance_id,
142 			struct kobject *attr_name_kobj)
143 {
144 	wmi_priv.integer_data[instance_id].attr_name_kobj = attr_name_kobj;
145 	if (check_property_type(integer, ATTR_NAME, ACPI_TYPE_STRING))
146 		return -EINVAL;
147 	strlcpy_attr(wmi_priv.integer_data[instance_id].attribute_name,
148 		integer_obj[ATTR_NAME].string.pointer);
149 	if (check_property_type(integer, DISPL_NAME_LANG_CODE, ACPI_TYPE_STRING))
150 		return -EINVAL;
151 	strlcpy_attr(wmi_priv.integer_data[instance_id].display_name_language_code,
152 		integer_obj[DISPL_NAME_LANG_CODE].string.pointer);
153 	if (check_property_type(integer, DISPLAY_NAME, ACPI_TYPE_STRING))
154 		return -EINVAL;
155 	strlcpy_attr(wmi_priv.integer_data[instance_id].display_name,
156 		integer_obj[DISPLAY_NAME].string.pointer);
157 	if (check_property_type(integer, DEFAULT_VAL, ACPI_TYPE_INTEGER))
158 		return -EINVAL;
159 	wmi_priv.integer_data[instance_id].default_value =
160 		(uintptr_t)integer_obj[DEFAULT_VAL].string.pointer;
161 	if (check_property_type(integer, MODIFIER, ACPI_TYPE_STRING))
162 		return -EINVAL;
163 	strlcpy_attr(wmi_priv.integer_data[instance_id].dell_modifier,
164 		integer_obj[MODIFIER].string.pointer);
165 	if (check_property_type(integer, MIN_VALUE, ACPI_TYPE_INTEGER))
166 		return -EINVAL;
167 	wmi_priv.integer_data[instance_id].min_value =
168 		(uintptr_t)integer_obj[MIN_VALUE].string.pointer;
169 	if (check_property_type(integer, MAX_VALUE, ACPI_TYPE_INTEGER))
170 		return -EINVAL;
171 	wmi_priv.integer_data[instance_id].max_value =
172 		(uintptr_t)integer_obj[MAX_VALUE].string.pointer;
173 	if (check_property_type(integer, SCALAR_INCR, ACPI_TYPE_INTEGER))
174 		return -EINVAL;
175 	wmi_priv.integer_data[instance_id].scalar_increment =
176 		(uintptr_t)integer_obj[SCALAR_INCR].string.pointer;
177 
178 	return sysfs_create_group(attr_name_kobj, &integer_attr_group);
179 }
180 
181 /**
182  * exit_int_attributes() - Clear all attribute data
183  *
184  * Clears all data allocated for this group of attributes
185  */
exit_int_attributes(void)186 void exit_int_attributes(void)
187 {
188 	int instance_id;
189 
190 	for (instance_id = 0; instance_id < wmi_priv.integer_instances_count; instance_id++) {
191 		if (wmi_priv.integer_data[instance_id].attr_name_kobj)
192 			sysfs_remove_group(wmi_priv.integer_data[instance_id].attr_name_kobj,
193 								&integer_attr_group);
194 	}
195 	wmi_priv.integer_instances_count = 0;
196 
197 	kfree(wmi_priv.integer_data);
198 	wmi_priv.integer_data = NULL;
199 }
200