blob: 250d7b2398b4c043df96f15bb7525494f7b88850 [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
72 bool read_takes_no_args; /* only defined if readable */
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
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800697static ssize_t readable_show(struct device *dev, struct device_attribute *attr,
698 char *buf)
699{
700 struct wmi_device *wdev = dev_to_wdev(dev);
701
702 return sprintf(buf, "%d\n", (int)wdev->readable);
703}
704static DEVICE_ATTR_RO(readable);
705
706static ssize_t writeable_show(struct device *dev, struct device_attribute *attr,
707 char *buf)
708{
709 struct wmi_device *wdev = dev_to_wdev(dev);
710
711 return sprintf(buf, "%d\n", (int)wdev->writeable);
712}
713static DEVICE_ATTR_RO(writeable);
714
715static struct attribute *wmi_data_attrs[] = {
716 &dev_attr_object_id.attr,
717 &dev_attr_readable.attr,
718 &dev_attr_writeable.attr,
719 NULL,
720};
721ATTRIBUTE_GROUPS(wmi_data);
722
723static struct attribute *wmi_method_attrs[] = {
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800724 &dev_attr_object_id.attr,
725 NULL,
726};
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800727ATTRIBUTE_GROUPS(wmi_method);
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800728
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500729static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
730{
Andy Lutomirski844af952015-11-24 19:49:23 -0800731 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500732
Andy Lutomirski844af952015-11-24 19:49:23 -0800733 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500734 return -ENOMEM;
735
Andy Lutomirski844af952015-11-24 19:49:23 -0800736 if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500737 return -ENOMEM;
738
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500739 return 0;
740}
741
Andy Lutomirski844af952015-11-24 19:49:23 -0800742static void wmi_dev_release(struct device *dev)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500743{
Andy Lutomirski844af952015-11-24 19:49:23 -0800744 struct wmi_block *wblock = dev_to_wblock(dev);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700745
Andy Lutomirski844af952015-11-24 19:49:23 -0800746 kfree(wblock);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500747}
748
Andy Lutomirski844af952015-11-24 19:49:23 -0800749static int wmi_dev_match(struct device *dev, struct device_driver *driver)
750{
751 struct wmi_driver *wmi_driver =
752 container_of(driver, struct wmi_driver, driver);
753 struct wmi_block *wblock = dev_to_wblock(dev);
754 const struct wmi_device_id *id = wmi_driver->id_table;
755
756 while (id->guid_string) {
757 uuid_le driver_guid;
758
759 if (WARN_ON(uuid_le_to_bin(id->guid_string, &driver_guid)))
760 continue;
761 if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
762 return 1;
763
764 id++;
765 }
766
767 return 0;
768}
769
770static int wmi_dev_probe(struct device *dev)
771{
772 struct wmi_block *wblock = dev_to_wblock(dev);
773 struct wmi_driver *wdriver =
774 container_of(dev->driver, struct wmi_driver, driver);
775 int ret = 0;
776
777 if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
778 dev_warn(dev, "failed to enable device -- probing anyway\n");
779
780 if (wdriver->probe) {
781 ret = wdriver->probe(dev_to_wdev(dev));
782 if (ret != 0 && ACPI_FAILURE(wmi_method_enable(wblock, 0)))
783 dev_warn(dev, "failed to disable device\n");
784 }
785
786 return ret;
787}
788
789static int wmi_dev_remove(struct device *dev)
790{
791 struct wmi_block *wblock = dev_to_wblock(dev);
792 struct wmi_driver *wdriver =
793 container_of(dev->driver, struct wmi_driver, driver);
794 int ret = 0;
795
796 if (wdriver->remove)
797 ret = wdriver->remove(dev_to_wdev(dev));
798
799 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
800 dev_warn(dev, "failed to disable device\n");
801
802 return ret;
803}
804
805static struct class wmi_bus_class = {
806 .name = "wmi_bus",
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500807};
808
Andy Lutomirski844af952015-11-24 19:49:23 -0800809static struct bus_type wmi_bus_type = {
810 .name = "wmi",
811 .dev_groups = wmi_groups,
812 .match = wmi_dev_match,
813 .uevent = wmi_dev_uevent,
814 .probe = wmi_dev_probe,
815 .remove = wmi_dev_remove,
816};
817
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800818static struct device_type wmi_type_event = {
819 .name = "event",
820 .groups = wmi_event_groups,
821 .release = wmi_dev_release,
822};
823
824static struct device_type wmi_type_method = {
825 .name = "method",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800826 .groups = wmi_method_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800827 .release = wmi_dev_release,
828};
829
830static struct device_type wmi_type_data = {
831 .name = "data",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800832 .groups = wmi_data_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800833 .release = wmi_dev_release,
834};
835
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700836static void wmi_create_device(struct device *wmi_bus_dev,
Andy Lutomirski844af952015-11-24 19:49:23 -0800837 const struct guid_block *gblock,
Andy Lutomirski7f5809b2015-11-19 14:44:46 -0800838 struct wmi_block *wblock,
839 struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500840{
Andy Lutomirski844af952015-11-24 19:49:23 -0800841 wblock->dev.dev.bus = &wmi_bus_type;
842 wblock->dev.dev.parent = wmi_bus_dev;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700843
Andy Lutomirski844af952015-11-24 19:49:23 -0800844 dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700845
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800846 if (gblock->flags & ACPI_WMI_EVENT) {
847 wblock->dev.dev.type = &wmi_type_event;
848 } else if (gblock->flags & ACPI_WMI_METHOD) {
849 wblock->dev.dev.type = &wmi_type_method;
850 } else {
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800851 struct acpi_device_info *info;
852 char method[5];
853 int result;
854
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800855 wblock->dev.dev.type = &wmi_type_data;
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800856
857 strcpy(method, "WQ");
858 strncat(method, wblock->gblock.object_id, 2);
859 result = get_subobj_info(device->handle, method, &info);
860
861 if (result == 0) {
862 wblock->dev.readable = true;
863
864 /*
865 * The Microsoft documentation specifically states:
866 *
867 * Data blocks registered with only a single instance
868 * can ignore the parameter.
869 *
870 * ACPICA will get mad at us if we call the method
871 * with the wrong number of arguments, so check what
872 * our method expects. (On some Dell laptops, WQxx
873 * may not be a method at all.)
874 */
875 if (info->type != ACPI_TYPE_METHOD ||
876 info->param_count == 0)
877 wblock->read_takes_no_args = true;
878
879 kfree(info);
880 }
881
882 strcpy(method, "WS");
883 strncat(method, wblock->gblock.object_id, 2);
884 result = get_subobj_info(device->handle, method, NULL);
885
886 if (result == 0) {
887 wblock->dev.writeable = true;
888 }
889
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800890 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700891
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700892 device_initialize(&wblock->dev.dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500893}
894
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800895static void wmi_free_devices(struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500896{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700897 struct wmi_block *wblock, *next;
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500898
899 /* Delete devices for all the GUIDs */
Dmitry Torokhov023b9562011-09-07 15:00:02 -0700900 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800901 if (wblock->acpi_device == device) {
902 list_del(&wblock->list);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700903 device_unregister(&wblock->dev.dev);
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800904 }
Dmitry Torokhov023b9562011-09-07 15:00:02 -0700905 }
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500906}
907
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800908static bool guid_already_parsed(struct acpi_device *device,
909 const u8 *guid)
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000910{
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000911 struct wmi_block *wblock;
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000912
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800913 list_for_each_entry(wblock, &wmi_block_list, list) {
914 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
915 /*
916 * Because we historically didn't track the relationship
917 * between GUIDs and ACPI nodes, we don't know whether
918 * we need to suppress GUIDs that are unique on a
919 * given node but duplicated across nodes.
920 */
921 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
922 guid, dev_name(&wblock->acpi_device->dev));
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000923 return true;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800924 }
925 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700926
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000927 return false;
928}
929
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500930/*
Carlos Corbachobff431e2008-02-05 02:17:04 +0000931 * Parse the _WDG method for the GUID data blocks
932 */
Andy Lutomirski844af952015-11-24 19:49:23 -0800933static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000934{
935 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
Dmitry Torokhov37830662010-08-26 00:15:09 -0700936 const struct guid_block *gblock;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700937 struct wmi_block *wblock, *next;
938 union acpi_object *obj;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000939 acpi_status status;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700940 int retval = 0;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000941 u32 i, total;
942
Andy Lutomirski7f5809b2015-11-19 14:44:46 -0800943 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000944 if (ACPI_FAILURE(status))
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700945 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000946
947 obj = (union acpi_object *) out.pointer;
Dmitry Torokhov3d2c63e2010-08-26 00:15:03 -0700948 if (!obj)
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700949 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000950
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -0700951 if (obj->type != ACPI_TYPE_BUFFER) {
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700952 retval = -ENXIO;
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -0700953 goto out_free_pointer;
954 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000955
Dmitry Torokhov37830662010-08-26 00:15:09 -0700956 gblock = (const struct guid_block *)obj->buffer.pointer;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000957 total = obj->buffer.length / sizeof(struct guid_block);
958
Carlos Corbachobff431e2008-02-05 02:17:04 +0000959 for (i = 0; i < total; i++) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200960 if (debug_dump_wdg)
961 wmi_dump_wdg(&gblock[i]);
962
Andy Lutomirskia1c31bcd2015-11-25 08:24:42 -0800963 /*
964 * Some WMI devices, like those for nVidia hooks, have a
965 * duplicate GUID. It's not clear what we should do in this
966 * case yet, so for now, we'll just ignore the duplicate
967 * for device creation.
968 */
969 if (guid_already_parsed(device, gblock[i].guid))
970 continue;
971
Colin King58f64252010-11-19 15:40:02 +0000972 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700973 if (!wblock) {
974 retval = -ENOMEM;
975 break;
976 }
Colin King58f64252010-11-19 15:40:02 +0000977
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800978 wblock->acpi_device = device;
Colin King58f64252010-11-19 15:40:02 +0000979 wblock->gblock = gblock[i];
980
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700981 wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000982
Colin King58f64252010-11-19 15:40:02 +0000983 list_add_tail(&wblock->list, &wmi_block_list);
984
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200985 if (debug_event) {
986 wblock->handler = wmi_notify_debug;
Dmitry Torokhov2d5ab552010-08-26 00:14:48 -0700987 wmi_method_enable(wblock, 1);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200988 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000989 }
990
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700991 /*
992 * Now that all of the devices are created, add them to the
993 * device tree and probe subdrivers.
994 */
995 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
996 if (wblock->acpi_device != device)
997 continue;
998
999 retval = device_add(&wblock->dev.dev);
1000 if (retval) {
1001 dev_err(wmi_bus_dev, "failed to register %pULL\n",
1002 wblock->gblock.guid);
1003 if (debug_event)
1004 wmi_method_enable(wblock, 0);
1005 list_del(&wblock->list);
1006 put_device(&wblock->dev.dev);
1007 }
1008 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001009
Axel Lina5167c52010-06-03 11:45:45 +08001010out_free_pointer:
1011 kfree(out.pointer);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001012 return retval;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001013}
1014
1015/*
1016 * WMI can have EmbeddedControl access regions. In which case, we just want to
1017 * hand these off to the EC driver.
1018 */
1019static acpi_status
1020acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
Lin Ming439913f2010-01-28 10:53:19 +08001021 u32 bits, u64 *value,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001022 void *handler_context, void *region_context)
1023{
1024 int result = 0, i = 0;
1025 u8 temp = 0;
1026
1027 if ((address > 0xFF) || !value)
1028 return AE_BAD_PARAMETER;
1029
1030 if (function != ACPI_READ && function != ACPI_WRITE)
1031 return AE_BAD_PARAMETER;
1032
1033 if (bits != 8)
1034 return AE_BAD_PARAMETER;
1035
1036 if (function == ACPI_READ) {
1037 result = ec_read(address, &temp);
Lin Ming439913f2010-01-28 10:53:19 +08001038 (*value) |= ((u64)temp) << i;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001039 } else {
1040 temp = 0xff & ((*value) >> i);
1041 result = ec_write(address, temp);
1042 }
1043
1044 switch (result) {
1045 case -EINVAL:
1046 return AE_BAD_PARAMETER;
1047 break;
1048 case -ENODEV:
1049 return AE_NOT_FOUND;
1050 break;
1051 case -ETIME:
1052 return AE_TIME;
1053 break;
1054 default:
1055 return AE_OK;
1056 }
1057}
1058
Andy Lutomirski1686f542015-11-25 17:33:25 -08001059static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1060 void *context)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001061{
1062 struct guid_block *block;
1063 struct wmi_block *wblock;
1064 struct list_head *p;
Andy Lutomirski1686f542015-11-25 17:33:25 -08001065 bool found_it = false;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001066
Dmitry Torokhov762e1a22010-08-26 00:15:14 -07001067 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +00001068 wblock = list_entry(p, struct wmi_block, list);
1069 block = &wblock->gblock;
1070
Andy Lutomirski1686f542015-11-25 17:33:25 -08001071 if (wblock->acpi_device->handle == handle &&
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001072 (block->flags & ACPI_WMI_EVENT) &&
Andy Lutomirski1686f542015-11-25 17:33:25 -08001073 (block->notify_id == event))
1074 {
1075 found_it = true;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001076 break;
1077 }
1078 }
Andy Lutomirski1686f542015-11-25 17:33:25 -08001079
1080 if (!found_it)
1081 return;
1082
1083 /* If a driver is bound, then notify the driver. */
1084 if (wblock->dev.dev.driver) {
1085 struct wmi_driver *driver;
1086 struct acpi_object_list input;
1087 union acpi_object params[1];
1088 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1089 acpi_status status;
1090
1091 driver = container_of(wblock->dev.dev.driver,
1092 struct wmi_driver, driver);
1093
1094 input.count = 1;
1095 input.pointer = params;
1096 params[0].type = ACPI_TYPE_INTEGER;
1097 params[0].integer.value = event;
1098
1099 status = acpi_evaluate_object(wblock->acpi_device->handle,
1100 "_WED", &input, &evdata);
1101 if (ACPI_FAILURE(status)) {
1102 dev_warn(&wblock->dev.dev,
1103 "failed to get event data\n");
1104 return;
1105 }
1106
1107 if (driver->notify)
1108 driver->notify(&wblock->dev,
1109 (union acpi_object *)evdata.pointer);
1110
1111 kfree(evdata.pointer);
1112 } else if (wblock->handler) {
1113 /* Legacy handler */
1114 wblock->handler(event, wblock->handler_data);
1115 }
1116
1117 if (debug_event) {
1118 pr_info("DEBUG Event GUID: %pUL\n",
1119 wblock->gblock.guid);
1120 }
1121
1122 acpi_bus_generate_netlink_event(
1123 wblock->acpi_device->pnp.device_class,
1124 dev_name(&wblock->dev.dev),
1125 event, 0);
1126
Carlos Corbachobff431e2008-02-05 02:17:04 +00001127}
1128
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001129static int acpi_wmi_remove(struct platform_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001130{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001131 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1132
1133 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001134 acpi_wmi_notify_handler);
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001135 acpi_remove_address_space_handler(acpi_device->handle,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001136 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001137 wmi_free_devices(acpi_device);
1138 device_unregister((struct device *)dev_get_drvdata(&device->dev));
Carlos Corbachobff431e2008-02-05 02:17:04 +00001139
1140 return 0;
1141}
1142
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001143static int acpi_wmi_probe(struct platform_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001144{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001145 struct acpi_device *acpi_device;
Andy Lutomirski844af952015-11-24 19:49:23 -08001146 struct device *wmi_bus_dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001147 acpi_status status;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001148 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001149
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001150 acpi_device = ACPI_COMPANION(&device->dev);
1151 if (!acpi_device) {
1152 dev_err(&device->dev, "ACPI companion is missing\n");
1153 return -ENODEV;
1154 }
1155
1156 status = acpi_install_address_space_handler(acpi_device->handle,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001157 ACPI_ADR_SPACE_EC,
1158 &acpi_wmi_ec_space_handler,
1159 NULL, NULL);
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001160 if (ACPI_FAILURE(status)) {
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001161 dev_err(&device->dev, "Error installing EC region handler\n");
Carlos Corbachobff431e2008-02-05 02:17:04 +00001162 return -ENODEV;
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001163 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001164
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001165 status = acpi_install_notify_handler(acpi_device->handle,
1166 ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001167 acpi_wmi_notify_handler,
1168 NULL);
1169 if (ACPI_FAILURE(status)) {
1170 dev_err(&device->dev, "Error installing notify handler\n");
1171 error = -ENODEV;
1172 goto err_remove_ec_handler;
1173 }
1174
Andy Lutomirski844af952015-11-24 19:49:23 -08001175 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1176 NULL, "wmi_bus-%s", dev_name(&device->dev));
1177 if (IS_ERR(wmi_bus_dev)) {
1178 error = PTR_ERR(wmi_bus_dev);
Andy Lutomirski1686f542015-11-25 17:33:25 -08001179 goto err_remove_notify_handler;
Andy Lutomirski844af952015-11-24 19:49:23 -08001180 }
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001181 dev_set_drvdata(&device->dev, wmi_bus_dev);
Andy Lutomirski844af952015-11-24 19:49:23 -08001182
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001183 error = parse_wdg(wmi_bus_dev, acpi_device);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001184 if (error) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001185 pr_err("Failed to parse WDG method\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001186 goto err_remove_busdev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001187 }
1188
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001189 return 0;
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001190
Andy Lutomirski844af952015-11-24 19:49:23 -08001191err_remove_busdev:
1192 device_unregister(wmi_bus_dev);
1193
Andy Lutomirski1686f542015-11-25 17:33:25 -08001194err_remove_notify_handler:
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001195 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001196 acpi_wmi_notify_handler);
1197
1198err_remove_ec_handler:
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001199 acpi_remove_address_space_handler(acpi_device->handle,
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001200 ACPI_ADR_SPACE_EC,
1201 &acpi_wmi_ec_space_handler);
1202
1203 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001204}
1205
Andy Lutomirski844af952015-11-24 19:49:23 -08001206int __must_check __wmi_driver_register(struct wmi_driver *driver,
1207 struct module *owner)
1208{
1209 driver->driver.owner = owner;
1210 driver->driver.bus = &wmi_bus_type;
1211
1212 return driver_register(&driver->driver);
1213}
1214EXPORT_SYMBOL(__wmi_driver_register);
1215
1216void wmi_driver_unregister(struct wmi_driver *driver)
1217{
1218 driver_unregister(&driver->driver);
1219}
1220EXPORT_SYMBOL(wmi_driver_unregister);
1221
Carlos Corbachobff431e2008-02-05 02:17:04 +00001222static int __init acpi_wmi_init(void)
1223{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001224 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001225
1226 if (acpi_disabled)
1227 return -ENODEV;
1228
Andy Lutomirski844af952015-11-24 19:49:23 -08001229 error = class_register(&wmi_bus_class);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001230 if (error)
1231 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001232
Andy Lutomirski844af952015-11-24 19:49:23 -08001233 error = bus_register(&wmi_bus_type);
1234 if (error)
1235 goto err_unreg_class;
1236
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001237 error = platform_driver_register(&acpi_wmi_driver);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001238 if (error) {
1239 pr_err("Error loading mapper\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001240 goto err_unreg_bus;
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001241 }
1242
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001243 return 0;
Andy Lutomirski844af952015-11-24 19:49:23 -08001244
1245err_unreg_class:
1246 class_unregister(&wmi_bus_class);
1247
1248err_unreg_bus:
1249 bus_unregister(&wmi_bus_type);
1250
1251 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001252}
1253
1254static void __exit acpi_wmi_exit(void)
1255{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001256 platform_driver_unregister(&acpi_wmi_driver);
Andy Lutomirski844af952015-11-24 19:49:23 -08001257 class_unregister(&wmi_bus_class);
1258 bus_unregister(&wmi_bus_type);
Carlos Corbachobff431e2008-02-05 02:17:04 +00001259}
1260
1261subsys_initcall(acpi_wmi_init);
1262module_exit(acpi_wmi_exit);