0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0018
0019 #include <linux/acpi.h>
0020 #include <linux/bits.h>
0021 #include <linux/build_bug.h>
0022 #include <linux/device.h>
0023 #include <linux/init.h>
0024 #include <linux/kernel.h>
0025 #include <linux/list.h>
0026 #include <linux/miscdevice.h>
0027 #include <linux/module.h>
0028 #include <linux/platform_device.h>
0029 #include <linux/slab.h>
0030 #include <linux/sysfs.h>
0031 #include <linux/types.h>
0032 #include <linux/uaccess.h>
0033 #include <linux/uuid.h>
0034 #include <linux/wmi.h>
0035 #include <linux/fs.h>
0036 #include <uapi/linux/wmi.h>
0037
0038 MODULE_AUTHOR("Carlos Corbacho");
0039 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
0040 MODULE_LICENSE("GPL");
0041
0042 static LIST_HEAD(wmi_block_list);
0043
0044 struct guid_block {
0045 guid_t guid;
0046 union {
0047 char object_id[2];
0048 struct {
0049 unsigned char notify_id;
0050 unsigned char reserved;
0051 };
0052 };
0053 u8 instance_count;
0054 u8 flags;
0055 } __packed;
0056 static_assert(sizeof(typeof_member(struct guid_block, guid)) == 16);
0057 static_assert(sizeof(struct guid_block) == 20);
0058 static_assert(__alignof__(struct guid_block) == 1);
0059
0060 enum {
0061 WMI_READ_TAKES_NO_ARGS,
0062 WMI_PROBED,
0063 };
0064
0065 struct wmi_block {
0066 struct wmi_device dev;
0067 struct list_head list;
0068 struct guid_block gblock;
0069 struct miscdevice char_dev;
0070 struct mutex char_mutex;
0071 struct acpi_device *acpi_device;
0072 wmi_notify_handler handler;
0073 void *handler_data;
0074 u64 req_buf_size;
0075 unsigned long flags;
0076 };
0077
0078
0079
0080
0081
0082
0083 #define ACPI_WMI_EXPENSIVE BIT(0)
0084 #define ACPI_WMI_METHOD BIT(1)
0085 #define ACPI_WMI_STRING BIT(2)
0086 #define ACPI_WMI_EVENT BIT(3)
0087
0088 static bool debug_event;
0089 module_param(debug_event, bool, 0444);
0090 MODULE_PARM_DESC(debug_event,
0091 "Log WMI Events [0/1]");
0092
0093 static bool debug_dump_wdg;
0094 module_param(debug_dump_wdg, bool, 0444);
0095 MODULE_PARM_DESC(debug_dump_wdg,
0096 "Dump available WMI interfaces [0/1]");
0097
0098 static int acpi_wmi_remove(struct platform_device *device);
0099 static int acpi_wmi_probe(struct platform_device *device);
0100
0101 static const struct acpi_device_id wmi_device_ids[] = {
0102 {"PNP0C14", 0},
0103 {"pnp0c14", 0},
0104 { }
0105 };
0106 MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
0107
0108 static struct platform_driver acpi_wmi_driver = {
0109 .driver = {
0110 .name = "acpi-wmi",
0111 .acpi_match_table = wmi_device_ids,
0112 },
0113 .probe = acpi_wmi_probe,
0114 .remove = acpi_wmi_remove,
0115 };
0116
0117
0118
0119
0120
0121 static acpi_status find_guid(const char *guid_string, struct wmi_block **out)
0122 {
0123 guid_t guid_input;
0124 struct wmi_block *wblock;
0125
0126 if (!guid_string)
0127 return AE_BAD_PARAMETER;
0128
0129 if (guid_parse(guid_string, &guid_input))
0130 return AE_BAD_PARAMETER;
0131
0132 list_for_each_entry(wblock, &wmi_block_list, list) {
0133 if (guid_equal(&wblock->gblock.guid, &guid_input)) {
0134 if (out)
0135 *out = wblock;
0136
0137 return AE_OK;
0138 }
0139 }
0140
0141 return AE_NOT_FOUND;
0142 }
0143
0144 static const void *find_guid_context(struct wmi_block *wblock,
0145 struct wmi_driver *wdriver)
0146 {
0147 const struct wmi_device_id *id;
0148
0149 id = wdriver->id_table;
0150 if (!id)
0151 return NULL;
0152
0153 while (*id->guid_string) {
0154 guid_t guid_input;
0155
0156 if (guid_parse(id->guid_string, &guid_input))
0157 continue;
0158 if (guid_equal(&wblock->gblock.guid, &guid_input))
0159 return id->context;
0160 id++;
0161 }
0162 return NULL;
0163 }
0164
0165 static int get_subobj_info(acpi_handle handle, const char *pathname,
0166 struct acpi_device_info **info)
0167 {
0168 struct acpi_device_info *dummy_info, **info_ptr;
0169 acpi_handle subobj_handle;
0170 acpi_status status;
0171
0172 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
0173 if (status == AE_NOT_FOUND)
0174 return -ENOENT;
0175 else if (ACPI_FAILURE(status))
0176 return -EIO;
0177
0178 info_ptr = info ? info : &dummy_info;
0179 status = acpi_get_object_info(subobj_handle, info_ptr);
0180 if (ACPI_FAILURE(status))
0181 return -EIO;
0182
0183 if (!info)
0184 kfree(dummy_info);
0185
0186 return 0;
0187 }
0188
0189 static acpi_status wmi_method_enable(struct wmi_block *wblock, bool enable)
0190 {
0191 struct guid_block *block;
0192 char method[5];
0193 acpi_status status;
0194 acpi_handle handle;
0195
0196 block = &wblock->gblock;
0197 handle = wblock->acpi_device->handle;
0198
0199 snprintf(method, 5, "WE%02X", block->notify_id);
0200 status = acpi_execute_simple_method(handle, method, enable);
0201 if (status == AE_NOT_FOUND)
0202 return AE_OK;
0203
0204 return status;
0205 }
0206
0207 #define WMI_ACPI_METHOD_NAME_SIZE 5
0208
0209 static inline void get_acpi_method_name(const struct wmi_block *wblock,
0210 const char method,
0211 char buffer[static WMI_ACPI_METHOD_NAME_SIZE])
0212 {
0213 static_assert(ARRAY_SIZE(wblock->gblock.object_id) == 2);
0214 static_assert(WMI_ACPI_METHOD_NAME_SIZE >= 5);
0215
0216 buffer[0] = 'W';
0217 buffer[1] = method;
0218 buffer[2] = wblock->gblock.object_id[0];
0219 buffer[3] = wblock->gblock.object_id[1];
0220 buffer[4] = '\0';
0221 }
0222
0223 static inline acpi_object_type get_param_acpi_type(const struct wmi_block *wblock)
0224 {
0225 if (wblock->gblock.flags & ACPI_WMI_STRING)
0226 return ACPI_TYPE_STRING;
0227 else
0228 return ACPI_TYPE_BUFFER;
0229 }
0230
0231 static acpi_status get_event_data(const struct wmi_block *wblock, struct acpi_buffer *out)
0232 {
0233 union acpi_object param = {
0234 .integer = {
0235 .type = ACPI_TYPE_INTEGER,
0236 .value = wblock->gblock.notify_id,
0237 }
0238 };
0239 struct acpi_object_list input = {
0240 .count = 1,
0241 .pointer = ¶m,
0242 };
0243
0244 return acpi_evaluate_object(wblock->acpi_device->handle, "_WED", &input, out);
0245 }
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258 int set_required_buffer_size(struct wmi_device *wdev, u64 length)
0259 {
0260 struct wmi_block *wblock;
0261
0262 wblock = container_of(wdev, struct wmi_block, dev);
0263 wblock->req_buf_size = length;
0264
0265 return 0;
0266 }
0267 EXPORT_SYMBOL_GPL(set_required_buffer_size);
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance, u32 method_id,
0280 const struct acpi_buffer *in, struct acpi_buffer *out)
0281 {
0282 struct wmi_block *wblock = NULL;
0283 acpi_status status;
0284
0285 status = find_guid(guid_string, &wblock);
0286 if (ACPI_FAILURE(status))
0287 return status;
0288
0289 return wmidev_evaluate_method(&wblock->dev, instance, method_id,
0290 in, out);
0291 }
0292 EXPORT_SYMBOL_GPL(wmi_evaluate_method);
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304 acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance, u32 method_id,
0305 const struct acpi_buffer *in, struct acpi_buffer *out)
0306 {
0307 struct guid_block *block;
0308 struct wmi_block *wblock;
0309 acpi_handle handle;
0310 struct acpi_object_list input;
0311 union acpi_object params[3];
0312 char method[WMI_ACPI_METHOD_NAME_SIZE];
0313
0314 wblock = container_of(wdev, struct wmi_block, dev);
0315 block = &wblock->gblock;
0316 handle = wblock->acpi_device->handle;
0317
0318 if (!(block->flags & ACPI_WMI_METHOD))
0319 return AE_BAD_DATA;
0320
0321 if (block->instance_count <= instance)
0322 return AE_BAD_PARAMETER;
0323
0324 input.count = 2;
0325 input.pointer = params;
0326 params[0].type = ACPI_TYPE_INTEGER;
0327 params[0].integer.value = instance;
0328 params[1].type = ACPI_TYPE_INTEGER;
0329 params[1].integer.value = method_id;
0330
0331 if (in) {
0332 input.count = 3;
0333
0334 params[2].type = get_param_acpi_type(wblock);
0335 params[2].buffer.length = in->length;
0336 params[2].buffer.pointer = in->pointer;
0337 }
0338
0339 get_acpi_method_name(wblock, 'M', method);
0340
0341 return acpi_evaluate_object(handle, method, &input, out);
0342 }
0343 EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
0344
0345 static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
0346 struct acpi_buffer *out)
0347 {
0348 struct guid_block *block;
0349 acpi_handle handle;
0350 acpi_status status, wc_status = AE_ERROR;
0351 struct acpi_object_list input;
0352 union acpi_object wq_params[1];
0353 char wc_method[WMI_ACPI_METHOD_NAME_SIZE];
0354 char method[WMI_ACPI_METHOD_NAME_SIZE];
0355
0356 if (!out)
0357 return AE_BAD_PARAMETER;
0358
0359 block = &wblock->gblock;
0360 handle = wblock->acpi_device->handle;
0361
0362 if (block->instance_count <= instance)
0363 return AE_BAD_PARAMETER;
0364
0365
0366 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
0367 return AE_ERROR;
0368
0369 input.count = 1;
0370 input.pointer = wq_params;
0371 wq_params[0].type = ACPI_TYPE_INTEGER;
0372 wq_params[0].integer.value = instance;
0373
0374 if (instance == 0 && test_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags))
0375 input.count = 0;
0376
0377
0378
0379
0380
0381 if (block->flags & ACPI_WMI_EXPENSIVE) {
0382 get_acpi_method_name(wblock, 'C', wc_method);
0383
0384
0385
0386
0387
0388
0389 wc_status = acpi_execute_simple_method(handle, wc_method, 1);
0390 }
0391
0392 get_acpi_method_name(wblock, 'Q', method);
0393 status = acpi_evaluate_object(handle, method, &input, out);
0394
0395
0396
0397
0398
0399 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
0400
0401
0402
0403
0404
0405
0406
0407 acpi_execute_simple_method(handle, wc_method, 0);
0408 }
0409
0410 return status;
0411 }
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421 acpi_status wmi_query_block(const char *guid_string, u8 instance,
0422 struct acpi_buffer *out)
0423 {
0424 struct wmi_block *wblock;
0425 acpi_status status;
0426
0427 status = find_guid(guid_string, &wblock);
0428 if (ACPI_FAILURE(status))
0429 return status;
0430
0431 return __query_block(wblock, instance, out);
0432 }
0433 EXPORT_SYMBOL_GPL(wmi_query_block);
0434
0435 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
0436 {
0437 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
0438 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
0439
0440 if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
0441 return NULL;
0442
0443 return out.pointer;
0444 }
0445 EXPORT_SYMBOL_GPL(wmidev_block_query);
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455 acpi_status wmi_set_block(const char *guid_string, u8 instance,
0456 const struct acpi_buffer *in)
0457 {
0458 struct wmi_block *wblock = NULL;
0459 struct guid_block *block;
0460 acpi_handle handle;
0461 struct acpi_object_list input;
0462 union acpi_object params[2];
0463 char method[WMI_ACPI_METHOD_NAME_SIZE];
0464 acpi_status status;
0465
0466 if (!in)
0467 return AE_BAD_DATA;
0468
0469 status = find_guid(guid_string, &wblock);
0470 if (ACPI_FAILURE(status))
0471 return status;
0472
0473 block = &wblock->gblock;
0474 handle = wblock->acpi_device->handle;
0475
0476 if (block->instance_count <= instance)
0477 return AE_BAD_PARAMETER;
0478
0479
0480 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
0481 return AE_ERROR;
0482
0483 input.count = 2;
0484 input.pointer = params;
0485 params[0].type = ACPI_TYPE_INTEGER;
0486 params[0].integer.value = instance;
0487 params[1].type = get_param_acpi_type(wblock);
0488 params[1].buffer.length = in->length;
0489 params[1].buffer.pointer = in->pointer;
0490
0491 get_acpi_method_name(wblock, 'S', method);
0492
0493 return acpi_evaluate_object(handle, method, &input, NULL);
0494 }
0495 EXPORT_SYMBOL_GPL(wmi_set_block);
0496
0497 static void wmi_dump_wdg(const struct guid_block *g)
0498 {
0499 pr_info("%pUL:\n", &g->guid);
0500 if (g->flags & ACPI_WMI_EVENT)
0501 pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
0502 else
0503 pr_info("\tobject_id: %2pE\n", g->object_id);
0504 pr_info("\tinstance_count: %d\n", g->instance_count);
0505 pr_info("\tflags: %#x", g->flags);
0506 if (g->flags) {
0507 if (g->flags & ACPI_WMI_EXPENSIVE)
0508 pr_cont(" ACPI_WMI_EXPENSIVE");
0509 if (g->flags & ACPI_WMI_METHOD)
0510 pr_cont(" ACPI_WMI_METHOD");
0511 if (g->flags & ACPI_WMI_STRING)
0512 pr_cont(" ACPI_WMI_STRING");
0513 if (g->flags & ACPI_WMI_EVENT)
0514 pr_cont(" ACPI_WMI_EVENT");
0515 }
0516 pr_cont("\n");
0517
0518 }
0519
0520 static void wmi_notify_debug(u32 value, void *context)
0521 {
0522 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
0523 union acpi_object *obj;
0524 acpi_status status;
0525
0526 status = wmi_get_event_data(value, &response);
0527 if (status != AE_OK) {
0528 pr_info("bad event status 0x%x\n", status);
0529 return;
0530 }
0531
0532 obj = response.pointer;
0533 if (!obj)
0534 return;
0535
0536 pr_info("DEBUG: event 0x%02X ", value);
0537 switch (obj->type) {
0538 case ACPI_TYPE_BUFFER:
0539 pr_cont("BUFFER_TYPE - length %u\n", obj->buffer.length);
0540 break;
0541 case ACPI_TYPE_STRING:
0542 pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
0543 break;
0544 case ACPI_TYPE_INTEGER:
0545 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
0546 break;
0547 case ACPI_TYPE_PACKAGE:
0548 pr_cont("PACKAGE_TYPE - %u elements\n", obj->package.count);
0549 break;
0550 default:
0551 pr_cont("object type 0x%X\n", obj->type);
0552 }
0553 kfree(obj);
0554 }
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564 acpi_status wmi_install_notify_handler(const char *guid,
0565 wmi_notify_handler handler,
0566 void *data)
0567 {
0568 struct wmi_block *block;
0569 acpi_status status = AE_NOT_EXIST;
0570 guid_t guid_input;
0571
0572 if (!guid || !handler)
0573 return AE_BAD_PARAMETER;
0574
0575 if (guid_parse(guid, &guid_input))
0576 return AE_BAD_PARAMETER;
0577
0578 list_for_each_entry(block, &wmi_block_list, list) {
0579 acpi_status wmi_status;
0580
0581 if (guid_equal(&block->gblock.guid, &guid_input)) {
0582 if (block->handler &&
0583 block->handler != wmi_notify_debug)
0584 return AE_ALREADY_ACQUIRED;
0585
0586 block->handler = handler;
0587 block->handler_data = data;
0588
0589 wmi_status = wmi_method_enable(block, true);
0590 if ((wmi_status != AE_OK) ||
0591 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
0592 status = wmi_status;
0593 }
0594 }
0595
0596 return status;
0597 }
0598 EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
0599
0600
0601
0602
0603
0604
0605
0606 acpi_status wmi_remove_notify_handler(const char *guid)
0607 {
0608 struct wmi_block *block;
0609 acpi_status status = AE_NOT_EXIST;
0610 guid_t guid_input;
0611
0612 if (!guid)
0613 return AE_BAD_PARAMETER;
0614
0615 if (guid_parse(guid, &guid_input))
0616 return AE_BAD_PARAMETER;
0617
0618 list_for_each_entry(block, &wmi_block_list, list) {
0619 acpi_status wmi_status;
0620
0621 if (guid_equal(&block->gblock.guid, &guid_input)) {
0622 if (!block->handler ||
0623 block->handler == wmi_notify_debug)
0624 return AE_NULL_ENTRY;
0625
0626 if (debug_event) {
0627 block->handler = wmi_notify_debug;
0628 status = AE_OK;
0629 } else {
0630 wmi_status = wmi_method_enable(block, false);
0631 block->handler = NULL;
0632 block->handler_data = NULL;
0633 if ((wmi_status != AE_OK) ||
0634 ((wmi_status == AE_OK) &&
0635 (status == AE_NOT_EXIST)))
0636 status = wmi_status;
0637 }
0638 }
0639 }
0640
0641 return status;
0642 }
0643 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
0654 {
0655 struct wmi_block *wblock;
0656
0657 list_for_each_entry(wblock, &wmi_block_list, list) {
0658 struct guid_block *gblock = &wblock->gblock;
0659
0660 if ((gblock->flags & ACPI_WMI_EVENT) && gblock->notify_id == event)
0661 return get_event_data(wblock, out);
0662 }
0663
0664 return AE_NOT_FOUND;
0665 }
0666 EXPORT_SYMBOL_GPL(wmi_get_event_data);
0667
0668
0669
0670
0671
0672
0673
0674 bool wmi_has_guid(const char *guid_string)
0675 {
0676 return ACPI_SUCCESS(find_guid(guid_string, NULL));
0677 }
0678 EXPORT_SYMBOL_GPL(wmi_has_guid);
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688 char *wmi_get_acpi_device_uid(const char *guid_string)
0689 {
0690 struct wmi_block *wblock = NULL;
0691 acpi_status status;
0692
0693 status = find_guid(guid_string, &wblock);
0694 if (ACPI_FAILURE(status))
0695 return NULL;
0696
0697 return acpi_device_uid(wblock->acpi_device);
0698 }
0699 EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid);
0700
0701 static struct wmi_block *dev_to_wblock(struct device *dev)
0702 {
0703 return container_of(dev, struct wmi_block, dev.dev);
0704 }
0705
0706 static struct wmi_device *dev_to_wdev(struct device *dev)
0707 {
0708 return container_of(dev, struct wmi_device, dev);
0709 }
0710
0711 static inline struct wmi_driver *drv_to_wdrv(struct device_driver *drv)
0712 {
0713 return container_of(drv, struct wmi_driver, driver);
0714 }
0715
0716
0717
0718
0719 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
0720 char *buf)
0721 {
0722 struct wmi_block *wblock = dev_to_wblock(dev);
0723
0724 return sysfs_emit(buf, "wmi:%pUL\n", &wblock->gblock.guid);
0725 }
0726 static DEVICE_ATTR_RO(modalias);
0727
0728 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
0729 char *buf)
0730 {
0731 struct wmi_block *wblock = dev_to_wblock(dev);
0732
0733 return sysfs_emit(buf, "%pUL\n", &wblock->gblock.guid);
0734 }
0735 static DEVICE_ATTR_RO(guid);
0736
0737 static ssize_t instance_count_show(struct device *dev,
0738 struct device_attribute *attr, char *buf)
0739 {
0740 struct wmi_block *wblock = dev_to_wblock(dev);
0741
0742 return sysfs_emit(buf, "%d\n", (int)wblock->gblock.instance_count);
0743 }
0744 static DEVICE_ATTR_RO(instance_count);
0745
0746 static ssize_t expensive_show(struct device *dev,
0747 struct device_attribute *attr, char *buf)
0748 {
0749 struct wmi_block *wblock = dev_to_wblock(dev);
0750
0751 return sysfs_emit(buf, "%d\n",
0752 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
0753 }
0754 static DEVICE_ATTR_RO(expensive);
0755
0756 static struct attribute *wmi_attrs[] = {
0757 &dev_attr_modalias.attr,
0758 &dev_attr_guid.attr,
0759 &dev_attr_instance_count.attr,
0760 &dev_attr_expensive.attr,
0761 NULL
0762 };
0763 ATTRIBUTE_GROUPS(wmi);
0764
0765 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
0766 char *buf)
0767 {
0768 struct wmi_block *wblock = dev_to_wblock(dev);
0769
0770 return sysfs_emit(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
0771 }
0772 static DEVICE_ATTR_RO(notify_id);
0773
0774 static struct attribute *wmi_event_attrs[] = {
0775 &dev_attr_notify_id.attr,
0776 NULL
0777 };
0778 ATTRIBUTE_GROUPS(wmi_event);
0779
0780 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
0781 char *buf)
0782 {
0783 struct wmi_block *wblock = dev_to_wblock(dev);
0784
0785 return sysfs_emit(buf, "%c%c\n", wblock->gblock.object_id[0],
0786 wblock->gblock.object_id[1]);
0787 }
0788 static DEVICE_ATTR_RO(object_id);
0789
0790 static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
0791 char *buf)
0792 {
0793 struct wmi_device *wdev = dev_to_wdev(dev);
0794
0795 return sysfs_emit(buf, "%d\n", (int)wdev->setable);
0796 }
0797 static DEVICE_ATTR_RO(setable);
0798
0799 static struct attribute *wmi_data_attrs[] = {
0800 &dev_attr_object_id.attr,
0801 &dev_attr_setable.attr,
0802 NULL
0803 };
0804 ATTRIBUTE_GROUPS(wmi_data);
0805
0806 static struct attribute *wmi_method_attrs[] = {
0807 &dev_attr_object_id.attr,
0808 NULL
0809 };
0810 ATTRIBUTE_GROUPS(wmi_method);
0811
0812 static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
0813 {
0814 struct wmi_block *wblock = dev_to_wblock(dev);
0815
0816 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", &wblock->gblock.guid))
0817 return -ENOMEM;
0818
0819 if (add_uevent_var(env, "WMI_GUID=%pUL", &wblock->gblock.guid))
0820 return -ENOMEM;
0821
0822 return 0;
0823 }
0824
0825 static void wmi_dev_release(struct device *dev)
0826 {
0827 struct wmi_block *wblock = dev_to_wblock(dev);
0828
0829 kfree(wblock);
0830 }
0831
0832 static int wmi_dev_match(struct device *dev, struct device_driver *driver)
0833 {
0834 struct wmi_driver *wmi_driver = drv_to_wdrv(driver);
0835 struct wmi_block *wblock = dev_to_wblock(dev);
0836 const struct wmi_device_id *id = wmi_driver->id_table;
0837
0838 if (id == NULL)
0839 return 0;
0840
0841 while (*id->guid_string) {
0842 guid_t driver_guid;
0843
0844 if (WARN_ON(guid_parse(id->guid_string, &driver_guid)))
0845 continue;
0846 if (guid_equal(&driver_guid, &wblock->gblock.guid))
0847 return 1;
0848
0849 id++;
0850 }
0851
0852 return 0;
0853 }
0854 static int wmi_char_open(struct inode *inode, struct file *filp)
0855 {
0856 const char *driver_name = filp->f_path.dentry->d_iname;
0857 struct wmi_block *wblock;
0858 struct wmi_block *next;
0859
0860 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
0861 if (!wblock->dev.dev.driver)
0862 continue;
0863 if (strcmp(driver_name, wblock->dev.dev.driver->name) == 0) {
0864 filp->private_data = wblock;
0865 break;
0866 }
0867 }
0868
0869 if (!filp->private_data)
0870 return -ENODEV;
0871
0872 return nonseekable_open(inode, filp);
0873 }
0874
0875 static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
0876 size_t length, loff_t *offset)
0877 {
0878 struct wmi_block *wblock = filp->private_data;
0879
0880 return simple_read_from_buffer(buffer, length, offset,
0881 &wblock->req_buf_size,
0882 sizeof(wblock->req_buf_size));
0883 }
0884
0885 static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
0886 {
0887 struct wmi_ioctl_buffer __user *input =
0888 (struct wmi_ioctl_buffer __user *) arg;
0889 struct wmi_block *wblock = filp->private_data;
0890 struct wmi_ioctl_buffer *buf;
0891 struct wmi_driver *wdriver;
0892 int ret;
0893
0894 if (_IOC_TYPE(cmd) != WMI_IOC)
0895 return -ENOTTY;
0896
0897
0898 if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
0899 return -EINVAL;
0900
0901 mutex_lock(&wblock->char_mutex);
0902 buf = wblock->handler_data;
0903 if (get_user(buf->length, &input->length)) {
0904 dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
0905 ret = -EFAULT;
0906 goto out_ioctl;
0907 }
0908
0909 if (buf->length < wblock->req_buf_size) {
0910 dev_err(&wblock->dev.dev,
0911 "Buffer %lld too small, need at least %lld\n",
0912 buf->length, wblock->req_buf_size);
0913 ret = -EINVAL;
0914 goto out_ioctl;
0915 }
0916
0917 if (buf->length > wblock->req_buf_size)
0918 dev_warn(&wblock->dev.dev,
0919 "Buffer %lld is bigger than required %lld\n",
0920 buf->length, wblock->req_buf_size);
0921
0922
0923 if (copy_from_user(buf, input, wblock->req_buf_size)) {
0924 dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
0925 wblock->req_buf_size);
0926 ret = -EFAULT;
0927 goto out_ioctl;
0928 }
0929
0930
0931 wdriver = drv_to_wdrv(wblock->dev.dev.driver);
0932 if (!try_module_get(wdriver->driver.owner)) {
0933 ret = -EBUSY;
0934 goto out_ioctl;
0935 }
0936 ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
0937 module_put(wdriver->driver.owner);
0938 if (ret)
0939 goto out_ioctl;
0940
0941
0942 if (copy_to_user(input, buf, wblock->req_buf_size)) {
0943 dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
0944 wblock->req_buf_size);
0945 ret = -EFAULT;
0946 }
0947
0948 out_ioctl:
0949 mutex_unlock(&wblock->char_mutex);
0950 return ret;
0951 }
0952
0953 static const struct file_operations wmi_fops = {
0954 .owner = THIS_MODULE,
0955 .read = wmi_char_read,
0956 .open = wmi_char_open,
0957 .unlocked_ioctl = wmi_ioctl,
0958 .compat_ioctl = compat_ptr_ioctl,
0959 };
0960
0961 static int wmi_dev_probe(struct device *dev)
0962 {
0963 struct wmi_block *wblock = dev_to_wblock(dev);
0964 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver);
0965 int ret = 0;
0966 char *buf;
0967
0968 if (ACPI_FAILURE(wmi_method_enable(wblock, true)))
0969 dev_warn(dev, "failed to enable device -- probing anyway\n");
0970
0971 if (wdriver->probe) {
0972 ret = wdriver->probe(dev_to_wdev(dev),
0973 find_guid_context(wblock, wdriver));
0974 if (ret != 0)
0975 goto probe_failure;
0976 }
0977
0978
0979 if (wdriver->filter_callback) {
0980
0981 if (!wblock->req_buf_size) {
0982 dev_err(&wblock->dev.dev,
0983 "Required buffer size not set\n");
0984 ret = -EINVAL;
0985 goto probe_failure;
0986 }
0987
0988 wblock->handler_data = kmalloc(wblock->req_buf_size,
0989 GFP_KERNEL);
0990 if (!wblock->handler_data) {
0991 ret = -ENOMEM;
0992 goto probe_failure;
0993 }
0994
0995 buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name);
0996 if (!buf) {
0997 ret = -ENOMEM;
0998 goto probe_string_failure;
0999 }
1000 wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
1001 wblock->char_dev.name = buf;
1002 wblock->char_dev.fops = &wmi_fops;
1003 wblock->char_dev.mode = 0444;
1004 ret = misc_register(&wblock->char_dev);
1005 if (ret) {
1006 dev_warn(dev, "failed to register char dev: %d\n", ret);
1007 ret = -ENOMEM;
1008 goto probe_misc_failure;
1009 }
1010 }
1011
1012 set_bit(WMI_PROBED, &wblock->flags);
1013 return 0;
1014
1015 probe_misc_failure:
1016 kfree(buf);
1017 probe_string_failure:
1018 kfree(wblock->handler_data);
1019 probe_failure:
1020 if (ACPI_FAILURE(wmi_method_enable(wblock, false)))
1021 dev_warn(dev, "failed to disable device\n");
1022 return ret;
1023 }
1024
1025 static void wmi_dev_remove(struct device *dev)
1026 {
1027 struct wmi_block *wblock = dev_to_wblock(dev);
1028 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver);
1029
1030 clear_bit(WMI_PROBED, &wblock->flags);
1031
1032 if (wdriver->filter_callback) {
1033 misc_deregister(&wblock->char_dev);
1034 kfree(wblock->char_dev.name);
1035 kfree(wblock->handler_data);
1036 }
1037
1038 if (wdriver->remove)
1039 wdriver->remove(dev_to_wdev(dev));
1040
1041 if (ACPI_FAILURE(wmi_method_enable(wblock, false)))
1042 dev_warn(dev, "failed to disable device\n");
1043 }
1044
1045 static struct class wmi_bus_class = {
1046 .name = "wmi_bus",
1047 };
1048
1049 static struct bus_type wmi_bus_type = {
1050 .name = "wmi",
1051 .dev_groups = wmi_groups,
1052 .match = wmi_dev_match,
1053 .uevent = wmi_dev_uevent,
1054 .probe = wmi_dev_probe,
1055 .remove = wmi_dev_remove,
1056 };
1057
1058 static const struct device_type wmi_type_event = {
1059 .name = "event",
1060 .groups = wmi_event_groups,
1061 .release = wmi_dev_release,
1062 };
1063
1064 static const struct device_type wmi_type_method = {
1065 .name = "method",
1066 .groups = wmi_method_groups,
1067 .release = wmi_dev_release,
1068 };
1069
1070 static const struct device_type wmi_type_data = {
1071 .name = "data",
1072 .groups = wmi_data_groups,
1073 .release = wmi_dev_release,
1074 };
1075
1076 static int wmi_create_device(struct device *wmi_bus_dev,
1077 struct wmi_block *wblock,
1078 struct acpi_device *device)
1079 {
1080 struct acpi_device_info *info;
1081 char method[WMI_ACPI_METHOD_NAME_SIZE];
1082 int result;
1083
1084 if (wblock->gblock.flags & ACPI_WMI_EVENT) {
1085 wblock->dev.dev.type = &wmi_type_event;
1086 goto out_init;
1087 }
1088
1089 if (wblock->gblock.flags & ACPI_WMI_METHOD) {
1090 wblock->dev.dev.type = &wmi_type_method;
1091 mutex_init(&wblock->char_mutex);
1092 goto out_init;
1093 }
1094
1095
1096
1097
1098
1099
1100 get_acpi_method_name(wblock, 'Q', method);
1101 result = get_subobj_info(device->handle, method, &info);
1102
1103 if (result) {
1104 dev_warn(wmi_bus_dev,
1105 "%s data block query control method not found\n",
1106 method);
1107 return result;
1108 }
1109
1110 wblock->dev.dev.type = &wmi_type_data;
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
1123 set_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags);
1124
1125 kfree(info);
1126
1127 get_acpi_method_name(wblock, 'S', method);
1128 result = get_subobj_info(device->handle, method, NULL);
1129
1130 if (result == 0)
1131 wblock->dev.setable = true;
1132
1133 out_init:
1134 wblock->dev.dev.bus = &wmi_bus_type;
1135 wblock->dev.dev.parent = wmi_bus_dev;
1136
1137 dev_set_name(&wblock->dev.dev, "%pUL", &wblock->gblock.guid);
1138
1139 device_initialize(&wblock->dev.dev);
1140
1141 return 0;
1142 }
1143
1144 static void wmi_free_devices(struct acpi_device *device)
1145 {
1146 struct wmi_block *wblock, *next;
1147
1148
1149 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1150 if (wblock->acpi_device == device) {
1151 list_del(&wblock->list);
1152 device_unregister(&wblock->dev.dev);
1153 }
1154 }
1155 }
1156
1157 static bool guid_already_parsed(struct acpi_device *device, const guid_t *guid)
1158 {
1159 struct wmi_block *wblock;
1160
1161 list_for_each_entry(wblock, &wmi_block_list, list) {
1162 if (guid_equal(&wblock->gblock.guid, guid)) {
1163
1164
1165
1166
1167
1168
1169 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
1170 guid, dev_name(&wblock->acpi_device->dev));
1171 return true;
1172 }
1173 }
1174
1175 return false;
1176 }
1177
1178
1179
1180
1181 static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
1182 {
1183 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1184 const struct guid_block *gblock;
1185 struct wmi_block *wblock, *next;
1186 union acpi_object *obj;
1187 acpi_status status;
1188 int retval = 0;
1189 u32 i, total;
1190
1191 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
1192 if (ACPI_FAILURE(status))
1193 return -ENXIO;
1194
1195 obj = out.pointer;
1196 if (!obj)
1197 return -ENXIO;
1198
1199 if (obj->type != ACPI_TYPE_BUFFER) {
1200 retval = -ENXIO;
1201 goto out_free_pointer;
1202 }
1203
1204 gblock = (const struct guid_block *)obj->buffer.pointer;
1205 total = obj->buffer.length / sizeof(struct guid_block);
1206
1207 for (i = 0; i < total; i++) {
1208 if (debug_dump_wdg)
1209 wmi_dump_wdg(&gblock[i]);
1210
1211
1212
1213
1214
1215
1216
1217 if (guid_already_parsed(device, &gblock[i].guid))
1218 continue;
1219
1220 wblock = kzalloc(sizeof(*wblock), GFP_KERNEL);
1221 if (!wblock) {
1222 retval = -ENOMEM;
1223 break;
1224 }
1225
1226 wblock->acpi_device = device;
1227 wblock->gblock = gblock[i];
1228
1229 retval = wmi_create_device(wmi_bus_dev, wblock, device);
1230 if (retval) {
1231 kfree(wblock);
1232 continue;
1233 }
1234
1235 list_add_tail(&wblock->list, &wmi_block_list);
1236
1237 if (debug_event) {
1238 wblock->handler = wmi_notify_debug;
1239 wmi_method_enable(wblock, true);
1240 }
1241 }
1242
1243
1244
1245
1246
1247 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
1248 if (wblock->acpi_device != device)
1249 continue;
1250
1251 retval = device_add(&wblock->dev.dev);
1252 if (retval) {
1253 dev_err(wmi_bus_dev, "failed to register %pUL\n",
1254 &wblock->gblock.guid);
1255 if (debug_event)
1256 wmi_method_enable(wblock, false);
1257 list_del(&wblock->list);
1258 put_device(&wblock->dev.dev);
1259 }
1260 }
1261
1262 out_free_pointer:
1263 kfree(out.pointer);
1264 return retval;
1265 }
1266
1267
1268
1269
1270
1271 static acpi_status
1272 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
1273 u32 bits, u64 *value,
1274 void *handler_context, void *region_context)
1275 {
1276 int result = 0, i = 0;
1277 u8 temp = 0;
1278
1279 if ((address > 0xFF) || !value)
1280 return AE_BAD_PARAMETER;
1281
1282 if (function != ACPI_READ && function != ACPI_WRITE)
1283 return AE_BAD_PARAMETER;
1284
1285 if (bits != 8)
1286 return AE_BAD_PARAMETER;
1287
1288 if (function == ACPI_READ) {
1289 result = ec_read(address, &temp);
1290 (*value) |= ((u64)temp) << i;
1291 } else {
1292 temp = 0xff & ((*value) >> i);
1293 result = ec_write(address, temp);
1294 }
1295
1296 switch (result) {
1297 case -EINVAL:
1298 return AE_BAD_PARAMETER;
1299 case -ENODEV:
1300 return AE_NOT_FOUND;
1301 case -ETIME:
1302 return AE_TIME;
1303 default:
1304 return AE_OK;
1305 }
1306 }
1307
1308 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
1309 void *context)
1310 {
1311 struct wmi_block *wblock = NULL, *iter;
1312
1313 list_for_each_entry(iter, &wmi_block_list, list) {
1314 struct guid_block *block = &iter->gblock;
1315
1316 if (iter->acpi_device->handle == handle &&
1317 (block->flags & ACPI_WMI_EVENT) &&
1318 (block->notify_id == event)) {
1319 wblock = iter;
1320 break;
1321 }
1322 }
1323
1324 if (!wblock)
1325 return;
1326
1327
1328 if (test_bit(WMI_PROBED, &wblock->flags) && wblock->dev.dev.driver) {
1329 struct wmi_driver *driver = drv_to_wdrv(wblock->dev.dev.driver);
1330 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
1331 acpi_status status;
1332
1333 if (!driver->no_notify_data) {
1334 status = get_event_data(wblock, &evdata);
1335 if (ACPI_FAILURE(status)) {
1336 dev_warn(&wblock->dev.dev, "failed to get event data\n");
1337 return;
1338 }
1339 }
1340
1341 if (driver->notify)
1342 driver->notify(&wblock->dev, evdata.pointer);
1343
1344 kfree(evdata.pointer);
1345 } else if (wblock->handler) {
1346
1347 wblock->handler(event, wblock->handler_data);
1348 }
1349
1350 if (debug_event)
1351 pr_info("DEBUG: GUID %pUL event 0x%02X\n", &wblock->gblock.guid, event);
1352
1353 acpi_bus_generate_netlink_event(
1354 wblock->acpi_device->pnp.device_class,
1355 dev_name(&wblock->dev.dev),
1356 event, 0);
1357 }
1358
1359 static int acpi_wmi_remove(struct platform_device *device)
1360 {
1361 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
1362
1363 acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
1364 acpi_wmi_notify_handler);
1365 acpi_remove_address_space_handler(acpi_device->handle,
1366 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
1367 wmi_free_devices(acpi_device);
1368 device_unregister(dev_get_drvdata(&device->dev));
1369
1370 return 0;
1371 }
1372
1373 static int acpi_wmi_probe(struct platform_device *device)
1374 {
1375 struct acpi_device *acpi_device;
1376 struct device *wmi_bus_dev;
1377 acpi_status status;
1378 int error;
1379
1380 acpi_device = ACPI_COMPANION(&device->dev);
1381 if (!acpi_device) {
1382 dev_err(&device->dev, "ACPI companion is missing\n");
1383 return -ENODEV;
1384 }
1385
1386 status = acpi_install_address_space_handler(acpi_device->handle,
1387 ACPI_ADR_SPACE_EC,
1388 &acpi_wmi_ec_space_handler,
1389 NULL, NULL);
1390 if (ACPI_FAILURE(status)) {
1391 dev_err(&device->dev, "Error installing EC region handler\n");
1392 return -ENODEV;
1393 }
1394
1395 status = acpi_install_notify_handler(acpi_device->handle,
1396 ACPI_ALL_NOTIFY,
1397 acpi_wmi_notify_handler,
1398 NULL);
1399 if (ACPI_FAILURE(status)) {
1400 dev_err(&device->dev, "Error installing notify handler\n");
1401 error = -ENODEV;
1402 goto err_remove_ec_handler;
1403 }
1404
1405 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
1406 NULL, "wmi_bus-%s", dev_name(&device->dev));
1407 if (IS_ERR(wmi_bus_dev)) {
1408 error = PTR_ERR(wmi_bus_dev);
1409 goto err_remove_notify_handler;
1410 }
1411 dev_set_drvdata(&device->dev, wmi_bus_dev);
1412
1413 error = parse_wdg(wmi_bus_dev, acpi_device);
1414 if (error) {
1415 pr_err("Failed to parse WDG method\n");
1416 goto err_remove_busdev;
1417 }
1418
1419 return 0;
1420
1421 err_remove_busdev:
1422 device_unregister(wmi_bus_dev);
1423
1424 err_remove_notify_handler:
1425 acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
1426 acpi_wmi_notify_handler);
1427
1428 err_remove_ec_handler:
1429 acpi_remove_address_space_handler(acpi_device->handle,
1430 ACPI_ADR_SPACE_EC,
1431 &acpi_wmi_ec_space_handler);
1432
1433 return error;
1434 }
1435
1436 int __must_check __wmi_driver_register(struct wmi_driver *driver,
1437 struct module *owner)
1438 {
1439 driver->driver.owner = owner;
1440 driver->driver.bus = &wmi_bus_type;
1441
1442 return driver_register(&driver->driver);
1443 }
1444 EXPORT_SYMBOL(__wmi_driver_register);
1445
1446 void wmi_driver_unregister(struct wmi_driver *driver)
1447 {
1448 driver_unregister(&driver->driver);
1449 }
1450 EXPORT_SYMBOL(wmi_driver_unregister);
1451
1452 static int __init acpi_wmi_init(void)
1453 {
1454 int error;
1455
1456 if (acpi_disabled)
1457 return -ENODEV;
1458
1459 error = class_register(&wmi_bus_class);
1460 if (error)
1461 return error;
1462
1463 error = bus_register(&wmi_bus_type);
1464 if (error)
1465 goto err_unreg_class;
1466
1467 error = platform_driver_register(&acpi_wmi_driver);
1468 if (error) {
1469 pr_err("Error loading mapper\n");
1470 goto err_unreg_bus;
1471 }
1472
1473 return 0;
1474
1475 err_unreg_bus:
1476 bus_unregister(&wmi_bus_type);
1477
1478 err_unreg_class:
1479 class_unregister(&wmi_bus_class);
1480
1481 return error;
1482 }
1483
1484 static void __exit acpi_wmi_exit(void)
1485 {
1486 platform_driver_unregister(&acpi_wmi_driver);
1487 bus_unregister(&wmi_bus_type);
1488 class_unregister(&wmi_bus_class);
1489 }
1490
1491 subsys_initcall_sync(acpi_wmi_init);
1492 module_exit(acpi_wmi_exit);