msi-wmi-platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux driver for WMI platform features on MSI notebooks.
  4. *
  5. * Copyright (C) 2024 Armin Wolf <W_Armin@gmx.de>
  6. */
  7. #define pr_format(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/acpi.h>
  9. #include <linux/bits.h>
  10. #include <linux/bitfield.h>
  11. #include <linux/cleanup.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/device.h>
  14. #include <linux/device/driver.h>
  15. #include <linux/errno.h>
  16. #include <linux/hwmon.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/printk.h>
  21. #include <linux/rwsem.h>
  22. #include <linux/types.h>
  23. #include <linux/wmi.h>
  24. #include <linux/unaligned.h>
  25. #define DRIVER_NAME "msi-wmi-platform"
  26. #define MSI_PLATFORM_GUID "ABBC0F6E-8EA1-11d1-00A0-C90629100000"
  27. #define MSI_WMI_PLATFORM_INTERFACE_VERSION 2
  28. #define MSI_PLATFORM_WMI_MAJOR_OFFSET 1
  29. #define MSI_PLATFORM_WMI_MINOR_OFFSET 2
  30. #define MSI_PLATFORM_EC_FLAGS_OFFSET 1
  31. #define MSI_PLATFORM_EC_MINOR_MASK GENMASK(3, 0)
  32. #define MSI_PLATFORM_EC_MAJOR_MASK GENMASK(5, 4)
  33. #define MSI_PLATFORM_EC_CHANGED_PAGE BIT(6)
  34. #define MSI_PLATFORM_EC_IS_TIGERLAKE BIT(7)
  35. #define MSI_PLATFORM_EC_VERSION_OFFSET 2
  36. static bool force;
  37. module_param_unsafe(force, bool, 0);
  38. MODULE_PARM_DESC(force, "Force loading without checking for supported WMI interface versions");
  39. enum msi_wmi_platform_method {
  40. MSI_PLATFORM_GET_PACKAGE = 0x01,
  41. MSI_PLATFORM_SET_PACKAGE = 0x02,
  42. MSI_PLATFORM_GET_EC = 0x03,
  43. MSI_PLATFORM_SET_EC = 0x04,
  44. MSI_PLATFORM_GET_BIOS = 0x05,
  45. MSI_PLATFORM_SET_BIOS = 0x06,
  46. MSI_PLATFORM_GET_SMBUS = 0x07,
  47. MSI_PLATFORM_SET_SMBUS = 0x08,
  48. MSI_PLATFORM_GET_MASTER_BATTERY = 0x09,
  49. MSI_PLATFORM_SET_MASTER_BATTERY = 0x0a,
  50. MSI_PLATFORM_GET_SLAVE_BATTERY = 0x0b,
  51. MSI_PLATFORM_SET_SLAVE_BATTERY = 0x0c,
  52. MSI_PLATFORM_GET_TEMPERATURE = 0x0d,
  53. MSI_PLATFORM_SET_TEMPERATURE = 0x0e,
  54. MSI_PLATFORM_GET_THERMAL = 0x0f,
  55. MSI_PLATFORM_SET_THERMAL = 0x10,
  56. MSI_PLATFORM_GET_FAN = 0x11,
  57. MSI_PLATFORM_SET_FAN = 0x12,
  58. MSI_PLATFORM_GET_DEVICE = 0x13,
  59. MSI_PLATFORM_SET_DEVICE = 0x14,
  60. MSI_PLATFORM_GET_POWER = 0x15,
  61. MSI_PLATFORM_SET_POWER = 0x16,
  62. MSI_PLATFORM_GET_DEBUG = 0x17,
  63. MSI_PLATFORM_SET_DEBUG = 0x18,
  64. MSI_PLATFORM_GET_AP = 0x19,
  65. MSI_PLATFORM_SET_AP = 0x1a,
  66. MSI_PLATFORM_GET_DATA = 0x1b,
  67. MSI_PLATFORM_SET_DATA = 0x1c,
  68. MSI_PLATFORM_GET_WMI = 0x1d,
  69. };
  70. struct msi_wmi_platform_data {
  71. struct wmi_device *wdev;
  72. struct mutex wmi_lock; /* Necessary when calling WMI methods */
  73. };
  74. struct msi_wmi_platform_debugfs_data {
  75. struct msi_wmi_platform_data *data;
  76. enum msi_wmi_platform_method method;
  77. struct rw_semaphore buffer_lock; /* Protects debugfs buffer */
  78. size_t length;
  79. u8 buffer[32];
  80. };
  81. static const char * const msi_wmi_platform_debugfs_names[] = {
  82. "get_package",
  83. "set_package",
  84. "get_ec",
  85. "set_ec",
  86. "get_bios",
  87. "set_bios",
  88. "get_smbus",
  89. "set_smbus",
  90. "get_master_battery",
  91. "set_master_battery",
  92. "get_slave_battery",
  93. "set_slave_battery",
  94. "get_temperature",
  95. "set_temperature",
  96. "get_thermal",
  97. "set_thermal",
  98. "get_fan",
  99. "set_fan",
  100. "get_device",
  101. "set_device",
  102. "get_power",
  103. "set_power",
  104. "get_debug",
  105. "set_debug",
  106. "get_ap",
  107. "set_ap",
  108. "get_data",
  109. "set_data",
  110. "get_wmi"
  111. };
  112. static int msi_wmi_platform_parse_buffer(union acpi_object *obj, u8 *output, size_t length)
  113. {
  114. if (obj->type != ACPI_TYPE_BUFFER)
  115. return -ENOMSG;
  116. if (obj->buffer.length != length)
  117. return -EPROTO;
  118. if (!obj->buffer.pointer[0])
  119. return -EIO;
  120. memcpy(output, obj->buffer.pointer, obj->buffer.length);
  121. return 0;
  122. }
  123. static int msi_wmi_platform_query(struct msi_wmi_platform_data *data,
  124. enum msi_wmi_platform_method method, u8 *input,
  125. size_t input_length, u8 *output, size_t output_length)
  126. {
  127. struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
  128. struct acpi_buffer in = {
  129. .length = input_length,
  130. .pointer = input
  131. };
  132. union acpi_object *obj;
  133. acpi_status status;
  134. int ret;
  135. if (!input_length || !output_length)
  136. return -EINVAL;
  137. /*
  138. * The ACPI control method responsible for handling the WMI method calls
  139. * is not thread-safe. Because of this we have to do the locking ourself.
  140. */
  141. scoped_guard(mutex, &data->wmi_lock) {
  142. status = wmidev_evaluate_method(data->wdev, 0x0, method, &in, &out);
  143. if (ACPI_FAILURE(status))
  144. return -EIO;
  145. }
  146. obj = out.pointer;
  147. if (!obj)
  148. return -ENODATA;
  149. ret = msi_wmi_platform_parse_buffer(obj, output, output_length);
  150. kfree(obj);
  151. return ret;
  152. }
  153. static umode_t msi_wmi_platform_is_visible(const void *drvdata, enum hwmon_sensor_types type,
  154. u32 attr, int channel)
  155. {
  156. return 0444;
  157. }
  158. static int msi_wmi_platform_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
  159. int channel, long *val)
  160. {
  161. struct msi_wmi_platform_data *data = dev_get_drvdata(dev);
  162. u8 input[32] = { 0 };
  163. u8 output[32];
  164. u16 value;
  165. int ret;
  166. ret = msi_wmi_platform_query(data, MSI_PLATFORM_GET_FAN, input, sizeof(input), output,
  167. sizeof(output));
  168. if (ret < 0)
  169. return ret;
  170. value = get_unaligned_be16(&output[channel * 2 + 1]);
  171. if (!value)
  172. *val = 0;
  173. else
  174. *val = 480000 / value;
  175. return 0;
  176. }
  177. static const struct hwmon_ops msi_wmi_platform_ops = {
  178. .is_visible = msi_wmi_platform_is_visible,
  179. .read = msi_wmi_platform_read,
  180. };
  181. static const struct hwmon_channel_info * const msi_wmi_platform_info[] = {
  182. HWMON_CHANNEL_INFO(fan,
  183. HWMON_F_INPUT,
  184. HWMON_F_INPUT,
  185. HWMON_F_INPUT,
  186. HWMON_F_INPUT
  187. ),
  188. NULL
  189. };
  190. static const struct hwmon_chip_info msi_wmi_platform_chip_info = {
  191. .ops = &msi_wmi_platform_ops,
  192. .info = msi_wmi_platform_info,
  193. };
  194. static ssize_t msi_wmi_platform_write(struct file *fp, const char __user *input, size_t length,
  195. loff_t *offset)
  196. {
  197. struct seq_file *seq = fp->private_data;
  198. struct msi_wmi_platform_debugfs_data *data = seq->private;
  199. u8 payload[32] = { };
  200. ssize_t ret;
  201. /* Do not allow partial writes */
  202. if (*offset != 0)
  203. return -EINVAL;
  204. /* Do not allow incomplete command buffers */
  205. if (length != data->length)
  206. return -EINVAL;
  207. ret = simple_write_to_buffer(payload, sizeof(payload), offset, input, length);
  208. if (ret < 0)
  209. return ret;
  210. down_write(&data->buffer_lock);
  211. ret = msi_wmi_platform_query(data->data, data->method, payload, data->length, data->buffer,
  212. data->length);
  213. up_write(&data->buffer_lock);
  214. if (ret < 0)
  215. return ret;
  216. return length;
  217. }
  218. static int msi_wmi_platform_show(struct seq_file *seq, void *p)
  219. {
  220. struct msi_wmi_platform_debugfs_data *data = seq->private;
  221. int ret;
  222. down_read(&data->buffer_lock);
  223. ret = seq_write(seq, data->buffer, data->length);
  224. up_read(&data->buffer_lock);
  225. return ret;
  226. }
  227. static int msi_wmi_platform_open(struct inode *inode, struct file *fp)
  228. {
  229. struct msi_wmi_platform_debugfs_data *data = inode->i_private;
  230. /* The seq_file uses the last byte of the buffer for detecting buffer overflows */
  231. return single_open_size(fp, msi_wmi_platform_show, data, data->length + 1);
  232. }
  233. static const struct file_operations msi_wmi_platform_debugfs_fops = {
  234. .owner = THIS_MODULE,
  235. .open = msi_wmi_platform_open,
  236. .read = seq_read,
  237. .write = msi_wmi_platform_write,
  238. .llseek = seq_lseek,
  239. .release = single_release,
  240. };
  241. static void msi_wmi_platform_debugfs_remove(void *data)
  242. {
  243. struct dentry *dir = data;
  244. debugfs_remove_recursive(dir);
  245. }
  246. static void msi_wmi_platform_debugfs_add(struct msi_wmi_platform_data *drvdata, struct dentry *dir,
  247. const char *name, enum msi_wmi_platform_method method)
  248. {
  249. struct msi_wmi_platform_debugfs_data *data;
  250. struct dentry *entry;
  251. data = devm_kzalloc(&drvdata->wdev->dev, sizeof(*data), GFP_KERNEL);
  252. if (!data)
  253. return;
  254. data->data = drvdata;
  255. data->method = method;
  256. init_rwsem(&data->buffer_lock);
  257. /* The ACPI firmware for now always requires a 32 byte input buffer due to
  258. * a peculiarity in how Windows handles the CreateByteField() ACPI operator.
  259. */
  260. data->length = 32;
  261. entry = debugfs_create_file(name, 0600, dir, data, &msi_wmi_platform_debugfs_fops);
  262. if (IS_ERR(entry))
  263. devm_kfree(&drvdata->wdev->dev, data);
  264. }
  265. static void msi_wmi_platform_debugfs_init(struct msi_wmi_platform_data *data)
  266. {
  267. struct dentry *dir;
  268. char dir_name[64];
  269. int ret, method;
  270. scnprintf(dir_name, ARRAY_SIZE(dir_name), "%s-%s", DRIVER_NAME, dev_name(&data->wdev->dev));
  271. dir = debugfs_create_dir(dir_name, NULL);
  272. if (IS_ERR(dir))
  273. return;
  274. ret = devm_add_action_or_reset(&data->wdev->dev, msi_wmi_platform_debugfs_remove, dir);
  275. if (ret < 0)
  276. return;
  277. for (method = MSI_PLATFORM_GET_PACKAGE; method <= MSI_PLATFORM_GET_WMI; method++)
  278. msi_wmi_platform_debugfs_add(data, dir, msi_wmi_platform_debugfs_names[method - 1],
  279. method);
  280. }
  281. static int msi_wmi_platform_hwmon_init(struct msi_wmi_platform_data *data)
  282. {
  283. struct device *hdev;
  284. hdev = devm_hwmon_device_register_with_info(&data->wdev->dev, "msi_wmi_platform", data,
  285. &msi_wmi_platform_chip_info, NULL);
  286. return PTR_ERR_OR_ZERO(hdev);
  287. }
  288. static int msi_wmi_platform_ec_init(struct msi_wmi_platform_data *data)
  289. {
  290. u8 input[32] = { 0 };
  291. u8 output[32];
  292. u8 flags;
  293. int ret;
  294. ret = msi_wmi_platform_query(data, MSI_PLATFORM_GET_EC, input, sizeof(input), output,
  295. sizeof(output));
  296. if (ret < 0)
  297. return ret;
  298. flags = output[MSI_PLATFORM_EC_FLAGS_OFFSET];
  299. dev_dbg(&data->wdev->dev, "EC RAM version %lu.%lu\n",
  300. FIELD_GET(MSI_PLATFORM_EC_MAJOR_MASK, flags),
  301. FIELD_GET(MSI_PLATFORM_EC_MINOR_MASK, flags));
  302. dev_dbg(&data->wdev->dev, "EC firmware version %.28s\n",
  303. &output[MSI_PLATFORM_EC_VERSION_OFFSET]);
  304. if (!(flags & MSI_PLATFORM_EC_IS_TIGERLAKE)) {
  305. if (!force)
  306. return -ENODEV;
  307. dev_warn(&data->wdev->dev, "Loading on a non-Tigerlake platform\n");
  308. }
  309. return 0;
  310. }
  311. static int msi_wmi_platform_init(struct msi_wmi_platform_data *data)
  312. {
  313. u8 input[32] = { 0 };
  314. u8 output[32];
  315. int ret;
  316. ret = msi_wmi_platform_query(data, MSI_PLATFORM_GET_WMI, input, sizeof(input), output,
  317. sizeof(output));
  318. if (ret < 0)
  319. return ret;
  320. dev_dbg(&data->wdev->dev, "WMI interface version %u.%u\n",
  321. output[MSI_PLATFORM_WMI_MAJOR_OFFSET],
  322. output[MSI_PLATFORM_WMI_MINOR_OFFSET]);
  323. if (output[MSI_PLATFORM_WMI_MAJOR_OFFSET] != MSI_WMI_PLATFORM_INTERFACE_VERSION) {
  324. if (!force)
  325. return -ENODEV;
  326. dev_warn(&data->wdev->dev,
  327. "Loading despite unsupported WMI interface version (%u.%u)\n",
  328. output[MSI_PLATFORM_WMI_MAJOR_OFFSET],
  329. output[MSI_PLATFORM_WMI_MINOR_OFFSET]);
  330. }
  331. return 0;
  332. }
  333. static int msi_wmi_platform_probe(struct wmi_device *wdev, const void *context)
  334. {
  335. struct msi_wmi_platform_data *data;
  336. int ret;
  337. data = devm_kzalloc(&wdev->dev, sizeof(*data), GFP_KERNEL);
  338. if (!data)
  339. return -ENOMEM;
  340. data->wdev = wdev;
  341. dev_set_drvdata(&wdev->dev, data);
  342. ret = devm_mutex_init(&wdev->dev, &data->wmi_lock);
  343. if (ret < 0)
  344. return ret;
  345. ret = msi_wmi_platform_init(data);
  346. if (ret < 0)
  347. return ret;
  348. ret = msi_wmi_platform_ec_init(data);
  349. if (ret < 0)
  350. return ret;
  351. msi_wmi_platform_debugfs_init(data);
  352. return msi_wmi_platform_hwmon_init(data);
  353. }
  354. static const struct wmi_device_id msi_wmi_platform_id_table[] = {
  355. { MSI_PLATFORM_GUID, NULL },
  356. { }
  357. };
  358. MODULE_DEVICE_TABLE(wmi, msi_wmi_platform_id_table);
  359. static struct wmi_driver msi_wmi_platform_driver = {
  360. .driver = {
  361. .name = DRIVER_NAME,
  362. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  363. },
  364. .id_table = msi_wmi_platform_id_table,
  365. .probe = msi_wmi_platform_probe,
  366. .no_singleton = true,
  367. };
  368. module_wmi_driver(msi_wmi_platform_driver);
  369. MODULE_AUTHOR("Armin Wolf <W_Armin@gmx.de>");
  370. MODULE_DESCRIPTION("MSI WMI platform features");
  371. MODULE_LICENSE("GPL");