1 // SPDX-License-Identifier: GPL-2.0-only
3 * WMI methods for use with dell-smbios
5 * Copyright (c) 2017 Dell Inc.
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/list.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/uaccess.h>
14 #include <linux/wmi.h>
15 #include "dell-smbios.h"
16 #include "dell-wmi-descriptor.h"
18 static DEFINE_MUTEX(call_mutex
);
19 static DEFINE_MUTEX(list_mutex
);
20 static int wmi_supported
;
22 struct misc_bios_flags_structure
{
23 struct dmi_header header
;
26 #define FLAG_HAS_ACPI_WMI 0x02
28 #define DELL_WMI_SMBIOS_GUID "A80593CE-A997-11DA-B012-B622A1EF5492"
30 struct wmi_smbios_priv
{
31 struct dell_wmi_smbios_buffer
*buf
;
32 struct list_head list
;
33 struct wmi_device
*wdev
;
37 static LIST_HEAD(wmi_list
);
39 static inline struct wmi_smbios_priv
*get_first_smbios_priv(void)
41 return list_first_entry_or_null(&wmi_list
,
42 struct wmi_smbios_priv
,
46 static int run_smbios_call(struct wmi_device
*wdev
)
48 struct acpi_buffer output
= {ACPI_ALLOCATE_BUFFER
, NULL
};
49 struct wmi_smbios_priv
*priv
;
50 struct acpi_buffer input
;
51 union acpi_object
*obj
;
54 priv
= dev_get_drvdata(&wdev
->dev
);
55 input
.length
= priv
->req_buf_size
- sizeof(u64
);
56 input
.pointer
= &priv
->buf
->std
;
58 dev_dbg(&wdev
->dev
, "evaluating: %u/%u [%x,%x,%x,%x]\n",
59 priv
->buf
->std
.cmd_class
, priv
->buf
->std
.cmd_select
,
60 priv
->buf
->std
.input
[0], priv
->buf
->std
.input
[1],
61 priv
->buf
->std
.input
[2], priv
->buf
->std
.input
[3]);
63 status
= wmidev_evaluate_method(wdev
, 0, 1, &input
, &output
);
64 if (ACPI_FAILURE(status
))
66 obj
= (union acpi_object
*)output
.pointer
;
67 if (obj
->type
!= ACPI_TYPE_BUFFER
) {
68 dev_dbg(&wdev
->dev
, "received type: %d\n", obj
->type
);
69 if (obj
->type
== ACPI_TYPE_INTEGER
)
70 dev_dbg(&wdev
->dev
, "SMBIOS call failed: %llu\n",
74 memcpy(&priv
->buf
->std
, obj
->buffer
.pointer
, obj
->buffer
.length
);
75 dev_dbg(&wdev
->dev
, "result: [%08x,%08x,%08x,%08x]\n",
76 priv
->buf
->std
.output
[0], priv
->buf
->std
.output
[1],
77 priv
->buf
->std
.output
[2], priv
->buf
->std
.output
[3]);
78 kfree(output
.pointer
);
83 static int dell_smbios_wmi_call(struct calling_interface_buffer
*buffer
)
85 struct wmi_smbios_priv
*priv
;
90 mutex_lock(&call_mutex
);
91 priv
= get_first_smbios_priv();
97 size
= sizeof(struct calling_interface_buffer
);
98 difference
= priv
->req_buf_size
- sizeof(u64
) - size
;
100 memset(&priv
->buf
->ext
, 0, difference
);
101 memcpy(&priv
->buf
->std
, buffer
, size
);
102 ret
= run_smbios_call(priv
->wdev
);
103 memcpy(buffer
, &priv
->buf
->std
, size
);
105 mutex_unlock(&call_mutex
);
110 static long dell_smbios_wmi_filter(struct wmi_device
*wdev
, unsigned int cmd
,
111 struct wmi_ioctl_buffer
*arg
)
113 struct wmi_smbios_priv
*priv
;
117 case DELL_WMI_SMBIOS_CMD
:
118 mutex_lock(&call_mutex
);
119 priv
= dev_get_drvdata(&wdev
->dev
);
122 goto fail_smbios_cmd
;
124 memcpy(priv
->buf
, arg
, priv
->req_buf_size
);
125 if (dell_smbios_call_filter(&wdev
->dev
, &priv
->buf
->std
)) {
126 dev_err(&wdev
->dev
, "Invalid call %d/%d:%8x\n",
127 priv
->buf
->std
.cmd_class
,
128 priv
->buf
->std
.cmd_select
,
129 priv
->buf
->std
.input
[0]);
131 goto fail_smbios_cmd
;
133 ret
= run_smbios_call(priv
->wdev
);
135 goto fail_smbios_cmd
;
136 memcpy(arg
, priv
->buf
, priv
->req_buf_size
);
138 mutex_unlock(&call_mutex
);
146 static int dell_smbios_wmi_probe(struct wmi_device
*wdev
, const void *context
)
148 struct wmi_driver
*wdriver
=
149 container_of(wdev
->dev
.driver
, struct wmi_driver
, driver
);
150 struct wmi_smbios_priv
*priv
;
155 ret
= dell_wmi_get_descriptor_valid();
159 priv
= devm_kzalloc(&wdev
->dev
, sizeof(struct wmi_smbios_priv
),
164 /* WMI buffer size will be either 4k or 32k depending on machine */
165 if (!dell_wmi_get_size(&priv
->req_buf_size
))
166 return -EPROBE_DEFER
;
168 /* some SMBIOS calls fail unless BIOS contains hotfix */
169 if (!dell_wmi_get_hotfix(&hotfix
))
170 return -EPROBE_DEFER
;
173 "WMI SMBIOS userspace interface not supported(%u), try upgrading to a newer BIOS\n",
175 wdriver
->filter_callback
= NULL
;
178 /* add in the length object we will use internally with ioctl */
179 priv
->req_buf_size
+= sizeof(u64
);
180 ret
= set_required_buffer_size(wdev
, priv
->req_buf_size
);
184 count
= get_order(priv
->req_buf_size
);
185 priv
->buf
= (void *)__get_free_pages(GFP_KERNEL
, count
);
189 /* ID is used by dell-smbios to set priority of drivers */
191 ret
= dell_smbios_register_device(&wdev
->dev
, &dell_smbios_wmi_call
);
196 dev_set_drvdata(&wdev
->dev
, priv
);
197 mutex_lock(&list_mutex
);
198 list_add_tail(&priv
->list
, &wmi_list
);
199 mutex_unlock(&list_mutex
);
204 free_pages((unsigned long)priv
->buf
, count
);
208 static int dell_smbios_wmi_remove(struct wmi_device
*wdev
)
210 struct wmi_smbios_priv
*priv
= dev_get_drvdata(&wdev
->dev
);
213 mutex_lock(&call_mutex
);
214 mutex_lock(&list_mutex
);
215 list_del(&priv
->list
);
216 mutex_unlock(&list_mutex
);
217 dell_smbios_unregister_device(&wdev
->dev
);
218 count
= get_order(priv
->req_buf_size
);
219 free_pages((unsigned long)priv
->buf
, count
);
220 mutex_unlock(&call_mutex
);
224 static const struct wmi_device_id dell_smbios_wmi_id_table
[] = {
225 { .guid_string
= DELL_WMI_SMBIOS_GUID
},
229 static void parse_b1_table(const struct dmi_header
*dm
)
231 struct misc_bios_flags_structure
*flags
=
232 container_of(dm
, struct misc_bios_flags_structure
, header
);
234 /* 4 bytes header, 8 bytes flags */
237 if (dm
->handle
!= 0xb100)
239 if ((flags
->flags0
& FLAG_HAS_ACPI_WMI
))
243 static void find_b1(const struct dmi_header
*dm
, void *dummy
)
246 case 0xb1: /* misc bios flags */
252 static struct wmi_driver dell_smbios_wmi_driver
= {
254 .name
= "dell-smbios",
256 .probe
= dell_smbios_wmi_probe
,
257 .remove
= dell_smbios_wmi_remove
,
258 .id_table
= dell_smbios_wmi_id_table
,
259 .filter_callback
= dell_smbios_wmi_filter
,
262 int init_dell_smbios_wmi(void)
264 dmi_walk(find_b1
, NULL
);
269 return wmi_driver_register(&dell_smbios_wmi_driver
);
272 void exit_dell_smbios_wmi(void)
274 wmi_driver_unregister(&dell_smbios_wmi_driver
);
277 MODULE_DEVICE_TABLE(wmi
, dell_smbios_wmi_id_table
);