blob: 22f4b92c5da4fc0afe1293acf9f0987b42bb29af [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Carlos Corbachobff431e2008-02-05 02:17:04 +00002/*
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)2c9c5662017-06-06 16:40:56 -070012 * 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 Corbachobff431e2008-02-05 02:17:04 +000015 */
16
Dmitry Torokhov8e075142010-08-26 00:15:19 -070017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Carlos Corbachobff431e2008-02-05 02:17:04 +000019#include <linux/acpi.h>
Mario Limonciellob60ee4e2017-09-26 13:50:03 -050020#include <linux/device.h>
21#include <linux/init.h>
22#include <linux/kernel.h>
23#include <linux/list.h>
Mario Limonciello44b6b762017-11-01 14:25:35 -050024#include <linux/miscdevice.h>
Paul Gortmaker7c52d552011-05-27 12:33:10 -040025#include <linux/module.h>
Andy Lutomirski9599ed92015-11-27 11:56:02 -080026#include <linux/platform_device.h>
Mario Limonciellob60ee4e2017-09-26 13:50:03 -050027#include <linux/slab.h>
28#include <linux/types.h>
Mario Limonciello44b6b762017-11-01 14:25:35 -050029#include <linux/uaccess.h>
Andy Shevchenko538d7eb2016-05-20 17:01:30 -070030#include <linux/uuid.h>
Mario Limonciellob60ee4e2017-09-26 13:50:03 -050031#include <linux/wmi.h>
Mario Limonciello44b6b762017-11-01 14:25:35 -050032#include <uapi/linux/wmi.h>
Carlos Corbachobff431e2008-02-05 02:17:04 +000033
34ACPI_MODULE_NAME("wmi");
35MODULE_AUTHOR("Carlos Corbacho");
36MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
37MODULE_LICENSE("GPL");
38
Dmitry Torokhov762e1a22010-08-26 00:15:14 -070039static LIST_HEAD(wmi_block_list);
Carlos Corbachobff431e2008-02-05 02:17:04 +000040
41struct 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
54struct wmi_block {
Andy Lutomirski844af952015-11-24 19:49:23 -080055 struct wmi_device dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +000056 struct list_head list;
57 struct guid_block gblock;
Mario Limonciello44b6b762017-11-01 14:25:35 -050058 struct miscdevice char_dev;
59 struct mutex char_mutex;
Andy Lutomirskib0e86302015-11-24 19:50:01 -080060 struct acpi_device *acpi_device;
Carlos Corbachobff431e2008-02-05 02:17:04 +000061 wmi_notify_handler handler;
62 void *handler_data;
Mario Limonciello44b6b762017-11-01 14:25:35 -050063 u64 req_buf_size;
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -080064
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -070065 bool read_takes_no_args;
Carlos Corbachobff431e2008-02-05 02:17:04 +000066};
67
Carlos Corbachobff431e2008-02-05 02:17:04 +000068
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 Russell90ab5ee2012-01-13 09:32:20 +103078static bool debug_event;
Thomas Renningerfc3155b2010-05-03 15:30:15 +020079module_param(debug_event, bool, 0444);
80MODULE_PARM_DESC(debug_event,
81 "Log WMI Events [0/1]");
82
Rusty Russell90ab5ee2012-01-13 09:32:20 +103083static bool debug_dump_wdg;
Thomas Renningera929aae2010-05-03 15:30:17 +020084module_param(debug_dump_wdg, bool, 0444);
85MODULE_PARM_DESC(debug_dump_wdg,
86 "Dump available WMI interfaces [0/1]");
87
Andy Lutomirski9599ed92015-11-27 11:56:02 -080088static int acpi_wmi_remove(struct platform_device *device);
89static int acpi_wmi_probe(struct platform_device *device);
Carlos Corbachobff431e2008-02-05 02:17:04 +000090
91static const struct acpi_device_id wmi_device_ids[] = {
92 {"PNP0C14", 0},
93 {"pnp0c14", 0},
94 {"", 0},
95};
96MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
97
Andy Lutomirski9599ed92015-11-27 11:56:02 -080098static struct platform_driver acpi_wmi_driver = {
99 .driver = {
100 .name = "acpi-wmi",
101 .acpi_match_table = wmi_device_ids,
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700102 },
Andy Lutomirski9599ed92015-11-27 11:56:02 -0800103 .probe = acpi_wmi_probe,
104 .remove = acpi_wmi_remove,
Carlos Corbachobff431e2008-02-05 02:17:04 +0000105};
106
107/*
108 * GUID parsing functions
109 */
110
Carlos Corbachobff431e2008-02-05 02:17:04 +0000111static bool find_guid(const char *guid_string, struct wmi_block **out)
112{
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700113 uuid_le guid_input;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000114 struct wmi_block *wblock;
115 struct guid_block *block;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000116
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700117 if (uuid_le_to_bin(guid_string, &guid_input))
118 return false;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000119
Andy Shevchenkocedb3b22018-02-19 17:02:28 +0200120 list_for_each_entry(wblock, &wmi_block_list, list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000121 block = &wblock->gblock;
122
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700123 if (memcmp(block->guid, &guid_input, 16) == 0) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000124 if (out)
125 *out = wblock;
Joe Perches097c27f2015-03-30 10:43:20 -0700126 return true;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000127 }
128 }
Joe Perches097c27f2015-03-30 10:43:20 -0700129 return false;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000130}
131
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800132static 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 Garretta66bfa72008-10-08 21:40:32 +0100156static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
157{
158 struct guid_block *block = NULL;
159 char method[5];
Matthew Garretta66bfa72008-10-08 21:40:32 +0100160 acpi_status status;
161 acpi_handle handle;
162
163 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800164 handle = wblock->acpi_device->handle;
Matthew Garretta66bfa72008-10-08 21:40:32 +0100165
Matthew Garretta66bfa72008-10-08 21:40:32 +0100166 snprintf(method, 5, "WE%02X", block->notify_id);
Zhang Rui8122ab662013-09-03 08:31:57 +0800167 status = acpi_execute_simple_method(handle, method, enable);
Matthew Garretta66bfa72008-10-08 21:40:32 +0100168
169 if (status != AE_OK && status != AE_NOT_FOUND)
170 return status;
171 else
172 return AE_OK;
173}
174
Carlos Corbachobff431e2008-02-05 02:17:04 +0000175/*
176 * Exported WMI functions
177 */
Mario Limonciello44b6b762017-11-01 14:25:35 -0500178
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 */
186int 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}
195EXPORT_SYMBOL_GPL(set_required_buffer_size);
196
Carlos Corbachobff431e2008-02-05 02:17:04 +0000197/**
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 */
207acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
208u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
209{
Mario Limonciello722c8562017-11-01 14:25:23 -0500210 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}
217EXPORT_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 */
229acpi_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 Corbachobff431e2008-02-05 02:17:04 +0000232 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 Leandrof3d83e22009-08-26 14:29:28 -0700238 char method[5] = "WM";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000239
Mario Limonciello722c8562017-11-01 14:25:23 -0500240 wblock = container_of(wdev, struct wmi_block, dev);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000241 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800242 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000243
Al Viroe6bafba2008-02-13 04:03:25 +0000244 if (!(block->flags & ACPI_WMI_METHOD))
Carlos Corbachobff431e2008-02-05 02:17:04 +0000245 return AE_BAD_DATA;
246
Pali Rohár6afa1e22017-08-12 09:44:18 +0200247 if (block->instance_count <= instance)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000248 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 Limonciello722c8562017-11-01 14:25:23 -0500275EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000276
Andy Lutomirski56a37022015-11-25 18:19:26 -0800277static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
278 struct acpi_buffer *out)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000279{
280 struct guid_block *block = NULL;
Zhang Rui54f14c22013-09-03 08:32:07 +0800281 acpi_handle handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000282 acpi_status status, wc_status = AE_ERROR;
Zhang Rui8122ab662013-09-03 08:31:57 +0800283 struct acpi_object_list input;
284 union acpi_object wq_params[1];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700285 char method[5];
286 char wc_method[5] = "WC";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000287
Andy Lutomirski56a37022015-11-25 18:19:26 -0800288 if (!out)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000289 return AE_BAD_PARAMETER;
290
Carlos Corbachobff431e2008-02-05 02:17:04 +0000291 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800292 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000293
Pali Rohár6afa1e22017-08-12 09:44:18 +0200294 if (block->instance_count <= instance)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000295 return AE_BAD_PARAMETER;
296
297 /* Check GUID is a data block */
298 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800299 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000300
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 Lutomirskid4fc91a2015-11-25 14:03:43 -0800306 if (instance == 0 && wblock->read_takes_no_args)
307 input.count = 0;
308
Carlos Corbachobff431e2008-02-05 02:17:04 +0000309 /*
310 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
311 * enable collection.
312 */
313 if (block->flags & ACPI_WMI_EXPENSIVE) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000314 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 Rui54f14c22013-09-03 08:32:07 +0800321 if (acpi_has_method(handle, wc_method))
Zhang Rui8122ab662013-09-03 08:31:57 +0800322 wc_status = acpi_execute_simple_method(handle,
323 wc_method, 1);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000324 }
325
326 strcpy(method, "WQ");
327 strncat(method, block->object_id, 2);
328
Carlos Corbachodab36ad2008-08-02 17:28:45 +0100329 status = acpi_evaluate_object(handle, method, &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000330
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 Corbachoa527f2d2008-02-24 13:34:34 +0000335 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
Zhang Rui8122ab662013-09-03 08:31:57 +0800336 status = acpi_execute_simple_method(handle, wc_method, 0);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000337 }
338
339 return status;
340}
Andy Lutomirski56a37022015-11-25 18:19:26 -0800341
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 */
350acpi_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 Corbachobff431e2008-02-05 02:17:04 +0000363EXPORT_SYMBOL_GPL(wmi_query_block);
364
Andy Lutomirski56a37022015-11-25 18:19:26 -0800365union 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}
375EXPORT_SYMBOL_GPL(wmidev_block_query);
376
Carlos Corbachobff431e2008-02-05 02:17:04 +0000377/**
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 */
385acpi_status wmi_set_block(const char *guid_string, u8 instance,
Andy Lutomirski56a37022015-11-25 18:19:26 -0800386 const struct acpi_buffer *in)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000387{
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 Leandrof3d83e22009-08-26 14:29:28 -0700393 char method[5] = "WS";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000394
395 if (!guid_string || !in)
396 return AE_BAD_DATA;
397
398 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800399 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000400
401 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800402 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000403
Pali Rohár6afa1e22017-08-12 09:44:18 +0200404 if (block->instance_count <= instance)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000405 return AE_BAD_PARAMETER;
406
407 /* Check GUID is a data block */
408 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800409 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000410
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}
428EXPORT_SYMBOL_GPL(wmi_set_block);
429
Dmitry Torokhov37830662010-08-26 00:15:09 -0700430static void wmi_dump_wdg(const struct guid_block *g)
Thomas Renningera929aae2010-05-03 15:30:17 +0200431{
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200432 pr_info("%pUL:\n", g->guid);
Pali Rohárcd3921f2017-06-10 12:57:11 +0200433 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 Torokhov8e075142010-08-26 00:15:19 -0700437 pr_info("\tinstance_count: %d\n", g->instance_count);
Joe Perchesdd8e9082011-03-29 15:21:53 -0700438 pr_info("\tflags: %#x", g->flags);
Thomas Renningera929aae2010-05-03 15:30:17 +0200439 if (g->flags) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200440 if (g->flags & ACPI_WMI_EXPENSIVE)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700441 pr_cont(" ACPI_WMI_EXPENSIVE");
Thomas Renningera929aae2010-05-03 15:30:17 +0200442 if (g->flags & ACPI_WMI_METHOD)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700443 pr_cont(" ACPI_WMI_METHOD");
Thomas Renningera929aae2010-05-03 15:30:17 +0200444 if (g->flags & ACPI_WMI_STRING)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700445 pr_cont(" ACPI_WMI_STRING");
Thomas Renningera929aae2010-05-03 15:30:17 +0200446 if (g->flags & ACPI_WMI_EVENT)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700447 pr_cont(" ACPI_WMI_EVENT");
Thomas Renningera929aae2010-05-03 15:30:17 +0200448 }
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700449 pr_cont("\n");
Thomas Renningera929aae2010-05-03 15:30:17 +0200450
451}
452
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200453static void wmi_notify_debug(u32 value, void *context)
454{
455 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
456 union acpi_object *obj;
Axel Lin14926162010-06-28 09:30:45 +0800457 acpi_status status;
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200458
Axel Lin14926162010-06-28 09:30:45 +0800459 status = wmi_get_event_data(value, &response);
460 if (status != AE_OK) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700461 pr_info("bad event status 0x%x\n", status);
Axel Lin14926162010-06-28 09:30:45 +0800462 return;
463 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200464
465 obj = (union acpi_object *)response.pointer;
466
467 if (!obj)
468 return;
469
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700470 pr_info("DEBUG Event ");
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200471 switch(obj->type) {
472 case ACPI_TYPE_BUFFER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700473 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200474 break;
475 case ACPI_TYPE_STRING:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700476 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200477 break;
478 case ACPI_TYPE_INTEGER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700479 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200480 break;
481 case ACPI_TYPE_PACKAGE:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700482 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200483 break;
484 default:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700485 pr_cont("object type 0x%X\n", obj->type);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200486 }
Axel Lin14926162010-06-28 09:30:45 +0800487 kfree(obj);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200488}
489
Carlos Corbachobff431e2008-02-05 02:17:04 +0000490/**
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 */
497acpi_status wmi_install_notify_handler(const char *guid,
498wmi_notify_handler handler, void *data)
499{
500 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000501 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700502 uuid_le guid_input;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000503
504 if (!guid || !handler)
505 return AE_BAD_PARAMETER;
506
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700507 if (uuid_le_to_bin(guid, &guid_input))
508 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000509
Andy Shevchenkocedb3b22018-02-19 17:02:28 +0200510 list_for_each_entry(block, &wmi_block_list, list) {
Colin King58f64252010-11-19 15:40:02 +0000511 acpi_status wmi_status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000512
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700513 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000514 if (block->handler &&
515 block->handler != wmi_notify_debug)
516 return AE_ALREADY_ACQUIRED;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000517
Colin King58f64252010-11-19 15:40:02 +0000518 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 Garretta66bfa72008-10-08 21:40:32 +0100527
528 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000529}
530EXPORT_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 */
537acpi_status wmi_remove_notify_handler(const char *guid)
538{
539 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000540 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700541 uuid_le guid_input;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000542
543 if (!guid)
544 return AE_BAD_PARAMETER;
545
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700546 if (uuid_le_to_bin(guid, &guid_input))
547 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000548
Andy Shevchenkocedb3b22018-02-19 17:02:28 +0200549 list_for_each_entry(block, &wmi_block_list, list) {
Colin King58f64252010-11-19 15:40:02 +0000550 acpi_status wmi_status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000551
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700552 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000553 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 Renningerfc3155b2010-05-03 15:30:15 +0200570 }
Colin King58f64252010-11-19 15:40:02 +0000571
Matthew Garretta66bfa72008-10-08 21:40:32 +0100572 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000573}
574EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
575
576/**
577 * wmi_get_event_data - Get WMI data associated with an event
578 *
Anisse Astier3e9b9882009-12-04 10:10:09 +0100579 * @event: Event to find
580 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
Carlos Corbachobff431e2008-02-05 02:17:04 +0000581 *
582 * Returns extra data associated with an event in WMI.
583 */
584acpi_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 Corbachobff431e2008-02-05 02:17:04 +0000590
591 input.count = 1;
592 input.pointer = params;
593 params[0].type = ACPI_TYPE_INTEGER;
594 params[0].integer.value = event;
595
Andy Shevchenkocedb3b22018-02-19 17:02:28 +0200596 list_for_each_entry(wblock, &wmi_block_list, list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000597 gblock = &wblock->gblock;
598
599 if ((gblock->flags & ACPI_WMI_EVENT) &&
600 (gblock->notify_id == event))
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800601 return acpi_evaluate_object(wblock->acpi_device->handle,
602 "_WED", &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000603 }
604
605 return AE_NOT_FOUND;
606}
607EXPORT_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 */
615bool wmi_has_guid(const char *guid_string)
616{
617 return find_guid(guid_string, NULL);
618}
619EXPORT_SYMBOL_GPL(wmi_has_guid);
620
Andy Lutomirski844af952015-11-24 19:49:23 -0800621static struct wmi_block *dev_to_wblock(struct device *dev)
622{
623 return container_of(dev, struct wmi_block, dev.dev);
624}
625
626static struct wmi_device *dev_to_wdev(struct device *dev)
627{
628 return container_of(dev, struct wmi_device, dev);
629}
630
Carlos Corbachobff431e2008-02-05 02:17:04 +0000631/*
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500632 * sysfs interface
633 */
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700634static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500635 char *buf)
636{
Andy Lutomirski844af952015-11-24 19:49:23 -0800637 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500638
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200639 return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500640}
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700641static DEVICE_ATTR_RO(modalias);
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700642
Andy Lutomirski844af952015-11-24 19:49:23 -0800643static 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}
650static DEVICE_ATTR_RO(guid);
651
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800652static 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}
659static DEVICE_ATTR_RO(instance_count);
660
661static 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}
669static DEVICE_ATTR_RO(expensive);
670
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700671static struct attribute *wmi_attrs[] = {
672 &dev_attr_modalias.attr,
Andy Lutomirski844af952015-11-24 19:49:23 -0800673 &dev_attr_guid.attr,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800674 &dev_attr_instance_count.attr,
675 &dev_attr_expensive.attr,
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700676 NULL,
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700677};
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700678ATTRIBUTE_GROUPS(wmi);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500679
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800680static 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}
687static DEVICE_ATTR_RO(notify_id);
688
689static struct attribute *wmi_event_attrs[] = {
690 &dev_attr_notify_id.attr,
691 NULL,
692};
693ATTRIBUTE_GROUPS(wmi_event);
694
695static 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}
703static DEVICE_ATTR_RO(object_id);
704
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700705static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
706 char *buf)
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800707{
708 struct wmi_device *wdev = dev_to_wdev(dev);
709
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700710 return sprintf(buf, "%d\n", (int)wdev->setable);
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800711}
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700712static DEVICE_ATTR_RO(setable);
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800713
714static struct attribute *wmi_data_attrs[] = {
715 &dev_attr_object_id.attr,
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700716 &dev_attr_setable.attr,
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800717 NULL,
718};
719ATTRIBUTE_GROUPS(wmi_data);
720
721static struct attribute *wmi_method_attrs[] = {
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800722 &dev_attr_object_id.attr,
723 NULL,
724};
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800725ATTRIBUTE_GROUPS(wmi_method);
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800726
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500727static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
728{
Andy Lutomirski844af952015-11-24 19:49:23 -0800729 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500730
Andy Lutomirski844af952015-11-24 19:49:23 -0800731 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500732 return -ENOMEM;
733
Andy Lutomirski844af952015-11-24 19:49:23 -0800734 if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500735 return -ENOMEM;
736
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500737 return 0;
738}
739
Andy Lutomirski844af952015-11-24 19:49:23 -0800740static void wmi_dev_release(struct device *dev)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500741{
Andy Lutomirski844af952015-11-24 19:49:23 -0800742 struct wmi_block *wblock = dev_to_wblock(dev);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700743
Andy Lutomirski844af952015-11-24 19:49:23 -0800744 kfree(wblock);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500745}
746
Andy Lutomirski844af952015-11-24 19:49:23 -0800747static 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 Jacobssonc355ec62019-01-30 16:14:24 +0100754 if (id == NULL)
755 return 0;
756
Mattias Jacobssoneacc95e2019-02-19 20:59:49 +0100757 while (*id->guid_string) {
Andy Lutomirski844af952015-11-24 19:49:23 -0800758 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 Limonciello44b6b762017-11-01 14:25:35 -0500770static 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
791static 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
801static 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 Limonciello5e3e2292017-11-05 21:34:33 -0600849 if (!try_module_get(wdriver->driver.owner)) {
850 ret = -EBUSY;
851 goto out_ioctl;
852 }
Mario Limonciello44b6b762017-11-01 14:25:35 -0500853 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
865out_ioctl:
866 mutex_unlock(&wblock->char_mutex);
867 return ret;
868}
869
870static 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 Lutomirski844af952015-11-24 19:49:23 -0800877
878static 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 Limonciello44b6b762017-11-01 14:25:35 -0500884 char *buf;
Andy Lutomirski844af952015-11-24 19:49:23 -0800885
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 Limonciello44b6b762017-11-01 14:25:35 -0500891 if (ret != 0)
892 goto probe_failure;
Andy Lutomirski844af952015-11-24 19:49:23 -0800893 }
894
Mario Limonciello44b6b762017-11-01 14:25:35 -0500895 /* 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 Cook6fb74102018-06-20 14:31:41 -0700905 wblock->handler_data = kmalloc(wblock->req_buf_size,
906 GFP_KERNEL);
Mario Limonciello44b6b762017-11-01 14:25:35 -0500907 if (!wblock->handler_data) {
908 ret = -ENOMEM;
909 goto probe_failure;
910 }
911
Andy Shevchenko7f166ad2018-02-16 17:40:24 +0200912 buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name);
Mario Limonciello44b6b762017-11-01 14:25:35 -0500913 if (!buf) {
914 ret = -ENOMEM;
915 goto probe_string_failure;
916 }
Mario Limonciello44b6b762017-11-01 14:25:35 -0500917 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 Perches501f7e52018-03-01 08:08:23 -0800923 dev_warn(dev, "failed to register char dev: %d\n", ret);
Mario Limonciello44b6b762017-11-01 14:25:35 -0500924 ret = -ENOMEM;
925 goto probe_misc_failure;
926 }
927 }
928
929 return 0;
930
931probe_misc_failure:
932 kfree(buf);
933probe_string_failure:
934 kfree(wblock->handler_data);
935probe_failure:
936 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
937 dev_warn(dev, "failed to disable device\n");
Andy Lutomirski844af952015-11-24 19:49:23 -0800938 return ret;
939}
940
941static 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 Limonciello44b6b762017-11-01 14:25:35 -0500948 if (wdriver->filter_callback) {
949 misc_deregister(&wblock->char_dev);
950 kfree(wblock->char_dev.name);
Kees Cook6fb74102018-06-20 14:31:41 -0700951 kfree(wblock->handler_data);
Mario Limonciello44b6b762017-11-01 14:25:35 -0500952 }
953
Andy Lutomirski844af952015-11-24 19:49:23 -0800954 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
963static struct class wmi_bus_class = {
964 .name = "wmi_bus",
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500965};
966
Andy Lutomirski844af952015-11-24 19:49:23 -0800967static 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 Goyal69372c12018-10-28 11:37:00 +0530976static const struct device_type wmi_type_event = {
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800977 .name = "event",
978 .groups = wmi_event_groups,
979 .release = wmi_dev_release,
980};
981
Bhumika Goyal69372c12018-10-28 11:37:00 +0530982static const struct device_type wmi_type_method = {
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800983 .name = "method",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800984 .groups = wmi_method_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800985 .release = wmi_dev_release,
986};
987
Bhumika Goyal69372c12018-10-28 11:37:00 +0530988static const struct device_type wmi_type_data = {
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800989 .name = "data",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800990 .groups = wmi_data_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800991 .release = wmi_dev_release,
992};
993
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700994static int wmi_create_device(struct device *wmi_bus_dev,
Andy Lutomirski844af952015-11-24 19:49:23 -0800995 const struct guid_block *gblock,
Andy Lutomirski7f5809bf2015-11-19 14:44:46 -0800996 struct wmi_block *wblock,
997 struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500998{
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700999 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 Limonciello44b6b762017-11-01 14:25:35 -05001010 mutex_init(&wblock->char_mutex);
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -07001011 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 Perches501f7e52018-03-01 08:08:23 -08001025 "%s data block query control method not found\n",
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -07001026 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 Lutomirski844af952015-11-24 19:49:23 -08001055 wblock->dev.dev.bus = &wmi_bus_type;
1056 wblock->dev.dev.parent = wmi_bus_dev;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001057
Andy Lutomirski844af952015-11-24 19:49:23 -08001058 dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001059
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001060 device_initialize(&wblock->dev.dev);
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -07001061
1062 return 0;
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001063}
1064
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001065static void wmi_free_devices(struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001066{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001067 struct wmi_block *wblock, *next;
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001068
1069 /* Delete devices for all the GUIDs */
Dmitry Torokhov023b9562011-09-07 15:00:02 -07001070 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001071 if (wblock->acpi_device == device) {
1072 list_del(&wblock->list);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001073 device_unregister(&wblock->dev.dev);
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001074 }
Dmitry Torokhov023b9562011-09-07 15:00:02 -07001075 }
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001076}
1077
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001078static bool guid_already_parsed(struct acpi_device *device,
1079 const u8 *guid)
Carlos Corbachod1f9e492009-12-26 19:14:59 +00001080{
Carlos Corbachod1f9e492009-12-26 19:14:59 +00001081 struct wmi_block *wblock;
Carlos Corbachod1f9e492009-12-26 19:14:59 +00001082
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001083 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 Corbachod1f9e492009-12-26 19:14:59 +00001093 return true;
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001094 }
1095 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001096
Carlos Corbachod1f9e492009-12-26 19:14:59 +00001097 return false;
1098}
1099
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001100/*
Carlos Corbachobff431e2008-02-05 02:17:04 +00001101 * Parse the _WDG method for the GUID data blocks
1102 */
Andy Lutomirski844af952015-11-24 19:49:23 -08001103static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001104{
1105 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
Dmitry Torokhov37830662010-08-26 00:15:09 -07001106 const struct guid_block *gblock;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001107 struct wmi_block *wblock, *next;
1108 union acpi_object *obj;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001109 acpi_status status;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001110 int retval = 0;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001111 u32 i, total;
1112
Andy Lutomirski7f5809bf2015-11-19 14:44:46 -08001113 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
Carlos Corbachobff431e2008-02-05 02:17:04 +00001114 if (ACPI_FAILURE(status))
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001115 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001116
1117 obj = (union acpi_object *) out.pointer;
Dmitry Torokhov3d2c63e2010-08-26 00:15:03 -07001118 if (!obj)
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001119 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001120
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -07001121 if (obj->type != ACPI_TYPE_BUFFER) {
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001122 retval = -ENXIO;
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -07001123 goto out_free_pointer;
1124 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001125
Dmitry Torokhov37830662010-08-26 00:15:09 -07001126 gblock = (const struct guid_block *)obj->buffer.pointer;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001127 total = obj->buffer.length / sizeof(struct guid_block);
1128
Carlos Corbachobff431e2008-02-05 02:17:04 +00001129 for (i = 0; i < total; i++) {
Thomas Renningera929aae2010-05-03 15:30:17 +02001130 if (debug_dump_wdg)
1131 wmi_dump_wdg(&gblock[i]);
1132
Andy Lutomirskia1c31bcd2015-11-25 08:24:42 -08001133 /*
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 King58f64252010-11-19 15:40:02 +00001142 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001143 if (!wblock) {
1144 retval = -ENOMEM;
1145 break;
1146 }
Colin King58f64252010-11-19 15:40:02 +00001147
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001148 wblock->acpi_device = device;
Colin King58f64252010-11-19 15:40:02 +00001149 wblock->gblock = gblock[i];
1150
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -07001151 retval = wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
1152 if (retval) {
1153 kfree(wblock);
1154 continue;
1155 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001156
Colin King58f64252010-11-19 15:40:02 +00001157 list_add_tail(&wblock->list, &wmi_block_list);
1158
Thomas Renningerfc3155b2010-05-03 15:30:15 +02001159 if (debug_event) {
1160 wblock->handler = wmi_notify_debug;
Dmitry Torokhov2d5ab552010-08-26 00:14:48 -07001161 wmi_method_enable(wblock, 1);
Thomas Renningerfc3155b2010-05-03 15:30:15 +02001162 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001163 }
1164
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001165 /*
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 Perches501f7e52018-03-01 08:08:23 -08001175 dev_err(wmi_bus_dev, "failed to register %pUL\n",
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001176 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 Torokhovc64eefd2010-08-26 00:15:30 -07001183
Axel Lina5167c52010-06-03 11:45:45 +08001184out_free_pointer:
1185 kfree(out.pointer);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001186 return retval;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001187}
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 */
1193static acpi_status
1194acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
Lin Ming439913f2010-01-28 10:53:19 +08001195 u32 bits, u64 *value,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001196 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 Ming439913f2010-01-28 10:53:19 +08001212 (*value) |= ((u64)temp) << i;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001213 } 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 Lutomirski1686f542015-11-25 17:33:25 -08001233static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1234 void *context)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001235{
1236 struct guid_block *block;
1237 struct wmi_block *wblock;
Andy Lutomirski1686f542015-11-25 17:33:25 -08001238 bool found_it = false;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001239
Andy Shevchenkocedb3b22018-02-19 17:02:28 +02001240 list_for_each_entry(wblock, &wmi_block_list, list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +00001241 block = &wblock->gblock;
1242
Andy Lutomirski1686f542015-11-25 17:33:25 -08001243 if (wblock->acpi_device->handle == handle &&
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001244 (block->flags & ACPI_WMI_EVENT) &&
Andy Lutomirski1686f542015-11-25 17:33:25 -08001245 (block->notify_id == event))
1246 {
1247 found_it = true;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001248 break;
1249 }
1250 }
Andy Lutomirski1686f542015-11-25 17:33:25 -08001251
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 Corbachobff431e2008-02-05 02:17:04 +00001299}
1300
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001301static int acpi_wmi_remove(struct platform_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001302{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001303 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1304
1305 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001306 acpi_wmi_notify_handler);
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001307 acpi_remove_address_space_handler(acpi_device->handle,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001308 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001309 wmi_free_devices(acpi_device);
Mario Limonciello7b11e892017-09-26 13:50:05 -05001310 device_destroy(&wmi_bus_class, MKDEV(0, 0));
Carlos Corbachobff431e2008-02-05 02:17:04 +00001311
1312 return 0;
1313}
1314
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001315static int acpi_wmi_probe(struct platform_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001316{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001317 struct acpi_device *acpi_device;
Andy Lutomirski844af952015-11-24 19:49:23 -08001318 struct device *wmi_bus_dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001319 acpi_status status;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001320 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001321
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001322 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 Corbachobff431e2008-02-05 02:17:04 +00001329 ACPI_ADR_SPACE_EC,
1330 &acpi_wmi_ec_space_handler,
1331 NULL, NULL);
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001332 if (ACPI_FAILURE(status)) {
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001333 dev_err(&device->dev, "Error installing EC region handler\n");
Carlos Corbachobff431e2008-02-05 02:17:04 +00001334 return -ENODEV;
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001335 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001336
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001337 status = acpi_install_notify_handler(acpi_device->handle,
1338 ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001339 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 Lutomirski844af952015-11-24 19:49:23 -08001347 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 Lutomirski1686f542015-11-25 17:33:25 -08001351 goto err_remove_notify_handler;
Andy Lutomirski844af952015-11-24 19:49:23 -08001352 }
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001353 dev_set_drvdata(&device->dev, wmi_bus_dev);
Andy Lutomirski844af952015-11-24 19:49:23 -08001354
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001355 error = parse_wdg(wmi_bus_dev, acpi_device);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001356 if (error) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001357 pr_err("Failed to parse WDG method\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001358 goto err_remove_busdev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001359 }
1360
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001361 return 0;
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001362
Andy Lutomirski844af952015-11-24 19:49:23 -08001363err_remove_busdev:
Mario Limonciello7b11e892017-09-26 13:50:05 -05001364 device_destroy(&wmi_bus_class, MKDEV(0, 0));
Andy Lutomirski844af952015-11-24 19:49:23 -08001365
Andy Lutomirski1686f542015-11-25 17:33:25 -08001366err_remove_notify_handler:
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001367 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001368 acpi_wmi_notify_handler);
1369
1370err_remove_ec_handler:
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001371 acpi_remove_address_space_handler(acpi_device->handle,
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001372 ACPI_ADR_SPACE_EC,
1373 &acpi_wmi_ec_space_handler);
1374
1375 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001376}
1377
Andy Lutomirski844af952015-11-24 19:49:23 -08001378int __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}
1386EXPORT_SYMBOL(__wmi_driver_register);
1387
1388void wmi_driver_unregister(struct wmi_driver *driver)
1389{
1390 driver_unregister(&driver->driver);
1391}
1392EXPORT_SYMBOL(wmi_driver_unregister);
1393
Carlos Corbachobff431e2008-02-05 02:17:04 +00001394static int __init acpi_wmi_init(void)
1395{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001396 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001397
1398 if (acpi_disabled)
1399 return -ENODEV;
1400
Andy Lutomirski844af952015-11-24 19:49:23 -08001401 error = class_register(&wmi_bus_class);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001402 if (error)
1403 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001404
Andy Lutomirski844af952015-11-24 19:49:23 -08001405 error = bus_register(&wmi_bus_type);
1406 if (error)
1407 goto err_unreg_class;
1408
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001409 error = platform_driver_register(&acpi_wmi_driver);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001410 if (error) {
1411 pr_err("Error loading mapper\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001412 goto err_unreg_bus;
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001413 }
1414
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001415 return 0;
Andy Lutomirski844af952015-11-24 19:49:23 -08001416
Andy Lutomirski844af952015-11-24 19:49:23 -08001417err_unreg_bus:
1418 bus_unregister(&wmi_bus_type);
1419
Alexey Khoroshilov97277712017-07-22 00:48:06 +03001420err_unreg_class:
1421 class_unregister(&wmi_bus_class);
1422
Andy Lutomirski844af952015-11-24 19:49:23 -08001423 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001424}
1425
1426static void __exit acpi_wmi_exit(void)
1427{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001428 platform_driver_unregister(&acpi_wmi_driver);
Andy Lutomirski844af952015-11-24 19:49:23 -08001429 bus_unregister(&wmi_bus_type);
Mario Limonciello303d1fc2017-09-26 13:50:04 -05001430 class_unregister(&wmi_bus_class);
Carlos Corbachobff431e2008-02-05 02:17:04 +00001431}
1432
Rafael J. Wysocki98b8e4e2018-01-03 12:49:29 +01001433subsys_initcall_sync(acpi_wmi_init);
Carlos Corbachobff431e2008-02-05 02:17:04 +00001434module_exit(acpi_wmi_exit);