blob: fbce8765e222b7fa909dbe6fb1cd6c811779530b [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);
Bjorn Helgaasf61bb932009-04-07 15:37:37 +000096static void acpi_wmi_notify(struct acpi_device *device, u32 event);
Carlos Corbachobff431e2008-02-05 02:17:04 +000097
98static const struct acpi_device_id wmi_device_ids[] = {
99 {"PNP0C14", 0},
100 {"pnp0c14", 0},
101 {"", 0},
102};
103MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
104
105static struct acpi_driver acpi_wmi_driver = {
Andy Lutomirski844af952015-11-24 19:49:23 -0800106 .name = "acpi-wmi",
107 .owner = THIS_MODULE,
Carlos Corbachobff431e2008-02-05 02:17:04 +0000108 .ids = wmi_device_ids,
109 .ops = {
110 .add = acpi_wmi_add,
111 .remove = acpi_wmi_remove,
Bjorn Helgaasf61bb932009-04-07 15:37:37 +0000112 .notify = acpi_wmi_notify,
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700113 },
Carlos Corbachobff431e2008-02-05 02:17:04 +0000114};
115
116/*
117 * GUID parsing functions
118 */
119
Carlos Corbachobff431e2008-02-05 02:17:04 +0000120static bool find_guid(const char *guid_string, struct wmi_block **out)
121{
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700122 uuid_le guid_input;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000123 struct wmi_block *wblock;
124 struct guid_block *block;
125 struct list_head *p;
126
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700127 if (uuid_le_to_bin(guid_string, &guid_input))
128 return false;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000129
Dmitry Torokhov762e1a22010-08-26 00:15:14 -0700130 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000131 wblock = list_entry(p, struct wmi_block, list);
132 block = &wblock->gblock;
133
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700134 if (memcmp(block->guid, &guid_input, 16) == 0) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000135 if (out)
136 *out = wblock;
Joe Perches097c27f2015-03-30 10:43:20 -0700137 return true;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000138 }
139 }
Joe Perches097c27f2015-03-30 10:43:20 -0700140 return false;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000141}
142
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800143static int get_subobj_info(acpi_handle handle, const char *pathname,
144 struct acpi_device_info **info)
145{
146 struct acpi_device_info *dummy_info, **info_ptr;
147 acpi_handle subobj_handle;
148 acpi_status status;
149
150 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
151 if (status == AE_NOT_FOUND)
152 return -ENOENT;
153 else if (ACPI_FAILURE(status))
154 return -EIO;
155
156 info_ptr = info ? info : &dummy_info;
157 status = acpi_get_object_info(subobj_handle, info_ptr);
158 if (ACPI_FAILURE(status))
159 return -EIO;
160
161 if (!info)
162 kfree(dummy_info);
163
164 return 0;
165}
166
Matthew Garretta66bfa72008-10-08 21:40:32 +0100167static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
168{
169 struct guid_block *block = NULL;
170 char method[5];
Matthew Garretta66bfa72008-10-08 21:40:32 +0100171 acpi_status status;
172 acpi_handle handle;
173
174 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800175 handle = wblock->acpi_device->handle;
Matthew Garretta66bfa72008-10-08 21:40:32 +0100176
Matthew Garretta66bfa72008-10-08 21:40:32 +0100177 snprintf(method, 5, "WE%02X", block->notify_id);
Zhang Rui8122ab662013-09-03 08:31:57 +0800178 status = acpi_execute_simple_method(handle, method, enable);
Matthew Garretta66bfa72008-10-08 21:40:32 +0100179
180 if (status != AE_OK && status != AE_NOT_FOUND)
181 return status;
182 else
183 return AE_OK;
184}
185
Carlos Corbachobff431e2008-02-05 02:17:04 +0000186/*
187 * Exported WMI functions
188 */
189/**
190 * wmi_evaluate_method - Evaluate a WMI method
191 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
192 * @instance: Instance index
193 * @method_id: Method ID to call
194 * &in: Buffer containing input for the method call
195 * &out: Empty buffer to return the method results
196 *
197 * Call an ACPI-WMI method
198 */
199acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
200u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
201{
202 struct guid_block *block = NULL;
203 struct wmi_block *wblock = NULL;
204 acpi_handle handle;
205 acpi_status status;
206 struct acpi_object_list input;
207 union acpi_object params[3];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700208 char method[5] = "WM";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000209
210 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800211 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000212
213 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800214 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000215
Al Viroe6bafba2008-02-13 04:03:25 +0000216 if (!(block->flags & ACPI_WMI_METHOD))
Carlos Corbachobff431e2008-02-05 02:17:04 +0000217 return AE_BAD_DATA;
218
219 if (block->instance_count < instance)
220 return AE_BAD_PARAMETER;
221
222 input.count = 2;
223 input.pointer = params;
224 params[0].type = ACPI_TYPE_INTEGER;
225 params[0].integer.value = instance;
226 params[1].type = ACPI_TYPE_INTEGER;
227 params[1].integer.value = method_id;
228
229 if (in) {
230 input.count = 3;
231
232 if (block->flags & ACPI_WMI_STRING) {
233 params[2].type = ACPI_TYPE_STRING;
234 } else {
235 params[2].type = ACPI_TYPE_BUFFER;
236 }
237 params[2].buffer.length = in->length;
238 params[2].buffer.pointer = in->pointer;
239 }
240
241 strncat(method, block->object_id, 2);
242
243 status = acpi_evaluate_object(handle, method, &input, out);
244
245 return status;
246}
247EXPORT_SYMBOL_GPL(wmi_evaluate_method);
248
249/**
250 * wmi_query_block - Return contents of a WMI block
251 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
252 * @instance: Instance index
253 * &out: Empty buffer to return the contents of the data block to
254 *
255 * Return the contents of an ACPI-WMI data block to a buffer
256 */
257acpi_status wmi_query_block(const char *guid_string, u8 instance,
258struct acpi_buffer *out)
259{
260 struct guid_block *block = NULL;
261 struct wmi_block *wblock = NULL;
Zhang Rui54f14c22013-09-03 08:32:07 +0800262 acpi_handle handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000263 acpi_status status, wc_status = AE_ERROR;
Zhang Rui8122ab662013-09-03 08:31:57 +0800264 struct acpi_object_list input;
265 union acpi_object wq_params[1];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700266 char method[5];
267 char wc_method[5] = "WC";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000268
269 if (!guid_string || !out)
270 return AE_BAD_PARAMETER;
271
272 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800273 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000274
275 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800276 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000277
278 if (block->instance_count < instance)
279 return AE_BAD_PARAMETER;
280
281 /* Check GUID is a data block */
282 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800283 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000284
285 input.count = 1;
286 input.pointer = wq_params;
287 wq_params[0].type = ACPI_TYPE_INTEGER;
288 wq_params[0].integer.value = instance;
289
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800290 if (instance == 0 && wblock->read_takes_no_args)
291 input.count = 0;
292
Carlos Corbachobff431e2008-02-05 02:17:04 +0000293 /*
294 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
295 * enable collection.
296 */
297 if (block->flags & ACPI_WMI_EXPENSIVE) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000298 strncat(wc_method, block->object_id, 2);
299
300 /*
301 * Some GUIDs break the specification by declaring themselves
302 * expensive, but have no corresponding WCxx method. So we
303 * should not fail if this happens.
304 */
Zhang Rui54f14c22013-09-03 08:32:07 +0800305 if (acpi_has_method(handle, wc_method))
Zhang Rui8122ab662013-09-03 08:31:57 +0800306 wc_status = acpi_execute_simple_method(handle,
307 wc_method, 1);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000308 }
309
310 strcpy(method, "WQ");
311 strncat(method, block->object_id, 2);
312
Carlos Corbachodab36ad2008-08-02 17:28:45 +0100313 status = acpi_evaluate_object(handle, method, &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000314
315 /*
316 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
317 * the WQxx method failed - we should disable collection anyway.
318 */
Carlos Corbachoa527f2d2008-02-24 13:34:34 +0000319 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
Zhang Rui8122ab662013-09-03 08:31:57 +0800320 status = acpi_execute_simple_method(handle, wc_method, 0);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000321 }
322
323 return status;
324}
325EXPORT_SYMBOL_GPL(wmi_query_block);
326
327/**
328 * wmi_set_block - Write to a WMI block
329 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
330 * @instance: Instance index
331 * &in: Buffer containing new values for the data block
332 *
333 * Write the contents of the input buffer to an ACPI-WMI data block
334 */
335acpi_status wmi_set_block(const char *guid_string, u8 instance,
336const struct acpi_buffer *in)
337{
338 struct guid_block *block = NULL;
339 struct wmi_block *wblock = NULL;
340 acpi_handle handle;
341 struct acpi_object_list input;
342 union acpi_object params[2];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700343 char method[5] = "WS";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000344
345 if (!guid_string || !in)
346 return AE_BAD_DATA;
347
348 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800349 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000350
351 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800352 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000353
354 if (block->instance_count < instance)
355 return AE_BAD_PARAMETER;
356
357 /* Check GUID is a data block */
358 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800359 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000360
361 input.count = 2;
362 input.pointer = params;
363 params[0].type = ACPI_TYPE_INTEGER;
364 params[0].integer.value = instance;
365
366 if (block->flags & ACPI_WMI_STRING) {
367 params[1].type = ACPI_TYPE_STRING;
368 } else {
369 params[1].type = ACPI_TYPE_BUFFER;
370 }
371 params[1].buffer.length = in->length;
372 params[1].buffer.pointer = in->pointer;
373
374 strncat(method, block->object_id, 2);
375
376 return acpi_evaluate_object(handle, method, &input, NULL);
377}
378EXPORT_SYMBOL_GPL(wmi_set_block);
379
Dmitry Torokhov37830662010-08-26 00:15:09 -0700380static void wmi_dump_wdg(const struct guid_block *g)
Thomas Renningera929aae2010-05-03 15:30:17 +0200381{
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200382 pr_info("%pUL:\n", g->guid);
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700383 pr_info("\tobject_id: %c%c\n", g->object_id[0], g->object_id[1]);
384 pr_info("\tnotify_id: %02X\n", g->notify_id);
385 pr_info("\treserved: %02X\n", g->reserved);
386 pr_info("\tinstance_count: %d\n", g->instance_count);
Joe Perchesdd8e9082011-03-29 15:21:53 -0700387 pr_info("\tflags: %#x", g->flags);
Thomas Renningera929aae2010-05-03 15:30:17 +0200388 if (g->flags) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200389 if (g->flags & ACPI_WMI_EXPENSIVE)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700390 pr_cont(" ACPI_WMI_EXPENSIVE");
Thomas Renningera929aae2010-05-03 15:30:17 +0200391 if (g->flags & ACPI_WMI_METHOD)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700392 pr_cont(" ACPI_WMI_METHOD");
Thomas Renningera929aae2010-05-03 15:30:17 +0200393 if (g->flags & ACPI_WMI_STRING)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700394 pr_cont(" ACPI_WMI_STRING");
Thomas Renningera929aae2010-05-03 15:30:17 +0200395 if (g->flags & ACPI_WMI_EVENT)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700396 pr_cont(" ACPI_WMI_EVENT");
Thomas Renningera929aae2010-05-03 15:30:17 +0200397 }
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700398 pr_cont("\n");
Thomas Renningera929aae2010-05-03 15:30:17 +0200399
400}
401
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200402static void wmi_notify_debug(u32 value, void *context)
403{
404 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
405 union acpi_object *obj;
Axel Lin14926162010-06-28 09:30:45 +0800406 acpi_status status;
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200407
Axel Lin14926162010-06-28 09:30:45 +0800408 status = wmi_get_event_data(value, &response);
409 if (status != AE_OK) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700410 pr_info("bad event status 0x%x\n", status);
Axel Lin14926162010-06-28 09:30:45 +0800411 return;
412 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200413
414 obj = (union acpi_object *)response.pointer;
415
416 if (!obj)
417 return;
418
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700419 pr_info("DEBUG Event ");
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200420 switch(obj->type) {
421 case ACPI_TYPE_BUFFER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700422 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200423 break;
424 case ACPI_TYPE_STRING:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700425 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200426 break;
427 case ACPI_TYPE_INTEGER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700428 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200429 break;
430 case ACPI_TYPE_PACKAGE:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700431 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200432 break;
433 default:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700434 pr_cont("object type 0x%X\n", obj->type);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200435 }
Axel Lin14926162010-06-28 09:30:45 +0800436 kfree(obj);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200437}
438
Carlos Corbachobff431e2008-02-05 02:17:04 +0000439/**
440 * wmi_install_notify_handler - Register handler for WMI events
441 * @handler: Function to handle notifications
442 * @data: Data to be returned to handler when event is fired
443 *
444 * Register a handler for events sent to the ACPI-WMI mapper device.
445 */
446acpi_status wmi_install_notify_handler(const char *guid,
447wmi_notify_handler handler, void *data)
448{
449 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000450 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700451 uuid_le guid_input;
Colin King58f64252010-11-19 15:40:02 +0000452 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000453
454 if (!guid || !handler)
455 return AE_BAD_PARAMETER;
456
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700457 if (uuid_le_to_bin(guid, &guid_input))
458 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000459
Colin King58f64252010-11-19 15:40:02 +0000460 list_for_each(p, &wmi_block_list) {
461 acpi_status wmi_status;
462 block = list_entry(p, struct wmi_block, list);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000463
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700464 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000465 if (block->handler &&
466 block->handler != wmi_notify_debug)
467 return AE_ALREADY_ACQUIRED;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000468
Colin King58f64252010-11-19 15:40:02 +0000469 block->handler = handler;
470 block->handler_data = data;
471
472 wmi_status = wmi_method_enable(block, 1);
473 if ((wmi_status != AE_OK) ||
474 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
475 status = wmi_status;
476 }
477 }
Matthew Garretta66bfa72008-10-08 21:40:32 +0100478
479 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000480}
481EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
482
483/**
484 * wmi_uninstall_notify_handler - Unregister handler for WMI events
485 *
486 * Unregister handler for events sent to the ACPI-WMI mapper device.
487 */
488acpi_status wmi_remove_notify_handler(const char *guid)
489{
490 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000491 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700492 uuid_le guid_input;
Colin King58f64252010-11-19 15:40:02 +0000493 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000494
495 if (!guid)
496 return AE_BAD_PARAMETER;
497
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700498 if (uuid_le_to_bin(guid, &guid_input))
499 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000500
Colin King58f64252010-11-19 15:40:02 +0000501 list_for_each(p, &wmi_block_list) {
502 acpi_status wmi_status;
503 block = list_entry(p, struct wmi_block, list);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000504
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700505 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000506 if (!block->handler ||
507 block->handler == wmi_notify_debug)
508 return AE_NULL_ENTRY;
509
510 if (debug_event) {
511 block->handler = wmi_notify_debug;
512 status = AE_OK;
513 } else {
514 wmi_status = wmi_method_enable(block, 0);
515 block->handler = NULL;
516 block->handler_data = NULL;
517 if ((wmi_status != AE_OK) ||
518 ((wmi_status == AE_OK) &&
519 (status == AE_NOT_EXIST)))
520 status = wmi_status;
521 }
522 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200523 }
Colin King58f64252010-11-19 15:40:02 +0000524
Matthew Garretta66bfa72008-10-08 21:40:32 +0100525 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000526}
527EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
528
529/**
530 * wmi_get_event_data - Get WMI data associated with an event
531 *
Anisse Astier3e9b9882009-12-04 10:10:09 +0100532 * @event: Event to find
533 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
Carlos Corbachobff431e2008-02-05 02:17:04 +0000534 *
535 * Returns extra data associated with an event in WMI.
536 */
537acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
538{
539 struct acpi_object_list input;
540 union acpi_object params[1];
541 struct guid_block *gblock;
542 struct wmi_block *wblock;
543 struct list_head *p;
544
545 input.count = 1;
546 input.pointer = params;
547 params[0].type = ACPI_TYPE_INTEGER;
548 params[0].integer.value = event;
549
Dmitry Torokhov762e1a22010-08-26 00:15:14 -0700550 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000551 wblock = list_entry(p, struct wmi_block, list);
552 gblock = &wblock->gblock;
553
554 if ((gblock->flags & ACPI_WMI_EVENT) &&
555 (gblock->notify_id == event))
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800556 return acpi_evaluate_object(wblock->acpi_device->handle,
557 "_WED", &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000558 }
559
560 return AE_NOT_FOUND;
561}
562EXPORT_SYMBOL_GPL(wmi_get_event_data);
563
564/**
565 * wmi_has_guid - Check if a GUID is available
566 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
567 *
568 * Check if a given GUID is defined by _WDG
569 */
570bool wmi_has_guid(const char *guid_string)
571{
572 return find_guid(guid_string, NULL);
573}
574EXPORT_SYMBOL_GPL(wmi_has_guid);
575
Andy Lutomirski844af952015-11-24 19:49:23 -0800576static struct wmi_block *dev_to_wblock(struct device *dev)
577{
578 return container_of(dev, struct wmi_block, dev.dev);
579}
580
581static struct wmi_device *dev_to_wdev(struct device *dev)
582{
583 return container_of(dev, struct wmi_device, dev);
584}
585
Carlos Corbachobff431e2008-02-05 02:17:04 +0000586/*
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500587 * sysfs interface
588 */
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700589static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500590 char *buf)
591{
Andy Lutomirski844af952015-11-24 19:49:23 -0800592 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500593
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200594 return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500595}
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700596static DEVICE_ATTR_RO(modalias);
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700597
Andy Lutomirski844af952015-11-24 19:49:23 -0800598static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
599 char *buf)
600{
601 struct wmi_block *wblock = dev_to_wblock(dev);
602
603 return sprintf(buf, "%pUL\n", wblock->gblock.guid);
604}
605static DEVICE_ATTR_RO(guid);
606
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800607static ssize_t instance_count_show(struct device *dev,
608 struct device_attribute *attr, char *buf)
609{
610 struct wmi_block *wblock = dev_to_wblock(dev);
611
612 return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
613}
614static DEVICE_ATTR_RO(instance_count);
615
616static ssize_t expensive_show(struct device *dev,
617 struct device_attribute *attr, char *buf)
618{
619 struct wmi_block *wblock = dev_to_wblock(dev);
620
621 return sprintf(buf, "%d\n",
622 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
623}
624static DEVICE_ATTR_RO(expensive);
625
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700626static struct attribute *wmi_attrs[] = {
627 &dev_attr_modalias.attr,
Andy Lutomirski844af952015-11-24 19:49:23 -0800628 &dev_attr_guid.attr,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800629 &dev_attr_instance_count.attr,
630 &dev_attr_expensive.attr,
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700631 NULL,
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700632};
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700633ATTRIBUTE_GROUPS(wmi);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500634
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800635static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
636 char *buf)
637{
638 struct wmi_block *wblock = dev_to_wblock(dev);
639
640 return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
641}
642static DEVICE_ATTR_RO(notify_id);
643
644static struct attribute *wmi_event_attrs[] = {
645 &dev_attr_notify_id.attr,
646 NULL,
647};
648ATTRIBUTE_GROUPS(wmi_event);
649
650static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
651 char *buf)
652{
653 struct wmi_block *wblock = dev_to_wblock(dev);
654
655 return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
656 wblock->gblock.object_id[1]);
657}
658static DEVICE_ATTR_RO(object_id);
659
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800660static ssize_t readable_show(struct device *dev, struct device_attribute *attr,
661 char *buf)
662{
663 struct wmi_device *wdev = dev_to_wdev(dev);
664
665 return sprintf(buf, "%d\n", (int)wdev->readable);
666}
667static DEVICE_ATTR_RO(readable);
668
669static ssize_t writeable_show(struct device *dev, struct device_attribute *attr,
670 char *buf)
671{
672 struct wmi_device *wdev = dev_to_wdev(dev);
673
674 return sprintf(buf, "%d\n", (int)wdev->writeable);
675}
676static DEVICE_ATTR_RO(writeable);
677
678static struct attribute *wmi_data_attrs[] = {
679 &dev_attr_object_id.attr,
680 &dev_attr_readable.attr,
681 &dev_attr_writeable.attr,
682 NULL,
683};
684ATTRIBUTE_GROUPS(wmi_data);
685
686static struct attribute *wmi_method_attrs[] = {
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800687 &dev_attr_object_id.attr,
688 NULL,
689};
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800690ATTRIBUTE_GROUPS(wmi_method);
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800691
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500692static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
693{
Andy Lutomirski844af952015-11-24 19:49:23 -0800694 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500695
Andy Lutomirski844af952015-11-24 19:49:23 -0800696 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500697 return -ENOMEM;
698
Andy Lutomirski844af952015-11-24 19:49:23 -0800699 if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500700 return -ENOMEM;
701
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500702 return 0;
703}
704
Andy Lutomirski844af952015-11-24 19:49:23 -0800705static void wmi_dev_release(struct device *dev)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500706{
Andy Lutomirski844af952015-11-24 19:49:23 -0800707 struct wmi_block *wblock = dev_to_wblock(dev);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700708
Andy Lutomirski844af952015-11-24 19:49:23 -0800709 kfree(wblock);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500710}
711
Andy Lutomirski844af952015-11-24 19:49:23 -0800712static int wmi_dev_match(struct device *dev, struct device_driver *driver)
713{
714 struct wmi_driver *wmi_driver =
715 container_of(driver, struct wmi_driver, driver);
716 struct wmi_block *wblock = dev_to_wblock(dev);
717 const struct wmi_device_id *id = wmi_driver->id_table;
718
719 while (id->guid_string) {
720 uuid_le driver_guid;
721
722 if (WARN_ON(uuid_le_to_bin(id->guid_string, &driver_guid)))
723 continue;
724 if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
725 return 1;
726
727 id++;
728 }
729
730 return 0;
731}
732
733static int wmi_dev_probe(struct device *dev)
734{
735 struct wmi_block *wblock = dev_to_wblock(dev);
736 struct wmi_driver *wdriver =
737 container_of(dev->driver, struct wmi_driver, driver);
738 int ret = 0;
739
740 if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
741 dev_warn(dev, "failed to enable device -- probing anyway\n");
742
743 if (wdriver->probe) {
744 ret = wdriver->probe(dev_to_wdev(dev));
745 if (ret != 0 && ACPI_FAILURE(wmi_method_enable(wblock, 0)))
746 dev_warn(dev, "failed to disable device\n");
747 }
748
749 return ret;
750}
751
752static int wmi_dev_remove(struct device *dev)
753{
754 struct wmi_block *wblock = dev_to_wblock(dev);
755 struct wmi_driver *wdriver =
756 container_of(dev->driver, struct wmi_driver, driver);
757 int ret = 0;
758
759 if (wdriver->remove)
760 ret = wdriver->remove(dev_to_wdev(dev));
761
762 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
763 dev_warn(dev, "failed to disable device\n");
764
765 return ret;
766}
767
768static struct class wmi_bus_class = {
769 .name = "wmi_bus",
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500770};
771
Andy Lutomirski844af952015-11-24 19:49:23 -0800772static struct bus_type wmi_bus_type = {
773 .name = "wmi",
774 .dev_groups = wmi_groups,
775 .match = wmi_dev_match,
776 .uevent = wmi_dev_uevent,
777 .probe = wmi_dev_probe,
778 .remove = wmi_dev_remove,
779};
780
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800781static struct device_type wmi_type_event = {
782 .name = "event",
783 .groups = wmi_event_groups,
784 .release = wmi_dev_release,
785};
786
787static struct device_type wmi_type_method = {
788 .name = "method",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800789 .groups = wmi_method_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800790 .release = wmi_dev_release,
791};
792
793static struct device_type wmi_type_data = {
794 .name = "data",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800795 .groups = wmi_data_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800796 .release = wmi_dev_release,
797};
798
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700799static void wmi_create_device(struct device *wmi_bus_dev,
Andy Lutomirski844af952015-11-24 19:49:23 -0800800 const struct guid_block *gblock,
Andy Lutomirski7f5809b2015-11-19 14:44:46 -0800801 struct wmi_block *wblock,
802 struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500803{
Andy Lutomirski844af952015-11-24 19:49:23 -0800804 wblock->dev.dev.bus = &wmi_bus_type;
805 wblock->dev.dev.parent = wmi_bus_dev;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700806
Andy Lutomirski844af952015-11-24 19:49:23 -0800807 dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700808
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800809 if (gblock->flags & ACPI_WMI_EVENT) {
810 wblock->dev.dev.type = &wmi_type_event;
811 } else if (gblock->flags & ACPI_WMI_METHOD) {
812 wblock->dev.dev.type = &wmi_type_method;
813 } else {
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800814 struct acpi_device_info *info;
815 char method[5];
816 int result;
817
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800818 wblock->dev.dev.type = &wmi_type_data;
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800819
820 strcpy(method, "WQ");
821 strncat(method, wblock->gblock.object_id, 2);
822 result = get_subobj_info(device->handle, method, &info);
823
824 if (result == 0) {
825 wblock->dev.readable = true;
826
827 /*
828 * The Microsoft documentation specifically states:
829 *
830 * Data blocks registered with only a single instance
831 * can ignore the parameter.
832 *
833 * ACPICA will get mad at us if we call the method
834 * with the wrong number of arguments, so check what
835 * our method expects. (On some Dell laptops, WQxx
836 * may not be a method at all.)
837 */
838 if (info->type != ACPI_TYPE_METHOD ||
839 info->param_count == 0)
840 wblock->read_takes_no_args = true;
841
842 kfree(info);
843 }
844
845 strcpy(method, "WS");
846 strncat(method, wblock->gblock.object_id, 2);
847 result = get_subobj_info(device->handle, method, NULL);
848
849 if (result == 0) {
850 wblock->dev.writeable = true;
851 }
852
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800853 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700854
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700855 device_initialize(&wblock->dev.dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500856}
857
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800858static void wmi_free_devices(struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500859{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700860 struct wmi_block *wblock, *next;
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500861
862 /* Delete devices for all the GUIDs */
Dmitry Torokhov023b9562011-09-07 15:00:02 -0700863 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800864 if (wblock->acpi_device == device) {
865 list_del(&wblock->list);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700866 device_unregister(&wblock->dev.dev);
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800867 }
Dmitry Torokhov023b9562011-09-07 15:00:02 -0700868 }
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500869}
870
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800871static bool guid_already_parsed(struct acpi_device *device,
872 const u8 *guid)
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000873{
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000874 struct wmi_block *wblock;
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000875
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800876 list_for_each_entry(wblock, &wmi_block_list, list) {
877 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
878 /*
879 * Because we historically didn't track the relationship
880 * between GUIDs and ACPI nodes, we don't know whether
881 * we need to suppress GUIDs that are unique on a
882 * given node but duplicated across nodes.
883 */
884 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
885 guid, dev_name(&wblock->acpi_device->dev));
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000886 return true;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800887 }
888 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700889
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000890 return false;
891}
892
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500893/*
Carlos Corbachobff431e2008-02-05 02:17:04 +0000894 * Parse the _WDG method for the GUID data blocks
895 */
Andy Lutomirski844af952015-11-24 19:49:23 -0800896static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000897{
898 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
Dmitry Torokhov37830662010-08-26 00:15:09 -0700899 const struct guid_block *gblock;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700900 struct wmi_block *wblock, *next;
901 union acpi_object *obj;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000902 acpi_status status;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700903 int retval = 0;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000904 u32 i, total;
905
Andy Lutomirski7f5809b2015-11-19 14:44:46 -0800906 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000907 if (ACPI_FAILURE(status))
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700908 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000909
910 obj = (union acpi_object *) out.pointer;
Dmitry Torokhov3d2c63e2010-08-26 00:15:03 -0700911 if (!obj)
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700912 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000913
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -0700914 if (obj->type != ACPI_TYPE_BUFFER) {
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700915 retval = -ENXIO;
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -0700916 goto out_free_pointer;
917 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000918
Dmitry Torokhov37830662010-08-26 00:15:09 -0700919 gblock = (const struct guid_block *)obj->buffer.pointer;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000920 total = obj->buffer.length / sizeof(struct guid_block);
921
Carlos Corbachobff431e2008-02-05 02:17:04 +0000922 for (i = 0; i < total; i++) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200923 if (debug_dump_wdg)
924 wmi_dump_wdg(&gblock[i]);
925
Andy Lutomirskia1c31bcd2015-11-25 08:24:42 -0800926 /*
927 * Some WMI devices, like those for nVidia hooks, have a
928 * duplicate GUID. It's not clear what we should do in this
929 * case yet, so for now, we'll just ignore the duplicate
930 * for device creation.
931 */
932 if (guid_already_parsed(device, gblock[i].guid))
933 continue;
934
Colin King58f64252010-11-19 15:40:02 +0000935 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700936 if (!wblock) {
937 retval = -ENOMEM;
938 break;
939 }
Colin King58f64252010-11-19 15:40:02 +0000940
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800941 wblock->acpi_device = device;
Colin King58f64252010-11-19 15:40:02 +0000942 wblock->gblock = gblock[i];
943
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700944 wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000945
Colin King58f64252010-11-19 15:40:02 +0000946 list_add_tail(&wblock->list, &wmi_block_list);
947
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200948 if (debug_event) {
949 wblock->handler = wmi_notify_debug;
Dmitry Torokhov2d5ab552010-08-26 00:14:48 -0700950 wmi_method_enable(wblock, 1);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200951 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000952 }
953
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -0700954 /*
955 * Now that all of the devices are created, add them to the
956 * device tree and probe subdrivers.
957 */
958 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
959 if (wblock->acpi_device != device)
960 continue;
961
962 retval = device_add(&wblock->dev.dev);
963 if (retval) {
964 dev_err(wmi_bus_dev, "failed to register %pULL\n",
965 wblock->gblock.guid);
966 if (debug_event)
967 wmi_method_enable(wblock, 0);
968 list_del(&wblock->list);
969 put_device(&wblock->dev.dev);
970 }
971 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700972
Axel Lina5167c52010-06-03 11:45:45 +0800973out_free_pointer:
974 kfree(out.pointer);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700975 return retval;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000976}
977
978/*
979 * WMI can have EmbeddedControl access regions. In which case, we just want to
980 * hand these off to the EC driver.
981 */
982static acpi_status
983acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
Lin Ming439913f2010-01-28 10:53:19 +0800984 u32 bits, u64 *value,
Carlos Corbachobff431e2008-02-05 02:17:04 +0000985 void *handler_context, void *region_context)
986{
987 int result = 0, i = 0;
988 u8 temp = 0;
989
990 if ((address > 0xFF) || !value)
991 return AE_BAD_PARAMETER;
992
993 if (function != ACPI_READ && function != ACPI_WRITE)
994 return AE_BAD_PARAMETER;
995
996 if (bits != 8)
997 return AE_BAD_PARAMETER;
998
999 if (function == ACPI_READ) {
1000 result = ec_read(address, &temp);
Lin Ming439913f2010-01-28 10:53:19 +08001001 (*value) |= ((u64)temp) << i;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001002 } else {
1003 temp = 0xff & ((*value) >> i);
1004 result = ec_write(address, temp);
1005 }
1006
1007 switch (result) {
1008 case -EINVAL:
1009 return AE_BAD_PARAMETER;
1010 break;
1011 case -ENODEV:
1012 return AE_NOT_FOUND;
1013 break;
1014 case -ETIME:
1015 return AE_TIME;
1016 break;
1017 default:
1018 return AE_OK;
1019 }
1020}
1021
Bjorn Helgaasf61bb932009-04-07 15:37:37 +00001022static void acpi_wmi_notify(struct acpi_device *device, u32 event)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001023{
1024 struct guid_block *block;
1025 struct wmi_block *wblock;
1026 struct list_head *p;
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 Lutomirskib0e86302015-11-24 19:50:01 -08001032 if (wblock->acpi_device == device &&
1033 (block->flags & ACPI_WMI_EVENT) &&
1034 (block->notify_id == event)) {
Carlos Corbachobff431e2008-02-05 02:17:04 +00001035 if (wblock->handler)
1036 wblock->handler(event, wblock->handler_data);
Thomas Renninger7715348c2010-05-03 15:30:16 +02001037 if (debug_event) {
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +02001038 pr_info("DEBUG Event GUID: %pUL\n",
1039 wblock->gblock.guid);
Thomas Renninger7715348c2010-05-03 15:30:16 +02001040 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001041
1042 acpi_bus_generate_netlink_event(
Kay Sievers07944692008-10-30 01:18:59 +01001043 device->pnp.device_class, dev_name(&device->dev),
Carlos Corbachobff431e2008-02-05 02:17:04 +00001044 event, 0);
1045 break;
1046 }
1047 }
1048}
1049
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001050static int acpi_wmi_remove(struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001051{
Carlos Corbachobff431e2008-02-05 02:17:04 +00001052 acpi_remove_address_space_handler(device->handle,
1053 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001054 wmi_free_devices(device);
Andy Lutomirski844af952015-11-24 19:49:23 -08001055 device_unregister((struct device *)acpi_driver_data(device));
1056 device->driver_data = NULL;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001057
1058 return 0;
1059}
1060
Thomas Renninger925b1082010-07-16 13:11:36 +02001061static int acpi_wmi_add(struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001062{
Andy Lutomirski844af952015-11-24 19:49:23 -08001063 struct device *wmi_bus_dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001064 acpi_status status;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001065 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001066
Carlos Corbachobff431e2008-02-05 02:17:04 +00001067 status = acpi_install_address_space_handler(device->handle,
1068 ACPI_ADR_SPACE_EC,
1069 &acpi_wmi_ec_space_handler,
1070 NULL, NULL);
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001071 if (ACPI_FAILURE(status)) {
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001072 dev_err(&device->dev, "Error installing EC region handler\n");
Carlos Corbachobff431e2008-02-05 02:17:04 +00001073 return -ENODEV;
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001074 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001075
Andy Lutomirski844af952015-11-24 19:49:23 -08001076 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1077 NULL, "wmi_bus-%s", dev_name(&device->dev));
1078 if (IS_ERR(wmi_bus_dev)) {
1079 error = PTR_ERR(wmi_bus_dev);
1080 goto err_remove_handler;
1081 }
1082 device->driver_data = wmi_bus_dev;
1083
1084 error = parse_wdg(wmi_bus_dev, device);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001085 if (error) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001086 pr_err("Failed to parse WDG method\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001087 goto err_remove_busdev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001088 }
1089
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001090 return 0;
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001091
Andy Lutomirski844af952015-11-24 19:49:23 -08001092err_remove_busdev:
1093 device_unregister(wmi_bus_dev);
1094
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001095err_remove_handler:
1096 acpi_remove_address_space_handler(device->handle,
1097 ACPI_ADR_SPACE_EC,
1098 &acpi_wmi_ec_space_handler);
1099
1100 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001101}
1102
Andy Lutomirski844af952015-11-24 19:49:23 -08001103int __must_check __wmi_driver_register(struct wmi_driver *driver,
1104 struct module *owner)
1105{
1106 driver->driver.owner = owner;
1107 driver->driver.bus = &wmi_bus_type;
1108
1109 return driver_register(&driver->driver);
1110}
1111EXPORT_SYMBOL(__wmi_driver_register);
1112
1113void wmi_driver_unregister(struct wmi_driver *driver)
1114{
1115 driver_unregister(&driver->driver);
1116}
1117EXPORT_SYMBOL(wmi_driver_unregister);
1118
Carlos Corbachobff431e2008-02-05 02:17:04 +00001119static int __init acpi_wmi_init(void)
1120{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001121 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001122
1123 if (acpi_disabled)
1124 return -ENODEV;
1125
Andy Lutomirski844af952015-11-24 19:49:23 -08001126 error = class_register(&wmi_bus_class);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001127 if (error)
1128 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001129
Andy Lutomirski844af952015-11-24 19:49:23 -08001130 error = bus_register(&wmi_bus_type);
1131 if (error)
1132 goto err_unreg_class;
1133
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001134 error = acpi_bus_register_driver(&acpi_wmi_driver);
1135 if (error) {
1136 pr_err("Error loading mapper\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001137 goto err_unreg_bus;
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001138 }
1139
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001140 return 0;
Andy Lutomirski844af952015-11-24 19:49:23 -08001141
1142err_unreg_class:
1143 class_unregister(&wmi_bus_class);
1144
1145err_unreg_bus:
1146 bus_unregister(&wmi_bus_type);
1147
1148 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001149}
1150
1151static void __exit acpi_wmi_exit(void)
1152{
Carlos Corbachobff431e2008-02-05 02:17:04 +00001153 acpi_bus_unregister_driver(&acpi_wmi_driver);
Andy Lutomirski844af952015-11-24 19:49:23 -08001154 class_unregister(&wmi_bus_class);
1155 bus_unregister(&wmi_bus_type);
Carlos Corbachobff431e2008-02-05 02:17:04 +00001156}
1157
1158subsys_initcall(acpi_wmi_init);
1159module_exit(acpi_wmi_exit);