]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/platform/x86/wmi.c
Merge branch 'for-4.15-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq
[mirror_ubuntu-bionic-kernel.git] / drivers / platform / x86 / wmi.c
CommitLineData
bff431e4
CC
1/*
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 *
2c9c5664
DHV
11 * WMI bus infrastructure by Andrew Lutomirski and Darren Hart:
12 * Copyright (C) 2015 Andrew Lutomirski
13 * Copyright (C) 2017 VMware, Inc. All Rights Reserved.
14 *
bff431e4
CC
15 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or (at
20 * your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
30 *
31 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32 */
33
8e07514d
DT
34#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
b60ee4e0 36#include <linux/acpi.h>
1caab3c1 37#include <linux/device.h>
b60ee4e0
ML
38#include <linux/init.h>
39#include <linux/kernel.h>
bff431e4 40#include <linux/list.h>
44b6b766 41#include <linux/miscdevice.h>
7c52d551 42#include <linux/module.h>
9599ed91 43#include <linux/platform_device.h>
b60ee4e0
ML
44#include <linux/slab.h>
45#include <linux/types.h>
44b6b766 46#include <linux/uaccess.h>
538d7eb8 47#include <linux/uuid.h>
b60ee4e0 48#include <linux/wmi.h>
44b6b766 49#include <uapi/linux/wmi.h>
bff431e4
CC
50
51ACPI_MODULE_NAME("wmi");
52MODULE_AUTHOR("Carlos Corbacho");
53MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
54MODULE_LICENSE("GPL");
55
762e1a2f 56static LIST_HEAD(wmi_block_list);
bff431e4
CC
57
58struct guid_block {
59 char guid[16];
60 union {
61 char object_id[2];
62 struct {
63 unsigned char notify_id;
64 unsigned char reserved;
65 };
66 };
67 u8 instance_count;
68 u8 flags;
69};
70
71struct wmi_block {
844af950 72 struct wmi_device dev;
bff431e4
CC
73 struct list_head list;
74 struct guid_block gblock;
44b6b766
ML
75 struct miscdevice char_dev;
76 struct mutex char_mutex;
b0e86302 77 struct acpi_device *acpi_device;
bff431e4
CC
78 wmi_notify_handler handler;
79 void *handler_data;
44b6b766 80 u64 req_buf_size;
d4fc91ad 81
fd70da6a 82 bool read_takes_no_args;
bff431e4
CC
83};
84
bff431e4
CC
85
86/*
87 * If the GUID data block is marked as expensive, we must enable and
88 * explicitily disable data collection.
89 */
90#define ACPI_WMI_EXPENSIVE 0x1
91#define ACPI_WMI_METHOD 0x2 /* GUID is a method */
92#define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
93#define ACPI_WMI_EVENT 0x8 /* GUID is an event */
94
90ab5ee9 95static bool debug_event;
fc3155b2
TR
96module_param(debug_event, bool, 0444);
97MODULE_PARM_DESC(debug_event,
98 "Log WMI Events [0/1]");
99
90ab5ee9 100static bool debug_dump_wdg;
a929aae0
TR
101module_param(debug_dump_wdg, bool, 0444);
102MODULE_PARM_DESC(debug_dump_wdg,
103 "Dump available WMI interfaces [0/1]");
104
9599ed91
AL
105static int acpi_wmi_remove(struct platform_device *device);
106static int acpi_wmi_probe(struct platform_device *device);
bff431e4
CC
107
108static const struct acpi_device_id wmi_device_ids[] = {
109 {"PNP0C14", 0},
110 {"pnp0c14", 0},
111 {"", 0},
112};
113MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
114
9599ed91
AL
115static struct platform_driver acpi_wmi_driver = {
116 .driver = {
117 .name = "acpi-wmi",
118 .acpi_match_table = wmi_device_ids,
c64eefd4 119 },
9599ed91
AL
120 .probe = acpi_wmi_probe,
121 .remove = acpi_wmi_remove,
bff431e4
CC
122};
123
124/*
125 * GUID parsing functions
126 */
127
bff431e4
CC
128static bool find_guid(const char *guid_string, struct wmi_block **out)
129{
538d7eb8 130 uuid_le guid_input;
bff431e4
CC
131 struct wmi_block *wblock;
132 struct guid_block *block;
133 struct list_head *p;
134
538d7eb8
AS
135 if (uuid_le_to_bin(guid_string, &guid_input))
136 return false;
bff431e4 137
762e1a2f 138 list_for_each(p, &wmi_block_list) {
bff431e4
CC
139 wblock = list_entry(p, struct wmi_block, list);
140 block = &wblock->gblock;
141
538d7eb8 142 if (memcmp(block->guid, &guid_input, 16) == 0) {
bff431e4
CC
143 if (out)
144 *out = wblock;
097c27fc 145 return true;
bff431e4
CC
146 }
147 }
097c27fc 148 return false;
bff431e4
CC
149}
150
d4fc91ad
AL
151static int get_subobj_info(acpi_handle handle, const char *pathname,
152 struct acpi_device_info **info)
153{
154 struct acpi_device_info *dummy_info, **info_ptr;
155 acpi_handle subobj_handle;
156 acpi_status status;
157
158 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
159 if (status == AE_NOT_FOUND)
160 return -ENOENT;
161 else if (ACPI_FAILURE(status))
162 return -EIO;
163
164 info_ptr = info ? info : &dummy_info;
165 status = acpi_get_object_info(subobj_handle, info_ptr);
166 if (ACPI_FAILURE(status))
167 return -EIO;
168
169 if (!info)
170 kfree(dummy_info);
171
172 return 0;
173}
174
a66bfa7a
MG
175static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
176{
177 struct guid_block *block = NULL;
178 char method[5];
a66bfa7a
MG
179 acpi_status status;
180 acpi_handle handle;
181
182 block = &wblock->gblock;
b0e86302 183 handle = wblock->acpi_device->handle;
a66bfa7a 184
a66bfa7a 185 snprintf(method, 5, "WE%02X", block->notify_id);
8122ab66 186 status = acpi_execute_simple_method(handle, method, enable);
a66bfa7a
MG
187
188 if (status != AE_OK && status != AE_NOT_FOUND)
189 return status;
190 else
191 return AE_OK;
192}
193
bff431e4
CC
194/*
195 * Exported WMI functions
196 */
44b6b766
ML
197
198/**
199 * set_required_buffer_size - Sets the buffer size needed for performing IOCTL
200 * @wdev: A wmi bus device from a driver
201 * @instance: Instance index
202 *
203 * Allocates memory needed for buffer, stores the buffer size in that memory
204 */
205int set_required_buffer_size(struct wmi_device *wdev, u64 length)
206{
207 struct wmi_block *wblock;
208
209 wblock = container_of(wdev, struct wmi_block, dev);
210 wblock->req_buf_size = length;
211
212 return 0;
213}
214EXPORT_SYMBOL_GPL(set_required_buffer_size);
215
bff431e4
CC
216/**
217 * wmi_evaluate_method - Evaluate a WMI method
218 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
219 * @instance: Instance index
220 * @method_id: Method ID to call
221 * &in: Buffer containing input for the method call
222 * &out: Empty buffer to return the method results
223 *
224 * Call an ACPI-WMI method
225 */
226acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
227u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
722c856d
ML
228{
229 struct wmi_block *wblock = NULL;
230
231 if (!find_guid(guid_string, &wblock))
232 return AE_ERROR;
233 return wmidev_evaluate_method(&wblock->dev, instance, method_id,
234 in, out);
235}
236EXPORT_SYMBOL_GPL(wmi_evaluate_method);
237
238/**
239 * wmidev_evaluate_method - Evaluate a WMI method
240 * @wdev: A wmi bus device from a driver
241 * @instance: Instance index
242 * @method_id: Method ID to call
243 * &in: Buffer containing input for the method call
244 * &out: Empty buffer to return the method results
245 *
246 * Call an ACPI-WMI method
247 */
248acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance,
249 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
bff431e4
CC
250{
251 struct guid_block *block = NULL;
252 struct wmi_block *wblock = NULL;
253 acpi_handle handle;
254 acpi_status status;
255 struct acpi_object_list input;
256 union acpi_object params[3];
f3d83e24 257 char method[5] = "WM";
bff431e4 258
722c856d 259 wblock = container_of(wdev, struct wmi_block, dev);
bff431e4 260 block = &wblock->gblock;
b0e86302 261 handle = wblock->acpi_device->handle;
bff431e4 262
e6bafba5 263 if (!(block->flags & ACPI_WMI_METHOD))
bff431e4
CC
264 return AE_BAD_DATA;
265
6afa1e2a 266 if (block->instance_count <= instance)
bff431e4
CC
267 return AE_BAD_PARAMETER;
268
269 input.count = 2;
270 input.pointer = params;
271 params[0].type = ACPI_TYPE_INTEGER;
272 params[0].integer.value = instance;
273 params[1].type = ACPI_TYPE_INTEGER;
274 params[1].integer.value = method_id;
275
276 if (in) {
277 input.count = 3;
278
279 if (block->flags & ACPI_WMI_STRING) {
280 params[2].type = ACPI_TYPE_STRING;
281 } else {
282 params[2].type = ACPI_TYPE_BUFFER;
283 }
284 params[2].buffer.length = in->length;
285 params[2].buffer.pointer = in->pointer;
286 }
287
288 strncat(method, block->object_id, 2);
289
290 status = acpi_evaluate_object(handle, method, &input, out);
291
292 return status;
293}
722c856d 294EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
bff431e4 295
56a37025
AL
296static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
297 struct acpi_buffer *out)
bff431e4
CC
298{
299 struct guid_block *block = NULL;
54f14c27 300 acpi_handle handle;
bff431e4 301 acpi_status status, wc_status = AE_ERROR;
8122ab66
ZR
302 struct acpi_object_list input;
303 union acpi_object wq_params[1];
f3d83e24
CL
304 char method[5];
305 char wc_method[5] = "WC";
bff431e4 306
56a37025 307 if (!out)
bff431e4
CC
308 return AE_BAD_PARAMETER;
309
bff431e4 310 block = &wblock->gblock;
b0e86302 311 handle = wblock->acpi_device->handle;
bff431e4 312
6afa1e2a 313 if (block->instance_count <= instance)
bff431e4
CC
314 return AE_BAD_PARAMETER;
315
316 /* Check GUID is a data block */
317 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
08237974 318 return AE_ERROR;
bff431e4
CC
319
320 input.count = 1;
321 input.pointer = wq_params;
322 wq_params[0].type = ACPI_TYPE_INTEGER;
323 wq_params[0].integer.value = instance;
324
d4fc91ad
AL
325 if (instance == 0 && wblock->read_takes_no_args)
326 input.count = 0;
327
bff431e4
CC
328 /*
329 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
330 * enable collection.
331 */
332 if (block->flags & ACPI_WMI_EXPENSIVE) {
bff431e4
CC
333 strncat(wc_method, block->object_id, 2);
334
335 /*
336 * Some GUIDs break the specification by declaring themselves
337 * expensive, but have no corresponding WCxx method. So we
338 * should not fail if this happens.
339 */
54f14c27 340 if (acpi_has_method(handle, wc_method))
8122ab66
ZR
341 wc_status = acpi_execute_simple_method(handle,
342 wc_method, 1);
bff431e4
CC
343 }
344
345 strcpy(method, "WQ");
346 strncat(method, block->object_id, 2);
347
dab36ad8 348 status = acpi_evaluate_object(handle, method, &input, out);
bff431e4
CC
349
350 /*
351 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
352 * the WQxx method failed - we should disable collection anyway.
353 */
a527f2d7 354 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
8122ab66 355 status = acpi_execute_simple_method(handle, wc_method, 0);
bff431e4
CC
356 }
357
358 return status;
359}
56a37025
AL
360
361/**
362 * wmi_query_block - Return contents of a WMI block (deprecated)
363 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
364 * @instance: Instance index
365 * &out: Empty buffer to return the contents of the data block to
366 *
367 * Return the contents of an ACPI-WMI data block to a buffer
368 */
369acpi_status wmi_query_block(const char *guid_string, u8 instance,
370 struct acpi_buffer *out)
371{
372 struct wmi_block *wblock;
373
374 if (!guid_string)
375 return AE_BAD_PARAMETER;
376
377 if (!find_guid(guid_string, &wblock))
378 return AE_ERROR;
379
380 return __query_block(wblock, instance, out);
381}
bff431e4
CC
382EXPORT_SYMBOL_GPL(wmi_query_block);
383
56a37025
AL
384union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
385{
386 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
387 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
388
389 if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
390 return NULL;
391
392 return (union acpi_object *)out.pointer;
393}
394EXPORT_SYMBOL_GPL(wmidev_block_query);
395
bff431e4
CC
396/**
397 * wmi_set_block - Write to a WMI block
398 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
399 * @instance: Instance index
400 * &in: Buffer containing new values for the data block
401 *
402 * Write the contents of the input buffer to an ACPI-WMI data block
403 */
404acpi_status wmi_set_block(const char *guid_string, u8 instance,
56a37025 405 const struct acpi_buffer *in)
bff431e4
CC
406{
407 struct guid_block *block = NULL;
408 struct wmi_block *wblock = NULL;
409 acpi_handle handle;
410 struct acpi_object_list input;
411 union acpi_object params[2];
f3d83e24 412 char method[5] = "WS";
bff431e4
CC
413
414 if (!guid_string || !in)
415 return AE_BAD_DATA;
416
417 if (!find_guid(guid_string, &wblock))
08237974 418 return AE_ERROR;
bff431e4
CC
419
420 block = &wblock->gblock;
b0e86302 421 handle = wblock->acpi_device->handle;
bff431e4 422
6afa1e2a 423 if (block->instance_count <= instance)
bff431e4
CC
424 return AE_BAD_PARAMETER;
425
426 /* Check GUID is a data block */
427 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
08237974 428 return AE_ERROR;
bff431e4
CC
429
430 input.count = 2;
431 input.pointer = params;
432 params[0].type = ACPI_TYPE_INTEGER;
433 params[0].integer.value = instance;
434
435 if (block->flags & ACPI_WMI_STRING) {
436 params[1].type = ACPI_TYPE_STRING;
437 } else {
438 params[1].type = ACPI_TYPE_BUFFER;
439 }
440 params[1].buffer.length = in->length;
441 params[1].buffer.pointer = in->pointer;
442
443 strncat(method, block->object_id, 2);
444
445 return acpi_evaluate_object(handle, method, &input, NULL);
446}
447EXPORT_SYMBOL_GPL(wmi_set_block);
448
37830662 449static void wmi_dump_wdg(const struct guid_block *g)
a929aae0 450{
85b4e4eb 451 pr_info("%pUL:\n", g->guid);
cd3921f8
PR
452 if (g->flags & ACPI_WMI_EVENT)
453 pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
454 else
455 pr_info("\tobject_id: %2pE\n", g->object_id);
8e07514d 456 pr_info("\tinstance_count: %d\n", g->instance_count);
dd8e908e 457 pr_info("\tflags: %#x", g->flags);
a929aae0 458 if (g->flags) {
a929aae0 459 if (g->flags & ACPI_WMI_EXPENSIVE)
dd8e908e 460 pr_cont(" ACPI_WMI_EXPENSIVE");
a929aae0 461 if (g->flags & ACPI_WMI_METHOD)
dd8e908e 462 pr_cont(" ACPI_WMI_METHOD");
a929aae0 463 if (g->flags & ACPI_WMI_STRING)
dd8e908e 464 pr_cont(" ACPI_WMI_STRING");
a929aae0 465 if (g->flags & ACPI_WMI_EVENT)
dd8e908e 466 pr_cont(" ACPI_WMI_EVENT");
a929aae0 467 }
8e07514d 468 pr_cont("\n");
a929aae0
TR
469
470}
471
fc3155b2
TR
472static void wmi_notify_debug(u32 value, void *context)
473{
474 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
475 union acpi_object *obj;
1492616a 476 acpi_status status;
fc3155b2 477
1492616a
AL
478 status = wmi_get_event_data(value, &response);
479 if (status != AE_OK) {
8e07514d 480 pr_info("bad event status 0x%x\n", status);
1492616a
AL
481 return;
482 }
fc3155b2
TR
483
484 obj = (union acpi_object *)response.pointer;
485
486 if (!obj)
487 return;
488
8e07514d 489 pr_info("DEBUG Event ");
fc3155b2
TR
490 switch(obj->type) {
491 case ACPI_TYPE_BUFFER:
8e07514d 492 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
fc3155b2
TR
493 break;
494 case ACPI_TYPE_STRING:
8e07514d 495 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
fc3155b2
TR
496 break;
497 case ACPI_TYPE_INTEGER:
8e07514d 498 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
fc3155b2
TR
499 break;
500 case ACPI_TYPE_PACKAGE:
8e07514d 501 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
fc3155b2
TR
502 break;
503 default:
8e07514d 504 pr_cont("object type 0x%X\n", obj->type);
fc3155b2 505 }
1492616a 506 kfree(obj);
fc3155b2
TR
507}
508
bff431e4
CC
509/**
510 * wmi_install_notify_handler - Register handler for WMI events
511 * @handler: Function to handle notifications
512 * @data: Data to be returned to handler when event is fired
513 *
514 * Register a handler for events sent to the ACPI-WMI mapper device.
515 */
516acpi_status wmi_install_notify_handler(const char *guid,
517wmi_notify_handler handler, void *data)
518{
519 struct wmi_block *block;
58f6425e 520 acpi_status status = AE_NOT_EXIST;
538d7eb8 521 uuid_le guid_input;
58f6425e 522 struct list_head *p;
bff431e4
CC
523
524 if (!guid || !handler)
525 return AE_BAD_PARAMETER;
526
538d7eb8
AS
527 if (uuid_le_to_bin(guid, &guid_input))
528 return AE_BAD_PARAMETER;
bff431e4 529
58f6425e
CK
530 list_for_each(p, &wmi_block_list) {
531 acpi_status wmi_status;
532 block = list_entry(p, struct wmi_block, list);
533
538d7eb8 534 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
58f6425e
CK
535 if (block->handler &&
536 block->handler != wmi_notify_debug)
537 return AE_ALREADY_ACQUIRED;
bff431e4 538
58f6425e
CK
539 block->handler = handler;
540 block->handler_data = data;
bff431e4 541
58f6425e
CK
542 wmi_status = wmi_method_enable(block, 1);
543 if ((wmi_status != AE_OK) ||
544 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
545 status = wmi_status;
546 }
547 }
a66bfa7a
MG
548
549 return status;
bff431e4
CC
550}
551EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
552
553/**
554 * wmi_uninstall_notify_handler - Unregister handler for WMI events
555 *
556 * Unregister handler for events sent to the ACPI-WMI mapper device.
557 */
558acpi_status wmi_remove_notify_handler(const char *guid)
559{
560 struct wmi_block *block;
58f6425e 561 acpi_status status = AE_NOT_EXIST;
538d7eb8 562 uuid_le guid_input;
58f6425e 563 struct list_head *p;
bff431e4
CC
564
565 if (!guid)
566 return AE_BAD_PARAMETER;
567
538d7eb8
AS
568 if (uuid_le_to_bin(guid, &guid_input))
569 return AE_BAD_PARAMETER;
bff431e4 570
58f6425e
CK
571 list_for_each(p, &wmi_block_list) {
572 acpi_status wmi_status;
573 block = list_entry(p, struct wmi_block, list);
574
538d7eb8 575 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
58f6425e
CK
576 if (!block->handler ||
577 block->handler == wmi_notify_debug)
578 return AE_NULL_ENTRY;
579
580 if (debug_event) {
581 block->handler = wmi_notify_debug;
582 status = AE_OK;
583 } else {
584 wmi_status = wmi_method_enable(block, 0);
585 block->handler = NULL;
586 block->handler_data = NULL;
587 if ((wmi_status != AE_OK) ||
588 ((wmi_status == AE_OK) &&
589 (status == AE_NOT_EXIST)))
590 status = wmi_status;
591 }
592 }
fc3155b2 593 }
58f6425e 594
a66bfa7a 595 return status;
bff431e4
CC
596}
597EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
598
599/**
600 * wmi_get_event_data - Get WMI data associated with an event
601 *
3e9b988e
AA
602 * @event: Event to find
603 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
bff431e4
CC
604 *
605 * Returns extra data associated with an event in WMI.
606 */
607acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
608{
609 struct acpi_object_list input;
610 union acpi_object params[1];
611 struct guid_block *gblock;
612 struct wmi_block *wblock;
613 struct list_head *p;
614
615 input.count = 1;
616 input.pointer = params;
617 params[0].type = ACPI_TYPE_INTEGER;
618 params[0].integer.value = event;
619
762e1a2f 620 list_for_each(p, &wmi_block_list) {
bff431e4
CC
621 wblock = list_entry(p, struct wmi_block, list);
622 gblock = &wblock->gblock;
623
624 if ((gblock->flags & ACPI_WMI_EVENT) &&
625 (gblock->notify_id == event))
b0e86302
AL
626 return acpi_evaluate_object(wblock->acpi_device->handle,
627 "_WED", &input, out);
bff431e4
CC
628 }
629
630 return AE_NOT_FOUND;
631}
632EXPORT_SYMBOL_GPL(wmi_get_event_data);
633
634/**
635 * wmi_has_guid - Check if a GUID is available
636 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
637 *
638 * Check if a given GUID is defined by _WDG
639 */
640bool wmi_has_guid(const char *guid_string)
641{
642 return find_guid(guid_string, NULL);
643}
644EXPORT_SYMBOL_GPL(wmi_has_guid);
645
844af950
AL
646static struct wmi_block *dev_to_wblock(struct device *dev)
647{
648 return container_of(dev, struct wmi_block, dev.dev);
649}
650
651static struct wmi_device *dev_to_wdev(struct device *dev)
652{
653 return container_of(dev, struct wmi_device, dev);
654}
655
1caab3c1
MG
656/*
657 * sysfs interface
658 */
614ef432 659static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
1caab3c1
MG
660 char *buf)
661{
844af950 662 struct wmi_block *wblock = dev_to_wblock(dev);
1caab3c1 663
85b4e4eb 664 return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
1caab3c1 665}
e80b89a5 666static DEVICE_ATTR_RO(modalias);
614ef432 667
844af950
AL
668static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
669 char *buf)
670{
671 struct wmi_block *wblock = dev_to_wblock(dev);
672
673 return sprintf(buf, "%pUL\n", wblock->gblock.guid);
674}
675static DEVICE_ATTR_RO(guid);
676
d79b1074
AL
677static ssize_t instance_count_show(struct device *dev,
678 struct device_attribute *attr, char *buf)
679{
680 struct wmi_block *wblock = dev_to_wblock(dev);
681
682 return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
683}
684static DEVICE_ATTR_RO(instance_count);
685
686static ssize_t expensive_show(struct device *dev,
687 struct device_attribute *attr, char *buf)
688{
689 struct wmi_block *wblock = dev_to_wblock(dev);
690
691 return sprintf(buf, "%d\n",
692 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
693}
694static DEVICE_ATTR_RO(expensive);
695
e80b89a5
GKH
696static struct attribute *wmi_attrs[] = {
697 &dev_attr_modalias.attr,
844af950 698 &dev_attr_guid.attr,
d79b1074
AL
699 &dev_attr_instance_count.attr,
700 &dev_attr_expensive.attr,
e80b89a5 701 NULL,
614ef432 702};
e80b89a5 703ATTRIBUTE_GROUPS(wmi);
1caab3c1 704
d79b1074
AL
705static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
706 char *buf)
707{
708 struct wmi_block *wblock = dev_to_wblock(dev);
709
710 return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
711}
712static DEVICE_ATTR_RO(notify_id);
713
714static struct attribute *wmi_event_attrs[] = {
715 &dev_attr_notify_id.attr,
716 NULL,
717};
718ATTRIBUTE_GROUPS(wmi_event);
719
720static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
721 char *buf)
722{
723 struct wmi_block *wblock = dev_to_wblock(dev);
724
725 return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
726 wblock->gblock.object_id[1]);
727}
728static DEVICE_ATTR_RO(object_id);
729
fd70da6a
DHV
730static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
731 char *buf)
d4fc91ad
AL
732{
733 struct wmi_device *wdev = dev_to_wdev(dev);
734
fd70da6a 735 return sprintf(buf, "%d\n", (int)wdev->setable);
d4fc91ad 736}
fd70da6a 737static DEVICE_ATTR_RO(setable);
d4fc91ad
AL
738
739static struct attribute *wmi_data_attrs[] = {
740 &dev_attr_object_id.attr,
fd70da6a 741 &dev_attr_setable.attr,
d4fc91ad
AL
742 NULL,
743};
744ATTRIBUTE_GROUPS(wmi_data);
745
746static struct attribute *wmi_method_attrs[] = {
d79b1074
AL
747 &dev_attr_object_id.attr,
748 NULL,
749};
d4fc91ad 750ATTRIBUTE_GROUPS(wmi_method);
d79b1074 751
1caab3c1
MG
752static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
753{
844af950 754 struct wmi_block *wblock = dev_to_wblock(dev);
1caab3c1 755
844af950 756 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
1caab3c1
MG
757 return -ENOMEM;
758
844af950 759 if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
1caab3c1
MG
760 return -ENOMEM;
761
844af950
AL
762 return 0;
763}
764
765static void wmi_dev_release(struct device *dev)
766{
767 struct wmi_block *wblock = dev_to_wblock(dev);
768
769 kfree(wblock);
770}
771
772static int wmi_dev_match(struct device *dev, struct device_driver *driver)
773{
774 struct wmi_driver *wmi_driver =
775 container_of(driver, struct wmi_driver, driver);
776 struct wmi_block *wblock = dev_to_wblock(dev);
777 const struct wmi_device_id *id = wmi_driver->id_table;
1caab3c1 778
844af950
AL
779 while (id->guid_string) {
780 uuid_le driver_guid;
781
782 if (WARN_ON(uuid_le_to_bin(id->guid_string, &driver_guid)))
783 continue;
784 if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
785 return 1;
786
787 id++;
788 }
1caab3c1
MG
789
790 return 0;
791}
44b6b766
ML
792static int wmi_char_open(struct inode *inode, struct file *filp)
793{
794 const char *driver_name = filp->f_path.dentry->d_iname;
795 struct wmi_block *wblock = NULL;
796 struct wmi_block *next = NULL;
797
798 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
799 if (!wblock->dev.dev.driver)
800 continue;
801 if (strcmp(driver_name, wblock->dev.dev.driver->name) == 0) {
802 filp->private_data = wblock;
803 break;
804 }
805 }
806
807 if (!filp->private_data)
808 return -ENODEV;
809
810 return nonseekable_open(inode, filp);
811}
812
813static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
814 size_t length, loff_t *offset)
815{
816 struct wmi_block *wblock = filp->private_data;
817
818 return simple_read_from_buffer(buffer, length, offset,
819 &wblock->req_buf_size,
820 sizeof(wblock->req_buf_size));
821}
822
823static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
824{
825 struct wmi_ioctl_buffer __user *input =
826 (struct wmi_ioctl_buffer __user *) arg;
827 struct wmi_block *wblock = filp->private_data;
828 struct wmi_ioctl_buffer *buf = NULL;
829 struct wmi_driver *wdriver = NULL;
830 int ret;
831
832 if (_IOC_TYPE(cmd) != WMI_IOC)
833 return -ENOTTY;
834
835 /* make sure we're not calling a higher instance than exists*/
836 if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
837 return -EINVAL;
838
839 mutex_lock(&wblock->char_mutex);
840 buf = wblock->handler_data;
841 if (get_user(buf->length, &input->length)) {
842 dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
843 ret = -EFAULT;
844 goto out_ioctl;
845 }
846 /* if it's too small, abort */
847 if (buf->length < wblock->req_buf_size) {
848 dev_err(&wblock->dev.dev,
849 "Buffer %lld too small, need at least %lld\n",
850 buf->length, wblock->req_buf_size);
851 ret = -EINVAL;
852 goto out_ioctl;
853 }
854 /* if it's too big, warn, driver will only use what is needed */
855 if (buf->length > wblock->req_buf_size)
856 dev_warn(&wblock->dev.dev,
857 "Buffer %lld is bigger than required %lld\n",
858 buf->length, wblock->req_buf_size);
859
860 /* copy the structure from userspace */
861 if (copy_from_user(buf, input, wblock->req_buf_size)) {
862 dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
863 wblock->req_buf_size);
864 ret = -EFAULT;
865 goto out_ioctl;
866 }
867
868 /* let the driver do any filtering and do the call */
869 wdriver = container_of(wblock->dev.dev.driver,
870 struct wmi_driver, driver);
5e3e2297
ML
871 if (!try_module_get(wdriver->driver.owner)) {
872 ret = -EBUSY;
873 goto out_ioctl;
874 }
44b6b766
ML
875 ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
876 module_put(wdriver->driver.owner);
877 if (ret)
878 goto out_ioctl;
879
880 /* return the result (only up to our internal buffer size) */
881 if (copy_to_user(input, buf, wblock->req_buf_size)) {
882 dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
883 wblock->req_buf_size);
884 ret = -EFAULT;
885 }
886
887out_ioctl:
888 mutex_unlock(&wblock->char_mutex);
889 return ret;
890}
891
892static const struct file_operations wmi_fops = {
893 .owner = THIS_MODULE,
894 .read = wmi_char_read,
895 .open = wmi_char_open,
896 .unlocked_ioctl = wmi_ioctl,
897 .compat_ioctl = wmi_ioctl,
898};
1caab3c1 899
844af950 900static int wmi_dev_probe(struct device *dev)
1caab3c1 901{
844af950
AL
902 struct wmi_block *wblock = dev_to_wblock(dev);
903 struct wmi_driver *wdriver =
904 container_of(dev->driver, struct wmi_driver, driver);
905 int ret = 0;
44b6b766
ML
906 int count;
907 char *buf;
844af950
AL
908
909 if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
910 dev_warn(dev, "failed to enable device -- probing anyway\n");
911
912 if (wdriver->probe) {
913 ret = wdriver->probe(dev_to_wdev(dev));
44b6b766
ML
914 if (ret != 0)
915 goto probe_failure;
916 }
917
918 /* driver wants a character device made */
919 if (wdriver->filter_callback) {
920 /* check that required buffer size declared by driver or MOF */
921 if (!wblock->req_buf_size) {
922 dev_err(&wblock->dev.dev,
923 "Required buffer size not set\n");
924 ret = -EINVAL;
925 goto probe_failure;
926 }
927
928 count = get_order(wblock->req_buf_size);
929 wblock->handler_data = (void *)__get_free_pages(GFP_KERNEL,
930 count);
931 if (!wblock->handler_data) {
932 ret = -ENOMEM;
933 goto probe_failure;
934 }
935
936 buf = kmalloc(strlen(wdriver->driver.name) + 4, GFP_KERNEL);
937 if (!buf) {
938 ret = -ENOMEM;
939 goto probe_string_failure;
940 }
941 sprintf(buf, "wmi/%s", wdriver->driver.name);
942 wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
943 wblock->char_dev.name = buf;
944 wblock->char_dev.fops = &wmi_fops;
945 wblock->char_dev.mode = 0444;
946 ret = misc_register(&wblock->char_dev);
947 if (ret) {
948 dev_warn(dev, "failed to register char dev: %d", ret);
949 ret = -ENOMEM;
950 goto probe_misc_failure;
951 }
844af950
AL
952 }
953
44b6b766
ML
954 return 0;
955
956probe_misc_failure:
957 kfree(buf);
958probe_string_failure:
959 kfree(wblock->handler_data);
960probe_failure:
961 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
962 dev_warn(dev, "failed to disable device\n");
844af950
AL
963 return ret;
964}
965
966static int wmi_dev_remove(struct device *dev)
967{
968 struct wmi_block *wblock = dev_to_wblock(dev);
969 struct wmi_driver *wdriver =
970 container_of(dev->driver, struct wmi_driver, driver);
971 int ret = 0;
972
44b6b766
ML
973 if (wdriver->filter_callback) {
974 misc_deregister(&wblock->char_dev);
975 kfree(wblock->char_dev.name);
976 free_pages((unsigned long)wblock->handler_data,
977 get_order(wblock->req_buf_size));
978 }
979
844af950
AL
980 if (wdriver->remove)
981 ret = wdriver->remove(dev_to_wdev(dev));
982
983 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
984 dev_warn(dev, "failed to disable device\n");
c64eefd4 985
844af950 986 return ret;
1caab3c1
MG
987}
988
844af950
AL
989static struct class wmi_bus_class = {
990 .name = "wmi_bus",
991};
992
993static struct bus_type wmi_bus_type = {
1caab3c1 994 .name = "wmi",
e80b89a5 995 .dev_groups = wmi_groups,
844af950
AL
996 .match = wmi_dev_match,
997 .uevent = wmi_dev_uevent,
998 .probe = wmi_dev_probe,
999 .remove = wmi_dev_remove,
1caab3c1
MG
1000};
1001
d79b1074
AL
1002static struct device_type wmi_type_event = {
1003 .name = "event",
1004 .groups = wmi_event_groups,
1005 .release = wmi_dev_release,
1006};
1007
1008static struct device_type wmi_type_method = {
1009 .name = "method",
d4fc91ad 1010 .groups = wmi_method_groups,
d79b1074
AL
1011 .release = wmi_dev_release,
1012};
1013
1014static struct device_type wmi_type_data = {
1015 .name = "data",
d4fc91ad 1016 .groups = wmi_data_groups,
d79b1074
AL
1017 .release = wmi_dev_release,
1018};
1019
fd70da6a 1020static int wmi_create_device(struct device *wmi_bus_dev,
844af950 1021 const struct guid_block *gblock,
7f5809bf
AL
1022 struct wmi_block *wblock,
1023 struct acpi_device *device)
1caab3c1 1024{
fd70da6a
DHV
1025 struct acpi_device_info *info;
1026 char method[5];
1027 int result;
1caab3c1 1028
d79b1074
AL
1029 if (gblock->flags & ACPI_WMI_EVENT) {
1030 wblock->dev.dev.type = &wmi_type_event;
fd70da6a
DHV
1031 goto out_init;
1032 }
1033
1034 if (gblock->flags & ACPI_WMI_METHOD) {
d79b1074 1035 wblock->dev.dev.type = &wmi_type_method;
44b6b766 1036 mutex_init(&wblock->char_mutex);
fd70da6a
DHV
1037 goto out_init;
1038 }
d4fc91ad 1039
fd70da6a
DHV
1040 /*
1041 * Data Block Query Control Method (WQxx by convention) is
1042 * required per the WMI documentation. If it is not present,
1043 * we ignore this data block.
1044 */
1045 strcpy(method, "WQ");
1046 strncat(method, wblock->gblock.object_id, 2);
1047 result = get_subobj_info(device->handle, method, &info);
1048
1049 if (result) {
1050 dev_warn(wmi_bus_dev,
1051 "%s data block query control method not found",
1052 method);
1053 return result;
1054 }
d4fc91ad 1055
fd70da6a 1056 wblock->dev.dev.type = &wmi_type_data;
d4fc91ad 1057
fd70da6a
DHV
1058 /*
1059 * The Microsoft documentation specifically states:
1060 *
1061 * Data blocks registered with only a single instance
1062 * can ignore the parameter.
1063 *
1064 * ACPICA will get mad at us if we call the method with the wrong number
1065 * of arguments, so check what our method expects. (On some Dell
1066 * laptops, WQxx may not be a method at all.)
1067 */
1068 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1069 wblock->read_takes_no_args = true;
d4fc91ad 1070
fd70da6a 1071 kfree(info);
d4fc91ad 1072
fd70da6a
DHV
1073 strcpy(method, "WS");
1074 strncat(method, wblock->gblock.object_id, 2);
1075 result = get_subobj_info(device->handle, method, NULL);
d4fc91ad 1076
fd70da6a
DHV
1077 if (result == 0)
1078 wblock->dev.setable = true;
d4fc91ad 1079
fd70da6a
DHV
1080 out_init:
1081 wblock->dev.dev.bus = &wmi_bus_type;
1082 wblock->dev.dev.parent = wmi_bus_dev;
d4fc91ad 1083
fd70da6a 1084 dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
1caab3c1 1085
6ee50aaa 1086 device_initialize(&wblock->dev.dev);
fd70da6a
DHV
1087
1088 return 0;
1caab3c1
MG
1089}
1090
b0e86302 1091static void wmi_free_devices(struct acpi_device *device)
1caab3c1 1092{
c64eefd4 1093 struct wmi_block *wblock, *next;
1caab3c1
MG
1094
1095 /* Delete devices for all the GUIDs */
023b9565 1096 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
b0e86302
AL
1097 if (wblock->acpi_device == device) {
1098 list_del(&wblock->list);
6ee50aaa 1099 device_unregister(&wblock->dev.dev);
b0e86302 1100 }
023b9565 1101 }
1caab3c1
MG
1102}
1103
b0e86302
AL
1104static bool guid_already_parsed(struct acpi_device *device,
1105 const u8 *guid)
d1f9e497 1106{
d1f9e497 1107 struct wmi_block *wblock;
d1f9e497 1108
b0e86302
AL
1109 list_for_each_entry(wblock, &wmi_block_list, list) {
1110 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
1111 /*
1112 * Because we historically didn't track the relationship
1113 * between GUIDs and ACPI nodes, we don't know whether
1114 * we need to suppress GUIDs that are unique on a
1115 * given node but duplicated across nodes.
1116 */
1117 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1118 guid, dev_name(&wblock->acpi_device->dev));
d1f9e497 1119 return true;
b0e86302
AL
1120 }
1121 }
d1f9e497 1122
c64eefd4 1123 return false;
2d5ab555
DT
1124}
1125
bff431e4
CC
1126/*
1127 * Parse the _WDG method for the GUID data blocks
1128 */
844af950 1129static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
bff431e4
CC
1130{
1131 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
37830662 1132 const struct guid_block *gblock;
6ee50aaa
DHV
1133 struct wmi_block *wblock, *next;
1134 union acpi_object *obj;
bff431e4 1135 acpi_status status;
6ee50aaa 1136 int retval = 0;
bff431e4
CC
1137 u32 i, total;
1138
7f5809bf 1139 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
bff431e4 1140 if (ACPI_FAILURE(status))
c64eefd4 1141 return -ENXIO;
bff431e4
CC
1142
1143 obj = (union acpi_object *) out.pointer;
3d2c63eb 1144 if (!obj)
c64eefd4 1145 return -ENXIO;
bff431e4 1146
64ed0ab8 1147 if (obj->type != ACPI_TYPE_BUFFER) {
c64eefd4 1148 retval = -ENXIO;
64ed0ab8
DT
1149 goto out_free_pointer;
1150 }
bff431e4 1151
37830662 1152 gblock = (const struct guid_block *)obj->buffer.pointer;
bff431e4
CC
1153 total = obj->buffer.length / sizeof(struct guid_block);
1154
bff431e4 1155 for (i = 0; i < total; i++) {
58f6425e
CK
1156 if (debug_dump_wdg)
1157 wmi_dump_wdg(&gblock[i]);
1158
a1c31bcd
AL
1159 /*
1160 * Some WMI devices, like those for nVidia hooks, have a
1161 * duplicate GUID. It's not clear what we should do in this
1162 * case yet, so for now, we'll just ignore the duplicate
1163 * for device creation.
1164 */
1165 if (guid_already_parsed(device, gblock[i].guid))
1166 continue;
1167
58f6425e 1168 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
6ee50aaa
DHV
1169 if (!wblock) {
1170 retval = -ENOMEM;
1171 break;
1172 }
58f6425e 1173
b0e86302 1174 wblock->acpi_device = device;
58f6425e
CK
1175 wblock->gblock = gblock[i];
1176
fd70da6a
DHV
1177 retval = wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
1178 if (retval) {
1179 kfree(wblock);
1180 continue;
1181 }
c64eefd4 1182
58f6425e 1183 list_add_tail(&wblock->list, &wmi_block_list);
bff431e4 1184
fc3155b2
TR
1185 if (debug_event) {
1186 wblock->handler = wmi_notify_debug;
2d5ab555 1187 wmi_method_enable(wblock, 1);
fc3155b2 1188 }
bff431e4
CC
1189 }
1190
6ee50aaa
DHV
1191 /*
1192 * Now that all of the devices are created, add them to the
1193 * device tree and probe subdrivers.
1194 */
1195 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1196 if (wblock->acpi_device != device)
1197 continue;
1198
1199 retval = device_add(&wblock->dev.dev);
1200 if (retval) {
1201 dev_err(wmi_bus_dev, "failed to register %pULL\n",
1202 wblock->gblock.guid);
1203 if (debug_event)
1204 wmi_method_enable(wblock, 0);
1205 list_del(&wblock->list);
1206 put_device(&wblock->dev.dev);
1207 }
1208 }
c64eefd4 1209
a5167c5b
AL
1210out_free_pointer:
1211 kfree(out.pointer);
c64eefd4 1212 return retval;
bff431e4
CC
1213}
1214
1215/*
1216 * WMI can have EmbeddedControl access regions. In which case, we just want to
1217 * hand these off to the EC driver.
1218 */
1219static acpi_status
1220acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
439913ff 1221 u32 bits, u64 *value,
bff431e4
CC
1222 void *handler_context, void *region_context)
1223{
1224 int result = 0, i = 0;
1225 u8 temp = 0;
1226
1227 if ((address > 0xFF) || !value)
1228 return AE_BAD_PARAMETER;
1229
1230 if (function != ACPI_READ && function != ACPI_WRITE)
1231 return AE_BAD_PARAMETER;
1232
1233 if (bits != 8)
1234 return AE_BAD_PARAMETER;
1235
1236 if (function == ACPI_READ) {
1237 result = ec_read(address, &temp);
439913ff 1238 (*value) |= ((u64)temp) << i;
bff431e4
CC
1239 } else {
1240 temp = 0xff & ((*value) >> i);
1241 result = ec_write(address, temp);
1242 }
1243
1244 switch (result) {
1245 case -EINVAL:
1246 return AE_BAD_PARAMETER;
1247 break;
1248 case -ENODEV:
1249 return AE_NOT_FOUND;
1250 break;
1251 case -ETIME:
1252 return AE_TIME;
1253 break;
1254 default:
1255 return AE_OK;
1256 }
1257}
1258
1686f544
AL
1259static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1260 void *context)
bff431e4
CC
1261{
1262 struct guid_block *block;
1263 struct wmi_block *wblock;
1264 struct list_head *p;
1686f544 1265 bool found_it = false;
bff431e4 1266
762e1a2f 1267 list_for_each(p, &wmi_block_list) {
bff431e4
CC
1268 wblock = list_entry(p, struct wmi_block, list);
1269 block = &wblock->gblock;
1270
1686f544 1271 if (wblock->acpi_device->handle == handle &&
b0e86302 1272 (block->flags & ACPI_WMI_EVENT) &&
1686f544
AL
1273 (block->notify_id == event))
1274 {
1275 found_it = true;
bff431e4
CC
1276 break;
1277 }
1278 }
1686f544
AL
1279
1280 if (!found_it)
1281 return;
1282
1283 /* If a driver is bound, then notify the driver. */
1284 if (wblock->dev.dev.driver) {
1285 struct wmi_driver *driver;
1286 struct acpi_object_list input;
1287 union acpi_object params[1];
1288 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1289 acpi_status status;
1290
1291 driver = container_of(wblock->dev.dev.driver,
1292 struct wmi_driver, driver);
1293
1294 input.count = 1;
1295 input.pointer = params;
1296 params[0].type = ACPI_TYPE_INTEGER;
1297 params[0].integer.value = event;
1298
1299 status = acpi_evaluate_object(wblock->acpi_device->handle,
1300 "_WED", &input, &evdata);
1301 if (ACPI_FAILURE(status)) {
1302 dev_warn(&wblock->dev.dev,
1303 "failed to get event data\n");
1304 return;
1305 }
1306
1307 if (driver->notify)
1308 driver->notify(&wblock->dev,
1309 (union acpi_object *)evdata.pointer);
1310
1311 kfree(evdata.pointer);
1312 } else if (wblock->handler) {
1313 /* Legacy handler */
1314 wblock->handler(event, wblock->handler_data);
1315 }
1316
1317 if (debug_event) {
1318 pr_info("DEBUG Event GUID: %pUL\n",
1319 wblock->gblock.guid);
1320 }
1321
1322 acpi_bus_generate_netlink_event(
1323 wblock->acpi_device->pnp.device_class,
1324 dev_name(&wblock->dev.dev),
1325 event, 0);
1326
bff431e4
CC
1327}
1328
9599ed91 1329static int acpi_wmi_remove(struct platform_device *device)
bff431e4 1330{
9599ed91
AL
1331 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1332
1333 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1686f544 1334 acpi_wmi_notify_handler);
9599ed91 1335 acpi_remove_address_space_handler(acpi_device->handle,
bff431e4 1336 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
9599ed91 1337 wmi_free_devices(acpi_device);
7b11e898 1338 device_destroy(&wmi_bus_class, MKDEV(0, 0));
bff431e4
CC
1339
1340 return 0;
1341}
1342
9599ed91 1343static int acpi_wmi_probe(struct platform_device *device)
bff431e4 1344{
9599ed91 1345 struct acpi_device *acpi_device;
844af950 1346 struct device *wmi_bus_dev;
bff431e4 1347 acpi_status status;
c64eefd4 1348 int error;
bff431e4 1349
9599ed91
AL
1350 acpi_device = ACPI_COMPANION(&device->dev);
1351 if (!acpi_device) {
1352 dev_err(&device->dev, "ACPI companion is missing\n");
1353 return -ENODEV;
1354 }
1355
1356 status = acpi_install_address_space_handler(acpi_device->handle,
bff431e4
CC
1357 ACPI_ADR_SPACE_EC,
1358 &acpi_wmi_ec_space_handler,
1359 NULL, NULL);
5212cd67 1360 if (ACPI_FAILURE(status)) {
46492ee4 1361 dev_err(&device->dev, "Error installing EC region handler\n");
bff431e4 1362 return -ENODEV;
5212cd67 1363 }
bff431e4 1364
9599ed91
AL
1365 status = acpi_install_notify_handler(acpi_device->handle,
1366 ACPI_DEVICE_NOTIFY,
1686f544
AL
1367 acpi_wmi_notify_handler,
1368 NULL);
1369 if (ACPI_FAILURE(status)) {
1370 dev_err(&device->dev, "Error installing notify handler\n");
1371 error = -ENODEV;
1372 goto err_remove_ec_handler;
1373 }
1374
844af950
AL
1375 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1376 NULL, "wmi_bus-%s", dev_name(&device->dev));
1377 if (IS_ERR(wmi_bus_dev)) {
1378 error = PTR_ERR(wmi_bus_dev);
1686f544 1379 goto err_remove_notify_handler;
844af950 1380 }
9599ed91 1381 dev_set_drvdata(&device->dev, wmi_bus_dev);
844af950 1382
9599ed91 1383 error = parse_wdg(wmi_bus_dev, acpi_device);
c64eefd4 1384 if (error) {
8e07514d 1385 pr_err("Failed to parse WDG method\n");
844af950 1386 goto err_remove_busdev;
bff431e4
CC
1387 }
1388
c64eefd4 1389 return 0;
46492ee4 1390
844af950 1391err_remove_busdev:
7b11e898 1392 device_destroy(&wmi_bus_class, MKDEV(0, 0));
844af950 1393
1686f544 1394err_remove_notify_handler:
9599ed91 1395 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
1686f544
AL
1396 acpi_wmi_notify_handler);
1397
1398err_remove_ec_handler:
9599ed91 1399 acpi_remove_address_space_handler(acpi_device->handle,
46492ee4
AL
1400 ACPI_ADR_SPACE_EC,
1401 &acpi_wmi_ec_space_handler);
1402
1403 return error;
bff431e4
CC
1404}
1405
844af950
AL
1406int __must_check __wmi_driver_register(struct wmi_driver *driver,
1407 struct module *owner)
1408{
1409 driver->driver.owner = owner;
1410 driver->driver.bus = &wmi_bus_type;
1411
1412 return driver_register(&driver->driver);
1413}
1414EXPORT_SYMBOL(__wmi_driver_register);
1415
1416void wmi_driver_unregister(struct wmi_driver *driver)
1417{
1418 driver_unregister(&driver->driver);
1419}
1420EXPORT_SYMBOL(wmi_driver_unregister);
1421
bff431e4
CC
1422static int __init acpi_wmi_init(void)
1423{
c64eefd4 1424 int error;
bff431e4
CC
1425
1426 if (acpi_disabled)
1427 return -ENODEV;
1428
844af950 1429 error = class_register(&wmi_bus_class);
c64eefd4
DT
1430 if (error)
1431 return error;
1caab3c1 1432
844af950
AL
1433 error = bus_register(&wmi_bus_type);
1434 if (error)
1435 goto err_unreg_class;
1436
9599ed91 1437 error = platform_driver_register(&acpi_wmi_driver);
c64eefd4
DT
1438 if (error) {
1439 pr_err("Error loading mapper\n");
844af950 1440 goto err_unreg_bus;
bff431e4
CC
1441 }
1442
8e07514d 1443 return 0;
844af950 1444
844af950
AL
1445err_unreg_bus:
1446 bus_unregister(&wmi_bus_type);
1447
97277717
AK
1448err_unreg_class:
1449 class_unregister(&wmi_bus_class);
1450
844af950 1451 return error;
bff431e4
CC
1452}
1453
1454static void __exit acpi_wmi_exit(void)
1455{
9599ed91 1456 platform_driver_unregister(&acpi_wmi_driver);
844af950 1457 bus_unregister(&wmi_bus_type);
303d1fcc 1458 class_unregister(&wmi_bus_class);
bff431e4
CC
1459}
1460
98b8e4e5 1461subsys_initcall_sync(acpi_wmi_init);
bff431e4 1462module_exit(acpi_wmi_exit);