xref: /linux/drivers/platform/wmi/core.c (revision 015b70a6ae697f5dac3562e4ab45ee275d98860b)
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 static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
431 				 struct acpi_buffer *out)
432 {
433 	struct guid_block *block;
434 	acpi_handle handle;
435 	struct acpi_object_list input;
436 	union acpi_object wq_params[1];
437 	char method[WMI_ACPI_METHOD_NAME_SIZE];
438 
439 	if (!out)
440 		return AE_BAD_PARAMETER;
441 
442 	block = &wblock->gblock;
443 	handle = wblock->acpi_device->handle;
444 
445 	if (block->instance_count <= instance)
446 		return AE_BAD_PARAMETER;
447 
448 	/* Check GUID is a data block */
449 	if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
450 		return AE_ERROR;
451 
452 	input.count = 1;
453 	input.pointer = wq_params;
454 	wq_params[0].type = ACPI_TYPE_INTEGER;
455 	wq_params[0].integer.value = instance;
456 
457 	if (instance == 0 && test_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags))
458 		input.count = 0;
459 
460 	get_acpi_method_name(wblock, 'Q', method);
461 
462 	return acpi_evaluate_object(handle, method, &input, out);
463 }
464 
465 /**
466  * wmi_query_block - Return contents of a WMI block (deprecated)
467  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
468  * @instance: Instance index
469  * @out: Empty buffer to return the contents of the data block to
470  *
471  * Query a ACPI-WMI block, the caller must free @out.
472  *
473  * Return: ACPI object containing the content of the WMI block.
474  */
475 acpi_status wmi_query_block(const char *guid_string, u8 instance,
476 			    struct acpi_buffer *out)
477 {
478 	struct wmi_block *wblock;
479 	struct wmi_device *wdev;
480 	acpi_status status;
481 
482 	wdev = wmi_find_device_by_guid(guid_string);
483 	if (IS_ERR(wdev))
484 		return AE_ERROR;
485 
486 	if (wmi_device_enable(wdev, true) < 0)
487 		dev_warn(&wdev->dev, "Failed to enable device\n");
488 
489 	wblock = container_of(wdev, struct wmi_block, dev);
490 	status = __query_block(wblock, instance, out);
491 
492 	if (wmi_device_enable(wdev, false) < 0)
493 		dev_warn(&wdev->dev, "Failed to disable device\n");
494 
495 	wmi_device_put(wdev);
496 
497 	return status;
498 }
499 EXPORT_SYMBOL_GPL(wmi_query_block);
500 
501 /**
502  * wmidev_block_query - Return contents of a WMI block (deprectated)
503  * @wdev: A wmi bus device from a driver
504  * @instance: Instance index
505  *
506  * Query an ACPI-WMI block, the caller must free the result.
507  *
508  * Return: ACPI object containing the content of the WMI block.
509  */
510 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
511 {
512 	struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
513 	struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
514 
515 	if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
516 		return NULL;
517 
518 	return out.pointer;
519 }
520 EXPORT_SYMBOL_GPL(wmidev_block_query);
521 
522 /**
523  * wmidev_query_block - Return contents of a WMI data block
524  * @wdev: A wmi bus device from a driver
525  * @instance: Instance index
526  * @out: WMI buffer to fill
527  *
528  * Query a WMI data block, the caller must free the resulting data inside @out.
529  * Said data is guaranteed to be aligned on a 8-byte boundary.
530  *
531  * Return: 0 on success or a negative error code on failure.
532  */
533 int wmidev_query_block(struct wmi_device *wdev, u8 instance, struct wmi_buffer *out)
534 {
535 	union acpi_object *obj;
536 	int ret;
537 
538 	obj = wmidev_block_query(wdev, instance);
539 	if (!obj)
540 		return -EIO;
541 
542 	ret = wmi_unmarshal_acpi_object(obj, out);
543 	kfree(obj);
544 
545 	return ret;
546 }
547 EXPORT_SYMBOL_GPL(wmidev_query_block);
548 
549 /**
550  * wmi_set_block - Write to a WMI block (deprecated)
551  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
552  * @instance: Instance index
553  * @in: Buffer containing new values for the data block
554  *
555  * Write the contents of the input buffer to an ACPI-WMI data block.
556  *
557  * Return: acpi_status signaling success or error.
558  */
559 acpi_status wmi_set_block(const char *guid_string, u8 instance, const struct acpi_buffer *in)
560 {
561 	struct wmi_device *wdev;
562 	acpi_status status;
563 
564 	wdev = wmi_find_device_by_guid(guid_string);
565 	if (IS_ERR(wdev))
566 		return AE_ERROR;
567 
568 	if (wmi_device_enable(wdev, true) < 0)
569 		dev_warn(&wdev->dev, "Failed to enable device\n");
570 
571 	status =  wmidev_block_set(wdev, instance, in);
572 
573 	if (wmi_device_enable(wdev, false) < 0)
574 		dev_warn(&wdev->dev, "Failed to disable device\n");
575 
576 	wmi_device_put(wdev);
577 
578 	return status;
579 }
580 EXPORT_SYMBOL_GPL(wmi_set_block);
581 
582 /**
583  * wmidev_block_set - Write to a WMI block (deprecated)
584  * @wdev: A wmi bus device from a driver
585  * @instance: Instance index
586  * @in: Buffer containing new values for the data block
587  *
588  * Write contents of the input buffer to an ACPI-WMI data block.
589  *
590  * Return: acpi_status signaling success or error.
591  */
592 acpi_status wmidev_block_set(struct wmi_device *wdev, u8 instance, const struct acpi_buffer *in)
593 {
594 	struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
595 	acpi_handle handle = wblock->acpi_device->handle;
596 	struct guid_block *block = &wblock->gblock;
597 	char method[WMI_ACPI_METHOD_NAME_SIZE];
598 	struct acpi_object_list input;
599 	union acpi_object params[2];
600 
601 	if (!in)
602 		return AE_BAD_DATA;
603 
604 	if (block->instance_count <= instance)
605 		return AE_BAD_PARAMETER;
606 
607 	/* Check GUID is a data block */
608 	if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
609 		return AE_ERROR;
610 
611 	input.count = 2;
612 	input.pointer = params;
613 	params[0].type = ACPI_TYPE_INTEGER;
614 	params[0].integer.value = instance;
615 
616 	if (wblock->gblock.flags & ACPI_WMI_STRING) {
617 		params[1].type = ACPI_TYPE_STRING;
618 		params[1].string.length = in->length;
619 		params[1].string.pointer = in->pointer;
620 	} else {
621 		params[1].type = ACPI_TYPE_BUFFER;
622 		params[1].buffer.length = in->length;
623 		params[1].buffer.pointer = in->pointer;
624 	}
625 
626 	get_acpi_method_name(wblock, 'S', method);
627 
628 	return acpi_evaluate_object(handle, method, &input, NULL);
629 }
630 EXPORT_SYMBOL_GPL(wmidev_block_set);
631 
632 /**
633  * wmidev_set_block - Write to a WMI data block
634  * @wdev: A wmi bus device from a driver
635  * @instance: Instance index
636  * @in: WMI buffer containing new values for the data block
637  *
638  * Write the content of @in into a WMI data block.
639  *
640  * Return: 0 on success or negative error code on failure.
641  */
642 int wmidev_set_block(struct wmi_device *wdev, u8 instance, const struct wmi_buffer *in)
643 {
644 	struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
645 	struct acpi_buffer buffer;
646 	acpi_status status;
647 	int ret;
648 
649 	if (wblock->gblock.flags & ACPI_WMI_STRING) {
650 		ret = wmi_marshal_string(in, &buffer);
651 		if (ret < 0)
652 			return ret;
653 	} else {
654 		if (in->length > U32_MAX)
655 			return -E2BIG;
656 
657 		buffer.length = in->length;
658 		buffer.pointer = in->data;
659 	}
660 
661 	status = wmidev_block_set(wdev, instance, &buffer);
662 	if (wblock->gblock.flags & ACPI_WMI_STRING)
663 		kfree(buffer.pointer);
664 
665 	if (ACPI_FAILURE(status))
666 		return -EIO;
667 
668 	return 0;
669 }
670 EXPORT_SYMBOL_GPL(wmidev_set_block);
671 
672 /**
673  * wmi_install_notify_handler - Register handler for WMI events (deprecated)
674  * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
675  * @handler: Function to handle notifications
676  * @data: Data to be returned to handler when event is fired
677  *
678  * Register a handler for events sent to the ACPI-WMI mapper device.
679  *
680  * Return: acpi_status signaling success or error.
681  */
682 acpi_status wmi_install_notify_handler(const char *guid,
683 				       wmi_notify_handler handler,
684 				       void *data)
685 {
686 	struct wmi_block *wblock;
687 	struct wmi_device *wdev;
688 	acpi_status status;
689 
690 	wdev = wmi_find_device_by_guid(guid);
691 	if (IS_ERR(wdev))
692 		return AE_ERROR;
693 
694 	wblock = container_of(wdev, struct wmi_block, dev);
695 
696 	down_write(&wblock->notify_lock);
697 	if (wblock->handler) {
698 		status = AE_ALREADY_ACQUIRED;
699 	} else {
700 		wblock->handler = handler;
701 		wblock->handler_data = data;
702 
703 		if (wmi_device_enable(wdev, true) < 0)
704 			dev_warn(&wblock->dev.dev, "Failed to enable device\n");
705 
706 		status = AE_OK;
707 	}
708 	up_write(&wblock->notify_lock);
709 
710 	wmi_device_put(wdev);
711 
712 	return status;
713 }
714 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
715 
716 /**
717  * wmi_remove_notify_handler - Unregister handler for WMI events (deprecated)
718  * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
719  *
720  * Unregister handler for events sent to the ACPI-WMI mapper device.
721  *
722  * Return: acpi_status signaling success or error.
723  */
724 acpi_status wmi_remove_notify_handler(const char *guid)
725 {
726 	struct wmi_block *wblock;
727 	struct wmi_device *wdev;
728 	acpi_status status;
729 
730 	wdev = wmi_find_device_by_guid(guid);
731 	if (IS_ERR(wdev))
732 		return AE_ERROR;
733 
734 	wblock = container_of(wdev, struct wmi_block, dev);
735 
736 	down_write(&wblock->notify_lock);
737 	if (!wblock->handler) {
738 		status = AE_NULL_ENTRY;
739 	} else {
740 		if (wmi_device_enable(wdev, false) < 0)
741 			dev_warn(&wblock->dev.dev, "Failed to disable device\n");
742 
743 		wblock->handler = NULL;
744 		wblock->handler_data = NULL;
745 
746 		status = AE_OK;
747 	}
748 	up_write(&wblock->notify_lock);
749 
750 	wmi_device_put(wdev);
751 
752 	return status;
753 }
754 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
755 
756 /**
757  * wmi_has_guid - Check if a GUID is available
758  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
759  *
760  * Check if a given GUID is defined by _WDG.
761  *
762  * Return: True if GUID is available, false otherwise.
763  */
764 bool wmi_has_guid(const char *guid_string)
765 {
766 	struct wmi_device *wdev;
767 
768 	wdev = wmi_find_device_by_guid(guid_string);
769 	if (IS_ERR(wdev))
770 		return false;
771 
772 	wmi_device_put(wdev);
773 
774 	return true;
775 }
776 EXPORT_SYMBOL_GPL(wmi_has_guid);
777 
778 /**
779  * wmi_get_acpi_device_uid() - Get _UID name of ACPI device that defines GUID (deprecated)
780  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
781  *
782  * Find the _UID of ACPI device associated with this WMI GUID.
783  *
784  * Return: The ACPI _UID field value or NULL if the WMI GUID was not found.
785  */
786 char *wmi_get_acpi_device_uid(const char *guid_string)
787 {
788 	struct wmi_block *wblock;
789 	struct wmi_device *wdev;
790 	char *uid;
791 
792 	wdev = wmi_find_device_by_guid(guid_string);
793 	if (IS_ERR(wdev))
794 		return NULL;
795 
796 	wblock = container_of(wdev, struct wmi_block, dev);
797 	uid = acpi_device_uid(wblock->acpi_device);
798 
799 	wmi_device_put(wdev);
800 
801 	return uid;
802 }
803 EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid);
804 
805 /*
806  * sysfs interface
807  */
808 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
809 			     char *buf)
810 {
811 	struct wmi_block *wblock = dev_to_wblock(dev);
812 
813 	return sysfs_emit(buf, "wmi:%pUL\n", &wblock->gblock.guid);
814 }
815 static DEVICE_ATTR_RO(modalias);
816 
817 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
818 			 char *buf)
819 {
820 	struct wmi_block *wblock = dev_to_wblock(dev);
821 
822 	return sysfs_emit(buf, "%pUL\n", &wblock->gblock.guid);
823 }
824 static DEVICE_ATTR_RO(guid);
825 
826 static ssize_t instance_count_show(struct device *dev,
827 				   struct device_attribute *attr, char *buf)
828 {
829 	struct wmi_block *wblock = dev_to_wblock(dev);
830 
831 	return sysfs_emit(buf, "%d\n", (int)wblock->gblock.instance_count);
832 }
833 static DEVICE_ATTR_RO(instance_count);
834 
835 static ssize_t expensive_show(struct device *dev,
836 			      struct device_attribute *attr, char *buf)
837 {
838 	struct wmi_block *wblock = dev_to_wblock(dev);
839 
840 	return sysfs_emit(buf, "%d\n",
841 			  (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
842 }
843 static DEVICE_ATTR_RO(expensive);
844 
845 static ssize_t driver_override_show(struct device *dev, struct device_attribute *attr,
846 				    char *buf)
847 {
848 	struct wmi_device *wdev = to_wmi_device(dev);
849 	ssize_t ret;
850 
851 	device_lock(dev);
852 	ret = sysfs_emit(buf, "%s\n", wdev->driver_override);
853 	device_unlock(dev);
854 
855 	return ret;
856 }
857 
858 static ssize_t driver_override_store(struct device *dev, struct device_attribute *attr,
859 				     const char *buf, size_t count)
860 {
861 	struct wmi_device *wdev = to_wmi_device(dev);
862 	int ret;
863 
864 	ret = driver_set_override(dev, &wdev->driver_override, buf, count);
865 	if (ret < 0)
866 		return ret;
867 
868 	return count;
869 }
870 static DEVICE_ATTR_RW(driver_override);
871 
872 static struct attribute *wmi_attrs[] = {
873 	&dev_attr_modalias.attr,
874 	&dev_attr_guid.attr,
875 	&dev_attr_instance_count.attr,
876 	&dev_attr_expensive.attr,
877 	&dev_attr_driver_override.attr,
878 	NULL
879 };
880 ATTRIBUTE_GROUPS(wmi);
881 
882 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
883 			      char *buf)
884 {
885 	struct wmi_block *wblock = dev_to_wblock(dev);
886 
887 	return sysfs_emit(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
888 }
889 static DEVICE_ATTR_RO(notify_id);
890 
891 static struct attribute *wmi_event_attrs[] = {
892 	&dev_attr_notify_id.attr,
893 	NULL
894 };
895 ATTRIBUTE_GROUPS(wmi_event);
896 
897 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
898 			      char *buf)
899 {
900 	struct wmi_block *wblock = dev_to_wblock(dev);
901 
902 	return sysfs_emit(buf, "%c%c\n", wblock->gblock.object_id[0],
903 			  wblock->gblock.object_id[1]);
904 }
905 static DEVICE_ATTR_RO(object_id);
906 
907 static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
908 			    char *buf)
909 {
910 	struct wmi_device *wdev = to_wmi_device(dev);
911 
912 	return sysfs_emit(buf, "%d\n", (int)wdev->setable);
913 }
914 static DEVICE_ATTR_RO(setable);
915 
916 static struct attribute *wmi_data_attrs[] = {
917 	&dev_attr_object_id.attr,
918 	&dev_attr_setable.attr,
919 	NULL
920 };
921 ATTRIBUTE_GROUPS(wmi_data);
922 
923 static struct attribute *wmi_method_attrs[] = {
924 	&dev_attr_object_id.attr,
925 	NULL
926 };
927 ATTRIBUTE_GROUPS(wmi_method);
928 
929 static int wmi_dev_uevent(const struct device *dev, struct kobj_uevent_env *env)
930 {
931 	const struct wmi_block *wblock = dev_to_wblock(dev);
932 
933 	if (add_uevent_var(env, "MODALIAS=wmi:%pUL", &wblock->gblock.guid))
934 		return -ENOMEM;
935 
936 	if (add_uevent_var(env, "WMI_GUID=%pUL", &wblock->gblock.guid))
937 		return -ENOMEM;
938 
939 	return 0;
940 }
941 
942 static void wmi_dev_release(struct device *dev)
943 {
944 	struct wmi_block *wblock = dev_to_wblock(dev);
945 
946 	kfree(wblock->dev.driver_override);
947 	kfree(wblock);
948 }
949 
950 static int wmi_dev_match(struct device *dev, const struct device_driver *driver)
951 {
952 	const struct wmi_driver *wmi_driver = to_wmi_driver(driver);
953 	struct wmi_block *wblock = dev_to_wblock(dev);
954 	const struct wmi_device_id *id = wmi_driver->id_table;
955 
956 	/* When driver_override is set, only bind to the matching driver */
957 	if (wblock->dev.driver_override)
958 		return !strcmp(wblock->dev.driver_override, driver->name);
959 
960 	if (id == NULL)
961 		return 0;
962 
963 	while (*id->guid_string) {
964 		if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid))
965 			return 1;
966 
967 		id++;
968 	}
969 
970 	return 0;
971 }
972 
973 static void wmi_dev_disable(void *data)
974 {
975 	struct device *dev = data;
976 
977 	if (wmi_device_enable(to_wmi_device(dev), false) < 0)
978 		dev_warn(dev, "Failed to disable device\n");
979 }
980 
981 static int wmi_dev_probe(struct device *dev)
982 {
983 	struct wmi_block *wblock = dev_to_wblock(dev);
984 	struct wmi_driver *wdriver = to_wmi_driver(dev->driver);
985 	int ret;
986 
987 	/* Some older WMI drivers will break if instantiated multiple times,
988 	 * so they are blocked from probing WMI devices with a duplicated GUID.
989 	 *
990 	 * New WMI drivers should support being instantiated multiple times.
991 	 */
992 	if (test_bit(WMI_GUID_DUPLICATED, &wblock->flags) && !wdriver->no_singleton) {
993 		dev_warn(dev, "Legacy driver %s cannot be instantiated multiple times\n",
994 			 dev->driver->name);
995 
996 		return -ENODEV;
997 	}
998 
999 	if (wdriver->notify || wdriver->notify_new) {
1000 		if (test_bit(WMI_NO_EVENT_DATA, &wblock->flags) && !wdriver->no_notify_data)
1001 			return -ENODEV;
1002 	}
1003 
1004 	if (wmi_device_enable(to_wmi_device(dev), true) < 0)
1005 		dev_warn(dev, "failed to enable device -- probing anyway\n");
1006 
1007 	/*
1008 	 * We have to make sure that all devres-managed resources are released first because
1009 	 * some might still want to access the underlying WMI device.
1010 	 */
1011 	ret = devm_add_action_or_reset(dev, wmi_dev_disable, dev);
1012 	if (ret < 0)
1013 		return ret;
1014 
1015 	if (wdriver->probe) {
1016 		ret = wdriver->probe(to_wmi_device(dev),
1017 				find_guid_context(wblock, wdriver));
1018 		if (ret)
1019 			return ret;
1020 	}
1021 
1022 	down_write(&wblock->notify_lock);
1023 	wblock->driver_ready = true;
1024 	up_write(&wblock->notify_lock);
1025 
1026 	return 0;
1027 }
1028 
1029 static void wmi_dev_remove(struct device *dev)
1030 {
1031 	struct wmi_block *wblock = dev_to_wblock(dev);
1032 	struct wmi_driver *wdriver = to_wmi_driver(dev->driver);
1033 
1034 	down_write(&wblock->notify_lock);
1035 	wblock->driver_ready = false;
1036 	up_write(&wblock->notify_lock);
1037 
1038 	if (wdriver->remove)
1039 		wdriver->remove(to_wmi_device(dev));
1040 }
1041 
1042 static void wmi_dev_shutdown(struct device *dev)
1043 {
1044 	struct wmi_driver *wdriver;
1045 	struct wmi_block *wblock;
1046 
1047 	if (dev->driver) {
1048 		wdriver = to_wmi_driver(dev->driver);
1049 		wblock = dev_to_wblock(dev);
1050 
1051 		/*
1052 		 * Some machines return bogus WMI event data when disabling
1053 		 * the WMI event. Because of this we must prevent the associated
1054 		 * WMI driver from receiving new WMI events before disabling it.
1055 		 */
1056 		down_write(&wblock->notify_lock);
1057 		wblock->driver_ready = false;
1058 		up_write(&wblock->notify_lock);
1059 
1060 		if (wdriver->shutdown)
1061 			wdriver->shutdown(to_wmi_device(dev));
1062 
1063 		/*
1064 		 * We still need to disable the WMI device here since devres-managed resources
1065 		 * like wmi_dev_disable() will not be release during shutdown.
1066 		 */
1067 		if (wmi_device_enable(to_wmi_device(dev), false) < 0)
1068 			dev_warn(dev, "Failed to disable device\n");
1069 	}
1070 }
1071 
1072 static struct class wmi_bus_class = {
1073 	.name = "wmi_bus",
1074 };
1075 
1076 static const struct bus_type wmi_bus_type = {
1077 	.name = "wmi",
1078 	.dev_groups = wmi_groups,
1079 	.match = wmi_dev_match,
1080 	.uevent = wmi_dev_uevent,
1081 	.probe = wmi_dev_probe,
1082 	.remove = wmi_dev_remove,
1083 	.shutdown = wmi_dev_shutdown,
1084 };
1085 
1086 static const struct device_type wmi_type_event = {
1087 	.name = "event",
1088 	.groups = wmi_event_groups,
1089 	.release = wmi_dev_release,
1090 };
1091 
1092 static const struct device_type wmi_type_method = {
1093 	.name = "method",
1094 	.groups = wmi_method_groups,
1095 	.release = wmi_dev_release,
1096 };
1097 
1098 static const struct device_type wmi_type_data = {
1099 	.name = "data",
1100 	.groups = wmi_data_groups,
1101 	.release = wmi_dev_release,
1102 };
1103 
1104 static int wmi_count_guids(struct device *dev, void *data)
1105 {
1106 	struct wmi_guid_count_context *context = data;
1107 	struct wmi_block *wblock = dev_to_wblock(dev);
1108 
1109 	if (guid_equal(&wblock->gblock.guid, context->guid))
1110 		context->count++;
1111 
1112 	return 0;
1113 }
1114 
1115 static int guid_count(const guid_t *guid)
1116 {
1117 	struct wmi_guid_count_context context = {
1118 		.guid = guid,
1119 		.count = 0,
1120 	};
1121 	int ret;
1122 
1123 	ret = bus_for_each_dev(&wmi_bus_type, NULL, &context, wmi_count_guids);
1124 	if (ret < 0)
1125 		return ret;
1126 
1127 	return context.count;
1128 }
1129 
1130 static int wmi_dev_set_name(struct wmi_block *wblock, int count)
1131 {
1132 	if (IS_ENABLED(CONFIG_ACPI_WMI_LEGACY_DEVICE_NAMES)) {
1133 		if (count)
1134 			return dev_set_name(&wblock->dev.dev, "%pUL-%d", &wblock->gblock.guid,
1135 					    count);
1136 		else
1137 			return dev_set_name(&wblock->dev.dev, "%pUL", &wblock->gblock.guid);
1138 	}
1139 
1140 	return dev_set_name(&wblock->dev.dev, "%pUL-%d", &wblock->gblock.guid, wblock->dev.dev.id);
1141 }
1142 
1143 static int wmi_create_device(struct device *wmi_bus_dev,
1144 			     struct wmi_block *wblock,
1145 			     struct acpi_device *device)
1146 {
1147 	char method[WMI_ACPI_METHOD_NAME_SIZE];
1148 	struct acpi_device_info *info;
1149 	acpi_handle method_handle;
1150 	acpi_status status;
1151 	int count, ret;
1152 
1153 	if (wblock->gblock.flags & ACPI_WMI_EVENT) {
1154 		wblock->dev.dev.type = &wmi_type_event;
1155 		goto out_init;
1156 	}
1157 
1158 	if (wblock->gblock.flags & ACPI_WMI_METHOD) {
1159 		get_acpi_method_name(wblock, 'M', method);
1160 		if (!acpi_has_method(device->handle, method)) {
1161 			dev_warn(wmi_bus_dev,
1162 				 FW_BUG "%s method block execution control method not found\n",
1163 				 method);
1164 
1165 			return -ENXIO;
1166 		}
1167 
1168 		wblock->dev.dev.type = &wmi_type_method;
1169 		goto out_init;
1170 	}
1171 
1172 	/*
1173 	 * Data Block Query Control Method (WQxx by convention) is
1174 	 * required per the WMI documentation. If it is not present,
1175 	 * we ignore this data block.
1176 	 */
1177 	get_acpi_method_name(wblock, 'Q', method);
1178 	status = acpi_get_handle(device->handle, method, &method_handle);
1179 	if (ACPI_FAILURE(status)) {
1180 		dev_warn(wmi_bus_dev,
1181 			 FW_BUG "%s data block query control method not found\n",
1182 			 method);
1183 
1184 		return -ENXIO;
1185 	}
1186 
1187 	status = acpi_get_object_info(method_handle, &info);
1188 	if (ACPI_FAILURE(status))
1189 		return -EIO;
1190 
1191 	wblock->dev.dev.type = &wmi_type_data;
1192 
1193 	/*
1194 	 * The Microsoft documentation specifically states:
1195 	 *
1196 	 *   Data blocks registered with only a single instance
1197 	 *   can ignore the parameter.
1198 	 *
1199 	 * ACPICA will get mad at us if we call the method with the wrong number
1200 	 * of arguments, so check what our method expects.  (On some Dell
1201 	 * laptops, WQxx may not be a method at all.)
1202 	 */
1203 	if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1204 		set_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags);
1205 
1206 	kfree(info);
1207 
1208 	get_acpi_method_name(wblock, 'S', method);
1209 	if (acpi_has_method(device->handle, method))
1210 		wblock->dev.setable = true;
1211 
1212  out_init:
1213 	init_rwsem(&wblock->notify_lock);
1214 	wblock->driver_ready = false;
1215 	wblock->dev.dev.bus = &wmi_bus_type;
1216 	wblock->dev.dev.parent = wmi_bus_dev;
1217 
1218 	count = guid_count(&wblock->gblock.guid);
1219 	if (count < 0)
1220 		return count;
1221 
1222 	if (count)
1223 		set_bit(WMI_GUID_DUPLICATED, &wblock->flags);
1224 
1225 	ret = ida_alloc(&wmi_ida, GFP_KERNEL);
1226 	if (ret < 0)
1227 		return ret;
1228 
1229 	wblock->dev.dev.id = ret;
1230 	ret = wmi_dev_set_name(wblock, count);
1231 	if (ret < 0) {
1232 		ida_free(&wmi_ida, wblock->dev.dev.id);
1233 		return ret;
1234 	}
1235 
1236 	device_initialize(&wblock->dev.dev);
1237 
1238 	return 0;
1239 }
1240 
1241 static int wmi_add_device(struct platform_device *pdev, struct wmi_device *wdev)
1242 {
1243 	struct device_link *link;
1244 
1245 	/*
1246 	 * Many aggregate WMI drivers do not use -EPROBE_DEFER when they
1247 	 * are unable to find a WMI device during probe, instead they require
1248 	 * all WMI devices associated with an platform device to become available
1249 	 * at once. This device link thus prevents WMI drivers from probing until
1250 	 * the associated platform device has finished probing (and has registered
1251 	 * all discovered WMI devices).
1252 	 */
1253 
1254 	link = device_link_add(&wdev->dev, &pdev->dev, DL_FLAG_AUTOREMOVE_SUPPLIER);
1255 	if (!link)
1256 		return -EINVAL;
1257 
1258 	return device_add(&wdev->dev);
1259 }
1260 
1261 /*
1262  * Parse the _WDG method for the GUID data blocks
1263  */
1264 static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev)
1265 {
1266 	struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
1267 	struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1268 	const struct guid_block *gblock;
1269 	bool event_data_available;
1270 	struct wmi_block *wblock;
1271 	union acpi_object *obj;
1272 	acpi_status status;
1273 	u32 i, total;
1274 	int retval;
1275 
1276 	status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1277 	if (ACPI_FAILURE(status))
1278 		return -ENXIO;
1279 
1280 	obj = out.pointer;
1281 	if (!obj)
1282 		return -ENXIO;
1283 
1284 	if (obj->type != ACPI_TYPE_BUFFER) {
1285 		kfree(obj);
1286 		return -ENXIO;
1287 	}
1288 
1289 	event_data_available = acpi_has_method(device->handle, "_WED");
1290 	gblock = (const struct guid_block *)obj->buffer.pointer;
1291 	total = obj->buffer.length / sizeof(struct guid_block);
1292 
1293 	for (i = 0; i < total; i++) {
1294 		if (!gblock[i].instance_count) {
1295 			dev_info(wmi_bus_dev, FW_INFO "%pUL has zero instances\n", &gblock[i].guid);
1296 			continue;
1297 		}
1298 
1299 		wblock = kzalloc(sizeof(*wblock), GFP_KERNEL);
1300 		if (!wblock)
1301 			continue;
1302 
1303 		wblock->acpi_device = device;
1304 		wblock->gblock = gblock[i];
1305 		if (gblock[i].flags & ACPI_WMI_EVENT && !event_data_available)
1306 			set_bit(WMI_NO_EVENT_DATA, &wblock->flags);
1307 
1308 		retval = wmi_create_device(wmi_bus_dev, wblock, device);
1309 		if (retval) {
1310 			kfree(wblock);
1311 			continue;
1312 		}
1313 
1314 		retval = wmi_add_device(pdev, &wblock->dev);
1315 		if (retval) {
1316 			dev_err(wmi_bus_dev, "failed to register %pUL\n",
1317 				&wblock->gblock.guid);
1318 
1319 			ida_free(&wmi_ida, wblock->dev.dev.id);
1320 			put_device(&wblock->dev.dev);
1321 		}
1322 	}
1323 
1324 	kfree(obj);
1325 
1326 	return 0;
1327 }
1328 
1329 static int wmi_get_notify_data(struct wmi_block *wblock, union acpi_object **obj)
1330 {
1331 	struct acpi_buffer data = { ACPI_ALLOCATE_BUFFER, NULL };
1332 	union acpi_object param = {
1333 		.integer = {
1334 			.type = ACPI_TYPE_INTEGER,
1335 			.value = wblock->gblock.notify_id,
1336 		}
1337 	};
1338 	struct acpi_object_list input = {
1339 		.count = 1,
1340 		.pointer = &param,
1341 	};
1342 	acpi_status status;
1343 
1344 	status = acpi_evaluate_object(wblock->acpi_device->handle, "_WED", &input, &data);
1345 	if (ACPI_FAILURE(status)) {
1346 		dev_warn(&wblock->dev.dev, "Failed to get event data\n");
1347 		return -EIO;
1348 	}
1349 
1350 	*obj = data.pointer;
1351 
1352 	return 0;
1353 }
1354 
1355 static void wmi_notify_driver(struct wmi_block *wblock, union acpi_object *obj)
1356 {
1357 	struct wmi_driver *driver = to_wmi_driver(wblock->dev.dev.driver);
1358 	struct wmi_buffer buffer;
1359 	int ret;
1360 
1361 	if (!obj && !driver->no_notify_data) {
1362 		dev_warn(&wblock->dev.dev, "Event contains no event data\n");
1363 		return;
1364 	}
1365 
1366 	if (driver->notify)
1367 		driver->notify(&wblock->dev, obj);
1368 
1369 	if (driver->notify_new) {
1370 		if (!obj) {
1371 			driver->notify_new(&wblock->dev, NULL);
1372 			return;
1373 		}
1374 
1375 		ret = wmi_unmarshal_acpi_object(obj, &buffer);
1376 		if (ret < 0) {
1377 			dev_warn(&wblock->dev.dev, "Failed to unmarshal event data: %d\n", ret);
1378 			return;
1379 		}
1380 
1381 		driver->notify_new(&wblock->dev, &buffer);
1382 		kfree(buffer.data);
1383 	}
1384 }
1385 
1386 static int wmi_notify_device(struct device *dev, void *data)
1387 {
1388 	struct wmi_block *wblock = dev_to_wblock(dev);
1389 	union acpi_object *obj = NULL;
1390 	u32 *event = data;
1391 	int ret;
1392 
1393 	if (!(wblock->gblock.flags & ACPI_WMI_EVENT && wblock->gblock.notify_id == *event))
1394 		return 0;
1395 
1396 	/* The ACPI WMI specification says that _WED should be
1397 	 * evaluated every time an notification is received, even
1398 	 * if no consumers are present.
1399 	 *
1400 	 * Some firmware implementations actually depend on this
1401 	 * by using a queue for events which will fill up if the
1402 	 * WMI driver core stops evaluating _WED due to missing
1403 	 * WMI event consumers.
1404 	 */
1405 	if (!test_bit(WMI_NO_EVENT_DATA, &wblock->flags)) {
1406 		ret = wmi_get_notify_data(wblock, &obj);
1407 		if (ret < 0)
1408 			return -EIO;
1409 	}
1410 
1411 	down_read(&wblock->notify_lock);
1412 
1413 	if (wblock->dev.dev.driver && wblock->driver_ready)
1414 		wmi_notify_driver(wblock, obj);
1415 
1416 	if (wblock->handler)
1417 		wblock->handler(obj, wblock->handler_data);
1418 
1419 	up_read(&wblock->notify_lock);
1420 
1421 	kfree(obj);
1422 
1423 	acpi_bus_generate_netlink_event("wmi", acpi_dev_name(wblock->acpi_device), *event, 0);
1424 
1425 	return -EBUSY;
1426 }
1427 
1428 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event, void *context)
1429 {
1430 	struct device *wmi_bus_dev = context;
1431 
1432 	device_for_each_child(wmi_bus_dev, &event, wmi_notify_device);
1433 }
1434 
1435 static int wmi_remove_device(struct device *dev, void *data)
1436 {
1437 	int id = dev->id;
1438 
1439 	device_unregister(dev);
1440 	ida_free(&wmi_ida, id);
1441 
1442 	return 0;
1443 }
1444 
1445 static void acpi_wmi_remove(struct platform_device *device)
1446 {
1447 	struct device *wmi_bus_device = dev_get_drvdata(&device->dev);
1448 
1449 	device_for_each_child_reverse(wmi_bus_device, NULL, wmi_remove_device);
1450 }
1451 
1452 static void acpi_wmi_remove_notify_handler(void *data)
1453 {
1454 	struct acpi_device *acpi_device = data;
1455 
1456 	acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY, acpi_wmi_notify_handler);
1457 }
1458 
1459 static void acpi_wmi_remove_bus_device(void *data)
1460 {
1461 	struct device *wmi_bus_dev = data;
1462 
1463 	device_unregister(wmi_bus_dev);
1464 }
1465 
1466 static int acpi_wmi_probe(struct platform_device *device)
1467 {
1468 	struct acpi_device *acpi_device;
1469 	struct device *wmi_bus_dev;
1470 	acpi_status status;
1471 	int error;
1472 
1473 	acpi_device = ACPI_COMPANION(&device->dev);
1474 	if (!acpi_device) {
1475 		dev_err(&device->dev, "ACPI companion is missing\n");
1476 		return -ENODEV;
1477 	}
1478 
1479 	wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0), NULL, "wmi_bus-%s",
1480 				    dev_name(&device->dev));
1481 	if (IS_ERR(wmi_bus_dev))
1482 		return PTR_ERR(wmi_bus_dev);
1483 
1484 	error = devm_add_action_or_reset(&device->dev, acpi_wmi_remove_bus_device, wmi_bus_dev);
1485 	if (error < 0)
1486 		return error;
1487 
1488 	dev_set_drvdata(&device->dev, wmi_bus_dev);
1489 
1490 	status = acpi_install_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
1491 					     acpi_wmi_notify_handler, wmi_bus_dev);
1492 	if (ACPI_FAILURE(status)) {
1493 		dev_err(&device->dev, "Error installing notify handler\n");
1494 		return -ENODEV;
1495 	}
1496 	error = devm_add_action_or_reset(&device->dev, acpi_wmi_remove_notify_handler,
1497 					 acpi_device);
1498 	if (error < 0)
1499 		return error;
1500 
1501 	error = parse_wdg(wmi_bus_dev, device);
1502 	if (error) {
1503 		dev_err(&device->dev, "Failed to parse _WDG method\n");
1504 		return error;
1505 	}
1506 
1507 	return 0;
1508 }
1509 
1510 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1511 				       struct module *owner)
1512 {
1513 	driver->driver.owner = owner;
1514 	driver->driver.bus = &wmi_bus_type;
1515 
1516 	return driver_register(&driver->driver);
1517 }
1518 EXPORT_SYMBOL(__wmi_driver_register);
1519 
1520 /**
1521  * wmi_driver_unregister() - Unregister a WMI driver
1522  * @driver: WMI driver to unregister
1523  *
1524  * Unregisters a WMI driver from the WMI bus.
1525  */
1526 void wmi_driver_unregister(struct wmi_driver *driver)
1527 {
1528 	driver_unregister(&driver->driver);
1529 }
1530 EXPORT_SYMBOL(wmi_driver_unregister);
1531 
1532 static struct platform_driver acpi_wmi_driver = {
1533 	.driver = {
1534 		.name = "acpi-wmi",
1535 		.acpi_match_table = wmi_device_ids,
1536 	},
1537 	.probe = acpi_wmi_probe,
1538 	.remove = acpi_wmi_remove,
1539 };
1540 
1541 static int __init acpi_wmi_init(void)
1542 {
1543 	int error;
1544 
1545 	if (acpi_disabled)
1546 		return -ENODEV;
1547 
1548 	error = class_register(&wmi_bus_class);
1549 	if (error)
1550 		return error;
1551 
1552 	error = bus_register(&wmi_bus_type);
1553 	if (error)
1554 		goto err_unreg_class;
1555 
1556 	error = platform_driver_register(&acpi_wmi_driver);
1557 	if (error) {
1558 		pr_err("Error loading mapper\n");
1559 		goto err_unreg_bus;
1560 	}
1561 
1562 	return 0;
1563 
1564 err_unreg_bus:
1565 	bus_unregister(&wmi_bus_type);
1566 
1567 err_unreg_class:
1568 	class_unregister(&wmi_bus_class);
1569 
1570 	return error;
1571 }
1572 
1573 static void __exit acpi_wmi_exit(void)
1574 {
1575 	platform_driver_unregister(&acpi_wmi_driver);
1576 	bus_unregister(&wmi_bus_type);
1577 	class_unregister(&wmi_bus_class);
1578 }
1579 
1580 subsys_initcall_sync(acpi_wmi_init);
1581 module_exit(acpi_wmi_exit);
1582