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