1f1e1ea51SMario Limonciello // SPDX-License-Identifier: GPL-2.0
2f1e1ea51SMario Limonciello /*
3f1e1ea51SMario Limonciello * Common methods for use with dell-wmi-sysman
4f1e1ea51SMario Limonciello *
5f1e1ea51SMario Limonciello * Copyright (c) 2020 Dell Inc.
6f1e1ea51SMario Limonciello */
7f1e1ea51SMario Limonciello
8f1e1ea51SMario Limonciello #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9f1e1ea51SMario Limonciello
10f1e1ea51SMario Limonciello #include <linux/fs.h>
11f1e1ea51SMario Limonciello #include <linux/dmi.h>
12f1e1ea51SMario Limonciello #include <linux/module.h>
13f1e1ea51SMario Limonciello #include <linux/kernel.h>
14f1e1ea51SMario Limonciello #include <linux/wmi.h>
15f1e1ea51SMario Limonciello #include "dell-wmi-sysman.h"
168a1c379cSMark Pearson #include "../../firmware_attributes_class.h"
17f1e1ea51SMario Limonciello
18f1e1ea51SMario Limonciello #define MAX_TYPES 4
19f1e1ea51SMario Limonciello #include <linux/nls.h>
20f1e1ea51SMario Limonciello
21f1e1ea51SMario Limonciello struct wmi_sysman_priv wmi_priv = {
22f1e1ea51SMario Limonciello .mutex = __MUTEX_INITIALIZER(wmi_priv.mutex),
23f1e1ea51SMario Limonciello };
24f1e1ea51SMario Limonciello
25f1e1ea51SMario Limonciello /* reset bios to defaults */
26f1e1ea51SMario Limonciello static const char * const reset_types[] = {"builtinsafe", "lastknowngood", "factory", "custom"};
27f1e1ea51SMario Limonciello static int reset_option = -1;
28cb58c277SHans de Goede static struct class *fw_attr_class;
29f1e1ea51SMario Limonciello
30f1e1ea51SMario Limonciello
31f1e1ea51SMario Limonciello /**
32f1e1ea51SMario Limonciello * populate_string_buffer() - populates a string buffer
33f1e1ea51SMario Limonciello * @buffer: the start of the destination buffer
34f1e1ea51SMario Limonciello * @buffer_len: length of the destination buffer
35f1e1ea51SMario Limonciello * @str: the string to insert into buffer
36f1e1ea51SMario Limonciello */
populate_string_buffer(char * buffer,size_t buffer_len,const char * str)37f1e1ea51SMario Limonciello ssize_t populate_string_buffer(char *buffer, size_t buffer_len, const char *str)
38f1e1ea51SMario Limonciello {
39f1e1ea51SMario Limonciello u16 *length = (u16 *)buffer;
40f1e1ea51SMario Limonciello u16 *target = length + 1;
41f1e1ea51SMario Limonciello int ret;
42f1e1ea51SMario Limonciello
43f1e1ea51SMario Limonciello ret = utf8s_to_utf16s(str, strlen(str), UTF16_HOST_ENDIAN,
44f1e1ea51SMario Limonciello target, buffer_len - sizeof(u16));
45f1e1ea51SMario Limonciello if (ret < 0) {
46f1e1ea51SMario Limonciello dev_err(wmi_priv.class_dev, "UTF16 conversion failed\n");
47f1e1ea51SMario Limonciello return ret;
48f1e1ea51SMario Limonciello }
49f1e1ea51SMario Limonciello
50f1e1ea51SMario Limonciello if ((ret * sizeof(u16)) > U16_MAX) {
51f1e1ea51SMario Limonciello dev_err(wmi_priv.class_dev, "Error string too long\n");
52f1e1ea51SMario Limonciello return -ERANGE;
53f1e1ea51SMario Limonciello }
54f1e1ea51SMario Limonciello
55f1e1ea51SMario Limonciello *length = ret * sizeof(u16);
56f1e1ea51SMario Limonciello return sizeof(u16) + *length;
57f1e1ea51SMario Limonciello }
58f1e1ea51SMario Limonciello
59f1e1ea51SMario Limonciello /**
60f1e1ea51SMario Limonciello * calculate_string_buffer() - determines size of string buffer for use with BIOS communication
61f1e1ea51SMario Limonciello * @str: the string to calculate based upon
62f1e1ea51SMario Limonciello *
63f1e1ea51SMario Limonciello */
calculate_string_buffer(const char * str)64f1e1ea51SMario Limonciello size_t calculate_string_buffer(const char *str)
65f1e1ea51SMario Limonciello {
66f1e1ea51SMario Limonciello /* u16 length field + one UTF16 char for each input char */
67f1e1ea51SMario Limonciello return sizeof(u16) + strlen(str) * sizeof(u16);
68f1e1ea51SMario Limonciello }
69f1e1ea51SMario Limonciello
70f1e1ea51SMario Limonciello /**
71f1e1ea51SMario Limonciello * calculate_security_buffer() - determines size of security buffer for authentication scheme
72f1e1ea51SMario Limonciello * @authentication: the authentication content
73f1e1ea51SMario Limonciello *
74f1e1ea51SMario Limonciello * Currently only supported type is Admin password
75f1e1ea51SMario Limonciello */
calculate_security_buffer(char * authentication)76f1e1ea51SMario Limonciello size_t calculate_security_buffer(char *authentication)
77f1e1ea51SMario Limonciello {
78f1e1ea51SMario Limonciello if (strlen(authentication) > 0) {
79f1e1ea51SMario Limonciello return (sizeof(u32) * 2) + strlen(authentication) +
80f1e1ea51SMario Limonciello strlen(authentication) % 2;
81f1e1ea51SMario Limonciello }
82f1e1ea51SMario Limonciello return sizeof(u32) * 2;
83f1e1ea51SMario Limonciello }
84f1e1ea51SMario Limonciello
85f1e1ea51SMario Limonciello /**
86f1e1ea51SMario Limonciello * populate_security_buffer() - builds a security buffer for authentication scheme
87f1e1ea51SMario Limonciello * @buffer: the buffer to populate
88f1e1ea51SMario Limonciello * @authentication: the authentication content
89f1e1ea51SMario Limonciello *
90f1e1ea51SMario Limonciello * Currently only supported type is PLAIN TEXT
91f1e1ea51SMario Limonciello */
populate_security_buffer(char * buffer,char * authentication)92f1e1ea51SMario Limonciello void populate_security_buffer(char *buffer, char *authentication)
93f1e1ea51SMario Limonciello {
94f1e1ea51SMario Limonciello char *auth = buffer + sizeof(u32) * 2;
95f1e1ea51SMario Limonciello u32 *sectype = (u32 *) buffer;
96f1e1ea51SMario Limonciello u32 *seclen = sectype + 1;
97f1e1ea51SMario Limonciello
98f1e1ea51SMario Limonciello *sectype = strlen(authentication) > 0 ? 1 : 0;
99f1e1ea51SMario Limonciello *seclen = strlen(authentication);
100f1e1ea51SMario Limonciello
101f1e1ea51SMario Limonciello /* plain text */
102f1e1ea51SMario Limonciello if (strlen(authentication) > 0)
103f1e1ea51SMario Limonciello memcpy(auth, authentication, *seclen);
104f1e1ea51SMario Limonciello }
105f1e1ea51SMario Limonciello
106f1e1ea51SMario Limonciello /**
107f1e1ea51SMario Limonciello * map_wmi_error() - map errors from WMI methods to kernel error codes
108f1e1ea51SMario Limonciello * @error_code: integer error code returned from Dell's firmware
109f1e1ea51SMario Limonciello */
map_wmi_error(int error_code)110f1e1ea51SMario Limonciello int map_wmi_error(int error_code)
111f1e1ea51SMario Limonciello {
112f1e1ea51SMario Limonciello switch (error_code) {
113f1e1ea51SMario Limonciello case 0:
114f1e1ea51SMario Limonciello /* success */
115f1e1ea51SMario Limonciello return 0;
116f1e1ea51SMario Limonciello case 1:
117f1e1ea51SMario Limonciello /* failed */
118f1e1ea51SMario Limonciello return -EIO;
119f1e1ea51SMario Limonciello case 2:
120f1e1ea51SMario Limonciello /* invalid parameter */
121f1e1ea51SMario Limonciello return -EINVAL;
122f1e1ea51SMario Limonciello case 3:
123f1e1ea51SMario Limonciello /* access denied */
124f1e1ea51SMario Limonciello return -EACCES;
125f1e1ea51SMario Limonciello case 4:
126f1e1ea51SMario Limonciello /* not supported */
127f1e1ea51SMario Limonciello return -EOPNOTSUPP;
128f1e1ea51SMario Limonciello case 5:
129f1e1ea51SMario Limonciello /* memory error */
130f1e1ea51SMario Limonciello return -ENOMEM;
131f1e1ea51SMario Limonciello case 6:
132f1e1ea51SMario Limonciello /* protocol error */
133f1e1ea51SMario Limonciello return -EPROTO;
134f1e1ea51SMario Limonciello }
135f1e1ea51SMario Limonciello /* unspecified error */
136f1e1ea51SMario Limonciello return -EIO;
137f1e1ea51SMario Limonciello }
138f1e1ea51SMario Limonciello
139f1e1ea51SMario Limonciello /**
140f1e1ea51SMario Limonciello * reset_bios_show() - sysfs implementaton for read reset_bios
141f1e1ea51SMario Limonciello * @kobj: Kernel object for this attribute
142f1e1ea51SMario Limonciello * @attr: Kernel object attribute
143f1e1ea51SMario Limonciello * @buf: The buffer to display to userspace
144f1e1ea51SMario Limonciello */
reset_bios_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)145f1e1ea51SMario Limonciello static ssize_t reset_bios_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
146f1e1ea51SMario Limonciello {
147f1e1ea51SMario Limonciello char *start = buf;
148f1e1ea51SMario Limonciello int i;
149f1e1ea51SMario Limonciello
150f1e1ea51SMario Limonciello for (i = 0; i < MAX_TYPES; i++) {
151f1e1ea51SMario Limonciello if (i == reset_option)
152f1e1ea51SMario Limonciello buf += sprintf(buf, "[%s] ", reset_types[i]);
153f1e1ea51SMario Limonciello else
154f1e1ea51SMario Limonciello buf += sprintf(buf, "%s ", reset_types[i]);
155f1e1ea51SMario Limonciello }
156f1e1ea51SMario Limonciello buf += sprintf(buf, "\n");
157f1e1ea51SMario Limonciello return buf-start;
158f1e1ea51SMario Limonciello }
159f1e1ea51SMario Limonciello
160f1e1ea51SMario Limonciello /**
161f1e1ea51SMario Limonciello * reset_bios_store() - sysfs implementaton for write reset_bios
162f1e1ea51SMario Limonciello * @kobj: Kernel object for this attribute
163f1e1ea51SMario Limonciello * @attr: Kernel object attribute
164f1e1ea51SMario Limonciello * @buf: The buffer from userspace
165f1e1ea51SMario Limonciello * @count: the size of the buffer from userspace
166f1e1ea51SMario Limonciello */
reset_bios_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)167f1e1ea51SMario Limonciello static ssize_t reset_bios_store(struct kobject *kobj,
168f1e1ea51SMario Limonciello struct kobj_attribute *attr, const char *buf, size_t count)
169f1e1ea51SMario Limonciello {
170f1e1ea51SMario Limonciello int type = sysfs_match_string(reset_types, buf);
171f1e1ea51SMario Limonciello int ret;
172f1e1ea51SMario Limonciello
173f1e1ea51SMario Limonciello if (type < 0)
174f1e1ea51SMario Limonciello return type;
175f1e1ea51SMario Limonciello
176f1e1ea51SMario Limonciello ret = set_bios_defaults(type);
177f1e1ea51SMario Limonciello pr_debug("reset all attributes request type %d: %d\n", type, ret);
178f1e1ea51SMario Limonciello if (!ret) {
179f1e1ea51SMario Limonciello reset_option = type;
180f1e1ea51SMario Limonciello ret = count;
181f1e1ea51SMario Limonciello }
182f1e1ea51SMario Limonciello
183f1e1ea51SMario Limonciello return ret;
184f1e1ea51SMario Limonciello }
185f1e1ea51SMario Limonciello
186f1e1ea51SMario Limonciello /**
187f1e1ea51SMario Limonciello * pending_reboot_show() - sysfs implementaton for read pending_reboot
188f1e1ea51SMario Limonciello * @kobj: Kernel object for this attribute
189f1e1ea51SMario Limonciello * @attr: Kernel object attribute
190f1e1ea51SMario Limonciello * @buf: The buffer to display to userspace
191f1e1ea51SMario Limonciello *
192f1e1ea51SMario Limonciello * Stores default value as 0
193f1e1ea51SMario Limonciello * When current_value is changed this attribute is set to 1 to notify reboot may be required
194f1e1ea51SMario Limonciello */
pending_reboot_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)195f1e1ea51SMario Limonciello static ssize_t pending_reboot_show(struct kobject *kobj, struct kobj_attribute *attr,
196f1e1ea51SMario Limonciello char *buf)
197f1e1ea51SMario Limonciello {
198f1e1ea51SMario Limonciello return sprintf(buf, "%d\n", wmi_priv.pending_changes);
199f1e1ea51SMario Limonciello }
200f1e1ea51SMario Limonciello
201f1e1ea51SMario Limonciello static struct kobj_attribute reset_bios = __ATTR_RW(reset_bios);
202f1e1ea51SMario Limonciello static struct kobj_attribute pending_reboot = __ATTR_RO(pending_reboot);
203f1e1ea51SMario Limonciello
204f1e1ea51SMario Limonciello
205f1e1ea51SMario Limonciello /**
206f1e1ea51SMario Limonciello * create_attributes_level_sysfs_files() - Creates reset_bios and
207f1e1ea51SMario Limonciello * pending_reboot attributes
208f1e1ea51SMario Limonciello */
create_attributes_level_sysfs_files(void)209f1e1ea51SMario Limonciello static int create_attributes_level_sysfs_files(void)
210f1e1ea51SMario Limonciello {
21142f38dccSHans de Goede int ret;
212f1e1ea51SMario Limonciello
21342f38dccSHans de Goede ret = sysfs_create_file(&wmi_priv.main_dir_kset->kobj, &reset_bios.attr);
21442f38dccSHans de Goede if (ret)
215f1e1ea51SMario Limonciello return ret;
216f1e1ea51SMario Limonciello
217f1e1ea51SMario Limonciello ret = sysfs_create_file(&wmi_priv.main_dir_kset->kobj, &pending_reboot.attr);
21842f38dccSHans de Goede if (ret)
219f1e1ea51SMario Limonciello return ret;
22042f38dccSHans de Goede
22142f38dccSHans de Goede return 0;
222f1e1ea51SMario Limonciello }
223f1e1ea51SMario Limonciello
wmi_sysman_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)224f1e1ea51SMario Limonciello static ssize_t wmi_sysman_attr_show(struct kobject *kobj, struct attribute *attr,
225f1e1ea51SMario Limonciello char *buf)
226f1e1ea51SMario Limonciello {
227f1e1ea51SMario Limonciello struct kobj_attribute *kattr;
228f1e1ea51SMario Limonciello ssize_t ret = -EIO;
229f1e1ea51SMario Limonciello
230f1e1ea51SMario Limonciello kattr = container_of(attr, struct kobj_attribute, attr);
231f1e1ea51SMario Limonciello if (kattr->show)
232f1e1ea51SMario Limonciello ret = kattr->show(kobj, kattr, buf);
233f1e1ea51SMario Limonciello return ret;
234f1e1ea51SMario Limonciello }
235f1e1ea51SMario Limonciello
wmi_sysman_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)236f1e1ea51SMario Limonciello static ssize_t wmi_sysman_attr_store(struct kobject *kobj, struct attribute *attr,
237f1e1ea51SMario Limonciello const char *buf, size_t count)
238f1e1ea51SMario Limonciello {
239f1e1ea51SMario Limonciello struct kobj_attribute *kattr;
240f1e1ea51SMario Limonciello ssize_t ret = -EIO;
241f1e1ea51SMario Limonciello
242f1e1ea51SMario Limonciello kattr = container_of(attr, struct kobj_attribute, attr);
243f1e1ea51SMario Limonciello if (kattr->store)
244f1e1ea51SMario Limonciello ret = kattr->store(kobj, kattr, buf, count);
245f1e1ea51SMario Limonciello return ret;
246f1e1ea51SMario Limonciello }
247f1e1ea51SMario Limonciello
248f1e1ea51SMario Limonciello static const struct sysfs_ops wmi_sysman_kobj_sysfs_ops = {
249f1e1ea51SMario Limonciello .show = wmi_sysman_attr_show,
250f1e1ea51SMario Limonciello .store = wmi_sysman_attr_store,
251f1e1ea51SMario Limonciello };
252f1e1ea51SMario Limonciello
attr_name_release(struct kobject * kobj)253f1e1ea51SMario Limonciello static void attr_name_release(struct kobject *kobj)
254f1e1ea51SMario Limonciello {
255f1e1ea51SMario Limonciello kfree(kobj);
256f1e1ea51SMario Limonciello }
257f1e1ea51SMario Limonciello
258438688d5SThomas Weißschuh static const struct kobj_type attr_name_ktype = {
259f1e1ea51SMario Limonciello .release = attr_name_release,
260f1e1ea51SMario Limonciello .sysfs_ops = &wmi_sysman_kobj_sysfs_ops,
261f1e1ea51SMario Limonciello };
262f1e1ea51SMario Limonciello
263f1e1ea51SMario Limonciello /**
264f1e1ea51SMario Limonciello * strlcpy_attr - Copy a length-limited, NULL-terminated string with bound checks
265f1e1ea51SMario Limonciello * @dest: Where to copy the string to
266f1e1ea51SMario Limonciello * @src: Where to copy the string from
267f1e1ea51SMario Limonciello */
strlcpy_attr(char * dest,char * src)268f1e1ea51SMario Limonciello void strlcpy_attr(char *dest, char *src)
269f1e1ea51SMario Limonciello {
270f1e1ea51SMario Limonciello size_t len = strlen(src) + 1;
271f1e1ea51SMario Limonciello
272f1e1ea51SMario Limonciello if (len > 1 && len <= MAX_BUFF)
2739c9ac2e6SWolfram Sang strscpy(dest, src, len);
274f1e1ea51SMario Limonciello
275f1e1ea51SMario Limonciello /*len can be zero because any property not-applicable to attribute can
276f1e1ea51SMario Limonciello * be empty so check only for too long buffers and log error
277f1e1ea51SMario Limonciello */
278f1e1ea51SMario Limonciello if (len > MAX_BUFF)
279f1e1ea51SMario Limonciello pr_err("Source string returned from BIOS is out of bound!\n");
280f1e1ea51SMario Limonciello }
281f1e1ea51SMario Limonciello
282f1e1ea51SMario Limonciello /**
283f1e1ea51SMario Limonciello * get_wmiobj_pointer() - Get Content of WMI block for particular instance
284f1e1ea51SMario Limonciello * @instance_id: WMI instance ID
285f1e1ea51SMario Limonciello * @guid_string: WMI GUID (in str form)
286f1e1ea51SMario Limonciello *
287f1e1ea51SMario Limonciello * Fetches the content for WMI block (instance_id) under GUID (guid_string)
288f1e1ea51SMario Limonciello * Caller must kfree the return
289f1e1ea51SMario Limonciello */
get_wmiobj_pointer(int instance_id,const char * guid_string)290f1e1ea51SMario Limonciello union acpi_object *get_wmiobj_pointer(int instance_id, const char *guid_string)
291f1e1ea51SMario Limonciello {
292f1e1ea51SMario Limonciello struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
293f1e1ea51SMario Limonciello acpi_status status;
294f1e1ea51SMario Limonciello
295f1e1ea51SMario Limonciello status = wmi_query_block(guid_string, instance_id, &out);
296f1e1ea51SMario Limonciello
297f1e1ea51SMario Limonciello return ACPI_SUCCESS(status) ? (union acpi_object *)out.pointer : NULL;
298f1e1ea51SMario Limonciello }
299f1e1ea51SMario Limonciello
300f1e1ea51SMario Limonciello /**
301f1e1ea51SMario Limonciello * get_instance_count() - Compute total number of instances under guid_string
302f1e1ea51SMario Limonciello * @guid_string: WMI GUID (in string form)
303f1e1ea51SMario Limonciello */
get_instance_count(const char * guid_string)304f1e1ea51SMario Limonciello int get_instance_count(const char *guid_string)
305f1e1ea51SMario Limonciello {
306d7296af8SArmin Wolf int ret;
307f1e1ea51SMario Limonciello
308d7296af8SArmin Wolf ret = wmi_instance_count(guid_string);
309d7296af8SArmin Wolf if (ret < 0)
310d7296af8SArmin Wolf return 0;
311f1e1ea51SMario Limonciello
312d7296af8SArmin Wolf return ret;
313f1e1ea51SMario Limonciello }
314f1e1ea51SMario Limonciello
315f1e1ea51SMario Limonciello /**
316f1e1ea51SMario Limonciello * alloc_attributes_data() - Allocate attributes data for a particular type
317f1e1ea51SMario Limonciello * @attr_type: Attribute type to allocate
318f1e1ea51SMario Limonciello */
alloc_attributes_data(int attr_type)319f1e1ea51SMario Limonciello static int alloc_attributes_data(int attr_type)
320f1e1ea51SMario Limonciello {
321f1e1ea51SMario Limonciello int retval = 0;
322f1e1ea51SMario Limonciello
323f1e1ea51SMario Limonciello switch (attr_type) {
324f1e1ea51SMario Limonciello case ENUM:
325f1e1ea51SMario Limonciello retval = alloc_enum_data();
326f1e1ea51SMario Limonciello break;
327f1e1ea51SMario Limonciello case INT:
328f1e1ea51SMario Limonciello retval = alloc_int_data();
329f1e1ea51SMario Limonciello break;
330f1e1ea51SMario Limonciello case STR:
331f1e1ea51SMario Limonciello retval = alloc_str_data();
332f1e1ea51SMario Limonciello break;
333f1e1ea51SMario Limonciello case PO:
334f1e1ea51SMario Limonciello retval = alloc_po_data();
335f1e1ea51SMario Limonciello break;
336f1e1ea51SMario Limonciello default:
337f1e1ea51SMario Limonciello break;
338f1e1ea51SMario Limonciello }
339f1e1ea51SMario Limonciello
340f1e1ea51SMario Limonciello return retval;
341f1e1ea51SMario Limonciello }
342f1e1ea51SMario Limonciello
343f1e1ea51SMario Limonciello /**
344f1e1ea51SMario Limonciello * destroy_attribute_objs() - Free a kset of kobjects
345f1e1ea51SMario Limonciello * @kset: The kset to destroy
346f1e1ea51SMario Limonciello *
347f1e1ea51SMario Limonciello * Fress kobjects created for each attribute_name under attribute type kset
348f1e1ea51SMario Limonciello */
destroy_attribute_objs(struct kset * kset)349f1e1ea51SMario Limonciello static void destroy_attribute_objs(struct kset *kset)
350f1e1ea51SMario Limonciello {
351f1e1ea51SMario Limonciello struct kobject *pos, *next;
352f1e1ea51SMario Limonciello
353f1e1ea51SMario Limonciello list_for_each_entry_safe(pos, next, &kset->list, entry) {
354f1e1ea51SMario Limonciello kobject_put(pos);
355f1e1ea51SMario Limonciello }
356f1e1ea51SMario Limonciello }
357f1e1ea51SMario Limonciello
358f1e1ea51SMario Limonciello /**
359f1e1ea51SMario Limonciello * release_attributes_data() - Clean-up all sysfs directories and files created
360f1e1ea51SMario Limonciello */
release_attributes_data(void)361f1e1ea51SMario Limonciello static void release_attributes_data(void)
362f1e1ea51SMario Limonciello {
363f1e1ea51SMario Limonciello mutex_lock(&wmi_priv.mutex);
364f1e1ea51SMario Limonciello exit_enum_attributes();
365f1e1ea51SMario Limonciello exit_int_attributes();
366f1e1ea51SMario Limonciello exit_str_attributes();
367f1e1ea51SMario Limonciello exit_po_attributes();
368f1e1ea51SMario Limonciello if (wmi_priv.authentication_dir_kset) {
369f1e1ea51SMario Limonciello destroy_attribute_objs(wmi_priv.authentication_dir_kset);
370f1e1ea51SMario Limonciello kset_unregister(wmi_priv.authentication_dir_kset);
371f1e1ea51SMario Limonciello wmi_priv.authentication_dir_kset = NULL;
372f1e1ea51SMario Limonciello }
373f1e1ea51SMario Limonciello if (wmi_priv.main_dir_kset) {
374ececdb89SHans de Goede sysfs_remove_file(&wmi_priv.main_dir_kset->kobj, &reset_bios.attr);
375ececdb89SHans de Goede sysfs_remove_file(&wmi_priv.main_dir_kset->kobj, &pending_reboot.attr);
376f1e1ea51SMario Limonciello destroy_attribute_objs(wmi_priv.main_dir_kset);
377f1e1ea51SMario Limonciello kset_unregister(wmi_priv.main_dir_kset);
378bdda3967SHans de Goede wmi_priv.main_dir_kset = NULL;
379f1e1ea51SMario Limonciello }
380f1e1ea51SMario Limonciello mutex_unlock(&wmi_priv.mutex);
381f1e1ea51SMario Limonciello }
382f1e1ea51SMario Limonciello
383f1e1ea51SMario Limonciello /**
384f1e1ea51SMario Limonciello * init_bios_attributes() - Initialize all attributes for a type
385f1e1ea51SMario Limonciello * @attr_type: The attribute type to initialize
386f1e1ea51SMario Limonciello * @guid: The WMI GUID associated with this type to initialize
387f1e1ea51SMario Limonciello *
388f1e1ea51SMario Limonciello * Initialiaze all 4 types of attributes enumeration, integer, string and password object.
389f1e1ea51SMario Limonciello * Populates each attrbute typ's respective properties under sysfs files
390f1e1ea51SMario Limonciello */
init_bios_attributes(int attr_type,const char * guid)391f1e1ea51SMario Limonciello static int init_bios_attributes(int attr_type, const char *guid)
392f1e1ea51SMario Limonciello {
393f1e1ea51SMario Limonciello struct kobject *attr_name_kobj; //individual attribute names
394f1e1ea51SMario Limonciello union acpi_object *obj = NULL;
395f1e1ea51SMario Limonciello union acpi_object *elements;
3967295a996SArmin Wolf struct kobject *duplicate;
397f1e1ea51SMario Limonciello struct kset *tmp_set;
3985e3f5973SHans de Goede int min_elements;
399f1e1ea51SMario Limonciello
400f1e1ea51SMario Limonciello /* instance_id needs to be reset for each type GUID
401f1e1ea51SMario Limonciello * also, instance IDs are unique within GUID but not across
402f1e1ea51SMario Limonciello */
403f1e1ea51SMario Limonciello int instance_id = 0;
404f1e1ea51SMario Limonciello int retval = 0;
405f1e1ea51SMario Limonciello
406f1e1ea51SMario Limonciello retval = alloc_attributes_data(attr_type);
407f1e1ea51SMario Limonciello if (retval)
408f1e1ea51SMario Limonciello return retval;
4095e3f5973SHans de Goede
4105e3f5973SHans de Goede switch (attr_type) {
4115e3f5973SHans de Goede case ENUM: min_elements = 8; break;
4125e3f5973SHans de Goede case INT: min_elements = 9; break;
4135e3f5973SHans de Goede case STR: min_elements = 8; break;
4145e3f5973SHans de Goede case PO: min_elements = 4; break;
4155e3f5973SHans de Goede default:
4165e3f5973SHans de Goede pr_err("Error: Unknown attr_type: %d\n", attr_type);
4175e3f5973SHans de Goede return -EINVAL;
4185e3f5973SHans de Goede }
4195e3f5973SHans de Goede
420f1e1ea51SMario Limonciello /* need to use specific instance_id and guid combination to get right data */
421f1e1ea51SMario Limonciello obj = get_wmiobj_pointer(instance_id, guid);
4225e3f5973SHans de Goede if (!obj)
423f1e1ea51SMario Limonciello return -ENODEV;
424f1e1ea51SMario Limonciello
425f1e1ea51SMario Limonciello mutex_lock(&wmi_priv.mutex);
4265e3f5973SHans de Goede while (obj) {
4275e3f5973SHans de Goede if (obj->type != ACPI_TYPE_PACKAGE) {
4285e3f5973SHans de Goede pr_err("Error: Expected ACPI-package type, got: %d\n", obj->type);
4295e3f5973SHans de Goede retval = -EIO;
4305e3f5973SHans de Goede goto err_attr_init;
4315e3f5973SHans de Goede }
4325e3f5973SHans de Goede
4335e3f5973SHans de Goede if (obj->package.count < min_elements) {
4345e3f5973SHans de Goede pr_err("Error: ACPI-package does not have enough elements: %d < %d\n",
4355e3f5973SHans de Goede obj->package.count, min_elements);
4365e3f5973SHans de Goede goto nextobj;
4375e3f5973SHans de Goede }
4385e3f5973SHans de Goede
4395e3f5973SHans de Goede elements = obj->package.elements;
4405e3f5973SHans de Goede
441f1e1ea51SMario Limonciello /* sanity checking */
442f1e1ea51SMario Limonciello if (elements[ATTR_NAME].type != ACPI_TYPE_STRING) {
443f1e1ea51SMario Limonciello pr_debug("incorrect element type\n");
444f1e1ea51SMario Limonciello goto nextobj;
445f1e1ea51SMario Limonciello }
446f1e1ea51SMario Limonciello if (strlen(elements[ATTR_NAME].string.pointer) == 0) {
447f1e1ea51SMario Limonciello pr_debug("empty attribute found\n");
448f1e1ea51SMario Limonciello goto nextobj;
449f1e1ea51SMario Limonciello }
450f1e1ea51SMario Limonciello if (attr_type == PO)
451f1e1ea51SMario Limonciello tmp_set = wmi_priv.authentication_dir_kset;
452f1e1ea51SMario Limonciello else
453f1e1ea51SMario Limonciello tmp_set = wmi_priv.main_dir_kset;
454f1e1ea51SMario Limonciello
4557295a996SArmin Wolf duplicate = kset_find_obj(tmp_set, elements[ATTR_NAME].string.pointer);
4567295a996SArmin Wolf if (duplicate) {
4577295a996SArmin Wolf pr_debug("Duplicate attribute name found - %s\n",
458f1e1ea51SMario Limonciello elements[ATTR_NAME].string.pointer);
4597295a996SArmin Wolf kobject_put(duplicate);
460f1e1ea51SMario Limonciello goto nextobj;
461f1e1ea51SMario Limonciello }
462f1e1ea51SMario Limonciello
463f1e1ea51SMario Limonciello /* build attribute */
464f1e1ea51SMario Limonciello attr_name_kobj = kzalloc(sizeof(*attr_name_kobj), GFP_KERNEL);
465f1e1ea51SMario Limonciello if (!attr_name_kobj) {
466f1e1ea51SMario Limonciello retval = -ENOMEM;
467f1e1ea51SMario Limonciello goto err_attr_init;
468f1e1ea51SMario Limonciello }
469f1e1ea51SMario Limonciello
470f1e1ea51SMario Limonciello attr_name_kobj->kset = tmp_set;
471f1e1ea51SMario Limonciello
472f1e1ea51SMario Limonciello retval = kobject_init_and_add(attr_name_kobj, &attr_name_ktype, NULL, "%s",
473f1e1ea51SMario Limonciello elements[ATTR_NAME].string.pointer);
474f1e1ea51SMario Limonciello if (retval) {
475f1e1ea51SMario Limonciello kobject_put(attr_name_kobj);
476f1e1ea51SMario Limonciello goto err_attr_init;
477f1e1ea51SMario Limonciello }
478f1e1ea51SMario Limonciello
479f1e1ea51SMario Limonciello /* enumerate all of this attribute */
480f1e1ea51SMario Limonciello switch (attr_type) {
481f1e1ea51SMario Limonciello case ENUM:
48297be86e3SPrasanth KSR retval = populate_enum_data(elements, instance_id, attr_name_kobj,
48397be86e3SPrasanth KSR obj->package.count);
484f1e1ea51SMario Limonciello break;
485f1e1ea51SMario Limonciello case INT:
486f1e1ea51SMario Limonciello retval = populate_int_data(elements, instance_id, attr_name_kobj);
487f1e1ea51SMario Limonciello break;
488f1e1ea51SMario Limonciello case STR:
489f1e1ea51SMario Limonciello retval = populate_str_data(elements, instance_id, attr_name_kobj);
490f1e1ea51SMario Limonciello break;
491f1e1ea51SMario Limonciello case PO:
492f1e1ea51SMario Limonciello retval = populate_po_data(elements, instance_id, attr_name_kobj);
493f1e1ea51SMario Limonciello break;
494f1e1ea51SMario Limonciello default:
495f1e1ea51SMario Limonciello break;
496f1e1ea51SMario Limonciello }
497f1e1ea51SMario Limonciello
498f1e1ea51SMario Limonciello if (retval) {
499f1e1ea51SMario Limonciello pr_debug("failed to populate %s\n",
500f1e1ea51SMario Limonciello elements[ATTR_NAME].string.pointer);
501f1e1ea51SMario Limonciello goto err_attr_init;
502f1e1ea51SMario Limonciello }
503f1e1ea51SMario Limonciello
504f1e1ea51SMario Limonciello nextobj:
505f1e1ea51SMario Limonciello kfree(obj);
506f1e1ea51SMario Limonciello instance_id++;
507f1e1ea51SMario Limonciello obj = get_wmiobj_pointer(instance_id, guid);
508f1e1ea51SMario Limonciello }
509f1e1ea51SMario Limonciello
510f1e1ea51SMario Limonciello mutex_unlock(&wmi_priv.mutex);
511f1e1ea51SMario Limonciello return 0;
512f1e1ea51SMario Limonciello
513f1e1ea51SMario Limonciello err_attr_init:
514f1e1ea51SMario Limonciello mutex_unlock(&wmi_priv.mutex);
515f1e1ea51SMario Limonciello kfree(obj);
516f1e1ea51SMario Limonciello return retval;
517f1e1ea51SMario Limonciello }
518f1e1ea51SMario Limonciello
sysman_init(void)519f1e1ea51SMario Limonciello static int __init sysman_init(void)
520f1e1ea51SMario Limonciello {
521f1e1ea51SMario Limonciello int ret = 0;
522f1e1ea51SMario Limonciello
523f1e1ea51SMario Limonciello if (!dmi_find_device(DMI_DEV_TYPE_OEM_STRING, "Dell System", NULL) &&
524*3d75f500SCrag Wang !dmi_find_device(DMI_DEV_TYPE_OEM_STRING, "Alienware", NULL) &&
525f1e1ea51SMario Limonciello !dmi_find_device(DMI_DEV_TYPE_OEM_STRING, "www.dell.com", NULL)) {
526f1e1ea51SMario Limonciello pr_err("Unable to run on non-Dell system\n");
527f1e1ea51SMario Limonciello return -ENODEV;
528f1e1ea51SMario Limonciello }
529f1e1ea51SMario Limonciello
530f1e1ea51SMario Limonciello ret = init_bios_attr_set_interface();
5319b95665aSHans de Goede if (ret)
532eaa1dcc7SHans de Goede return ret;
533f1e1ea51SMario Limonciello
534f1e1ea51SMario Limonciello ret = init_bios_attr_pass_interface();
5359b95665aSHans de Goede if (ret)
536eaa1dcc7SHans de Goede goto err_exit_bios_attr_set_interface;
5379b95665aSHans de Goede
5389b95665aSHans de Goede if (!wmi_priv.bios_attr_wdev || !wmi_priv.password_attr_wdev) {
5399b95665aSHans de Goede pr_debug("failed to find set or pass interface\n");
5409b95665aSHans de Goede ret = -ENODEV;
5419b95665aSHans de Goede goto err_exit_bios_attr_pass_interface;
542f1e1ea51SMario Limonciello }
543f1e1ea51SMario Limonciello
5448a1c379cSMark Pearson ret = fw_attributes_class_get(&fw_attr_class);
545f1e1ea51SMario Limonciello if (ret)
546eaa1dcc7SHans de Goede goto err_exit_bios_attr_pass_interface;
547f1e1ea51SMario Limonciello
5488a1c379cSMark Pearson wmi_priv.class_dev = device_create(fw_attr_class, NULL, MKDEV(0, 0),
549f1e1ea51SMario Limonciello NULL, "%s", DRIVER_NAME);
550f1e1ea51SMario Limonciello if (IS_ERR(wmi_priv.class_dev)) {
551f1e1ea51SMario Limonciello ret = PTR_ERR(wmi_priv.class_dev);
552eaa1dcc7SHans de Goede goto err_unregister_class;
553f1e1ea51SMario Limonciello }
554f1e1ea51SMario Limonciello
555f1e1ea51SMario Limonciello wmi_priv.main_dir_kset = kset_create_and_add("attributes", NULL,
556f1e1ea51SMario Limonciello &wmi_priv.class_dev->kobj);
557f1e1ea51SMario Limonciello if (!wmi_priv.main_dir_kset) {
558f1e1ea51SMario Limonciello ret = -ENOMEM;
559eaa1dcc7SHans de Goede goto err_destroy_classdev;
560f1e1ea51SMario Limonciello }
561f1e1ea51SMario Limonciello
562f1e1ea51SMario Limonciello wmi_priv.authentication_dir_kset = kset_create_and_add("authentication", NULL,
563f1e1ea51SMario Limonciello &wmi_priv.class_dev->kobj);
564f1e1ea51SMario Limonciello if (!wmi_priv.authentication_dir_kset) {
565f1e1ea51SMario Limonciello ret = -ENOMEM;
566eaa1dcc7SHans de Goede goto err_release_attributes_data;
567f1e1ea51SMario Limonciello }
568f1e1ea51SMario Limonciello
569f1e1ea51SMario Limonciello ret = create_attributes_level_sysfs_files();
570f1e1ea51SMario Limonciello if (ret) {
571f1e1ea51SMario Limonciello pr_debug("could not create reset BIOS attribute\n");
572eaa1dcc7SHans de Goede goto err_release_attributes_data;
573f1e1ea51SMario Limonciello }
574f1e1ea51SMario Limonciello
575f1e1ea51SMario Limonciello ret = init_bios_attributes(ENUM, DELL_WMI_BIOS_ENUMERATION_ATTRIBUTE_GUID);
576f1e1ea51SMario Limonciello if (ret) {
577f1e1ea51SMario Limonciello pr_debug("failed to populate enumeration type attributes\n");
578eaa1dcc7SHans de Goede goto err_release_attributes_data;
579f1e1ea51SMario Limonciello }
580f1e1ea51SMario Limonciello
581f1e1ea51SMario Limonciello ret = init_bios_attributes(INT, DELL_WMI_BIOS_INTEGER_ATTRIBUTE_GUID);
582f1e1ea51SMario Limonciello if (ret) {
583f1e1ea51SMario Limonciello pr_debug("failed to populate integer type attributes\n");
584eaa1dcc7SHans de Goede goto err_release_attributes_data;
585f1e1ea51SMario Limonciello }
586f1e1ea51SMario Limonciello
587f1e1ea51SMario Limonciello ret = init_bios_attributes(STR, DELL_WMI_BIOS_STRING_ATTRIBUTE_GUID);
588f1e1ea51SMario Limonciello if (ret) {
589f1e1ea51SMario Limonciello pr_debug("failed to populate string type attributes\n");
590eaa1dcc7SHans de Goede goto err_release_attributes_data;
591f1e1ea51SMario Limonciello }
592f1e1ea51SMario Limonciello
593f1e1ea51SMario Limonciello ret = init_bios_attributes(PO, DELL_WMI_BIOS_PASSOBJ_ATTRIBUTE_GUID);
594f1e1ea51SMario Limonciello if (ret) {
595f1e1ea51SMario Limonciello pr_debug("failed to populate pass object type attributes\n");
596eaa1dcc7SHans de Goede goto err_release_attributes_data;
597f1e1ea51SMario Limonciello }
598f1e1ea51SMario Limonciello
599f1e1ea51SMario Limonciello return 0;
600f1e1ea51SMario Limonciello
601eaa1dcc7SHans de Goede err_release_attributes_data:
602f1e1ea51SMario Limonciello release_attributes_data();
603f1e1ea51SMario Limonciello
604eaa1dcc7SHans de Goede err_destroy_classdev:
6058a1c379cSMark Pearson device_destroy(fw_attr_class, MKDEV(0, 0));
606f1e1ea51SMario Limonciello
607eaa1dcc7SHans de Goede err_unregister_class:
6088a1c379cSMark Pearson fw_attributes_class_put();
609f1e1ea51SMario Limonciello
610eaa1dcc7SHans de Goede err_exit_bios_attr_pass_interface:
611f1e1ea51SMario Limonciello exit_bios_attr_pass_interface();
612f1e1ea51SMario Limonciello
613eaa1dcc7SHans de Goede err_exit_bios_attr_set_interface:
614f1e1ea51SMario Limonciello exit_bios_attr_set_interface();
615f1e1ea51SMario Limonciello
616f1e1ea51SMario Limonciello return ret;
617f1e1ea51SMario Limonciello }
618f1e1ea51SMario Limonciello
sysman_exit(void)619f1e1ea51SMario Limonciello static void __exit sysman_exit(void)
620f1e1ea51SMario Limonciello {
621f1e1ea51SMario Limonciello release_attributes_data();
6228a1c379cSMark Pearson device_destroy(fw_attr_class, MKDEV(0, 0));
6238a1c379cSMark Pearson fw_attributes_class_put();
624f1e1ea51SMario Limonciello exit_bios_attr_set_interface();
625f1e1ea51SMario Limonciello exit_bios_attr_pass_interface();
626f1e1ea51SMario Limonciello }
627f1e1ea51SMario Limonciello
628f1e1ea51SMario Limonciello module_init(sysman_init);
629f1e1ea51SMario Limonciello module_exit(sysman_exit);
630f1e1ea51SMario Limonciello
6311ea602e4SMario Limonciello MODULE_AUTHOR("Mario Limonciello <mario.limonciello@outlook.com>");
632f1e1ea51SMario Limonciello MODULE_AUTHOR("Prasanth Ksr <prasanth.ksr@dell.com>");
633f1e1ea51SMario Limonciello MODULE_AUTHOR("Divya Bharathi <divya.bharathi@dell.com>");
634f1e1ea51SMario Limonciello MODULE_DESCRIPTION("Dell platform setting control interface");
635f1e1ea51SMario Limonciello MODULE_LICENSE("GPL");
636