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