1f1e1ea51SMario Limonciello // SPDX-License-Identifier: GPL-2.0
2f1e1ea51SMario Limonciello /*
3f1e1ea51SMario Limonciello * Functions corresponding to string type attributes under BIOS String GUID for use with
4f1e1ea51SMario Limonciello * 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 enum string_properties {MIN_LEN = 6, MAX_LEN};
12f1e1ea51SMario Limonciello
13f1e1ea51SMario Limonciello get_instance_id(str);
14f1e1ea51SMario Limonciello
current_value_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)15f1e1ea51SMario Limonciello static ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
16f1e1ea51SMario Limonciello {
17f1e1ea51SMario Limonciello int instance_id = get_str_instance_id(kobj);
18f1e1ea51SMario Limonciello union acpi_object *obj;
19f1e1ea51SMario Limonciello ssize_t ret;
20f1e1ea51SMario Limonciello
21f1e1ea51SMario Limonciello if (instance_id < 0)
22f1e1ea51SMario Limonciello return -EIO;
23f1e1ea51SMario Limonciello
24f1e1ea51SMario Limonciello /* need to use specific instance_id and guid combination to get right data */
25f1e1ea51SMario Limonciello obj = get_wmiobj_pointer(instance_id, DELL_WMI_BIOS_STRING_ATTRIBUTE_GUID);
26f1e1ea51SMario Limonciello if (!obj)
27f1e1ea51SMario Limonciello return -EIO;
28f1e1ea51SMario Limonciello if (obj->package.elements[CURRENT_VAL].type != ACPI_TYPE_STRING) {
29f1e1ea51SMario Limonciello kfree(obj);
30f1e1ea51SMario Limonciello return -EINVAL;
31f1e1ea51SMario Limonciello }
32f1e1ea51SMario Limonciello ret = snprintf(buf, PAGE_SIZE, "%s\n", obj->package.elements[CURRENT_VAL].string.pointer);
33f1e1ea51SMario Limonciello kfree(obj);
34f1e1ea51SMario Limonciello return ret;
35f1e1ea51SMario Limonciello }
36f1e1ea51SMario Limonciello
37f1e1ea51SMario Limonciello /**
38f1e1ea51SMario Limonciello * validate_str_input() - Validate input of current_value against min and max lengths
39f1e1ea51SMario Limonciello * @instance_id: The instance on which input is validated
40f1e1ea51SMario Limonciello * @buf: Input value
41f1e1ea51SMario Limonciello */
validate_str_input(int instance_id,const char * buf)42f1e1ea51SMario Limonciello static int validate_str_input(int instance_id, const char *buf)
43f1e1ea51SMario Limonciello {
44f1e1ea51SMario Limonciello int in_len = strlen(buf);
45f1e1ea51SMario Limonciello
46f1e1ea51SMario Limonciello if ((in_len < wmi_priv.str_data[instance_id].min_length) ||
47f1e1ea51SMario Limonciello (in_len > wmi_priv.str_data[instance_id].max_length))
48f1e1ea51SMario Limonciello return -EINVAL;
49f1e1ea51SMario Limonciello
50f1e1ea51SMario Limonciello return 0;
51f1e1ea51SMario Limonciello }
52f1e1ea51SMario Limonciello
53f1e1ea51SMario Limonciello attribute_s_property_show(display_name_language_code, str);
54f1e1ea51SMario Limonciello static struct kobj_attribute str_displ_langcode =
55f1e1ea51SMario Limonciello __ATTR_RO(display_name_language_code);
56f1e1ea51SMario Limonciello
57f1e1ea51SMario Limonciello attribute_s_property_show(display_name, str);
58f1e1ea51SMario Limonciello static struct kobj_attribute str_displ_name =
59f1e1ea51SMario Limonciello __ATTR_RO(display_name);
60f1e1ea51SMario Limonciello
61f1e1ea51SMario Limonciello attribute_s_property_show(default_value, str);
62f1e1ea51SMario Limonciello static struct kobj_attribute str_default_val =
63f1e1ea51SMario Limonciello __ATTR_RO(default_value);
64f1e1ea51SMario Limonciello
65f1e1ea51SMario Limonciello attribute_property_store(current_value, str);
66f1e1ea51SMario Limonciello static struct kobj_attribute str_current_val =
67f1e1ea51SMario Limonciello __ATTR_RW_MODE(current_value, 0600);
68f1e1ea51SMario Limonciello
69f1e1ea51SMario Limonciello attribute_s_property_show(dell_modifier, str);
70f1e1ea51SMario Limonciello static struct kobj_attribute str_modifier =
71f1e1ea51SMario Limonciello __ATTR_RO(dell_modifier);
72f1e1ea51SMario Limonciello
73f1e1ea51SMario Limonciello attribute_n_property_show(min_length, str);
74f1e1ea51SMario Limonciello static struct kobj_attribute str_min_length =
75f1e1ea51SMario Limonciello __ATTR_RO(min_length);
76f1e1ea51SMario Limonciello
77f1e1ea51SMario Limonciello attribute_n_property_show(max_length, str);
78f1e1ea51SMario Limonciello static struct kobj_attribute str_max_length =
79f1e1ea51SMario Limonciello __ATTR_RO(max_length);
80f1e1ea51SMario Limonciello
type_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)81f1e1ea51SMario Limonciello static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
82f1e1ea51SMario Limonciello char *buf)
83f1e1ea51SMario Limonciello {
84f1e1ea51SMario Limonciello return sprintf(buf, "string\n");
85f1e1ea51SMario Limonciello }
86f1e1ea51SMario Limonciello static struct kobj_attribute str_type =
87f1e1ea51SMario Limonciello __ATTR_RO(type);
88f1e1ea51SMario Limonciello
89f1e1ea51SMario Limonciello static struct attribute *str_attrs[] = {
90f1e1ea51SMario Limonciello &str_displ_langcode.attr,
91f1e1ea51SMario Limonciello &str_displ_name.attr,
92f1e1ea51SMario Limonciello &str_default_val.attr,
93f1e1ea51SMario Limonciello &str_current_val.attr,
94f1e1ea51SMario Limonciello &str_modifier.attr,
95f1e1ea51SMario Limonciello &str_min_length.attr,
96f1e1ea51SMario Limonciello &str_max_length.attr,
97f1e1ea51SMario Limonciello &str_type.attr,
98f1e1ea51SMario Limonciello NULL,
99f1e1ea51SMario Limonciello };
100f1e1ea51SMario Limonciello
101f1e1ea51SMario Limonciello static const struct attribute_group str_attr_group = {
102f1e1ea51SMario Limonciello .attrs = str_attrs,
103f1e1ea51SMario Limonciello };
104f1e1ea51SMario Limonciello
alloc_str_data(void)105f1e1ea51SMario Limonciello int alloc_str_data(void)
106f1e1ea51SMario Limonciello {
107f1e1ea51SMario Limonciello int ret = 0;
108f1e1ea51SMario Limonciello
109f1e1ea51SMario Limonciello wmi_priv.str_instances_count = get_instance_count(DELL_WMI_BIOS_STRING_ATTRIBUTE_GUID);
110f1e1ea51SMario Limonciello wmi_priv.str_data = kcalloc(wmi_priv.str_instances_count,
111f1e1ea51SMario Limonciello sizeof(struct str_data), GFP_KERNEL);
112f1e1ea51SMario Limonciello if (!wmi_priv.str_data) {
113f1e1ea51SMario Limonciello wmi_priv.str_instances_count = 0;
114f1e1ea51SMario Limonciello ret = -ENOMEM;
115f1e1ea51SMario Limonciello }
116f1e1ea51SMario Limonciello return ret;
117f1e1ea51SMario Limonciello }
118f1e1ea51SMario Limonciello
119f1e1ea51SMario Limonciello /**
120f1e1ea51SMario Limonciello * populate_str_data() - Populate all properties of an instance under string attribute
121*97be86e3SPrasanth KSR * @str_obj: ACPI object with string data
122f1e1ea51SMario Limonciello * @instance_id: The instance to enumerate
123f1e1ea51SMario Limonciello * @attr_name_kobj: The parent kernel object
124f1e1ea51SMario Limonciello */
populate_str_data(union acpi_object * str_obj,int instance_id,struct kobject * attr_name_kobj)125f1e1ea51SMario Limonciello int populate_str_data(union acpi_object *str_obj, int instance_id, struct kobject *attr_name_kobj)
126f1e1ea51SMario Limonciello {
127f1e1ea51SMario Limonciello wmi_priv.str_data[instance_id].attr_name_kobj = attr_name_kobj;
128*97be86e3SPrasanth KSR if (check_property_type(str, ATTR_NAME, ACPI_TYPE_STRING))
129*97be86e3SPrasanth KSR return -EINVAL;
130f1e1ea51SMario Limonciello strlcpy_attr(wmi_priv.str_data[instance_id].attribute_name,
131f1e1ea51SMario Limonciello str_obj[ATTR_NAME].string.pointer);
132*97be86e3SPrasanth KSR if (check_property_type(str, DISPL_NAME_LANG_CODE, ACPI_TYPE_STRING))
133*97be86e3SPrasanth KSR return -EINVAL;
134f1e1ea51SMario Limonciello strlcpy_attr(wmi_priv.str_data[instance_id].display_name_language_code,
135f1e1ea51SMario Limonciello str_obj[DISPL_NAME_LANG_CODE].string.pointer);
136*97be86e3SPrasanth KSR if (check_property_type(str, DISPLAY_NAME, ACPI_TYPE_STRING))
137*97be86e3SPrasanth KSR return -EINVAL;
138f1e1ea51SMario Limonciello strlcpy_attr(wmi_priv.str_data[instance_id].display_name,
139f1e1ea51SMario Limonciello str_obj[DISPLAY_NAME].string.pointer);
140*97be86e3SPrasanth KSR if (check_property_type(str, DEFAULT_VAL, ACPI_TYPE_STRING))
141*97be86e3SPrasanth KSR return -EINVAL;
142f1e1ea51SMario Limonciello strlcpy_attr(wmi_priv.str_data[instance_id].default_value,
143f1e1ea51SMario Limonciello str_obj[DEFAULT_VAL].string.pointer);
144*97be86e3SPrasanth KSR if (check_property_type(str, MODIFIER, ACPI_TYPE_STRING))
145*97be86e3SPrasanth KSR return -EINVAL;
146f1e1ea51SMario Limonciello strlcpy_attr(wmi_priv.str_data[instance_id].dell_modifier,
147f1e1ea51SMario Limonciello str_obj[MODIFIER].string.pointer);
148*97be86e3SPrasanth KSR if (check_property_type(str, MIN_LEN, ACPI_TYPE_INTEGER))
149*97be86e3SPrasanth KSR return -EINVAL;
150f1e1ea51SMario Limonciello wmi_priv.str_data[instance_id].min_length = (uintptr_t)str_obj[MIN_LEN].string.pointer;
151*97be86e3SPrasanth KSR if (check_property_type(str, MAX_LEN, ACPI_TYPE_INTEGER))
152*97be86e3SPrasanth KSR return -EINVAL;
153f1e1ea51SMario Limonciello wmi_priv.str_data[instance_id].max_length = (uintptr_t) str_obj[MAX_LEN].string.pointer;
154f1e1ea51SMario Limonciello
155f1e1ea51SMario Limonciello return sysfs_create_group(attr_name_kobj, &str_attr_group);
156f1e1ea51SMario Limonciello }
157f1e1ea51SMario Limonciello
158f1e1ea51SMario Limonciello /**
159f1e1ea51SMario Limonciello * exit_str_attributes() - Clear all attribute data
160f1e1ea51SMario Limonciello *
161f1e1ea51SMario Limonciello * Clears all data allocated for this group of attributes
162f1e1ea51SMario Limonciello */
exit_str_attributes(void)163f1e1ea51SMario Limonciello void exit_str_attributes(void)
164f1e1ea51SMario Limonciello {
165f1e1ea51SMario Limonciello int instance_id;
166f1e1ea51SMario Limonciello
167f1e1ea51SMario Limonciello for (instance_id = 0; instance_id < wmi_priv.str_instances_count; instance_id++) {
168f1e1ea51SMario Limonciello if (wmi_priv.str_data[instance_id].attr_name_kobj)
169f1e1ea51SMario Limonciello sysfs_remove_group(wmi_priv.str_data[instance_id].attr_name_kobj,
170f1e1ea51SMario Limonciello &str_attr_group);
171f1e1ea51SMario Limonciello }
1722d0c418cSHans de Goede wmi_priv.str_instances_count = 0;
1732d0c418cSHans de Goede
174f1e1ea51SMario Limonciello kfree(wmi_priv.str_data);
1752d0c418cSHans de Goede wmi_priv.str_data = NULL;
176f1e1ea51SMario Limonciello }
177