162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Functions corresponding to enumeration type attributes under
462306a36Sopenharmony_ci * BIOS Enumeration GUID for use with dell-wmi-sysman
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci *  Copyright (c) 2020 Dell Inc.
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include "dell-wmi-sysman.h"
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciget_instance_id(enumeration);
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_cistatic ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1462306a36Sopenharmony_ci{
1562306a36Sopenharmony_ci	int instance_id = get_enumeration_instance_id(kobj);
1662306a36Sopenharmony_ci	union acpi_object *obj;
1762306a36Sopenharmony_ci	ssize_t ret;
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci	if (instance_id < 0)
2062306a36Sopenharmony_ci		return instance_id;
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci	/* need to use specific instance_id and guid combination to get right data */
2362306a36Sopenharmony_ci	obj = get_wmiobj_pointer(instance_id, DELL_WMI_BIOS_ENUMERATION_ATTRIBUTE_GUID);
2462306a36Sopenharmony_ci	if (!obj)
2562306a36Sopenharmony_ci		return -EIO;
2662306a36Sopenharmony_ci	if (obj->package.elements[CURRENT_VAL].type != ACPI_TYPE_STRING) {
2762306a36Sopenharmony_ci		kfree(obj);
2862306a36Sopenharmony_ci		return -EINVAL;
2962306a36Sopenharmony_ci	}
3062306a36Sopenharmony_ci	ret = snprintf(buf, PAGE_SIZE, "%s\n", obj->package.elements[CURRENT_VAL].string.pointer);
3162306a36Sopenharmony_ci	kfree(obj);
3262306a36Sopenharmony_ci	return ret;
3362306a36Sopenharmony_ci}
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/**
3662306a36Sopenharmony_ci * validate_enumeration_input() - Validate input of current_value against possible values
3762306a36Sopenharmony_ci * @instance_id: The instance on which input is validated
3862306a36Sopenharmony_ci * @buf: Input value
3962306a36Sopenharmony_ci */
4062306a36Sopenharmony_cistatic int validate_enumeration_input(int instance_id, const char *buf)
4162306a36Sopenharmony_ci{
4262306a36Sopenharmony_ci	char *options, *tmp, *p;
4362306a36Sopenharmony_ci	int ret = -EINVAL;
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	options = tmp = kstrdup(wmi_priv.enumeration_data[instance_id].possible_values,
4662306a36Sopenharmony_ci				 GFP_KERNEL);
4762306a36Sopenharmony_ci	if (!options)
4862306a36Sopenharmony_ci		return -ENOMEM;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	while ((p = strsep(&options, ";")) != NULL) {
5162306a36Sopenharmony_ci		if (!*p)
5262306a36Sopenharmony_ci			continue;
5362306a36Sopenharmony_ci		if (!strcasecmp(p, buf)) {
5462306a36Sopenharmony_ci			ret = 0;
5562306a36Sopenharmony_ci			break;
5662306a36Sopenharmony_ci		}
5762306a36Sopenharmony_ci	}
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	kfree(tmp);
6062306a36Sopenharmony_ci	return ret;
6162306a36Sopenharmony_ci}
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ciattribute_s_property_show(display_name_language_code, enumeration);
6462306a36Sopenharmony_cistatic struct kobj_attribute displ_langcode =
6562306a36Sopenharmony_ci		__ATTR_RO(display_name_language_code);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ciattribute_s_property_show(display_name, enumeration);
6862306a36Sopenharmony_cistatic struct kobj_attribute displ_name =
6962306a36Sopenharmony_ci		__ATTR_RO(display_name);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ciattribute_s_property_show(default_value, enumeration);
7262306a36Sopenharmony_cistatic struct kobj_attribute default_val =
7362306a36Sopenharmony_ci		__ATTR_RO(default_value);
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciattribute_property_store(current_value, enumeration);
7662306a36Sopenharmony_cistatic struct kobj_attribute current_val =
7762306a36Sopenharmony_ci		__ATTR_RW_MODE(current_value, 0600);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciattribute_s_property_show(dell_modifier, enumeration);
8062306a36Sopenharmony_cistatic struct kobj_attribute modifier =
8162306a36Sopenharmony_ci		__ATTR_RO(dell_modifier);
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ciattribute_s_property_show(dell_value_modifier, enumeration);
8462306a36Sopenharmony_cistatic struct kobj_attribute value_modfr =
8562306a36Sopenharmony_ci		__ATTR_RO(dell_value_modifier);
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciattribute_s_property_show(possible_values, enumeration);
8862306a36Sopenharmony_cistatic struct kobj_attribute poss_val =
8962306a36Sopenharmony_ci		__ATTR_RO(possible_values);
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_cistatic ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
9262306a36Sopenharmony_ci			 char *buf)
9362306a36Sopenharmony_ci{
9462306a36Sopenharmony_ci	return sprintf(buf, "enumeration\n");
9562306a36Sopenharmony_ci}
9662306a36Sopenharmony_cistatic struct kobj_attribute type =
9762306a36Sopenharmony_ci		__ATTR_RO(type);
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_cistatic struct attribute *enumeration_attrs[] = {
10062306a36Sopenharmony_ci	&displ_langcode.attr,
10162306a36Sopenharmony_ci	&displ_name.attr,
10262306a36Sopenharmony_ci	&default_val.attr,
10362306a36Sopenharmony_ci	&current_val.attr,
10462306a36Sopenharmony_ci	&modifier.attr,
10562306a36Sopenharmony_ci	&value_modfr.attr,
10662306a36Sopenharmony_ci	&poss_val.attr,
10762306a36Sopenharmony_ci	&type.attr,
10862306a36Sopenharmony_ci	NULL,
10962306a36Sopenharmony_ci};
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_cistatic const struct attribute_group enumeration_attr_group = {
11262306a36Sopenharmony_ci	.attrs = enumeration_attrs,
11362306a36Sopenharmony_ci};
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciint alloc_enum_data(void)
11662306a36Sopenharmony_ci{
11762306a36Sopenharmony_ci	int ret = 0;
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	wmi_priv.enumeration_instances_count =
12062306a36Sopenharmony_ci		get_instance_count(DELL_WMI_BIOS_ENUMERATION_ATTRIBUTE_GUID);
12162306a36Sopenharmony_ci	wmi_priv.enumeration_data = kcalloc(wmi_priv.enumeration_instances_count,
12262306a36Sopenharmony_ci					sizeof(struct enumeration_data), GFP_KERNEL);
12362306a36Sopenharmony_ci	if (!wmi_priv.enumeration_data) {
12462306a36Sopenharmony_ci		wmi_priv.enumeration_instances_count = 0;
12562306a36Sopenharmony_ci		ret = -ENOMEM;
12662306a36Sopenharmony_ci	}
12762306a36Sopenharmony_ci	return ret;
12862306a36Sopenharmony_ci}
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci/**
13162306a36Sopenharmony_ci * populate_enum_data() - Populate all properties of an instance under enumeration attribute
13262306a36Sopenharmony_ci * @enumeration_obj: ACPI object with enumeration data
13362306a36Sopenharmony_ci * @instance_id: The instance to enumerate
13462306a36Sopenharmony_ci * @attr_name_kobj: The parent kernel object
13562306a36Sopenharmony_ci * @enum_property_count: Total properties count under enumeration type
13662306a36Sopenharmony_ci */
13762306a36Sopenharmony_ciint populate_enum_data(union acpi_object *enumeration_obj, int instance_id,
13862306a36Sopenharmony_ci			struct kobject *attr_name_kobj, u32 enum_property_count)
13962306a36Sopenharmony_ci{
14062306a36Sopenharmony_ci	int i, next_obj, value_modifier_count, possible_values_count;
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	wmi_priv.enumeration_data[instance_id].attr_name_kobj = attr_name_kobj;
14362306a36Sopenharmony_ci	if (check_property_type(enumeration, ATTR_NAME, ACPI_TYPE_STRING))
14462306a36Sopenharmony_ci		return -EINVAL;
14562306a36Sopenharmony_ci	strlcpy_attr(wmi_priv.enumeration_data[instance_id].attribute_name,
14662306a36Sopenharmony_ci		enumeration_obj[ATTR_NAME].string.pointer);
14762306a36Sopenharmony_ci	if (check_property_type(enumeration, DISPL_NAME_LANG_CODE, ACPI_TYPE_STRING))
14862306a36Sopenharmony_ci		return -EINVAL;
14962306a36Sopenharmony_ci	strlcpy_attr(wmi_priv.enumeration_data[instance_id].display_name_language_code,
15062306a36Sopenharmony_ci		enumeration_obj[DISPL_NAME_LANG_CODE].string.pointer);
15162306a36Sopenharmony_ci	if (check_property_type(enumeration, DISPLAY_NAME, ACPI_TYPE_STRING))
15262306a36Sopenharmony_ci		return -EINVAL;
15362306a36Sopenharmony_ci	strlcpy_attr(wmi_priv.enumeration_data[instance_id].display_name,
15462306a36Sopenharmony_ci		enumeration_obj[DISPLAY_NAME].string.pointer);
15562306a36Sopenharmony_ci	if (check_property_type(enumeration, DEFAULT_VAL, ACPI_TYPE_STRING))
15662306a36Sopenharmony_ci		return -EINVAL;
15762306a36Sopenharmony_ci	strlcpy_attr(wmi_priv.enumeration_data[instance_id].default_value,
15862306a36Sopenharmony_ci		enumeration_obj[DEFAULT_VAL].string.pointer);
15962306a36Sopenharmony_ci	if (check_property_type(enumeration, MODIFIER, ACPI_TYPE_STRING))
16062306a36Sopenharmony_ci		return -EINVAL;
16162306a36Sopenharmony_ci	strlcpy_attr(wmi_priv.enumeration_data[instance_id].dell_modifier,
16262306a36Sopenharmony_ci		enumeration_obj[MODIFIER].string.pointer);
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	next_obj = MODIFIER + 1;
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	if (next_obj >= enum_property_count)
16762306a36Sopenharmony_ci		return -EINVAL;
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	if (check_property_type(enumeration, next_obj, ACPI_TYPE_INTEGER))
17062306a36Sopenharmony_ci		return -EINVAL;
17162306a36Sopenharmony_ci	value_modifier_count = (uintptr_t)enumeration_obj[next_obj++].string.pointer;
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	for (i = 0; i < value_modifier_count; i++) {
17462306a36Sopenharmony_ci		if (next_obj >= enum_property_count)
17562306a36Sopenharmony_ci			return -EINVAL;
17662306a36Sopenharmony_ci		if (check_property_type(enumeration, next_obj, ACPI_TYPE_STRING))
17762306a36Sopenharmony_ci			return -EINVAL;
17862306a36Sopenharmony_ci		strcat(wmi_priv.enumeration_data[instance_id].dell_value_modifier,
17962306a36Sopenharmony_ci			enumeration_obj[next_obj++].string.pointer);
18062306a36Sopenharmony_ci		strcat(wmi_priv.enumeration_data[instance_id].dell_value_modifier, ";");
18162306a36Sopenharmony_ci	}
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci	if (next_obj >= enum_property_count)
18462306a36Sopenharmony_ci		return -EINVAL;
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	if (check_property_type(enumeration, next_obj, ACPI_TYPE_INTEGER))
18762306a36Sopenharmony_ci		return -EINVAL;
18862306a36Sopenharmony_ci	possible_values_count = (uintptr_t) enumeration_obj[next_obj++].string.pointer;
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	for (i = 0; i < possible_values_count; i++) {
19162306a36Sopenharmony_ci		if (next_obj >= enum_property_count)
19262306a36Sopenharmony_ci			return -EINVAL;
19362306a36Sopenharmony_ci		if (check_property_type(enumeration, next_obj, ACPI_TYPE_STRING))
19462306a36Sopenharmony_ci			return -EINVAL;
19562306a36Sopenharmony_ci		strcat(wmi_priv.enumeration_data[instance_id].possible_values,
19662306a36Sopenharmony_ci			enumeration_obj[next_obj++].string.pointer);
19762306a36Sopenharmony_ci		strcat(wmi_priv.enumeration_data[instance_id].possible_values, ";");
19862306a36Sopenharmony_ci	}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	return sysfs_create_group(attr_name_kobj, &enumeration_attr_group);
20162306a36Sopenharmony_ci}
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci/**
20462306a36Sopenharmony_ci * exit_enum_attributes() - Clear all attribute data
20562306a36Sopenharmony_ci *
20662306a36Sopenharmony_ci * Clears all data allocated for this group of attributes
20762306a36Sopenharmony_ci */
20862306a36Sopenharmony_civoid exit_enum_attributes(void)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	int instance_id;
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci	for (instance_id = 0; instance_id < wmi_priv.enumeration_instances_count; instance_id++) {
21362306a36Sopenharmony_ci		if (wmi_priv.enumeration_data[instance_id].attr_name_kobj)
21462306a36Sopenharmony_ci			sysfs_remove_group(wmi_priv.enumeration_data[instance_id].attr_name_kobj,
21562306a36Sopenharmony_ci								&enumeration_attr_group);
21662306a36Sopenharmony_ci	}
21762306a36Sopenharmony_ci	wmi_priv.enumeration_instances_count = 0;
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	kfree(wmi_priv.enumeration_data);
22062306a36Sopenharmony_ci	wmi_priv.enumeration_data = NULL;
22162306a36Sopenharmony_ci}
222