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