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