162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Functions corresponding to integer type attributes under BIOS Integer GUID for use with
462306a36Sopenharmony_ci * 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_cienum int_properties {MIN_VALUE = 6, MAX_VALUE, SCALAR_INCR};
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ciget_instance_id(integer);
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_cistatic ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1662306a36Sopenharmony_ci{
1762306a36Sopenharmony_ci	int instance_id = get_integer_instance_id(kobj);
1862306a36Sopenharmony_ci	union acpi_object *obj;
1962306a36Sopenharmony_ci	ssize_t ret;
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci	if (instance_id < 0)
2262306a36Sopenharmony_ci		return instance_id;
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci	/* need to use specific instance_id and guid combination to get right data */
2562306a36Sopenharmony_ci	obj = get_wmiobj_pointer(instance_id, DELL_WMI_BIOS_INTEGER_ATTRIBUTE_GUID);
2662306a36Sopenharmony_ci	if (!obj)
2762306a36Sopenharmony_ci		return -EIO;
2862306a36Sopenharmony_ci	if (obj->package.elements[CURRENT_VAL].type != ACPI_TYPE_INTEGER) {
2962306a36Sopenharmony_ci		kfree(obj);
3062306a36Sopenharmony_ci		return -EINVAL;
3162306a36Sopenharmony_ci	}
3262306a36Sopenharmony_ci	ret = snprintf(buf, PAGE_SIZE, "%lld\n", obj->package.elements[CURRENT_VAL].integer.value);
3362306a36Sopenharmony_ci	kfree(obj);
3462306a36Sopenharmony_ci	return ret;
3562306a36Sopenharmony_ci}
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci/**
3862306a36Sopenharmony_ci * validate_integer_input() - Validate input of current_value against lower and upper bound
3962306a36Sopenharmony_ci * @instance_id: The instance on which input is validated
4062306a36Sopenharmony_ci * @buf: Input value
4162306a36Sopenharmony_ci */
4262306a36Sopenharmony_cistatic int validate_integer_input(int instance_id, char *buf)
4362306a36Sopenharmony_ci{
4462306a36Sopenharmony_ci	int in_val;
4562306a36Sopenharmony_ci	int ret;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	ret = kstrtoint(buf, 0, &in_val);
4862306a36Sopenharmony_ci	if (ret)
4962306a36Sopenharmony_ci		return ret;
5062306a36Sopenharmony_ci	if (in_val < wmi_priv.integer_data[instance_id].min_value ||
5162306a36Sopenharmony_ci			in_val > wmi_priv.integer_data[instance_id].max_value)
5262306a36Sopenharmony_ci		return -EINVAL;
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	/* workaround for BIOS error.
5562306a36Sopenharmony_ci	 * validate input to avoid setting 0 when integer input passed with + sign
5662306a36Sopenharmony_ci	 */
5762306a36Sopenharmony_ci	if (*buf == '+')
5862306a36Sopenharmony_ci		memmove(buf, (buf + 1), strlen(buf + 1) + 1);
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	return ret;
6162306a36Sopenharmony_ci}
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ciattribute_s_property_show(display_name_language_code, integer);
6462306a36Sopenharmony_cistatic struct kobj_attribute integer_displ_langcode =
6562306a36Sopenharmony_ci	__ATTR_RO(display_name_language_code);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ciattribute_s_property_show(display_name, integer);
6862306a36Sopenharmony_cistatic struct kobj_attribute integer_displ_name =
6962306a36Sopenharmony_ci	__ATTR_RO(display_name);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ciattribute_n_property_show(default_value, integer);
7262306a36Sopenharmony_cistatic struct kobj_attribute integer_default_val =
7362306a36Sopenharmony_ci	__ATTR_RO(default_value);
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciattribute_property_store(current_value, integer);
7662306a36Sopenharmony_cistatic struct kobj_attribute integer_current_val =
7762306a36Sopenharmony_ci	__ATTR_RW_MODE(current_value, 0600);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciattribute_s_property_show(dell_modifier, integer);
8062306a36Sopenharmony_cistatic struct kobj_attribute integer_modifier =
8162306a36Sopenharmony_ci	__ATTR_RO(dell_modifier);
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ciattribute_n_property_show(min_value, integer);
8462306a36Sopenharmony_cistatic struct kobj_attribute integer_lower_bound =
8562306a36Sopenharmony_ci	__ATTR_RO(min_value);
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciattribute_n_property_show(max_value, integer);
8862306a36Sopenharmony_cistatic struct kobj_attribute integer_upper_bound =
8962306a36Sopenharmony_ci	__ATTR_RO(max_value);
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ciattribute_n_property_show(scalar_increment, integer);
9262306a36Sopenharmony_cistatic struct kobj_attribute integer_scalar_increment =
9362306a36Sopenharmony_ci	__ATTR_RO(scalar_increment);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_cistatic ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
9662306a36Sopenharmony_ci			 char *buf)
9762306a36Sopenharmony_ci{
9862306a36Sopenharmony_ci	return sprintf(buf, "integer\n");
9962306a36Sopenharmony_ci}
10062306a36Sopenharmony_cistatic struct kobj_attribute integer_type =
10162306a36Sopenharmony_ci	__ATTR_RO(type);
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_cistatic struct attribute *integer_attrs[] = {
10462306a36Sopenharmony_ci	&integer_displ_langcode.attr,
10562306a36Sopenharmony_ci	&integer_displ_name.attr,
10662306a36Sopenharmony_ci	&integer_default_val.attr,
10762306a36Sopenharmony_ci	&integer_current_val.attr,
10862306a36Sopenharmony_ci	&integer_modifier.attr,
10962306a36Sopenharmony_ci	&integer_lower_bound.attr,
11062306a36Sopenharmony_ci	&integer_upper_bound.attr,
11162306a36Sopenharmony_ci	&integer_scalar_increment.attr,
11262306a36Sopenharmony_ci	&integer_type.attr,
11362306a36Sopenharmony_ci	NULL,
11462306a36Sopenharmony_ci};
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cistatic const struct attribute_group integer_attr_group = {
11762306a36Sopenharmony_ci	.attrs = integer_attrs,
11862306a36Sopenharmony_ci};
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ciint alloc_int_data(void)
12162306a36Sopenharmony_ci{
12262306a36Sopenharmony_ci	int ret = 0;
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	wmi_priv.integer_instances_count = get_instance_count(DELL_WMI_BIOS_INTEGER_ATTRIBUTE_GUID);
12562306a36Sopenharmony_ci	wmi_priv.integer_data = kcalloc(wmi_priv.integer_instances_count,
12662306a36Sopenharmony_ci					sizeof(struct integer_data), GFP_KERNEL);
12762306a36Sopenharmony_ci	if (!wmi_priv.integer_data) {
12862306a36Sopenharmony_ci		wmi_priv.integer_instances_count = 0;
12962306a36Sopenharmony_ci		ret = -ENOMEM;
13062306a36Sopenharmony_ci	}
13162306a36Sopenharmony_ci	return ret;
13262306a36Sopenharmony_ci}
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci/**
13562306a36Sopenharmony_ci * populate_int_data() - Populate all properties of an instance under integer attribute
13662306a36Sopenharmony_ci * @integer_obj: ACPI object with integer data
13762306a36Sopenharmony_ci * @instance_id: The instance to enumerate
13862306a36Sopenharmony_ci * @attr_name_kobj: The parent kernel object
13962306a36Sopenharmony_ci */
14062306a36Sopenharmony_ciint populate_int_data(union acpi_object *integer_obj, int instance_id,
14162306a36Sopenharmony_ci			struct kobject *attr_name_kobj)
14262306a36Sopenharmony_ci{
14362306a36Sopenharmony_ci	wmi_priv.integer_data[instance_id].attr_name_kobj = attr_name_kobj;
14462306a36Sopenharmony_ci	if (check_property_type(integer, ATTR_NAME, ACPI_TYPE_STRING))
14562306a36Sopenharmony_ci		return -EINVAL;
14662306a36Sopenharmony_ci	strlcpy_attr(wmi_priv.integer_data[instance_id].attribute_name,
14762306a36Sopenharmony_ci		integer_obj[ATTR_NAME].string.pointer);
14862306a36Sopenharmony_ci	if (check_property_type(integer, DISPL_NAME_LANG_CODE, ACPI_TYPE_STRING))
14962306a36Sopenharmony_ci		return -EINVAL;
15062306a36Sopenharmony_ci	strlcpy_attr(wmi_priv.integer_data[instance_id].display_name_language_code,
15162306a36Sopenharmony_ci		integer_obj[DISPL_NAME_LANG_CODE].string.pointer);
15262306a36Sopenharmony_ci	if (check_property_type(integer, DISPLAY_NAME, ACPI_TYPE_STRING))
15362306a36Sopenharmony_ci		return -EINVAL;
15462306a36Sopenharmony_ci	strlcpy_attr(wmi_priv.integer_data[instance_id].display_name,
15562306a36Sopenharmony_ci		integer_obj[DISPLAY_NAME].string.pointer);
15662306a36Sopenharmony_ci	if (check_property_type(integer, DEFAULT_VAL, ACPI_TYPE_INTEGER))
15762306a36Sopenharmony_ci		return -EINVAL;
15862306a36Sopenharmony_ci	wmi_priv.integer_data[instance_id].default_value =
15962306a36Sopenharmony_ci		(uintptr_t)integer_obj[DEFAULT_VAL].string.pointer;
16062306a36Sopenharmony_ci	if (check_property_type(integer, MODIFIER, ACPI_TYPE_STRING))
16162306a36Sopenharmony_ci		return -EINVAL;
16262306a36Sopenharmony_ci	strlcpy_attr(wmi_priv.integer_data[instance_id].dell_modifier,
16362306a36Sopenharmony_ci		integer_obj[MODIFIER].string.pointer);
16462306a36Sopenharmony_ci	if (check_property_type(integer, MIN_VALUE, ACPI_TYPE_INTEGER))
16562306a36Sopenharmony_ci		return -EINVAL;
16662306a36Sopenharmony_ci	wmi_priv.integer_data[instance_id].min_value =
16762306a36Sopenharmony_ci		(uintptr_t)integer_obj[MIN_VALUE].string.pointer;
16862306a36Sopenharmony_ci	if (check_property_type(integer, MAX_VALUE, ACPI_TYPE_INTEGER))
16962306a36Sopenharmony_ci		return -EINVAL;
17062306a36Sopenharmony_ci	wmi_priv.integer_data[instance_id].max_value =
17162306a36Sopenharmony_ci		(uintptr_t)integer_obj[MAX_VALUE].string.pointer;
17262306a36Sopenharmony_ci	if (check_property_type(integer, SCALAR_INCR, ACPI_TYPE_INTEGER))
17362306a36Sopenharmony_ci		return -EINVAL;
17462306a36Sopenharmony_ci	wmi_priv.integer_data[instance_id].scalar_increment =
17562306a36Sopenharmony_ci		(uintptr_t)integer_obj[SCALAR_INCR].string.pointer;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	return sysfs_create_group(attr_name_kobj, &integer_attr_group);
17862306a36Sopenharmony_ci}
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci/**
18162306a36Sopenharmony_ci * exit_int_attributes() - Clear all attribute data
18262306a36Sopenharmony_ci *
18362306a36Sopenharmony_ci * Clears all data allocated for this group of attributes
18462306a36Sopenharmony_ci */
18562306a36Sopenharmony_civoid exit_int_attributes(void)
18662306a36Sopenharmony_ci{
18762306a36Sopenharmony_ci	int instance_id;
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	for (instance_id = 0; instance_id < wmi_priv.integer_instances_count; instance_id++) {
19062306a36Sopenharmony_ci		if (wmi_priv.integer_data[instance_id].attr_name_kobj)
19162306a36Sopenharmony_ci			sysfs_remove_group(wmi_priv.integer_data[instance_id].attr_name_kobj,
19262306a36Sopenharmony_ci								&integer_attr_group);
19362306a36Sopenharmony_ci	}
19462306a36Sopenharmony_ci	wmi_priv.integer_instances_count = 0;
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	kfree(wmi_priv.integer_data);
19762306a36Sopenharmony_ci	wmi_priv.integer_data = NULL;
19862306a36Sopenharmony_ci}
199