blob: 8c31ed4f0e1bee69157a19343b7239775ef207e5 [file] [log] [blame]
Carlos Corbachobff431e2008-02-05 02:17:04 +00001/*
2 * ACPI-WMI mapping driver
3 *
4 * Copyright (C) 2007-2008 Carlos Corbacho <carlos@strangeworlds.co.uk>
5 *
6 * GUID parsing code from ldm.c is:
7 * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
8 * Copyright (c) 2001-2007 Anton Altaparmakov
9 * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
10 *
Darren Hart (VMware)2c9c5662017-06-06 16:40:56 -070011 * WMI bus infrastructure by Andrew Lutomirski and Darren Hart:
12 * Copyright (C) 2015 Andrew Lutomirski
13 * Copyright (C) 2017 VMware, Inc. All Rights Reserved.
14 *
Carlos Corbachobff431e2008-02-05 02:17:04 +000015 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or (at
20 * your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
30 *
31 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32 */
33
Dmitry Torokhov8e075142010-08-26 00:15:19 -070034#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
Carlos Corbachobff431e2008-02-05 02:17:04 +000036#include <linux/acpi.h>
Mario Limonciellob60ee4e2017-09-26 13:50:03 -050037#include <linux/device.h>
38#include <linux/init.h>
39#include <linux/kernel.h>
40#include <linux/list.h>
Mario Limonciello44b6b762017-11-01 14:25:35 -050041#include <linux/miscdevice.h>
Paul Gortmaker7c52d552011-05-27 12:33:10 -040042#include <linux/module.h>
Andy Lutomirski9599ed92015-11-27 11:56:02 -080043#include <linux/platform_device.h>
Mario Limonciellob60ee4e2017-09-26 13:50:03 -050044#include <linux/slab.h>
45#include <linux/types.h>
Mario Limonciello44b6b762017-11-01 14:25:35 -050046#include <linux/uaccess.h>
Andy Shevchenko538d7eb2016-05-20 17:01:30 -070047#include <linux/uuid.h>
Mario Limonciellob60ee4e2017-09-26 13:50:03 -050048#include <linux/wmi.h>
Mario Limonciello44b6b762017-11-01 14:25:35 -050049#include <uapi/linux/wmi.h>
Carlos Corbachobff431e2008-02-05 02:17:04 +000050
51ACPI_MODULE_NAME("wmi");
52MODULE_AUTHOR("Carlos Corbacho");
53MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
54MODULE_LICENSE("GPL");
55
Dmitry Torokhov762e1a22010-08-26 00:15:14 -070056static LIST_HEAD(wmi_block_list);
Carlos Corbachobff431e2008-02-05 02:17:04 +000057
58struct guid_block {
59 char guid[16];
60 union {
61 char object_id[2];
62 struct {
63 unsigned char notify_id;
64 unsigned char reserved;
65 };
66 };
67 u8 instance_count;
68 u8 flags;
69};
70
71struct wmi_block {
Andy Lutomirski844af952015-11-24 19:49:23 -080072 struct wmi_device dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +000073 struct list_head list;
74 struct guid_block gblock;
Mario Limonciello44b6b762017-11-01 14:25:35 -050075 struct miscdevice char_dev;
76 struct mutex char_mutex;
Andy Lutomirskib0e86302015-11-24 19:50:01 -080077 struct acpi_device *acpi_device;
Carlos Corbachobff431e2008-02-05 02:17:04 +000078 wmi_notify_handler handler;
79 void *handler_data;
Mario Limonciello44b6b762017-11-01 14:25:35 -050080 u64 req_buf_size;
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -080081
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -070082 bool read_takes_no_args;
Carlos Corbachobff431e2008-02-05 02:17:04 +000083};
84
Carlos Corbachobff431e2008-02-05 02:17:04 +000085
86/*
87 * If the GUID data block is marked as expensive, we must enable and
88 * explicitily disable data collection.
89 */
90#define ACPI_WMI_EXPENSIVE 0x1
91#define ACPI_WMI_METHOD 0x2 /* GUID is a method */
92#define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
93#define ACPI_WMI_EVENT 0x8 /* GUID is an event */
94
Rusty Russell90ab5ee2012-01-13 09:32:20 +103095static bool debug_event;
Thomas Renningerfc3155b2010-05-03 15:30:15 +020096module_param(debug_event, bool, 0444);
97MODULE_PARM_DESC(debug_event,
98 "Log WMI Events [0/1]");
99
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030100static bool debug_dump_wdg;
Thomas Renningera929aae2010-05-03 15:30:17 +0200101module_param(debug_dump_wdg, bool, 0444);
102MODULE_PARM_DESC(debug_dump_wdg,
103 "Dump available WMI interfaces [0/1]");
104
Andy Lutomirski9599ed92015-11-27 11:56:02 -0800105static int acpi_wmi_remove(struct platform_device *device);
106static int acpi_wmi_probe(struct platform_device *device);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000107
108static const struct acpi_device_id wmi_device_ids[] = {
109 {"PNP0C14", 0},
110 {"pnp0c14", 0},
111 {"", 0},
112};
113MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
114
Andy Lutomirski9599ed92015-11-27 11:56:02 -0800115static struct platform_driver acpi_wmi_driver = {
116 .driver = {
117 .name = "acpi-wmi",
118 .acpi_match_table = wmi_device_ids,
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700119 },
Andy Lutomirski9599ed92015-11-27 11:56:02 -0800120 .probe = acpi_wmi_probe,
121 .remove = acpi_wmi_remove,
Carlos Corbachobff431e2008-02-05 02:17:04 +0000122};
123
124/*
125 * GUID parsing functions
126 */
127
Carlos Corbachobff431e2008-02-05 02:17:04 +0000128static bool find_guid(const char *guid_string, struct wmi_block **out)
129{
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700130 uuid_le guid_input;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000131 struct wmi_block *wblock;
132 struct guid_block *block;
133 struct list_head *p;
134
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700135 if (uuid_le_to_bin(guid_string, &guid_input))
136 return false;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000137
Dmitry Torokhov762e1a22010-08-26 00:15:14 -0700138 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000139 wblock = list_entry(p, struct wmi_block, list);
140 block = &wblock->gblock;
141
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700142 if (memcmp(block->guid, &guid_input, 16) == 0) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000143 if (out)
144 *out = wblock;
Joe Perches097c27f2015-03-30 10:43:20 -0700145 return true;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000146 }
147 }
Joe Perches097c27f2015-03-30 10:43:20 -0700148 return false;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000149}
150
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800151static int get_subobj_info(acpi_handle handle, const char *pathname,
152 struct acpi_device_info **info)
153{
154 struct acpi_device_info *dummy_info, **info_ptr;
155 acpi_handle subobj_handle;
156 acpi_status status;
157
158 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
159 if (status == AE_NOT_FOUND)
160 return -ENOENT;
161 else if (ACPI_FAILURE(status))
162 return -EIO;
163
164 info_ptr = info ? info : &dummy_info;
165 status = acpi_get_object_info(subobj_handle, info_ptr);
166 if (ACPI_FAILURE(status))
167 return -EIO;
168
169 if (!info)
170 kfree(dummy_info);
171
172 return 0;
173}
174
Matthew Garretta66bfa72008-10-08 21:40:32 +0100175static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
176{
177 struct guid_block *block = NULL;
178 char method[5];
Matthew Garretta66bfa72008-10-08 21:40:32 +0100179 acpi_status status;
180 acpi_handle handle;
181
182 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800183 handle = wblock->acpi_device->handle;
Matthew Garretta66bfa72008-10-08 21:40:32 +0100184
Matthew Garretta66bfa72008-10-08 21:40:32 +0100185 snprintf(method, 5, "WE%02X", block->notify_id);
Zhang Rui8122ab662013-09-03 08:31:57 +0800186 status = acpi_execute_simple_method(handle, method, enable);
Matthew Garretta66bfa72008-10-08 21:40:32 +0100187
188 if (status != AE_OK && status != AE_NOT_FOUND)
189 return status;
190 else
191 return AE_OK;
192}
193
Carlos Corbachobff431e2008-02-05 02:17:04 +0000194/*
195 * Exported WMI functions
196 */
Mario Limonciello44b6b762017-11-01 14:25:35 -0500197
198/**
199 * set_required_buffer_size - Sets the buffer size needed for performing IOCTL
200 * @wdev: A wmi bus device from a driver
201 * @instance: Instance index
202 *
203 * Allocates memory needed for buffer, stores the buffer size in that memory
204 */
205int set_required_buffer_size(struct wmi_device *wdev, u64 length)
206{
207 struct wmi_block *wblock;
208
209 wblock = container_of(wdev, struct wmi_block, dev);
210 wblock->req_buf_size = length;
211
212 return 0;
213}
214EXPORT_SYMBOL_GPL(set_required_buffer_size);
215
Carlos Corbachobff431e2008-02-05 02:17:04 +0000216/**
217 * wmi_evaluate_method - Evaluate a WMI method
218 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
219 * @instance: Instance index
220 * @method_id: Method ID to call
221 * &in: Buffer containing input for the method call
222 * &out: Empty buffer to return the method results
223 *
224 * Call an ACPI-WMI method
225 */
226acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
227u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
228{
Mario Limonciello722c8562017-11-01 14:25:23 -0500229 struct wmi_block *wblock = NULL;
230
231 if (!find_guid(guid_string, &wblock))
232 return AE_ERROR;
233 return wmidev_evaluate_method(&wblock->dev, instance, method_id,
234 in, out);
235}
236EXPORT_SYMBOL_GPL(wmi_evaluate_method);
237
238/**
239 * wmidev_evaluate_method - Evaluate a WMI method
240 * @wdev: A wmi bus device from a driver
241 * @instance: Instance index
242 * @method_id: Method ID to call
243 * &in: Buffer containing input for the method call
244 * &out: Empty buffer to return the method results
245 *
246 * Call an ACPI-WMI method
247 */
248acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance,
249 u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
250{
Carlos Corbachobff431e2008-02-05 02:17:04 +0000251 struct guid_block *block = NULL;
252 struct wmi_block *wblock = NULL;
253 acpi_handle handle;
254 acpi_status status;
255 struct acpi_object_list input;
256 union acpi_object params[3];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700257 char method[5] = "WM";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000258
Mario Limonciello722c8562017-11-01 14:25:23 -0500259 wblock = container_of(wdev, struct wmi_block, dev);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000260 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800261 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000262
Al Viroe6bafba2008-02-13 04:03:25 +0000263 if (!(block->flags & ACPI_WMI_METHOD))
Carlos Corbachobff431e2008-02-05 02:17:04 +0000264 return AE_BAD_DATA;
265
Pali Rohár6afa1e22017-08-12 09:44:18 +0200266 if (block->instance_count <= instance)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000267 return AE_BAD_PARAMETER;
268
269 input.count = 2;
270 input.pointer = params;
271 params[0].type = ACPI_TYPE_INTEGER;
272 params[0].integer.value = instance;
273 params[1].type = ACPI_TYPE_INTEGER;
274 params[1].integer.value = method_id;
275
276 if (in) {
277 input.count = 3;
278
279 if (block->flags & ACPI_WMI_STRING) {
280 params[2].type = ACPI_TYPE_STRING;
281 } else {
282 params[2].type = ACPI_TYPE_BUFFER;
283 }
284 params[2].buffer.length = in->length;
285 params[2].buffer.pointer = in->pointer;
286 }
287
288 strncat(method, block->object_id, 2);
289
290 status = acpi_evaluate_object(handle, method, &input, out);
291
292 return status;
293}
Mario Limonciello722c8562017-11-01 14:25:23 -0500294EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000295
Andy Lutomirski56a37022015-11-25 18:19:26 -0800296static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
297 struct acpi_buffer *out)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000298{
299 struct guid_block *block = NULL;
Zhang Rui54f14c22013-09-03 08:32:07 +0800300 acpi_handle handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000301 acpi_status status, wc_status = AE_ERROR;
Zhang Rui8122ab662013-09-03 08:31:57 +0800302 struct acpi_object_list input;
303 union acpi_object wq_params[1];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700304 char method[5];
305 char wc_method[5] = "WC";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000306
Andy Lutomirski56a37022015-11-25 18:19:26 -0800307 if (!out)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000308 return AE_BAD_PARAMETER;
309
Carlos Corbachobff431e2008-02-05 02:17:04 +0000310 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800311 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000312
Pali Rohár6afa1e22017-08-12 09:44:18 +0200313 if (block->instance_count <= instance)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000314 return AE_BAD_PARAMETER;
315
316 /* Check GUID is a data block */
317 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800318 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000319
320 input.count = 1;
321 input.pointer = wq_params;
322 wq_params[0].type = ACPI_TYPE_INTEGER;
323 wq_params[0].integer.value = instance;
324
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800325 if (instance == 0 && wblock->read_takes_no_args)
326 input.count = 0;
327
Carlos Corbachobff431e2008-02-05 02:17:04 +0000328 /*
329 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
330 * enable collection.
331 */
332 if (block->flags & ACPI_WMI_EXPENSIVE) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000333 strncat(wc_method, block->object_id, 2);
334
335 /*
336 * Some GUIDs break the specification by declaring themselves
337 * expensive, but have no corresponding WCxx method. So we
338 * should not fail if this happens.
339 */
Zhang Rui54f14c22013-09-03 08:32:07 +0800340 if (acpi_has_method(handle, wc_method))
Zhang Rui8122ab662013-09-03 08:31:57 +0800341 wc_status = acpi_execute_simple_method(handle,
342 wc_method, 1);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000343 }
344
345 strcpy(method, "WQ");
346 strncat(method, block->object_id, 2);
347
Carlos Corbachodab36ad2008-08-02 17:28:45 +0100348 status = acpi_evaluate_object(handle, method, &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000349
350 /*
351 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
352 * the WQxx method failed - we should disable collection anyway.
353 */
Carlos Corbachoa527f2d2008-02-24 13:34:34 +0000354 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
Zhang Rui8122ab662013-09-03 08:31:57 +0800355 status = acpi_execute_simple_method(handle, wc_method, 0);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000356 }
357
358 return status;
359}
Andy Lutomirski56a37022015-11-25 18:19:26 -0800360
361/**
362 * wmi_query_block - Return contents of a WMI block (deprecated)
363 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
364 * @instance: Instance index
365 * &out: Empty buffer to return the contents of the data block to
366 *
367 * Return the contents of an ACPI-WMI data block to a buffer
368 */
369acpi_status wmi_query_block(const char *guid_string, u8 instance,
370 struct acpi_buffer *out)
371{
372 struct wmi_block *wblock;
373
374 if (!guid_string)
375 return AE_BAD_PARAMETER;
376
377 if (!find_guid(guid_string, &wblock))
378 return AE_ERROR;
379
380 return __query_block(wblock, instance, out);
381}
Carlos Corbachobff431e2008-02-05 02:17:04 +0000382EXPORT_SYMBOL_GPL(wmi_query_block);
383
Andy Lutomirski56a37022015-11-25 18:19:26 -0800384union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
385{
386 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
387 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
388
389 if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
390 return NULL;
391
392 return (union acpi_object *)out.pointer;
393}
394EXPORT_SYMBOL_GPL(wmidev_block_query);
395
Carlos Corbachobff431e2008-02-05 02:17:04 +0000396/**
397 * wmi_set_block - Write to a WMI block
398 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
399 * @instance: Instance index
400 * &in: Buffer containing new values for the data block
401 *
402 * Write the contents of the input buffer to an ACPI-WMI data block
403 */
404acpi_status wmi_set_block(const char *guid_string, u8 instance,
Andy Lutomirski56a37022015-11-25 18:19:26 -0800405 const struct acpi_buffer *in)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000406{
407 struct guid_block *block = NULL;
408 struct wmi_block *wblock = NULL;
409 acpi_handle handle;
410 struct acpi_object_list input;
411 union acpi_object params[2];
Costantino Leandrof3d83e22009-08-26 14:29:28 -0700412 char method[5] = "WS";
Carlos Corbachobff431e2008-02-05 02:17:04 +0000413
414 if (!guid_string || !in)
415 return AE_BAD_DATA;
416
417 if (!find_guid(guid_string, &wblock))
Lin Ming08237972008-08-08 11:57:11 +0800418 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000419
420 block = &wblock->gblock;
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800421 handle = wblock->acpi_device->handle;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000422
Pali Rohár6afa1e22017-08-12 09:44:18 +0200423 if (block->instance_count <= instance)
Carlos Corbachobff431e2008-02-05 02:17:04 +0000424 return AE_BAD_PARAMETER;
425
426 /* Check GUID is a data block */
427 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
Lin Ming08237972008-08-08 11:57:11 +0800428 return AE_ERROR;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000429
430 input.count = 2;
431 input.pointer = params;
432 params[0].type = ACPI_TYPE_INTEGER;
433 params[0].integer.value = instance;
434
435 if (block->flags & ACPI_WMI_STRING) {
436 params[1].type = ACPI_TYPE_STRING;
437 } else {
438 params[1].type = ACPI_TYPE_BUFFER;
439 }
440 params[1].buffer.length = in->length;
441 params[1].buffer.pointer = in->pointer;
442
443 strncat(method, block->object_id, 2);
444
445 return acpi_evaluate_object(handle, method, &input, NULL);
446}
447EXPORT_SYMBOL_GPL(wmi_set_block);
448
Dmitry Torokhov37830662010-08-26 00:15:09 -0700449static void wmi_dump_wdg(const struct guid_block *g)
Thomas Renningera929aae2010-05-03 15:30:17 +0200450{
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200451 pr_info("%pUL:\n", g->guid);
Pali Rohárcd3921f2017-06-10 12:57:11 +0200452 if (g->flags & ACPI_WMI_EVENT)
453 pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
454 else
455 pr_info("\tobject_id: %2pE\n", g->object_id);
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700456 pr_info("\tinstance_count: %d\n", g->instance_count);
Joe Perchesdd8e9082011-03-29 15:21:53 -0700457 pr_info("\tflags: %#x", g->flags);
Thomas Renningera929aae2010-05-03 15:30:17 +0200458 if (g->flags) {
Thomas Renningera929aae2010-05-03 15:30:17 +0200459 if (g->flags & ACPI_WMI_EXPENSIVE)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700460 pr_cont(" ACPI_WMI_EXPENSIVE");
Thomas Renningera929aae2010-05-03 15:30:17 +0200461 if (g->flags & ACPI_WMI_METHOD)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700462 pr_cont(" ACPI_WMI_METHOD");
Thomas Renningera929aae2010-05-03 15:30:17 +0200463 if (g->flags & ACPI_WMI_STRING)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700464 pr_cont(" ACPI_WMI_STRING");
Thomas Renningera929aae2010-05-03 15:30:17 +0200465 if (g->flags & ACPI_WMI_EVENT)
Joe Perchesdd8e9082011-03-29 15:21:53 -0700466 pr_cont(" ACPI_WMI_EVENT");
Thomas Renningera929aae2010-05-03 15:30:17 +0200467 }
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700468 pr_cont("\n");
Thomas Renningera929aae2010-05-03 15:30:17 +0200469
470}
471
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200472static void wmi_notify_debug(u32 value, void *context)
473{
474 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
475 union acpi_object *obj;
Axel Lin14926162010-06-28 09:30:45 +0800476 acpi_status status;
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200477
Axel Lin14926162010-06-28 09:30:45 +0800478 status = wmi_get_event_data(value, &response);
479 if (status != AE_OK) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700480 pr_info("bad event status 0x%x\n", status);
Axel Lin14926162010-06-28 09:30:45 +0800481 return;
482 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200483
484 obj = (union acpi_object *)response.pointer;
485
486 if (!obj)
487 return;
488
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700489 pr_info("DEBUG Event ");
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200490 switch(obj->type) {
491 case ACPI_TYPE_BUFFER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700492 pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200493 break;
494 case ACPI_TYPE_STRING:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700495 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200496 break;
497 case ACPI_TYPE_INTEGER:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700498 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200499 break;
500 case ACPI_TYPE_PACKAGE:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700501 pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200502 break;
503 default:
Dmitry Torokhov8e075142010-08-26 00:15:19 -0700504 pr_cont("object type 0x%X\n", obj->type);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200505 }
Axel Lin14926162010-06-28 09:30:45 +0800506 kfree(obj);
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200507}
508
Carlos Corbachobff431e2008-02-05 02:17:04 +0000509/**
510 * wmi_install_notify_handler - Register handler for WMI events
511 * @handler: Function to handle notifications
512 * @data: Data to be returned to handler when event is fired
513 *
514 * Register a handler for events sent to the ACPI-WMI mapper device.
515 */
516acpi_status wmi_install_notify_handler(const char *guid,
517wmi_notify_handler handler, void *data)
518{
519 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000520 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700521 uuid_le guid_input;
Colin King58f64252010-11-19 15:40:02 +0000522 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000523
524 if (!guid || !handler)
525 return AE_BAD_PARAMETER;
526
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700527 if (uuid_le_to_bin(guid, &guid_input))
528 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000529
Colin King58f64252010-11-19 15:40:02 +0000530 list_for_each(p, &wmi_block_list) {
531 acpi_status wmi_status;
532 block = list_entry(p, struct wmi_block, list);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000533
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700534 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000535 if (block->handler &&
536 block->handler != wmi_notify_debug)
537 return AE_ALREADY_ACQUIRED;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000538
Colin King58f64252010-11-19 15:40:02 +0000539 block->handler = handler;
540 block->handler_data = data;
541
542 wmi_status = wmi_method_enable(block, 1);
543 if ((wmi_status != AE_OK) ||
544 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
545 status = wmi_status;
546 }
547 }
Matthew Garretta66bfa72008-10-08 21:40:32 +0100548
549 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000550}
551EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
552
553/**
554 * wmi_uninstall_notify_handler - Unregister handler for WMI events
555 *
556 * Unregister handler for events sent to the ACPI-WMI mapper device.
557 */
558acpi_status wmi_remove_notify_handler(const char *guid)
559{
560 struct wmi_block *block;
Colin King58f64252010-11-19 15:40:02 +0000561 acpi_status status = AE_NOT_EXIST;
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700562 uuid_le guid_input;
Colin King58f64252010-11-19 15:40:02 +0000563 struct list_head *p;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000564
565 if (!guid)
566 return AE_BAD_PARAMETER;
567
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700568 if (uuid_le_to_bin(guid, &guid_input))
569 return AE_BAD_PARAMETER;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000570
Colin King58f64252010-11-19 15:40:02 +0000571 list_for_each(p, &wmi_block_list) {
572 acpi_status wmi_status;
573 block = list_entry(p, struct wmi_block, list);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000574
Andy Shevchenko538d7eb2016-05-20 17:01:30 -0700575 if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
Colin King58f64252010-11-19 15:40:02 +0000576 if (!block->handler ||
577 block->handler == wmi_notify_debug)
578 return AE_NULL_ENTRY;
579
580 if (debug_event) {
581 block->handler = wmi_notify_debug;
582 status = AE_OK;
583 } else {
584 wmi_status = wmi_method_enable(block, 0);
585 block->handler = NULL;
586 block->handler_data = NULL;
587 if ((wmi_status != AE_OK) ||
588 ((wmi_status == AE_OK) &&
589 (status == AE_NOT_EXIST)))
590 status = wmi_status;
591 }
592 }
Thomas Renningerfc3155b2010-05-03 15:30:15 +0200593 }
Colin King58f64252010-11-19 15:40:02 +0000594
Matthew Garretta66bfa72008-10-08 21:40:32 +0100595 return status;
Carlos Corbachobff431e2008-02-05 02:17:04 +0000596}
597EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
598
599/**
600 * wmi_get_event_data - Get WMI data associated with an event
601 *
Anisse Astier3e9b9882009-12-04 10:10:09 +0100602 * @event: Event to find
603 * @out: Buffer to hold event data. out->pointer should be freed with kfree()
Carlos Corbachobff431e2008-02-05 02:17:04 +0000604 *
605 * Returns extra data associated with an event in WMI.
606 */
607acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
608{
609 struct acpi_object_list input;
610 union acpi_object params[1];
611 struct guid_block *gblock;
612 struct wmi_block *wblock;
613 struct list_head *p;
614
615 input.count = 1;
616 input.pointer = params;
617 params[0].type = ACPI_TYPE_INTEGER;
618 params[0].integer.value = event;
619
Dmitry Torokhov762e1a22010-08-26 00:15:14 -0700620 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +0000621 wblock = list_entry(p, struct wmi_block, list);
622 gblock = &wblock->gblock;
623
624 if ((gblock->flags & ACPI_WMI_EVENT) &&
625 (gblock->notify_id == event))
Andy Lutomirskib0e86302015-11-24 19:50:01 -0800626 return acpi_evaluate_object(wblock->acpi_device->handle,
627 "_WED", &input, out);
Carlos Corbachobff431e2008-02-05 02:17:04 +0000628 }
629
630 return AE_NOT_FOUND;
631}
632EXPORT_SYMBOL_GPL(wmi_get_event_data);
633
634/**
635 * wmi_has_guid - Check if a GUID is available
636 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
637 *
638 * Check if a given GUID is defined by _WDG
639 */
640bool wmi_has_guid(const char *guid_string)
641{
642 return find_guid(guid_string, NULL);
643}
644EXPORT_SYMBOL_GPL(wmi_has_guid);
645
Andy Lutomirski844af952015-11-24 19:49:23 -0800646static struct wmi_block *dev_to_wblock(struct device *dev)
647{
648 return container_of(dev, struct wmi_block, dev.dev);
649}
650
651static struct wmi_device *dev_to_wdev(struct device *dev)
652{
653 return container_of(dev, struct wmi_device, dev);
654}
655
Carlos Corbachobff431e2008-02-05 02:17:04 +0000656/*
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500657 * sysfs interface
658 */
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700659static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500660 char *buf)
661{
Andy Lutomirski844af952015-11-24 19:49:23 -0800662 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500663
Rasmus Villemoes85b4e4e2015-09-10 00:08:45 +0200664 return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500665}
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700666static DEVICE_ATTR_RO(modalias);
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700667
Andy Lutomirski844af952015-11-24 19:49:23 -0800668static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
669 char *buf)
670{
671 struct wmi_block *wblock = dev_to_wblock(dev);
672
673 return sprintf(buf, "%pUL\n", wblock->gblock.guid);
674}
675static DEVICE_ATTR_RO(guid);
676
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800677static ssize_t instance_count_show(struct device *dev,
678 struct device_attribute *attr, char *buf)
679{
680 struct wmi_block *wblock = dev_to_wblock(dev);
681
682 return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
683}
684static DEVICE_ATTR_RO(instance_count);
685
686static ssize_t expensive_show(struct device *dev,
687 struct device_attribute *attr, char *buf)
688{
689 struct wmi_block *wblock = dev_to_wblock(dev);
690
691 return sprintf(buf, "%d\n",
692 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
693}
694static DEVICE_ATTR_RO(expensive);
695
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700696static struct attribute *wmi_attrs[] = {
697 &dev_attr_modalias.attr,
Andy Lutomirski844af952015-11-24 19:49:23 -0800698 &dev_attr_guid.attr,
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800699 &dev_attr_instance_count.attr,
700 &dev_attr_expensive.attr,
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700701 NULL,
Dmitry Torokhov614ef432010-08-26 00:15:25 -0700702};
Greg Kroah-Hartmane80b89a2013-07-24 15:05:18 -0700703ATTRIBUTE_GROUPS(wmi);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500704
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800705static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
706 char *buf)
707{
708 struct wmi_block *wblock = dev_to_wblock(dev);
709
710 return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
711}
712static DEVICE_ATTR_RO(notify_id);
713
714static struct attribute *wmi_event_attrs[] = {
715 &dev_attr_notify_id.attr,
716 NULL,
717};
718ATTRIBUTE_GROUPS(wmi_event);
719
720static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
721 char *buf)
722{
723 struct wmi_block *wblock = dev_to_wblock(dev);
724
725 return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
726 wblock->gblock.object_id[1]);
727}
728static DEVICE_ATTR_RO(object_id);
729
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700730static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
731 char *buf)
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800732{
733 struct wmi_device *wdev = dev_to_wdev(dev);
734
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700735 return sprintf(buf, "%d\n", (int)wdev->setable);
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800736}
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700737static DEVICE_ATTR_RO(setable);
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800738
739static struct attribute *wmi_data_attrs[] = {
740 &dev_attr_object_id.attr,
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -0700741 &dev_attr_setable.attr,
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800742 NULL,
743};
744ATTRIBUTE_GROUPS(wmi_data);
745
746static struct attribute *wmi_method_attrs[] = {
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800747 &dev_attr_object_id.attr,
748 NULL,
749};
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -0800750ATTRIBUTE_GROUPS(wmi_method);
Andy Lutomirskid79b1072015-11-25 10:01:37 -0800751
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500752static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
753{
Andy Lutomirski844af952015-11-24 19:49:23 -0800754 struct wmi_block *wblock = dev_to_wblock(dev);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500755
Andy Lutomirski844af952015-11-24 19:49:23 -0800756 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500757 return -ENOMEM;
758
Andy Lutomirski844af952015-11-24 19:49:23 -0800759 if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500760 return -ENOMEM;
761
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500762 return 0;
763}
764
Andy Lutomirski844af952015-11-24 19:49:23 -0800765static void wmi_dev_release(struct device *dev)
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500766{
Andy Lutomirski844af952015-11-24 19:49:23 -0800767 struct wmi_block *wblock = dev_to_wblock(dev);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -0700768
Andy Lutomirski844af952015-11-24 19:49:23 -0800769 kfree(wblock);
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500770}
771
Andy Lutomirski844af952015-11-24 19:49:23 -0800772static int wmi_dev_match(struct device *dev, struct device_driver *driver)
773{
774 struct wmi_driver *wmi_driver =
775 container_of(driver, struct wmi_driver, driver);
776 struct wmi_block *wblock = dev_to_wblock(dev);
777 const struct wmi_device_id *id = wmi_driver->id_table;
778
779 while (id->guid_string) {
780 uuid_le driver_guid;
781
782 if (WARN_ON(uuid_le_to_bin(id->guid_string, &driver_guid)))
783 continue;
784 if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
785 return 1;
786
787 id++;
788 }
789
790 return 0;
791}
Mario Limonciello44b6b762017-11-01 14:25:35 -0500792static int wmi_char_open(struct inode *inode, struct file *filp)
793{
794 const char *driver_name = filp->f_path.dentry->d_iname;
795 struct wmi_block *wblock = NULL;
796 struct wmi_block *next = NULL;
797
798 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
799 if (!wblock->dev.dev.driver)
800 continue;
801 if (strcmp(driver_name, wblock->dev.dev.driver->name) == 0) {
802 filp->private_data = wblock;
803 break;
804 }
805 }
806
807 if (!filp->private_data)
808 return -ENODEV;
809
810 return nonseekable_open(inode, filp);
811}
812
813static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
814 size_t length, loff_t *offset)
815{
816 struct wmi_block *wblock = filp->private_data;
817
818 return simple_read_from_buffer(buffer, length, offset,
819 &wblock->req_buf_size,
820 sizeof(wblock->req_buf_size));
821}
822
823static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
824{
825 struct wmi_ioctl_buffer __user *input =
826 (struct wmi_ioctl_buffer __user *) arg;
827 struct wmi_block *wblock = filp->private_data;
828 struct wmi_ioctl_buffer *buf = NULL;
829 struct wmi_driver *wdriver = NULL;
830 int ret;
831
832 if (_IOC_TYPE(cmd) != WMI_IOC)
833 return -ENOTTY;
834
835 /* make sure we're not calling a higher instance than exists*/
836 if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
837 return -EINVAL;
838
839 mutex_lock(&wblock->char_mutex);
840 buf = wblock->handler_data;
841 if (get_user(buf->length, &input->length)) {
842 dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
843 ret = -EFAULT;
844 goto out_ioctl;
845 }
846 /* if it's too small, abort */
847 if (buf->length < wblock->req_buf_size) {
848 dev_err(&wblock->dev.dev,
849 "Buffer %lld too small, need at least %lld\n",
850 buf->length, wblock->req_buf_size);
851 ret = -EINVAL;
852 goto out_ioctl;
853 }
854 /* if it's too big, warn, driver will only use what is needed */
855 if (buf->length > wblock->req_buf_size)
856 dev_warn(&wblock->dev.dev,
857 "Buffer %lld is bigger than required %lld\n",
858 buf->length, wblock->req_buf_size);
859
860 /* copy the structure from userspace */
861 if (copy_from_user(buf, input, wblock->req_buf_size)) {
862 dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
863 wblock->req_buf_size);
864 ret = -EFAULT;
865 goto out_ioctl;
866 }
867
868 /* let the driver do any filtering and do the call */
869 wdriver = container_of(wblock->dev.dev.driver,
870 struct wmi_driver, driver);
871 if (!try_module_get(wdriver->driver.owner))
872 return -EBUSY;
873 ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
874 module_put(wdriver->driver.owner);
875 if (ret)
876 goto out_ioctl;
877
878 /* return the result (only up to our internal buffer size) */
879 if (copy_to_user(input, buf, wblock->req_buf_size)) {
880 dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
881 wblock->req_buf_size);
882 ret = -EFAULT;
883 }
884
885out_ioctl:
886 mutex_unlock(&wblock->char_mutex);
887 return ret;
888}
889
890static const struct file_operations wmi_fops = {
891 .owner = THIS_MODULE,
892 .read = wmi_char_read,
893 .open = wmi_char_open,
894 .unlocked_ioctl = wmi_ioctl,
895 .compat_ioctl = wmi_ioctl,
896};
Andy Lutomirski844af952015-11-24 19:49:23 -0800897
898static int wmi_dev_probe(struct device *dev)
899{
900 struct wmi_block *wblock = dev_to_wblock(dev);
901 struct wmi_driver *wdriver =
902 container_of(dev->driver, struct wmi_driver, driver);
903 int ret = 0;
Mario Limonciello44b6b762017-11-01 14:25:35 -0500904 int count;
905 char *buf;
Andy Lutomirski844af952015-11-24 19:49:23 -0800906
907 if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
908 dev_warn(dev, "failed to enable device -- probing anyway\n");
909
910 if (wdriver->probe) {
911 ret = wdriver->probe(dev_to_wdev(dev));
Mario Limonciello44b6b762017-11-01 14:25:35 -0500912 if (ret != 0)
913 goto probe_failure;
Andy Lutomirski844af952015-11-24 19:49:23 -0800914 }
915
Mario Limonciello44b6b762017-11-01 14:25:35 -0500916 /* driver wants a character device made */
917 if (wdriver->filter_callback) {
918 /* check that required buffer size declared by driver or MOF */
919 if (!wblock->req_buf_size) {
920 dev_err(&wblock->dev.dev,
921 "Required buffer size not set\n");
922 ret = -EINVAL;
923 goto probe_failure;
924 }
925
926 count = get_order(wblock->req_buf_size);
927 wblock->handler_data = (void *)__get_free_pages(GFP_KERNEL,
928 count);
929 if (!wblock->handler_data) {
930 ret = -ENOMEM;
931 goto probe_failure;
932 }
933
934 buf = kmalloc(strlen(wdriver->driver.name) + 4, GFP_KERNEL);
935 if (!buf) {
936 ret = -ENOMEM;
937 goto probe_string_failure;
938 }
939 sprintf(buf, "wmi/%s", wdriver->driver.name);
940 wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
941 wblock->char_dev.name = buf;
942 wblock->char_dev.fops = &wmi_fops;
943 wblock->char_dev.mode = 0444;
944 ret = misc_register(&wblock->char_dev);
945 if (ret) {
946 dev_warn(dev, "failed to register char dev: %d", ret);
947 ret = -ENOMEM;
948 goto probe_misc_failure;
949 }
950 }
951
952 return 0;
953
954probe_misc_failure:
955 kfree(buf);
956probe_string_failure:
957 kfree(wblock->handler_data);
958probe_failure:
959 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
960 dev_warn(dev, "failed to disable device\n");
Andy Lutomirski844af952015-11-24 19:49:23 -0800961 return ret;
962}
963
964static int wmi_dev_remove(struct device *dev)
965{
966 struct wmi_block *wblock = dev_to_wblock(dev);
967 struct wmi_driver *wdriver =
968 container_of(dev->driver, struct wmi_driver, driver);
969 int ret = 0;
970
Mario Limonciello44b6b762017-11-01 14:25:35 -0500971 if (wdriver->filter_callback) {
972 misc_deregister(&wblock->char_dev);
973 kfree(wblock->char_dev.name);
974 free_pages((unsigned long)wblock->handler_data,
975 get_order(wblock->req_buf_size));
976 }
977
Andy Lutomirski844af952015-11-24 19:49:23 -0800978 if (wdriver->remove)
979 ret = wdriver->remove(dev_to_wdev(dev));
980
981 if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
982 dev_warn(dev, "failed to disable device\n");
983
984 return ret;
985}
986
987static struct class wmi_bus_class = {
988 .name = "wmi_bus",
Matthew Garrett1caab3c2009-11-04 14:17:53 -0500989};
990
Andy Lutomirski844af952015-11-24 19:49:23 -0800991static struct bus_type wmi_bus_type = {
992 .name = "wmi",
993 .dev_groups = wmi_groups,
994 .match = wmi_dev_match,
995 .uevent = wmi_dev_uevent,
996 .probe = wmi_dev_probe,
997 .remove = wmi_dev_remove,
998};
999
Andy Lutomirskid79b1072015-11-25 10:01:37 -08001000static struct device_type wmi_type_event = {
1001 .name = "event",
1002 .groups = wmi_event_groups,
1003 .release = wmi_dev_release,
1004};
1005
1006static struct device_type wmi_type_method = {
1007 .name = "method",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -08001008 .groups = wmi_method_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -08001009 .release = wmi_dev_release,
1010};
1011
1012static struct device_type wmi_type_data = {
1013 .name = "data",
Andy Lutomirskid4fc91a2015-11-25 14:03:43 -08001014 .groups = wmi_data_groups,
Andy Lutomirskid79b1072015-11-25 10:01:37 -08001015 .release = wmi_dev_release,
1016};
1017
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -07001018static int wmi_create_device(struct device *wmi_bus_dev,
Andy Lutomirski844af952015-11-24 19:49:23 -08001019 const struct guid_block *gblock,
Andy Lutomirski7f5809b2015-11-19 14:44:46 -08001020 struct wmi_block *wblock,
1021 struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001022{
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -07001023 struct acpi_device_info *info;
1024 char method[5];
1025 int result;
1026
1027 if (gblock->flags & ACPI_WMI_EVENT) {
1028 wblock->dev.dev.type = &wmi_type_event;
1029 goto out_init;
1030 }
1031
1032 if (gblock->flags & ACPI_WMI_METHOD) {
1033 wblock->dev.dev.type = &wmi_type_method;
Mario Limonciello44b6b762017-11-01 14:25:35 -05001034 mutex_init(&wblock->char_mutex);
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -07001035 goto out_init;
1036 }
1037
1038 /*
1039 * Data Block Query Control Method (WQxx by convention) is
1040 * required per the WMI documentation. If it is not present,
1041 * we ignore this data block.
1042 */
1043 strcpy(method, "WQ");
1044 strncat(method, wblock->gblock.object_id, 2);
1045 result = get_subobj_info(device->handle, method, &info);
1046
1047 if (result) {
1048 dev_warn(wmi_bus_dev,
1049 "%s data block query control method not found",
1050 method);
1051 return result;
1052 }
1053
1054 wblock->dev.dev.type = &wmi_type_data;
1055
1056 /*
1057 * The Microsoft documentation specifically states:
1058 *
1059 * Data blocks registered with only a single instance
1060 * can ignore the parameter.
1061 *
1062 * ACPICA will get mad at us if we call the method with the wrong number
1063 * of arguments, so check what our method expects. (On some Dell
1064 * laptops, WQxx may not be a method at all.)
1065 */
1066 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1067 wblock->read_takes_no_args = true;
1068
1069 kfree(info);
1070
1071 strcpy(method, "WS");
1072 strncat(method, wblock->gblock.object_id, 2);
1073 result = get_subobj_info(device->handle, method, NULL);
1074
1075 if (result == 0)
1076 wblock->dev.setable = true;
1077
1078 out_init:
Andy Lutomirski844af952015-11-24 19:49:23 -08001079 wblock->dev.dev.bus = &wmi_bus_type;
1080 wblock->dev.dev.parent = wmi_bus_dev;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001081
Andy Lutomirski844af952015-11-24 19:49:23 -08001082 dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001083
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001084 device_initialize(&wblock->dev.dev);
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -07001085
1086 return 0;
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001087}
1088
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001089static void wmi_free_devices(struct acpi_device *device)
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001090{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001091 struct wmi_block *wblock, *next;
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001092
1093 /* Delete devices for all the GUIDs */
Dmitry Torokhov023b9562011-09-07 15:00:02 -07001094 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001095 if (wblock->acpi_device == device) {
1096 list_del(&wblock->list);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001097 device_unregister(&wblock->dev.dev);
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001098 }
Dmitry Torokhov023b9562011-09-07 15:00:02 -07001099 }
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001100}
1101
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001102static bool guid_already_parsed(struct acpi_device *device,
1103 const u8 *guid)
Carlos Corbachod1f9e492009-12-26 19:14:59 +00001104{
Carlos Corbachod1f9e492009-12-26 19:14:59 +00001105 struct wmi_block *wblock;
Carlos Corbachod1f9e492009-12-26 19:14:59 +00001106
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001107 list_for_each_entry(wblock, &wmi_block_list, list) {
1108 if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
1109 /*
1110 * Because we historically didn't track the relationship
1111 * between GUIDs and ACPI nodes, we don't know whether
1112 * we need to suppress GUIDs that are unique on a
1113 * given node but duplicated across nodes.
1114 */
1115 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1116 guid, dev_name(&wblock->acpi_device->dev));
Carlos Corbachod1f9e492009-12-26 19:14:59 +00001117 return true;
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001118 }
1119 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001120
Carlos Corbachod1f9e492009-12-26 19:14:59 +00001121 return false;
1122}
1123
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001124/*
Carlos Corbachobff431e2008-02-05 02:17:04 +00001125 * Parse the _WDG method for the GUID data blocks
1126 */
Andy Lutomirski844af952015-11-24 19:49:23 -08001127static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001128{
1129 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
Dmitry Torokhov37830662010-08-26 00:15:09 -07001130 const struct guid_block *gblock;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001131 struct wmi_block *wblock, *next;
1132 union acpi_object *obj;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001133 acpi_status status;
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001134 int retval = 0;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001135 u32 i, total;
1136
Andy Lutomirski7f5809b2015-11-19 14:44:46 -08001137 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
Carlos Corbachobff431e2008-02-05 02:17:04 +00001138 if (ACPI_FAILURE(status))
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001139 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001140
1141 obj = (union acpi_object *) out.pointer;
Dmitry Torokhov3d2c63e2010-08-26 00:15:03 -07001142 if (!obj)
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001143 return -ENXIO;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001144
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -07001145 if (obj->type != ACPI_TYPE_BUFFER) {
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001146 retval = -ENXIO;
Dmitry Torokhov64ed0ab2010-08-26 00:14:58 -07001147 goto out_free_pointer;
1148 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001149
Dmitry Torokhov37830662010-08-26 00:15:09 -07001150 gblock = (const struct guid_block *)obj->buffer.pointer;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001151 total = obj->buffer.length / sizeof(struct guid_block);
1152
Carlos Corbachobff431e2008-02-05 02:17:04 +00001153 for (i = 0; i < total; i++) {
Thomas Renningera929aae2010-05-03 15:30:17 +02001154 if (debug_dump_wdg)
1155 wmi_dump_wdg(&gblock[i]);
1156
Andy Lutomirskia1c31bcd2015-11-25 08:24:42 -08001157 /*
1158 * Some WMI devices, like those for nVidia hooks, have a
1159 * duplicate GUID. It's not clear what we should do in this
1160 * case yet, so for now, we'll just ignore the duplicate
1161 * for device creation.
1162 */
1163 if (guid_already_parsed(device, gblock[i].guid))
1164 continue;
1165
Colin King58f64252010-11-19 15:40:02 +00001166 wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001167 if (!wblock) {
1168 retval = -ENOMEM;
1169 break;
1170 }
Colin King58f64252010-11-19 15:40:02 +00001171
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001172 wblock->acpi_device = device;
Colin King58f64252010-11-19 15:40:02 +00001173 wblock->gblock = gblock[i];
1174
Darren Hart (VMware)fd70da62017-05-19 19:28:36 -07001175 retval = wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
1176 if (retval) {
1177 kfree(wblock);
1178 continue;
1179 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001180
Colin King58f64252010-11-19 15:40:02 +00001181 list_add_tail(&wblock->list, &wmi_block_list);
1182
Thomas Renningerfc3155b2010-05-03 15:30:15 +02001183 if (debug_event) {
1184 wblock->handler = wmi_notify_debug;
Dmitry Torokhov2d5ab552010-08-26 00:14:48 -07001185 wmi_method_enable(wblock, 1);
Thomas Renningerfc3155b2010-05-03 15:30:15 +02001186 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001187 }
1188
Darren Hart (VMware)6ee50aa2017-06-06 10:13:49 -07001189 /*
1190 * Now that all of the devices are created, add them to the
1191 * device tree and probe subdrivers.
1192 */
1193 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1194 if (wblock->acpi_device != device)
1195 continue;
1196
1197 retval = device_add(&wblock->dev.dev);
1198 if (retval) {
1199 dev_err(wmi_bus_dev, "failed to register %pULL\n",
1200 wblock->gblock.guid);
1201 if (debug_event)
1202 wmi_method_enable(wblock, 0);
1203 list_del(&wblock->list);
1204 put_device(&wblock->dev.dev);
1205 }
1206 }
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001207
Axel Lina5167c52010-06-03 11:45:45 +08001208out_free_pointer:
1209 kfree(out.pointer);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001210 return retval;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001211}
1212
1213/*
1214 * WMI can have EmbeddedControl access regions. In which case, we just want to
1215 * hand these off to the EC driver.
1216 */
1217static acpi_status
1218acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
Lin Ming439913f2010-01-28 10:53:19 +08001219 u32 bits, u64 *value,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001220 void *handler_context, void *region_context)
1221{
1222 int result = 0, i = 0;
1223 u8 temp = 0;
1224
1225 if ((address > 0xFF) || !value)
1226 return AE_BAD_PARAMETER;
1227
1228 if (function != ACPI_READ && function != ACPI_WRITE)
1229 return AE_BAD_PARAMETER;
1230
1231 if (bits != 8)
1232 return AE_BAD_PARAMETER;
1233
1234 if (function == ACPI_READ) {
1235 result = ec_read(address, &temp);
Lin Ming439913f2010-01-28 10:53:19 +08001236 (*value) |= ((u64)temp) << i;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001237 } else {
1238 temp = 0xff & ((*value) >> i);
1239 result = ec_write(address, temp);
1240 }
1241
1242 switch (result) {
1243 case -EINVAL:
1244 return AE_BAD_PARAMETER;
1245 break;
1246 case -ENODEV:
1247 return AE_NOT_FOUND;
1248 break;
1249 case -ETIME:
1250 return AE_TIME;
1251 break;
1252 default:
1253 return AE_OK;
1254 }
1255}
1256
Andy Lutomirski1686f542015-11-25 17:33:25 -08001257static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1258 void *context)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001259{
1260 struct guid_block *block;
1261 struct wmi_block *wblock;
1262 struct list_head *p;
Andy Lutomirski1686f542015-11-25 17:33:25 -08001263 bool found_it = false;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001264
Dmitry Torokhov762e1a22010-08-26 00:15:14 -07001265 list_for_each(p, &wmi_block_list) {
Carlos Corbachobff431e2008-02-05 02:17:04 +00001266 wblock = list_entry(p, struct wmi_block, list);
1267 block = &wblock->gblock;
1268
Andy Lutomirski1686f542015-11-25 17:33:25 -08001269 if (wblock->acpi_device->handle == handle &&
Andy Lutomirskib0e86302015-11-24 19:50:01 -08001270 (block->flags & ACPI_WMI_EVENT) &&
Andy Lutomirski1686f542015-11-25 17:33:25 -08001271 (block->notify_id == event))
1272 {
1273 found_it = true;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001274 break;
1275 }
1276 }
Andy Lutomirski1686f542015-11-25 17:33:25 -08001277
1278 if (!found_it)
1279 return;
1280
1281 /* If a driver is bound, then notify the driver. */
1282 if (wblock->dev.dev.driver) {
1283 struct wmi_driver *driver;
1284 struct acpi_object_list input;
1285 union acpi_object params[1];
1286 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1287 acpi_status status;
1288
1289 driver = container_of(wblock->dev.dev.driver,
1290 struct wmi_driver, driver);
1291
1292 input.count = 1;
1293 input.pointer = params;
1294 params[0].type = ACPI_TYPE_INTEGER;
1295 params[0].integer.value = event;
1296
1297 status = acpi_evaluate_object(wblock->acpi_device->handle,
1298 "_WED", &input, &evdata);
1299 if (ACPI_FAILURE(status)) {
1300 dev_warn(&wblock->dev.dev,
1301 "failed to get event data\n");
1302 return;
1303 }
1304
1305 if (driver->notify)
1306 driver->notify(&wblock->dev,
1307 (union acpi_object *)evdata.pointer);
1308
1309 kfree(evdata.pointer);
1310 } else if (wblock->handler) {
1311 /* Legacy handler */
1312 wblock->handler(event, wblock->handler_data);
1313 }
1314
1315 if (debug_event) {
1316 pr_info("DEBUG Event GUID: %pUL\n",
1317 wblock->gblock.guid);
1318 }
1319
1320 acpi_bus_generate_netlink_event(
1321 wblock->acpi_device->pnp.device_class,
1322 dev_name(&wblock->dev.dev),
1323 event, 0);
1324
Carlos Corbachobff431e2008-02-05 02:17:04 +00001325}
1326
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001327static int acpi_wmi_remove(struct platform_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001328{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001329 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1330
1331 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001332 acpi_wmi_notify_handler);
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001333 acpi_remove_address_space_handler(acpi_device->handle,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001334 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001335 wmi_free_devices(acpi_device);
Mario Limonciello7b11e892017-09-26 13:50:05 -05001336 device_destroy(&wmi_bus_class, MKDEV(0, 0));
Carlos Corbachobff431e2008-02-05 02:17:04 +00001337
1338 return 0;
1339}
1340
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001341static int acpi_wmi_probe(struct platform_device *device)
Carlos Corbachobff431e2008-02-05 02:17:04 +00001342{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001343 struct acpi_device *acpi_device;
Andy Lutomirski844af952015-11-24 19:49:23 -08001344 struct device *wmi_bus_dev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001345 acpi_status status;
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001346 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001347
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001348 acpi_device = ACPI_COMPANION(&device->dev);
1349 if (!acpi_device) {
1350 dev_err(&device->dev, "ACPI companion is missing\n");
1351 return -ENODEV;
1352 }
1353
1354 status = acpi_install_address_space_handler(acpi_device->handle,
Carlos Corbachobff431e2008-02-05 02:17:04 +00001355 ACPI_ADR_SPACE_EC,
1356 &acpi_wmi_ec_space_handler,
1357 NULL, NULL);
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001358 if (ACPI_FAILURE(status)) {
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001359 dev_err(&device->dev, "Error installing EC region handler\n");
Carlos Corbachobff431e2008-02-05 02:17:04 +00001360 return -ENODEV;
Dmitry Torokhov5212cd62010-08-26 00:14:42 -07001361 }
Carlos Corbachobff431e2008-02-05 02:17:04 +00001362
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001363 status = acpi_install_notify_handler(acpi_device->handle,
1364 ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001365 acpi_wmi_notify_handler,
1366 NULL);
1367 if (ACPI_FAILURE(status)) {
1368 dev_err(&device->dev, "Error installing notify handler\n");
1369 error = -ENODEV;
1370 goto err_remove_ec_handler;
1371 }
1372
Andy Lutomirski844af952015-11-24 19:49:23 -08001373 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1374 NULL, "wmi_bus-%s", dev_name(&device->dev));
1375 if (IS_ERR(wmi_bus_dev)) {
1376 error = PTR_ERR(wmi_bus_dev);
Andy Lutomirski1686f542015-11-25 17:33:25 -08001377 goto err_remove_notify_handler;
Andy Lutomirski844af952015-11-24 19:49:23 -08001378 }
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001379 dev_set_drvdata(&device->dev, wmi_bus_dev);
Andy Lutomirski844af952015-11-24 19:49:23 -08001380
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001381 error = parse_wdg(wmi_bus_dev, acpi_device);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001382 if (error) {
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001383 pr_err("Failed to parse WDG method\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001384 goto err_remove_busdev;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001385 }
1386
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001387 return 0;
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001388
Andy Lutomirski844af952015-11-24 19:49:23 -08001389err_remove_busdev:
Mario Limonciello7b11e892017-09-26 13:50:05 -05001390 device_destroy(&wmi_bus_class, MKDEV(0, 0));
Andy Lutomirski844af952015-11-24 19:49:23 -08001391
Andy Lutomirski1686f542015-11-25 17:33:25 -08001392err_remove_notify_handler:
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001393 acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
Andy Lutomirski1686f542015-11-25 17:33:25 -08001394 acpi_wmi_notify_handler);
1395
1396err_remove_ec_handler:
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001397 acpi_remove_address_space_handler(acpi_device->handle,
Andy Lutomirski46492ee2015-11-24 19:54:46 -08001398 ACPI_ADR_SPACE_EC,
1399 &acpi_wmi_ec_space_handler);
1400
1401 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001402}
1403
Andy Lutomirski844af952015-11-24 19:49:23 -08001404int __must_check __wmi_driver_register(struct wmi_driver *driver,
1405 struct module *owner)
1406{
1407 driver->driver.owner = owner;
1408 driver->driver.bus = &wmi_bus_type;
1409
1410 return driver_register(&driver->driver);
1411}
1412EXPORT_SYMBOL(__wmi_driver_register);
1413
1414void wmi_driver_unregister(struct wmi_driver *driver)
1415{
1416 driver_unregister(&driver->driver);
1417}
1418EXPORT_SYMBOL(wmi_driver_unregister);
1419
Carlos Corbachobff431e2008-02-05 02:17:04 +00001420static int __init acpi_wmi_init(void)
1421{
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001422 int error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001423
1424 if (acpi_disabled)
1425 return -ENODEV;
1426
Andy Lutomirski844af952015-11-24 19:49:23 -08001427 error = class_register(&wmi_bus_class);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001428 if (error)
1429 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001430
Andy Lutomirski844af952015-11-24 19:49:23 -08001431 error = bus_register(&wmi_bus_type);
1432 if (error)
1433 goto err_unreg_class;
1434
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001435 error = platform_driver_register(&acpi_wmi_driver);
Dmitry Torokhovc64eefd2010-08-26 00:15:30 -07001436 if (error) {
1437 pr_err("Error loading mapper\n");
Andy Lutomirski844af952015-11-24 19:49:23 -08001438 goto err_unreg_bus;
Matthew Garrett1caab3c2009-11-04 14:17:53 -05001439 }
1440
Dmitry Torokhov8e075142010-08-26 00:15:19 -07001441 return 0;
Andy Lutomirski844af952015-11-24 19:49:23 -08001442
Andy Lutomirski844af952015-11-24 19:49:23 -08001443err_unreg_bus:
1444 bus_unregister(&wmi_bus_type);
1445
Alexey Khoroshilov97277712017-07-22 00:48:06 +03001446err_unreg_class:
1447 class_unregister(&wmi_bus_class);
1448
Andy Lutomirski844af952015-11-24 19:49:23 -08001449 return error;
Carlos Corbachobff431e2008-02-05 02:17:04 +00001450}
1451
1452static void __exit acpi_wmi_exit(void)
1453{
Andy Lutomirski9599ed92015-11-27 11:56:02 -08001454 platform_driver_unregister(&acpi_wmi_driver);
Andy Lutomirski844af952015-11-24 19:49:23 -08001455 bus_unregister(&wmi_bus_type);
Mario Limonciello303d1fc2017-09-26 13:50:04 -05001456 class_unregister(&wmi_bus_class);
Carlos Corbachobff431e2008-02-05 02:17:04 +00001457}
1458
1459subsys_initcall(acpi_wmi_init);
1460module_exit(acpi_wmi_exit);