2 * ACPI-WMI mapping driver
4 * Copyright (C) 2007-2008 Carlos Corbacho <carlos@strangeworlds.co.uk>
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>
11 * WMI bus infrastructure by Andrew Lutomirski and Darren Hart:
12 * Copyright (C) 2015 Andrew Lutomirski
13 * Copyright (C) 2017 VMware, Inc. All Rights Reserved.
15 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
31 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 #include <linux/acpi.h>
37 #include <linux/device.h>
38 #include <linux/init.h>
39 #include <linux/kernel.h>
40 #include <linux/list.h>
41 #include <linux/module.h>
42 #include <linux/platform_device.h>
43 #include <linux/slab.h>
44 #include <linux/types.h>
45 #include <linux/uuid.h>
46 #include <linux/wmi.h>
48 ACPI_MODULE_NAME("wmi");
49 MODULE_AUTHOR("Carlos Corbacho");
50 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
51 MODULE_LICENSE("GPL");
53 static LIST_HEAD(wmi_block_list
);
60 unsigned char notify_id
;
61 unsigned char reserved
;
69 struct wmi_device dev
;
70 struct list_head list
;
71 struct guid_block gblock
;
72 struct acpi_device
*acpi_device
;
73 wmi_notify_handler handler
;
76 bool read_takes_no_args
;
81 * If the GUID data block is marked as expensive, we must enable and
82 * explicitily disable data collection.
84 #define ACPI_WMI_EXPENSIVE 0x1
85 #define ACPI_WMI_METHOD 0x2 /* GUID is a method */
86 #define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
87 #define ACPI_WMI_EVENT 0x8 /* GUID is an event */
89 static bool debug_event
;
90 module_param(debug_event
, bool, 0444);
91 MODULE_PARM_DESC(debug_event
,
92 "Log WMI Events [0/1]");
94 static bool debug_dump_wdg
;
95 module_param(debug_dump_wdg
, bool, 0444);
96 MODULE_PARM_DESC(debug_dump_wdg
,
97 "Dump available WMI interfaces [0/1]");
99 static int acpi_wmi_remove(struct platform_device
*device
);
100 static int acpi_wmi_probe(struct platform_device
*device
);
102 static const struct acpi_device_id wmi_device_ids
[] = {
107 MODULE_DEVICE_TABLE(acpi
, wmi_device_ids
);
109 static struct platform_driver acpi_wmi_driver
= {
112 .acpi_match_table
= wmi_device_ids
,
114 .probe
= acpi_wmi_probe
,
115 .remove
= acpi_wmi_remove
,
119 * GUID parsing functions
122 static bool find_guid(const char *guid_string
, struct wmi_block
**out
)
125 struct wmi_block
*wblock
;
126 struct guid_block
*block
;
129 if (uuid_le_to_bin(guid_string
, &guid_input
))
132 list_for_each(p
, &wmi_block_list
) {
133 wblock
= list_entry(p
, struct wmi_block
, list
);
134 block
= &wblock
->gblock
;
136 if (memcmp(block
->guid
, &guid_input
, 16) == 0) {
145 static int get_subobj_info(acpi_handle handle
, const char *pathname
,
146 struct acpi_device_info
**info
)
148 struct acpi_device_info
*dummy_info
, **info_ptr
;
149 acpi_handle subobj_handle
;
152 status
= acpi_get_handle(handle
, (char *)pathname
, &subobj_handle
);
153 if (status
== AE_NOT_FOUND
)
155 else if (ACPI_FAILURE(status
))
158 info_ptr
= info
? info
: &dummy_info
;
159 status
= acpi_get_object_info(subobj_handle
, info_ptr
);
160 if (ACPI_FAILURE(status
))
169 static acpi_status
wmi_method_enable(struct wmi_block
*wblock
, int enable
)
171 struct guid_block
*block
= NULL
;
176 block
= &wblock
->gblock
;
177 handle
= wblock
->acpi_device
->handle
;
179 snprintf(method
, 5, "WE%02X", block
->notify_id
);
180 status
= acpi_execute_simple_method(handle
, method
, enable
);
182 if (status
!= AE_OK
&& status
!= AE_NOT_FOUND
)
189 * Exported WMI functions
192 * wmi_evaluate_method - Evaluate a WMI method
193 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
194 * @instance: Instance index
195 * @method_id: Method ID to call
196 * &in: Buffer containing input for the method call
197 * &out: Empty buffer to return the method results
199 * Call an ACPI-WMI method
201 acpi_status
wmi_evaluate_method(const char *guid_string
, u8 instance
,
202 u32 method_id
, const struct acpi_buffer
*in
, struct acpi_buffer
*out
)
204 struct wmi_block
*wblock
= NULL
;
206 if (!find_guid(guid_string
, &wblock
))
208 return wmidev_evaluate_method(&wblock
->dev
, instance
, method_id
,
211 EXPORT_SYMBOL_GPL(wmi_evaluate_method
);
214 * wmidev_evaluate_method - Evaluate a WMI method
215 * @wdev: A wmi bus device from a driver
216 * @instance: Instance index
217 * @method_id: Method ID to call
218 * &in: Buffer containing input for the method call
219 * &out: Empty buffer to return the method results
221 * Call an ACPI-WMI method
223 acpi_status
wmidev_evaluate_method(struct wmi_device
*wdev
, u8 instance
,
224 u32 method_id
, const struct acpi_buffer
*in
, struct acpi_buffer
*out
)
226 struct guid_block
*block
= NULL
;
227 struct wmi_block
*wblock
= NULL
;
230 struct acpi_object_list input
;
231 union acpi_object params
[3];
232 char method
[5] = "WM";
234 wblock
= container_of(wdev
, struct wmi_block
, dev
);
235 block
= &wblock
->gblock
;
236 handle
= wblock
->acpi_device
->handle
;
238 if (!(block
->flags
& ACPI_WMI_METHOD
))
241 if (block
->instance_count
<= instance
)
242 return AE_BAD_PARAMETER
;
245 input
.pointer
= params
;
246 params
[0].type
= ACPI_TYPE_INTEGER
;
247 params
[0].integer
.value
= instance
;
248 params
[1].type
= ACPI_TYPE_INTEGER
;
249 params
[1].integer
.value
= method_id
;
254 if (block
->flags
& ACPI_WMI_STRING
) {
255 params
[2].type
= ACPI_TYPE_STRING
;
257 params
[2].type
= ACPI_TYPE_BUFFER
;
259 params
[2].buffer
.length
= in
->length
;
260 params
[2].buffer
.pointer
= in
->pointer
;
263 strncat(method
, block
->object_id
, 2);
265 status
= acpi_evaluate_object(handle
, method
, &input
, out
);
269 EXPORT_SYMBOL_GPL(wmidev_evaluate_method
);
271 static acpi_status
__query_block(struct wmi_block
*wblock
, u8 instance
,
272 struct acpi_buffer
*out
)
274 struct guid_block
*block
= NULL
;
276 acpi_status status
, wc_status
= AE_ERROR
;
277 struct acpi_object_list input
;
278 union acpi_object wq_params
[1];
280 char wc_method
[5] = "WC";
283 return AE_BAD_PARAMETER
;
285 block
= &wblock
->gblock
;
286 handle
= wblock
->acpi_device
->handle
;
288 if (block
->instance_count
<= instance
)
289 return AE_BAD_PARAMETER
;
291 /* Check GUID is a data block */
292 if (block
->flags
& (ACPI_WMI_EVENT
| ACPI_WMI_METHOD
))
296 input
.pointer
= wq_params
;
297 wq_params
[0].type
= ACPI_TYPE_INTEGER
;
298 wq_params
[0].integer
.value
= instance
;
300 if (instance
== 0 && wblock
->read_takes_no_args
)
304 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
307 if (block
->flags
& ACPI_WMI_EXPENSIVE
) {
308 strncat(wc_method
, block
->object_id
, 2);
311 * Some GUIDs break the specification by declaring themselves
312 * expensive, but have no corresponding WCxx method. So we
313 * should not fail if this happens.
315 if (acpi_has_method(handle
, wc_method
))
316 wc_status
= acpi_execute_simple_method(handle
,
320 strcpy(method
, "WQ");
321 strncat(method
, block
->object_id
, 2);
323 status
= acpi_evaluate_object(handle
, method
, &input
, out
);
326 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
327 * the WQxx method failed - we should disable collection anyway.
329 if ((block
->flags
& ACPI_WMI_EXPENSIVE
) && ACPI_SUCCESS(wc_status
)) {
330 status
= acpi_execute_simple_method(handle
, wc_method
, 0);
337 * wmi_query_block - Return contents of a WMI block (deprecated)
338 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
339 * @instance: Instance index
340 * &out: Empty buffer to return the contents of the data block to
342 * Return the contents of an ACPI-WMI data block to a buffer
344 acpi_status
wmi_query_block(const char *guid_string
, u8 instance
,
345 struct acpi_buffer
*out
)
347 struct wmi_block
*wblock
;
350 return AE_BAD_PARAMETER
;
352 if (!find_guid(guid_string
, &wblock
))
355 return __query_block(wblock
, instance
, out
);
357 EXPORT_SYMBOL_GPL(wmi_query_block
);
359 union acpi_object
*wmidev_block_query(struct wmi_device
*wdev
, u8 instance
)
361 struct acpi_buffer out
= { ACPI_ALLOCATE_BUFFER
, NULL
};
362 struct wmi_block
*wblock
= container_of(wdev
, struct wmi_block
, dev
);
364 if (ACPI_FAILURE(__query_block(wblock
, instance
, &out
)))
367 return (union acpi_object
*)out
.pointer
;
369 EXPORT_SYMBOL_GPL(wmidev_block_query
);
371 struct wmi_device
*wmidev_get_other_guid(struct wmi_device
*wdev
,
372 const char *guid_string
)
374 struct wmi_block
*this_wb
= container_of(wdev
, struct wmi_block
, dev
);
375 struct wmi_block
*other_wb
;
377 if (!find_guid(guid_string
, &other_wb
))
380 if (other_wb
->acpi_device
!= this_wb
->acpi_device
)
383 get_device(&other_wb
->dev
.dev
);
384 return &other_wb
->dev
;
386 EXPORT_SYMBOL_GPL(wmidev_get_other_guid
);
389 * wmi_set_block - Write to a WMI block
390 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
391 * @instance: Instance index
392 * &in: Buffer containing new values for the data block
394 * Write the contents of the input buffer to an ACPI-WMI data block
396 acpi_status
wmi_set_block(const char *guid_string
, u8 instance
,
397 const struct acpi_buffer
*in
)
399 struct guid_block
*block
= NULL
;
400 struct wmi_block
*wblock
= NULL
;
402 struct acpi_object_list input
;
403 union acpi_object params
[2];
404 char method
[5] = "WS";
406 if (!guid_string
|| !in
)
409 if (!find_guid(guid_string
, &wblock
))
412 block
= &wblock
->gblock
;
413 handle
= wblock
->acpi_device
->handle
;
415 if (block
->instance_count
<= instance
)
416 return AE_BAD_PARAMETER
;
418 /* Check GUID is a data block */
419 if (block
->flags
& (ACPI_WMI_EVENT
| ACPI_WMI_METHOD
))
423 input
.pointer
= params
;
424 params
[0].type
= ACPI_TYPE_INTEGER
;
425 params
[0].integer
.value
= instance
;
427 if (block
->flags
& ACPI_WMI_STRING
) {
428 params
[1].type
= ACPI_TYPE_STRING
;
430 params
[1].type
= ACPI_TYPE_BUFFER
;
432 params
[1].buffer
.length
= in
->length
;
433 params
[1].buffer
.pointer
= in
->pointer
;
435 strncat(method
, block
->object_id
, 2);
437 return acpi_evaluate_object(handle
, method
, &input
, NULL
);
439 EXPORT_SYMBOL_GPL(wmi_set_block
);
441 static void wmi_dump_wdg(const struct guid_block
*g
)
443 pr_info("%pUL:\n", g
->guid
);
444 if (g
->flags
& ACPI_WMI_EVENT
)
445 pr_info("\tnotify_id: 0x%02X\n", g
->notify_id
);
447 pr_info("\tobject_id: %2pE\n", g
->object_id
);
448 pr_info("\tinstance_count: %d\n", g
->instance_count
);
449 pr_info("\tflags: %#x", g
->flags
);
451 if (g
->flags
& ACPI_WMI_EXPENSIVE
)
452 pr_cont(" ACPI_WMI_EXPENSIVE");
453 if (g
->flags
& ACPI_WMI_METHOD
)
454 pr_cont(" ACPI_WMI_METHOD");
455 if (g
->flags
& ACPI_WMI_STRING
)
456 pr_cont(" ACPI_WMI_STRING");
457 if (g
->flags
& ACPI_WMI_EVENT
)
458 pr_cont(" ACPI_WMI_EVENT");
464 static void wmi_notify_debug(u32 value
, void *context
)
466 struct acpi_buffer response
= { ACPI_ALLOCATE_BUFFER
, NULL
};
467 union acpi_object
*obj
;
470 status
= wmi_get_event_data(value
, &response
);
471 if (status
!= AE_OK
) {
472 pr_info("bad event status 0x%x\n", status
);
476 obj
= (union acpi_object
*)response
.pointer
;
481 pr_info("DEBUG Event ");
483 case ACPI_TYPE_BUFFER
:
484 pr_cont("BUFFER_TYPE - length %d\n", obj
->buffer
.length
);
486 case ACPI_TYPE_STRING
:
487 pr_cont("STRING_TYPE - %s\n", obj
->string
.pointer
);
489 case ACPI_TYPE_INTEGER
:
490 pr_cont("INTEGER_TYPE - %llu\n", obj
->integer
.value
);
492 case ACPI_TYPE_PACKAGE
:
493 pr_cont("PACKAGE_TYPE - %d elements\n", obj
->package
.count
);
496 pr_cont("object type 0x%X\n", obj
->type
);
502 * wmi_install_notify_handler - Register handler for WMI events
503 * @handler: Function to handle notifications
504 * @data: Data to be returned to handler when event is fired
506 * Register a handler for events sent to the ACPI-WMI mapper device.
508 acpi_status
wmi_install_notify_handler(const char *guid
,
509 wmi_notify_handler handler
, void *data
)
511 struct wmi_block
*block
;
512 acpi_status status
= AE_NOT_EXIST
;
516 if (!guid
|| !handler
)
517 return AE_BAD_PARAMETER
;
519 if (uuid_le_to_bin(guid
, &guid_input
))
520 return AE_BAD_PARAMETER
;
522 list_for_each(p
, &wmi_block_list
) {
523 acpi_status wmi_status
;
524 block
= list_entry(p
, struct wmi_block
, list
);
526 if (memcmp(block
->gblock
.guid
, &guid_input
, 16) == 0) {
527 if (block
->handler
&&
528 block
->handler
!= wmi_notify_debug
)
529 return AE_ALREADY_ACQUIRED
;
531 block
->handler
= handler
;
532 block
->handler_data
= data
;
534 wmi_status
= wmi_method_enable(block
, 1);
535 if ((wmi_status
!= AE_OK
) ||
536 ((wmi_status
== AE_OK
) && (status
== AE_NOT_EXIST
)))
543 EXPORT_SYMBOL_GPL(wmi_install_notify_handler
);
546 * wmi_uninstall_notify_handler - Unregister handler for WMI events
548 * Unregister handler for events sent to the ACPI-WMI mapper device.
550 acpi_status
wmi_remove_notify_handler(const char *guid
)
552 struct wmi_block
*block
;
553 acpi_status status
= AE_NOT_EXIST
;
558 return AE_BAD_PARAMETER
;
560 if (uuid_le_to_bin(guid
, &guid_input
))
561 return AE_BAD_PARAMETER
;
563 list_for_each(p
, &wmi_block_list
) {
564 acpi_status wmi_status
;
565 block
= list_entry(p
, struct wmi_block
, list
);
567 if (memcmp(block
->gblock
.guid
, &guid_input
, 16) == 0) {
568 if (!block
->handler
||
569 block
->handler
== wmi_notify_debug
)
570 return AE_NULL_ENTRY
;
573 block
->handler
= wmi_notify_debug
;
576 wmi_status
= wmi_method_enable(block
, 0);
577 block
->handler
= NULL
;
578 block
->handler_data
= NULL
;
579 if ((wmi_status
!= AE_OK
) ||
580 ((wmi_status
== AE_OK
) &&
581 (status
== AE_NOT_EXIST
)))
589 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler
);
592 * wmi_get_event_data - Get WMI data associated with an event
594 * @event: Event to find
595 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
597 * Returns extra data associated with an event in WMI.
599 acpi_status
wmi_get_event_data(u32 event
, struct acpi_buffer
*out
)
601 struct acpi_object_list input
;
602 union acpi_object params
[1];
603 struct guid_block
*gblock
;
604 struct wmi_block
*wblock
;
608 input
.pointer
= params
;
609 params
[0].type
= ACPI_TYPE_INTEGER
;
610 params
[0].integer
.value
= event
;
612 list_for_each(p
, &wmi_block_list
) {
613 wblock
= list_entry(p
, struct wmi_block
, list
);
614 gblock
= &wblock
->gblock
;
616 if ((gblock
->flags
& ACPI_WMI_EVENT
) &&
617 (gblock
->notify_id
== event
))
618 return acpi_evaluate_object(wblock
->acpi_device
->handle
,
619 "_WED", &input
, out
);
624 EXPORT_SYMBOL_GPL(wmi_get_event_data
);
627 * wmi_has_guid - Check if a GUID is available
628 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
630 * Check if a given GUID is defined by _WDG
632 bool wmi_has_guid(const char *guid_string
)
634 return find_guid(guid_string
, NULL
);
636 EXPORT_SYMBOL_GPL(wmi_has_guid
);
638 static struct wmi_block
*dev_to_wblock(struct device
*dev
)
640 return container_of(dev
, struct wmi_block
, dev
.dev
);
643 static struct wmi_device
*dev_to_wdev(struct device
*dev
)
645 return container_of(dev
, struct wmi_device
, dev
);
651 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
,
654 struct wmi_block
*wblock
= dev_to_wblock(dev
);
656 return sprintf(buf
, "wmi:%pUL\n", wblock
->gblock
.guid
);
658 static DEVICE_ATTR_RO(modalias
);
660 static ssize_t
guid_show(struct device
*dev
, struct device_attribute
*attr
,
663 struct wmi_block
*wblock
= dev_to_wblock(dev
);
665 return sprintf(buf
, "%pUL\n", wblock
->gblock
.guid
);
667 static DEVICE_ATTR_RO(guid
);
669 static ssize_t
instance_count_show(struct device
*dev
,
670 struct device_attribute
*attr
, char *buf
)
672 struct wmi_block
*wblock
= dev_to_wblock(dev
);
674 return sprintf(buf
, "%d\n", (int)wblock
->gblock
.instance_count
);
676 static DEVICE_ATTR_RO(instance_count
);
678 static ssize_t
expensive_show(struct device
*dev
,
679 struct device_attribute
*attr
, char *buf
)
681 struct wmi_block
*wblock
= dev_to_wblock(dev
);
683 return sprintf(buf
, "%d\n",
684 (wblock
->gblock
.flags
& ACPI_WMI_EXPENSIVE
) != 0);
686 static DEVICE_ATTR_RO(expensive
);
688 static struct attribute
*wmi_attrs
[] = {
689 &dev_attr_modalias
.attr
,
691 &dev_attr_instance_count
.attr
,
692 &dev_attr_expensive
.attr
,
695 ATTRIBUTE_GROUPS(wmi
);
697 static ssize_t
notify_id_show(struct device
*dev
, struct device_attribute
*attr
,
700 struct wmi_block
*wblock
= dev_to_wblock(dev
);
702 return sprintf(buf
, "%02X\n", (unsigned int)wblock
->gblock
.notify_id
);
704 static DEVICE_ATTR_RO(notify_id
);
706 static struct attribute
*wmi_event_attrs
[] = {
707 &dev_attr_notify_id
.attr
,
710 ATTRIBUTE_GROUPS(wmi_event
);
712 static ssize_t
object_id_show(struct device
*dev
, struct device_attribute
*attr
,
715 struct wmi_block
*wblock
= dev_to_wblock(dev
);
717 return sprintf(buf
, "%c%c\n", wblock
->gblock
.object_id
[0],
718 wblock
->gblock
.object_id
[1]);
720 static DEVICE_ATTR_RO(object_id
);
722 static ssize_t
setable_show(struct device
*dev
, struct device_attribute
*attr
,
725 struct wmi_device
*wdev
= dev_to_wdev(dev
);
727 return sprintf(buf
, "%d\n", (int)wdev
->setable
);
729 static DEVICE_ATTR_RO(setable
);
731 static struct attribute
*wmi_data_attrs
[] = {
732 &dev_attr_object_id
.attr
,
733 &dev_attr_setable
.attr
,
736 ATTRIBUTE_GROUPS(wmi_data
);
738 static struct attribute
*wmi_method_attrs
[] = {
739 &dev_attr_object_id
.attr
,
742 ATTRIBUTE_GROUPS(wmi_method
);
744 static int wmi_dev_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
746 struct wmi_block
*wblock
= dev_to_wblock(dev
);
748 if (add_uevent_var(env
, "MODALIAS=wmi:%pUL", wblock
->gblock
.guid
))
751 if (add_uevent_var(env
, "WMI_GUID=%pUL", wblock
->gblock
.guid
))
757 static void wmi_dev_release(struct device
*dev
)
759 struct wmi_block
*wblock
= dev_to_wblock(dev
);
764 static int wmi_dev_match(struct device
*dev
, struct device_driver
*driver
)
766 struct wmi_driver
*wmi_driver
=
767 container_of(driver
, struct wmi_driver
, driver
);
768 struct wmi_block
*wblock
= dev_to_wblock(dev
);
769 const struct wmi_device_id
*id
= wmi_driver
->id_table
;
771 while (id
->guid_string
) {
774 if (WARN_ON(uuid_le_to_bin(id
->guid_string
, &driver_guid
)))
776 if (!memcmp(&driver_guid
, wblock
->gblock
.guid
, 16))
785 static int wmi_dev_probe(struct device
*dev
)
787 struct wmi_block
*wblock
= dev_to_wblock(dev
);
788 struct wmi_driver
*wdriver
=
789 container_of(dev
->driver
, struct wmi_driver
, driver
);
792 if (ACPI_FAILURE(wmi_method_enable(wblock
, 1)))
793 dev_warn(dev
, "failed to enable device -- probing anyway\n");
795 if (wdriver
->probe
) {
796 ret
= wdriver
->probe(dev_to_wdev(dev
));
797 if (ret
!= 0 && ACPI_FAILURE(wmi_method_enable(wblock
, 0)))
798 dev_warn(dev
, "failed to disable device\n");
804 static int wmi_dev_remove(struct device
*dev
)
806 struct wmi_block
*wblock
= dev_to_wblock(dev
);
807 struct wmi_driver
*wdriver
=
808 container_of(dev
->driver
, struct wmi_driver
, driver
);
812 ret
= wdriver
->remove(dev_to_wdev(dev
));
814 if (ACPI_FAILURE(wmi_method_enable(wblock
, 0)))
815 dev_warn(dev
, "failed to disable device\n");
820 static struct class wmi_bus_class
= {
824 static struct bus_type wmi_bus_type
= {
826 .dev_groups
= wmi_groups
,
827 .match
= wmi_dev_match
,
828 .uevent
= wmi_dev_uevent
,
829 .probe
= wmi_dev_probe
,
830 .remove
= wmi_dev_remove
,
833 static struct device_type wmi_type_event
= {
835 .groups
= wmi_event_groups
,
836 .release
= wmi_dev_release
,
839 static struct device_type wmi_type_method
= {
841 .groups
= wmi_method_groups
,
842 .release
= wmi_dev_release
,
845 static struct device_type wmi_type_data
= {
847 .groups
= wmi_data_groups
,
848 .release
= wmi_dev_release
,
851 static int wmi_create_device(struct device
*wmi_bus_dev
,
852 const struct guid_block
*gblock
,
853 struct wmi_block
*wblock
,
854 struct acpi_device
*device
)
856 struct acpi_device_info
*info
;
860 if (gblock
->flags
& ACPI_WMI_EVENT
) {
861 wblock
->dev
.dev
.type
= &wmi_type_event
;
865 if (gblock
->flags
& ACPI_WMI_METHOD
) {
866 wblock
->dev
.dev
.type
= &wmi_type_method
;
871 * Data Block Query Control Method (WQxx by convention) is
872 * required per the WMI documentation. If it is not present,
873 * we ignore this data block.
875 strcpy(method
, "WQ");
876 strncat(method
, wblock
->gblock
.object_id
, 2);
877 result
= get_subobj_info(device
->handle
, method
, &info
);
880 dev_warn(wmi_bus_dev
,
881 "%s data block query control method not found",
886 wblock
->dev
.dev
.type
= &wmi_type_data
;
889 * The Microsoft documentation specifically states:
891 * Data blocks registered with only a single instance
892 * can ignore the parameter.
894 * ACPICA will get mad at us if we call the method with the wrong number
895 * of arguments, so check what our method expects. (On some Dell
896 * laptops, WQxx may not be a method at all.)
898 if (info
->type
!= ACPI_TYPE_METHOD
|| info
->param_count
== 0)
899 wblock
->read_takes_no_args
= true;
903 strcpy(method
, "WS");
904 strncat(method
, wblock
->gblock
.object_id
, 2);
905 result
= get_subobj_info(device
->handle
, method
, NULL
);
908 wblock
->dev
.setable
= true;
911 wblock
->dev
.dev
.bus
= &wmi_bus_type
;
912 wblock
->dev
.dev
.parent
= wmi_bus_dev
;
914 dev_set_name(&wblock
->dev
.dev
, "%pUL", gblock
->guid
);
916 device_initialize(&wblock
->dev
.dev
);
921 static void wmi_free_devices(struct acpi_device
*device
)
923 struct wmi_block
*wblock
, *next
;
925 /* Delete devices for all the GUIDs */
926 list_for_each_entry_safe(wblock
, next
, &wmi_block_list
, list
) {
927 if (wblock
->acpi_device
== device
) {
928 list_del(&wblock
->list
);
929 device_unregister(&wblock
->dev
.dev
);
934 static bool guid_already_parsed(struct acpi_device
*device
,
937 struct wmi_block
*wblock
;
939 list_for_each_entry(wblock
, &wmi_block_list
, list
) {
940 if (memcmp(wblock
->gblock
.guid
, guid
, 16) == 0) {
942 * Because we historically didn't track the relationship
943 * between GUIDs and ACPI nodes, we don't know whether
944 * we need to suppress GUIDs that are unique on a
945 * given node but duplicated across nodes.
947 dev_warn(&device
->dev
, "duplicate WMI GUID %pUL (first instance was on %s)\n",
948 guid
, dev_name(&wblock
->acpi_device
->dev
));
957 * Parse the _WDG method for the GUID data blocks
959 static int parse_wdg(struct device
*wmi_bus_dev
, struct acpi_device
*device
)
961 struct acpi_buffer out
= {ACPI_ALLOCATE_BUFFER
, NULL
};
962 const struct guid_block
*gblock
;
963 struct wmi_block
*wblock
, *next
;
964 union acpi_object
*obj
;
969 status
= acpi_evaluate_object(device
->handle
, "_WDG", NULL
, &out
);
970 if (ACPI_FAILURE(status
))
973 obj
= (union acpi_object
*) out
.pointer
;
977 if (obj
->type
!= ACPI_TYPE_BUFFER
) {
979 goto out_free_pointer
;
982 gblock
= (const struct guid_block
*)obj
->buffer
.pointer
;
983 total
= obj
->buffer
.length
/ sizeof(struct guid_block
);
985 for (i
= 0; i
< total
; i
++) {
987 wmi_dump_wdg(&gblock
[i
]);
990 * Some WMI devices, like those for nVidia hooks, have a
991 * duplicate GUID. It's not clear what we should do in this
992 * case yet, so for now, we'll just ignore the duplicate
993 * for device creation.
995 if (guid_already_parsed(device
, gblock
[i
].guid
))
998 wblock
= kzalloc(sizeof(struct wmi_block
), GFP_KERNEL
);
1004 wblock
->acpi_device
= device
;
1005 wblock
->gblock
= gblock
[i
];
1007 retval
= wmi_create_device(wmi_bus_dev
, &gblock
[i
], wblock
, device
);
1013 list_add_tail(&wblock
->list
, &wmi_block_list
);
1016 wblock
->handler
= wmi_notify_debug
;
1017 wmi_method_enable(wblock
, 1);
1022 * Now that all of the devices are created, add them to the
1023 * device tree and probe subdrivers.
1025 list_for_each_entry_safe(wblock
, next
, &wmi_block_list
, list
) {
1026 if (wblock
->acpi_device
!= device
)
1029 retval
= device_add(&wblock
->dev
.dev
);
1031 dev_err(wmi_bus_dev
, "failed to register %pULL\n",
1032 wblock
->gblock
.guid
);
1034 wmi_method_enable(wblock
, 0);
1035 list_del(&wblock
->list
);
1036 put_device(&wblock
->dev
.dev
);
1046 * WMI can have EmbeddedControl access regions. In which case, we just want to
1047 * hand these off to the EC driver.
1050 acpi_wmi_ec_space_handler(u32 function
, acpi_physical_address address
,
1051 u32 bits
, u64
*value
,
1052 void *handler_context
, void *region_context
)
1054 int result
= 0, i
= 0;
1057 if ((address
> 0xFF) || !value
)
1058 return AE_BAD_PARAMETER
;
1060 if (function
!= ACPI_READ
&& function
!= ACPI_WRITE
)
1061 return AE_BAD_PARAMETER
;
1064 return AE_BAD_PARAMETER
;
1066 if (function
== ACPI_READ
) {
1067 result
= ec_read(address
, &temp
);
1068 (*value
) |= ((u64
)temp
) << i
;
1070 temp
= 0xff & ((*value
) >> i
);
1071 result
= ec_write(address
, temp
);
1076 return AE_BAD_PARAMETER
;
1079 return AE_NOT_FOUND
;
1089 static void acpi_wmi_notify_handler(acpi_handle handle
, u32 event
,
1092 struct guid_block
*block
;
1093 struct wmi_block
*wblock
;
1094 struct list_head
*p
;
1095 bool found_it
= false;
1097 list_for_each(p
, &wmi_block_list
) {
1098 wblock
= list_entry(p
, struct wmi_block
, list
);
1099 block
= &wblock
->gblock
;
1101 if (wblock
->acpi_device
->handle
== handle
&&
1102 (block
->flags
& ACPI_WMI_EVENT
) &&
1103 (block
->notify_id
== event
))
1113 /* If a driver is bound, then notify the driver. */
1114 if (wblock
->dev
.dev
.driver
) {
1115 struct wmi_driver
*driver
;
1116 struct acpi_object_list input
;
1117 union acpi_object params
[1];
1118 struct acpi_buffer evdata
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1121 driver
= container_of(wblock
->dev
.dev
.driver
,
1122 struct wmi_driver
, driver
);
1125 input
.pointer
= params
;
1126 params
[0].type
= ACPI_TYPE_INTEGER
;
1127 params
[0].integer
.value
= event
;
1129 status
= acpi_evaluate_object(wblock
->acpi_device
->handle
,
1130 "_WED", &input
, &evdata
);
1131 if (ACPI_FAILURE(status
)) {
1132 dev_warn(&wblock
->dev
.dev
,
1133 "failed to get event data\n");
1138 driver
->notify(&wblock
->dev
,
1139 (union acpi_object
*)evdata
.pointer
);
1141 kfree(evdata
.pointer
);
1142 } else if (wblock
->handler
) {
1143 /* Legacy handler */
1144 wblock
->handler(event
, wblock
->handler_data
);
1148 pr_info("DEBUG Event GUID: %pUL\n",
1149 wblock
->gblock
.guid
);
1152 acpi_bus_generate_netlink_event(
1153 wblock
->acpi_device
->pnp
.device_class
,
1154 dev_name(&wblock
->dev
.dev
),
1159 static int acpi_wmi_remove(struct platform_device
*device
)
1161 struct acpi_device
*acpi_device
= ACPI_COMPANION(&device
->dev
);
1163 acpi_remove_notify_handler(acpi_device
->handle
, ACPI_DEVICE_NOTIFY
,
1164 acpi_wmi_notify_handler
);
1165 acpi_remove_address_space_handler(acpi_device
->handle
,
1166 ACPI_ADR_SPACE_EC
, &acpi_wmi_ec_space_handler
);
1167 wmi_free_devices(acpi_device
);
1168 device_destroy(&wmi_bus_class
, MKDEV(0, 0));
1173 static int acpi_wmi_probe(struct platform_device
*device
)
1175 struct acpi_device
*acpi_device
;
1176 struct device
*wmi_bus_dev
;
1180 acpi_device
= ACPI_COMPANION(&device
->dev
);
1182 dev_err(&device
->dev
, "ACPI companion is missing\n");
1186 status
= acpi_install_address_space_handler(acpi_device
->handle
,
1188 &acpi_wmi_ec_space_handler
,
1190 if (ACPI_FAILURE(status
)) {
1191 dev_err(&device
->dev
, "Error installing EC region handler\n");
1195 status
= acpi_install_notify_handler(acpi_device
->handle
,
1197 acpi_wmi_notify_handler
,
1199 if (ACPI_FAILURE(status
)) {
1200 dev_err(&device
->dev
, "Error installing notify handler\n");
1202 goto err_remove_ec_handler
;
1205 wmi_bus_dev
= device_create(&wmi_bus_class
, &device
->dev
, MKDEV(0, 0),
1206 NULL
, "wmi_bus-%s", dev_name(&device
->dev
));
1207 if (IS_ERR(wmi_bus_dev
)) {
1208 error
= PTR_ERR(wmi_bus_dev
);
1209 goto err_remove_notify_handler
;
1211 dev_set_drvdata(&device
->dev
, wmi_bus_dev
);
1213 error
= parse_wdg(wmi_bus_dev
, acpi_device
);
1215 pr_err("Failed to parse WDG method\n");
1216 goto err_remove_busdev
;
1222 device_destroy(&wmi_bus_class
, MKDEV(0, 0));
1224 err_remove_notify_handler
:
1225 acpi_remove_notify_handler(acpi_device
->handle
, ACPI_DEVICE_NOTIFY
,
1226 acpi_wmi_notify_handler
);
1228 err_remove_ec_handler
:
1229 acpi_remove_address_space_handler(acpi_device
->handle
,
1231 &acpi_wmi_ec_space_handler
);
1236 int __must_check
__wmi_driver_register(struct wmi_driver
*driver
,
1237 struct module
*owner
)
1239 driver
->driver
.owner
= owner
;
1240 driver
->driver
.bus
= &wmi_bus_type
;
1242 return driver_register(&driver
->driver
);
1244 EXPORT_SYMBOL(__wmi_driver_register
);
1246 void wmi_driver_unregister(struct wmi_driver
*driver
)
1248 driver_unregister(&driver
->driver
);
1250 EXPORT_SYMBOL(wmi_driver_unregister
);
1252 static int __init
acpi_wmi_init(void)
1259 error
= class_register(&wmi_bus_class
);
1263 error
= bus_register(&wmi_bus_type
);
1265 goto err_unreg_class
;
1267 error
= platform_driver_register(&acpi_wmi_driver
);
1269 pr_err("Error loading mapper\n");
1276 bus_unregister(&wmi_bus_type
);
1279 class_unregister(&wmi_bus_class
);
1284 static void __exit
acpi_wmi_exit(void)
1286 platform_driver_unregister(&acpi_wmi_driver
);
1287 bus_unregister(&wmi_bus_type
);
1288 class_unregister(&wmi_bus_class
);
1291 subsys_initcall(acpi_wmi_init
);
1292 module_exit(acpi_wmi_exit
);