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