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