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