1 /*
2  * WMI Thunderbolt driver
3  *
4  * Copyright (C) 2017 Dell Inc. All Rights Reserved.
5  *
6  *  This program is free software; you can redistribute it and/or modify it
7  *  under the terms of the GNU General Public License version 2 as published
8  *  by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  */
15 
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 
18 #include <linux/acpi.h>
19 #include <linux/device.h>
20 #include <linux/fs.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/string.h>
24 #include <linux/sysfs.h>
25 #include <linux/types.h>
26 #include <linux/wmi.h>
27 
28 #define INTEL_WMI_THUNDERBOLT_GUID "86CCFD48-205E-4A77-9C48-2021CBEDE341"
29 
force_power_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)30 static ssize_t force_power_store(struct device *dev,
31 				 struct device_attribute *attr,
32 				 const char *buf, size_t count)
33 {
34 	struct acpi_buffer input;
35 	acpi_status status;
36 	u8 mode;
37 
38 	input.length = sizeof(u8);
39 	input.pointer = &mode;
40 	mode = hex_to_bin(buf[0]);
41 	if (mode == 0 || mode == 1) {
42 		status = wmi_evaluate_method(INTEL_WMI_THUNDERBOLT_GUID, 0, 1,
43 					     &input, NULL);
44 		if (ACPI_FAILURE(status))
45 			return -ENODEV;
46 	} else {
47 		return -EINVAL;
48 	}
49 	return count;
50 }
51 
52 static DEVICE_ATTR_WO(force_power);
53 
54 static struct attribute *tbt_attrs[] = {
55 	&dev_attr_force_power.attr,
56 	NULL
57 };
58 
59 static const struct attribute_group tbt_attribute_group = {
60 	.attrs = tbt_attrs,
61 };
62 
intel_wmi_thunderbolt_probe(struct wmi_device * wdev)63 static int intel_wmi_thunderbolt_probe(struct wmi_device *wdev)
64 {
65 	int ret;
66 
67 	ret = sysfs_create_group(&wdev->dev.kobj, &tbt_attribute_group);
68 	kobject_uevent(&wdev->dev.kobj, KOBJ_CHANGE);
69 	return ret;
70 }
71 
intel_wmi_thunderbolt_remove(struct wmi_device * wdev)72 static int intel_wmi_thunderbolt_remove(struct wmi_device *wdev)
73 {
74 	sysfs_remove_group(&wdev->dev.kobj, &tbt_attribute_group);
75 	kobject_uevent(&wdev->dev.kobj, KOBJ_CHANGE);
76 	return 0;
77 }
78 
79 static const struct wmi_device_id intel_wmi_thunderbolt_id_table[] = {
80 	{ .guid_string = INTEL_WMI_THUNDERBOLT_GUID },
81 	{ },
82 };
83 
84 static struct wmi_driver intel_wmi_thunderbolt_driver = {
85 	.driver = {
86 		.name = "intel-wmi-thunderbolt",
87 	},
88 	.probe = intel_wmi_thunderbolt_probe,
89 	.remove = intel_wmi_thunderbolt_remove,
90 	.id_table = intel_wmi_thunderbolt_id_table,
91 };
92 
93 module_wmi_driver(intel_wmi_thunderbolt_driver);
94 
95 MODULE_ALIAS("wmi:" INTEL_WMI_THUNDERBOLT_GUID);
96 MODULE_AUTHOR("Mario Limonciello <mario.limonciello@dell.com>");
97 MODULE_DESCRIPTION("Intel WMI Thunderbolt force power driver");
98 MODULE_LICENSE("GPL");
99