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