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