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