blob: bcb41c1c7f528d32b8cf1bcd2ce105d43fb5e3a3 [file] [log] [blame]
Carlos Corbachobff431e2008-02-05 02:17:04 +00001/*
2 * ACPI-WMI mapping driver
3 *
4 * Copyright (C) 2007-2008 Carlos Corbacho <carlos@strangeworlds.co.uk>
5 *
6 * GUID parsing code from ldm.c is:
7 * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
8 * Copyright (c) 2001-2007 Anton Altaparmakov
9 * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
10 *
Darren Hart (VMware)2c9c5662017-06-06 16:40:56 -070011 * WMI bus infrastructure by Andrew Lutomirski and Darren Hart:
12 * Copyright (C) 2015 Andrew Lutomirski
13 * Copyright (C) 2017 VMware, Inc. All Rights Reserved.
14 *
Carlos Corbachobff431e2008-02-05 02:17:04 +000015 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or (at
20 * your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
30 *
31 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32 */
33
Dmitry Torokhov8e075142010-08-26 00:15:19 -070034#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
Carlos Corbachobff431e2008-02-05 02:17:04 +000036#include <linux/acpi.h>
Mario Limonciellob60ee4e2017-09-26 13:50:03 -050037#include <linux/device.h>
38#include <linux/init.h>
39#include <linux/kernel.h>
40#include <linux/list.h>
Paul Gortmaker7c52d552011-05-27 12:33:10 -040041#include <linux/module.h>
Andy Lutomirski9599ed92015-11-27 11:56:02 -080042#include <linux/platform_device.h>
Mario Limonciellob60ee4e2017-09-26 13:50:03 -050043#include <linux/slab.h>
44#include <linux/types.h>
Andy Shevchenko538d7eb2016-05-20 17:01:30 -070045#include <linux/uuid.h>
Mario Limonciellob60ee4e2017-09-26 13:50:03 -050046#include <linux/wmi.h>
Carlos Corbachobff431e2008-02-05 02:17:04 +000047
48ACPI_MODULE_NAME("wmi");
49MODULE_AUTHOR("Carlos Corbacho");
50MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
51MODULE_LICENSE("GPL");
52
Dmitry Torokhov762e1a22010-08-26 00:15:14 -070053static LIST_HEAD(wmi_block_list);
Carlos Corbachobff431e2008-02-05 02:17:04 +000054
55struct guid_block {
56 char guid[16];
57 union {
58 char object_id[2];
59 struct {
60 unsigned char notify_id;
61 unsigned char reserved;
62 };
63 };
64 u8 instance_count;
65 u8 flags;
66};
67
68struct wmi_block {
Andy Lutomirski844af952015-11-24 19:49:23 -080069 struct wmi_device dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +000070 struct list_head list;
71 struct guid_block gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -080072 struct acpi_device *acpi_device;
Carlos Corbachobff431e2008-02-05 02:17:04 +000073 wmi_notify_handler handler;
74 void *handler_data;
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -080075
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -070076 bool read_takes_no_args;
Carlos Corbachobff431e2008-02-05 02:17:04 +000077};
78
Carlos Corbachobff431e2008-02-05 02:17:04 +000079
80/*
81 * If the GUID data block is marked as expensive, we must enable and
82 * explicitily disable data collection.
83 */
84#define ACPI_WMI_EXPENSIVE 0x1
85#define ACPI_WMI_METHOD 0x2 /* GUID is a method */
86#define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
87#define ACPI_WMI_EVENT 0x8 /* GUID is an event */
88
Rusty Russell90ab5ee2012-01-13 09:32:20 +103089static bool debug_event;
Thomas Renningerfc3155b2010-05-03 15:30:15 +020090module_param(debug_event, bool, 0444);
91MODULE_PARM_DESC(debug_event,
92 "Log WMI Events [0/1]");
93
Rusty Russell90ab5ee2012-01-13 09:32:20 +103094static bool debug_dump_wdg;
Thomas Renningera929aae2010-05-03 15:30:17 +020095module_param(debug_dump_wdg, bool, 0444);
96MODULE_PARM_DESC(debug_dump_wdg,
97 "Dump available WMI interfaces [0/1]");
98
Andy Lutomirski9599ed92015-11-27 11:56:02 -080099static int acpi_wmi_remove(struct platform_device *device);
100static int acpi_wmi_probe(struct platform_device *device);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000101
102static const struct acpi_device_id wmi_device_ids[] = {
103 {"PNP0C14", 0},
104 {"pnp0c14", 0},
105 {"", 0},
106};
107MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
108
Andy Lutomirski9599ed92015-11-27 11:56:02 -0800109static struct platform_driver acpi_wmi_driver = {
110 .driver = {
111 .name = "acpi-wmi",
112 .acpi_match_table = wmi_device_ids,
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700113 },
Andy Lutomirski9599ed92015-11-27 11:56:02 -0800114 .probe = acpi_wmi_probe,
115 .remove = acpi_wmi_remove,
Carlos Corbachobff431e2008-02-05 02:17:04 +0000116};
117
118/*
119 * GUID parsing functions
120 */
121
Carlos Corbachobff431e2008-02-05 02:17:04 +0000122static bool find_guid(const char *guid_string, struct wmi_block **out)
123{
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700124 uuid_le guid_input;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000125 struct wmi_block *wblock;
126 struct guid_block *block;
127 struct list_head *p;
128
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700129 if (uuid_le_to_bin(guid_string, &guid_input))
130 return false;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000131
Dmitry Torokhov762e1a22010-08-26 00:15:14 -0700132 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000133 wblock = list_entry(p, struct wmi_block, list);
134 block = &wblock->gblock;
135
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700136 if (memcmp(block->guid, &guid_input, 16) == 0) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000137 if (out)
138 *out = wblock;
Joe Perches097c27f2015-03-30 10:43:20 -0700139 return true;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000140 }
141 }
Joe Perches097c27f2015-03-30 10:43:20 -0700142 return false;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000143}
144
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800145static int get_subobj_info(acpi_handle handle, const char *pathname,
146 struct acpi_device_info **info)
147{
148 struct acpi_device_info *dummy_info, **info_ptr;
149 acpi_handle subobj_handle;
150 acpi_status status;
151
152 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
153 if (status == AE_NOT_FOUND)
154 return -ENOENT;
155 else if (ACPI_FAILURE(status))
156 return -EIO;
157
158 info_ptr = info ? info : &dummy_info;
159 status = acpi_get_object_info(subobj_handle, info_ptr);
160 if (ACPI_FAILURE(status))
161 return -EIO;
162
163 if (!info)
164 kfree(dummy_info);
165
166 return 0;
167}
168
Matthew Garretta66bfa72008-10-08 21:40:32 +0100169static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
170{
171 struct guid_block *block = NULL;
172 char method[5];
Matthew Garretta66bfa72008-10-08 21:40:32 +0100173 acpi_status status;
174 acpi_handle handle;
175
176 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800177 handle = wblock->acpi_device->handle;
Matthew Garretta66bfa72008-10-08 21:40:32 +0100178
Matthew Garretta66bfa72008-10-08 21:40:32 +0100179 snprintf(method, 5, "WE%02X", block->notify_id);
Zhang Rui8122ab662013-09-03 08:31:57 +0800180 status = acpi_execute_simple_method(handle, method, enable);
Matthew Garretta66bfa72008-10-08 21:40:32 +0100181
182 if (status != AE_OK && status != AE_NOT_FOUND)
183 return status;
184 else
185 return AE_OK;
186}
187
Carlos Corbachobff431e2008-02-05 02:17:04 +0000188/*
189 * Exported WMI functions
190 */
191/**
192 * wmi_evaluate_method - Evaluate a WMI method
193 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
194 * @instance: Instance index
195 * @method_id: Method ID to call
196 * &in: Buffer containing input for the method call
197 * &out: Empty buffer to return the method results
198 *
199 * Call an ACPI-WMI method
200 */
201acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
202u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
203{
Mario Limonciello722c8562017-11-01 14:25:23 -0500204 struct wmi_block *wblock = NULL;
205
206 if (!find_guid(guid_string, &wblock))
207 return AE_ERROR;
208 return wmidev_evaluate_method(&wblock->dev, instance, method_id,
209 in, out);
210}
211EXPORT_SYMBOL_GPL(wmi_evaluate_method);
212
213/**
214 * wmidev_evaluate_method - Evaluate a WMI method
215 * @wdev: A wmi bus device from a driver
216 * @instance: Instance index
217 * @method_id: Method ID to call
218 * &in: Buffer containing input for the method call
219 * &out: Empty buffer to return the method results
220 *
221 * Call an ACPI-WMI method
222 */
223acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance,
224 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
225{
Carlos Corbachobff431e2008-02-05 02:17:04 +0000226 struct guid_block *block = NULL;
227 struct wmi_block *wblock = NULL;
228 acpi_handle handle;
229 acpi_status status;
230 struct acpi_object_list input;
231 union acpi_object params[3];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700232 char method[5] = "WM";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000233
Mario Limonciello722c8562017-11-01 14:25:23 -0500234 wblock = container_of(wdev, struct wmi_block, dev);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000235 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800236 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000237
Al Viroe6bafba2008-02-13 04:03:25 +0000238 if (!(block->flags & ACPI_WMI_METHOD))
Carlos Corbachobff431e2008-02-05 02:17:04 +0000239 return AE_BAD_DATA;
240
Pali Rohár6afa1e22017-08-12 09:44:18 +0200241 if (block->instance_count <= instance)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000242 return AE_BAD_PARAMETER;
243
244 input.count = 2;
245 input.pointer = params;
246 params[0].type = ACPI_TYPE_INTEGER;
247 params[0].integer.value = instance;
248 params[1].type = ACPI_TYPE_INTEGER;
249 params[1].integer.value = method_id;
250
251 if (in) {
252 input.count = 3;
253
254 if (block->flags & ACPI_WMI_STRING) {
255 params[2].type = ACPI_TYPE_STRING;
256 } else {
257 params[2].type = ACPI_TYPE_BUFFER;
258 }
259 params[2].buffer.length = in->length;
260 params[2].buffer.pointer = in->pointer;
261 }
262
263 strncat(method, block->object_id, 2);
264
265 status = acpi_evaluate_object(handle, method, &input, out);
266
267 return status;
268}
Mario Limonciello722c8562017-11-01 14:25:23 -0500269EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000270
Andy Lutomirski56a37022015-11-25 18:19:26 -0800271static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
272 struct acpi_buffer *out)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000273{
274 struct guid_block *block = NULL;
Zhang Rui54f14c22013-09-03 08:32:07 +0800275 acpi_handle handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000276 acpi_status status, wc_status = AE_ERROR;
Zhang Rui8122ab662013-09-03 08:31:57 +0800277 struct acpi_object_list input;
278 union acpi_object wq_params[1];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700279 char method[5];
280 char wc_method[5] = "WC";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000281
Andy Lutomirski56a37022015-11-25 18:19:26 -0800282 if (!out)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000283 return AE_BAD_PARAMETER;
284
Carlos Corbachobff431e2008-02-05 02:17:04 +0000285 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800286 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000287
Pali Rohár6afa1e22017-08-12 09:44:18 +0200288 if (block->instance_count <= instance)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000289 return AE_BAD_PARAMETER;
290
291 /* Check GUID is a data block */
292 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800293 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000294
295 input.count = 1;
296 input.pointer = wq_params;
297 wq_params[0].type = ACPI_TYPE_INTEGER;
298 wq_params[0].integer.value = instance;
299
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800300 if (instance == 0 && wblock->read_takes_no_args)
301 input.count = 0;
302
Carlos Corbachobff431e2008-02-05 02:17:04 +0000303 /*
304 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
305 * enable collection.
306 */
307 if (block->flags & ACPI_WMI_EXPENSIVE) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000308 strncat(wc_method, block->object_id, 2);
309
310 /*
311 * Some GUIDs break the specification by declaring themselves
312 * expensive, but have no corresponding WCxx method. So we
313 * should not fail if this happens.
314 */
Zhang Rui54f14c22013-09-03 08:32:07 +0800315 if (acpi_has_method(handle, wc_method))
Zhang Rui8122ab662013-09-03 08:31:57 +0800316 wc_status = acpi_execute_simple_method(handle,
317 wc_method, 1);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000318 }
319
320 strcpy(method, "WQ");
321 strncat(method, block->object_id, 2);
322
Carlos Corbachodab36ad2008-08-02 17:28:45 +0100323 status = acpi_evaluate_object(handle, method, &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000324
325 /*
326 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
327 * the WQxx method failed - we should disable collection anyway.
328 */
Carlos Corbachoa527f2d2008-02-24 13:34:34 +0000329 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
Zhang Rui8122ab662013-09-03 08:31:57 +0800330 status = acpi_execute_simple_method(handle, wc_method, 0);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000331 }
332
333 return status;
334}
Andy Lutomirski56a37022015-11-25 18:19:26 -0800335
336/**
337 * wmi_query_block - Return contents of a WMI block (deprecated)
338 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
339 * @instance: Instance index
340 * &out: Empty buffer to return the contents of the data block to
341 *
342 * Return the contents of an ACPI-WMI data block to a buffer
343 */
344acpi_status wmi_query_block(const char *guid_string, u8 instance,
345 struct acpi_buffer *out)
346{
347 struct wmi_block *wblock;
348
349 if (!guid_string)
350 return AE_BAD_PARAMETER;
351
352 if (!find_guid(guid_string, &wblock))
353 return AE_ERROR;
354
355 return __query_block(wblock, instance, out);
356}
Carlos Corbachobff431e2008-02-05 02:17:04 +0000357EXPORT_SYMBOL_GPL(wmi_query_block);
358
Andy Lutomirski56a37022015-11-25 18:19:26 -0800359union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
360{
361 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
362 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
363
364 if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
365 return NULL;
366
367 return (union acpi_object *)out.pointer;
368}
369EXPORT_SYMBOL_GPL(wmidev_block_query);
370
Carlos Corbachobff431e2008-02-05 02:17:04 +0000371/**
372 * wmi_set_block - Write to a WMI block
373 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
374 * @instance: Instance index
375 * &in: Buffer containing new values for the data block
376 *
377 * Write the contents of the input buffer to an ACPI-WMI data block
378 */
379acpi_status wmi_set_block(const char *guid_string, u8 instance,
Andy Lutomirski56a37022015-11-25 18:19:26 -0800380 const struct acpi_buffer *in)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000381{
382 struct guid_block *block = NULL;
383 struct wmi_block *wblock = NULL;
384 acpi_handle handle;
385 struct acpi_object_list input;
386 union acpi_object params[2];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700387 char method[5] = "WS";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000388
389 if (!guid_string || !in)
390 return AE_BAD_DATA;
391
392 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800393 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000394
395 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800396 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000397
Pali Rohár6afa1e22017-08-12 09:44:18 +0200398 if (block->instance_count <= instance)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000399 return AE_BAD_PARAMETER;
400
401 /* Check GUID is a data block */
402 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800403 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000404
405 input.count = 2;
406 input.pointer = params;
407 params[0].type = ACPI_TYPE_INTEGER;
408 params[0].integer.value = instance;
409
410 if (block->flags & ACPI_WMI_STRING) {
411 params[1].type = ACPI_TYPE_STRING;
412 } else {
413 params[1].type = ACPI_TYPE_BUFFER;
414 }
415 params[1].buffer.length = in->length;
416 params[1].buffer.pointer = in->pointer;
417
418 strncat(method, block->object_id, 2);
419
420 return acpi_evaluate_object(handle, method, &input, NULL);
421}
422EXPORT_SYMBOL_GPL(wmi_set_block);
423
Dmitry Torokhov37830662010-08-26 00:15:09 -0700424static void wmi_dump_wdg(const struct guid_block *g)
Thomas Renningera929aae2010-05-03 15:30:17 +0200425{
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200426 pr_info("%pUL:\n", g->guid);
Pali Rohárcd3921f2017-06-10 12:57:11 +0200427 if (g->flags & ACPI_WMI_EVENT)
428 pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
429 else
430 pr_info("\tobject_id: %2pE\n", g->object_id);
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700431 pr_info("\tinstance_count: %d\n", g->instance_count);
Joe Perchesdd8e9082011-03-29 15:21:53 -0700432 pr_info("\tflags: %#x", g->flags);
Thomas Renningera929aae2010-05-03 15:30:17 +0200433 if (g->flags) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200434 if (g->flags & ACPI_WMI_EXPENSIVE)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700435 pr_cont(" ACPI_WMI_EXPENSIVE");
Thomas Renningera929aae2010-05-03 15:30:17 +0200436 if (g->flags & ACPI_WMI_METHOD)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700437 pr_cont(" ACPI_WMI_METHOD");
Thomas Renningera929aae2010-05-03 15:30:17 +0200438 if (g->flags & ACPI_WMI_STRING)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700439 pr_cont(" ACPI_WMI_STRING");
Thomas Renningera929aae2010-05-03 15:30:17 +0200440 if (g->flags & ACPI_WMI_EVENT)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700441 pr_cont(" ACPI_WMI_EVENT");
Thomas Renningera929aae2010-05-03 15:30:17 +0200442 }
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700443 pr_cont("\n");
Thomas Renningera929aae2010-05-03 15:30:17 +0200444
445}
446
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200447static void wmi_notify_debug(u32 value, void *context)
448{
449 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
450 union acpi_object *obj;
Axel Lin14926162010-06-28 09:30:45 +0800451 acpi_status status;
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200452
Axel Lin14926162010-06-28 09:30:45 +0800453 status = wmi_get_event_data(value, &response);
454 if (status != AE_OK) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700455 pr_info("bad event status 0x%x\n", status);
Axel Lin14926162010-06-28 09:30:45 +0800456 return;
457 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200458
459 obj = (union acpi_object *)response.pointer;
460
461 if (!obj)
462 return;
463
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700464 pr_info("DEBUG Event ");
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200465 switch(obj->type) {
466 case ACPI_TYPE_BUFFER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700467 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200468 break;
469 case ACPI_TYPE_STRING:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700470 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200471 break;
472 case ACPI_TYPE_INTEGER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700473 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200474 break;
475 case ACPI_TYPE_PACKAGE:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700476 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200477 break;
478 default:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700479 pr_cont("object type 0x%X\n", obj->type);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200480 }
Axel Lin14926162010-06-28 09:30:45 +0800481 kfree(obj);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200482}
483
Carlos Corbachobff431e2008-02-05 02:17:04 +0000484/**
485 * wmi_install_notify_handler - Register handler for WMI events
486 * @handler: Function to handle notifications
487 * @data: Data to be returned to handler when event is fired
488 *
489 * Register a handler for events sent to the ACPI-WMI mapper device.
490 */
491acpi_status wmi_install_notify_handler(const char *guid,
492wmi_notify_handler handler, void *data)
493{
494 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000495 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700496 uuid_le guid_input;
Colin King58f64252010-11-19 15:40:02 +0000497 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000498
499 if (!guid || !handler)
500 return AE_BAD_PARAMETER;
501
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700502 if (uuid_le_to_bin(guid, &guid_input))
503 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000504
Colin King58f64252010-11-19 15:40:02 +0000505 list_for_each(p, &wmi_block_list) {
506 acpi_status wmi_status;
507 block = list_entry(p, struct wmi_block, list);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000508
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700509 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000510 if (block->handler &&
511 block->handler != wmi_notify_debug)
512 return AE_ALREADY_ACQUIRED;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000513
Colin King58f64252010-11-19 15:40:02 +0000514 block->handler = handler;
515 block->handler_data = data;
516
517 wmi_status = wmi_method_enable(block, 1);
518 if ((wmi_status != AE_OK) ||
519 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
520 status = wmi_status;
521 }
522 }
Matthew Garretta66bfa72008-10-08 21:40:32 +0100523
524 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000525}
526EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
527
528/**
529 * wmi_uninstall_notify_handler - Unregister handler for WMI events
530 *
531 * Unregister handler for events sent to the ACPI-WMI mapper device.
532 */
533acpi_status wmi_remove_notify_handler(const char *guid)
534{
535 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000536 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700537 uuid_le guid_input;
Colin King58f64252010-11-19 15:40:02 +0000538 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000539
540 if (!guid)
541 return AE_BAD_PARAMETER;
542
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700543 if (uuid_le_to_bin(guid, &guid_input))
544 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000545
Colin King58f64252010-11-19 15:40:02 +0000546 list_for_each(p, &wmi_block_list) {
547 acpi_status wmi_status;
548 block = list_entry(p, struct wmi_block, list);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000549
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700550 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000551 if (!block->handler ||
552 block->handler == wmi_notify_debug)
553 return AE_NULL_ENTRY;
554
555 if (debug_event) {
556 block->handler = wmi_notify_debug;
557 status = AE_OK;
558 } else {
559 wmi_status = wmi_method_enable(block, 0);
560 block->handler = NULL;
561 block->handler_data = NULL;
562 if ((wmi_status != AE_OK) ||
563 ((wmi_status == AE_OK) &&
564 (status == AE_NOT_EXIST)))
565 status = wmi_status;
566 }
567 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200568 }
Colin King58f64252010-11-19 15:40:02 +0000569
Matthew Garretta66bfa72008-10-08 21:40:32 +0100570 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000571}
572EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
573
574/**
575 * wmi_get_event_data - Get WMI data associated with an event
576 *
Anisse Astier3e9b9882009-12-04 10:10:09 +0100577 * @event: Event to find
578 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
Carlos Corbachobff431e2008-02-05 02:17:04 +0000579 *
580 * Returns extra data associated with an event in WMI.
581 */
582acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
583{
584 struct acpi_object_list input;
585 union acpi_object params[1];
586 struct guid_block *gblock;
587 struct wmi_block *wblock;
588 struct list_head *p;
589
590 input.count = 1;
591 input.pointer = params;
592 params[0].type = ACPI_TYPE_INTEGER;
593 params[0].integer.value = event;
594
Dmitry Torokhov762e1a22010-08-26 00:15:14 -0700595 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000596 wblock = list_entry(p, struct wmi_block, list);
597 gblock = &wblock->gblock;
598
599 if ((gblock->flags & ACPI_WMI_EVENT) &&
600 (gblock->notify_id == event))
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800601 return acpi_evaluate_object(wblock->acpi_device->handle,
602 "_WED", &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000603 }
604
605 return AE_NOT_FOUND;
606}
607EXPORT_SYMBOL_GPL(wmi_get_event_data);
608
609/**
610 * wmi_has_guid - Check if a GUID is available
611 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
612 *
613 * Check if a given GUID is defined by _WDG
614 */
615bool wmi_has_guid(const char *guid_string)
616{
617 return find_guid(guid_string, NULL);
618}
619EXPORT_SYMBOL_GPL(wmi_has_guid);
620
Andy Lutomirski844af952015-11-24 19:49:23 -0800621static struct wmi_block *dev_to_wblock(struct device *dev)
622{
623 return container_of(dev, struct wmi_block, dev.dev);
624}
625
626static struct wmi_device *dev_to_wdev(struct device *dev)
627{
628 return container_of(dev, struct wmi_device, dev);
629}
630
Carlos Corbachobff431e2008-02-05 02:17:04 +0000631/*
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500632 * sysfs interface
633 */
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700634static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500635 char *buf)
636{
Andy Lutomirski844af952015-11-24 19:49:23 -0800637 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500638
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200639 return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500640}
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700641static DEVICE_ATTR_RO(modalias);
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700642
Andy Lutomirski844af952015-11-24 19:49:23 -0800643static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
644 char *buf)
645{
646 struct wmi_block *wblock = dev_to_wblock(dev);
647
648 return sprintf(buf, "%pUL\n", wblock->gblock.guid);
649}
650static DEVICE_ATTR_RO(guid);
651
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800652static ssize_t instance_count_show(struct device *dev,
653 struct device_attribute *attr, char *buf)
654{
655 struct wmi_block *wblock = dev_to_wblock(dev);
656
657 return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
658}
659static DEVICE_ATTR_RO(instance_count);
660
661static ssize_t expensive_show(struct device *dev,
662 struct device_attribute *attr, char *buf)
663{
664 struct wmi_block *wblock = dev_to_wblock(dev);
665
666 return sprintf(buf, "%d\n",
667 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
668}
669static DEVICE_ATTR_RO(expensive);
670
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700671static struct attribute *wmi_attrs[] = {
672 &dev_attr_modalias.attr,
Andy Lutomirski844af952015-11-24 19:49:23 -0800673 &dev_attr_guid.attr,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800674 &dev_attr_instance_count.attr,
675 &dev_attr_expensive.attr,
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700676 NULL,
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700677};
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700678ATTRIBUTE_GROUPS(wmi);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500679
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800680static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
681 char *buf)
682{
683 struct wmi_block *wblock = dev_to_wblock(dev);
684
685 return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
686}
687static DEVICE_ATTR_RO(notify_id);
688
689static struct attribute *wmi_event_attrs[] = {
690 &dev_attr_notify_id.attr,
691 NULL,
692};
693ATTRIBUTE_GROUPS(wmi_event);
694
695static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
696 char *buf)
697{
698 struct wmi_block *wblock = dev_to_wblock(dev);
699
700 return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
701 wblock->gblock.object_id[1]);
702}
703static DEVICE_ATTR_RO(object_id);
704
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700705static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
706 char *buf)
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800707{
708 struct wmi_device *wdev = dev_to_wdev(dev);
709
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700710 return sprintf(buf, "%d\n", (int)wdev->setable);
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800711}
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700712static DEVICE_ATTR_RO(setable);
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800713
714static struct attribute *wmi_data_attrs[] = {
715 &dev_attr_object_id.attr,
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700716 &dev_attr_setable.attr,
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800717 NULL,
718};
719ATTRIBUTE_GROUPS(wmi_data);
720
721static struct attribute *wmi_method_attrs[] = {
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800722 &dev_attr_object_id.attr,
723 NULL,
724};
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800725ATTRIBUTE_GROUPS(wmi_method);
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800726
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500727static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
728{
Andy Lutomirski844af952015-11-24 19:49:23 -0800729 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500730
Andy Lutomirski844af952015-11-24 19:49:23 -0800731 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500732 return -ENOMEM;
733
Andy Lutomirski844af952015-11-24 19:49:23 -0800734 if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500735 return -ENOMEM;
736
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500737 return 0;
738}
739
Andy Lutomirski844af952015-11-24 19:49:23 -0800740static void wmi_dev_release(struct device *dev)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500741{
Andy Lutomirski844af952015-11-24 19:49:23 -0800742 struct wmi_block *wblock = dev_to_wblock(dev);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700743
Andy Lutomirski844af952015-11-24 19:49:23 -0800744 kfree(wblock);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500745}
746
Andy Lutomirski844af952015-11-24 19:49:23 -0800747static int wmi_dev_match(struct device *dev, struct device_driver *driver)
748{
749 struct wmi_driver *wmi_driver =
750 container_of(driver, struct wmi_driver, driver);
751 struct wmi_block *wblock = dev_to_wblock(dev);
752 const struct wmi_device_id *id = wmi_driver->id_table;
753
754 while (id->guid_string) {
755 uuid_le driver_guid;
756
757 if (WARN_ON(uuid_le_to_bin(id->guid_string, &driver_guid)))
758 continue;
759 if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
760 return 1;
761
762 id++;
763 }
764
765 return 0;
766}
767
768static int wmi_dev_probe(struct device *dev)
769{
770 struct wmi_block *wblock = dev_to_wblock(dev);
771 struct wmi_driver *wdriver =
772 container_of(dev->driver, struct wmi_driver, driver);
773 int ret = 0;
774
775 if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
776 dev_warn(dev, "failed to enable device -- probing anyway\n");
777
778 if (wdriver->probe) {
779 ret = wdriver->probe(dev_to_wdev(dev));
780 if (ret != 0 && ACPI_FAILURE(wmi_method_enable(wblock, 0)))
781 dev_warn(dev, "failed to disable device\n");
782 }
783
784 return ret;
785}
786
787static int wmi_dev_remove(struct device *dev)
788{
789 struct wmi_block *wblock = dev_to_wblock(dev);
790 struct wmi_driver *wdriver =
791 container_of(dev->driver, struct wmi_driver, driver);
792 int ret = 0;
793
794 if (wdriver->remove)
795 ret = wdriver->remove(dev_to_wdev(dev));
796
797 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
798 dev_warn(dev, "failed to disable device\n");
799
800 return ret;
801}
802
803static struct class wmi_bus_class = {
804 .name = "wmi_bus",
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500805};
806
Andy Lutomirski844af952015-11-24 19:49:23 -0800807static struct bus_type wmi_bus_type = {
808 .name = "wmi",
809 .dev_groups = wmi_groups,
810 .match = wmi_dev_match,
811 .uevent = wmi_dev_uevent,
812 .probe = wmi_dev_probe,
813 .remove = wmi_dev_remove,
814};
815
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800816static struct device_type wmi_type_event = {
817 .name = "event",
818 .groups = wmi_event_groups,
819 .release = wmi_dev_release,
820};
821
822static struct device_type wmi_type_method = {
823 .name = "method",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800824 .groups = wmi_method_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800825 .release = wmi_dev_release,
826};
827
828static struct device_type wmi_type_data = {
829 .name = "data",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800830 .groups = wmi_data_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800831 .release = wmi_dev_release,
832};
833
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700834static int wmi_create_device(struct device *wmi_bus_dev,
Andy Lutomirski844af952015-11-24 19:49:23 -0800835 const struct guid_block *gblock,
Andy Lutomirski7f5809b2015-11-19 14:44:46 -0800836 struct wmi_block *wblock,
837 struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500838{
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700839 struct acpi_device_info *info;
840 char method[5];
841 int result;
842
843 if (gblock->flags & ACPI_WMI_EVENT) {
844 wblock->dev.dev.type = &wmi_type_event;
845 goto out_init;
846 }
847
848 if (gblock->flags & ACPI_WMI_METHOD) {
849 wblock->dev.dev.type = &wmi_type_method;
850 goto out_init;
851 }
852
853 /*
854 * Data Block Query Control Method (WQxx by convention) is
855 * required per the WMI documentation. If it is not present,
856 * we ignore this data block.
857 */
858 strcpy(method, "WQ");
859 strncat(method, wblock->gblock.object_id, 2);
860 result = get_subobj_info(device->handle, method, &info);
861
862 if (result) {
863 dev_warn(wmi_bus_dev,
864 "%s data block query control method not found",
865 method);
866 return result;
867 }
868
869 wblock->dev.dev.type = &wmi_type_data;
870
871 /*
872 * The Microsoft documentation specifically states:
873 *
874 * Data blocks registered with only a single instance
875 * can ignore the parameter.
876 *
877 * ACPICA will get mad at us if we call the method with the wrong number
878 * of arguments, so check what our method expects. (On some Dell
879 * laptops, WQxx may not be a method at all.)
880 */
881 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
882 wblock->read_takes_no_args = true;
883
884 kfree(info);
885
886 strcpy(method, "WS");
887 strncat(method, wblock->gblock.object_id, 2);
888 result = get_subobj_info(device->handle, method, NULL);
889
890 if (result == 0)
891 wblock->dev.setable = true;
892
893 out_init:
Andy Lutomirski844af952015-11-24 19:49:23 -0800894 wblock->dev.dev.bus = &wmi_bus_type;
895 wblock->dev.dev.parent = wmi_bus_dev;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700896
Andy Lutomirski844af952015-11-24 19:49:23 -0800897 dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700898
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700899 device_initialize(&wblock->dev.dev);
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700900
901 return 0;
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500902}
903
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800904static void wmi_free_devices(struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500905{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700906 struct wmi_block *wblock, *next;
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500907
908 /* Delete devices for all the GUIDs */
Dmitry Torokhov023b9562011-09-07 15:00:02 -0700909 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800910 if (wblock->acpi_device == device) {
911 list_del(&wblock->list);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700912 device_unregister(&wblock->dev.dev);
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800913 }
Dmitry Torokhov023b9562011-09-07 15:00:02 -0700914 }
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500915}
916
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800917static bool guid_already_parsed(struct acpi_device *device,
918 const u8 *guid)
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000919{
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000920 struct wmi_block *wblock;
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000921
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800922 list_for_each_entry(wblock, &wmi_block_list, list) {
923 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
924 /*
925 * Because we historically didn't track the relationship
926 * between GUIDs and ACPI nodes, we don't know whether
927 * we need to suppress GUIDs that are unique on a
928 * given node but duplicated across nodes.
929 */
930 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
931 guid, dev_name(&wblock->acpi_device->dev));
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000932 return true;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800933 }
934 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700935
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000936 return false;
937}
938
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500939/*
Carlos Corbachobff431e2008-02-05 02:17:04 +0000940 * Parse the _WDG method for the GUID data blocks
941 */
Andy Lutomirski844af952015-11-24 19:49:23 -0800942static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000943{
944 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
Dmitry Torokhov37830662010-08-26 00:15:09 -0700945 const struct guid_block *gblock;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700946 struct wmi_block *wblock, *next;
947 union acpi_object *obj;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000948 acpi_status status;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700949 int retval = 0;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000950 u32 i, total;
951
Andy Lutomirski7f5809b2015-11-19 14:44:46 -0800952 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000953 if (ACPI_FAILURE(status))
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700954 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000955
956 obj = (union acpi_object *) out.pointer;
Dmitry Torokhov3d2c63e2010-08-26 00:15:03 -0700957 if (!obj)
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700958 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000959
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -0700960 if (obj->type != ACPI_TYPE_BUFFER) {
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700961 retval = -ENXIO;
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -0700962 goto out_free_pointer;
963 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000964
Dmitry Torokhov37830662010-08-26 00:15:09 -0700965 gblock = (const struct guid_block *)obj->buffer.pointer;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000966 total = obj->buffer.length / sizeof(struct guid_block);
967
Carlos Corbachobff431e2008-02-05 02:17:04 +0000968 for (i = 0; i < total; i++) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200969 if (debug_dump_wdg)
970 wmi_dump_wdg(&gblock[i]);
971
Andy Lutomirskia1c31bcd2015-11-25 08:24:42 -0800972 /*
973 * Some WMI devices, like those for nVidia hooks, have a
974 * duplicate GUID. It's not clear what we should do in this
975 * case yet, so for now, we'll just ignore the duplicate
976 * for device creation.
977 */
978 if (guid_already_parsed(device, gblock[i].guid))
979 continue;
980
Colin King58f64252010-11-19 15:40:02 +0000981 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700982 if (!wblock) {
983 retval = -ENOMEM;
984 break;
985 }
Colin King58f64252010-11-19 15:40:02 +0000986
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800987 wblock->acpi_device = device;
Colin King58f64252010-11-19 15:40:02 +0000988 wblock->gblock = gblock[i];
989
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700990 retval = wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
991 if (retval) {
992 kfree(wblock);
993 continue;
994 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000995
Colin King58f64252010-11-19 15:40:02 +0000996 list_add_tail(&wblock->list, &wmi_block_list);
997
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200998 if (debug_event) {
999 wblock->handler = wmi_notify_debug;
Dmitry Torokhov2d5ab552010-08-26 00:14:48 -07001000 wmi_method_enable(wblock, 1);
Thomas Renningerfc3155b2010-05-03 15:30:15 +02001001 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001002 }
1003
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001004 /*
1005 * Now that all of the devices are created, add them to the
1006 * device tree and probe subdrivers.
1007 */
1008 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1009 if (wblock->acpi_device != device)
1010 continue;
1011
1012 retval = device_add(&wblock->dev.dev);
1013 if (retval) {
1014 dev_err(wmi_bus_dev, "failed to register %pULL\n",
1015 wblock->gblock.guid);
1016 if (debug_event)
1017 wmi_method_enable(wblock, 0);
1018 list_del(&wblock->list);
1019 put_device(&wblock->dev.dev);
1020 }
1021 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001022
Axel Lina5167c52010-06-03 11:45:45 +08001023out_free_pointer:
1024 kfree(out.pointer);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001025 return retval;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001026}
1027
1028/*
1029 * WMI can have EmbeddedControl access regions. In which case, we just want to
1030 * hand these off to the EC driver.
1031 */
1032static acpi_status
1033acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
Lin Ming439913f2010-01-28 10:53:19 +08001034 u32 bits, u64 *value,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001035 void *handler_context, void *region_context)
1036{
1037 int result = 0, i = 0;
1038 u8 temp = 0;
1039
1040 if ((address > 0xFF) || !value)
1041 return AE_BAD_PARAMETER;
1042
1043 if (function != ACPI_READ && function != ACPI_WRITE)
1044 return AE_BAD_PARAMETER;
1045
1046 if (bits != 8)
1047 return AE_BAD_PARAMETER;
1048
1049 if (function == ACPI_READ) {
1050 result = ec_read(address, &temp);
Lin Ming439913f2010-01-28 10:53:19 +08001051 (*value) |= ((u64)temp) << i;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001052 } else {
1053 temp = 0xff & ((*value) >> i);
1054 result = ec_write(address, temp);
1055 }
1056
1057 switch (result) {
1058 case -EINVAL:
1059 return AE_BAD_PARAMETER;
1060 break;
1061 case -ENODEV:
1062 return AE_NOT_FOUND;
1063 break;
1064 case -ETIME:
1065 return AE_TIME;
1066 break;
1067 default:
1068 return AE_OK;
1069 }
1070}
1071
Andy Lutomirski1686f542015-11-25 17:33:25 -08001072static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1073 void *context)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001074{
1075 struct guid_block *block;
1076 struct wmi_block *wblock;
1077 struct list_head *p;
Andy Lutomirski1686f542015-11-25 17:33:25 -08001078 bool found_it = false;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001079
Dmitry Torokhov762e1a22010-08-26 00:15:14 -07001080 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +00001081 wblock = list_entry(p, struct wmi_block, list);
1082 block = &wblock->gblock;
1083
Andy Lutomirski1686f542015-11-25 17:33:25 -08001084 if (wblock->acpi_device->handle == handle &&
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001085 (block->flags & ACPI_WMI_EVENT) &&
Andy Lutomirski1686f542015-11-25 17:33:25 -08001086 (block->notify_id == event))
1087 {
1088 found_it = true;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001089 break;
1090 }
1091 }
Andy Lutomirski1686f542015-11-25 17:33:25 -08001092
1093 if (!found_it)
1094 return;
1095
1096 /* If a driver is bound, then notify the driver. */
1097 if (wblock->dev.dev.driver) {
1098 struct wmi_driver *driver;
1099 struct acpi_object_list input;
1100 union acpi_object params[1];
1101 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1102 acpi_status status;
1103
1104 driver = container_of(wblock->dev.dev.driver,
1105 struct wmi_driver, driver);
1106
1107 input.count = 1;
1108 input.pointer = params;
1109 params[0].type = ACPI_TYPE_INTEGER;
1110 params[0].integer.value = event;
1111
1112 status = acpi_evaluate_object(wblock->acpi_device->handle,
1113 "_WED", &input, &evdata);
1114 if (ACPI_FAILURE(status)) {
1115 dev_warn(&wblock->dev.dev,
1116 "failed to get event data\n");
1117 return;
1118 }
1119
1120 if (driver->notify)
1121 driver->notify(&wblock->dev,
1122 (union acpi_object *)evdata.pointer);
1123
1124 kfree(evdata.pointer);
1125 } else if (wblock->handler) {
1126 /* Legacy handler */
1127 wblock->handler(event, wblock->handler_data);
1128 }
1129
1130 if (debug_event) {
1131 pr_info("DEBUG Event GUID: %pUL\n",
1132 wblock->gblock.guid);
1133 }
1134
1135 acpi_bus_generate_netlink_event(
1136 wblock->acpi_device->pnp.device_class,
1137 dev_name(&wblock->dev.dev),
1138 event, 0);
1139
Carlos Corbachobff431e2008-02-05 02:17:04 +00001140}
1141
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001142static int acpi_wmi_remove(struct platform_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001143{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001144 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1145
1146 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001147 acpi_wmi_notify_handler);
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001148 acpi_remove_address_space_handler(acpi_device->handle,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001149 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001150 wmi_free_devices(acpi_device);
Mario Limonciello7b11e892017-09-26 13:50:05 -05001151 device_destroy(&wmi_bus_class, MKDEV(0, 0));
Carlos Corbachobff431e2008-02-05 02:17:04 +00001152
1153 return 0;
1154}
1155
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001156static int acpi_wmi_probe(struct platform_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001157{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001158 struct acpi_device *acpi_device;
Andy Lutomirski844af952015-11-24 19:49:23 -08001159 struct device *wmi_bus_dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001160 acpi_status status;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001161 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001162
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001163 acpi_device = ACPI_COMPANION(&device->dev);
1164 if (!acpi_device) {
1165 dev_err(&device->dev, "ACPI companion is missing\n");
1166 return -ENODEV;
1167 }
1168
1169 status = acpi_install_address_space_handler(acpi_device->handle,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001170 ACPI_ADR_SPACE_EC,
1171 &acpi_wmi_ec_space_handler,
1172 NULL, NULL);
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001173 if (ACPI_FAILURE(status)) {
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001174 dev_err(&device->dev, "Error installing EC region handler\n");
Carlos Corbachobff431e2008-02-05 02:17:04 +00001175 return -ENODEV;
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001176 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001177
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001178 status = acpi_install_notify_handler(acpi_device->handle,
1179 ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001180 acpi_wmi_notify_handler,
1181 NULL);
1182 if (ACPI_FAILURE(status)) {
1183 dev_err(&device->dev, "Error installing notify handler\n");
1184 error = -ENODEV;
1185 goto err_remove_ec_handler;
1186 }
1187
Andy Lutomirski844af952015-11-24 19:49:23 -08001188 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1189 NULL, "wmi_bus-%s", dev_name(&device->dev));
1190 if (IS_ERR(wmi_bus_dev)) {
1191 error = PTR_ERR(wmi_bus_dev);
Andy Lutomirski1686f542015-11-25 17:33:25 -08001192 goto err_remove_notify_handler;
Andy Lutomirski844af952015-11-24 19:49:23 -08001193 }
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001194 dev_set_drvdata(&device->dev, wmi_bus_dev);
Andy Lutomirski844af952015-11-24 19:49:23 -08001195
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001196 error = parse_wdg(wmi_bus_dev, acpi_device);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001197 if (error) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001198 pr_err("Failed to parse WDG method\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001199 goto err_remove_busdev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001200 }
1201
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001202 return 0;
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001203
Andy Lutomirski844af952015-11-24 19:49:23 -08001204err_remove_busdev:
Mario Limonciello7b11e892017-09-26 13:50:05 -05001205 device_destroy(&wmi_bus_class, MKDEV(0, 0));
Andy Lutomirski844af952015-11-24 19:49:23 -08001206
Andy Lutomirski1686f542015-11-25 17:33:25 -08001207err_remove_notify_handler:
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001208 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001209 acpi_wmi_notify_handler);
1210
1211err_remove_ec_handler:
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001212 acpi_remove_address_space_handler(acpi_device->handle,
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001213 ACPI_ADR_SPACE_EC,
1214 &acpi_wmi_ec_space_handler);
1215
1216 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001217}
1218
Andy Lutomirski844af952015-11-24 19:49:23 -08001219int __must_check __wmi_driver_register(struct wmi_driver *driver,
1220 struct module *owner)
1221{
1222 driver->driver.owner = owner;
1223 driver->driver.bus = &wmi_bus_type;
1224
1225 return driver_register(&driver->driver);
1226}
1227EXPORT_SYMBOL(__wmi_driver_register);
1228
1229void wmi_driver_unregister(struct wmi_driver *driver)
1230{
1231 driver_unregister(&driver->driver);
1232}
1233EXPORT_SYMBOL(wmi_driver_unregister);
1234
Carlos Corbachobff431e2008-02-05 02:17:04 +00001235static int __init acpi_wmi_init(void)
1236{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001237 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001238
1239 if (acpi_disabled)
1240 return -ENODEV;
1241
Andy Lutomirski844af952015-11-24 19:49:23 -08001242 error = class_register(&wmi_bus_class);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001243 if (error)
1244 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001245
Andy Lutomirski844af952015-11-24 19:49:23 -08001246 error = bus_register(&wmi_bus_type);
1247 if (error)
1248 goto err_unreg_class;
1249
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001250 error = platform_driver_register(&acpi_wmi_driver);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001251 if (error) {
1252 pr_err("Error loading mapper\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001253 goto err_unreg_bus;
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001254 }
1255
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001256 return 0;
Andy Lutomirski844af952015-11-24 19:49:23 -08001257
Andy Lutomirski844af952015-11-24 19:49:23 -08001258err_unreg_bus:
1259 bus_unregister(&wmi_bus_type);
1260
Alexey Khoroshilov97277712017-07-22 00:48:06 +03001261err_unreg_class:
1262 class_unregister(&wmi_bus_class);
1263
Andy Lutomirski844af952015-11-24 19:49:23 -08001264 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001265}
1266
1267static void __exit acpi_wmi_exit(void)
1268{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001269 platform_driver_unregister(&acpi_wmi_driver);
Andy Lutomirski844af952015-11-24 19:49:23 -08001270 bus_unregister(&wmi_bus_type);
Mario Limonciello303d1fc2017-09-26 13:50:04 -05001271 class_unregister(&wmi_bus_class);
Carlos Corbachobff431e2008-02-05 02:17:04 +00001272}
1273
1274subsys_initcall(acpi_wmi_init);
1275module_exit(acpi_wmi_exit);