Debian Codegraph @

drivers/platform/x86/dell/dell-wmi-sysman/enum-attributes.c

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