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/list.h>
26 #include <linux/miscdevice.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
30 #include <linux/sysfs.h>
31 #include <linux/types.h>
32 #include <linux/uaccess.h>
33 #include <linux/uuid.h>
34 #include <linux/wmi.h>
35 #include <linux/fs.h>
36 #include <uapi/linux/wmi.h>
37 
38 MODULE_AUTHOR("Carlos Corbacho");
39 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
40 MODULE_LICENSE("GPL");
41 
42 static LIST_HEAD(wmi_block_list);
43 
44 struct guid_block {
45 	guid_t guid;
46 	union {
47 		char object_id[2];
48 		struct {
49 			unsigned char notify_id;
50 			unsigned char reserved;
51 		};
52 	};
53 	u8 instance_count;
54 	u8 flags;
55 } __packed;
56 static_assert(sizeof(typeof_member(struct guid_block, guid)) == 16);
57 static_assert(sizeof(struct guid_block) == 20);
58 static_assert(__alignof__(struct guid_block) == 1);
59 
60 enum {	/* wmi_block flags */
61 	WMI_READ_TAKES_NO_ARGS,
62 	WMI_PROBED,
63 };
64 
65 struct wmi_block {
66 	struct wmi_device dev;
67 	struct list_head list;
68 	struct guid_block gblock;
69 	struct miscdevice char_dev;
70 	struct mutex char_mutex;
71 	struct acpi_device *acpi_device;
72 	wmi_notify_handler handler;
73 	void *handler_data;
74 	u64 req_buf_size;
75 	unsigned long flags;
76 };
77 
78 
79 /*
80  * If the GUID data block is marked as expensive, we must enable and
81  * explicitily disable data collection.
82  */
83 #define ACPI_WMI_EXPENSIVE   BIT(0)
84 #define ACPI_WMI_METHOD      BIT(1)	/* GUID is a method */
85 #define ACPI_WMI_STRING      BIT(2)	/* GUID takes & returns a string */
86 #define ACPI_WMI_EVENT       BIT(3)	/* GUID is an event */
87 
88 static bool debug_event;
89 module_param(debug_event, bool, 0444);
90 MODULE_PARM_DESC(debug_event,
91 		 "Log WMI Events [0/1]");
92 
93 static bool debug_dump_wdg;
94 module_param(debug_dump_wdg, bool, 0444);
95 MODULE_PARM_DESC(debug_dump_wdg,
96 		 "Dump available WMI interfaces [0/1]");
97 
98 static const struct acpi_device_id wmi_device_ids[] = {
99 	{"PNP0C14", 0},
100 	{"pnp0c14", 0},
101 	{ }
102 };
103 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
104 
105 /* allow duplicate GUIDs as these device drivers use struct wmi_driver */
106 static const char * const allow_duplicates[] = {
107 	"05901221-D566-11D1-B2F0-00A0C9062910",	/* wmi-bmof */
108 	NULL
109 };
110 
111 /*
112  * GUID parsing functions
113  */
114 
find_guid(const char * guid_string,struct wmi_block ** out)115 static acpi_status find_guid(const char *guid_string, struct wmi_block **out)
116 {
117 	guid_t guid_input;
118 	struct wmi_block *wblock;
119 
120 	if (!guid_string)
121 		return AE_BAD_PARAMETER;
122 
123 	if (guid_parse(guid_string, &guid_input))
124 		return AE_BAD_PARAMETER;
125 
126 	list_for_each_entry(wblock, &wmi_block_list, list) {
127 		if (guid_equal(&wblock->gblock.guid, &guid_input)) {
128 			if (out)
129 				*out = wblock;
130 
131 			return AE_OK;
132 		}
133 	}
134 
135 	return AE_NOT_FOUND;
136 }
137 
find_guid_context(struct wmi_block * wblock,struct wmi_driver * wdriver)138 static const void *find_guid_context(struct wmi_block *wblock,
139 				     struct wmi_driver *wdriver)
140 {
141 	const struct wmi_device_id *id;
142 
143 	id = wdriver->id_table;
144 	if (!id)
145 		return NULL;
146 
147 	while (*id->guid_string) {
148 		guid_t guid_input;
149 
150 		if (guid_parse(id->guid_string, &guid_input))
151 			continue;
152 		if (guid_equal(&wblock->gblock.guid, &guid_input))
153 			return id->context;
154 		id++;
155 	}
156 	return NULL;
157 }
158 
get_subobj_info(acpi_handle handle,const char * pathname,struct acpi_device_info ** info)159 static int get_subobj_info(acpi_handle handle, const char *pathname,
160 			   struct acpi_device_info **info)
161 {
162 	struct acpi_device_info *dummy_info, **info_ptr;
163 	acpi_handle subobj_handle;
164 	acpi_status status;
165 
166 	status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
167 	if (status == AE_NOT_FOUND)
168 		return -ENOENT;
169 	else if (ACPI_FAILURE(status))
170 		return -EIO;
171 
172 	info_ptr = info ? info : &dummy_info;
173 	status = acpi_get_object_info(subobj_handle, info_ptr);
174 	if (ACPI_FAILURE(status))
175 		return -EIO;
176 
177 	if (!info)
178 		kfree(dummy_info);
179 
180 	return 0;
181 }
182 
wmi_method_enable(struct wmi_block * wblock,bool enable)183 static acpi_status wmi_method_enable(struct wmi_block *wblock, bool enable)
184 {
185 	struct guid_block *block;
186 	char method[5];
187 	acpi_status status;
188 	acpi_handle handle;
189 
190 	block = &wblock->gblock;
191 	handle = wblock->acpi_device->handle;
192 
193 	snprintf(method, 5, "WE%02X", block->notify_id);
194 	status = acpi_execute_simple_method(handle, method, enable);
195 	if (status == AE_NOT_FOUND)
196 		return AE_OK;
197 
198 	return status;
199 }
200 
201 #define WMI_ACPI_METHOD_NAME_SIZE 5
202 
get_acpi_method_name(const struct wmi_block * wblock,const char method,char buffer[static WMI_ACPI_METHOD_NAME_SIZE])203 static inline void get_acpi_method_name(const struct wmi_block *wblock,
204 					const char method,
205 					char buffer[static WMI_ACPI_METHOD_NAME_SIZE])
206 {
207 	static_assert(ARRAY_SIZE(wblock->gblock.object_id) == 2);
208 	static_assert(WMI_ACPI_METHOD_NAME_SIZE >= 5);
209 
210 	buffer[0] = 'W';
211 	buffer[1] = method;
212 	buffer[2] = wblock->gblock.object_id[0];
213 	buffer[3] = wblock->gblock.object_id[1];
214 	buffer[4] = '\0';
215 }
216 
get_param_acpi_type(const struct wmi_block * wblock)217 static inline acpi_object_type get_param_acpi_type(const struct wmi_block *wblock)
218 {
219 	if (wblock->gblock.flags & ACPI_WMI_STRING)
220 		return ACPI_TYPE_STRING;
221 	else
222 		return ACPI_TYPE_BUFFER;
223 }
224 
get_event_data(const struct wmi_block * wblock,struct acpi_buffer * out)225 static acpi_status get_event_data(const struct wmi_block *wblock, struct acpi_buffer *out)
226 {
227 	union acpi_object param = {
228 		.integer = {
229 			.type = ACPI_TYPE_INTEGER,
230 			.value = wblock->gblock.notify_id,
231 		}
232 	};
233 	struct acpi_object_list input = {
234 		.count = 1,
235 		.pointer = &param,
236 	};
237 
238 	return acpi_evaluate_object(wblock->acpi_device->handle, "_WED", &input, out);
239 }
240 
241 /*
242  * Exported WMI functions
243  */
244 
245 /**
246  * set_required_buffer_size - Sets the buffer size needed for performing IOCTL
247  * @wdev: A wmi bus device from a driver
248  * @length: Required buffer size
249  *
250  * Allocates memory needed for buffer, stores the buffer size in that memory
251  */
set_required_buffer_size(struct wmi_device * wdev,u64 length)252 int set_required_buffer_size(struct wmi_device *wdev, u64 length)
253 {
254 	struct wmi_block *wblock;
255 
256 	wblock = container_of(wdev, struct wmi_block, dev);
257 	wblock->req_buf_size = length;
258 
259 	return 0;
260 }
261 EXPORT_SYMBOL_GPL(set_required_buffer_size);
262 
263 /**
264  * wmi_evaluate_method - Evaluate a WMI method
265  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
266  * @instance: Instance index
267  * @method_id: Method ID to call
268  * @in: Buffer containing input for the method call
269  * @out: Empty buffer to return the method results
270  *
271  * Call an ACPI-WMI method
272  */
wmi_evaluate_method(const char * guid_string,u8 instance,u32 method_id,const struct acpi_buffer * in,struct acpi_buffer * out)273 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance, u32 method_id,
274 				const struct acpi_buffer *in, struct acpi_buffer *out)
275 {
276 	struct wmi_block *wblock = NULL;
277 	acpi_status status;
278 
279 	status = find_guid(guid_string, &wblock);
280 	if (ACPI_FAILURE(status))
281 		return status;
282 
283 	return wmidev_evaluate_method(&wblock->dev, instance, method_id,
284 				      in, out);
285 }
286 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
287 
288 /**
289  * wmidev_evaluate_method - Evaluate a WMI method
290  * @wdev: A wmi bus device from a driver
291  * @instance: Instance index
292  * @method_id: Method ID to call
293  * @in: Buffer containing input for the method call
294  * @out: Empty buffer to return the method results
295  *
296  * Call an ACPI-WMI method
297  */
wmidev_evaluate_method(struct wmi_device * wdev,u8 instance,u32 method_id,const struct acpi_buffer * in,struct acpi_buffer * out)298 acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance, u32 method_id,
299 				   const struct acpi_buffer *in, struct acpi_buffer *out)
300 {
301 	struct guid_block *block;
302 	struct wmi_block *wblock;
303 	acpi_handle handle;
304 	struct acpi_object_list input;
305 	union acpi_object params[3];
306 	char method[WMI_ACPI_METHOD_NAME_SIZE];
307 
308 	wblock = container_of(wdev, struct wmi_block, dev);
309 	block = &wblock->gblock;
310 	handle = wblock->acpi_device->handle;
311 
312 	if (!(block->flags & ACPI_WMI_METHOD))
313 		return AE_BAD_DATA;
314 
315 	if (block->instance_count <= instance)
316 		return AE_BAD_PARAMETER;
317 
318 	input.count = 2;
319 	input.pointer = params;
320 	params[0].type = ACPI_TYPE_INTEGER;
321 	params[0].integer.value = instance;
322 	params[1].type = ACPI_TYPE_INTEGER;
323 	params[1].integer.value = method_id;
324 
325 	if (in) {
326 		input.count = 3;
327 
328 		params[2].type = get_param_acpi_type(wblock);
329 		params[2].buffer.length = in->length;
330 		params[2].buffer.pointer = in->pointer;
331 	}
332 
333 	get_acpi_method_name(wblock, 'M', method);
334 
335 	return acpi_evaluate_object(handle, method, &input, out);
336 }
337 EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
338 
__query_block(struct wmi_block * wblock,u8 instance,struct acpi_buffer * out)339 static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
340 				 struct acpi_buffer *out)
341 {
342 	struct guid_block *block;
343 	acpi_handle handle;
344 	acpi_status status, wc_status = AE_ERROR;
345 	struct acpi_object_list input;
346 	union acpi_object wq_params[1];
347 	char wc_method[WMI_ACPI_METHOD_NAME_SIZE];
348 	char method[WMI_ACPI_METHOD_NAME_SIZE];
349 
350 	if (!out)
351 		return AE_BAD_PARAMETER;
352 
353 	block = &wblock->gblock;
354 	handle = wblock->acpi_device->handle;
355 
356 	if (block->instance_count <= instance)
357 		return AE_BAD_PARAMETER;
358 
359 	/* Check GUID is a data block */
360 	if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
361 		return AE_ERROR;
362 
363 	input.count = 1;
364 	input.pointer = wq_params;
365 	wq_params[0].type = ACPI_TYPE_INTEGER;
366 	wq_params[0].integer.value = instance;
367 
368 	if (instance == 0 && test_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags))
369 		input.count = 0;
370 
371 	/*
372 	 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
373 	 * enable collection.
374 	 */
375 	if (block->flags & ACPI_WMI_EXPENSIVE) {
376 		get_acpi_method_name(wblock, 'C', wc_method);
377 
378 		/*
379 		 * Some GUIDs break the specification by declaring themselves
380 		 * expensive, but have no corresponding WCxx method. So we
381 		 * should not fail if this happens.
382 		 */
383 		wc_status = acpi_execute_simple_method(handle, wc_method, 1);
384 	}
385 
386 	get_acpi_method_name(wblock, 'Q', method);
387 	status = acpi_evaluate_object(handle, method, &input, out);
388 
389 	/*
390 	 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
391 	 * the WQxx method failed - we should disable collection anyway.
392 	 */
393 	if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
394 		/*
395 		 * Ignore whether this WCxx call succeeds or not since
396 		 * the previously executed WQxx method call might have
397 		 * succeeded, and returning the failing status code
398 		 * of this call would throw away the result of the WQxx
399 		 * call, potentially leaking memory.
400 		 */
401 		acpi_execute_simple_method(handle, wc_method, 0);
402 	}
403 
404 	return status;
405 }
406 
407 /**
408  * wmi_query_block - Return contents of a WMI block (deprecated)
409  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
410  * @instance: Instance index
411  * @out: Empty buffer to return the contents of the data block to
412  *
413  * Return the contents of an ACPI-WMI data block to a buffer
414  */
wmi_query_block(const char * guid_string,u8 instance,struct acpi_buffer * out)415 acpi_status wmi_query_block(const char *guid_string, u8 instance,
416 			    struct acpi_buffer *out)
417 {
418 	struct wmi_block *wblock;
419 	acpi_status status;
420 
421 	status = find_guid(guid_string, &wblock);
422 	if (ACPI_FAILURE(status))
423 		return status;
424 
425 	return __query_block(wblock, instance, out);
426 }
427 EXPORT_SYMBOL_GPL(wmi_query_block);
428 
wmidev_block_query(struct wmi_device * wdev,u8 instance)429 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
430 {
431 	struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
432 	struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
433 
434 	if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
435 		return NULL;
436 
437 	return out.pointer;
438 }
439 EXPORT_SYMBOL_GPL(wmidev_block_query);
440 
441 /**
442  * wmi_set_block - Write to a WMI block
443  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
444  * @instance: Instance index
445  * @in: Buffer containing new values for the data block
446  *
447  * Write the contents of the input buffer to an ACPI-WMI data block
448  */
wmi_set_block(const char * guid_string,u8 instance,const struct acpi_buffer * in)449 acpi_status wmi_set_block(const char *guid_string, u8 instance,
450 			  const struct acpi_buffer *in)
451 {
452 	struct wmi_block *wblock = NULL;
453 	struct guid_block *block;
454 	acpi_handle handle;
455 	struct acpi_object_list input;
456 	union acpi_object params[2];
457 	char method[WMI_ACPI_METHOD_NAME_SIZE];
458 	acpi_status status;
459 
460 	if (!in)
461 		return AE_BAD_DATA;
462 
463 	status = find_guid(guid_string, &wblock);
464 	if (ACPI_FAILURE(status))
465 		return status;
466 
467 	block = &wblock->gblock;
468 	handle = wblock->acpi_device->handle;
469 
470 	if (block->instance_count <= instance)
471 		return AE_BAD_PARAMETER;
472 
473 	/* Check GUID is a data block */
474 	if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
475 		return AE_ERROR;
476 
477 	input.count = 2;
478 	input.pointer = params;
479 	params[0].type = ACPI_TYPE_INTEGER;
480 	params[0].integer.value = instance;
481 	params[1].type = get_param_acpi_type(wblock);
482 	params[1].buffer.length = in->length;
483 	params[1].buffer.pointer = in->pointer;
484 
485 	get_acpi_method_name(wblock, 'S', method);
486 
487 	return acpi_evaluate_object(handle, method, &input, NULL);
488 }
489 EXPORT_SYMBOL_GPL(wmi_set_block);
490 
wmi_dump_wdg(const struct guid_block * g)491 static void wmi_dump_wdg(const struct guid_block *g)
492 {
493 	pr_info("%pUL:\n", &g->guid);
494 	if (g->flags & ACPI_WMI_EVENT)
495 		pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
496 	else
497 		pr_info("\tobject_id: %2pE\n", g->object_id);
498 	pr_info("\tinstance_count: %d\n", g->instance_count);
499 	pr_info("\tflags: %#x", g->flags);
500 	if (g->flags) {
501 		if (g->flags & ACPI_WMI_EXPENSIVE)
502 			pr_cont(" ACPI_WMI_EXPENSIVE");
503 		if (g->flags & ACPI_WMI_METHOD)
504 			pr_cont(" ACPI_WMI_METHOD");
505 		if (g->flags & ACPI_WMI_STRING)
506 			pr_cont(" ACPI_WMI_STRING");
507 		if (g->flags & ACPI_WMI_EVENT)
508 			pr_cont(" ACPI_WMI_EVENT");
509 	}
510 	pr_cont("\n");
511 
512 }
513 
wmi_notify_debug(u32 value,void * context)514 static void wmi_notify_debug(u32 value, void *context)
515 {
516 	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
517 	union acpi_object *obj;
518 	acpi_status status;
519 
520 	status = wmi_get_event_data(value, &response);
521 	if (status != AE_OK) {
522 		pr_info("bad event status 0x%x\n", status);
523 		return;
524 	}
525 
526 	obj = response.pointer;
527 	if (!obj)
528 		return;
529 
530 	pr_info("DEBUG: event 0x%02X ", value);
531 	switch (obj->type) {
532 	case ACPI_TYPE_BUFFER:
533 		pr_cont("BUFFER_TYPE - length %u\n", obj->buffer.length);
534 		break;
535 	case ACPI_TYPE_STRING:
536 		pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
537 		break;
538 	case ACPI_TYPE_INTEGER:
539 		pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
540 		break;
541 	case ACPI_TYPE_PACKAGE:
542 		pr_cont("PACKAGE_TYPE - %u elements\n", obj->package.count);
543 		break;
544 	default:
545 		pr_cont("object type 0x%X\n", obj->type);
546 	}
547 	kfree(obj);
548 }
549 
550 /**
551  * wmi_install_notify_handler - Register handler for WMI events
552  * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
553  * @handler: Function to handle notifications
554  * @data: Data to be returned to handler when event is fired
555  *
556  * Register a handler for events sent to the ACPI-WMI mapper device.
557  */
wmi_install_notify_handler(const char * guid,wmi_notify_handler handler,void * data)558 acpi_status wmi_install_notify_handler(const char *guid,
559 				       wmi_notify_handler handler,
560 				       void *data)
561 {
562 	struct wmi_block *block;
563 	acpi_status status = AE_NOT_EXIST;
564 	guid_t guid_input;
565 
566 	if (!guid || !handler)
567 		return AE_BAD_PARAMETER;
568 
569 	if (guid_parse(guid, &guid_input))
570 		return AE_BAD_PARAMETER;
571 
572 	list_for_each_entry(block, &wmi_block_list, list) {
573 		acpi_status wmi_status;
574 
575 		if (guid_equal(&block->gblock.guid, &guid_input)) {
576 			if (block->handler &&
577 			    block->handler != wmi_notify_debug)
578 				return AE_ALREADY_ACQUIRED;
579 
580 			block->handler = handler;
581 			block->handler_data = data;
582 
583 			wmi_status = wmi_method_enable(block, true);
584 			if ((wmi_status != AE_OK) ||
585 			    ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
586 				status = wmi_status;
587 		}
588 	}
589 
590 	return status;
591 }
592 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
593 
594 /**
595  * wmi_remove_notify_handler - Unregister handler for WMI events
596  * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
597  *
598  * Unregister handler for events sent to the ACPI-WMI mapper device.
599  */
wmi_remove_notify_handler(const char * guid)600 acpi_status wmi_remove_notify_handler(const char *guid)
601 {
602 	struct wmi_block *block;
603 	acpi_status status = AE_NOT_EXIST;
604 	guid_t guid_input;
605 
606 	if (!guid)
607 		return AE_BAD_PARAMETER;
608 
609 	if (guid_parse(guid, &guid_input))
610 		return AE_BAD_PARAMETER;
611 
612 	list_for_each_entry(block, &wmi_block_list, list) {
613 		acpi_status wmi_status;
614 
615 		if (guid_equal(&block->gblock.guid, &guid_input)) {
616 			if (!block->handler ||
617 			    block->handler == wmi_notify_debug)
618 				return AE_NULL_ENTRY;
619 
620 			if (debug_event) {
621 				block->handler = wmi_notify_debug;
622 				status = AE_OK;
623 			} else {
624 				wmi_status = wmi_method_enable(block, false);
625 				block->handler = NULL;
626 				block->handler_data = NULL;
627 				if ((wmi_status != AE_OK) ||
628 				    ((wmi_status == AE_OK) &&
629 				     (status == AE_NOT_EXIST)))
630 					status = wmi_status;
631 			}
632 		}
633 	}
634 
635 	return status;
636 }
637 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
638 
639 /**
640  * wmi_get_event_data - Get WMI data associated with an event
641  *
642  * @event: Event to find
643  * @out: Buffer to hold event data. out->pointer should be freed with kfree()
644  *
645  * Returns extra data associated with an event in WMI.
646  */
wmi_get_event_data(u32 event,struct acpi_buffer * out)647 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
648 {
649 	struct wmi_block *wblock;
650 
651 	list_for_each_entry(wblock, &wmi_block_list, list) {
652 		struct guid_block *gblock = &wblock->gblock;
653 
654 		if ((gblock->flags & ACPI_WMI_EVENT) && gblock->notify_id == event)
655 			return get_event_data(wblock, out);
656 	}
657 
658 	return AE_NOT_FOUND;
659 }
660 EXPORT_SYMBOL_GPL(wmi_get_event_data);
661 
662 /**
663  * wmi_has_guid - Check if a GUID is available
664  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
665  *
666  * Check if a given GUID is defined by _WDG
667  */
wmi_has_guid(const char * guid_string)668 bool wmi_has_guid(const char *guid_string)
669 {
670 	return ACPI_SUCCESS(find_guid(guid_string, NULL));
671 }
672 EXPORT_SYMBOL_GPL(wmi_has_guid);
673 
674 /**
675  * wmi_get_acpi_device_uid() - Get _UID name of ACPI device that defines GUID
676  * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
677  *
678  * Find the _UID of ACPI device associated with this WMI GUID.
679  *
680  * Return: The ACPI _UID field value or NULL if the WMI GUID was not found
681  */
wmi_get_acpi_device_uid(const char * guid_string)682 char *wmi_get_acpi_device_uid(const char *guid_string)
683 {
684 	struct wmi_block *wblock = NULL;
685 	acpi_status status;
686 
687 	status = find_guid(guid_string, &wblock);
688 	if (ACPI_FAILURE(status))
689 		return NULL;
690 
691 	return acpi_device_uid(wblock->acpi_device);
692 }
693 EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid);
694 
dev_to_wblock(struct device * dev)695 static struct wmi_block *dev_to_wblock(struct device *dev)
696 {
697 	return container_of(dev, struct wmi_block, dev.dev);
698 }
699 
dev_to_wdev(struct device * dev)700 static struct wmi_device *dev_to_wdev(struct device *dev)
701 {
702 	return container_of(dev, struct wmi_device, dev);
703 }
704 
drv_to_wdrv(struct device_driver * drv)705 static inline struct wmi_driver *drv_to_wdrv(struct device_driver *drv)
706 {
707 	return container_of(drv, struct wmi_driver, driver);
708 }
709 
710 /*
711  * sysfs interface
712  */
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)713 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
714 			     char *buf)
715 {
716 	struct wmi_block *wblock = dev_to_wblock(dev);
717 
718 	return sysfs_emit(buf, "wmi:%pUL\n", &wblock->gblock.guid);
719 }
720 static DEVICE_ATTR_RO(modalias);
721 
guid_show(struct device * dev,struct device_attribute * attr,char * buf)722 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
723 			 char *buf)
724 {
725 	struct wmi_block *wblock = dev_to_wblock(dev);
726 
727 	return sysfs_emit(buf, "%pUL\n", &wblock->gblock.guid);
728 }
729 static DEVICE_ATTR_RO(guid);
730 
instance_count_show(struct device * dev,struct device_attribute * attr,char * buf)731 static ssize_t instance_count_show(struct device *dev,
732 				   struct device_attribute *attr, char *buf)
733 {
734 	struct wmi_block *wblock = dev_to_wblock(dev);
735 
736 	return sysfs_emit(buf, "%d\n", (int)wblock->gblock.instance_count);
737 }
738 static DEVICE_ATTR_RO(instance_count);
739 
expensive_show(struct device * dev,struct device_attribute * attr,char * buf)740 static ssize_t expensive_show(struct device *dev,
741 			      struct device_attribute *attr, char *buf)
742 {
743 	struct wmi_block *wblock = dev_to_wblock(dev);
744 
745 	return sysfs_emit(buf, "%d\n",
746 			  (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
747 }
748 static DEVICE_ATTR_RO(expensive);
749 
750 static struct attribute *wmi_attrs[] = {
751 	&dev_attr_modalias.attr,
752 	&dev_attr_guid.attr,
753 	&dev_attr_instance_count.attr,
754 	&dev_attr_expensive.attr,
755 	NULL
756 };
757 ATTRIBUTE_GROUPS(wmi);
758 
notify_id_show(struct device * dev,struct device_attribute * attr,char * buf)759 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
760 			      char *buf)
761 {
762 	struct wmi_block *wblock = dev_to_wblock(dev);
763 
764 	return sysfs_emit(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
765 }
766 static DEVICE_ATTR_RO(notify_id);
767 
768 static struct attribute *wmi_event_attrs[] = {
769 	&dev_attr_notify_id.attr,
770 	NULL
771 };
772 ATTRIBUTE_GROUPS(wmi_event);
773 
object_id_show(struct device * dev,struct device_attribute * attr,char * buf)774 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
775 			      char *buf)
776 {
777 	struct wmi_block *wblock = dev_to_wblock(dev);
778 
779 	return sysfs_emit(buf, "%c%c\n", wblock->gblock.object_id[0],
780 			  wblock->gblock.object_id[1]);
781 }
782 static DEVICE_ATTR_RO(object_id);
783 
setable_show(struct device * dev,struct device_attribute * attr,char * buf)784 static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
785 			    char *buf)
786 {
787 	struct wmi_device *wdev = dev_to_wdev(dev);
788 
789 	return sysfs_emit(buf, "%d\n", (int)wdev->setable);
790 }
791 static DEVICE_ATTR_RO(setable);
792 
793 static struct attribute *wmi_data_attrs[] = {
794 	&dev_attr_object_id.attr,
795 	&dev_attr_setable.attr,
796 	NULL
797 };
798 ATTRIBUTE_GROUPS(wmi_data);
799 
800 static struct attribute *wmi_method_attrs[] = {
801 	&dev_attr_object_id.attr,
802 	NULL
803 };
804 ATTRIBUTE_GROUPS(wmi_method);
805 
wmi_dev_uevent(struct device * dev,struct kobj_uevent_env * env)806 static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
807 {
808 	struct wmi_block *wblock = dev_to_wblock(dev);
809 
810 	if (add_uevent_var(env, "MODALIAS=wmi:%pUL", &wblock->gblock.guid))
811 		return -ENOMEM;
812 
813 	if (add_uevent_var(env, "WMI_GUID=%pUL", &wblock->gblock.guid))
814 		return -ENOMEM;
815 
816 	return 0;
817 }
818 
wmi_dev_release(struct device * dev)819 static void wmi_dev_release(struct device *dev)
820 {
821 	struct wmi_block *wblock = dev_to_wblock(dev);
822 
823 	kfree(wblock);
824 }
825 
wmi_dev_match(struct device * dev,struct device_driver * driver)826 static int wmi_dev_match(struct device *dev, struct device_driver *driver)
827 {
828 	struct wmi_driver *wmi_driver = drv_to_wdrv(driver);
829 	struct wmi_block *wblock = dev_to_wblock(dev);
830 	const struct wmi_device_id *id = wmi_driver->id_table;
831 
832 	if (id == NULL)
833 		return 0;
834 
835 	while (*id->guid_string) {
836 		guid_t driver_guid;
837 
838 		if (WARN_ON(guid_parse(id->guid_string, &driver_guid)))
839 			continue;
840 		if (guid_equal(&driver_guid, &wblock->gblock.guid))
841 			return 1;
842 
843 		id++;
844 	}
845 
846 	return 0;
847 }
wmi_char_open(struct inode * inode,struct file * filp)848 static int wmi_char_open(struct inode *inode, struct file *filp)
849 {
850 	const char *driver_name = filp->f_path.dentry->d_iname;
851 	struct wmi_block *wblock;
852 	struct wmi_block *next;
853 
854 	list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
855 		if (!wblock->dev.dev.driver)
856 			continue;
857 		if (strcmp(driver_name, wblock->dev.dev.driver->name) == 0) {
858 			filp->private_data = wblock;
859 			break;
860 		}
861 	}
862 
863 	if (!filp->private_data)
864 		return -ENODEV;
865 
866 	return nonseekable_open(inode, filp);
867 }
868 
wmi_char_read(struct file * filp,char __user * buffer,size_t length,loff_t * offset)869 static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
870 			     size_t length, loff_t *offset)
871 {
872 	struct wmi_block *wblock = filp->private_data;
873 
874 	return simple_read_from_buffer(buffer, length, offset,
875 				       &wblock->req_buf_size,
876 				       sizeof(wblock->req_buf_size));
877 }
878 
wmi_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)879 static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
880 {
881 	struct wmi_ioctl_buffer __user *input =
882 		(struct wmi_ioctl_buffer __user *) arg;
883 	struct wmi_block *wblock = filp->private_data;
884 	struct wmi_ioctl_buffer *buf;
885 	struct wmi_driver *wdriver;
886 	int ret;
887 
888 	if (_IOC_TYPE(cmd) != WMI_IOC)
889 		return -ENOTTY;
890 
891 	/* make sure we're not calling a higher instance than exists*/
892 	if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
893 		return -EINVAL;
894 
895 	mutex_lock(&wblock->char_mutex);
896 	buf = wblock->handler_data;
897 	if (get_user(buf->length, &input->length)) {
898 		dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
899 		ret = -EFAULT;
900 		goto out_ioctl;
901 	}
902 	/* if it's too small, abort */
903 	if (buf->length < wblock->req_buf_size) {
904 		dev_err(&wblock->dev.dev,
905 			"Buffer %lld too small, need at least %lld\n",
906 			buf->length, wblock->req_buf_size);
907 		ret = -EINVAL;
908 		goto out_ioctl;
909 	}
910 	/* if it's too big, warn, driver will only use what is needed */
911 	if (buf->length > wblock->req_buf_size)
912 		dev_warn(&wblock->dev.dev,
913 			"Buffer %lld is bigger than required %lld\n",
914 			buf->length, wblock->req_buf_size);
915 
916 	/* copy the structure from userspace */
917 	if (copy_from_user(buf, input, wblock->req_buf_size)) {
918 		dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
919 			wblock->req_buf_size);
920 		ret = -EFAULT;
921 		goto out_ioctl;
922 	}
923 
924 	/* let the driver do any filtering and do the call */
925 	wdriver = drv_to_wdrv(wblock->dev.dev.driver);
926 	if (!try_module_get(wdriver->driver.owner)) {
927 		ret = -EBUSY;
928 		goto out_ioctl;
929 	}
930 	ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
931 	module_put(wdriver->driver.owner);
932 	if (ret)
933 		goto out_ioctl;
934 
935 	/* return the result (only up to our internal buffer size) */
936 	if (copy_to_user(input, buf, wblock->req_buf_size)) {
937 		dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
938 			wblock->req_buf_size);
939 		ret = -EFAULT;
940 	}
941 
942 out_ioctl:
943 	mutex_unlock(&wblock->char_mutex);
944 	return ret;
945 }
946 
947 static const struct file_operations wmi_fops = {
948 	.owner		= THIS_MODULE,
949 	.read		= wmi_char_read,
950 	.open		= wmi_char_open,
951 	.unlocked_ioctl	= wmi_ioctl,
952 	.compat_ioctl	= compat_ptr_ioctl,
953 };
954 
wmi_dev_probe(struct device * dev)955 static int wmi_dev_probe(struct device *dev)
956 {
957 	struct wmi_block *wblock = dev_to_wblock(dev);
958 	struct wmi_driver *wdriver = drv_to_wdrv(dev->driver);
959 	int ret = 0;
960 	char *buf;
961 
962 	if (ACPI_FAILURE(wmi_method_enable(wblock, true)))
963 		dev_warn(dev, "failed to enable device -- probing anyway\n");
964 
965 	if (wdriver->probe) {
966 		ret = wdriver->probe(dev_to_wdev(dev),
967 				find_guid_context(wblock, wdriver));
968 		if (ret != 0)
969 			goto probe_failure;
970 	}
971 
972 	/* driver wants a character device made */
973 	if (wdriver->filter_callback) {
974 		/* check that required buffer size declared by driver or MOF */
975 		if (!wblock->req_buf_size) {
976 			dev_err(&wblock->dev.dev,
977 				"Required buffer size not set\n");
978 			ret = -EINVAL;
979 			goto probe_failure;
980 		}
981 
982 		wblock->handler_data = kmalloc(wblock->req_buf_size,
983 					       GFP_KERNEL);
984 		if (!wblock->handler_data) {
985 			ret = -ENOMEM;
986 			goto probe_failure;
987 		}
988 
989 		buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name);
990 		if (!buf) {
991 			ret = -ENOMEM;
992 			goto probe_string_failure;
993 		}
994 		wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
995 		wblock->char_dev.name = buf;
996 		wblock->char_dev.fops = &wmi_fops;
997 		wblock->char_dev.mode = 0444;
998 		ret = misc_register(&wblock->char_dev);
999 		if (ret) {
1000 			dev_warn(dev, "failed to register char dev: %d\n", ret);
1001 			ret = -ENOMEM;
1002 			goto probe_misc_failure;
1003 		}
1004 	}
1005 
1006 	set_bit(WMI_PROBED, &wblock->flags);
1007 	return 0;
1008 
1009 probe_misc_failure:
1010 	kfree(buf);
1011 probe_string_failure:
1012 	kfree(wblock->handler_data);
1013 probe_failure:
1014 	if (ACPI_FAILURE(wmi_method_enable(wblock, false)))
1015 		dev_warn(dev, "failed to disable device\n");
1016 	return ret;
1017 }
1018 
wmi_dev_remove(struct device * dev)1019 static void wmi_dev_remove(struct device *dev)
1020 {
1021 	struct wmi_block *wblock = dev_to_wblock(dev);
1022 	struct wmi_driver *wdriver = drv_to_wdrv(dev->driver);
1023 
1024 	clear_bit(WMI_PROBED, &wblock->flags);
1025 
1026 	if (wdriver->filter_callback) {
1027 		misc_deregister(&wblock->char_dev);
1028 		kfree(wblock->char_dev.name);
1029 		kfree(wblock->handler_data);
1030 	}
1031 
1032 	if (wdriver->remove)
1033 		wdriver->remove(dev_to_wdev(dev));
1034 
1035 	if (ACPI_FAILURE(wmi_method_enable(wblock, false)))
1036 		dev_warn(dev, "failed to disable device\n");
1037 }
1038 
1039 static struct class wmi_bus_class = {
1040 	.name = "wmi_bus",
1041 };
1042 
1043 static struct bus_type wmi_bus_type = {
1044 	.name = "wmi",
1045 	.dev_groups = wmi_groups,
1046 	.match = wmi_dev_match,
1047 	.uevent = wmi_dev_uevent,
1048 	.probe = wmi_dev_probe,
1049 	.remove = wmi_dev_remove,
1050 };
1051 
1052 static const struct device_type wmi_type_event = {
1053 	.name = "event",
1054 	.groups = wmi_event_groups,
1055 	.release = wmi_dev_release,
1056 };
1057 
1058 static const struct device_type wmi_type_method = {
1059 	.name = "method",
1060 	.groups = wmi_method_groups,
1061 	.release = wmi_dev_release,
1062 };
1063 
1064 static const struct device_type wmi_type_data = {
1065 	.name = "data",
1066 	.groups = wmi_data_groups,
1067 	.release = wmi_dev_release,
1068 };
1069 
1070 /*
1071  * _WDG is a static list that is only parsed at startup,
1072  * so it's safe to count entries without extra protection.
1073  */
guid_count(const guid_t * guid)1074 static int guid_count(const guid_t *guid)
1075 {
1076 	struct wmi_block *wblock;
1077 	int count = 0;
1078 
1079 	list_for_each_entry(wblock, &wmi_block_list, list) {
1080 		if (guid_equal(&wblock->gblock.guid, guid))
1081 			count++;
1082 	}
1083 
1084 	return count;
1085 }
1086 
wmi_create_device(struct device * wmi_bus_dev,struct wmi_block * wblock,struct acpi_device * device)1087 static int wmi_create_device(struct device *wmi_bus_dev,
1088 			     struct wmi_block *wblock,
1089 			     struct acpi_device *device)
1090 {
1091 	struct acpi_device_info *info;
1092 	char method[WMI_ACPI_METHOD_NAME_SIZE];
1093 	int result;
1094 	uint count;
1095 
1096 	if (wblock->gblock.flags & ACPI_WMI_EVENT) {
1097 		wblock->dev.dev.type = &wmi_type_event;
1098 		goto out_init;
1099 	}
1100 
1101 	if (wblock->gblock.flags & ACPI_WMI_METHOD) {
1102 		wblock->dev.dev.type = &wmi_type_method;
1103 		mutex_init(&wblock->char_mutex);
1104 		goto out_init;
1105 	}
1106 
1107 	/*
1108 	 * Data Block Query Control Method (WQxx by convention) is
1109 	 * required per the WMI documentation. If it is not present,
1110 	 * we ignore this data block.
1111 	 */
1112 	get_acpi_method_name(wblock, 'Q', method);
1113 	result = get_subobj_info(device->handle, method, &info);
1114 
1115 	if (result) {
1116 		dev_warn(wmi_bus_dev,
1117 			 "%s data block query control method not found\n",
1118 			 method);
1119 		return result;
1120 	}
1121 
1122 	wblock->dev.dev.type = &wmi_type_data;
1123 
1124 	/*
1125 	 * The Microsoft documentation specifically states:
1126 	 *
1127 	 *   Data blocks registered with only a single instance
1128 	 *   can ignore the parameter.
1129 	 *
1130 	 * ACPICA will get mad at us if we call the method with the wrong number
1131 	 * of arguments, so check what our method expects.  (On some Dell
1132 	 * laptops, WQxx may not be a method at all.)
1133 	 */
1134 	if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1135 		set_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags);
1136 
1137 	kfree(info);
1138 
1139 	get_acpi_method_name(wblock, 'S', method);
1140 	result = get_subobj_info(device->handle, method, NULL);
1141 
1142 	if (result == 0)
1143 		wblock->dev.setable = true;
1144 
1145  out_init:
1146 	wblock->dev.dev.bus = &wmi_bus_type;
1147 	wblock->dev.dev.parent = wmi_bus_dev;
1148 
1149 	count = guid_count(&wblock->gblock.guid);
1150 	if (count)
1151 		dev_set_name(&wblock->dev.dev, "%pUL-%d", &wblock->gblock.guid, count);
1152 	else
1153 		dev_set_name(&wblock->dev.dev, "%pUL", &wblock->gblock.guid);
1154 
1155 	device_initialize(&wblock->dev.dev);
1156 
1157 	return 0;
1158 }
1159 
wmi_free_devices(struct acpi_device * device)1160 static void wmi_free_devices(struct acpi_device *device)
1161 {
1162 	struct wmi_block *wblock, *next;
1163 
1164 	/* Delete devices for all the GUIDs */
1165 	list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1166 		if (wblock->acpi_device == device) {
1167 			list_del(&wblock->list);
1168 			device_unregister(&wblock->dev.dev);
1169 		}
1170 	}
1171 }
1172 
guid_already_parsed_for_legacy(struct acpi_device * device,const guid_t * guid)1173 static bool guid_already_parsed_for_legacy(struct acpi_device *device, const guid_t *guid)
1174 {
1175 	struct wmi_block *wblock;
1176 
1177 	list_for_each_entry(wblock, &wmi_block_list, list) {
1178 		/* skip warning and register if we know the driver will use struct wmi_driver */
1179 		for (int i = 0; allow_duplicates[i] != NULL; i++) {
1180 			guid_t tmp;
1181 
1182 			if (guid_parse(allow_duplicates[i], &tmp))
1183 				continue;
1184 			if (guid_equal(&tmp, guid))
1185 				return false;
1186 		}
1187 		if (guid_equal(&wblock->gblock.guid, guid)) {
1188 			/*
1189 			 * Because we historically didn't track the relationship
1190 			 * between GUIDs and ACPI nodes, we don't know whether
1191 			 * we need to suppress GUIDs that are unique on a
1192 			 * given node but duplicated across nodes.
1193 			 */
1194 			dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1195 				 guid, dev_name(&wblock->acpi_device->dev));
1196 			return true;
1197 		}
1198 	}
1199 
1200 	return false;
1201 }
1202 
1203 /*
1204  * Parse the _WDG method for the GUID data blocks
1205  */
parse_wdg(struct device * wmi_bus_dev,struct acpi_device * device)1206 static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
1207 {
1208 	struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1209 	const struct guid_block *gblock;
1210 	struct wmi_block *wblock, *next;
1211 	union acpi_object *obj;
1212 	acpi_status status;
1213 	int retval = 0;
1214 	u32 i, total;
1215 
1216 	status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1217 	if (ACPI_FAILURE(status))
1218 		return -ENXIO;
1219 
1220 	obj = out.pointer;
1221 	if (!obj)
1222 		return -ENXIO;
1223 
1224 	if (obj->type != ACPI_TYPE_BUFFER) {
1225 		retval = -ENXIO;
1226 		goto out_free_pointer;
1227 	}
1228 
1229 	gblock = (const struct guid_block *)obj->buffer.pointer;
1230 	total = obj->buffer.length / sizeof(struct guid_block);
1231 
1232 	for (i = 0; i < total; i++) {
1233 		if (debug_dump_wdg)
1234 			wmi_dump_wdg(&gblock[i]);
1235 
1236 		if (guid_already_parsed_for_legacy(device, &gblock[i].guid))
1237 			continue;
1238 
1239 		wblock = kzalloc(sizeof(*wblock), GFP_KERNEL);
1240 		if (!wblock) {
1241 			retval = -ENOMEM;
1242 			break;
1243 		}
1244 
1245 		wblock->acpi_device = device;
1246 		wblock->gblock = gblock[i];
1247 
1248 		retval = wmi_create_device(wmi_bus_dev, wblock, device);
1249 		if (retval) {
1250 			kfree(wblock);
1251 			continue;
1252 		}
1253 
1254 		list_add_tail(&wblock->list, &wmi_block_list);
1255 
1256 		if (debug_event) {
1257 			wblock->handler = wmi_notify_debug;
1258 			wmi_method_enable(wblock, true);
1259 		}
1260 	}
1261 
1262 	/*
1263 	 * Now that all of the devices are created, add them to the
1264 	 * device tree and probe subdrivers.
1265 	 */
1266 	list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1267 		if (wblock->acpi_device != device)
1268 			continue;
1269 
1270 		retval = device_add(&wblock->dev.dev);
1271 		if (retval) {
1272 			dev_err(wmi_bus_dev, "failed to register %pUL\n",
1273 				&wblock->gblock.guid);
1274 			if (debug_event)
1275 				wmi_method_enable(wblock, false);
1276 			list_del(&wblock->list);
1277 			put_device(&wblock->dev.dev);
1278 		}
1279 	}
1280 
1281 out_free_pointer:
1282 	kfree(out.pointer);
1283 	return retval;
1284 }
1285 
1286 /*
1287  * WMI can have EmbeddedControl access regions. In which case, we just want to
1288  * hand these off to the EC driver.
1289  */
1290 static acpi_status
acpi_wmi_ec_space_handler(u32 function,acpi_physical_address address,u32 bits,u64 * value,void * handler_context,void * region_context)1291 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
1292 			  u32 bits, u64 *value,
1293 			  void *handler_context, void *region_context)
1294 {
1295 	int result = 0, i = 0;
1296 	u8 temp = 0;
1297 
1298 	if ((address > 0xFF) || !value)
1299 		return AE_BAD_PARAMETER;
1300 
1301 	if (function != ACPI_READ && function != ACPI_WRITE)
1302 		return AE_BAD_PARAMETER;
1303 
1304 	if (bits != 8)
1305 		return AE_BAD_PARAMETER;
1306 
1307 	if (function == ACPI_READ) {
1308 		result = ec_read(address, &temp);
1309 		(*value) |= ((u64)temp) << i;
1310 	} else {
1311 		temp = 0xff & ((*value) >> i);
1312 		result = ec_write(address, temp);
1313 	}
1314 
1315 	switch (result) {
1316 	case -EINVAL:
1317 		return AE_BAD_PARAMETER;
1318 	case -ENODEV:
1319 		return AE_NOT_FOUND;
1320 	case -ETIME:
1321 		return AE_TIME;
1322 	default:
1323 		return AE_OK;
1324 	}
1325 }
1326 
acpi_wmi_notify_handler(acpi_handle handle,u32 event,void * context)1327 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1328 				    void *context)
1329 {
1330 	struct wmi_block *wblock = NULL, *iter;
1331 
1332 	list_for_each_entry(iter, &wmi_block_list, list) {
1333 		struct guid_block *block = &iter->gblock;
1334 
1335 		if (iter->acpi_device->handle == handle &&
1336 		    (block->flags & ACPI_WMI_EVENT) &&
1337 		    (block->notify_id == event)) {
1338 			wblock = iter;
1339 			break;
1340 		}
1341 	}
1342 
1343 	if (!wblock)
1344 		return;
1345 
1346 	/* If a driver is bound, then notify the driver. */
1347 	if (test_bit(WMI_PROBED, &wblock->flags) && wblock->dev.dev.driver) {
1348 		struct wmi_driver *driver = drv_to_wdrv(wblock->dev.dev.driver);
1349 		struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1350 		acpi_status status;
1351 
1352 		if (!driver->no_notify_data) {
1353 			status = get_event_data(wblock, &evdata);
1354 			if (ACPI_FAILURE(status)) {
1355 				dev_warn(&wblock->dev.dev, "failed to get event data\n");
1356 				return;
1357 			}
1358 		}
1359 
1360 		if (driver->notify)
1361 			driver->notify(&wblock->dev, evdata.pointer);
1362 
1363 		kfree(evdata.pointer);
1364 	} else if (wblock->handler) {
1365 		/* Legacy handler */
1366 		wblock->handler(event, wblock->handler_data);
1367 	}
1368 
1369 	if (debug_event)
1370 		pr_info("DEBUG: GUID %pUL event 0x%02X\n", &wblock->gblock.guid, event);
1371 
1372 	acpi_bus_generate_netlink_event(
1373 		wblock->acpi_device->pnp.device_class,
1374 		dev_name(&wblock->dev.dev),
1375 		event, 0);
1376 }
1377 
acpi_wmi_remove(struct platform_device * device)1378 static int acpi_wmi_remove(struct platform_device *device)
1379 {
1380 	struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1381 
1382 	acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
1383 				   acpi_wmi_notify_handler);
1384 	acpi_remove_address_space_handler(acpi_device->handle,
1385 				ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
1386 	wmi_free_devices(acpi_device);
1387 	device_unregister(dev_get_drvdata(&device->dev));
1388 
1389 	return 0;
1390 }
1391 
acpi_wmi_probe(struct platform_device * device)1392 static int acpi_wmi_probe(struct platform_device *device)
1393 {
1394 	struct acpi_device *acpi_device;
1395 	struct device *wmi_bus_dev;
1396 	acpi_status status;
1397 	int error;
1398 
1399 	acpi_device = ACPI_COMPANION(&device->dev);
1400 	if (!acpi_device) {
1401 		dev_err(&device->dev, "ACPI companion is missing\n");
1402 		return -ENODEV;
1403 	}
1404 
1405 	status = acpi_install_address_space_handler(acpi_device->handle,
1406 						    ACPI_ADR_SPACE_EC,
1407 						    &acpi_wmi_ec_space_handler,
1408 						    NULL, NULL);
1409 	if (ACPI_FAILURE(status)) {
1410 		dev_err(&device->dev, "Error installing EC region handler\n");
1411 		return -ENODEV;
1412 	}
1413 
1414 	status = acpi_install_notify_handler(acpi_device->handle,
1415 					     ACPI_ALL_NOTIFY,
1416 					     acpi_wmi_notify_handler,
1417 					     NULL);
1418 	if (ACPI_FAILURE(status)) {
1419 		dev_err(&device->dev, "Error installing notify handler\n");
1420 		error = -ENODEV;
1421 		goto err_remove_ec_handler;
1422 	}
1423 
1424 	wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1425 				    NULL, "wmi_bus-%s", dev_name(&device->dev));
1426 	if (IS_ERR(wmi_bus_dev)) {
1427 		error = PTR_ERR(wmi_bus_dev);
1428 		goto err_remove_notify_handler;
1429 	}
1430 	dev_set_drvdata(&device->dev, wmi_bus_dev);
1431 
1432 	error = parse_wdg(wmi_bus_dev, acpi_device);
1433 	if (error) {
1434 		pr_err("Failed to parse WDG method\n");
1435 		goto err_remove_busdev;
1436 	}
1437 
1438 	return 0;
1439 
1440 err_remove_busdev:
1441 	device_unregister(wmi_bus_dev);
1442 
1443 err_remove_notify_handler:
1444 	acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
1445 				   acpi_wmi_notify_handler);
1446 
1447 err_remove_ec_handler:
1448 	acpi_remove_address_space_handler(acpi_device->handle,
1449 					  ACPI_ADR_SPACE_EC,
1450 					  &acpi_wmi_ec_space_handler);
1451 
1452 	return error;
1453 }
1454 
__wmi_driver_register(struct wmi_driver * driver,struct module * owner)1455 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1456 				       struct module *owner)
1457 {
1458 	driver->driver.owner = owner;
1459 	driver->driver.bus = &wmi_bus_type;
1460 
1461 	return driver_register(&driver->driver);
1462 }
1463 EXPORT_SYMBOL(__wmi_driver_register);
1464 
wmi_driver_unregister(struct wmi_driver * driver)1465 void wmi_driver_unregister(struct wmi_driver *driver)
1466 {
1467 	driver_unregister(&driver->driver);
1468 }
1469 EXPORT_SYMBOL(wmi_driver_unregister);
1470 
1471 static struct platform_driver acpi_wmi_driver = {
1472 	.driver = {
1473 		.name = "acpi-wmi",
1474 		.acpi_match_table = wmi_device_ids,
1475 	},
1476 	.probe = acpi_wmi_probe,
1477 	.remove = acpi_wmi_remove,
1478 };
1479 
acpi_wmi_init(void)1480 static int __init acpi_wmi_init(void)
1481 {
1482 	int error;
1483 
1484 	if (acpi_disabled)
1485 		return -ENODEV;
1486 
1487 	error = class_register(&wmi_bus_class);
1488 	if (error)
1489 		return error;
1490 
1491 	error = bus_register(&wmi_bus_type);
1492 	if (error)
1493 		goto err_unreg_class;
1494 
1495 	error = platform_driver_register(&acpi_wmi_driver);
1496 	if (error) {
1497 		pr_err("Error loading mapper\n");
1498 		goto err_unreg_bus;
1499 	}
1500 
1501 	return 0;
1502 
1503 err_unreg_bus:
1504 	bus_unregister(&wmi_bus_type);
1505 
1506 err_unreg_class:
1507 	class_unregister(&wmi_bus_class);
1508 
1509 	return error;
1510 }
1511 
acpi_wmi_exit(void)1512 static void __exit acpi_wmi_exit(void)
1513 {
1514 	platform_driver_unregister(&acpi_wmi_driver);
1515 	bus_unregister(&wmi_bus_type);
1516 	class_unregister(&wmi_bus_class);
1517 }
1518 
1519 subsys_initcall_sync(acpi_wmi_init);
1520 module_exit(acpi_wmi_exit);
1521