msi-wmi-platform.c 12 KB

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