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