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