xref: /linux/drivers/platform/x86/dell/dell-wmi-sysman/enum-attributes.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1f1e1ea51SMario Limonciello // SPDX-License-Identifier: GPL-2.0
2f1e1ea51SMario Limonciello /*
3f1e1ea51SMario Limonciello  * Functions corresponding to enumeration type attributes under
4f1e1ea51SMario Limonciello  * BIOS Enumeration GUID for use with dell-wmi-sysman
5f1e1ea51SMario Limonciello  *
6f1e1ea51SMario Limonciello  *  Copyright (c) 2020 Dell Inc.
7f1e1ea51SMario Limonciello  */
8f1e1ea51SMario Limonciello 
9f1e1ea51SMario Limonciello #include "dell-wmi-sysman.h"
10f1e1ea51SMario Limonciello 
11f1e1ea51SMario Limonciello get_instance_id(enumeration);
12f1e1ea51SMario Limonciello 
current_value_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)13f1e1ea51SMario Limonciello static ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
14f1e1ea51SMario Limonciello {
15f1e1ea51SMario Limonciello 	int instance_id = get_enumeration_instance_id(kobj);
16f1e1ea51SMario Limonciello 	union acpi_object *obj;
17f1e1ea51SMario Limonciello 	ssize_t ret;
18f1e1ea51SMario Limonciello 
19f1e1ea51SMario Limonciello 	if (instance_id < 0)
20f1e1ea51SMario Limonciello 		return instance_id;
21f1e1ea51SMario Limonciello 
22f1e1ea51SMario Limonciello 	/* need to use specific instance_id and guid combination to get right data */
23f1e1ea51SMario Limonciello 	obj = get_wmiobj_pointer(instance_id, DELL_WMI_BIOS_ENUMERATION_ATTRIBUTE_GUID);
24f1e1ea51SMario Limonciello 	if (!obj)
25f1e1ea51SMario Limonciello 		return -EIO;
26f1e1ea51SMario Limonciello 	if (obj->package.elements[CURRENT_VAL].type != ACPI_TYPE_STRING) {
27f1e1ea51SMario Limonciello 		kfree(obj);
28f1e1ea51SMario Limonciello 		return -EINVAL;
29f1e1ea51SMario Limonciello 	}
30f1e1ea51SMario Limonciello 	ret = snprintf(buf, PAGE_SIZE, "%s\n", obj->package.elements[CURRENT_VAL].string.pointer);
31f1e1ea51SMario Limonciello 	kfree(obj);
32f1e1ea51SMario Limonciello 	return ret;
33f1e1ea51SMario Limonciello }
34f1e1ea51SMario Limonciello 
35f1e1ea51SMario Limonciello /**
36f1e1ea51SMario Limonciello  * validate_enumeration_input() - Validate input of current_value against possible values
37f1e1ea51SMario Limonciello  * @instance_id: The instance on which input is validated
38f1e1ea51SMario Limonciello  * @buf: Input value
39f1e1ea51SMario Limonciello  */
validate_enumeration_input(int instance_id,const char * buf)40f1e1ea51SMario Limonciello static int validate_enumeration_input(int instance_id, const char *buf)
41f1e1ea51SMario Limonciello {
42f1e1ea51SMario Limonciello 	char *options, *tmp, *p;
43f1e1ea51SMario Limonciello 	int ret = -EINVAL;
44f1e1ea51SMario Limonciello 
45f1e1ea51SMario Limonciello 	options = tmp = kstrdup(wmi_priv.enumeration_data[instance_id].possible_values,
46f1e1ea51SMario Limonciello 				 GFP_KERNEL);
47f1e1ea51SMario Limonciello 	if (!options)
48f1e1ea51SMario Limonciello 		return -ENOMEM;
49f1e1ea51SMario Limonciello 
50f1e1ea51SMario Limonciello 	while ((p = strsep(&options, ";")) != NULL) {
51f1e1ea51SMario Limonciello 		if (!*p)
52f1e1ea51SMario Limonciello 			continue;
53f1e1ea51SMario Limonciello 		if (!strcasecmp(p, buf)) {
54f1e1ea51SMario Limonciello 			ret = 0;
55f1e1ea51SMario Limonciello 			break;
56f1e1ea51SMario Limonciello 		}
57f1e1ea51SMario Limonciello 	}
58f1e1ea51SMario Limonciello 
59f1e1ea51SMario Limonciello 	kfree(tmp);
60f1e1ea51SMario Limonciello 	return ret;
61f1e1ea51SMario Limonciello }
62f1e1ea51SMario Limonciello 
63f1e1ea51SMario Limonciello attribute_s_property_show(display_name_language_code, enumeration);
64f1e1ea51SMario Limonciello static struct kobj_attribute displ_langcode =
65f1e1ea51SMario Limonciello 		__ATTR_RO(display_name_language_code);
66f1e1ea51SMario Limonciello 
67f1e1ea51SMario Limonciello attribute_s_property_show(display_name, enumeration);
68f1e1ea51SMario Limonciello static struct kobj_attribute displ_name =
69f1e1ea51SMario Limonciello 		__ATTR_RO(display_name);
70f1e1ea51SMario Limonciello 
71f1e1ea51SMario Limonciello attribute_s_property_show(default_value, enumeration);
72f1e1ea51SMario Limonciello static struct kobj_attribute default_val =
73f1e1ea51SMario Limonciello 		__ATTR_RO(default_value);
74f1e1ea51SMario Limonciello 
75f1e1ea51SMario Limonciello attribute_property_store(current_value, enumeration);
76f1e1ea51SMario Limonciello static struct kobj_attribute current_val =
77f1e1ea51SMario Limonciello 		__ATTR_RW_MODE(current_value, 0600);
78f1e1ea51SMario Limonciello 
79f1e1ea51SMario Limonciello attribute_s_property_show(dell_modifier, enumeration);
80f1e1ea51SMario Limonciello static struct kobj_attribute modifier =
81f1e1ea51SMario Limonciello 		__ATTR_RO(dell_modifier);
82f1e1ea51SMario Limonciello 
83f1e1ea51SMario Limonciello attribute_s_property_show(dell_value_modifier, enumeration);
84f1e1ea51SMario Limonciello static struct kobj_attribute value_modfr =
85f1e1ea51SMario Limonciello 		__ATTR_RO(dell_value_modifier);
86f1e1ea51SMario Limonciello 
87f1e1ea51SMario Limonciello attribute_s_property_show(possible_values, enumeration);
88f1e1ea51SMario Limonciello static struct kobj_attribute poss_val =
89f1e1ea51SMario Limonciello 		__ATTR_RO(possible_values);
90f1e1ea51SMario Limonciello 
type_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)91f1e1ea51SMario Limonciello static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
92f1e1ea51SMario Limonciello 			 char *buf)
93f1e1ea51SMario Limonciello {
94f1e1ea51SMario Limonciello 	return sprintf(buf, "enumeration\n");
95f1e1ea51SMario Limonciello }
96f1e1ea51SMario Limonciello static struct kobj_attribute type =
97f1e1ea51SMario Limonciello 		__ATTR_RO(type);
98f1e1ea51SMario Limonciello 
99f1e1ea51SMario Limonciello static struct attribute *enumeration_attrs[] = {
100f1e1ea51SMario Limonciello 	&displ_langcode.attr,
101f1e1ea51SMario Limonciello 	&displ_name.attr,
102f1e1ea51SMario Limonciello 	&default_val.attr,
103f1e1ea51SMario Limonciello 	&current_val.attr,
104f1e1ea51SMario Limonciello 	&modifier.attr,
105f1e1ea51SMario Limonciello 	&value_modfr.attr,
106f1e1ea51SMario Limonciello 	&poss_val.attr,
107f1e1ea51SMario Limonciello 	&type.attr,
108f1e1ea51SMario Limonciello 	NULL,
109f1e1ea51SMario Limonciello };
110f1e1ea51SMario Limonciello 
111f1e1ea51SMario Limonciello static const struct attribute_group enumeration_attr_group = {
112f1e1ea51SMario Limonciello 	.attrs = enumeration_attrs,
113f1e1ea51SMario Limonciello };
114f1e1ea51SMario Limonciello 
alloc_enum_data(void)115f1e1ea51SMario Limonciello int alloc_enum_data(void)
116f1e1ea51SMario Limonciello {
117f1e1ea51SMario Limonciello 	int ret = 0;
118f1e1ea51SMario Limonciello 
119f1e1ea51SMario Limonciello 	wmi_priv.enumeration_instances_count =
120f1e1ea51SMario Limonciello 		get_instance_count(DELL_WMI_BIOS_ENUMERATION_ATTRIBUTE_GUID);
121f1e1ea51SMario Limonciello 	wmi_priv.enumeration_data = kcalloc(wmi_priv.enumeration_instances_count,
122f1e1ea51SMario Limonciello 					sizeof(struct enumeration_data), GFP_KERNEL);
123f1e1ea51SMario Limonciello 	if (!wmi_priv.enumeration_data) {
124f1e1ea51SMario Limonciello 		wmi_priv.enumeration_instances_count = 0;
125f1e1ea51SMario Limonciello 		ret = -ENOMEM;
126f1e1ea51SMario Limonciello 	}
127f1e1ea51SMario Limonciello 	return ret;
128f1e1ea51SMario Limonciello }
129f1e1ea51SMario Limonciello 
130f1e1ea51SMario Limonciello /**
131f1e1ea51SMario Limonciello  * populate_enum_data() - Populate all properties of an instance under enumeration attribute
132f1e1ea51SMario Limonciello  * @enumeration_obj: ACPI object with enumeration data
133f1e1ea51SMario Limonciello  * @instance_id: The instance to enumerate
134f1e1ea51SMario Limonciello  * @attr_name_kobj: The parent kernel object
135*97be86e3SPrasanth KSR  * @enum_property_count: Total properties count under enumeration type
136f1e1ea51SMario Limonciello  */
populate_enum_data(union acpi_object * enumeration_obj,int instance_id,struct kobject * attr_name_kobj,u32 enum_property_count)137f1e1ea51SMario Limonciello int populate_enum_data(union acpi_object *enumeration_obj, int instance_id,
138*97be86e3SPrasanth KSR 			struct kobject *attr_name_kobj, u32 enum_property_count)
139f1e1ea51SMario Limonciello {
140f1e1ea51SMario Limonciello 	int i, next_obj, value_modifier_count, possible_values_count;
141f1e1ea51SMario Limonciello 
142f1e1ea51SMario Limonciello 	wmi_priv.enumeration_data[instance_id].attr_name_kobj = attr_name_kobj;
143*97be86e3SPrasanth KSR 	if (check_property_type(enumeration, ATTR_NAME, ACPI_TYPE_STRING))
144*97be86e3SPrasanth KSR 		return -EINVAL;
145f1e1ea51SMario Limonciello 	strlcpy_attr(wmi_priv.enumeration_data[instance_id].attribute_name,
146f1e1ea51SMario Limonciello 		enumeration_obj[ATTR_NAME].string.pointer);
147*97be86e3SPrasanth KSR 	if (check_property_type(enumeration, DISPL_NAME_LANG_CODE, ACPI_TYPE_STRING))
148*97be86e3SPrasanth KSR 		return -EINVAL;
149f1e1ea51SMario Limonciello 	strlcpy_attr(wmi_priv.enumeration_data[instance_id].display_name_language_code,
150f1e1ea51SMario Limonciello 		enumeration_obj[DISPL_NAME_LANG_CODE].string.pointer);
151*97be86e3SPrasanth KSR 	if (check_property_type(enumeration, DISPLAY_NAME, ACPI_TYPE_STRING))
152*97be86e3SPrasanth KSR 		return -EINVAL;
153f1e1ea51SMario Limonciello 	strlcpy_attr(wmi_priv.enumeration_data[instance_id].display_name,
154f1e1ea51SMario Limonciello 		enumeration_obj[DISPLAY_NAME].string.pointer);
155*97be86e3SPrasanth KSR 	if (check_property_type(enumeration, DEFAULT_VAL, ACPI_TYPE_STRING))
156*97be86e3SPrasanth KSR 		return -EINVAL;
157f1e1ea51SMario Limonciello 	strlcpy_attr(wmi_priv.enumeration_data[instance_id].default_value,
158f1e1ea51SMario Limonciello 		enumeration_obj[DEFAULT_VAL].string.pointer);
159*97be86e3SPrasanth KSR 	if (check_property_type(enumeration, MODIFIER, ACPI_TYPE_STRING))
160*97be86e3SPrasanth KSR 		return -EINVAL;
161f1e1ea51SMario Limonciello 	strlcpy_attr(wmi_priv.enumeration_data[instance_id].dell_modifier,
162f1e1ea51SMario Limonciello 		enumeration_obj[MODIFIER].string.pointer);
163f1e1ea51SMario Limonciello 
164f1e1ea51SMario Limonciello 	next_obj = MODIFIER + 1;
165f1e1ea51SMario Limonciello 
166*97be86e3SPrasanth KSR 	if (next_obj >= enum_property_count)
167*97be86e3SPrasanth KSR 		return -EINVAL;
168*97be86e3SPrasanth KSR 
169*97be86e3SPrasanth KSR 	if (check_property_type(enumeration, next_obj, ACPI_TYPE_INTEGER))
170*97be86e3SPrasanth KSR 		return -EINVAL;
171*97be86e3SPrasanth KSR 	value_modifier_count = (uintptr_t)enumeration_obj[next_obj++].string.pointer;
172f1e1ea51SMario Limonciello 
173f1e1ea51SMario Limonciello 	for (i = 0; i < value_modifier_count; i++) {
174*97be86e3SPrasanth KSR 		if (next_obj >= enum_property_count)
175*97be86e3SPrasanth KSR 			return -EINVAL;
176*97be86e3SPrasanth KSR 		if (check_property_type(enumeration, next_obj, ACPI_TYPE_STRING))
177*97be86e3SPrasanth KSR 			return -EINVAL;
178f1e1ea51SMario Limonciello 		strcat(wmi_priv.enumeration_data[instance_id].dell_value_modifier,
179*97be86e3SPrasanth KSR 			enumeration_obj[next_obj++].string.pointer);
180f1e1ea51SMario Limonciello 		strcat(wmi_priv.enumeration_data[instance_id].dell_value_modifier, ";");
181f1e1ea51SMario Limonciello 	}
182f1e1ea51SMario Limonciello 
183*97be86e3SPrasanth KSR 	if (next_obj >= enum_property_count)
184*97be86e3SPrasanth KSR 		return -EINVAL;
185*97be86e3SPrasanth KSR 
186*97be86e3SPrasanth KSR 	if (check_property_type(enumeration, next_obj, ACPI_TYPE_INTEGER))
187*97be86e3SPrasanth KSR 		return -EINVAL;
188*97be86e3SPrasanth KSR 	possible_values_count = (uintptr_t) enumeration_obj[next_obj++].string.pointer;
189f1e1ea51SMario Limonciello 
190f1e1ea51SMario Limonciello 	for (i = 0; i < possible_values_count; i++) {
191*97be86e3SPrasanth KSR 		if (next_obj >= enum_property_count)
192*97be86e3SPrasanth KSR 			return -EINVAL;
193*97be86e3SPrasanth KSR 		if (check_property_type(enumeration, next_obj, ACPI_TYPE_STRING))
194*97be86e3SPrasanth KSR 			return -EINVAL;
195f1e1ea51SMario Limonciello 		strcat(wmi_priv.enumeration_data[instance_id].possible_values,
196*97be86e3SPrasanth KSR 			enumeration_obj[next_obj++].string.pointer);
197f1e1ea51SMario Limonciello 		strcat(wmi_priv.enumeration_data[instance_id].possible_values, ";");
198f1e1ea51SMario Limonciello 	}
199f1e1ea51SMario Limonciello 
200f1e1ea51SMario Limonciello 	return sysfs_create_group(attr_name_kobj, &enumeration_attr_group);
201f1e1ea51SMario Limonciello }
202f1e1ea51SMario Limonciello 
203f1e1ea51SMario Limonciello /**
204f1e1ea51SMario Limonciello  * exit_enum_attributes() - Clear all attribute data
205f1e1ea51SMario Limonciello  *
206f1e1ea51SMario Limonciello  * Clears all data allocated for this group of attributes
207f1e1ea51SMario Limonciello  */
exit_enum_attributes(void)208f1e1ea51SMario Limonciello void exit_enum_attributes(void)
209f1e1ea51SMario Limonciello {
210f1e1ea51SMario Limonciello 	int instance_id;
211f1e1ea51SMario Limonciello 
212f1e1ea51SMario Limonciello 	for (instance_id = 0; instance_id < wmi_priv.enumeration_instances_count; instance_id++) {
213f1e1ea51SMario Limonciello 		if (wmi_priv.enumeration_data[instance_id].attr_name_kobj)
214f1e1ea51SMario Limonciello 			sysfs_remove_group(wmi_priv.enumeration_data[instance_id].attr_name_kobj,
215f1e1ea51SMario Limonciello 								&enumeration_attr_group);
216f1e1ea51SMario Limonciello 	}
2172d0c418cSHans de Goede 	wmi_priv.enumeration_instances_count = 0;
2182d0c418cSHans de Goede 
219f1e1ea51SMario Limonciello 	kfree(wmi_priv.enumeration_data);
2202d0c418cSHans de Goede 	wmi_priv.enumeration_data = NULL;
221f1e1ea51SMario Limonciello }
222