xref: /linux/drivers/platform/wmi/core.c (revision 7e2d964f417ec13763eecfecc5d2813f63cb8da0)
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/idr.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/limits.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/rwsem.h>
30 #include <linux/slab.h>
31 #include <linux/sysfs.h>
32 #include <linux/types.h>
33 #include <linux/uuid.h>
34 #include <linux/wmi.h>
35 #include <linux/fs.h>
36 
37 #include "internal.h"
38 
39 MODULE_AUTHOR("Carlos Corbacho");
40 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
41 MODULE_LICENSE("GPL");
42 
43 struct guid_block {
44 	guid_t guid;
45 	union {
46 		char object_id[2];
47 		struct {
48 			unsigned char notify_id;
49 			unsigned char reserved;
50 		};
51 	};
52 	u8 instance_count;
53 	u8 flags;
54 } __packed;
55 static_assert(sizeof(typeof_member(struct guid_block, guid)) == 16);
56 static_assert(sizeof(struct guid_block) == 20);
57 static_assert(__alignof__(struct guid_block) == 1);
58 
59 enum {	/* wmi_block flags */
60 	WMI_READ_TAKES_NO_ARGS,
61 	WMI_GUID_DUPLICATED,
62 	WMI_NO_EVENT_DATA,
63 };
64 
65 struct wmi_block {
66 	struct wmi_device dev;
67 	struct guid_block gblock;
68 	struct acpi_device *acpi_device;
69 	struct rw_semaphore notify_lock;	/* Protects notify callback add/remove */
70 	wmi_notify_handler handler;
71 	void *handler_data;
72 	bool driver_ready;
73 	unsigned long flags;
74 };
75 
76 struct wmi_guid_count_context {
77 	const guid_t *guid;
78 	int count;
79 };
80 
81 static DEFINE_IDA(wmi_ida);
82 
83 /*
84  * If the GUID data block is marked as expensive, we must enable and
85  * explicitily disable data collection.
86  */
87 #define ACPI_WMI_EXPENSIVE   BIT(0)
88 #define ACPI_WMI_METHOD      BIT(1)	/* GUID is a method */
89 #define ACPI_WMI_STRING      BIT(2)	/* GUID takes & returns a string */
90 #define ACPI_WMI_EVENT       BIT(3)	/* GUID is an event */
91 
92 static const struct acpi_device_id wmi_device_ids[] = {
93 	{"PNP0C14", 0},
94 	{"pnp0c14", 0},
95 	{ }
96 };
97 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
98 
99 #define dev_to_wblock(__dev)	container_of_const(__dev, struct wmi_block, dev.dev)
100 
101 /*
102  * GUID parsing functions
103  */
104 
105 static bool guid_parse_and_compare(const char *string, const guid_t *guid)
106 {
107 	guid_t guid_input;
108 
109 	if (guid_parse(string, &guid_input))
110 		return false;
111 
112 	return guid_equal(&guid_input, guid);
113 }
114 
115 static const void *find_guid_context(struct wmi_block *wblock,
116 				     struct wmi_driver *wdriver)
117 {
118 	const struct wmi_device_id *id;
119 
120 	id = wdriver->id_table;
121 	if (!id)
122 		return NULL;
123 
124 	while (*id->guid_string) {
125 		if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid))
126 			return id->context;
127 		id++;
128 	}
129 	return NULL;
130 }
131 
132 #define WMI_ACPI_METHOD_NAME_SIZE 5
133 
134 static inline void get_acpi_method_name(const struct wmi_block *wblock,
135 					const char method,
136 					char buffer[static WMI_ACPI_METHOD_NAME_SIZE])
137 {
138 	static_assert(ARRAY_SIZE(wblock->gblock.object_id) == 2);
139 	static_assert(WMI_ACPI_METHOD_NAME_SIZE >= 5);
140 
141 	buffer[0] = 'W';
142 	buffer[1] = method;
143 	buffer[2] = wblock->gblock.object_id[0];
144 	buffer[3] = wblock->gblock.object_id[1];
145 	buffer[4] = '\0';
146 }
147 
148 static int wmidev_match_guid(struct device *dev, const void *data)
149 {
150 	struct wmi_block *wblock = dev_to_wblock(dev);
151 	const guid_t *guid = data;
152 
153 	/* Legacy GUID-based functions are restricted to only see
154 	 * a single WMI device for each GUID.
155 	 */
156 	if (test_bit(WMI_GUID_DUPLICATED, &wblock->flags))
157 		return 0;
158 
159 	if (guid_equal(guid, &wblock->gblock.guid))
160 		return 1;
161 
162 	return 0;
163 }
164 
165 static const struct bus_type wmi_bus_type;
166 
167 static const struct device_type wmi_type_event;
168 
169 static const struct device_type wmi_type_method;
170 
171 static int wmi_device_enable(struct wmi_device *wdev, bool enable)
172 {
173 	struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
174 	char method[WMI_ACPI_METHOD_NAME_SIZE];
175 	acpi_handle handle;
176 	acpi_status status;
177 
178 	if (wblock->dev.dev.type == &wmi_type_method)
179 		return 0;
180 
181 	if (wblock->dev.dev.type == &wmi_type_event) {
182 		/*
183 		 * Windows always enables/disables WMI events, even when they are
184 		 * not marked as being expensive. We follow this behavior for
185 		 * compatibility reasons.
186 		 */
187 		snprintf(method, sizeof(method), "WE%02X", wblock->gblock.notify_id);
188 	} else {
189 		if (!(wblock->gblock.flags & ACPI_WMI_EXPENSIVE))
190 			return 0;
191 
192 		get_acpi_method_name(wblock, 'C', method);
193 	}
194 
195 	/*
196 	 * Not all WMI devices marked as expensive actually implement the
197 	 * necessary ACPI method. Ignore this missing ACPI method to match
198 	 * the behaviour of the Windows driver.
199 	 */
200 	status = acpi_get_handle(wblock->acpi_device->handle, method, &handle);
201 	if (ACPI_FAILURE(status))
202 		return 0;
203 
204 	status = acpi_execute_simple_method(handle, NULL, enable);
205 	if (ACPI_FAILURE(status))
206 		return -EIO;
207 
208 	return 0;
209 }
210 
211 static struct wmi_device *wmi_find_device_by_guid(const char *guid_string)
212 {
213 	struct device *dev;
214 	guid_t guid;
215 	int ret;
216 
217 	ret = guid_parse(guid_string, &guid);
218 	if (ret < 0)
219 		return ERR_PTR(ret);
220 
221 	dev = bus_find_device(&wmi_bus_type, NULL, &guid, wmidev_match_guid);
222 	if (!dev)
223 		return ERR_PTR(-ENODEV);
224 
225 	return to_wmi_device(dev);
226 }
227 
228 static void wmi_device_put(struct wmi_device *wdev)
229 {
230 	put_device(&wdev->dev);
231 }
232 
233 /*
234  * Exported WMI functions
235  */
236 
237 /**
238  * wmi_instance_count - Get number of WMI object instances
239  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
240  *
241  * Get the number of WMI object instances.
242  *
243  * Returns: Number of WMI object instances or negative error code.
244  */
245 int wmi_instance_count(const char *guid_string)
246 {
247 	struct wmi_device *wdev;
248 	int ret;
249 
250 	wdev = wmi_find_device_by_guid(guid_string);
251 	if (IS_ERR(wdev))
252 		return PTR_ERR(wdev);
253 
254 	ret = wmidev_instance_count(wdev);
255 	wmi_device_put(wdev);
256 
257 	return ret;
258 }
259 EXPORT_SYMBOL_GPL(wmi_instance_count);
260 
261 /**
262  * wmidev_instance_count - Get number of WMI object instances
263  * @wdev: A wmi bus device from a driver
264  *
265  * Get the number of WMI object instances.
266  *
267  * Returns: Number of WMI object instances.
268  */
269 u8 wmidev_instance_count(struct wmi_device *wdev)
270 {
271 	struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
272 
273 	return wblock->gblock.instance_count;
274 }
275 EXPORT_SYMBOL_GPL(wmidev_instance_count);
276 
277 /**
278  * wmi_evaluate_method - Evaluate a WMI method (deprecated)
279  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
280  * @instance: Instance index
281  * @method_id: Method ID to call
282  * @in: Mandatory buffer containing input for the method call
283  * @out: Empty buffer to return the method results
284  *
285  * Call an ACPI-WMI method, the caller must free @out.
286  *
287  * Return: acpi_status signaling success or error.
288  */
289 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance, u32 method_id,
290 				const struct acpi_buffer *in, struct acpi_buffer *out)
291 {
292 	struct wmi_device *wdev;
293 	acpi_status status;
294 
295 	wdev = wmi_find_device_by_guid(guid_string);
296 	if (IS_ERR(wdev))
297 		return AE_ERROR;
298 
299 	status = wmidev_evaluate_method(wdev, instance, method_id, in, out);
300 
301 	wmi_device_put(wdev);
302 
303 	return status;
304 }
305 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
306 
307 /**
308  * wmidev_evaluate_method - Evaluate a WMI method (deprecated)
309  * @wdev: A wmi bus device from a driver
310  * @instance: Instance index
311  * @method_id: Method ID to call
312  * @in: Mandatory 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 wmidev_evaluate_method(struct wmi_device *wdev, u8 instance, u32 method_id,
320 				   const struct acpi_buffer *in, struct acpi_buffer *out)
321 {
322 	struct guid_block *block;
323 	struct wmi_block *wblock;
324 	acpi_handle handle;
325 	struct acpi_object_list input;
326 	union acpi_object params[3];
327 	char method[WMI_ACPI_METHOD_NAME_SIZE];
328 
329 	wblock = container_of(wdev, struct wmi_block, dev);
330 	block = &wblock->gblock;
331 	handle = wblock->acpi_device->handle;
332 
333 	if (!in)
334 		return AE_BAD_DATA;
335 
336 	if (!(block->flags & ACPI_WMI_METHOD))
337 		return AE_BAD_DATA;
338 
339 	if (block->instance_count <= instance)
340 		return AE_BAD_PARAMETER;
341 
342 	input.count = 3;
343 	input.pointer = params;
344 
345 	params[0].type = ACPI_TYPE_INTEGER;
346 	params[0].integer.value = instance;
347 	params[1].type = ACPI_TYPE_INTEGER;
348 	params[1].integer.value = method_id;
349 
350 	if (wblock->gblock.flags & ACPI_WMI_STRING) {
351 		params[2].type = ACPI_TYPE_STRING;
352 		params[2].string.length = in->length;
353 		params[2].string.pointer = in->pointer;
354 	} else {
355 		params[2].type = ACPI_TYPE_BUFFER;
356 		params[2].buffer.length = in->length;
357 		params[2].buffer.pointer = in->pointer;
358 	}
359 
360 	get_acpi_method_name(wblock, 'M', method);
361 
362 	return acpi_evaluate_object(handle, method, &input, out);
363 }
364 EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
365 
366 /**
367  * wmidev_invoke_method - Invoke a WMI method
368  * @wdev: A wmi bus device from a driver
369  * @instance: Instance index
370  * @method_id: Method ID to call
371  * @in: Mandatory WMI buffer containing input for the method call
372  * @out: Optional WMI buffer to return the method results
373  *
374  * Invoke a WMI method, the caller must free the resulting data inside @out.
375  * Said data is guaranteed to be aligned on a 8-byte boundary.
376  *
377  * Return: 0 on success or negative error code on failure.
378  */
379 int wmidev_invoke_method(struct wmi_device *wdev, u8 instance, u32 method_id,
380 			 const struct wmi_buffer *in, struct wmi_buffer *out)
381 {
382 	struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
383 	struct acpi_buffer aout = { ACPI_ALLOCATE_BUFFER, NULL };
384 	struct acpi_buffer ain;
385 	union acpi_object *obj;
386 	acpi_status status;
387 	int ret;
388 
389 	if (wblock->gblock.flags & ACPI_WMI_STRING) {
390 		ret = wmi_marshal_string(in, &ain);
391 		if (ret < 0)
392 			return ret;
393 	} else {
394 		if (in->length > U32_MAX)
395 			return -E2BIG;
396 
397 		ain.length = in->length;
398 		ain.pointer = in->data;
399 	}
400 
401 	if (out)
402 		status = wmidev_evaluate_method(wdev, instance, method_id, &ain, &aout);
403 	else
404 		status = wmidev_evaluate_method(wdev, instance, method_id, &ain, NULL);
405 
406 	if (wblock->gblock.flags & ACPI_WMI_STRING)
407 		kfree(ain.pointer);
408 
409 	if (ACPI_FAILURE(status))
410 		return -EIO;
411 
412 	if (!out)
413 		return 0;
414 
415 	obj = aout.pointer;
416 	if (!obj) {
417 		out->length = 0;
418 		out->data = ZERO_SIZE_PTR;
419 
420 		return 0;
421 	}
422 
423 	ret = wmi_unmarshal_acpi_object(obj, out);
424 	kfree(obj);
425 
426 	return ret;
427 }
428 EXPORT_SYMBOL_GPL(wmidev_invoke_method);
429 
430 /**
431  * wmidev_invoke_procedure - Invoke a WMI method that does not return values
432  * @wdev: A wmi bus device from a driver
433  * @instance: Instance index
434  * @method_id: Method ID to call
435  * @in: Mandatory WMI buffer containing input for the method call
436  *
437  * Invoke a WMI method that does not return any values. Use wmidev_invoke_method()
438  * for WMI methods that do return values.
439  *
440  * Return: 0 on success or negative error code on failure.
441  */
442 int wmidev_invoke_procedure(struct wmi_device *wdev, u8 instance, u32 method_id,
443 			    const struct wmi_buffer *in)
444 {
445 	struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
446 	struct acpi_buffer ain;
447 	acpi_status status;
448 	int ret;
449 
450 	if (wblock->gblock.flags & ACPI_WMI_STRING) {
451 		ret = wmi_marshal_string(in, &ain);
452 		if (ret < 0)
453 			return ret;
454 	} else {
455 		if (in->length > U32_MAX)
456 			return -E2BIG;
457 
458 		ain.length = in->length;
459 		ain.pointer = in->data;
460 	}
461 
462 	status = wmidev_evaluate_method(wdev, instance, method_id, &ain, NULL);
463 
464 	if (wblock->gblock.flags & ACPI_WMI_STRING)
465 		kfree(ain.pointer);
466 
467 	if (ACPI_FAILURE(status))
468 		return -EIO;
469 
470 	return 0;
471 }
472 EXPORT_SYMBOL_GPL(wmidev_invoke_procedure);
473 
474 static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
475 				 struct acpi_buffer *out)
476 {
477 	struct guid_block *block;
478 	acpi_handle handle;
479 	struct acpi_object_list input;
480 	union acpi_object wq_params[1];
481 	char method[WMI_ACPI_METHOD_NAME_SIZE];
482 
483 	if (!out)
484 		return AE_BAD_PARAMETER;
485 
486 	block = &wblock->gblock;
487 	handle = wblock->acpi_device->handle;
488 
489 	if (block->instance_count <= instance)
490 		return AE_BAD_PARAMETER;
491 
492 	/* Check GUID is a data block */
493 	if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
494 		return AE_ERROR;
495 
496 	input.count = 1;
497 	input.pointer = wq_params;
498 	wq_params[0].type = ACPI_TYPE_INTEGER;
499 	wq_params[0].integer.value = instance;
500 
501 	if (instance == 0 && test_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags))
502 		input.count = 0;
503 
504 	get_acpi_method_name(wblock, 'Q', method);
505 
506 	return acpi_evaluate_object(handle, method, &input, out);
507 }
508 
509 /**
510  * wmi_query_block - Return contents of a WMI block (deprecated)
511  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
512  * @instance: Instance index
513  * @out: Empty buffer to return the contents of the data block to
514  *
515  * Query a ACPI-WMI block, the caller must free @out.
516  *
517  * Return: ACPI object containing the content of the WMI block.
518  */
519 acpi_status wmi_query_block(const char *guid_string, u8 instance,
520 			    struct acpi_buffer *out)
521 {
522 	struct wmi_block *wblock;
523 	struct wmi_device *wdev;
524 	acpi_status status;
525 
526 	wdev = wmi_find_device_by_guid(guid_string);
527 	if (IS_ERR(wdev))
528 		return AE_ERROR;
529 
530 	if (wmi_device_enable(wdev, true) < 0)
531 		dev_warn(&wdev->dev, "Failed to enable device\n");
532 
533 	wblock = container_of(wdev, struct wmi_block, dev);
534 	status = __query_block(wblock, instance, out);
535 
536 	if (wmi_device_enable(wdev, false) < 0)
537 		dev_warn(&wdev->dev, "Failed to disable device\n");
538 
539 	wmi_device_put(wdev);
540 
541 	return status;
542 }
543 EXPORT_SYMBOL_GPL(wmi_query_block);
544 
545 /**
546  * wmidev_block_query - Return contents of a WMI block (deprectated)
547  * @wdev: A wmi bus device from a driver
548  * @instance: Instance index
549  *
550  * Query an ACPI-WMI block, the caller must free the result.
551  *
552  * Return: ACPI object containing the content of the WMI block.
553  */
554 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
555 {
556 	struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
557 	struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
558 
559 	if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
560 		return NULL;
561 
562 	return out.pointer;
563 }
564 EXPORT_SYMBOL_GPL(wmidev_block_query);
565 
566 /**
567  * wmidev_query_block - Return contents of a WMI data block
568  * @wdev: A wmi bus device from a driver
569  * @instance: Instance index
570  * @out: WMI buffer to fill
571  *
572  * Query a WMI data block, the caller must free the resulting data inside @out.
573  * Said data is guaranteed to be aligned on a 8-byte boundary.
574  *
575  * Return: 0 on success or a negative error code on failure.
576  */
577 int wmidev_query_block(struct wmi_device *wdev, u8 instance, struct wmi_buffer *out)
578 {
579 	union acpi_object *obj;
580 	int ret;
581 
582 	obj = wmidev_block_query(wdev, instance);
583 	if (!obj)
584 		return -EIO;
585 
586 	ret = wmi_unmarshal_acpi_object(obj, out);
587 	kfree(obj);
588 
589 	return ret;
590 }
591 EXPORT_SYMBOL_GPL(wmidev_query_block);
592 
593 /**
594  * wmi_set_block - Write to a WMI block (deprecated)
595  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
596  * @instance: Instance index
597  * @in: Buffer containing new values for the data block
598  *
599  * Write the contents of the input buffer to an ACPI-WMI data block.
600  *
601  * Return: acpi_status signaling success or error.
602  */
603 acpi_status wmi_set_block(const char *guid_string, u8 instance, const struct acpi_buffer *in)
604 {
605 	struct wmi_device *wdev;
606 	acpi_status status;
607 
608 	wdev = wmi_find_device_by_guid(guid_string);
609 	if (IS_ERR(wdev))
610 		return AE_ERROR;
611 
612 	if (wmi_device_enable(wdev, true) < 0)
613 		dev_warn(&wdev->dev, "Failed to enable device\n");
614 
615 	status =  wmidev_block_set(wdev, instance, in);
616 
617 	if (wmi_device_enable(wdev, false) < 0)
618 		dev_warn(&wdev->dev, "Failed to disable device\n");
619 
620 	wmi_device_put(wdev);
621 
622 	return status;
623 }
624 EXPORT_SYMBOL_GPL(wmi_set_block);
625 
626 /**
627  * wmidev_block_set - Write to a WMI block (deprecated)
628  * @wdev: A wmi bus device from a driver
629  * @instance: Instance index
630  * @in: Buffer containing new values for the data block
631  *
632  * Write contents of the input buffer to an ACPI-WMI data block.
633  *
634  * Return: acpi_status signaling success or error.
635  */
636 acpi_status wmidev_block_set(struct wmi_device *wdev, u8 instance, const struct acpi_buffer *in)
637 {
638 	struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
639 	acpi_handle handle = wblock->acpi_device->handle;
640 	struct guid_block *block = &wblock->gblock;
641 	char method[WMI_ACPI_METHOD_NAME_SIZE];
642 	struct acpi_object_list input;
643 	union acpi_object params[2];
644 
645 	if (!in)
646 		return AE_BAD_DATA;
647 
648 	if (block->instance_count <= instance)
649 		return AE_BAD_PARAMETER;
650 
651 	/* Check GUID is a data block */
652 	if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
653 		return AE_ERROR;
654 
655 	input.count = 2;
656 	input.pointer = params;
657 	params[0].type = ACPI_TYPE_INTEGER;
658 	params[0].integer.value = instance;
659 
660 	if (wblock->gblock.flags & ACPI_WMI_STRING) {
661 		params[1].type = ACPI_TYPE_STRING;
662 		params[1].string.length = in->length;
663 		params[1].string.pointer = in->pointer;
664 	} else {
665 		params[1].type = ACPI_TYPE_BUFFER;
666 		params[1].buffer.length = in->length;
667 		params[1].buffer.pointer = in->pointer;
668 	}
669 
670 	get_acpi_method_name(wblock, 'S', method);
671 
672 	return acpi_evaluate_object(handle, method, &input, NULL);
673 }
674 EXPORT_SYMBOL_GPL(wmidev_block_set);
675 
676 /**
677  * wmidev_set_block - Write to a WMI data block
678  * @wdev: A wmi bus device from a driver
679  * @instance: Instance index
680  * @in: WMI buffer containing new values for the data block
681  *
682  * Write the content of @in into a WMI data block.
683  *
684  * Return: 0 on success or negative error code on failure.
685  */
686 int wmidev_set_block(struct wmi_device *wdev, u8 instance, const struct wmi_buffer *in)
687 {
688 	struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
689 	struct acpi_buffer buffer;
690 	acpi_status status;
691 	int ret;
692 
693 	if (wblock->gblock.flags & ACPI_WMI_STRING) {
694 		ret = wmi_marshal_string(in, &buffer);
695 		if (ret < 0)
696 			return ret;
697 	} else {
698 		if (in->length > U32_MAX)
699 			return -E2BIG;
700 
701 		buffer.length = in->length;
702 		buffer.pointer = in->data;
703 	}
704 
705 	status = wmidev_block_set(wdev, instance, &buffer);
706 	if (wblock->gblock.flags & ACPI_WMI_STRING)
707 		kfree(buffer.pointer);
708 
709 	if (ACPI_FAILURE(status))
710 		return -EIO;
711 
712 	return 0;
713 }
714 EXPORT_SYMBOL_GPL(wmidev_set_block);
715 
716 /**
717  * wmi_install_notify_handler - Register handler for WMI events (deprecated)
718  * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
719  * @handler: Function to handle notifications
720  * @data: Data to be returned to handler when event is fired
721  *
722  * Register a handler for events sent to the ACPI-WMI mapper device.
723  *
724  * Return: acpi_status signaling success or error.
725  */
726 acpi_status wmi_install_notify_handler(const char *guid,
727 				       wmi_notify_handler handler,
728 				       void *data)
729 {
730 	struct wmi_block *wblock;
731 	struct wmi_device *wdev;
732 	acpi_status status;
733 
734 	wdev = wmi_find_device_by_guid(guid);
735 	if (IS_ERR(wdev))
736 		return AE_ERROR;
737 
738 	wblock = container_of(wdev, struct wmi_block, dev);
739 
740 	down_write(&wblock->notify_lock);
741 	if (wblock->handler) {
742 		status = AE_ALREADY_ACQUIRED;
743 	} else {
744 		wblock->handler = handler;
745 		wblock->handler_data = data;
746 
747 		if (wmi_device_enable(wdev, true) < 0)
748 			dev_warn(&wblock->dev.dev, "Failed to enable device\n");
749 
750 		status = AE_OK;
751 	}
752 	up_write(&wblock->notify_lock);
753 
754 	wmi_device_put(wdev);
755 
756 	return status;
757 }
758 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
759 
760 /**
761  * wmi_remove_notify_handler - Unregister handler for WMI events (deprecated)
762  * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
763  *
764  * Unregister handler for events sent to the ACPI-WMI mapper device.
765  *
766  * Return: acpi_status signaling success or error.
767  */
768 acpi_status wmi_remove_notify_handler(const char *guid)
769 {
770 	struct wmi_block *wblock;
771 	struct wmi_device *wdev;
772 	acpi_status status;
773 
774 	wdev = wmi_find_device_by_guid(guid);
775 	if (IS_ERR(wdev))
776 		return AE_ERROR;
777 
778 	wblock = container_of(wdev, struct wmi_block, dev);
779 
780 	down_write(&wblock->notify_lock);
781 	if (!wblock->handler) {
782 		status = AE_NULL_ENTRY;
783 	} else {
784 		if (wmi_device_enable(wdev, false) < 0)
785 			dev_warn(&wblock->dev.dev, "Failed to disable device\n");
786 
787 		wblock->handler = NULL;
788 		wblock->handler_data = NULL;
789 
790 		status = AE_OK;
791 	}
792 	up_write(&wblock->notify_lock);
793 
794 	wmi_device_put(wdev);
795 
796 	return status;
797 }
798 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
799 
800 /**
801  * wmi_has_guid - Check if a GUID is available
802  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
803  *
804  * Check if a given GUID is defined by _WDG.
805  *
806  * Return: True if GUID is available, false otherwise.
807  */
808 bool wmi_has_guid(const char *guid_string)
809 {
810 	struct wmi_device *wdev;
811 
812 	wdev = wmi_find_device_by_guid(guid_string);
813 	if (IS_ERR(wdev))
814 		return false;
815 
816 	wmi_device_put(wdev);
817 
818 	return true;
819 }
820 EXPORT_SYMBOL_GPL(wmi_has_guid);
821 
822 /**
823  * wmi_get_acpi_device_uid() - Get _UID name of ACPI device that defines GUID (deprecated)
824  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
825  *
826  * Find the _UID of ACPI device associated with this WMI GUID.
827  *
828  * Return: The ACPI _UID field value or NULL if the WMI GUID was not found.
829  */
830 char *wmi_get_acpi_device_uid(const char *guid_string)
831 {
832 	struct wmi_block *wblock;
833 	struct wmi_device *wdev;
834 	char *uid;
835 
836 	wdev = wmi_find_device_by_guid(guid_string);
837 	if (IS_ERR(wdev))
838 		return NULL;
839 
840 	wblock = container_of(wdev, struct wmi_block, dev);
841 	uid = acpi_device_uid(wblock->acpi_device);
842 
843 	wmi_device_put(wdev);
844 
845 	return uid;
846 }
847 EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid);
848 
849 /*
850  * sysfs interface
851  */
852 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
853 			     char *buf)
854 {
855 	struct wmi_block *wblock = dev_to_wblock(dev);
856 
857 	return sysfs_emit(buf, "wmi:%pUL\n", &wblock->gblock.guid);
858 }
859 static DEVICE_ATTR_RO(modalias);
860 
861 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
862 			 char *buf)
863 {
864 	struct wmi_block *wblock = dev_to_wblock(dev);
865 
866 	return sysfs_emit(buf, "%pUL\n", &wblock->gblock.guid);
867 }
868 static DEVICE_ATTR_RO(guid);
869 
870 static ssize_t instance_count_show(struct device *dev,
871 				   struct device_attribute *attr, char *buf)
872 {
873 	struct wmi_block *wblock = dev_to_wblock(dev);
874 
875 	return sysfs_emit(buf, "%d\n", (int)wblock->gblock.instance_count);
876 }
877 static DEVICE_ATTR_RO(instance_count);
878 
879 static ssize_t expensive_show(struct device *dev,
880 			      struct device_attribute *attr, char *buf)
881 {
882 	struct wmi_block *wblock = dev_to_wblock(dev);
883 
884 	return sysfs_emit(buf, "%d\n",
885 			  (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
886 }
887 static DEVICE_ATTR_RO(expensive);
888 
889 static ssize_t driver_override_show(struct device *dev, struct device_attribute *attr,
890 				    char *buf)
891 {
892 	struct wmi_device *wdev = to_wmi_device(dev);
893 	ssize_t ret;
894 
895 	device_lock(dev);
896 	ret = sysfs_emit(buf, "%s\n", wdev->driver_override);
897 	device_unlock(dev);
898 
899 	return ret;
900 }
901 
902 static ssize_t driver_override_store(struct device *dev, struct device_attribute *attr,
903 				     const char *buf, size_t count)
904 {
905 	struct wmi_device *wdev = to_wmi_device(dev);
906 	int ret;
907 
908 	ret = driver_set_override(dev, &wdev->driver_override, buf, count);
909 	if (ret < 0)
910 		return ret;
911 
912 	return count;
913 }
914 static DEVICE_ATTR_RW(driver_override);
915 
916 static struct attribute *wmi_attrs[] = {
917 	&dev_attr_modalias.attr,
918 	&dev_attr_guid.attr,
919 	&dev_attr_instance_count.attr,
920 	&dev_attr_expensive.attr,
921 	&dev_attr_driver_override.attr,
922 	NULL
923 };
924 ATTRIBUTE_GROUPS(wmi);
925 
926 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
927 			      char *buf)
928 {
929 	struct wmi_block *wblock = dev_to_wblock(dev);
930 
931 	return sysfs_emit(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
932 }
933 static DEVICE_ATTR_RO(notify_id);
934 
935 static struct attribute *wmi_event_attrs[] = {
936 	&dev_attr_notify_id.attr,
937 	NULL
938 };
939 ATTRIBUTE_GROUPS(wmi_event);
940 
941 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
942 			      char *buf)
943 {
944 	struct wmi_block *wblock = dev_to_wblock(dev);
945 
946 	return sysfs_emit(buf, "%c%c\n", wblock->gblock.object_id[0],
947 			  wblock->gblock.object_id[1]);
948 }
949 static DEVICE_ATTR_RO(object_id);
950 
951 static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
952 			    char *buf)
953 {
954 	struct wmi_device *wdev = to_wmi_device(dev);
955 
956 	return sysfs_emit(buf, "%d\n", (int)wdev->setable);
957 }
958 static DEVICE_ATTR_RO(setable);
959 
960 static struct attribute *wmi_data_attrs[] = {
961 	&dev_attr_object_id.attr,
962 	&dev_attr_setable.attr,
963 	NULL
964 };
965 ATTRIBUTE_GROUPS(wmi_data);
966 
967 static struct attribute *wmi_method_attrs[] = {
968 	&dev_attr_object_id.attr,
969 	NULL
970 };
971 ATTRIBUTE_GROUPS(wmi_method);
972 
973 static int wmi_dev_uevent(const struct device *dev, struct kobj_uevent_env *env)
974 {
975 	const struct wmi_block *wblock = dev_to_wblock(dev);
976 
977 	if (add_uevent_var(env, "MODALIAS=wmi:%pUL", &wblock->gblock.guid))
978 		return -ENOMEM;
979 
980 	if (add_uevent_var(env, "WMI_GUID=%pUL", &wblock->gblock.guid))
981 		return -ENOMEM;
982 
983 	return 0;
984 }
985 
986 static void wmi_dev_release(struct device *dev)
987 {
988 	struct wmi_block *wblock = dev_to_wblock(dev);
989 
990 	kfree(wblock->dev.driver_override);
991 	kfree(wblock);
992 }
993 
994 static int wmi_dev_match(struct device *dev, const struct device_driver *driver)
995 {
996 	const struct wmi_driver *wmi_driver = to_wmi_driver(driver);
997 	struct wmi_block *wblock = dev_to_wblock(dev);
998 	const struct wmi_device_id *id = wmi_driver->id_table;
999 
1000 	/* When driver_override is set, only bind to the matching driver */
1001 	if (wblock->dev.driver_override)
1002 		return !strcmp(wblock->dev.driver_override, driver->name);
1003 
1004 	if (id == NULL)
1005 		return 0;
1006 
1007 	while (*id->guid_string) {
1008 		if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid))
1009 			return 1;
1010 
1011 		id++;
1012 	}
1013 
1014 	return 0;
1015 }
1016 
1017 static void wmi_dev_disable(void *data)
1018 {
1019 	struct device *dev = data;
1020 
1021 	if (wmi_device_enable(to_wmi_device(dev), false) < 0)
1022 		dev_warn(dev, "Failed to disable device\n");
1023 }
1024 
1025 static int wmi_dev_probe(struct device *dev)
1026 {
1027 	struct wmi_block *wblock = dev_to_wblock(dev);
1028 	struct wmi_driver *wdriver = to_wmi_driver(dev->driver);
1029 	int ret;
1030 
1031 	/* Some older WMI drivers will break if instantiated multiple times,
1032 	 * so they are blocked from probing WMI devices with a duplicated GUID.
1033 	 *
1034 	 * New WMI drivers should support being instantiated multiple times.
1035 	 */
1036 	if (test_bit(WMI_GUID_DUPLICATED, &wblock->flags) && !wdriver->no_singleton) {
1037 		dev_warn(dev, "Legacy driver %s cannot be instantiated multiple times\n",
1038 			 dev->driver->name);
1039 
1040 		return -ENODEV;
1041 	}
1042 
1043 	if (wdriver->notify || wdriver->notify_new) {
1044 		if (test_bit(WMI_NO_EVENT_DATA, &wblock->flags) && !wdriver->no_notify_data)
1045 			return -ENODEV;
1046 	}
1047 
1048 	if (wmi_device_enable(to_wmi_device(dev), true) < 0)
1049 		dev_warn(dev, "failed to enable device -- probing anyway\n");
1050 
1051 	/*
1052 	 * We have to make sure that all devres-managed resources are released first because
1053 	 * some might still want to access the underlying WMI device.
1054 	 */
1055 	ret = devm_add_action_or_reset(dev, wmi_dev_disable, dev);
1056 	if (ret < 0)
1057 		return ret;
1058 
1059 	if (wdriver->probe) {
1060 		ret = wdriver->probe(to_wmi_device(dev),
1061 				find_guid_context(wblock, wdriver));
1062 		if (ret)
1063 			return ret;
1064 	}
1065 
1066 	down_write(&wblock->notify_lock);
1067 	wblock->driver_ready = true;
1068 	up_write(&wblock->notify_lock);
1069 
1070 	return 0;
1071 }
1072 
1073 static void wmi_dev_remove(struct device *dev)
1074 {
1075 	struct wmi_block *wblock = dev_to_wblock(dev);
1076 	struct wmi_driver *wdriver = to_wmi_driver(dev->driver);
1077 
1078 	down_write(&wblock->notify_lock);
1079 	wblock->driver_ready = false;
1080 	up_write(&wblock->notify_lock);
1081 
1082 	if (wdriver->remove)
1083 		wdriver->remove(to_wmi_device(dev));
1084 }
1085 
1086 static void wmi_dev_shutdown(struct device *dev)
1087 {
1088 	struct wmi_driver *wdriver;
1089 	struct wmi_block *wblock;
1090 
1091 	if (dev->driver) {
1092 		wdriver = to_wmi_driver(dev->driver);
1093 		wblock = dev_to_wblock(dev);
1094 
1095 		/*
1096 		 * Some machines return bogus WMI event data when disabling
1097 		 * the WMI event. Because of this we must prevent the associated
1098 		 * WMI driver from receiving new WMI events before disabling it.
1099 		 */
1100 		down_write(&wblock->notify_lock);
1101 		wblock->driver_ready = false;
1102 		up_write(&wblock->notify_lock);
1103 
1104 		if (wdriver->shutdown)
1105 			wdriver->shutdown(to_wmi_device(dev));
1106 
1107 		/*
1108 		 * We still need to disable the WMI device here since devres-managed resources
1109 		 * like wmi_dev_disable() will not be release during shutdown.
1110 		 */
1111 		if (wmi_device_enable(to_wmi_device(dev), false) < 0)
1112 			dev_warn(dev, "Failed to disable device\n");
1113 	}
1114 }
1115 
1116 static struct class wmi_bus_class = {
1117 	.name = "wmi_bus",
1118 };
1119 
1120 static const struct bus_type wmi_bus_type = {
1121 	.name = "wmi",
1122 	.dev_groups = wmi_groups,
1123 	.match = wmi_dev_match,
1124 	.uevent = wmi_dev_uevent,
1125 	.probe = wmi_dev_probe,
1126 	.remove = wmi_dev_remove,
1127 	.shutdown = wmi_dev_shutdown,
1128 };
1129 
1130 static const struct device_type wmi_type_event = {
1131 	.name = "event",
1132 	.groups = wmi_event_groups,
1133 	.release = wmi_dev_release,
1134 };
1135 
1136 static const struct device_type wmi_type_method = {
1137 	.name = "method",
1138 	.groups = wmi_method_groups,
1139 	.release = wmi_dev_release,
1140 };
1141 
1142 static const struct device_type wmi_type_data = {
1143 	.name = "data",
1144 	.groups = wmi_data_groups,
1145 	.release = wmi_dev_release,
1146 };
1147 
1148 static int wmi_count_guids(struct device *dev, void *data)
1149 {
1150 	struct wmi_guid_count_context *context = data;
1151 	struct wmi_block *wblock = dev_to_wblock(dev);
1152 
1153 	if (guid_equal(&wblock->gblock.guid, context->guid))
1154 		context->count++;
1155 
1156 	return 0;
1157 }
1158 
1159 static int guid_count(const guid_t *guid)
1160 {
1161 	struct wmi_guid_count_context context = {
1162 		.guid = guid,
1163 		.count = 0,
1164 	};
1165 	int ret;
1166 
1167 	ret = bus_for_each_dev(&wmi_bus_type, NULL, &context, wmi_count_guids);
1168 	if (ret < 0)
1169 		return ret;
1170 
1171 	return context.count;
1172 }
1173 
1174 static int wmi_dev_set_name(struct wmi_block *wblock, int count)
1175 {
1176 	if (IS_ENABLED(CONFIG_ACPI_WMI_LEGACY_DEVICE_NAMES)) {
1177 		if (count)
1178 			return dev_set_name(&wblock->dev.dev, "%pUL-%d", &wblock->gblock.guid,
1179 					    count);
1180 		else
1181 			return dev_set_name(&wblock->dev.dev, "%pUL", &wblock->gblock.guid);
1182 	}
1183 
1184 	return dev_set_name(&wblock->dev.dev, "%pUL-%d", &wblock->gblock.guid, wblock->dev.dev.id);
1185 }
1186 
1187 static int wmi_create_device(struct device *wmi_bus_dev,
1188 			     struct wmi_block *wblock,
1189 			     struct acpi_device *device)
1190 {
1191 	char method[WMI_ACPI_METHOD_NAME_SIZE];
1192 	struct acpi_device_info *info;
1193 	acpi_handle method_handle;
1194 	acpi_status status;
1195 	int count, ret;
1196 
1197 	if (wblock->gblock.flags & ACPI_WMI_EVENT) {
1198 		wblock->dev.dev.type = &wmi_type_event;
1199 		goto out_init;
1200 	}
1201 
1202 	if (wblock->gblock.flags & ACPI_WMI_METHOD) {
1203 		get_acpi_method_name(wblock, 'M', method);
1204 		if (!acpi_has_method(device->handle, method)) {
1205 			dev_warn(wmi_bus_dev,
1206 				 FW_BUG "%s method block execution control method not found\n",
1207 				 method);
1208 
1209 			return -ENXIO;
1210 		}
1211 
1212 		wblock->dev.dev.type = &wmi_type_method;
1213 		goto out_init;
1214 	}
1215 
1216 	/*
1217 	 * Data Block Query Control Method (WQxx by convention) is
1218 	 * required per the WMI documentation. If it is not present,
1219 	 * we ignore this data block.
1220 	 */
1221 	get_acpi_method_name(wblock, 'Q', method);
1222 	status = acpi_get_handle(device->handle, method, &method_handle);
1223 	if (ACPI_FAILURE(status)) {
1224 		dev_warn(wmi_bus_dev,
1225 			 FW_BUG "%s data block query control method not found\n",
1226 			 method);
1227 
1228 		return -ENXIO;
1229 	}
1230 
1231 	status = acpi_get_object_info(method_handle, &info);
1232 	if (ACPI_FAILURE(status))
1233 		return -EIO;
1234 
1235 	wblock->dev.dev.type = &wmi_type_data;
1236 
1237 	/*
1238 	 * The Microsoft documentation specifically states:
1239 	 *
1240 	 *   Data blocks registered with only a single instance
1241 	 *   can ignore the parameter.
1242 	 *
1243 	 * ACPICA will get mad at us if we call the method with the wrong number
1244 	 * of arguments, so check what our method expects.  (On some Dell
1245 	 * laptops, WQxx may not be a method at all.)
1246 	 */
1247 	if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1248 		set_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags);
1249 
1250 	kfree(info);
1251 
1252 	get_acpi_method_name(wblock, 'S', method);
1253 	if (acpi_has_method(device->handle, method))
1254 		wblock->dev.setable = true;
1255 
1256  out_init:
1257 	init_rwsem(&wblock->notify_lock);
1258 	wblock->driver_ready = false;
1259 	wblock->dev.dev.bus = &wmi_bus_type;
1260 	wblock->dev.dev.parent = wmi_bus_dev;
1261 
1262 	count = guid_count(&wblock->gblock.guid);
1263 	if (count < 0)
1264 		return count;
1265 
1266 	if (count)
1267 		set_bit(WMI_GUID_DUPLICATED, &wblock->flags);
1268 
1269 	ret = ida_alloc(&wmi_ida, GFP_KERNEL);
1270 	if (ret < 0)
1271 		return ret;
1272 
1273 	wblock->dev.dev.id = ret;
1274 	ret = wmi_dev_set_name(wblock, count);
1275 	if (ret < 0) {
1276 		ida_free(&wmi_ida, wblock->dev.dev.id);
1277 		return ret;
1278 	}
1279 
1280 	device_initialize(&wblock->dev.dev);
1281 
1282 	return 0;
1283 }
1284 
1285 static int wmi_add_device(struct platform_device *pdev, struct wmi_device *wdev)
1286 {
1287 	struct device_link *link;
1288 
1289 	/*
1290 	 * Many aggregate WMI drivers do not use -EPROBE_DEFER when they
1291 	 * are unable to find a WMI device during probe, instead they require
1292 	 * all WMI devices associated with an platform device to become available
1293 	 * at once. This device link thus prevents WMI drivers from probing until
1294 	 * the associated platform device has finished probing (and has registered
1295 	 * all discovered WMI devices).
1296 	 */
1297 
1298 	link = device_link_add(&wdev->dev, &pdev->dev, DL_FLAG_AUTOREMOVE_SUPPLIER);
1299 	if (!link)
1300 		return -EINVAL;
1301 
1302 	return device_add(&wdev->dev);
1303 }
1304 
1305 /*
1306  * Parse the _WDG method for the GUID data blocks
1307  */
1308 static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev)
1309 {
1310 	struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
1311 	struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1312 	const struct guid_block *gblock;
1313 	bool event_data_available;
1314 	struct wmi_block *wblock;
1315 	union acpi_object *obj;
1316 	acpi_status status;
1317 	u32 i, total;
1318 	int retval;
1319 
1320 	status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1321 	if (ACPI_FAILURE(status))
1322 		return -ENXIO;
1323 
1324 	obj = out.pointer;
1325 	if (!obj)
1326 		return -ENXIO;
1327 
1328 	if (obj->type != ACPI_TYPE_BUFFER) {
1329 		kfree(obj);
1330 		return -ENXIO;
1331 	}
1332 
1333 	event_data_available = acpi_has_method(device->handle, "_WED");
1334 	gblock = (const struct guid_block *)obj->buffer.pointer;
1335 	total = obj->buffer.length / sizeof(struct guid_block);
1336 
1337 	for (i = 0; i < total; i++) {
1338 		if (!gblock[i].instance_count) {
1339 			dev_info(wmi_bus_dev, FW_INFO "%pUL has zero instances\n", &gblock[i].guid);
1340 			continue;
1341 		}
1342 
1343 		wblock = kzalloc_obj(*wblock);
1344 		if (!wblock)
1345 			continue;
1346 
1347 		wblock->acpi_device = device;
1348 		wblock->gblock = gblock[i];
1349 		if (gblock[i].flags & ACPI_WMI_EVENT && !event_data_available)
1350 			set_bit(WMI_NO_EVENT_DATA, &wblock->flags);
1351 
1352 		retval = wmi_create_device(wmi_bus_dev, wblock, device);
1353 		if (retval) {
1354 			kfree(wblock);
1355 			continue;
1356 		}
1357 
1358 		retval = wmi_add_device(pdev, &wblock->dev);
1359 		if (retval) {
1360 			dev_err(wmi_bus_dev, "failed to register %pUL\n",
1361 				&wblock->gblock.guid);
1362 
1363 			ida_free(&wmi_ida, wblock->dev.dev.id);
1364 			put_device(&wblock->dev.dev);
1365 		}
1366 	}
1367 
1368 	kfree(obj);
1369 
1370 	return 0;
1371 }
1372 
1373 static int wmi_get_notify_data(struct wmi_block *wblock, union acpi_object **obj)
1374 {
1375 	struct acpi_buffer data = { ACPI_ALLOCATE_BUFFER, NULL };
1376 	union acpi_object param = {
1377 		.integer = {
1378 			.type = ACPI_TYPE_INTEGER,
1379 			.value = wblock->gblock.notify_id,
1380 		}
1381 	};
1382 	struct acpi_object_list input = {
1383 		.count = 1,
1384 		.pointer = &param,
1385 	};
1386 	acpi_status status;
1387 
1388 	status = acpi_evaluate_object(wblock->acpi_device->handle, "_WED", &input, &data);
1389 	if (ACPI_FAILURE(status)) {
1390 		dev_warn(&wblock->dev.dev, "Failed to get event data\n");
1391 		return -EIO;
1392 	}
1393 
1394 	*obj = data.pointer;
1395 
1396 	return 0;
1397 }
1398 
1399 static void wmi_notify_driver(struct wmi_block *wblock, union acpi_object *obj)
1400 {
1401 	struct wmi_driver *driver = to_wmi_driver(wblock->dev.dev.driver);
1402 	struct wmi_buffer buffer;
1403 	int ret;
1404 
1405 	if (!obj && !driver->no_notify_data) {
1406 		dev_warn(&wblock->dev.dev, "Event contains no event data\n");
1407 		return;
1408 	}
1409 
1410 	if (driver->notify)
1411 		driver->notify(&wblock->dev, obj);
1412 
1413 	if (driver->notify_new) {
1414 		if (!obj) {
1415 			driver->notify_new(&wblock->dev, NULL);
1416 			return;
1417 		}
1418 
1419 		ret = wmi_unmarshal_acpi_object(obj, &buffer);
1420 		if (ret < 0) {
1421 			dev_warn(&wblock->dev.dev, "Failed to unmarshal event data: %d\n", ret);
1422 			return;
1423 		}
1424 
1425 		driver->notify_new(&wblock->dev, &buffer);
1426 		kfree(buffer.data);
1427 	}
1428 }
1429 
1430 static int wmi_notify_device(struct device *dev, void *data)
1431 {
1432 	struct wmi_block *wblock = dev_to_wblock(dev);
1433 	union acpi_object *obj = NULL;
1434 	u32 *event = data;
1435 	int ret;
1436 
1437 	if (!(wblock->gblock.flags & ACPI_WMI_EVENT && wblock->gblock.notify_id == *event))
1438 		return 0;
1439 
1440 	/* The ACPI WMI specification says that _WED should be
1441 	 * evaluated every time an notification is received, even
1442 	 * if no consumers are present.
1443 	 *
1444 	 * Some firmware implementations actually depend on this
1445 	 * by using a queue for events which will fill up if the
1446 	 * WMI driver core stops evaluating _WED due to missing
1447 	 * WMI event consumers.
1448 	 */
1449 	if (!test_bit(WMI_NO_EVENT_DATA, &wblock->flags)) {
1450 		ret = wmi_get_notify_data(wblock, &obj);
1451 		if (ret < 0)
1452 			return -EIO;
1453 	}
1454 
1455 	down_read(&wblock->notify_lock);
1456 
1457 	if (wblock->dev.dev.driver && wblock->driver_ready)
1458 		wmi_notify_driver(wblock, obj);
1459 
1460 	if (wblock->handler)
1461 		wblock->handler(obj, wblock->handler_data);
1462 
1463 	up_read(&wblock->notify_lock);
1464 
1465 	kfree(obj);
1466 
1467 	acpi_bus_generate_netlink_event("wmi", acpi_dev_name(wblock->acpi_device), *event, 0);
1468 
1469 	return -EBUSY;
1470 }
1471 
1472 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event, void *context)
1473 {
1474 	struct device *wmi_bus_dev = context;
1475 
1476 	device_for_each_child(wmi_bus_dev, &event, wmi_notify_device);
1477 }
1478 
1479 static int wmi_remove_device(struct device *dev, void *data)
1480 {
1481 	int id = dev->id;
1482 
1483 	device_unregister(dev);
1484 	ida_free(&wmi_ida, id);
1485 
1486 	return 0;
1487 }
1488 
1489 static void acpi_wmi_remove(struct platform_device *device)
1490 {
1491 	struct device *wmi_bus_device = dev_get_drvdata(&device->dev);
1492 
1493 	device_for_each_child_reverse(wmi_bus_device, NULL, wmi_remove_device);
1494 }
1495 
1496 static void acpi_wmi_remove_notify_handler(void *data)
1497 {
1498 	struct acpi_device *acpi_device = data;
1499 
1500 	acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY, acpi_wmi_notify_handler);
1501 }
1502 
1503 static void acpi_wmi_remove_bus_device(void *data)
1504 {
1505 	struct device *wmi_bus_dev = data;
1506 
1507 	device_unregister(wmi_bus_dev);
1508 }
1509 
1510 static int acpi_wmi_probe(struct platform_device *device)
1511 {
1512 	struct acpi_device *acpi_device;
1513 	struct device *wmi_bus_dev;
1514 	acpi_status status;
1515 	int error;
1516 
1517 	acpi_device = ACPI_COMPANION(&device->dev);
1518 	if (!acpi_device) {
1519 		dev_err(&device->dev, "ACPI companion is missing\n");
1520 		return -ENODEV;
1521 	}
1522 
1523 	wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0), NULL, "wmi_bus-%s",
1524 				    dev_name(&device->dev));
1525 	if (IS_ERR(wmi_bus_dev))
1526 		return PTR_ERR(wmi_bus_dev);
1527 
1528 	error = devm_add_action_or_reset(&device->dev, acpi_wmi_remove_bus_device, wmi_bus_dev);
1529 	if (error < 0)
1530 		return error;
1531 
1532 	dev_set_drvdata(&device->dev, wmi_bus_dev);
1533 
1534 	status = acpi_install_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
1535 					     acpi_wmi_notify_handler, wmi_bus_dev);
1536 	if (ACPI_FAILURE(status)) {
1537 		dev_err(&device->dev, "Error installing notify handler\n");
1538 		return -ENODEV;
1539 	}
1540 	error = devm_add_action_or_reset(&device->dev, acpi_wmi_remove_notify_handler,
1541 					 acpi_device);
1542 	if (error < 0)
1543 		return error;
1544 
1545 	error = parse_wdg(wmi_bus_dev, device);
1546 	if (error) {
1547 		dev_err(&device->dev, "Failed to parse _WDG method\n");
1548 		return error;
1549 	}
1550 
1551 	return 0;
1552 }
1553 
1554 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1555 				       struct module *owner)
1556 {
1557 	driver->driver.owner = owner;
1558 	driver->driver.bus = &wmi_bus_type;
1559 
1560 	return driver_register(&driver->driver);
1561 }
1562 EXPORT_SYMBOL(__wmi_driver_register);
1563 
1564 /**
1565  * wmi_driver_unregister() - Unregister a WMI driver
1566  * @driver: WMI driver to unregister
1567  *
1568  * Unregisters a WMI driver from the WMI bus.
1569  */
1570 void wmi_driver_unregister(struct wmi_driver *driver)
1571 {
1572 	driver_unregister(&driver->driver);
1573 }
1574 EXPORT_SYMBOL(wmi_driver_unregister);
1575 
1576 static struct platform_driver acpi_wmi_driver = {
1577 	.driver = {
1578 		.name = "acpi-wmi",
1579 		.acpi_match_table = wmi_device_ids,
1580 	},
1581 	.probe = acpi_wmi_probe,
1582 	.remove = acpi_wmi_remove,
1583 };
1584 
1585 static int __init acpi_wmi_init(void)
1586 {
1587 	int error;
1588 
1589 	if (acpi_disabled)
1590 		return -ENODEV;
1591 
1592 	error = class_register(&wmi_bus_class);
1593 	if (error)
1594 		return error;
1595 
1596 	error = bus_register(&wmi_bus_type);
1597 	if (error)
1598 		goto err_unreg_class;
1599 
1600 	error = platform_driver_register(&acpi_wmi_driver);
1601 	if (error) {
1602 		pr_err("Error loading mapper\n");
1603 		goto err_unreg_bus;
1604 	}
1605 
1606 	return 0;
1607 
1608 err_unreg_bus:
1609 	bus_unregister(&wmi_bus_type);
1610 
1611 err_unreg_class:
1612 	class_unregister(&wmi_bus_class);
1613 
1614 	return error;
1615 }
1616 
1617 static void __exit acpi_wmi_exit(void)
1618 {
1619 	platform_driver_unregister(&acpi_wmi_driver);
1620 	bus_unregister(&wmi_bus_type);
1621 	class_unregister(&wmi_bus_class);
1622 }
1623 
1624 subsys_initcall_sync(acpi_wmi_init);
1625 module_exit(acpi_wmi_exit);
1626