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