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