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