blob: 1a764e311e11a2a9c4eeea310fbd8ac1960db704 [file] [log] [blame]
Carlos Corbachobff431e2008-02-05 02:17:04 +00001/*
2 * ACPI-WMI mapping driver
3 *
4 * Copyright (C) 2007-2008 Carlos Corbacho <carlos@strangeworlds.co.uk>
5 *
6 * GUID parsing code from ldm.c is:
7 * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
8 * Copyright (c) 2001-2007 Anton Altaparmakov
9 * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
10 *
Darren Hart (VMware)2c9c5662017-06-06 16:40:56 -070011 * WMI bus infrastructure by Andrew Lutomirski and Darren Hart:
12 * Copyright (C) 2015 Andrew Lutomirski
13 * Copyright (C) 2017 VMware, Inc. All Rights Reserved.
14 *
Carlos Corbachobff431e2008-02-05 02:17:04 +000015 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or (at
20 * your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
30 *
31 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32 */
33
Dmitry Torokhov8e075142010-08-26 00:15:19 -070034#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
Carlos Corbachobff431e2008-02-05 02:17:04 +000036#include <linux/kernel.h>
37#include <linux/init.h>
38#include <linux/types.h>
Matthew Garrett1caab3c2009-11-04 14:17:53 -050039#include <linux/device.h>
Carlos Corbachobff431e2008-02-05 02:17:04 +000040#include <linux/list.h>
41#include <linux/acpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Paul Gortmaker7c52d552011-05-27 12:33:10 -040043#include <linux/module.h>
Andy Lutomirski9599ed92015-11-27 11:56:02 -080044#include <linux/platform_device.h>
Andy Lutomirski844af952015-11-24 19:49:23 -080045#include <linux/wmi.h>
Andy Shevchenko538d7eb2016-05-20 17:01:30 -070046#include <linux/uuid.h>
Carlos Corbachobff431e2008-02-05 02:17:04 +000047
48ACPI_MODULE_NAME("wmi");
49MODULE_AUTHOR("Carlos Corbacho");
50MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
51MODULE_LICENSE("GPL");
52
Dmitry Torokhov762e1a22010-08-26 00:15:14 -070053static LIST_HEAD(wmi_block_list);
Carlos Corbachobff431e2008-02-05 02:17:04 +000054
55struct guid_block {
56 char guid[16];
57 union {
58 char object_id[2];
59 struct {
60 unsigned char notify_id;
61 unsigned char reserved;
62 };
63 };
64 u8 instance_count;
65 u8 flags;
66};
67
68struct wmi_block {
Andy Lutomirski844af952015-11-24 19:49:23 -080069 struct wmi_device dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +000070 struct list_head list;
71 struct guid_block gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -080072 struct acpi_device *acpi_device;
Carlos Corbachobff431e2008-02-05 02:17:04 +000073 wmi_notify_handler handler;
74 void *handler_data;
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -080075
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -070076 bool read_takes_no_args;
Carlos Corbachobff431e2008-02-05 02:17:04 +000077};
78
Carlos Corbachobff431e2008-02-05 02:17:04 +000079
80/*
81 * If the GUID data block is marked as expensive, we must enable and
82 * explicitily disable data collection.
83 */
84#define ACPI_WMI_EXPENSIVE 0x1
85#define ACPI_WMI_METHOD 0x2 /* GUID is a method */
86#define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
87#define ACPI_WMI_EVENT 0x8 /* GUID is an event */
88
Rusty Russell90ab5ee2012-01-13 09:32:20 +103089static bool debug_event;
Thomas Renningerfc3155b2010-05-03 15:30:15 +020090module_param(debug_event, bool, 0444);
91MODULE_PARM_DESC(debug_event,
92 "Log WMI Events [0/1]");
93
Rusty Russell90ab5ee2012-01-13 09:32:20 +103094static bool debug_dump_wdg;
Thomas Renningera929aae2010-05-03 15:30:17 +020095module_param(debug_dump_wdg, bool, 0444);
96MODULE_PARM_DESC(debug_dump_wdg,
97 "Dump available WMI interfaces [0/1]");
98
Andy Lutomirski9599ed92015-11-27 11:56:02 -080099static int acpi_wmi_remove(struct platform_device *device);
100static int acpi_wmi_probe(struct platform_device *device);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000101
102static const struct acpi_device_id wmi_device_ids[] = {
103 {"PNP0C14", 0},
104 {"pnp0c14", 0},
105 {"", 0},
106};
107MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
108
Andy Lutomirski9599ed92015-11-27 11:56:02 -0800109static struct platform_driver acpi_wmi_driver = {
110 .driver = {
111 .name = "acpi-wmi",
112 .acpi_match_table = wmi_device_ids,
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700113 },
Andy Lutomirski9599ed92015-11-27 11:56:02 -0800114 .probe = acpi_wmi_probe,
115 .remove = acpi_wmi_remove,
Carlos Corbachobff431e2008-02-05 02:17:04 +0000116};
117
118/*
119 * GUID parsing functions
120 */
121
Carlos Corbachobff431e2008-02-05 02:17:04 +0000122static bool find_guid(const char *guid_string, struct wmi_block **out)
123{
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700124 uuid_le guid_input;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000125 struct wmi_block *wblock;
126 struct guid_block *block;
127 struct list_head *p;
128
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700129 if (uuid_le_to_bin(guid_string, &guid_input))
130 return false;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000131
Dmitry Torokhov762e1a22010-08-26 00:15:14 -0700132 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000133 wblock = list_entry(p, struct wmi_block, list);
134 block = &wblock->gblock;
135
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700136 if (memcmp(block->guid, &guid_input, 16) == 0) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000137 if (out)
138 *out = wblock;
Joe Perches097c27f2015-03-30 10:43:20 -0700139 return true;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000140 }
141 }
Joe Perches097c27f2015-03-30 10:43:20 -0700142 return false;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000143}
144
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800145static int get_subobj_info(acpi_handle handle, const char *pathname,
146 struct acpi_device_info **info)
147{
148 struct acpi_device_info *dummy_info, **info_ptr;
149 acpi_handle subobj_handle;
150 acpi_status status;
151
152 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
153 if (status == AE_NOT_FOUND)
154 return -ENOENT;
155 else if (ACPI_FAILURE(status))
156 return -EIO;
157
158 info_ptr = info ? info : &dummy_info;
159 status = acpi_get_object_info(subobj_handle, info_ptr);
160 if (ACPI_FAILURE(status))
161 return -EIO;
162
163 if (!info)
164 kfree(dummy_info);
165
166 return 0;
167}
168
Matthew Garretta66bfa72008-10-08 21:40:32 +0100169static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
170{
171 struct guid_block *block = NULL;
172 char method[5];
Matthew Garretta66bfa72008-10-08 21:40:32 +0100173 acpi_status status;
174 acpi_handle handle;
175
176 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800177 handle = wblock->acpi_device->handle;
Matthew Garretta66bfa72008-10-08 21:40:32 +0100178
Matthew Garretta66bfa72008-10-08 21:40:32 +0100179 snprintf(method, 5, "WE%02X", block->notify_id);
Zhang Rui8122ab62013-09-03 08:31:57 +0800180 status = acpi_execute_simple_method(handle, method, enable);
Matthew Garretta66bfa72008-10-08 21:40:32 +0100181
182 if (status != AE_OK && status != AE_NOT_FOUND)
183 return status;
184 else
185 return AE_OK;
186}
187
Carlos Corbachobff431e2008-02-05 02:17:04 +0000188/*
189 * Exported WMI functions
190 */
191/**
192 * wmi_evaluate_method - Evaluate a WMI method
193 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
194 * @instance: Instance index
195 * @method_id: Method ID to call
196 * &in: Buffer containing input for the method call
197 * &out: Empty buffer to return the method results
198 *
199 * Call an ACPI-WMI method
200 */
201acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
202u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
203{
204 struct guid_block *block = NULL;
205 struct wmi_block *wblock = NULL;
206 acpi_handle handle;
207 acpi_status status;
208 struct acpi_object_list input;
209 union acpi_object params[3];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700210 char method[5] = "WM";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000211
212 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800213 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000214
215 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800216 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000217
Al Viroe6bafba2008-02-13 04:03:25 +0000218 if (!(block->flags & ACPI_WMI_METHOD))
Carlos Corbachobff431e2008-02-05 02:17:04 +0000219 return AE_BAD_DATA;
220
221 if (block->instance_count < instance)
222 return AE_BAD_PARAMETER;
223
224 input.count = 2;
225 input.pointer = params;
226 params[0].type = ACPI_TYPE_INTEGER;
227 params[0].integer.value = instance;
228 params[1].type = ACPI_TYPE_INTEGER;
229 params[1].integer.value = method_id;
230
231 if (in) {
232 input.count = 3;
233
234 if (block->flags & ACPI_WMI_STRING) {
235 params[2].type = ACPI_TYPE_STRING;
236 } else {
237 params[2].type = ACPI_TYPE_BUFFER;
238 }
239 params[2].buffer.length = in->length;
240 params[2].buffer.pointer = in->pointer;
241 }
242
243 strncat(method, block->object_id, 2);
244
245 status = acpi_evaluate_object(handle, method, &input, out);
246
247 return status;
248}
249EXPORT_SYMBOL_GPL(wmi_evaluate_method);
250
Andy Lutomirski56a37022015-11-25 18:19:26 -0800251static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
252 struct acpi_buffer *out)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000253{
254 struct guid_block *block = NULL;
Zhang Rui54f14c22013-09-03 08:32:07 +0800255 acpi_handle handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000256 acpi_status status, wc_status = AE_ERROR;
Zhang Rui8122ab62013-09-03 08:31:57 +0800257 struct acpi_object_list input;
258 union acpi_object wq_params[1];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700259 char method[5];
260 char wc_method[5] = "WC";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000261
Andy Lutomirski56a37022015-11-25 18:19:26 -0800262 if (!out)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000263 return AE_BAD_PARAMETER;
264
Carlos Corbachobff431e2008-02-05 02:17:04 +0000265 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800266 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000267
268 if (block->instance_count < instance)
269 return AE_BAD_PARAMETER;
270
271 /* Check GUID is a data block */
272 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800273 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000274
275 input.count = 1;
276 input.pointer = wq_params;
277 wq_params[0].type = ACPI_TYPE_INTEGER;
278 wq_params[0].integer.value = instance;
279
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800280 if (instance == 0 && wblock->read_takes_no_args)
281 input.count = 0;
282
Carlos Corbachobff431e2008-02-05 02:17:04 +0000283 /*
284 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
285 * enable collection.
286 */
287 if (block->flags & ACPI_WMI_EXPENSIVE) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000288 strncat(wc_method, block->object_id, 2);
289
290 /*
291 * Some GUIDs break the specification by declaring themselves
292 * expensive, but have no corresponding WCxx method. So we
293 * should not fail if this happens.
294 */
Zhang Rui54f14c22013-09-03 08:32:07 +0800295 if (acpi_has_method(handle, wc_method))
Zhang Rui8122ab62013-09-03 08:31:57 +0800296 wc_status = acpi_execute_simple_method(handle,
297 wc_method, 1);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000298 }
299
300 strcpy(method, "WQ");
301 strncat(method, block->object_id, 2);
302
Carlos Corbachodab36ad2008-08-02 17:28:45 +0100303 status = acpi_evaluate_object(handle, method, &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000304
305 /*
306 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
307 * the WQxx method failed - we should disable collection anyway.
308 */
Carlos Corbachoa527f2d2008-02-24 13:34:34 +0000309 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
Zhang Rui8122ab62013-09-03 08:31:57 +0800310 status = acpi_execute_simple_method(handle, wc_method, 0);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000311 }
312
313 return status;
314}
Andy Lutomirski56a37022015-11-25 18:19:26 -0800315
316/**
317 * wmi_query_block - Return contents of a WMI block (deprecated)
318 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
319 * @instance: Instance index
320 * &out: Empty buffer to return the contents of the data block to
321 *
322 * Return the contents of an ACPI-WMI data block to a buffer
323 */
324acpi_status wmi_query_block(const char *guid_string, u8 instance,
325 struct acpi_buffer *out)
326{
327 struct wmi_block *wblock;
328
329 if (!guid_string)
330 return AE_BAD_PARAMETER;
331
332 if (!find_guid(guid_string, &wblock))
333 return AE_ERROR;
334
335 return __query_block(wblock, instance, out);
336}
Carlos Corbachobff431e2008-02-05 02:17:04 +0000337EXPORT_SYMBOL_GPL(wmi_query_block);
338
Andy Lutomirski56a37022015-11-25 18:19:26 -0800339union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
340{
341 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
342 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
343
344 if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
345 return NULL;
346
347 return (union acpi_object *)out.pointer;
348}
349EXPORT_SYMBOL_GPL(wmidev_block_query);
350
Andy Lutomirskif6301982015-12-29 22:53:51 -0800351struct wmi_device *wmidev_get_other_guid(struct wmi_device *wdev,
352 const char *guid_string)
353{
354 struct wmi_block *this_wb = container_of(wdev, struct wmi_block, dev);
355 struct wmi_block *other_wb;
356
357 if (!find_guid(guid_string, &other_wb))
358 return NULL;
359
360 if (other_wb->acpi_device != this_wb->acpi_device)
361 return NULL;
362
363 get_device(&other_wb->dev.dev);
364 return &other_wb->dev;
365}
366EXPORT_SYMBOL_GPL(wmidev_get_other_guid);
367
Carlos Corbachobff431e2008-02-05 02:17:04 +0000368/**
369 * wmi_set_block - Write to a WMI block
370 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
371 * @instance: Instance index
372 * &in: Buffer containing new values for the data block
373 *
374 * Write the contents of the input buffer to an ACPI-WMI data block
375 */
376acpi_status wmi_set_block(const char *guid_string, u8 instance,
Andy Lutomirski56a37022015-11-25 18:19:26 -0800377 const struct acpi_buffer *in)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000378{
379 struct guid_block *block = NULL;
380 struct wmi_block *wblock = NULL;
381 acpi_handle handle;
382 struct acpi_object_list input;
383 union acpi_object params[2];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700384 char method[5] = "WS";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000385
386 if (!guid_string || !in)
387 return AE_BAD_DATA;
388
389 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800390 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000391
392 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800393 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000394
395 if (block->instance_count < instance)
396 return AE_BAD_PARAMETER;
397
398 /* Check GUID is a data block */
399 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800400 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000401
402 input.count = 2;
403 input.pointer = params;
404 params[0].type = ACPI_TYPE_INTEGER;
405 params[0].integer.value = instance;
406
407 if (block->flags & ACPI_WMI_STRING) {
408 params[1].type = ACPI_TYPE_STRING;
409 } else {
410 params[1].type = ACPI_TYPE_BUFFER;
411 }
412 params[1].buffer.length = in->length;
413 params[1].buffer.pointer = in->pointer;
414
415 strncat(method, block->object_id, 2);
416
417 return acpi_evaluate_object(handle, method, &input, NULL);
418}
419EXPORT_SYMBOL_GPL(wmi_set_block);
420
Dmitry Torokhov37830662010-08-26 00:15:09 -0700421static void wmi_dump_wdg(const struct guid_block *g)
Thomas Renningera929aae2010-05-03 15:30:17 +0200422{
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200423 pr_info("%pUL:\n", g->guid);
Pali Rohárcd3921f2017-06-10 12:57:11 +0200424 if (g->flags & ACPI_WMI_EVENT)
425 pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
426 else
427 pr_info("\tobject_id: %2pE\n", g->object_id);
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700428 pr_info("\tinstance_count: %d\n", g->instance_count);
Joe Perchesdd8e9082011-03-29 15:21:53 -0700429 pr_info("\tflags: %#x", g->flags);
Thomas Renningera929aae2010-05-03 15:30:17 +0200430 if (g->flags) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200431 if (g->flags & ACPI_WMI_EXPENSIVE)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700432 pr_cont(" ACPI_WMI_EXPENSIVE");
Thomas Renningera929aae2010-05-03 15:30:17 +0200433 if (g->flags & ACPI_WMI_METHOD)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700434 pr_cont(" ACPI_WMI_METHOD");
Thomas Renningera929aae2010-05-03 15:30:17 +0200435 if (g->flags & ACPI_WMI_STRING)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700436 pr_cont(" ACPI_WMI_STRING");
Thomas Renningera929aae2010-05-03 15:30:17 +0200437 if (g->flags & ACPI_WMI_EVENT)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700438 pr_cont(" ACPI_WMI_EVENT");
Thomas Renningera929aae2010-05-03 15:30:17 +0200439 }
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700440 pr_cont("\n");
Thomas Renningera929aae2010-05-03 15:30:17 +0200441
442}
443
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200444static void wmi_notify_debug(u32 value, void *context)
445{
446 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
447 union acpi_object *obj;
Axel Lin1492616a2010-06-28 09:30:45 +0800448 acpi_status status;
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200449
Axel Lin1492616a2010-06-28 09:30:45 +0800450 status = wmi_get_event_data(value, &response);
451 if (status != AE_OK) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700452 pr_info("bad event status 0x%x\n", status);
Axel Lin1492616a2010-06-28 09:30:45 +0800453 return;
454 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200455
456 obj = (union acpi_object *)response.pointer;
457
458 if (!obj)
459 return;
460
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700461 pr_info("DEBUG Event ");
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200462 switch(obj->type) {
463 case ACPI_TYPE_BUFFER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700464 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200465 break;
466 case ACPI_TYPE_STRING:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700467 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200468 break;
469 case ACPI_TYPE_INTEGER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700470 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200471 break;
472 case ACPI_TYPE_PACKAGE:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700473 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200474 break;
475 default:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700476 pr_cont("object type 0x%X\n", obj->type);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200477 }
Axel Lin1492616a2010-06-28 09:30:45 +0800478 kfree(obj);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200479}
480
Carlos Corbachobff431e2008-02-05 02:17:04 +0000481/**
482 * wmi_install_notify_handler - Register handler for WMI events
483 * @handler: Function to handle notifications
484 * @data: Data to be returned to handler when event is fired
485 *
486 * Register a handler for events sent to the ACPI-WMI mapper device.
487 */
488acpi_status wmi_install_notify_handler(const char *guid,
489wmi_notify_handler handler, void *data)
490{
491 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000492 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700493 uuid_le guid_input;
Colin King58f64252010-11-19 15:40:02 +0000494 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000495
496 if (!guid || !handler)
497 return AE_BAD_PARAMETER;
498
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700499 if (uuid_le_to_bin(guid, &guid_input))
500 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000501
Colin King58f64252010-11-19 15:40:02 +0000502 list_for_each(p, &wmi_block_list) {
503 acpi_status wmi_status;
504 block = list_entry(p, struct wmi_block, list);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000505
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700506 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000507 if (block->handler &&
508 block->handler != wmi_notify_debug)
509 return AE_ALREADY_ACQUIRED;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000510
Colin King58f64252010-11-19 15:40:02 +0000511 block->handler = handler;
512 block->handler_data = data;
513
514 wmi_status = wmi_method_enable(block, 1);
515 if ((wmi_status != AE_OK) ||
516 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
517 status = wmi_status;
518 }
519 }
Matthew Garretta66bfa72008-10-08 21:40:32 +0100520
521 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000522}
523EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
524
525/**
526 * wmi_uninstall_notify_handler - Unregister handler for WMI events
527 *
528 * Unregister handler for events sent to the ACPI-WMI mapper device.
529 */
530acpi_status wmi_remove_notify_handler(const char *guid)
531{
532 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000533 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700534 uuid_le guid_input;
Colin King58f64252010-11-19 15:40:02 +0000535 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000536
537 if (!guid)
538 return AE_BAD_PARAMETER;
539
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700540 if (uuid_le_to_bin(guid, &guid_input))
541 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000542
Colin King58f64252010-11-19 15:40:02 +0000543 list_for_each(p, &wmi_block_list) {
544 acpi_status wmi_status;
545 block = list_entry(p, struct wmi_block, list);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000546
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700547 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000548 if (!block->handler ||
549 block->handler == wmi_notify_debug)
550 return AE_NULL_ENTRY;
551
552 if (debug_event) {
553 block->handler = wmi_notify_debug;
554 status = AE_OK;
555 } else {
556 wmi_status = wmi_method_enable(block, 0);
557 block->handler = NULL;
558 block->handler_data = NULL;
559 if ((wmi_status != AE_OK) ||
560 ((wmi_status == AE_OK) &&
561 (status == AE_NOT_EXIST)))
562 status = wmi_status;
563 }
564 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200565 }
Colin King58f64252010-11-19 15:40:02 +0000566
Matthew Garretta66bfa72008-10-08 21:40:32 +0100567 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000568}
569EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
570
571/**
572 * wmi_get_event_data - Get WMI data associated with an event
573 *
Anisse Astier3e9b9882009-12-04 10:10:09 +0100574 * @event: Event to find
575 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
Carlos Corbachobff431e2008-02-05 02:17:04 +0000576 *
577 * Returns extra data associated with an event in WMI.
578 */
579acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
580{
581 struct acpi_object_list input;
582 union acpi_object params[1];
583 struct guid_block *gblock;
584 struct wmi_block *wblock;
585 struct list_head *p;
586
587 input.count = 1;
588 input.pointer = params;
589 params[0].type = ACPI_TYPE_INTEGER;
590 params[0].integer.value = event;
591
Dmitry Torokhov762e1a22010-08-26 00:15:14 -0700592 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000593 wblock = list_entry(p, struct wmi_block, list);
594 gblock = &wblock->gblock;
595
596 if ((gblock->flags & ACPI_WMI_EVENT) &&
597 (gblock->notify_id == event))
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800598 return acpi_evaluate_object(wblock->acpi_device->handle,
599 "_WED", &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000600 }
601
602 return AE_NOT_FOUND;
603}
604EXPORT_SYMBOL_GPL(wmi_get_event_data);
605
606/**
607 * wmi_has_guid - Check if a GUID is available
608 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
609 *
610 * Check if a given GUID is defined by _WDG
611 */
612bool wmi_has_guid(const char *guid_string)
613{
614 return find_guid(guid_string, NULL);
615}
616EXPORT_SYMBOL_GPL(wmi_has_guid);
617
Andy Lutomirski844af952015-11-24 19:49:23 -0800618static struct wmi_block *dev_to_wblock(struct device *dev)
619{
620 return container_of(dev, struct wmi_block, dev.dev);
621}
622
623static struct wmi_device *dev_to_wdev(struct device *dev)
624{
625 return container_of(dev, struct wmi_device, dev);
626}
627
Carlos Corbachobff431e2008-02-05 02:17:04 +0000628/*
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500629 * sysfs interface
630 */
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700631static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500632 char *buf)
633{
Andy Lutomirski844af952015-11-24 19:49:23 -0800634 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500635
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200636 return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500637}
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700638static DEVICE_ATTR_RO(modalias);
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700639
Andy Lutomirski844af952015-11-24 19:49:23 -0800640static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
641 char *buf)
642{
643 struct wmi_block *wblock = dev_to_wblock(dev);
644
645 return sprintf(buf, "%pUL\n", wblock->gblock.guid);
646}
647static DEVICE_ATTR_RO(guid);
648
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800649static ssize_t instance_count_show(struct device *dev,
650 struct device_attribute *attr, char *buf)
651{
652 struct wmi_block *wblock = dev_to_wblock(dev);
653
654 return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
655}
656static DEVICE_ATTR_RO(instance_count);
657
658static ssize_t expensive_show(struct device *dev,
659 struct device_attribute *attr, char *buf)
660{
661 struct wmi_block *wblock = dev_to_wblock(dev);
662
663 return sprintf(buf, "%d\n",
664 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
665}
666static DEVICE_ATTR_RO(expensive);
667
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700668static struct attribute *wmi_attrs[] = {
669 &dev_attr_modalias.attr,
Andy Lutomirski844af952015-11-24 19:49:23 -0800670 &dev_attr_guid.attr,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800671 &dev_attr_instance_count.attr,
672 &dev_attr_expensive.attr,
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700673 NULL,
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700674};
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700675ATTRIBUTE_GROUPS(wmi);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500676
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800677static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
678 char *buf)
679{
680 struct wmi_block *wblock = dev_to_wblock(dev);
681
682 return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
683}
684static DEVICE_ATTR_RO(notify_id);
685
686static struct attribute *wmi_event_attrs[] = {
687 &dev_attr_notify_id.attr,
688 NULL,
689};
690ATTRIBUTE_GROUPS(wmi_event);
691
692static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
693 char *buf)
694{
695 struct wmi_block *wblock = dev_to_wblock(dev);
696
697 return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
698 wblock->gblock.object_id[1]);
699}
700static DEVICE_ATTR_RO(object_id);
701
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700702static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
703 char *buf)
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800704{
705 struct wmi_device *wdev = dev_to_wdev(dev);
706
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700707 return sprintf(buf, "%d\n", (int)wdev->setable);
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800708}
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700709static DEVICE_ATTR_RO(setable);
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800710
711static struct attribute *wmi_data_attrs[] = {
712 &dev_attr_object_id.attr,
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700713 &dev_attr_setable.attr,
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800714 NULL,
715};
716ATTRIBUTE_GROUPS(wmi_data);
717
718static struct attribute *wmi_method_attrs[] = {
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800719 &dev_attr_object_id.attr,
720 NULL,
721};
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800722ATTRIBUTE_GROUPS(wmi_method);
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800723
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500724static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
725{
Andy Lutomirski844af952015-11-24 19:49:23 -0800726 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500727
Andy Lutomirski844af952015-11-24 19:49:23 -0800728 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500729 return -ENOMEM;
730
Andy Lutomirski844af952015-11-24 19:49:23 -0800731 if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500732 return -ENOMEM;
733
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500734 return 0;
735}
736
Andy Lutomirski844af952015-11-24 19:49:23 -0800737static void wmi_dev_release(struct device *dev)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500738{
Andy Lutomirski844af952015-11-24 19:49:23 -0800739 struct wmi_block *wblock = dev_to_wblock(dev);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700740
Andy Lutomirski844af952015-11-24 19:49:23 -0800741 kfree(wblock);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500742}
743
Andy Lutomirski844af952015-11-24 19:49:23 -0800744static int wmi_dev_match(struct device *dev, struct device_driver *driver)
745{
746 struct wmi_driver *wmi_driver =
747 container_of(driver, struct wmi_driver, driver);
748 struct wmi_block *wblock = dev_to_wblock(dev);
749 const struct wmi_device_id *id = wmi_driver->id_table;
750
751 while (id->guid_string) {
752 uuid_le driver_guid;
753
754 if (WARN_ON(uuid_le_to_bin(id->guid_string, &driver_guid)))
755 continue;
756 if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
757 return 1;
758
759 id++;
760 }
761
762 return 0;
763}
764
765static int wmi_dev_probe(struct device *dev)
766{
767 struct wmi_block *wblock = dev_to_wblock(dev);
768 struct wmi_driver *wdriver =
769 container_of(dev->driver, struct wmi_driver, driver);
770 int ret = 0;
771
772 if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
773 dev_warn(dev, "failed to enable device -- probing anyway\n");
774
775 if (wdriver->probe) {
776 ret = wdriver->probe(dev_to_wdev(dev));
777 if (ret != 0 && ACPI_FAILURE(wmi_method_enable(wblock, 0)))
778 dev_warn(dev, "failed to disable device\n");
779 }
780
781 return ret;
782}
783
784static int wmi_dev_remove(struct device *dev)
785{
786 struct wmi_block *wblock = dev_to_wblock(dev);
787 struct wmi_driver *wdriver =
788 container_of(dev->driver, struct wmi_driver, driver);
789 int ret = 0;
790
791 if (wdriver->remove)
792 ret = wdriver->remove(dev_to_wdev(dev));
793
794 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
795 dev_warn(dev, "failed to disable device\n");
796
797 return ret;
798}
799
800static struct class wmi_bus_class = {
801 .name = "wmi_bus",
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500802};
803
Andy Lutomirski844af952015-11-24 19:49:23 -0800804static struct bus_type wmi_bus_type = {
805 .name = "wmi",
806 .dev_groups = wmi_groups,
807 .match = wmi_dev_match,
808 .uevent = wmi_dev_uevent,
809 .probe = wmi_dev_probe,
810 .remove = wmi_dev_remove,
811};
812
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800813static struct device_type wmi_type_event = {
814 .name = "event",
815 .groups = wmi_event_groups,
816 .release = wmi_dev_release,
817};
818
819static struct device_type wmi_type_method = {
820 .name = "method",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800821 .groups = wmi_method_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800822 .release = wmi_dev_release,
823};
824
825static struct device_type wmi_type_data = {
826 .name = "data",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800827 .groups = wmi_data_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800828 .release = wmi_dev_release,
829};
830
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700831static int wmi_create_device(struct device *wmi_bus_dev,
Andy Lutomirski844af952015-11-24 19:49:23 -0800832 const struct guid_block *gblock,
Andy Lutomirski7f5809b2015-11-19 14:44:46 -0800833 struct wmi_block *wblock,
834 struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500835{
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700836 struct acpi_device_info *info;
837 char method[5];
838 int result;
839
840 if (gblock->flags & ACPI_WMI_EVENT) {
841 wblock->dev.dev.type = &wmi_type_event;
842 goto out_init;
843 }
844
845 if (gblock->flags & ACPI_WMI_METHOD) {
846 wblock->dev.dev.type = &wmi_type_method;
847 goto out_init;
848 }
849
850 /*
851 * Data Block Query Control Method (WQxx by convention) is
852 * required per the WMI documentation. If it is not present,
853 * we ignore this data block.
854 */
855 strcpy(method, "WQ");
856 strncat(method, wblock->gblock.object_id, 2);
857 result = get_subobj_info(device->handle, method, &info);
858
859 if (result) {
860 dev_warn(wmi_bus_dev,
861 "%s data block query control method not found",
862 method);
863 return result;
864 }
865
866 wblock->dev.dev.type = &wmi_type_data;
867
868 /*
869 * The Microsoft documentation specifically states:
870 *
871 * Data blocks registered with only a single instance
872 * can ignore the parameter.
873 *
874 * ACPICA will get mad at us if we call the method with the wrong number
875 * of arguments, so check what our method expects. (On some Dell
876 * laptops, WQxx may not be a method at all.)
877 */
878 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
879 wblock->read_takes_no_args = true;
880
881 kfree(info);
882
883 strcpy(method, "WS");
884 strncat(method, wblock->gblock.object_id, 2);
885 result = get_subobj_info(device->handle, method, NULL);
886
887 if (result == 0)
888 wblock->dev.setable = true;
889
890 out_init:
Andy Lutomirski844af952015-11-24 19:49:23 -0800891 wblock->dev.dev.bus = &wmi_bus_type;
892 wblock->dev.dev.parent = wmi_bus_dev;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700893
Andy Lutomirski844af952015-11-24 19:49:23 -0800894 dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700895
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700896 device_initialize(&wblock->dev.dev);
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700897
898 return 0;
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500899}
900
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800901static void wmi_free_devices(struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500902{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700903 struct wmi_block *wblock, *next;
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500904
905 /* Delete devices for all the GUIDs */
Dmitry Torokhov023b9562011-09-07 15:00:02 -0700906 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800907 if (wblock->acpi_device == device) {
908 list_del(&wblock->list);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700909 device_unregister(&wblock->dev.dev);
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800910 }
Dmitry Torokhov023b9562011-09-07 15:00:02 -0700911 }
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500912}
913
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800914static bool guid_already_parsed(struct acpi_device *device,
915 const u8 *guid)
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000916{
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000917 struct wmi_block *wblock;
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000918
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800919 list_for_each_entry(wblock, &wmi_block_list, list) {
920 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
921 /*
922 * Because we historically didn't track the relationship
923 * between GUIDs and ACPI nodes, we don't know whether
924 * we need to suppress GUIDs that are unique on a
925 * given node but duplicated across nodes.
926 */
927 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
928 guid, dev_name(&wblock->acpi_device->dev));
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000929 return true;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800930 }
931 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700932
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000933 return false;
934}
935
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500936/*
Carlos Corbachobff431e2008-02-05 02:17:04 +0000937 * Parse the _WDG method for the GUID data blocks
938 */
Andy Lutomirski844af952015-11-24 19:49:23 -0800939static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000940{
941 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
Dmitry Torokhov37830662010-08-26 00:15:09 -0700942 const struct guid_block *gblock;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700943 struct wmi_block *wblock, *next;
944 union acpi_object *obj;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000945 acpi_status status;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700946 int retval = 0;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000947 u32 i, total;
948
Andy Lutomirski7f5809b2015-11-19 14:44:46 -0800949 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000950 if (ACPI_FAILURE(status))
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700951 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000952
953 obj = (union acpi_object *) out.pointer;
Dmitry Torokhov3d2c63e2010-08-26 00:15:03 -0700954 if (!obj)
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700955 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000956
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -0700957 if (obj->type != ACPI_TYPE_BUFFER) {
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700958 retval = -ENXIO;
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -0700959 goto out_free_pointer;
960 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000961
Dmitry Torokhov37830662010-08-26 00:15:09 -0700962 gblock = (const struct guid_block *)obj->buffer.pointer;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000963 total = obj->buffer.length / sizeof(struct guid_block);
964
Carlos Corbachobff431e2008-02-05 02:17:04 +0000965 for (i = 0; i < total; i++) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200966 if (debug_dump_wdg)
967 wmi_dump_wdg(&gblock[i]);
968
Andy Lutomirskia1c31bcd2015-11-25 08:24:42 -0800969 /*
970 * Some WMI devices, like those for nVidia hooks, have a
971 * duplicate GUID. It's not clear what we should do in this
972 * case yet, so for now, we'll just ignore the duplicate
973 * for device creation.
974 */
975 if (guid_already_parsed(device, gblock[i].guid))
976 continue;
977
Colin King58f64252010-11-19 15:40:02 +0000978 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700979 if (!wblock) {
980 retval = -ENOMEM;
981 break;
982 }
Colin King58f64252010-11-19 15:40:02 +0000983
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800984 wblock->acpi_device = device;
Colin King58f64252010-11-19 15:40:02 +0000985 wblock->gblock = gblock[i];
986
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700987 retval = wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
988 if (retval) {
989 kfree(wblock);
990 continue;
991 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000992
Colin King58f64252010-11-19 15:40:02 +0000993 list_add_tail(&wblock->list, &wmi_block_list);
994
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200995 if (debug_event) {
996 wblock->handler = wmi_notify_debug;
Dmitry Torokhov2d5ab552010-08-26 00:14:48 -0700997 wmi_method_enable(wblock, 1);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200998 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000999 }
1000
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001001 /*
1002 * Now that all of the devices are created, add them to the
1003 * device tree and probe subdrivers.
1004 */
1005 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1006 if (wblock->acpi_device != device)
1007 continue;
1008
1009 retval = device_add(&wblock->dev.dev);
1010 if (retval) {
1011 dev_err(wmi_bus_dev, "failed to register %pULL\n",
1012 wblock->gblock.guid);
1013 if (debug_event)
1014 wmi_method_enable(wblock, 0);
1015 list_del(&wblock->list);
1016 put_device(&wblock->dev.dev);
1017 }
1018 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001019
Axel Lina5167c52010-06-03 11:45:45 +08001020out_free_pointer:
1021 kfree(out.pointer);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001022 return retval;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001023}
1024
1025/*
1026 * WMI can have EmbeddedControl access regions. In which case, we just want to
1027 * hand these off to the EC driver.
1028 */
1029static acpi_status
1030acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
Lin Ming439913f2010-01-28 10:53:19 +08001031 u32 bits, u64 *value,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001032 void *handler_context, void *region_context)
1033{
1034 int result = 0, i = 0;
1035 u8 temp = 0;
1036
1037 if ((address > 0xFF) || !value)
1038 return AE_BAD_PARAMETER;
1039
1040 if (function != ACPI_READ && function != ACPI_WRITE)
1041 return AE_BAD_PARAMETER;
1042
1043 if (bits != 8)
1044 return AE_BAD_PARAMETER;
1045
1046 if (function == ACPI_READ) {
1047 result = ec_read(address, &temp);
Lin Ming439913f2010-01-28 10:53:19 +08001048 (*value) |= ((u64)temp) << i;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001049 } else {
1050 temp = 0xff & ((*value) >> i);
1051 result = ec_write(address, temp);
1052 }
1053
1054 switch (result) {
1055 case -EINVAL:
1056 return AE_BAD_PARAMETER;
1057 break;
1058 case -ENODEV:
1059 return AE_NOT_FOUND;
1060 break;
1061 case -ETIME:
1062 return AE_TIME;
1063 break;
1064 default:
1065 return AE_OK;
1066 }
1067}
1068
Andy Lutomirski1686f542015-11-25 17:33:25 -08001069static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1070 void *context)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001071{
1072 struct guid_block *block;
1073 struct wmi_block *wblock;
1074 struct list_head *p;
Andy Lutomirski1686f542015-11-25 17:33:25 -08001075 bool found_it = false;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001076
Dmitry Torokhov762e1a22010-08-26 00:15:14 -07001077 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +00001078 wblock = list_entry(p, struct wmi_block, list);
1079 block = &wblock->gblock;
1080
Andy Lutomirski1686f542015-11-25 17:33:25 -08001081 if (wblock->acpi_device->handle == handle &&
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001082 (block->flags & ACPI_WMI_EVENT) &&
Andy Lutomirski1686f542015-11-25 17:33:25 -08001083 (block->notify_id == event))
1084 {
1085 found_it = true;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001086 break;
1087 }
1088 }
Andy Lutomirski1686f542015-11-25 17:33:25 -08001089
1090 if (!found_it)
1091 return;
1092
1093 /* If a driver is bound, then notify the driver. */
1094 if (wblock->dev.dev.driver) {
1095 struct wmi_driver *driver;
1096 struct acpi_object_list input;
1097 union acpi_object params[1];
1098 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1099 acpi_status status;
1100
1101 driver = container_of(wblock->dev.dev.driver,
1102 struct wmi_driver, driver);
1103
1104 input.count = 1;
1105 input.pointer = params;
1106 params[0].type = ACPI_TYPE_INTEGER;
1107 params[0].integer.value = event;
1108
1109 status = acpi_evaluate_object(wblock->acpi_device->handle,
1110 "_WED", &input, &evdata);
1111 if (ACPI_FAILURE(status)) {
1112 dev_warn(&wblock->dev.dev,
1113 "failed to get event data\n");
1114 return;
1115 }
1116
1117 if (driver->notify)
1118 driver->notify(&wblock->dev,
1119 (union acpi_object *)evdata.pointer);
1120
1121 kfree(evdata.pointer);
1122 } else if (wblock->handler) {
1123 /* Legacy handler */
1124 wblock->handler(event, wblock->handler_data);
1125 }
1126
1127 if (debug_event) {
1128 pr_info("DEBUG Event GUID: %pUL\n",
1129 wblock->gblock.guid);
1130 }
1131
1132 acpi_bus_generate_netlink_event(
1133 wblock->acpi_device->pnp.device_class,
1134 dev_name(&wblock->dev.dev),
1135 event, 0);
1136
Carlos Corbachobff431e2008-02-05 02:17:04 +00001137}
1138
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001139static int acpi_wmi_remove(struct platform_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001140{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001141 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1142
1143 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001144 acpi_wmi_notify_handler);
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001145 acpi_remove_address_space_handler(acpi_device->handle,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001146 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001147 wmi_free_devices(acpi_device);
1148 device_unregister((struct device *)dev_get_drvdata(&device->dev));
Carlos Corbachobff431e2008-02-05 02:17:04 +00001149
1150 return 0;
1151}
1152
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001153static int acpi_wmi_probe(struct platform_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001154{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001155 struct acpi_device *acpi_device;
Andy Lutomirski844af952015-11-24 19:49:23 -08001156 struct device *wmi_bus_dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001157 acpi_status status;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001158 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001159
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001160 acpi_device = ACPI_COMPANION(&device->dev);
1161 if (!acpi_device) {
1162 dev_err(&device->dev, "ACPI companion is missing\n");
1163 return -ENODEV;
1164 }
1165
1166 status = acpi_install_address_space_handler(acpi_device->handle,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001167 ACPI_ADR_SPACE_EC,
1168 &acpi_wmi_ec_space_handler,
1169 NULL, NULL);
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001170 if (ACPI_FAILURE(status)) {
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001171 dev_err(&device->dev, "Error installing EC region handler\n");
Carlos Corbachobff431e2008-02-05 02:17:04 +00001172 return -ENODEV;
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001173 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001174
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001175 status = acpi_install_notify_handler(acpi_device->handle,
1176 ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001177 acpi_wmi_notify_handler,
1178 NULL);
1179 if (ACPI_FAILURE(status)) {
1180 dev_err(&device->dev, "Error installing notify handler\n");
1181 error = -ENODEV;
1182 goto err_remove_ec_handler;
1183 }
1184
Andy Lutomirski844af952015-11-24 19:49:23 -08001185 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1186 NULL, "wmi_bus-%s", dev_name(&device->dev));
1187 if (IS_ERR(wmi_bus_dev)) {
1188 error = PTR_ERR(wmi_bus_dev);
Andy Lutomirski1686f542015-11-25 17:33:25 -08001189 goto err_remove_notify_handler;
Andy Lutomirski844af952015-11-24 19:49:23 -08001190 }
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001191 dev_set_drvdata(&device->dev, wmi_bus_dev);
Andy Lutomirski844af952015-11-24 19:49:23 -08001192
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001193 error = parse_wdg(wmi_bus_dev, acpi_device);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001194 if (error) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001195 pr_err("Failed to parse WDG method\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001196 goto err_remove_busdev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001197 }
1198
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001199 return 0;
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001200
Andy Lutomirski844af952015-11-24 19:49:23 -08001201err_remove_busdev:
1202 device_unregister(wmi_bus_dev);
1203
Andy Lutomirski1686f542015-11-25 17:33:25 -08001204err_remove_notify_handler:
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001205 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001206 acpi_wmi_notify_handler);
1207
1208err_remove_ec_handler:
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001209 acpi_remove_address_space_handler(acpi_device->handle,
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001210 ACPI_ADR_SPACE_EC,
1211 &acpi_wmi_ec_space_handler);
1212
1213 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001214}
1215
Andy Lutomirski844af952015-11-24 19:49:23 -08001216int __must_check __wmi_driver_register(struct wmi_driver *driver,
1217 struct module *owner)
1218{
1219 driver->driver.owner = owner;
1220 driver->driver.bus = &wmi_bus_type;
1221
1222 return driver_register(&driver->driver);
1223}
1224EXPORT_SYMBOL(__wmi_driver_register);
1225
1226void wmi_driver_unregister(struct wmi_driver *driver)
1227{
1228 driver_unregister(&driver->driver);
1229}
1230EXPORT_SYMBOL(wmi_driver_unregister);
1231
Carlos Corbachobff431e2008-02-05 02:17:04 +00001232static int __init acpi_wmi_init(void)
1233{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001234 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001235
1236 if (acpi_disabled)
1237 return -ENODEV;
1238
Andy Lutomirski844af952015-11-24 19:49:23 -08001239 error = class_register(&wmi_bus_class);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001240 if (error)
1241 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001242
Andy Lutomirski844af952015-11-24 19:49:23 -08001243 error = bus_register(&wmi_bus_type);
1244 if (error)
1245 goto err_unreg_class;
1246
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001247 error = platform_driver_register(&acpi_wmi_driver);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001248 if (error) {
1249 pr_err("Error loading mapper\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001250 goto err_unreg_bus;
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001251 }
1252
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001253 return 0;
Andy Lutomirski844af952015-11-24 19:49:23 -08001254
1255err_unreg_class:
1256 class_unregister(&wmi_bus_class);
1257
1258err_unreg_bus:
1259 bus_unregister(&wmi_bus_type);
1260
1261 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001262}
1263
1264static void __exit acpi_wmi_exit(void)
1265{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001266 platform_driver_unregister(&acpi_wmi_driver);
Andy Lutomirski844af952015-11-24 19:49:23 -08001267 class_unregister(&wmi_bus_class);
1268 bus_unregister(&wmi_bus_type);
Carlos Corbachobff431e2008-02-05 02:17:04 +00001269}
1270
1271subsys_initcall(acpi_wmi_init);
1272module_exit(acpi_wmi_exit);