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