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