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