blob: 33a3609d54db414af555bf3606b2eaef4d82f58e [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;
70};
71
Carlos Corbachobff431e2008-02-05 02:17:04 +000072
73/*
74 * If the GUID data block is marked as expensive, we must enable and
75 * explicitily disable data collection.
76 */
77#define ACPI_WMI_EXPENSIVE 0x1
78#define ACPI_WMI_METHOD 0x2 /* GUID is a method */
79#define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
80#define ACPI_WMI_EVENT 0x8 /* GUID is an event */
81
Rusty Russell90ab5ee2012-01-13 09:32:20 +103082static bool debug_event;
Thomas Renningerfc3155b2010-05-03 15:30:15 +020083module_param(debug_event, bool, 0444);
84MODULE_PARM_DESC(debug_event,
85 "Log WMI Events [0/1]");
86
Rusty Russell90ab5ee2012-01-13 09:32:20 +103087static bool debug_dump_wdg;
Thomas Renningera929aae2010-05-03 15:30:17 +020088module_param(debug_dump_wdg, bool, 0444);
89MODULE_PARM_DESC(debug_dump_wdg,
90 "Dump available WMI interfaces [0/1]");
91
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010092static int acpi_wmi_remove(struct acpi_device *device);
Carlos Corbachobff431e2008-02-05 02:17:04 +000093static int acpi_wmi_add(struct acpi_device *device);
Bjorn Helgaasf61bb932009-04-07 15:37:37 +000094static void acpi_wmi_notify(struct acpi_device *device, u32 event);
Carlos Corbachobff431e2008-02-05 02:17:04 +000095
96static const struct acpi_device_id wmi_device_ids[] = {
97 {"PNP0C14", 0},
98 {"pnp0c14", 0},
99 {"", 0},
100};
101MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
102
103static struct acpi_driver acpi_wmi_driver = {
Andy Lutomirski844af952015-11-24 19:49:23 -0800104 .name = "acpi-wmi",
105 .owner = THIS_MODULE,
Carlos Corbachobff431e2008-02-05 02:17:04 +0000106 .ids = wmi_device_ids,
107 .ops = {
108 .add = acpi_wmi_add,
109 .remove = acpi_wmi_remove,
Bjorn Helgaasf61bb932009-04-07 15:37:37 +0000110 .notify = acpi_wmi_notify,
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
Matthew Garretta66bfa72008-10-08 21:40:32 +0100141static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
142{
143 struct guid_block *block = NULL;
144 char method[5];
Matthew Garretta66bfa72008-10-08 21:40:32 +0100145 acpi_status status;
146 acpi_handle handle;
147
148 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800149 handle = wblock->acpi_device->handle;
Matthew Garretta66bfa72008-10-08 21:40:32 +0100150
Matthew Garretta66bfa72008-10-08 21:40:32 +0100151 snprintf(method, 5, "WE%02X", block->notify_id);
Zhang Rui8122ab662013-09-03 08:31:57 +0800152 status = acpi_execute_simple_method(handle, method, enable);
Matthew Garretta66bfa72008-10-08 21:40:32 +0100153
154 if (status != AE_OK && status != AE_NOT_FOUND)
155 return status;
156 else
157 return AE_OK;
158}
159
Carlos Corbachobff431e2008-02-05 02:17:04 +0000160/*
161 * Exported WMI functions
162 */
163/**
164 * wmi_evaluate_method - Evaluate a WMI method
165 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
166 * @instance: Instance index
167 * @method_id: Method ID to call
168 * &in: Buffer containing input for the method call
169 * &out: Empty buffer to return the method results
170 *
171 * Call an ACPI-WMI method
172 */
173acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
174u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
175{
176 struct guid_block *block = NULL;
177 struct wmi_block *wblock = NULL;
178 acpi_handle handle;
179 acpi_status status;
180 struct acpi_object_list input;
181 union acpi_object params[3];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700182 char method[5] = "WM";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000183
184 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800185 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000186
187 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800188 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000189
Al Viroe6bafba2008-02-13 04:03:25 +0000190 if (!(block->flags & ACPI_WMI_METHOD))
Carlos Corbachobff431e2008-02-05 02:17:04 +0000191 return AE_BAD_DATA;
192
193 if (block->instance_count < instance)
194 return AE_BAD_PARAMETER;
195
196 input.count = 2;
197 input.pointer = params;
198 params[0].type = ACPI_TYPE_INTEGER;
199 params[0].integer.value = instance;
200 params[1].type = ACPI_TYPE_INTEGER;
201 params[1].integer.value = method_id;
202
203 if (in) {
204 input.count = 3;
205
206 if (block->flags & ACPI_WMI_STRING) {
207 params[2].type = ACPI_TYPE_STRING;
208 } else {
209 params[2].type = ACPI_TYPE_BUFFER;
210 }
211 params[2].buffer.length = in->length;
212 params[2].buffer.pointer = in->pointer;
213 }
214
215 strncat(method, block->object_id, 2);
216
217 status = acpi_evaluate_object(handle, method, &input, out);
218
219 return status;
220}
221EXPORT_SYMBOL_GPL(wmi_evaluate_method);
222
223/**
224 * wmi_query_block - Return contents of a WMI block
225 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
226 * @instance: Instance index
227 * &out: Empty buffer to return the contents of the data block to
228 *
229 * Return the contents of an ACPI-WMI data block to a buffer
230 */
231acpi_status wmi_query_block(const char *guid_string, u8 instance,
232struct acpi_buffer *out)
233{
234 struct guid_block *block = NULL;
235 struct wmi_block *wblock = NULL;
Zhang Rui54f14c22013-09-03 08:32:07 +0800236 acpi_handle handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000237 acpi_status status, wc_status = AE_ERROR;
Zhang Rui8122ab662013-09-03 08:31:57 +0800238 struct acpi_object_list input;
239 union acpi_object wq_params[1];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700240 char method[5];
241 char wc_method[5] = "WC";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000242
243 if (!guid_string || !out)
244 return AE_BAD_PARAMETER;
245
246 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800247 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000248
249 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800250 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000251
252 if (block->instance_count < instance)
253 return AE_BAD_PARAMETER;
254
255 /* Check GUID is a data block */
256 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800257 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000258
259 input.count = 1;
260 input.pointer = wq_params;
261 wq_params[0].type = ACPI_TYPE_INTEGER;
262 wq_params[0].integer.value = instance;
263
264 /*
265 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
266 * enable collection.
267 */
268 if (block->flags & ACPI_WMI_EXPENSIVE) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000269 strncat(wc_method, block->object_id, 2);
270
271 /*
272 * Some GUIDs break the specification by declaring themselves
273 * expensive, but have no corresponding WCxx method. So we
274 * should not fail if this happens.
275 */
Zhang Rui54f14c22013-09-03 08:32:07 +0800276 if (acpi_has_method(handle, wc_method))
Zhang Rui8122ab662013-09-03 08:31:57 +0800277 wc_status = acpi_execute_simple_method(handle,
278 wc_method, 1);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000279 }
280
281 strcpy(method, "WQ");
282 strncat(method, block->object_id, 2);
283
Carlos Corbachodab36ad2008-08-02 17:28:45 +0100284 status = acpi_evaluate_object(handle, method, &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000285
286 /*
287 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
288 * the WQxx method failed - we should disable collection anyway.
289 */
Carlos Corbachoa527f2d2008-02-24 13:34:34 +0000290 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
Zhang Rui8122ab662013-09-03 08:31:57 +0800291 status = acpi_execute_simple_method(handle, wc_method, 0);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000292 }
293
294 return status;
295}
296EXPORT_SYMBOL_GPL(wmi_query_block);
297
298/**
299 * wmi_set_block - Write to a WMI block
300 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
301 * @instance: Instance index
302 * &in: Buffer containing new values for the data block
303 *
304 * Write the contents of the input buffer to an ACPI-WMI data block
305 */
306acpi_status wmi_set_block(const char *guid_string, u8 instance,
307const struct acpi_buffer *in)
308{
309 struct guid_block *block = NULL;
310 struct wmi_block *wblock = NULL;
311 acpi_handle handle;
312 struct acpi_object_list input;
313 union acpi_object params[2];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700314 char method[5] = "WS";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000315
316 if (!guid_string || !in)
317 return AE_BAD_DATA;
318
319 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800320 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000321
322 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800323 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000324
325 if (block->instance_count < instance)
326 return AE_BAD_PARAMETER;
327
328 /* Check GUID is a data block */
329 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800330 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000331
332 input.count = 2;
333 input.pointer = params;
334 params[0].type = ACPI_TYPE_INTEGER;
335 params[0].integer.value = instance;
336
337 if (block->flags & ACPI_WMI_STRING) {
338 params[1].type = ACPI_TYPE_STRING;
339 } else {
340 params[1].type = ACPI_TYPE_BUFFER;
341 }
342 params[1].buffer.length = in->length;
343 params[1].buffer.pointer = in->pointer;
344
345 strncat(method, block->object_id, 2);
346
347 return acpi_evaluate_object(handle, method, &input, NULL);
348}
349EXPORT_SYMBOL_GPL(wmi_set_block);
350
Dmitry Torokhov37830662010-08-26 00:15:09 -0700351static void wmi_dump_wdg(const struct guid_block *g)
Thomas Renningera929aae2010-05-03 15:30:17 +0200352{
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200353 pr_info("%pUL:\n", g->guid);
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700354 pr_info("\tobject_id: %c%c\n", g->object_id[0], g->object_id[1]);
355 pr_info("\tnotify_id: %02X\n", g->notify_id);
356 pr_info("\treserved: %02X\n", g->reserved);
357 pr_info("\tinstance_count: %d\n", g->instance_count);
Joe Perchesdd8e9082011-03-29 15:21:53 -0700358 pr_info("\tflags: %#x", g->flags);
Thomas Renningera929aae2010-05-03 15:30:17 +0200359 if (g->flags) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200360 if (g->flags & ACPI_WMI_EXPENSIVE)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700361 pr_cont(" ACPI_WMI_EXPENSIVE");
Thomas Renningera929aae2010-05-03 15:30:17 +0200362 if (g->flags & ACPI_WMI_METHOD)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700363 pr_cont(" ACPI_WMI_METHOD");
Thomas Renningera929aae2010-05-03 15:30:17 +0200364 if (g->flags & ACPI_WMI_STRING)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700365 pr_cont(" ACPI_WMI_STRING");
Thomas Renningera929aae2010-05-03 15:30:17 +0200366 if (g->flags & ACPI_WMI_EVENT)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700367 pr_cont(" ACPI_WMI_EVENT");
Thomas Renningera929aae2010-05-03 15:30:17 +0200368 }
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700369 pr_cont("\n");
Thomas Renningera929aae2010-05-03 15:30:17 +0200370
371}
372
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200373static void wmi_notify_debug(u32 value, void *context)
374{
375 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
376 union acpi_object *obj;
Axel Lin14926162010-06-28 09:30:45 +0800377 acpi_status status;
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200378
Axel Lin14926162010-06-28 09:30:45 +0800379 status = wmi_get_event_data(value, &response);
380 if (status != AE_OK) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700381 pr_info("bad event status 0x%x\n", status);
Axel Lin14926162010-06-28 09:30:45 +0800382 return;
383 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200384
385 obj = (union acpi_object *)response.pointer;
386
387 if (!obj)
388 return;
389
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700390 pr_info("DEBUG Event ");
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200391 switch(obj->type) {
392 case ACPI_TYPE_BUFFER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700393 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200394 break;
395 case ACPI_TYPE_STRING:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700396 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200397 break;
398 case ACPI_TYPE_INTEGER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700399 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200400 break;
401 case ACPI_TYPE_PACKAGE:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700402 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200403 break;
404 default:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700405 pr_cont("object type 0x%X\n", obj->type);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200406 }
Axel Lin14926162010-06-28 09:30:45 +0800407 kfree(obj);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200408}
409
Carlos Corbachobff431e2008-02-05 02:17:04 +0000410/**
411 * wmi_install_notify_handler - Register handler for WMI events
412 * @handler: Function to handle notifications
413 * @data: Data to be returned to handler when event is fired
414 *
415 * Register a handler for events sent to the ACPI-WMI mapper device.
416 */
417acpi_status wmi_install_notify_handler(const char *guid,
418wmi_notify_handler handler, void *data)
419{
420 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000421 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700422 uuid_le guid_input;
Colin King58f64252010-11-19 15:40:02 +0000423 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000424
425 if (!guid || !handler)
426 return AE_BAD_PARAMETER;
427
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700428 if (uuid_le_to_bin(guid, &guid_input))
429 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000430
Colin King58f64252010-11-19 15:40:02 +0000431 list_for_each(p, &wmi_block_list) {
432 acpi_status wmi_status;
433 block = list_entry(p, struct wmi_block, list);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000434
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700435 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000436 if (block->handler &&
437 block->handler != wmi_notify_debug)
438 return AE_ALREADY_ACQUIRED;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000439
Colin King58f64252010-11-19 15:40:02 +0000440 block->handler = handler;
441 block->handler_data = data;
442
443 wmi_status = wmi_method_enable(block, 1);
444 if ((wmi_status != AE_OK) ||
445 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
446 status = wmi_status;
447 }
448 }
Matthew Garretta66bfa72008-10-08 21:40:32 +0100449
450 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000451}
452EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
453
454/**
455 * wmi_uninstall_notify_handler - Unregister handler for WMI events
456 *
457 * Unregister handler for events sent to the ACPI-WMI mapper device.
458 */
459acpi_status wmi_remove_notify_handler(const char *guid)
460{
461 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000462 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700463 uuid_le guid_input;
Colin King58f64252010-11-19 15:40:02 +0000464 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000465
466 if (!guid)
467 return AE_BAD_PARAMETER;
468
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700469 if (uuid_le_to_bin(guid, &guid_input))
470 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000471
Colin King58f64252010-11-19 15:40:02 +0000472 list_for_each(p, &wmi_block_list) {
473 acpi_status wmi_status;
474 block = list_entry(p, struct wmi_block, list);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000475
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700476 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000477 if (!block->handler ||
478 block->handler == wmi_notify_debug)
479 return AE_NULL_ENTRY;
480
481 if (debug_event) {
482 block->handler = wmi_notify_debug;
483 status = AE_OK;
484 } else {
485 wmi_status = wmi_method_enable(block, 0);
486 block->handler = NULL;
487 block->handler_data = NULL;
488 if ((wmi_status != AE_OK) ||
489 ((wmi_status == AE_OK) &&
490 (status == AE_NOT_EXIST)))
491 status = wmi_status;
492 }
493 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200494 }
Colin King58f64252010-11-19 15:40:02 +0000495
Matthew Garretta66bfa72008-10-08 21:40:32 +0100496 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000497}
498EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
499
500/**
501 * wmi_get_event_data - Get WMI data associated with an event
502 *
Anisse Astier3e9b9882009-12-04 10:10:09 +0100503 * @event: Event to find
504 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
Carlos Corbachobff431e2008-02-05 02:17:04 +0000505 *
506 * Returns extra data associated with an event in WMI.
507 */
508acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
509{
510 struct acpi_object_list input;
511 union acpi_object params[1];
512 struct guid_block *gblock;
513 struct wmi_block *wblock;
514 struct list_head *p;
515
516 input.count = 1;
517 input.pointer = params;
518 params[0].type = ACPI_TYPE_INTEGER;
519 params[0].integer.value = event;
520
Dmitry Torokhov762e1a22010-08-26 00:15:14 -0700521 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000522 wblock = list_entry(p, struct wmi_block, list);
523 gblock = &wblock->gblock;
524
525 if ((gblock->flags & ACPI_WMI_EVENT) &&
526 (gblock->notify_id == event))
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800527 return acpi_evaluate_object(wblock->acpi_device->handle,
528 "_WED", &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000529 }
530
531 return AE_NOT_FOUND;
532}
533EXPORT_SYMBOL_GPL(wmi_get_event_data);
534
535/**
536 * wmi_has_guid - Check if a GUID is available
537 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
538 *
539 * Check if a given GUID is defined by _WDG
540 */
541bool wmi_has_guid(const char *guid_string)
542{
543 return find_guid(guid_string, NULL);
544}
545EXPORT_SYMBOL_GPL(wmi_has_guid);
546
Andy Lutomirski844af952015-11-24 19:49:23 -0800547static struct wmi_block *dev_to_wblock(struct device *dev)
548{
549 return container_of(dev, struct wmi_block, dev.dev);
550}
551
552static struct wmi_device *dev_to_wdev(struct device *dev)
553{
554 return container_of(dev, struct wmi_device, dev);
555}
556
Carlos Corbachobff431e2008-02-05 02:17:04 +0000557/*
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500558 * sysfs interface
559 */
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700560static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500561 char *buf)
562{
Andy Lutomirski844af952015-11-24 19:49:23 -0800563 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500564
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200565 return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500566}
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700567static DEVICE_ATTR_RO(modalias);
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700568
Andy Lutomirski844af952015-11-24 19:49:23 -0800569static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
570 char *buf)
571{
572 struct wmi_block *wblock = dev_to_wblock(dev);
573
574 return sprintf(buf, "%pUL\n", wblock->gblock.guid);
575}
576static DEVICE_ATTR_RO(guid);
577
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800578static ssize_t instance_count_show(struct device *dev,
579 struct device_attribute *attr, char *buf)
580{
581 struct wmi_block *wblock = dev_to_wblock(dev);
582
583 return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
584}
585static DEVICE_ATTR_RO(instance_count);
586
587static ssize_t expensive_show(struct device *dev,
588 struct device_attribute *attr, char *buf)
589{
590 struct wmi_block *wblock = dev_to_wblock(dev);
591
592 return sprintf(buf, "%d\n",
593 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
594}
595static DEVICE_ATTR_RO(expensive);
596
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700597static struct attribute *wmi_attrs[] = {
598 &dev_attr_modalias.attr,
Andy Lutomirski844af952015-11-24 19:49:23 -0800599 &dev_attr_guid.attr,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800600 &dev_attr_instance_count.attr,
601 &dev_attr_expensive.attr,
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700602 NULL,
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700603};
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700604ATTRIBUTE_GROUPS(wmi);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500605
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800606static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
607 char *buf)
608{
609 struct wmi_block *wblock = dev_to_wblock(dev);
610
611 return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
612}
613static DEVICE_ATTR_RO(notify_id);
614
615static struct attribute *wmi_event_attrs[] = {
616 &dev_attr_notify_id.attr,
617 NULL,
618};
619ATTRIBUTE_GROUPS(wmi_event);
620
621static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
622 char *buf)
623{
624 struct wmi_block *wblock = dev_to_wblock(dev);
625
626 return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
627 wblock->gblock.object_id[1]);
628}
629static DEVICE_ATTR_RO(object_id);
630
631static struct attribute *wmi_data_or_method_attrs[] = {
632 &dev_attr_object_id.attr,
633 NULL,
634};
635ATTRIBUTE_GROUPS(wmi_data_or_method);
636
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500637static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
638{
Andy Lutomirski844af952015-11-24 19:49:23 -0800639 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500640
Andy Lutomirski844af952015-11-24 19:49:23 -0800641 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500642 return -ENOMEM;
643
Andy Lutomirski844af952015-11-24 19:49:23 -0800644 if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500645 return -ENOMEM;
646
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500647 return 0;
648}
649
Andy Lutomirski844af952015-11-24 19:49:23 -0800650static void wmi_dev_release(struct device *dev)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500651{
Andy Lutomirski844af952015-11-24 19:49:23 -0800652 struct wmi_block *wblock = dev_to_wblock(dev);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700653
Andy Lutomirski844af952015-11-24 19:49:23 -0800654 kfree(wblock);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500655}
656
Andy Lutomirski844af952015-11-24 19:49:23 -0800657static int wmi_dev_match(struct device *dev, struct device_driver *driver)
658{
659 struct wmi_driver *wmi_driver =
660 container_of(driver, struct wmi_driver, driver);
661 struct wmi_block *wblock = dev_to_wblock(dev);
662 const struct wmi_device_id *id = wmi_driver->id_table;
663
664 while (id->guid_string) {
665 uuid_le driver_guid;
666
667 if (WARN_ON(uuid_le_to_bin(id->guid_string, &driver_guid)))
668 continue;
669 if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
670 return 1;
671
672 id++;
673 }
674
675 return 0;
676}
677
678static int wmi_dev_probe(struct device *dev)
679{
680 struct wmi_block *wblock = dev_to_wblock(dev);
681 struct wmi_driver *wdriver =
682 container_of(dev->driver, struct wmi_driver, driver);
683 int ret = 0;
684
685 if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
686 dev_warn(dev, "failed to enable device -- probing anyway\n");
687
688 if (wdriver->probe) {
689 ret = wdriver->probe(dev_to_wdev(dev));
690 if (ret != 0 && ACPI_FAILURE(wmi_method_enable(wblock, 0)))
691 dev_warn(dev, "failed to disable device\n");
692 }
693
694 return ret;
695}
696
697static int wmi_dev_remove(struct device *dev)
698{
699 struct wmi_block *wblock = dev_to_wblock(dev);
700 struct wmi_driver *wdriver =
701 container_of(dev->driver, struct wmi_driver, driver);
702 int ret = 0;
703
704 if (wdriver->remove)
705 ret = wdriver->remove(dev_to_wdev(dev));
706
707 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
708 dev_warn(dev, "failed to disable device\n");
709
710 return ret;
711}
712
713static struct class wmi_bus_class = {
714 .name = "wmi_bus",
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500715};
716
Andy Lutomirski844af952015-11-24 19:49:23 -0800717static struct bus_type wmi_bus_type = {
718 .name = "wmi",
719 .dev_groups = wmi_groups,
720 .match = wmi_dev_match,
721 .uevent = wmi_dev_uevent,
722 .probe = wmi_dev_probe,
723 .remove = wmi_dev_remove,
724};
725
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800726static struct device_type wmi_type_event = {
727 .name = "event",
728 .groups = wmi_event_groups,
729 .release = wmi_dev_release,
730};
731
732static struct device_type wmi_type_method = {
733 .name = "method",
734 .groups = wmi_data_or_method_groups,
735 .release = wmi_dev_release,
736};
737
738static struct device_type wmi_type_data = {
739 .name = "data",
740 .groups = wmi_data_or_method_groups,
741 .release = wmi_dev_release,
742};
743
Andy Lutomirski844af952015-11-24 19:49:23 -0800744static int wmi_create_device(struct device *wmi_bus_dev,
745 const struct guid_block *gblock,
Andy Lutomirski7f5809b2015-11-19 14:44:46 -0800746 struct wmi_block *wblock,
747 struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500748{
Andy Lutomirski844af952015-11-24 19:49:23 -0800749 wblock->dev.dev.bus = &wmi_bus_type;
750 wblock->dev.dev.parent = wmi_bus_dev;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700751
Andy Lutomirski844af952015-11-24 19:49:23 -0800752 dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700753
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800754 if (gblock->flags & ACPI_WMI_EVENT) {
755 wblock->dev.dev.type = &wmi_type_event;
756 } else if (gblock->flags & ACPI_WMI_METHOD) {
757 wblock->dev.dev.type = &wmi_type_method;
758 } else {
759 wblock->dev.dev.type = &wmi_type_data;
760 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700761
Andy Lutomirski844af952015-11-24 19:49:23 -0800762 return device_register(&wblock->dev.dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500763}
764
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800765static void wmi_free_devices(struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500766{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700767 struct wmi_block *wblock, *next;
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500768
769 /* Delete devices for all the GUIDs */
Dmitry Torokhov023b9562011-09-07 15:00:02 -0700770 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800771 if (wblock->acpi_device == device) {
772 list_del(&wblock->list);
Andy Lutomirski844af952015-11-24 19:49:23 -0800773 if (wblock->dev.dev.bus)
774 device_unregister(&wblock->dev.dev);
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800775 else
776 kfree(wblock);
777 }
Dmitry Torokhov023b9562011-09-07 15:00:02 -0700778 }
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500779}
780
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800781static bool guid_already_parsed(struct acpi_device *device,
782 const u8 *guid)
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000783{
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000784 struct wmi_block *wblock;
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000785
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800786 list_for_each_entry(wblock, &wmi_block_list, list) {
787 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
788 /*
789 * Because we historically didn't track the relationship
790 * between GUIDs and ACPI nodes, we don't know whether
791 * we need to suppress GUIDs that are unique on a
792 * given node but duplicated across nodes.
793 */
794 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
795 guid, dev_name(&wblock->acpi_device->dev));
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000796 return true;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800797 }
798 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700799
Carlos Corbachod1f9e492009-12-26 19:14:59 +0000800 return false;
801}
802
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500803/*
Carlos Corbachobff431e2008-02-05 02:17:04 +0000804 * Parse the _WDG method for the GUID data blocks
805 */
Andy Lutomirski844af952015-11-24 19:49:23 -0800806static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000807{
808 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
809 union acpi_object *obj;
Dmitry Torokhov37830662010-08-26 00:15:09 -0700810 const struct guid_block *gblock;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000811 struct wmi_block *wblock;
812 acpi_status status;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700813 int retval;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000814 u32 i, total;
815
Andy Lutomirski7f5809b2015-11-19 14:44:46 -0800816 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000817 if (ACPI_FAILURE(status))
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700818 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000819
820 obj = (union acpi_object *) out.pointer;
Dmitry Torokhov3d2c63e2010-08-26 00:15:03 -0700821 if (!obj)
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700822 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000823
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -0700824 if (obj->type != ACPI_TYPE_BUFFER) {
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700825 retval = -ENXIO;
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -0700826 goto out_free_pointer;
827 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000828
Dmitry Torokhov37830662010-08-26 00:15:09 -0700829 gblock = (const struct guid_block *)obj->buffer.pointer;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000830 total = obj->buffer.length / sizeof(struct guid_block);
831
Carlos Corbachobff431e2008-02-05 02:17:04 +0000832 for (i = 0; i < total; i++) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200833 if (debug_dump_wdg)
834 wmi_dump_wdg(&gblock[i]);
835
Andy Lutomirskia1c31bcd2015-11-25 08:24:42 -0800836 /*
837 * Some WMI devices, like those for nVidia hooks, have a
838 * duplicate GUID. It's not clear what we should do in this
839 * case yet, so for now, we'll just ignore the duplicate
840 * for device creation.
841 */
842 if (guid_already_parsed(device, gblock[i].guid))
843 continue;
844
Colin King58f64252010-11-19 15:40:02 +0000845 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
846 if (!wblock)
Dan Carpenter0a018a62013-08-14 12:02:30 +0300847 return -ENOMEM;
Colin King58f64252010-11-19 15:40:02 +0000848
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800849 wblock->acpi_device = device;
Colin King58f64252010-11-19 15:40:02 +0000850 wblock->gblock = gblock[i];
851
Andy Lutomirskia1c31bcd2015-11-25 08:24:42 -0800852 retval = wmi_create_device(wmi_bus_dev, &gblock[i],
853 wblock, device);
854 if (retval) {
855 put_device(&wblock->dev.dev);
856 wmi_free_devices(device);
857 goto out_free_pointer;
Axel Lina5167c52010-06-03 11:45:45 +0800858 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000859
Colin King58f64252010-11-19 15:40:02 +0000860 list_add_tail(&wblock->list, &wmi_block_list);
861
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200862 if (debug_event) {
863 wblock->handler = wmi_notify_debug;
Dmitry Torokhov2d5ab552010-08-26 00:14:48 -0700864 wmi_method_enable(wblock, 1);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200865 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000866 }
867
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700868 retval = 0;
869
Axel Lina5167c52010-06-03 11:45:45 +0800870out_free_pointer:
871 kfree(out.pointer);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000872
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700873 return retval;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000874}
875
876/*
877 * WMI can have EmbeddedControl access regions. In which case, we just want to
878 * hand these off to the EC driver.
879 */
880static acpi_status
881acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
Lin Ming439913f2010-01-28 10:53:19 +0800882 u32 bits, u64 *value,
Carlos Corbachobff431e2008-02-05 02:17:04 +0000883 void *handler_context, void *region_context)
884{
885 int result = 0, i = 0;
886 u8 temp = 0;
887
888 if ((address > 0xFF) || !value)
889 return AE_BAD_PARAMETER;
890
891 if (function != ACPI_READ && function != ACPI_WRITE)
892 return AE_BAD_PARAMETER;
893
894 if (bits != 8)
895 return AE_BAD_PARAMETER;
896
897 if (function == ACPI_READ) {
898 result = ec_read(address, &temp);
Lin Ming439913f2010-01-28 10:53:19 +0800899 (*value) |= ((u64)temp) << i;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000900 } else {
901 temp = 0xff & ((*value) >> i);
902 result = ec_write(address, temp);
903 }
904
905 switch (result) {
906 case -EINVAL:
907 return AE_BAD_PARAMETER;
908 break;
909 case -ENODEV:
910 return AE_NOT_FOUND;
911 break;
912 case -ETIME:
913 return AE_TIME;
914 break;
915 default:
916 return AE_OK;
917 }
918}
919
Bjorn Helgaasf61bb932009-04-07 15:37:37 +0000920static void acpi_wmi_notify(struct acpi_device *device, u32 event)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000921{
922 struct guid_block *block;
923 struct wmi_block *wblock;
924 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000925
Dmitry Torokhov762e1a22010-08-26 00:15:14 -0700926 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000927 wblock = list_entry(p, struct wmi_block, list);
928 block = &wblock->gblock;
929
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800930 if (wblock->acpi_device == device &&
931 (block->flags & ACPI_WMI_EVENT) &&
932 (block->notify_id == event)) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000933 if (wblock->handler)
934 wblock->handler(event, wblock->handler_data);
Thomas Renninger7715348c2010-05-03 15:30:16 +0200935 if (debug_event) {
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200936 pr_info("DEBUG Event GUID: %pUL\n",
937 wblock->gblock.guid);
Thomas Renninger7715348c2010-05-03 15:30:16 +0200938 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000939
940 acpi_bus_generate_netlink_event(
Kay Sievers07944692008-10-30 01:18:59 +0100941 device->pnp.device_class, dev_name(&device->dev),
Carlos Corbachobff431e2008-02-05 02:17:04 +0000942 event, 0);
943 break;
944 }
945 }
946}
947
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100948static int acpi_wmi_remove(struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000949{
Carlos Corbachobff431e2008-02-05 02:17:04 +0000950 acpi_remove_address_space_handler(device->handle,
951 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800952 wmi_free_devices(device);
Andy Lutomirski844af952015-11-24 19:49:23 -0800953 device_unregister((struct device *)acpi_driver_data(device));
954 device->driver_data = NULL;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000955
956 return 0;
957}
958
Thomas Renninger925b1082010-07-16 13:11:36 +0200959static int acpi_wmi_add(struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000960{
Andy Lutomirski844af952015-11-24 19:49:23 -0800961 struct device *wmi_bus_dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000962 acpi_status status;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700963 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000964
Carlos Corbachobff431e2008-02-05 02:17:04 +0000965 status = acpi_install_address_space_handler(device->handle,
966 ACPI_ADR_SPACE_EC,
967 &acpi_wmi_ec_space_handler,
968 NULL, NULL);
Dmitry Torokhov5212cd62010-08-26 00:14:42 -0700969 if (ACPI_FAILURE(status)) {
Andy Lutomirski46492ee2015-11-24 19:54:46 -0800970 dev_err(&device->dev, "Error installing EC region handler\n");
Carlos Corbachobff431e2008-02-05 02:17:04 +0000971 return -ENODEV;
Dmitry Torokhov5212cd62010-08-26 00:14:42 -0700972 }
Carlos Corbachobff431e2008-02-05 02:17:04 +0000973
Andy Lutomirski844af952015-11-24 19:49:23 -0800974 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
975 NULL, "wmi_bus-%s", dev_name(&device->dev));
976 if (IS_ERR(wmi_bus_dev)) {
977 error = PTR_ERR(wmi_bus_dev);
978 goto err_remove_handler;
979 }
980 device->driver_data = wmi_bus_dev;
981
982 error = parse_wdg(wmi_bus_dev, device);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700983 if (error) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700984 pr_err("Failed to parse WDG method\n");
Andy Lutomirski844af952015-11-24 19:49:23 -0800985 goto err_remove_busdev;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000986 }
987
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700988 return 0;
Andy Lutomirski46492ee2015-11-24 19:54:46 -0800989
Andy Lutomirski844af952015-11-24 19:49:23 -0800990err_remove_busdev:
991 device_unregister(wmi_bus_dev);
992
Andy Lutomirski46492ee2015-11-24 19:54:46 -0800993err_remove_handler:
994 acpi_remove_address_space_handler(device->handle,
995 ACPI_ADR_SPACE_EC,
996 &acpi_wmi_ec_space_handler);
997
998 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000999}
1000
Andy Lutomirski844af952015-11-24 19:49:23 -08001001int __must_check __wmi_driver_register(struct wmi_driver *driver,
1002 struct module *owner)
1003{
1004 driver->driver.owner = owner;
1005 driver->driver.bus = &wmi_bus_type;
1006
1007 return driver_register(&driver->driver);
1008}
1009EXPORT_SYMBOL(__wmi_driver_register);
1010
1011void wmi_driver_unregister(struct wmi_driver *driver)
1012{
1013 driver_unregister(&driver->driver);
1014}
1015EXPORT_SYMBOL(wmi_driver_unregister);
1016
Carlos Corbachobff431e2008-02-05 02:17:04 +00001017static int __init acpi_wmi_init(void)
1018{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001019 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001020
1021 if (acpi_disabled)
1022 return -ENODEV;
1023
Andy Lutomirski844af952015-11-24 19:49:23 -08001024 error = class_register(&wmi_bus_class);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001025 if (error)
1026 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001027
Andy Lutomirski844af952015-11-24 19:49:23 -08001028 error = bus_register(&wmi_bus_type);
1029 if (error)
1030 goto err_unreg_class;
1031
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001032 error = acpi_bus_register_driver(&acpi_wmi_driver);
1033 if (error) {
1034 pr_err("Error loading mapper\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001035 goto err_unreg_bus;
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001036 }
1037
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001038 return 0;
Andy Lutomirski844af952015-11-24 19:49:23 -08001039
1040err_unreg_class:
1041 class_unregister(&wmi_bus_class);
1042
1043err_unreg_bus:
1044 bus_unregister(&wmi_bus_type);
1045
1046 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001047}
1048
1049static void __exit acpi_wmi_exit(void)
1050{
Carlos Corbachobff431e2008-02-05 02:17:04 +00001051 acpi_bus_unregister_driver(&acpi_wmi_driver);
Andy Lutomirski844af952015-11-24 19:49:23 -08001052 class_unregister(&wmi_bus_class);
1053 bus_unregister(&wmi_bus_type);
Carlos Corbachobff431e2008-02-05 02:17:04 +00001054}
1055
1056subsys_initcall(acpi_wmi_init);
1057module_exit(acpi_wmi_exit);