blob: 37f6651d1bf778824171368beb5ccb4f145fa65b [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 *
11 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or (at
16 * your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 *
27 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28 */
29
Dmitry Torokhov8e075142010-08-26 00:15:19 -070030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
Carlos Corbachobff431e2008-02-05 02:17:04 +000032#include <linux/kernel.h>
33#include <linux/init.h>
34#include <linux/types.h>
Matthew Garrett1caab3c2009-11-04 14:17:53 -050035#include <linux/device.h>
Carlos Corbachobff431e2008-02-05 02:17:04 +000036#include <linux/list.h>
37#include <linux/acpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Paul Gortmaker7c52d552011-05-27 12:33:10 -040039#include <linux/module.h>
Andy Lutomirski9599ed92015-11-27 11:56:02 -080040#include <linux/platform_device.h>
Andy Lutomirski844af952015-11-24 19:49:23 -080041#include <linux/wmi.h>
Andy Shevchenko538d7eb2016-05-20 17:01:30 -070042#include <linux/uuid.h>
Carlos Corbachobff431e2008-02-05 02:17:04 +000043
44ACPI_MODULE_NAME("wmi");
45MODULE_AUTHOR("Carlos Corbacho");
46MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
47MODULE_LICENSE("GPL");
48
Dmitry Torokhov762e1a22010-08-26 00:15:14 -070049static LIST_HEAD(wmi_block_list);
Carlos Corbachobff431e2008-02-05 02:17:04 +000050
51struct guid_block {
52 char guid[16];
53 union {
54 char object_id[2];
55 struct {
56 unsigned char notify_id;
57 unsigned char reserved;
58 };
59 };
60 u8 instance_count;
61 u8 flags;
62};
63
64struct wmi_block {
Andy Lutomirski844af952015-11-24 19:49:23 -080065 struct wmi_device dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +000066 struct list_head list;
67 struct guid_block gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -080068 struct acpi_device *acpi_device;
Carlos Corbachobff431e2008-02-05 02:17:04 +000069 wmi_notify_handler handler;
70 void *handler_data;
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -080071
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -070072 bool read_takes_no_args;
Carlos Corbachobff431e2008-02-05 02:17:04 +000073};
74
Carlos Corbachobff431e2008-02-05 02:17:04 +000075
76/*
77 * If the GUID data block is marked as expensive, we must enable and
78 * explicitily disable data collection.
79 */
80#define ACPI_WMI_EXPENSIVE 0x1
81#define ACPI_WMI_METHOD 0x2 /* GUID is a method */
82#define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
83#define ACPI_WMI_EVENT 0x8 /* GUID is an event */
84
Rusty Russell90ab5ee2012-01-13 09:32:20 +103085static bool debug_event;
Thomas Renningerfc3155b2010-05-03 15:30:15 +020086module_param(debug_event, bool, 0444);
87MODULE_PARM_DESC(debug_event,
88 "Log WMI Events [0/1]");
89
Rusty Russell90ab5ee2012-01-13 09:32:20 +103090static bool debug_dump_wdg;
Thomas Renningera929aae2010-05-03 15:30:17 +020091module_param(debug_dump_wdg, bool, 0444);
92MODULE_PARM_DESC(debug_dump_wdg,
93 "Dump available WMI interfaces [0/1]");
94
Andy Lutomirski9599ed92015-11-27 11:56:02 -080095static int acpi_wmi_remove(struct platform_device *device);
96static int acpi_wmi_probe(struct platform_device *device);
Carlos Corbachobff431e2008-02-05 02:17:04 +000097
98static const struct acpi_device_id wmi_device_ids[] = {
99 {"PNP0C14", 0},
100 {"pnp0c14", 0},
101 {"", 0},
102};
103MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
104
Andy Lutomirski9599ed92015-11-27 11:56:02 -0800105static struct platform_driver acpi_wmi_driver = {
106 .driver = {
107 .name = "acpi-wmi",
108 .acpi_match_table = wmi_device_ids,
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700109 },
Andy Lutomirski9599ed92015-11-27 11:56:02 -0800110 .probe = acpi_wmi_probe,
111 .remove = acpi_wmi_remove,
Carlos Corbachobff431e2008-02-05 02:17:04 +0000112};
113
114/*
115 * GUID parsing functions
116 */
117
Carlos Corbachobff431e2008-02-05 02:17:04 +0000118static bool find_guid(const char *guid_string, struct wmi_block **out)
119{
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700120 uuid_le guid_input;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000121 struct wmi_block *wblock;
122 struct guid_block *block;
123 struct list_head *p;
124
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700125 if (uuid_le_to_bin(guid_string, &guid_input))
126 return false;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000127
Dmitry Torokhov762e1a22010-08-26 00:15:14 -0700128 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000129 wblock = list_entry(p, struct wmi_block, list);
130 block = &wblock->gblock;
131
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700132 if (memcmp(block->guid, &guid_input, 16) == 0) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000133 if (out)
134 *out = wblock;
Joe Perches097c27f2015-03-30 10:43:20 -0700135 return true;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000136 }
137 }
Joe Perches097c27f2015-03-30 10:43:20 -0700138 return false;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000139}
140
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800141static int get_subobj_info(acpi_handle handle, const char *pathname,
142 struct acpi_device_info **info)
143{
144 struct acpi_device_info *dummy_info, **info_ptr;
145 acpi_handle subobj_handle;
146 acpi_status status;
147
148 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
149 if (status == AE_NOT_FOUND)
150 return -ENOENT;
151 else if (ACPI_FAILURE(status))
152 return -EIO;
153
154 info_ptr = info ? info : &dummy_info;
155 status = acpi_get_object_info(subobj_handle, info_ptr);
156 if (ACPI_FAILURE(status))
157 return -EIO;
158
159 if (!info)
160 kfree(dummy_info);
161
162 return 0;
163}
164
Matthew Garretta66bfa72008-10-08 21:40:32 +0100165static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
166{
167 struct guid_block *block = NULL;
168 char method[5];
Matthew Garretta66bfa72008-10-08 21:40:32 +0100169 acpi_status status;
170 acpi_handle handle;
171
172 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800173 handle = wblock->acpi_device->handle;
Matthew Garretta66bfa72008-10-08 21:40:32 +0100174
Matthew Garretta66bfa72008-10-08 21:40:32 +0100175 snprintf(method, 5, "WE%02X", block->notify_id);
Zhang Rui8122ab662013-09-03 08:31:57 +0800176 status = acpi_execute_simple_method(handle, method, enable);
Matthew Garretta66bfa72008-10-08 21:40:32 +0100177
178 if (status != AE_OK && status != AE_NOT_FOUND)
179 return status;
180 else
181 return AE_OK;
182}
183
Carlos Corbachobff431e2008-02-05 02:17:04 +0000184/*
185 * Exported WMI functions
186 */
187/**
188 * wmi_evaluate_method - Evaluate a WMI method
189 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
190 * @instance: Instance index
191 * @method_id: Method ID to call
192 * &in: Buffer containing input for the method call
193 * &out: Empty buffer to return the method results
194 *
195 * Call an ACPI-WMI method
196 */
197acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
198u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
199{
200 struct guid_block *block = NULL;
201 struct wmi_block *wblock = NULL;
202 acpi_handle handle;
203 acpi_status status;
204 struct acpi_object_list input;
205 union acpi_object params[3];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700206 char method[5] = "WM";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000207
208 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800209 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000210
211 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800212 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000213
Al Viroe6bafba2008-02-13 04:03:25 +0000214 if (!(block->flags & ACPI_WMI_METHOD))
Carlos Corbachobff431e2008-02-05 02:17:04 +0000215 return AE_BAD_DATA;
216
217 if (block->instance_count < instance)
218 return AE_BAD_PARAMETER;
219
220 input.count = 2;
221 input.pointer = params;
222 params[0].type = ACPI_TYPE_INTEGER;
223 params[0].integer.value = instance;
224 params[1].type = ACPI_TYPE_INTEGER;
225 params[1].integer.value = method_id;
226
227 if (in) {
228 input.count = 3;
229
230 if (block->flags & ACPI_WMI_STRING) {
231 params[2].type = ACPI_TYPE_STRING;
232 } else {
233 params[2].type = ACPI_TYPE_BUFFER;
234 }
235 params[2].buffer.length = in->length;
236 params[2].buffer.pointer = in->pointer;
237 }
238
239 strncat(method, block->object_id, 2);
240
241 status = acpi_evaluate_object(handle, method, &input, out);
242
243 return status;
244}
245EXPORT_SYMBOL_GPL(wmi_evaluate_method);
246
Andy Lutomirski56a37022015-11-25 18:19:26 -0800247static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
248 struct acpi_buffer *out)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000249{
250 struct guid_block *block = NULL;
Zhang Rui54f14c22013-09-03 08:32:07 +0800251 acpi_handle handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000252 acpi_status status, wc_status = AE_ERROR;
Zhang Rui8122ab662013-09-03 08:31:57 +0800253 struct acpi_object_list input;
254 union acpi_object wq_params[1];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700255 char method[5];
256 char wc_method[5] = "WC";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000257
Andy Lutomirski56a37022015-11-25 18:19:26 -0800258 if (!out)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000259 return AE_BAD_PARAMETER;
260
Carlos Corbachobff431e2008-02-05 02:17:04 +0000261 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800262 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000263
264 if (block->instance_count < instance)
265 return AE_BAD_PARAMETER;
266
267 /* Check GUID is a data block */
268 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800269 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000270
271 input.count = 1;
272 input.pointer = wq_params;
273 wq_params[0].type = ACPI_TYPE_INTEGER;
274 wq_params[0].integer.value = instance;
275
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800276 if (instance == 0 && wblock->read_takes_no_args)
277 input.count = 0;
278
Carlos Corbachobff431e2008-02-05 02:17:04 +0000279 /*
280 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
281 * enable collection.
282 */
283 if (block->flags & ACPI_WMI_EXPENSIVE) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000284 strncat(wc_method, block->object_id, 2);
285
286 /*
287 * Some GUIDs break the specification by declaring themselves
288 * expensive, but have no corresponding WCxx method. So we
289 * should not fail if this happens.
290 */
Zhang Rui54f14c22013-09-03 08:32:07 +0800291 if (acpi_has_method(handle, wc_method))
Zhang Rui8122ab662013-09-03 08:31:57 +0800292 wc_status = acpi_execute_simple_method(handle,
293 wc_method, 1);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000294 }
295
296 strcpy(method, "WQ");
297 strncat(method, block->object_id, 2);
298
Carlos Corbachodab36ad2008-08-02 17:28:45 +0100299 status = acpi_evaluate_object(handle, method, &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000300
301 /*
302 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
303 * the WQxx method failed - we should disable collection anyway.
304 */
Carlos Corbachoa527f2d2008-02-24 13:34:34 +0000305 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
Zhang Rui8122ab662013-09-03 08:31:57 +0800306 status = acpi_execute_simple_method(handle, wc_method, 0);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000307 }
308
309 return status;
310}
Andy Lutomirski56a37022015-11-25 18:19:26 -0800311
312/**
313 * wmi_query_block - Return contents of a WMI block (deprecated)
314 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
315 * @instance: Instance index
316 * &out: Empty buffer to return the contents of the data block to
317 *
318 * Return the contents of an ACPI-WMI data block to a buffer
319 */
320acpi_status wmi_query_block(const char *guid_string, u8 instance,
321 struct acpi_buffer *out)
322{
323 struct wmi_block *wblock;
324
325 if (!guid_string)
326 return AE_BAD_PARAMETER;
327
328 if (!find_guid(guid_string, &wblock))
329 return AE_ERROR;
330
331 return __query_block(wblock, instance, out);
332}
Carlos Corbachobff431e2008-02-05 02:17:04 +0000333EXPORT_SYMBOL_GPL(wmi_query_block);
334
Andy Lutomirski56a37022015-11-25 18:19:26 -0800335union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
336{
337 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
338 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
339
340 if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
341 return NULL;
342
343 return (union acpi_object *)out.pointer;
344}
345EXPORT_SYMBOL_GPL(wmidev_block_query);
346
Andy Lutomirskif6301982015-12-29 22:53:51 -0800347struct wmi_device *wmidev_get_other_guid(struct wmi_device *wdev,
348 const char *guid_string)
349{
350 struct wmi_block *this_wb = container_of(wdev, struct wmi_block, dev);
351 struct wmi_block *other_wb;
352
353 if (!find_guid(guid_string, &other_wb))
354 return NULL;
355
356 if (other_wb->acpi_device != this_wb->acpi_device)
357 return NULL;
358
359 get_device(&other_wb->dev.dev);
360 return &other_wb->dev;
361}
362EXPORT_SYMBOL_GPL(wmidev_get_other_guid);
363
Carlos Corbachobff431e2008-02-05 02:17:04 +0000364/**
365 * wmi_set_block - Write to a WMI block
366 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
367 * @instance: Instance index
368 * &in: Buffer containing new values for the data block
369 *
370 * Write the contents of the input buffer to an ACPI-WMI data block
371 */
372acpi_status wmi_set_block(const char *guid_string, u8 instance,
Andy Lutomirski56a37022015-11-25 18:19:26 -0800373 const struct acpi_buffer *in)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000374{
375 struct guid_block *block = NULL;
376 struct wmi_block *wblock = NULL;
377 acpi_handle handle;
378 struct acpi_object_list input;
379 union acpi_object params[2];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700380 char method[5] = "WS";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000381
382 if (!guid_string || !in)
383 return AE_BAD_DATA;
384
385 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800386 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000387
388 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800389 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000390
391 if (block->instance_count < instance)
392 return AE_BAD_PARAMETER;
393
394 /* Check GUID is a data block */
395 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800396 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000397
398 input.count = 2;
399 input.pointer = params;
400 params[0].type = ACPI_TYPE_INTEGER;
401 params[0].integer.value = instance;
402
403 if (block->flags & ACPI_WMI_STRING) {
404 params[1].type = ACPI_TYPE_STRING;
405 } else {
406 params[1].type = ACPI_TYPE_BUFFER;
407 }
408 params[1].buffer.length = in->length;
409 params[1].buffer.pointer = in->pointer;
410
411 strncat(method, block->object_id, 2);
412
413 return acpi_evaluate_object(handle, method, &input, NULL);
414}
415EXPORT_SYMBOL_GPL(wmi_set_block);
416
Dmitry Torokhov37830662010-08-26 00:15:09 -0700417static void wmi_dump_wdg(const struct guid_block *g)
Thomas Renningera929aae2010-05-03 15:30:17 +0200418{
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200419 pr_info("%pUL:\n", g->guid);
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700420 pr_info("\tobject_id: %c%c\n", g->object_id[0], g->object_id[1]);
421 pr_info("\tnotify_id: %02X\n", g->notify_id);
422 pr_info("\treserved: %02X\n", g->reserved);
423 pr_info("\tinstance_count: %d\n", g->instance_count);
Joe Perchesdd8e9082011-03-29 15:21:53 -0700424 pr_info("\tflags: %#x", g->flags);
Thomas Renningera929aae2010-05-03 15:30:17 +0200425 if (g->flags) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200426 if (g->flags & ACPI_WMI_EXPENSIVE)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700427 pr_cont(" ACPI_WMI_EXPENSIVE");
Thomas Renningera929aae2010-05-03 15:30:17 +0200428 if (g->flags & ACPI_WMI_METHOD)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700429 pr_cont(" ACPI_WMI_METHOD");
Thomas Renningera929aae2010-05-03 15:30:17 +0200430 if (g->flags & ACPI_WMI_STRING)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700431 pr_cont(" ACPI_WMI_STRING");
Thomas Renningera929aae2010-05-03 15:30:17 +0200432 if (g->flags & ACPI_WMI_EVENT)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700433 pr_cont(" ACPI_WMI_EVENT");
Thomas Renningera929aae2010-05-03 15:30:17 +0200434 }
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700435 pr_cont("\n");
Thomas Renningera929aae2010-05-03 15:30:17 +0200436
437}
438
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200439static void wmi_notify_debug(u32 value, void *context)
440{
441 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
442 union acpi_object *obj;
Axel Lin14926162010-06-28 09:30:45 +0800443 acpi_status status;
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200444
Axel Lin14926162010-06-28 09:30:45 +0800445 status = wmi_get_event_data(value, &response);
446 if (status != AE_OK) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700447 pr_info("bad event status 0x%x\n", status);
Axel Lin14926162010-06-28 09:30:45 +0800448 return;
449 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200450
451 obj = (union acpi_object *)response.pointer;
452
453 if (!obj)
454 return;
455
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700456 pr_info("DEBUG Event ");
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200457 switch(obj->type) {
458 case ACPI_TYPE_BUFFER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700459 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200460 break;
461 case ACPI_TYPE_STRING:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700462 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200463 break;
464 case ACPI_TYPE_INTEGER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700465 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200466 break;
467 case ACPI_TYPE_PACKAGE:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700468 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200469 break;
470 default:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700471 pr_cont("object type 0x%X\n", obj->type);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200472 }
Axel Lin14926162010-06-28 09:30:45 +0800473 kfree(obj);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200474}
475
Carlos Corbachobff431e2008-02-05 02:17:04 +0000476/**
477 * wmi_install_notify_handler - Register handler for WMI events
478 * @handler: Function to handle notifications
479 * @data: Data to be returned to handler when event is fired
480 *
481 * Register a handler for events sent to the ACPI-WMI mapper device.
482 */
483acpi_status wmi_install_notify_handler(const char *guid,
484wmi_notify_handler handler, void *data)
485{
486 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000487 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700488 uuid_le guid_input;
Colin King58f64252010-11-19 15:40:02 +0000489 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000490
491 if (!guid || !handler)
492 return AE_BAD_PARAMETER;
493
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700494 if (uuid_le_to_bin(guid, &guid_input))
495 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000496
Colin King58f64252010-11-19 15:40:02 +0000497 list_for_each(p, &wmi_block_list) {
498 acpi_status wmi_status;
499 block = list_entry(p, struct wmi_block, list);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000500
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700501 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000502 if (block->handler &&
503 block->handler != wmi_notify_debug)
504 return AE_ALREADY_ACQUIRED;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000505
Colin King58f64252010-11-19 15:40:02 +0000506 block->handler = handler;
507 block->handler_data = data;
508
509 wmi_status = wmi_method_enable(block, 1);
510 if ((wmi_status != AE_OK) ||
511 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
512 status = wmi_status;
513 }
514 }
Matthew Garretta66bfa72008-10-08 21:40:32 +0100515
516 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000517}
518EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
519
520/**
521 * wmi_uninstall_notify_handler - Unregister handler for WMI events
522 *
523 * Unregister handler for events sent to the ACPI-WMI mapper device.
524 */
525acpi_status wmi_remove_notify_handler(const char *guid)
526{
527 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000528 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700529 uuid_le guid_input;
Colin King58f64252010-11-19 15:40:02 +0000530 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000531
532 if (!guid)
533 return AE_BAD_PARAMETER;
534
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700535 if (uuid_le_to_bin(guid, &guid_input))
536 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000537
Colin King58f64252010-11-19 15:40:02 +0000538 list_for_each(p, &wmi_block_list) {
539 acpi_status wmi_status;
540 block = list_entry(p, struct wmi_block, list);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000541
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700542 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000543 if (!block->handler ||
544 block->handler == wmi_notify_debug)
545 return AE_NULL_ENTRY;
546
547 if (debug_event) {
548 block->handler = wmi_notify_debug;
549 status = AE_OK;
550 } else {
551 wmi_status = wmi_method_enable(block, 0);
552 block->handler = NULL;
553 block->handler_data = NULL;
554 if ((wmi_status != AE_OK) ||
555 ((wmi_status == AE_OK) &&
556 (status == AE_NOT_EXIST)))
557 status = wmi_status;
558 }
559 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200560 }
Colin King58f64252010-11-19 15:40:02 +0000561
Matthew Garretta66bfa72008-10-08 21:40:32 +0100562 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000563}
564EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
565
566/**
567 * wmi_get_event_data - Get WMI data associated with an event
568 *
Anisse Astier3e9b9882009-12-04 10:10:09 +0100569 * @event: Event to find
570 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
Carlos Corbachobff431e2008-02-05 02:17:04 +0000571 *
572 * Returns extra data associated with an event in WMI.
573 */
574acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
575{
576 struct acpi_object_list input;
577 union acpi_object params[1];
578 struct guid_block *gblock;
579 struct wmi_block *wblock;
580 struct list_head *p;
581
582 input.count = 1;
583 input.pointer = params;
584 params[0].type = ACPI_TYPE_INTEGER;
585 params[0].integer.value = event;
586
Dmitry Torokhov762e1a22010-08-26 00:15:14 -0700587 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000588 wblock = list_entry(p, struct wmi_block, list);
589 gblock = &wblock->gblock;
590
591 if ((gblock->flags & ACPI_WMI_EVENT) &&
592 (gblock->notify_id == event))
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800593 return acpi_evaluate_object(wblock->acpi_device->handle,
594 "_WED", &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000595 }
596
597 return AE_NOT_FOUND;
598}
599EXPORT_SYMBOL_GPL(wmi_get_event_data);
600
601/**
602 * wmi_has_guid - Check if a GUID is available
603 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
604 *
605 * Check if a given GUID is defined by _WDG
606 */
607bool wmi_has_guid(const char *guid_string)
608{
609 return find_guid(guid_string, NULL);
610}
611EXPORT_SYMBOL_GPL(wmi_has_guid);
612
Andy Lutomirski844af952015-11-24 19:49:23 -0800613static struct wmi_block *dev_to_wblock(struct device *dev)
614{
615 return container_of(dev, struct wmi_block, dev.dev);
616}
617
618static struct wmi_device *dev_to_wdev(struct device *dev)
619{
620 return container_of(dev, struct wmi_device, dev);
621}
622
Carlos Corbachobff431e2008-02-05 02:17:04 +0000623/*
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500624 * sysfs interface
625 */
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700626static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500627 char *buf)
628{
Andy Lutomirski844af952015-11-24 19:49:23 -0800629 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500630
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200631 return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500632}
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700633static DEVICE_ATTR_RO(modalias);
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700634
Andy Lutomirski844af952015-11-24 19:49:23 -0800635static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
636 char *buf)
637{
638 struct wmi_block *wblock = dev_to_wblock(dev);
639
640 return sprintf(buf, "%pUL\n", wblock->gblock.guid);
641}
642static DEVICE_ATTR_RO(guid);
643
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800644static ssize_t instance_count_show(struct device *dev,
645 struct device_attribute *attr, char *buf)
646{
647 struct wmi_block *wblock = dev_to_wblock(dev);
648
649 return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
650}
651static DEVICE_ATTR_RO(instance_count);
652
653static ssize_t expensive_show(struct device *dev,
654 struct device_attribute *attr, char *buf)
655{
656 struct wmi_block *wblock = dev_to_wblock(dev);
657
658 return sprintf(buf, "%d\n",
659 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
660}
661static DEVICE_ATTR_RO(expensive);
662
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700663static struct attribute *wmi_attrs[] = {
664 &dev_attr_modalias.attr,
Andy Lutomirski844af952015-11-24 19:49:23 -0800665 &dev_attr_guid.attr,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800666 &dev_attr_instance_count.attr,
667 &dev_attr_expensive.attr,
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700668 NULL,
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700669};
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700670ATTRIBUTE_GROUPS(wmi);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500671
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800672static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
673 char *buf)
674{
675 struct wmi_block *wblock = dev_to_wblock(dev);
676
677 return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
678}
679static DEVICE_ATTR_RO(notify_id);
680
681static struct attribute *wmi_event_attrs[] = {
682 &dev_attr_notify_id.attr,
683 NULL,
684};
685ATTRIBUTE_GROUPS(wmi_event);
686
687static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
688 char *buf)
689{
690 struct wmi_block *wblock = dev_to_wblock(dev);
691
692 return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
693 wblock->gblock.object_id[1]);
694}
695static DEVICE_ATTR_RO(object_id);
696
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700697static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
698 char *buf)
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800699{
700 struct wmi_device *wdev = dev_to_wdev(dev);
701
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700702 return sprintf(buf, "%d\n", (int)wdev->setable);
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800703}
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700704static DEVICE_ATTR_RO(setable);
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800705
706static struct attribute *wmi_data_attrs[] = {
707 &dev_attr_object_id.attr,
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700708 &dev_attr_setable.attr,
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800709 NULL,
710};
711ATTRIBUTE_GROUPS(wmi_data);
712
713static struct attribute *wmi_method_attrs[] = {
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800714 &dev_attr_object_id.attr,
715 NULL,
716};
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800717ATTRIBUTE_GROUPS(wmi_method);
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800718
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500719static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
720{
Andy Lutomirski844af952015-11-24 19:49:23 -0800721 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500722
Andy Lutomirski844af952015-11-24 19:49:23 -0800723 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500724 return -ENOMEM;
725
Andy Lutomirski844af952015-11-24 19:49:23 -0800726 if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500727 return -ENOMEM;
728
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500729 return 0;
730}
731
Andy Lutomirski844af952015-11-24 19:49:23 -0800732static void wmi_dev_release(struct device *dev)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500733{
Andy Lutomirski844af952015-11-24 19:49:23 -0800734 struct wmi_block *wblock = dev_to_wblock(dev);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700735
Andy Lutomirski844af952015-11-24 19:49:23 -0800736 kfree(wblock);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500737}
738
Andy Lutomirski844af952015-11-24 19:49:23 -0800739static int wmi_dev_match(struct device *dev, struct device_driver *driver)
740{
741 struct wmi_driver *wmi_driver =
742 container_of(driver, struct wmi_driver, driver);
743 struct wmi_block *wblock = dev_to_wblock(dev);
744 const struct wmi_device_id *id = wmi_driver->id_table;
745
746 while (id->guid_string) {
747 uuid_le driver_guid;
748
749 if (WARN_ON(uuid_le_to_bin(id->guid_string, &driver_guid)))
750 continue;
751 if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
752 return 1;
753
754 id++;
755 }
756
757 return 0;
758}
759
760static int wmi_dev_probe(struct device *dev)
761{
762 struct wmi_block *wblock = dev_to_wblock(dev);
763 struct wmi_driver *wdriver =
764 container_of(dev->driver, struct wmi_driver, driver);
765 int ret = 0;
766
767 if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
768 dev_warn(dev, "failed to enable device -- probing anyway\n");
769
770 if (wdriver->probe) {
771 ret = wdriver->probe(dev_to_wdev(dev));
772 if (ret != 0 && ACPI_FAILURE(wmi_method_enable(wblock, 0)))
773 dev_warn(dev, "failed to disable device\n");
774 }
775
776 return ret;
777}
778
779static int wmi_dev_remove(struct device *dev)
780{
781 struct wmi_block *wblock = dev_to_wblock(dev);
782 struct wmi_driver *wdriver =
783 container_of(dev->driver, struct wmi_driver, driver);
784 int ret = 0;
785
786 if (wdriver->remove)
787 ret = wdriver->remove(dev_to_wdev(dev));
788
789 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
790 dev_warn(dev, "failed to disable device\n");
791
792 return ret;
793}
794
795static struct class wmi_bus_class = {
796 .name = "wmi_bus",
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500797};
798
Andy Lutomirski844af952015-11-24 19:49:23 -0800799static struct bus_type wmi_bus_type = {
800 .name = "wmi",
801 .dev_groups = wmi_groups,
802 .match = wmi_dev_match,
803 .uevent = wmi_dev_uevent,
804 .probe = wmi_dev_probe,
805 .remove = wmi_dev_remove,
806};
807
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800808static struct device_type wmi_type_event = {
809 .name = "event",
810 .groups = wmi_event_groups,
811 .release = wmi_dev_release,
812};
813
814static struct device_type wmi_type_method = {
815 .name = "method",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800816 .groups = wmi_method_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800817 .release = wmi_dev_release,
818};
819
820static struct device_type wmi_type_data = {
821 .name = "data",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800822 .groups = wmi_data_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800823 .release = wmi_dev_release,
824};
825
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700826static int wmi_create_device(struct device *wmi_bus_dev,
Andy Lutomirski844af952015-11-24 19:49:23 -0800827 const struct guid_block *gblock,
Andy Lutomirski7f5809b2015-11-19 14:44:46 -0800828 struct wmi_block *wblock,
829 struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500830{
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700831 struct acpi_device_info *info;
832 char method[5];
833 int result;
834
835 if (gblock->flags & ACPI_WMI_EVENT) {
836 wblock->dev.dev.type = &wmi_type_event;
837 goto out_init;
838 }
839
840 if (gblock->flags & ACPI_WMI_METHOD) {
841 wblock->dev.dev.type = &wmi_type_method;
842 goto out_init;
843 }
844
845 /*
846 * Data Block Query Control Method (WQxx by convention) is
847 * required per the WMI documentation. If it is not present,
848 * we ignore this data block.
849 */
850 strcpy(method, "WQ");
851 strncat(method, wblock->gblock.object_id, 2);
852 result = get_subobj_info(device->handle, method, &info);
853
854 if (result) {
855 dev_warn(wmi_bus_dev,
856 "%s data block query control method not found",
857 method);
858 return result;
859 }
860
861 wblock->dev.dev.type = &wmi_type_data;
862
863 /*
864 * The Microsoft documentation specifically states:
865 *
866 * Data blocks registered with only a single instance
867 * can ignore the parameter.
868 *
869 * ACPICA will get mad at us if we call the method with the wrong number
870 * of arguments, so check what our method expects. (On some Dell
871 * laptops, WQxx may not be a method at all.)
872 */
873 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
874 wblock->read_takes_no_args = true;
875
876 kfree(info);
877
878 strcpy(method, "WS");
879 strncat(method, wblock->gblock.object_id, 2);
880 result = get_subobj_info(device->handle, method, NULL);
881
882 if (result == 0)
883 wblock->dev.setable = true;
884
885 out_init:
Andy Lutomirski844af952015-11-24 19:49:23 -0800886 wblock->dev.dev.bus = &wmi_bus_type;
887 wblock->dev.dev.parent = wmi_bus_dev;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700888
Andy Lutomirski844af952015-11-24 19:49:23 -0800889 dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700890
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700891 device_initialize(&wblock->dev.dev);
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700892
893 return 0;
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500894}
895
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800896static void wmi_free_devices(struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500897{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700898 struct wmi_block *wblock, *next;
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500899
900 /* Delete devices for all the GUIDs */
Dmitry Torokhov023b9562011-09-07 15:00:02 -0700901 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800902 if (wblock->acpi_device == device) {
903 list_del(&wblock->list);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700904 device_unregister(&wblock->dev.dev);
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800905 }
Dmitry Torokhov023b9562011-09-07 15:00:02 -0700906 }
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500907}
908
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800909static bool guid_already_parsed(struct acpi_device *device,
910 const u8 *guid)
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000911{
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000912 struct wmi_block *wblock;
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000913
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800914 list_for_each_entry(wblock, &wmi_block_list, list) {
915 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
916 /*
917 * Because we historically didn't track the relationship
918 * between GUIDs and ACPI nodes, we don't know whether
919 * we need to suppress GUIDs that are unique on a
920 * given node but duplicated across nodes.
921 */
922 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
923 guid, dev_name(&wblock->acpi_device->dev));
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000924 return true;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800925 }
926 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700927
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000928 return false;
929}
930
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500931/*
Carlos Corbachobff431e2008-02-05 02:17:04 +0000932 * Parse the _WDG method for the GUID data blocks
933 */
Andy Lutomirski844af952015-11-24 19:49:23 -0800934static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000935{
936 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
Dmitry Torokhov37830662010-08-26 00:15:09 -0700937 const struct guid_block *gblock;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700938 struct wmi_block *wblock, *next;
939 union acpi_object *obj;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000940 acpi_status status;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700941 int retval = 0;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000942 u32 i, total;
943
Andy Lutomirski7f5809b2015-11-19 14:44:46 -0800944 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000945 if (ACPI_FAILURE(status))
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700946 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000947
948 obj = (union acpi_object *) out.pointer;
Dmitry Torokhov3d2c63e2010-08-26 00:15:03 -0700949 if (!obj)
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700950 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000951
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -0700952 if (obj->type != ACPI_TYPE_BUFFER) {
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700953 retval = -ENXIO;
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -0700954 goto out_free_pointer;
955 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000956
Dmitry Torokhov37830662010-08-26 00:15:09 -0700957 gblock = (const struct guid_block *)obj->buffer.pointer;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000958 total = obj->buffer.length / sizeof(struct guid_block);
959
Carlos Corbachobff431e2008-02-05 02:17:04 +0000960 for (i = 0; i < total; i++) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200961 if (debug_dump_wdg)
962 wmi_dump_wdg(&gblock[i]);
963
Andy Lutomirskia1c31bcd2015-11-25 08:24:42 -0800964 /*
965 * Some WMI devices, like those for nVidia hooks, have a
966 * duplicate GUID. It's not clear what we should do in this
967 * case yet, so for now, we'll just ignore the duplicate
968 * for device creation.
969 */
970 if (guid_already_parsed(device, gblock[i].guid))
971 continue;
972
Colin King58f64252010-11-19 15:40:02 +0000973 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700974 if (!wblock) {
975 retval = -ENOMEM;
976 break;
977 }
Colin King58f64252010-11-19 15:40:02 +0000978
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800979 wblock->acpi_device = device;
Colin King58f64252010-11-19 15:40:02 +0000980 wblock->gblock = gblock[i];
981
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700982 retval = wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
983 if (retval) {
984 kfree(wblock);
985 continue;
986 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000987
Colin King58f64252010-11-19 15:40:02 +0000988 list_add_tail(&wblock->list, &wmi_block_list);
989
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200990 if (debug_event) {
991 wblock->handler = wmi_notify_debug;
Dmitry Torokhov2d5ab552010-08-26 00:14:48 -0700992 wmi_method_enable(wblock, 1);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200993 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000994 }
995
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700996 /*
997 * Now that all of the devices are created, add them to the
998 * device tree and probe subdrivers.
999 */
1000 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1001 if (wblock->acpi_device != device)
1002 continue;
1003
1004 retval = device_add(&wblock->dev.dev);
1005 if (retval) {
1006 dev_err(wmi_bus_dev, "failed to register %pULL\n",
1007 wblock->gblock.guid);
1008 if (debug_event)
1009 wmi_method_enable(wblock, 0);
1010 list_del(&wblock->list);
1011 put_device(&wblock->dev.dev);
1012 }
1013 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001014
Axel Lina5167c52010-06-03 11:45:45 +08001015out_free_pointer:
1016 kfree(out.pointer);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001017 return retval;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001018}
1019
1020/*
1021 * WMI can have EmbeddedControl access regions. In which case, we just want to
1022 * hand these off to the EC driver.
1023 */
1024static acpi_status
1025acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
Lin Ming439913f2010-01-28 10:53:19 +08001026 u32 bits, u64 *value,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001027 void *handler_context, void *region_context)
1028{
1029 int result = 0, i = 0;
1030 u8 temp = 0;
1031
1032 if ((address > 0xFF) || !value)
1033 return AE_BAD_PARAMETER;
1034
1035 if (function != ACPI_READ && function != ACPI_WRITE)
1036 return AE_BAD_PARAMETER;
1037
1038 if (bits != 8)
1039 return AE_BAD_PARAMETER;
1040
1041 if (function == ACPI_READ) {
1042 result = ec_read(address, &temp);
Lin Ming439913f2010-01-28 10:53:19 +08001043 (*value) |= ((u64)temp) << i;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001044 } else {
1045 temp = 0xff & ((*value) >> i);
1046 result = ec_write(address, temp);
1047 }
1048
1049 switch (result) {
1050 case -EINVAL:
1051 return AE_BAD_PARAMETER;
1052 break;
1053 case -ENODEV:
1054 return AE_NOT_FOUND;
1055 break;
1056 case -ETIME:
1057 return AE_TIME;
1058 break;
1059 default:
1060 return AE_OK;
1061 }
1062}
1063
Andy Lutomirski1686f542015-11-25 17:33:25 -08001064static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1065 void *context)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001066{
1067 struct guid_block *block;
1068 struct wmi_block *wblock;
1069 struct list_head *p;
Andy Lutomirski1686f542015-11-25 17:33:25 -08001070 bool found_it = false;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001071
Dmitry Torokhov762e1a22010-08-26 00:15:14 -07001072 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +00001073 wblock = list_entry(p, struct wmi_block, list);
1074 block = &wblock->gblock;
1075
Andy Lutomirski1686f542015-11-25 17:33:25 -08001076 if (wblock->acpi_device->handle == handle &&
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001077 (block->flags & ACPI_WMI_EVENT) &&
Andy Lutomirski1686f542015-11-25 17:33:25 -08001078 (block->notify_id == event))
1079 {
1080 found_it = true;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001081 break;
1082 }
1083 }
Andy Lutomirski1686f542015-11-25 17:33:25 -08001084
1085 if (!found_it)
1086 return;
1087
1088 /* If a driver is bound, then notify the driver. */
1089 if (wblock->dev.dev.driver) {
1090 struct wmi_driver *driver;
1091 struct acpi_object_list input;
1092 union acpi_object params[1];
1093 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1094 acpi_status status;
1095
1096 driver = container_of(wblock->dev.dev.driver,
1097 struct wmi_driver, driver);
1098
1099 input.count = 1;
1100 input.pointer = params;
1101 params[0].type = ACPI_TYPE_INTEGER;
1102 params[0].integer.value = event;
1103
1104 status = acpi_evaluate_object(wblock->acpi_device->handle,
1105 "_WED", &input, &evdata);
1106 if (ACPI_FAILURE(status)) {
1107 dev_warn(&wblock->dev.dev,
1108 "failed to get event data\n");
1109 return;
1110 }
1111
1112 if (driver->notify)
1113 driver->notify(&wblock->dev,
1114 (union acpi_object *)evdata.pointer);
1115
1116 kfree(evdata.pointer);
1117 } else if (wblock->handler) {
1118 /* Legacy handler */
1119 wblock->handler(event, wblock->handler_data);
1120 }
1121
1122 if (debug_event) {
1123 pr_info("DEBUG Event GUID: %pUL\n",
1124 wblock->gblock.guid);
1125 }
1126
1127 acpi_bus_generate_netlink_event(
1128 wblock->acpi_device->pnp.device_class,
1129 dev_name(&wblock->dev.dev),
1130 event, 0);
1131
Carlos Corbachobff431e2008-02-05 02:17:04 +00001132}
1133
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001134static int acpi_wmi_remove(struct platform_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001135{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001136 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1137
1138 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001139 acpi_wmi_notify_handler);
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001140 acpi_remove_address_space_handler(acpi_device->handle,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001141 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001142 wmi_free_devices(acpi_device);
1143 device_unregister((struct device *)dev_get_drvdata(&device->dev));
Carlos Corbachobff431e2008-02-05 02:17:04 +00001144
1145 return 0;
1146}
1147
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001148static int acpi_wmi_probe(struct platform_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001149{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001150 struct acpi_device *acpi_device;
Andy Lutomirski844af952015-11-24 19:49:23 -08001151 struct device *wmi_bus_dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001152 acpi_status status;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001153 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001154
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001155 acpi_device = ACPI_COMPANION(&device->dev);
1156 if (!acpi_device) {
1157 dev_err(&device->dev, "ACPI companion is missing\n");
1158 return -ENODEV;
1159 }
1160
1161 status = acpi_install_address_space_handler(acpi_device->handle,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001162 ACPI_ADR_SPACE_EC,
1163 &acpi_wmi_ec_space_handler,
1164 NULL, NULL);
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001165 if (ACPI_FAILURE(status)) {
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001166 dev_err(&device->dev, "Error installing EC region handler\n");
Carlos Corbachobff431e2008-02-05 02:17:04 +00001167 return -ENODEV;
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001168 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001169
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001170 status = acpi_install_notify_handler(acpi_device->handle,
1171 ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001172 acpi_wmi_notify_handler,
1173 NULL);
1174 if (ACPI_FAILURE(status)) {
1175 dev_err(&device->dev, "Error installing notify handler\n");
1176 error = -ENODEV;
1177 goto err_remove_ec_handler;
1178 }
1179
Andy Lutomirski844af952015-11-24 19:49:23 -08001180 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1181 NULL, "wmi_bus-%s", dev_name(&device->dev));
1182 if (IS_ERR(wmi_bus_dev)) {
1183 error = PTR_ERR(wmi_bus_dev);
Andy Lutomirski1686f542015-11-25 17:33:25 -08001184 goto err_remove_notify_handler;
Andy Lutomirski844af952015-11-24 19:49:23 -08001185 }
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001186 dev_set_drvdata(&device->dev, wmi_bus_dev);
Andy Lutomirski844af952015-11-24 19:49:23 -08001187
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001188 error = parse_wdg(wmi_bus_dev, acpi_device);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001189 if (error) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001190 pr_err("Failed to parse WDG method\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001191 goto err_remove_busdev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001192 }
1193
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001194 return 0;
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001195
Andy Lutomirski844af952015-11-24 19:49:23 -08001196err_remove_busdev:
1197 device_unregister(wmi_bus_dev);
1198
Andy Lutomirski1686f542015-11-25 17:33:25 -08001199err_remove_notify_handler:
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001200 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001201 acpi_wmi_notify_handler);
1202
1203err_remove_ec_handler:
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001204 acpi_remove_address_space_handler(acpi_device->handle,
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001205 ACPI_ADR_SPACE_EC,
1206 &acpi_wmi_ec_space_handler);
1207
1208 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001209}
1210
Andy Lutomirski844af952015-11-24 19:49:23 -08001211int __must_check __wmi_driver_register(struct wmi_driver *driver,
1212 struct module *owner)
1213{
1214 driver->driver.owner = owner;
1215 driver->driver.bus = &wmi_bus_type;
1216
1217 return driver_register(&driver->driver);
1218}
1219EXPORT_SYMBOL(__wmi_driver_register);
1220
1221void wmi_driver_unregister(struct wmi_driver *driver)
1222{
1223 driver_unregister(&driver->driver);
1224}
1225EXPORT_SYMBOL(wmi_driver_unregister);
1226
Carlos Corbachobff431e2008-02-05 02:17:04 +00001227static int __init acpi_wmi_init(void)
1228{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001229 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001230
1231 if (acpi_disabled)
1232 return -ENODEV;
1233
Andy Lutomirski844af952015-11-24 19:49:23 -08001234 error = class_register(&wmi_bus_class);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001235 if (error)
1236 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001237
Andy Lutomirski844af952015-11-24 19:49:23 -08001238 error = bus_register(&wmi_bus_type);
1239 if (error)
1240 goto err_unreg_class;
1241
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001242 error = platform_driver_register(&acpi_wmi_driver);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001243 if (error) {
1244 pr_err("Error loading mapper\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001245 goto err_unreg_bus;
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001246 }
1247
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001248 return 0;
Andy Lutomirski844af952015-11-24 19:49:23 -08001249
1250err_unreg_class:
1251 class_unregister(&wmi_bus_class);
1252
1253err_unreg_bus:
1254 bus_unregister(&wmi_bus_type);
1255
1256 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001257}
1258
1259static void __exit acpi_wmi_exit(void)
1260{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001261 platform_driver_unregister(&acpi_wmi_driver);
Andy Lutomirski844af952015-11-24 19:49:23 -08001262 class_unregister(&wmi_bus_class);
1263 bus_unregister(&wmi_bus_type);
Carlos Corbachobff431e2008-02-05 02:17:04 +00001264}
1265
1266subsys_initcall(acpi_wmi_init);
1267module_exit(acpi_wmi_exit);