blob: 4395d83ba9cc31526f6b62f811b14692832aa306 [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 Lutomirski844af952015-11-24 19:49:23 -080040#include <linux/wmi.h>
Andy Shevchenko538d7eb2016-05-20 17:01:30 -070041#include <linux/uuid.h>
Carlos Corbachobff431e2008-02-05 02:17:04 +000042
43ACPI_MODULE_NAME("wmi");
44MODULE_AUTHOR("Carlos Corbacho");
45MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
46MODULE_LICENSE("GPL");
47
Dmitry Torokhov762e1a22010-08-26 00:15:14 -070048static LIST_HEAD(wmi_block_list);
Carlos Corbachobff431e2008-02-05 02:17:04 +000049
50struct guid_block {
51 char guid[16];
52 union {
53 char object_id[2];
54 struct {
55 unsigned char notify_id;
56 unsigned char reserved;
57 };
58 };
59 u8 instance_count;
60 u8 flags;
61};
62
63struct wmi_block {
Andy Lutomirski844af952015-11-24 19:49:23 -080064 struct wmi_device dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +000065 struct list_head list;
66 struct guid_block gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -080067 struct acpi_device *acpi_device;
Carlos Corbachobff431e2008-02-05 02:17:04 +000068 wmi_notify_handler handler;
69 void *handler_data;
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -080070
71 bool read_takes_no_args; /* only defined if readable */
Carlos Corbachobff431e2008-02-05 02:17:04 +000072};
73
Carlos Corbachobff431e2008-02-05 02:17:04 +000074
75/*
76 * If the GUID data block is marked as expensive, we must enable and
77 * explicitily disable data collection.
78 */
79#define ACPI_WMI_EXPENSIVE 0x1
80#define ACPI_WMI_METHOD 0x2 /* GUID is a method */
81#define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
82#define ACPI_WMI_EVENT 0x8 /* GUID is an event */
83
Rusty Russell90ab5ee2012-01-13 09:32:20 +103084static bool debug_event;
Thomas Renningerfc3155b2010-05-03 15:30:15 +020085module_param(debug_event, bool, 0444);
86MODULE_PARM_DESC(debug_event,
87 "Log WMI Events [0/1]");
88
Rusty Russell90ab5ee2012-01-13 09:32:20 +103089static bool debug_dump_wdg;
Thomas Renningera929aae2010-05-03 15:30:17 +020090module_param(debug_dump_wdg, bool, 0444);
91MODULE_PARM_DESC(debug_dump_wdg,
92 "Dump available WMI interfaces [0/1]");
93
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010094static int acpi_wmi_remove(struct acpi_device *device);
Carlos Corbachobff431e2008-02-05 02:17:04 +000095static int acpi_wmi_add(struct acpi_device *device);
96
97static const struct acpi_device_id wmi_device_ids[] = {
98 {"PNP0C14", 0},
99 {"pnp0c14", 0},
100 {"", 0},
101};
102MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
103
104static struct acpi_driver acpi_wmi_driver = {
Andy Lutomirski844af952015-11-24 19:49:23 -0800105 .name = "acpi-wmi",
106 .owner = THIS_MODULE,
Carlos Corbachobff431e2008-02-05 02:17:04 +0000107 .ids = wmi_device_ids,
108 .ops = {
109 .add = acpi_wmi_add,
110 .remove = acpi_wmi_remove,
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700111 },
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
247/**
248 * wmi_query_block - Return contents of a WMI block
249 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
250 * @instance: Instance index
251 * &out: Empty buffer to return the contents of the data block to
252 *
253 * Return the contents of an ACPI-WMI data block to a buffer
254 */
255acpi_status wmi_query_block(const char *guid_string, u8 instance,
256struct acpi_buffer *out)
257{
258 struct guid_block *block = NULL;
259 struct wmi_block *wblock = NULL;
Zhang Rui54f14c22013-09-03 08:32:07 +0800260 acpi_handle handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000261 acpi_status status, wc_status = AE_ERROR;
Zhang Rui8122ab662013-09-03 08:31:57 +0800262 struct acpi_object_list input;
263 union acpi_object wq_params[1];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700264 char method[5];
265 char wc_method[5] = "WC";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000266
267 if (!guid_string || !out)
268 return AE_BAD_PARAMETER;
269
270 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800271 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000272
273 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800274 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000275
276 if (block->instance_count < instance)
277 return AE_BAD_PARAMETER;
278
279 /* Check GUID is a data block */
280 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800281 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000282
283 input.count = 1;
284 input.pointer = wq_params;
285 wq_params[0].type = ACPI_TYPE_INTEGER;
286 wq_params[0].integer.value = instance;
287
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800288 if (instance == 0 && wblock->read_takes_no_args)
289 input.count = 0;
290
Carlos Corbachobff431e2008-02-05 02:17:04 +0000291 /*
292 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
293 * enable collection.
294 */
295 if (block->flags & ACPI_WMI_EXPENSIVE) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000296 strncat(wc_method, block->object_id, 2);
297
298 /*
299 * Some GUIDs break the specification by declaring themselves
300 * expensive, but have no corresponding WCxx method. So we
301 * should not fail if this happens.
302 */
Zhang Rui54f14c22013-09-03 08:32:07 +0800303 if (acpi_has_method(handle, wc_method))
Zhang Rui8122ab662013-09-03 08:31:57 +0800304 wc_status = acpi_execute_simple_method(handle,
305 wc_method, 1);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000306 }
307
308 strcpy(method, "WQ");
309 strncat(method, block->object_id, 2);
310
Carlos Corbachodab36ad2008-08-02 17:28:45 +0100311 status = acpi_evaluate_object(handle, method, &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000312
313 /*
314 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
315 * the WQxx method failed - we should disable collection anyway.
316 */
Carlos Corbachoa527f2d2008-02-24 13:34:34 +0000317 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
Zhang Rui8122ab662013-09-03 08:31:57 +0800318 status = acpi_execute_simple_method(handle, wc_method, 0);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000319 }
320
321 return status;
322}
323EXPORT_SYMBOL_GPL(wmi_query_block);
324
325/**
326 * wmi_set_block - Write to a WMI block
327 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
328 * @instance: Instance index
329 * &in: Buffer containing new values for the data block
330 *
331 * Write the contents of the input buffer to an ACPI-WMI data block
332 */
333acpi_status wmi_set_block(const char *guid_string, u8 instance,
334const struct acpi_buffer *in)
335{
336 struct guid_block *block = NULL;
337 struct wmi_block *wblock = NULL;
338 acpi_handle handle;
339 struct acpi_object_list input;
340 union acpi_object params[2];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700341 char method[5] = "WS";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000342
343 if (!guid_string || !in)
344 return AE_BAD_DATA;
345
346 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800347 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000348
349 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800350 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000351
352 if (block->instance_count < instance)
353 return AE_BAD_PARAMETER;
354
355 /* Check GUID is a data block */
356 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800357 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000358
359 input.count = 2;
360 input.pointer = params;
361 params[0].type = ACPI_TYPE_INTEGER;
362 params[0].integer.value = instance;
363
364 if (block->flags & ACPI_WMI_STRING) {
365 params[1].type = ACPI_TYPE_STRING;
366 } else {
367 params[1].type = ACPI_TYPE_BUFFER;
368 }
369 params[1].buffer.length = in->length;
370 params[1].buffer.pointer = in->pointer;
371
372 strncat(method, block->object_id, 2);
373
374 return acpi_evaluate_object(handle, method, &input, NULL);
375}
376EXPORT_SYMBOL_GPL(wmi_set_block);
377
Dmitry Torokhov37830662010-08-26 00:15:09 -0700378static void wmi_dump_wdg(const struct guid_block *g)
Thomas Renningera929aae2010-05-03 15:30:17 +0200379{
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200380 pr_info("%pUL:\n", g->guid);
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700381 pr_info("\tobject_id: %c%c\n", g->object_id[0], g->object_id[1]);
382 pr_info("\tnotify_id: %02X\n", g->notify_id);
383 pr_info("\treserved: %02X\n", g->reserved);
384 pr_info("\tinstance_count: %d\n", g->instance_count);
Joe Perchesdd8e9082011-03-29 15:21:53 -0700385 pr_info("\tflags: %#x", g->flags);
Thomas Renningera929aae2010-05-03 15:30:17 +0200386 if (g->flags) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200387 if (g->flags & ACPI_WMI_EXPENSIVE)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700388 pr_cont(" ACPI_WMI_EXPENSIVE");
Thomas Renningera929aae2010-05-03 15:30:17 +0200389 if (g->flags & ACPI_WMI_METHOD)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700390 pr_cont(" ACPI_WMI_METHOD");
Thomas Renningera929aae2010-05-03 15:30:17 +0200391 if (g->flags & ACPI_WMI_STRING)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700392 pr_cont(" ACPI_WMI_STRING");
Thomas Renningera929aae2010-05-03 15:30:17 +0200393 if (g->flags & ACPI_WMI_EVENT)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700394 pr_cont(" ACPI_WMI_EVENT");
Thomas Renningera929aae2010-05-03 15:30:17 +0200395 }
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700396 pr_cont("\n");
Thomas Renningera929aae2010-05-03 15:30:17 +0200397
398}
399
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200400static void wmi_notify_debug(u32 value, void *context)
401{
402 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
403 union acpi_object *obj;
Axel Lin14926162010-06-28 09:30:45 +0800404 acpi_status status;
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200405
Axel Lin14926162010-06-28 09:30:45 +0800406 status = wmi_get_event_data(value, &response);
407 if (status != AE_OK) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700408 pr_info("bad event status 0x%x\n", status);
Axel Lin14926162010-06-28 09:30:45 +0800409 return;
410 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200411
412 obj = (union acpi_object *)response.pointer;
413
414 if (!obj)
415 return;
416
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700417 pr_info("DEBUG Event ");
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200418 switch(obj->type) {
419 case ACPI_TYPE_BUFFER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700420 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200421 break;
422 case ACPI_TYPE_STRING:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700423 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200424 break;
425 case ACPI_TYPE_INTEGER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700426 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200427 break;
428 case ACPI_TYPE_PACKAGE:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700429 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200430 break;
431 default:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700432 pr_cont("object type 0x%X\n", obj->type);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200433 }
Axel Lin14926162010-06-28 09:30:45 +0800434 kfree(obj);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200435}
436
Carlos Corbachobff431e2008-02-05 02:17:04 +0000437/**
438 * wmi_install_notify_handler - Register handler for WMI events
439 * @handler: Function to handle notifications
440 * @data: Data to be returned to handler when event is fired
441 *
442 * Register a handler for events sent to the ACPI-WMI mapper device.
443 */
444acpi_status wmi_install_notify_handler(const char *guid,
445wmi_notify_handler handler, void *data)
446{
447 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000448 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700449 uuid_le guid_input;
Colin King58f64252010-11-19 15:40:02 +0000450 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000451
452 if (!guid || !handler)
453 return AE_BAD_PARAMETER;
454
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700455 if (uuid_le_to_bin(guid, &guid_input))
456 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000457
Colin King58f64252010-11-19 15:40:02 +0000458 list_for_each(p, &wmi_block_list) {
459 acpi_status wmi_status;
460 block = list_entry(p, struct wmi_block, list);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000461
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700462 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000463 if (block->handler &&
464 block->handler != wmi_notify_debug)
465 return AE_ALREADY_ACQUIRED;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000466
Colin King58f64252010-11-19 15:40:02 +0000467 block->handler = handler;
468 block->handler_data = data;
469
470 wmi_status = wmi_method_enable(block, 1);
471 if ((wmi_status != AE_OK) ||
472 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
473 status = wmi_status;
474 }
475 }
Matthew Garretta66bfa72008-10-08 21:40:32 +0100476
477 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000478}
479EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
480
481/**
482 * wmi_uninstall_notify_handler - Unregister handler for WMI events
483 *
484 * Unregister handler for events sent to the ACPI-WMI mapper device.
485 */
486acpi_status wmi_remove_notify_handler(const char *guid)
487{
488 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000489 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700490 uuid_le guid_input;
Colin King58f64252010-11-19 15:40:02 +0000491 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000492
493 if (!guid)
494 return AE_BAD_PARAMETER;
495
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700496 if (uuid_le_to_bin(guid, &guid_input))
497 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000498
Colin King58f64252010-11-19 15:40:02 +0000499 list_for_each(p, &wmi_block_list) {
500 acpi_status wmi_status;
501 block = list_entry(p, struct wmi_block, list);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000502
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700503 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000504 if (!block->handler ||
505 block->handler == wmi_notify_debug)
506 return AE_NULL_ENTRY;
507
508 if (debug_event) {
509 block->handler = wmi_notify_debug;
510 status = AE_OK;
511 } else {
512 wmi_status = wmi_method_enable(block, 0);
513 block->handler = NULL;
514 block->handler_data = NULL;
515 if ((wmi_status != AE_OK) ||
516 ((wmi_status == AE_OK) &&
517 (status == AE_NOT_EXIST)))
518 status = wmi_status;
519 }
520 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200521 }
Colin King58f64252010-11-19 15:40:02 +0000522
Matthew Garretta66bfa72008-10-08 21:40:32 +0100523 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000524}
525EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
526
527/**
528 * wmi_get_event_data - Get WMI data associated with an event
529 *
Anisse Astier3e9b9882009-12-04 10:10:09 +0100530 * @event: Event to find
531 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
Carlos Corbachobff431e2008-02-05 02:17:04 +0000532 *
533 * Returns extra data associated with an event in WMI.
534 */
535acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
536{
537 struct acpi_object_list input;
538 union acpi_object params[1];
539 struct guid_block *gblock;
540 struct wmi_block *wblock;
541 struct list_head *p;
542
543 input.count = 1;
544 input.pointer = params;
545 params[0].type = ACPI_TYPE_INTEGER;
546 params[0].integer.value = event;
547
Dmitry Torokhov762e1a22010-08-26 00:15:14 -0700548 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000549 wblock = list_entry(p, struct wmi_block, list);
550 gblock = &wblock->gblock;
551
552 if ((gblock->flags & ACPI_WMI_EVENT) &&
553 (gblock->notify_id == event))
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800554 return acpi_evaluate_object(wblock->acpi_device->handle,
555 "_WED", &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000556 }
557
558 return AE_NOT_FOUND;
559}
560EXPORT_SYMBOL_GPL(wmi_get_event_data);
561
562/**
563 * wmi_has_guid - Check if a GUID is available
564 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
565 *
566 * Check if a given GUID is defined by _WDG
567 */
568bool wmi_has_guid(const char *guid_string)
569{
570 return find_guid(guid_string, NULL);
571}
572EXPORT_SYMBOL_GPL(wmi_has_guid);
573
Andy Lutomirski844af952015-11-24 19:49:23 -0800574static struct wmi_block *dev_to_wblock(struct device *dev)
575{
576 return container_of(dev, struct wmi_block, dev.dev);
577}
578
579static struct wmi_device *dev_to_wdev(struct device *dev)
580{
581 return container_of(dev, struct wmi_device, dev);
582}
583
Carlos Corbachobff431e2008-02-05 02:17:04 +0000584/*
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500585 * sysfs interface
586 */
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700587static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500588 char *buf)
589{
Andy Lutomirski844af952015-11-24 19:49:23 -0800590 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500591
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200592 return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500593}
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700594static DEVICE_ATTR_RO(modalias);
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700595
Andy Lutomirski844af952015-11-24 19:49:23 -0800596static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
597 char *buf)
598{
599 struct wmi_block *wblock = dev_to_wblock(dev);
600
601 return sprintf(buf, "%pUL\n", wblock->gblock.guid);
602}
603static DEVICE_ATTR_RO(guid);
604
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800605static ssize_t instance_count_show(struct device *dev,
606 struct device_attribute *attr, char *buf)
607{
608 struct wmi_block *wblock = dev_to_wblock(dev);
609
610 return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
611}
612static DEVICE_ATTR_RO(instance_count);
613
614static ssize_t expensive_show(struct device *dev,
615 struct device_attribute *attr, char *buf)
616{
617 struct wmi_block *wblock = dev_to_wblock(dev);
618
619 return sprintf(buf, "%d\n",
620 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
621}
622static DEVICE_ATTR_RO(expensive);
623
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700624static struct attribute *wmi_attrs[] = {
625 &dev_attr_modalias.attr,
Andy Lutomirski844af952015-11-24 19:49:23 -0800626 &dev_attr_guid.attr,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800627 &dev_attr_instance_count.attr,
628 &dev_attr_expensive.attr,
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700629 NULL,
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700630};
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700631ATTRIBUTE_GROUPS(wmi);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500632
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800633static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
634 char *buf)
635{
636 struct wmi_block *wblock = dev_to_wblock(dev);
637
638 return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
639}
640static DEVICE_ATTR_RO(notify_id);
641
642static struct attribute *wmi_event_attrs[] = {
643 &dev_attr_notify_id.attr,
644 NULL,
645};
646ATTRIBUTE_GROUPS(wmi_event);
647
648static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
649 char *buf)
650{
651 struct wmi_block *wblock = dev_to_wblock(dev);
652
653 return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
654 wblock->gblock.object_id[1]);
655}
656static DEVICE_ATTR_RO(object_id);
657
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800658static ssize_t readable_show(struct device *dev, struct device_attribute *attr,
659 char *buf)
660{
661 struct wmi_device *wdev = dev_to_wdev(dev);
662
663 return sprintf(buf, "%d\n", (int)wdev->readable);
664}
665static DEVICE_ATTR_RO(readable);
666
667static ssize_t writeable_show(struct device *dev, struct device_attribute *attr,
668 char *buf)
669{
670 struct wmi_device *wdev = dev_to_wdev(dev);
671
672 return sprintf(buf, "%d\n", (int)wdev->writeable);
673}
674static DEVICE_ATTR_RO(writeable);
675
676static struct attribute *wmi_data_attrs[] = {
677 &dev_attr_object_id.attr,
678 &dev_attr_readable.attr,
679 &dev_attr_writeable.attr,
680 NULL,
681};
682ATTRIBUTE_GROUPS(wmi_data);
683
684static struct attribute *wmi_method_attrs[] = {
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800685 &dev_attr_object_id.attr,
686 NULL,
687};
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800688ATTRIBUTE_GROUPS(wmi_method);
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800689
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500690static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
691{
Andy Lutomirski844af952015-11-24 19:49:23 -0800692 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500693
Andy Lutomirski844af952015-11-24 19:49:23 -0800694 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500695 return -ENOMEM;
696
Andy Lutomirski844af952015-11-24 19:49:23 -0800697 if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500698 return -ENOMEM;
699
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500700 return 0;
701}
702
Andy Lutomirski844af952015-11-24 19:49:23 -0800703static void wmi_dev_release(struct device *dev)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500704{
Andy Lutomirski844af952015-11-24 19:49:23 -0800705 struct wmi_block *wblock = dev_to_wblock(dev);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700706
Andy Lutomirski844af952015-11-24 19:49:23 -0800707 kfree(wblock);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500708}
709
Andy Lutomirski844af952015-11-24 19:49:23 -0800710static int wmi_dev_match(struct device *dev, struct device_driver *driver)
711{
712 struct wmi_driver *wmi_driver =
713 container_of(driver, struct wmi_driver, driver);
714 struct wmi_block *wblock = dev_to_wblock(dev);
715 const struct wmi_device_id *id = wmi_driver->id_table;
716
717 while (id->guid_string) {
718 uuid_le driver_guid;
719
720 if (WARN_ON(uuid_le_to_bin(id->guid_string, &driver_guid)))
721 continue;
722 if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
723 return 1;
724
725 id++;
726 }
727
728 return 0;
729}
730
731static int wmi_dev_probe(struct device *dev)
732{
733 struct wmi_block *wblock = dev_to_wblock(dev);
734 struct wmi_driver *wdriver =
735 container_of(dev->driver, struct wmi_driver, driver);
736 int ret = 0;
737
738 if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
739 dev_warn(dev, "failed to enable device -- probing anyway\n");
740
741 if (wdriver->probe) {
742 ret = wdriver->probe(dev_to_wdev(dev));
743 if (ret != 0 && ACPI_FAILURE(wmi_method_enable(wblock, 0)))
744 dev_warn(dev, "failed to disable device\n");
745 }
746
747 return ret;
748}
749
750static int wmi_dev_remove(struct device *dev)
751{
752 struct wmi_block *wblock = dev_to_wblock(dev);
753 struct wmi_driver *wdriver =
754 container_of(dev->driver, struct wmi_driver, driver);
755 int ret = 0;
756
757 if (wdriver->remove)
758 ret = wdriver->remove(dev_to_wdev(dev));
759
760 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
761 dev_warn(dev, "failed to disable device\n");
762
763 return ret;
764}
765
766static struct class wmi_bus_class = {
767 .name = "wmi_bus",
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500768};
769
Andy Lutomirski844af952015-11-24 19:49:23 -0800770static struct bus_type wmi_bus_type = {
771 .name = "wmi",
772 .dev_groups = wmi_groups,
773 .match = wmi_dev_match,
774 .uevent = wmi_dev_uevent,
775 .probe = wmi_dev_probe,
776 .remove = wmi_dev_remove,
777};
778
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800779static struct device_type wmi_type_event = {
780 .name = "event",
781 .groups = wmi_event_groups,
782 .release = wmi_dev_release,
783};
784
785static struct device_type wmi_type_method = {
786 .name = "method",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800787 .groups = wmi_method_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800788 .release = wmi_dev_release,
789};
790
791static struct device_type wmi_type_data = {
792 .name = "data",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800793 .groups = wmi_data_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800794 .release = wmi_dev_release,
795};
796
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700797static void wmi_create_device(struct device *wmi_bus_dev,
Andy Lutomirski844af952015-11-24 19:49:23 -0800798 const struct guid_block *gblock,
Andy Lutomirski7f5809b2015-11-19 14:44:46 -0800799 struct wmi_block *wblock,
800 struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500801{
Andy Lutomirski844af952015-11-24 19:49:23 -0800802 wblock->dev.dev.bus = &wmi_bus_type;
803 wblock->dev.dev.parent = wmi_bus_dev;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700804
Andy Lutomirski844af952015-11-24 19:49:23 -0800805 dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700806
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800807 if (gblock->flags & ACPI_WMI_EVENT) {
808 wblock->dev.dev.type = &wmi_type_event;
809 } else if (gblock->flags & ACPI_WMI_METHOD) {
810 wblock->dev.dev.type = &wmi_type_method;
811 } else {
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800812 struct acpi_device_info *info;
813 char method[5];
814 int result;
815
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800816 wblock->dev.dev.type = &wmi_type_data;
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800817
818 strcpy(method, "WQ");
819 strncat(method, wblock->gblock.object_id, 2);
820 result = get_subobj_info(device->handle, method, &info);
821
822 if (result == 0) {
823 wblock->dev.readable = true;
824
825 /*
826 * The Microsoft documentation specifically states:
827 *
828 * Data blocks registered with only a single instance
829 * can ignore the parameter.
830 *
831 * ACPICA will get mad at us if we call the method
832 * with the wrong number of arguments, so check what
833 * our method expects. (On some Dell laptops, WQxx
834 * may not be a method at all.)
835 */
836 if (info->type != ACPI_TYPE_METHOD ||
837 info->param_count == 0)
838 wblock->read_takes_no_args = true;
839
840 kfree(info);
841 }
842
843 strcpy(method, "WS");
844 strncat(method, wblock->gblock.object_id, 2);
845 result = get_subobj_info(device->handle, method, NULL);
846
847 if (result == 0) {
848 wblock->dev.writeable = true;
849 }
850
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800851 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700852
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700853 device_initialize(&wblock->dev.dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500854}
855
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800856static void wmi_free_devices(struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500857{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700858 struct wmi_block *wblock, *next;
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500859
860 /* Delete devices for all the GUIDs */
Dmitry Torokhov023b9562011-09-07 15:00:02 -0700861 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800862 if (wblock->acpi_device == device) {
863 list_del(&wblock->list);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700864 device_unregister(&wblock->dev.dev);
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800865 }
Dmitry Torokhov023b9562011-09-07 15:00:02 -0700866 }
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500867}
868
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800869static bool guid_already_parsed(struct acpi_device *device,
870 const u8 *guid)
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000871{
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000872 struct wmi_block *wblock;
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000873
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800874 list_for_each_entry(wblock, &wmi_block_list, list) {
875 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
876 /*
877 * Because we historically didn't track the relationship
878 * between GUIDs and ACPI nodes, we don't know whether
879 * we need to suppress GUIDs that are unique on a
880 * given node but duplicated across nodes.
881 */
882 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
883 guid, dev_name(&wblock->acpi_device->dev));
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000884 return true;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800885 }
886 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700887
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000888 return false;
889}
890
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500891/*
Carlos Corbachobff431e2008-02-05 02:17:04 +0000892 * Parse the _WDG method for the GUID data blocks
893 */
Andy Lutomirski844af952015-11-24 19:49:23 -0800894static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000895{
896 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
Dmitry Torokhov37830662010-08-26 00:15:09 -0700897 const struct guid_block *gblock;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700898 struct wmi_block *wblock, *next;
899 union acpi_object *obj;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000900 acpi_status status;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700901 int retval = 0;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000902 u32 i, total;
903
Andy Lutomirski7f5809b2015-11-19 14:44:46 -0800904 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000905 if (ACPI_FAILURE(status))
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700906 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000907
908 obj = (union acpi_object *) out.pointer;
Dmitry Torokhov3d2c63e2010-08-26 00:15:03 -0700909 if (!obj)
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700910 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000911
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -0700912 if (obj->type != ACPI_TYPE_BUFFER) {
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700913 retval = -ENXIO;
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -0700914 goto out_free_pointer;
915 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000916
Dmitry Torokhov37830662010-08-26 00:15:09 -0700917 gblock = (const struct guid_block *)obj->buffer.pointer;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000918 total = obj->buffer.length / sizeof(struct guid_block);
919
Carlos Corbachobff431e2008-02-05 02:17:04 +0000920 for (i = 0; i < total; i++) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200921 if (debug_dump_wdg)
922 wmi_dump_wdg(&gblock[i]);
923
Andy Lutomirskia1c31bcd2015-11-25 08:24:42 -0800924 /*
925 * Some WMI devices, like those for nVidia hooks, have a
926 * duplicate GUID. It's not clear what we should do in this
927 * case yet, so for now, we'll just ignore the duplicate
928 * for device creation.
929 */
930 if (guid_already_parsed(device, gblock[i].guid))
931 continue;
932
Colin King58f64252010-11-19 15:40:02 +0000933 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700934 if (!wblock) {
935 retval = -ENOMEM;
936 break;
937 }
Colin King58f64252010-11-19 15:40:02 +0000938
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800939 wblock->acpi_device = device;
Colin King58f64252010-11-19 15:40:02 +0000940 wblock->gblock = gblock[i];
941
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700942 wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000943
Colin King58f64252010-11-19 15:40:02 +0000944 list_add_tail(&wblock->list, &wmi_block_list);
945
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200946 if (debug_event) {
947 wblock->handler = wmi_notify_debug;
Dmitry Torokhov2d5ab552010-08-26 00:14:48 -0700948 wmi_method_enable(wblock, 1);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200949 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000950 }
951
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700952 /*
953 * Now that all of the devices are created, add them to the
954 * device tree and probe subdrivers.
955 */
956 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
957 if (wblock->acpi_device != device)
958 continue;
959
960 retval = device_add(&wblock->dev.dev);
961 if (retval) {
962 dev_err(wmi_bus_dev, "failed to register %pULL\n",
963 wblock->gblock.guid);
964 if (debug_event)
965 wmi_method_enable(wblock, 0);
966 list_del(&wblock->list);
967 put_device(&wblock->dev.dev);
968 }
969 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700970
Axel Lina5167c52010-06-03 11:45:45 +0800971out_free_pointer:
972 kfree(out.pointer);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700973 return retval;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000974}
975
976/*
977 * WMI can have EmbeddedControl access regions. In which case, we just want to
978 * hand these off to the EC driver.
979 */
980static acpi_status
981acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
Lin Ming439913f2010-01-28 10:53:19 +0800982 u32 bits, u64 *value,
Carlos Corbachobff431e2008-02-05 02:17:04 +0000983 void *handler_context, void *region_context)
984{
985 int result = 0, i = 0;
986 u8 temp = 0;
987
988 if ((address > 0xFF) || !value)
989 return AE_BAD_PARAMETER;
990
991 if (function != ACPI_READ && function != ACPI_WRITE)
992 return AE_BAD_PARAMETER;
993
994 if (bits != 8)
995 return AE_BAD_PARAMETER;
996
997 if (function == ACPI_READ) {
998 result = ec_read(address, &temp);
Lin Ming439913f2010-01-28 10:53:19 +0800999 (*value) |= ((u64)temp) << i;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001000 } else {
1001 temp = 0xff & ((*value) >> i);
1002 result = ec_write(address, temp);
1003 }
1004
1005 switch (result) {
1006 case -EINVAL:
1007 return AE_BAD_PARAMETER;
1008 break;
1009 case -ENODEV:
1010 return AE_NOT_FOUND;
1011 break;
1012 case -ETIME:
1013 return AE_TIME;
1014 break;
1015 default:
1016 return AE_OK;
1017 }
1018}
1019
Andy Lutomirski1686f542015-11-25 17:33:25 -08001020static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1021 void *context)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001022{
1023 struct guid_block *block;
1024 struct wmi_block *wblock;
1025 struct list_head *p;
Andy Lutomirski1686f542015-11-25 17:33:25 -08001026 bool found_it = false;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001027
Dmitry Torokhov762e1a22010-08-26 00:15:14 -07001028 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +00001029 wblock = list_entry(p, struct wmi_block, list);
1030 block = &wblock->gblock;
1031
Andy Lutomirski1686f542015-11-25 17:33:25 -08001032 if (wblock->acpi_device->handle == handle &&
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001033 (block->flags & ACPI_WMI_EVENT) &&
Andy Lutomirski1686f542015-11-25 17:33:25 -08001034 (block->notify_id == event))
1035 {
1036 found_it = true;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001037 break;
1038 }
1039 }
Andy Lutomirski1686f542015-11-25 17:33:25 -08001040
1041 if (!found_it)
1042 return;
1043
1044 /* If a driver is bound, then notify the driver. */
1045 if (wblock->dev.dev.driver) {
1046 struct wmi_driver *driver;
1047 struct acpi_object_list input;
1048 union acpi_object params[1];
1049 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1050 acpi_status status;
1051
1052 driver = container_of(wblock->dev.dev.driver,
1053 struct wmi_driver, driver);
1054
1055 input.count = 1;
1056 input.pointer = params;
1057 params[0].type = ACPI_TYPE_INTEGER;
1058 params[0].integer.value = event;
1059
1060 status = acpi_evaluate_object(wblock->acpi_device->handle,
1061 "_WED", &input, &evdata);
1062 if (ACPI_FAILURE(status)) {
1063 dev_warn(&wblock->dev.dev,
1064 "failed to get event data\n");
1065 return;
1066 }
1067
1068 if (driver->notify)
1069 driver->notify(&wblock->dev,
1070 (union acpi_object *)evdata.pointer);
1071
1072 kfree(evdata.pointer);
1073 } else if (wblock->handler) {
1074 /* Legacy handler */
1075 wblock->handler(event, wblock->handler_data);
1076 }
1077
1078 if (debug_event) {
1079 pr_info("DEBUG Event GUID: %pUL\n",
1080 wblock->gblock.guid);
1081 }
1082
1083 acpi_bus_generate_netlink_event(
1084 wblock->acpi_device->pnp.device_class,
1085 dev_name(&wblock->dev.dev),
1086 event, 0);
1087
Carlos Corbachobff431e2008-02-05 02:17:04 +00001088}
1089
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001090static int acpi_wmi_remove(struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001091{
Andy Lutomirski1686f542015-11-25 17:33:25 -08001092 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1093 acpi_wmi_notify_handler);
Carlos Corbachobff431e2008-02-05 02:17:04 +00001094 acpi_remove_address_space_handler(device->handle,
1095 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001096 wmi_free_devices(device);
Andy Lutomirski844af952015-11-24 19:49:23 -08001097 device_unregister((struct device *)acpi_driver_data(device));
1098 device->driver_data = NULL;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001099
1100 return 0;
1101}
1102
Thomas Renninger925b1082010-07-16 13:11:36 +02001103static int acpi_wmi_add(struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001104{
Andy Lutomirski844af952015-11-24 19:49:23 -08001105 struct device *wmi_bus_dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001106 acpi_status status;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001107 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001108
Carlos Corbachobff431e2008-02-05 02:17:04 +00001109 status = acpi_install_address_space_handler(device->handle,
1110 ACPI_ADR_SPACE_EC,
1111 &acpi_wmi_ec_space_handler,
1112 NULL, NULL);
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001113 if (ACPI_FAILURE(status)) {
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001114 dev_err(&device->dev, "Error installing EC region handler\n");
Carlos Corbachobff431e2008-02-05 02:17:04 +00001115 return -ENODEV;
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001116 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001117
Andy Lutomirski1686f542015-11-25 17:33:25 -08001118 status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1119 acpi_wmi_notify_handler,
1120 NULL);
1121 if (ACPI_FAILURE(status)) {
1122 dev_err(&device->dev, "Error installing notify handler\n");
1123 error = -ENODEV;
1124 goto err_remove_ec_handler;
1125 }
1126
Andy Lutomirski844af952015-11-24 19:49:23 -08001127 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1128 NULL, "wmi_bus-%s", dev_name(&device->dev));
1129 if (IS_ERR(wmi_bus_dev)) {
1130 error = PTR_ERR(wmi_bus_dev);
Andy Lutomirski1686f542015-11-25 17:33:25 -08001131 goto err_remove_notify_handler;
Andy Lutomirski844af952015-11-24 19:49:23 -08001132 }
1133 device->driver_data = wmi_bus_dev;
1134
1135 error = parse_wdg(wmi_bus_dev, device);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001136 if (error) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001137 pr_err("Failed to parse WDG method\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001138 goto err_remove_busdev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001139 }
1140
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001141 return 0;
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001142
Andy Lutomirski844af952015-11-24 19:49:23 -08001143err_remove_busdev:
1144 device_unregister(wmi_bus_dev);
1145
Andy Lutomirski1686f542015-11-25 17:33:25 -08001146err_remove_notify_handler:
1147 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1148 acpi_wmi_notify_handler);
1149
1150err_remove_ec_handler:
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001151 acpi_remove_address_space_handler(device->handle,
1152 ACPI_ADR_SPACE_EC,
1153 &acpi_wmi_ec_space_handler);
1154
1155 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001156}
1157
Andy Lutomirski844af952015-11-24 19:49:23 -08001158int __must_check __wmi_driver_register(struct wmi_driver *driver,
1159 struct module *owner)
1160{
1161 driver->driver.owner = owner;
1162 driver->driver.bus = &wmi_bus_type;
1163
1164 return driver_register(&driver->driver);
1165}
1166EXPORT_SYMBOL(__wmi_driver_register);
1167
1168void wmi_driver_unregister(struct wmi_driver *driver)
1169{
1170 driver_unregister(&driver->driver);
1171}
1172EXPORT_SYMBOL(wmi_driver_unregister);
1173
Carlos Corbachobff431e2008-02-05 02:17:04 +00001174static int __init acpi_wmi_init(void)
1175{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001176 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001177
1178 if (acpi_disabled)
1179 return -ENODEV;
1180
Andy Lutomirski844af952015-11-24 19:49:23 -08001181 error = class_register(&wmi_bus_class);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001182 if (error)
1183 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001184
Andy Lutomirski844af952015-11-24 19:49:23 -08001185 error = bus_register(&wmi_bus_type);
1186 if (error)
1187 goto err_unreg_class;
1188
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001189 error = acpi_bus_register_driver(&acpi_wmi_driver);
1190 if (error) {
1191 pr_err("Error loading mapper\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001192 goto err_unreg_bus;
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001193 }
1194
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001195 return 0;
Andy Lutomirski844af952015-11-24 19:49:23 -08001196
1197err_unreg_class:
1198 class_unregister(&wmi_bus_class);
1199
1200err_unreg_bus:
1201 bus_unregister(&wmi_bus_type);
1202
1203 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001204}
1205
1206static void __exit acpi_wmi_exit(void)
1207{
Carlos Corbachobff431e2008-02-05 02:17:04 +00001208 acpi_bus_unregister_driver(&acpi_wmi_driver);
Andy Lutomirski844af952015-11-24 19:49:23 -08001209 class_unregister(&wmi_bus_class);
1210 bus_unregister(&wmi_bus_type);
Carlos Corbachobff431e2008-02-05 02:17:04 +00001211}
1212
1213subsys_initcall(acpi_wmi_init);
1214module_exit(acpi_wmi_exit);