wmi-capdata.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Lenovo Capability Data WMI Data Block driver.
  4. *
  5. * Lenovo Capability Data provides information on tunable attributes used by
  6. * the "Other Mode" WMI interface.
  7. *
  8. * Capability Data 00 includes if the attribute is supported by the hardware,
  9. * and the default_value. All attributes are independent of thermal modes.
  10. *
  11. * Capability Data 01 includes if the attribute is supported by the hardware,
  12. * and the default_value, max_value, min_value, and step increment. Each
  13. * attribute has multiple pages, one for each of the thermal modes managed by
  14. * the Gamezone interface.
  15. *
  16. * Fan Test Data includes the max/min fan speed RPM for each fan. This is
  17. * reference data for self-test. If the fan is in good condition, it is capable
  18. * to spin faster than max RPM or slower than min RPM.
  19. *
  20. * Copyright (C) 2025 Derek J. Clark <derekjohn.clark@gmail.com>
  21. * - Initial implementation (formerly named lenovo-wmi-capdata01)
  22. *
  23. * Copyright (C) 2025 Rong Zhang <i@rong.moe>
  24. * - Unified implementation
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include <linux/acpi.h>
  28. #include <linux/bitfield.h>
  29. #include <linux/bug.h>
  30. #include <linux/cleanup.h>
  31. #include <linux/component.h>
  32. #include <linux/container_of.h>
  33. #include <linux/device.h>
  34. #include <linux/dev_printk.h>
  35. #include <linux/err.h>
  36. #include <linux/export.h>
  37. #include <linux/gfp_types.h>
  38. #include <linux/limits.h>
  39. #include <linux/module.h>
  40. #include <linux/mutex.h>
  41. #include <linux/mutex_types.h>
  42. #include <linux/notifier.h>
  43. #include <linux/overflow.h>
  44. #include <linux/stddef.h>
  45. #include <linux/types.h>
  46. #include <linux/wmi.h>
  47. #include "wmi-capdata.h"
  48. #define LENOVO_CAPABILITY_DATA_00_GUID "362A3AFE-3D96-4665-8530-96DAD5BB300E"
  49. #define LENOVO_CAPABILITY_DATA_01_GUID "7A8F5407-CB67-4D6E-B547-39B3BE018154"
  50. #define LENOVO_FAN_TEST_DATA_GUID "B642801B-3D21-45DE-90AE-6E86F164FB21"
  51. #define ACPI_AC_CLASS "ac_adapter"
  52. #define ACPI_AC_NOTIFY_STATUS 0x80
  53. #define LWMI_FEATURE_ID_FAN_TEST 0x05
  54. #define LWMI_ATTR_ID_FAN_TEST \
  55. (FIELD_PREP(LWMI_ATTR_DEV_ID_MASK, LWMI_DEVICE_ID_FAN) | \
  56. FIELD_PREP(LWMI_ATTR_FEAT_ID_MASK, LWMI_FEATURE_ID_FAN_TEST))
  57. enum lwmi_cd_type {
  58. LENOVO_CAPABILITY_DATA_00,
  59. LENOVO_CAPABILITY_DATA_01,
  60. LENOVO_FAN_TEST_DATA,
  61. CD_TYPE_NONE = -1,
  62. };
  63. #define LWMI_CD_TABLE_ITEM(_type) \
  64. [_type] = { \
  65. .name = #_type, \
  66. .type = _type, \
  67. }
  68. static const struct lwmi_cd_info {
  69. const char *name;
  70. enum lwmi_cd_type type;
  71. } lwmi_cd_table[] = {
  72. LWMI_CD_TABLE_ITEM(LENOVO_CAPABILITY_DATA_00),
  73. LWMI_CD_TABLE_ITEM(LENOVO_CAPABILITY_DATA_01),
  74. LWMI_CD_TABLE_ITEM(LENOVO_FAN_TEST_DATA),
  75. };
  76. struct lwmi_cd_priv {
  77. struct notifier_block acpi_nb; /* ACPI events */
  78. struct wmi_device *wdev;
  79. struct cd_list *list;
  80. /*
  81. * A capdata device may be a component master of another capdata device.
  82. * E.g., lenovo-wmi-other <-> capdata00 <-> capdata_fan
  83. * |- master |- component
  84. * |- sub-master
  85. * |- sub-component
  86. */
  87. struct lwmi_cd_sub_master_priv {
  88. struct device *master_dev;
  89. cd_list_cb_t master_cb;
  90. struct cd_list *sub_component_list; /* ERR_PTR(-ENODEV) implies no sub-component. */
  91. bool registered; /* Has the sub-master been registered? */
  92. } *sub_master;
  93. };
  94. struct cd_list {
  95. struct mutex list_mutex; /* list R/W mutex */
  96. enum lwmi_cd_type type;
  97. u8 count;
  98. union {
  99. DECLARE_FLEX_ARRAY(struct capdata00, cd00);
  100. DECLARE_FLEX_ARRAY(struct capdata01, cd01);
  101. DECLARE_FLEX_ARRAY(struct capdata_fan, cd_fan);
  102. };
  103. };
  104. static struct wmi_driver lwmi_cd_driver;
  105. /**
  106. * lwmi_cd_match() - Match rule for the master driver.
  107. * @dev: Pointer to the capability data parent device.
  108. * @type: Pointer to capability data type (enum lwmi_cd_type *) to match.
  109. *
  110. * Return: int.
  111. */
  112. static int lwmi_cd_match(struct device *dev, void *type)
  113. {
  114. struct lwmi_cd_priv *priv;
  115. if (dev->driver != &lwmi_cd_driver.driver)
  116. return false;
  117. priv = dev_get_drvdata(dev);
  118. return priv->list->type == *(enum lwmi_cd_type *)type;
  119. }
  120. /**
  121. * lwmi_cd_match_add_all() - Add all match rule for the master driver.
  122. * @master: Pointer to the master device.
  123. * @matchptr: Pointer to the returned component_match pointer.
  124. *
  125. * Adds all component matches to the list stored in @matchptr for the @master
  126. * device. @matchptr must be initialized to NULL.
  127. */
  128. void lwmi_cd_match_add_all(struct device *master, struct component_match **matchptr)
  129. {
  130. int i;
  131. if (WARN_ON(*matchptr))
  132. return;
  133. for (i = 0; i < ARRAY_SIZE(lwmi_cd_table); i++) {
  134. /* Skip sub-components. */
  135. if (lwmi_cd_table[i].type == LENOVO_FAN_TEST_DATA)
  136. continue;
  137. component_match_add(master, matchptr, lwmi_cd_match,
  138. (void *)&lwmi_cd_table[i].type);
  139. if (IS_ERR(*matchptr))
  140. return;
  141. }
  142. }
  143. EXPORT_SYMBOL_NS_GPL(lwmi_cd_match_add_all, "LENOVO_WMI_CAPDATA");
  144. /**
  145. * lwmi_cd_call_master_cb() - Call the master callback for the sub-component.
  146. * @priv: Pointer to the capability data private data.
  147. *
  148. * Call the master callback and pass the sub-component list to it if the
  149. * dependency chain (master <-> sub-master <-> sub-component) is complete.
  150. */
  151. static void lwmi_cd_call_master_cb(struct lwmi_cd_priv *priv)
  152. {
  153. struct cd_list *sub_component_list = priv->sub_master->sub_component_list;
  154. /*
  155. * Call the callback only if the dependency chain is ready:
  156. * - Binding between master and sub-master: fills master_dev and master_cb
  157. * - Binding between sub-master and sub-component: fills sub_component_list
  158. *
  159. * If a binding has been unbound before the other binding is bound, the
  160. * corresponding members filled by the former are guaranteed to be cleared.
  161. *
  162. * This function is only called in bind callbacks, and the component
  163. * framework guarantees bind/unbind callbacks may never execute
  164. * simultaneously, which implies that it's impossible to have a race
  165. * condition.
  166. *
  167. * Hence, this check is sufficient to ensure that the callback is called
  168. * at most once and with the correct state, without relying on a specific
  169. * sequence of binding establishment.
  170. */
  171. if (!sub_component_list ||
  172. !priv->sub_master->master_dev ||
  173. !priv->sub_master->master_cb)
  174. return;
  175. if (PTR_ERR(sub_component_list) == -ENODEV)
  176. sub_component_list = NULL;
  177. else if (WARN_ON(IS_ERR(sub_component_list)))
  178. return;
  179. priv->sub_master->master_cb(priv->sub_master->master_dev,
  180. sub_component_list);
  181. /*
  182. * Userspace may unbind a device from its driver and bind it again
  183. * through sysfs. Let's call this operation "reprobe" to distinguish it
  184. * from component "rebind".
  185. *
  186. * When reprobing capdata00/01 or the master device, the master device
  187. * is unbound from us with appropriate cleanup before we bind to it and
  188. * call master_cb. Everything is fine in this case.
  189. *
  190. * When reprobing capdata_fan, the master device has never been unbound
  191. * from us (hence no cleanup is done)[1], but we call master_cb the
  192. * second time. To solve this issue, we clear master_cb and master_dev
  193. * so we won't call master_cb twice while a binding is still complete.
  194. *
  195. * Note that we can't clear sub_component_list, otherwise reprobing
  196. * capdata01 or the master device causes master_cb to be never called
  197. * after we rebind to the master device.
  198. *
  199. * [1]: The master device does not need capdata_fan in run time, so
  200. * losing capdata_fan will not break the binding to the master device.
  201. */
  202. priv->sub_master->master_cb = NULL;
  203. priv->sub_master->master_dev = NULL;
  204. }
  205. /**
  206. * lwmi_cd_component_bind() - Bind component to master device.
  207. * @cd_dev: Pointer to the lenovo-wmi-capdata driver parent device.
  208. * @om_dev: Pointer to the lenovo-wmi-other driver parent device.
  209. * @data: lwmi_cd_binder object pointer used to return the capability data.
  210. *
  211. * On lenovo-wmi-other's master bind, provide a pointer to the local capdata
  212. * list. This is used to call lwmi_cd*_get_data to look up attribute data
  213. * from the lenovo-wmi-other driver.
  214. *
  215. * If cd_dev is a sub-master, try to call the master callback.
  216. *
  217. * Return: 0
  218. */
  219. static int lwmi_cd_component_bind(struct device *cd_dev,
  220. struct device *om_dev, void *data)
  221. {
  222. struct lwmi_cd_priv *priv = dev_get_drvdata(cd_dev);
  223. struct lwmi_cd_binder *binder = data;
  224. switch (priv->list->type) {
  225. case LENOVO_CAPABILITY_DATA_00:
  226. binder->cd00_list = priv->list;
  227. priv->sub_master->master_dev = om_dev;
  228. priv->sub_master->master_cb = binder->cd_fan_list_cb;
  229. lwmi_cd_call_master_cb(priv);
  230. break;
  231. case LENOVO_CAPABILITY_DATA_01:
  232. binder->cd01_list = priv->list;
  233. break;
  234. default:
  235. return -EINVAL;
  236. }
  237. return 0;
  238. }
  239. /**
  240. * lwmi_cd_component_unbind() - Unbind component to master device.
  241. * @cd_dev: Pointer to the lenovo-wmi-capdata driver parent device.
  242. * @om_dev: Pointer to the lenovo-wmi-other driver parent device.
  243. * @data: Unused.
  244. *
  245. * If cd_dev is a sub-master, clear the collected data from the master device to
  246. * prevent the binding establishment between the sub-master and the sub-
  247. * component (if it's about to happen) from calling the master callback.
  248. */
  249. static void lwmi_cd_component_unbind(struct device *cd_dev,
  250. struct device *om_dev, void *data)
  251. {
  252. struct lwmi_cd_priv *priv = dev_get_drvdata(cd_dev);
  253. switch (priv->list->type) {
  254. case LENOVO_CAPABILITY_DATA_00:
  255. priv->sub_master->master_dev = NULL;
  256. priv->sub_master->master_cb = NULL;
  257. return;
  258. default:
  259. return;
  260. }
  261. }
  262. static const struct component_ops lwmi_cd_component_ops = {
  263. .bind = lwmi_cd_component_bind,
  264. .unbind = lwmi_cd_component_unbind,
  265. };
  266. /**
  267. * lwmi_cd_sub_master_bind() - Bind sub-component of sub-master device
  268. * @dev: The sub-master capdata basic device.
  269. *
  270. * Call component_bind_all to bind the sub-component device to the sub-master
  271. * device. On success, collect the pointer to the sub-component list and try
  272. * to call the master callback.
  273. *
  274. * Return: 0 on success, or an error code.
  275. */
  276. static int lwmi_cd_sub_master_bind(struct device *dev)
  277. {
  278. struct lwmi_cd_priv *priv = dev_get_drvdata(dev);
  279. struct cd_list *sub_component_list;
  280. int ret;
  281. ret = component_bind_all(dev, &sub_component_list);
  282. if (ret)
  283. return ret;
  284. priv->sub_master->sub_component_list = sub_component_list;
  285. lwmi_cd_call_master_cb(priv);
  286. return 0;
  287. }
  288. /**
  289. * lwmi_cd_sub_master_unbind() - Unbind sub-component of sub-master device
  290. * @dev: The sub-master capdata basic device
  291. *
  292. * Clear the collected pointer to the sub-component list to prevent the binding
  293. * establishment between the sub-master and the sub-component (if it's about to
  294. * happen) from calling the master callback. Then, call component_unbind_all to
  295. * unbind the sub-component device from the sub-master device.
  296. */
  297. static void lwmi_cd_sub_master_unbind(struct device *dev)
  298. {
  299. struct lwmi_cd_priv *priv = dev_get_drvdata(dev);
  300. priv->sub_master->sub_component_list = NULL;
  301. component_unbind_all(dev, NULL);
  302. }
  303. static const struct component_master_ops lwmi_cd_sub_master_ops = {
  304. .bind = lwmi_cd_sub_master_bind,
  305. .unbind = lwmi_cd_sub_master_unbind,
  306. };
  307. /**
  308. * lwmi_cd_sub_master_add() - Register a sub-master with its sub-component
  309. * @priv: Pointer to the sub-master capdata device private data.
  310. * @sub_component_type: Type of the sub-component.
  311. *
  312. * Match the sub-component type and register the current capdata device as a
  313. * sub-master. If the given sub-component type is CD_TYPE_NONE, mark the sub-
  314. * component as non-existent without registering sub-master.
  315. *
  316. * Return: 0 on success, or an error code.
  317. */
  318. static int lwmi_cd_sub_master_add(struct lwmi_cd_priv *priv,
  319. enum lwmi_cd_type sub_component_type)
  320. {
  321. struct component_match *master_match = NULL;
  322. int ret;
  323. priv->sub_master = devm_kzalloc(&priv->wdev->dev, sizeof(*priv->sub_master), GFP_KERNEL);
  324. if (!priv->sub_master)
  325. return -ENOMEM;
  326. if (sub_component_type == CD_TYPE_NONE) {
  327. /* The master callback will be called with NULL on bind. */
  328. priv->sub_master->sub_component_list = ERR_PTR(-ENODEV);
  329. priv->sub_master->registered = false;
  330. return 0;
  331. }
  332. /*
  333. * lwmi_cd_match() needs a pointer to enum lwmi_cd_type, but on-stack
  334. * data cannot be used here. Steal one from lwmi_cd_table.
  335. */
  336. component_match_add(&priv->wdev->dev, &master_match, lwmi_cd_match,
  337. (void *)&lwmi_cd_table[sub_component_type].type);
  338. if (IS_ERR(master_match))
  339. return PTR_ERR(master_match);
  340. ret = component_master_add_with_match(&priv->wdev->dev, &lwmi_cd_sub_master_ops,
  341. master_match);
  342. if (ret)
  343. return ret;
  344. priv->sub_master->registered = true;
  345. return 0;
  346. }
  347. /**
  348. * lwmi_cd_sub_master_del() - Unregister a sub-master if it's registered
  349. * @priv: Pointer to the sub-master capdata device private data.
  350. */
  351. static void lwmi_cd_sub_master_del(struct lwmi_cd_priv *priv)
  352. {
  353. if (!priv->sub_master->registered)
  354. return;
  355. component_master_del(&priv->wdev->dev, &lwmi_cd_sub_master_ops);
  356. priv->sub_master->registered = false;
  357. }
  358. /**
  359. * lwmi_cd_sub_component_bind() - Bind sub-component to sub-master device.
  360. * @sc_dev: Pointer to the sub-component capdata parent device.
  361. * @sm_dev: Pointer to the sub-master capdata parent device.
  362. * @data: Pointer used to return the capability data list pointer.
  363. *
  364. * On sub-master's bind, provide a pointer to the local capdata list.
  365. * This is used by the sub-master to call the master callback.
  366. *
  367. * Return: 0
  368. */
  369. static int lwmi_cd_sub_component_bind(struct device *sc_dev,
  370. struct device *sm_dev, void *data)
  371. {
  372. struct lwmi_cd_priv *priv = dev_get_drvdata(sc_dev);
  373. struct cd_list **listp = data;
  374. *listp = priv->list;
  375. return 0;
  376. }
  377. static const struct component_ops lwmi_cd_sub_component_ops = {
  378. .bind = lwmi_cd_sub_component_bind,
  379. };
  380. /*
  381. * lwmi_cd*_get_data - Get the data of the specified attribute
  382. * @list: The lenovo-wmi-capdata pointer to its cd_list struct.
  383. * @attribute_id: The capdata attribute ID to be found.
  384. * @output: Pointer to a capdata* struct to return the data.
  385. *
  386. * Retrieves the capability data struct pointer for the given
  387. * attribute.
  388. *
  389. * Return: 0 on success, or -EINVAL.
  390. */
  391. #define DEF_LWMI_CDXX_GET_DATA(_cdxx, _cd_type, _output_t) \
  392. int lwmi_##_cdxx##_get_data(struct cd_list *list, u32 attribute_id, _output_t *output) \
  393. { \
  394. u8 idx; \
  395. \
  396. if (WARN_ON(list->type != _cd_type)) \
  397. return -EINVAL; \
  398. \
  399. guard(mutex)(&list->list_mutex); \
  400. for (idx = 0; idx < list->count; idx++) { \
  401. if (list->_cdxx[idx].id != attribute_id) \
  402. continue; \
  403. memcpy(output, &list->_cdxx[idx], sizeof(list->_cdxx[idx])); \
  404. return 0; \
  405. } \
  406. return -EINVAL; \
  407. }
  408. DEF_LWMI_CDXX_GET_DATA(cd00, LENOVO_CAPABILITY_DATA_00, struct capdata00);
  409. EXPORT_SYMBOL_NS_GPL(lwmi_cd00_get_data, "LENOVO_WMI_CAPDATA");
  410. DEF_LWMI_CDXX_GET_DATA(cd01, LENOVO_CAPABILITY_DATA_01, struct capdata01);
  411. EXPORT_SYMBOL_NS_GPL(lwmi_cd01_get_data, "LENOVO_WMI_CAPDATA");
  412. DEF_LWMI_CDXX_GET_DATA(cd_fan, LENOVO_FAN_TEST_DATA, struct capdata_fan);
  413. EXPORT_SYMBOL_NS_GPL(lwmi_cd_fan_get_data, "LENOVO_WMI_CAPDATA");
  414. /**
  415. * lwmi_cd_cache() - Cache all WMI data block information
  416. * @priv: lenovo-wmi-capdata driver data.
  417. *
  418. * Loop through each WMI data block and cache the data.
  419. *
  420. * Return: 0 on success, or an error.
  421. */
  422. static int lwmi_cd_cache(struct lwmi_cd_priv *priv)
  423. {
  424. size_t size;
  425. int idx;
  426. void *p;
  427. switch (priv->list->type) {
  428. case LENOVO_CAPABILITY_DATA_00:
  429. p = &priv->list->cd00[0];
  430. size = sizeof(priv->list->cd00[0]);
  431. break;
  432. case LENOVO_CAPABILITY_DATA_01:
  433. p = &priv->list->cd01[0];
  434. size = sizeof(priv->list->cd01[0]);
  435. break;
  436. case LENOVO_FAN_TEST_DATA:
  437. /* Done by lwmi_cd_alloc() => lwmi_cd_fan_list_alloc_cache(). */
  438. return 0;
  439. default:
  440. return -EINVAL;
  441. }
  442. guard(mutex)(&priv->list->list_mutex);
  443. for (idx = 0; idx < priv->list->count; idx++, p += size) {
  444. union acpi_object *ret_obj __free(kfree) = NULL;
  445. ret_obj = wmidev_block_query(priv->wdev, idx);
  446. if (!ret_obj)
  447. return -ENODEV;
  448. if (ret_obj->type != ACPI_TYPE_BUFFER ||
  449. ret_obj->buffer.length < size)
  450. continue;
  451. memcpy(p, ret_obj->buffer.pointer, size);
  452. }
  453. return 0;
  454. }
  455. /**
  456. * lwmi_cd_fan_list_alloc_cache() - Alloc and cache Fan Test Data list
  457. * @priv: lenovo-wmi-capdata driver data.
  458. * @listptr: Pointer to returned cd_list pointer.
  459. *
  460. * Return: count of fans found, or an error.
  461. */
  462. static int lwmi_cd_fan_list_alloc_cache(struct lwmi_cd_priv *priv, struct cd_list **listptr)
  463. {
  464. struct cd_list *list;
  465. size_t size;
  466. u32 count;
  467. int idx;
  468. /* Emit unaligned access to u8 buffer with __packed. */
  469. struct cd_fan_block {
  470. u32 nr;
  471. u32 data[]; /* id[nr], max_rpm[nr], min_rpm[nr] */
  472. } __packed * block;
  473. union acpi_object *ret_obj __free(kfree) = wmidev_block_query(priv->wdev, 0);
  474. if (!ret_obj)
  475. return -ENODEV;
  476. if (ret_obj->type == ACPI_TYPE_BUFFER) {
  477. block = (struct cd_fan_block *)ret_obj->buffer.pointer;
  478. size = ret_obj->buffer.length;
  479. count = size >= sizeof(*block) ? block->nr : 0;
  480. if (size < struct_size(block, data, count * 3)) {
  481. dev_warn(&priv->wdev->dev,
  482. "incomplete fan test data block: %zu < %zu, ignoring\n",
  483. size, struct_size(block, data, count * 3));
  484. count = 0;
  485. } else if (count > U8_MAX) {
  486. dev_warn(&priv->wdev->dev,
  487. "too many fans reported: %u > %u, truncating\n",
  488. count, U8_MAX);
  489. count = U8_MAX;
  490. }
  491. } else {
  492. /*
  493. * This is usually caused by a dummy ACPI method. Do not return an error
  494. * as failing to probe this device will result in sub-master device being
  495. * unbound. This behavior aligns with lwmi_cd_cache().
  496. */
  497. count = 0;
  498. }
  499. list = devm_kzalloc(&priv->wdev->dev, struct_size(list, cd_fan, count), GFP_KERNEL);
  500. if (!list)
  501. return -ENOMEM;
  502. for (idx = 0; idx < count; idx++) {
  503. /* Do not calculate array index using count, as it may be truncated. */
  504. list->cd_fan[idx] = (struct capdata_fan) {
  505. .id = block->data[idx],
  506. .max_rpm = block->data[idx + block->nr],
  507. .min_rpm = block->data[idx + (2 * block->nr)],
  508. };
  509. }
  510. *listptr = list;
  511. return count;
  512. }
  513. /**
  514. * lwmi_cd_alloc() - Allocate a cd_list struct in drvdata
  515. * @priv: lenovo-wmi-capdata driver data.
  516. * @type: The type of capability data.
  517. *
  518. * Allocate a cd_list struct large enough to contain data from all WMI data
  519. * blocks provided by the interface.
  520. *
  521. * Return: 0 on success, or an error.
  522. */
  523. static int lwmi_cd_alloc(struct lwmi_cd_priv *priv, enum lwmi_cd_type type)
  524. {
  525. struct cd_list *list;
  526. size_t list_size;
  527. int count, ret;
  528. count = wmidev_instance_count(priv->wdev);
  529. switch (type) {
  530. case LENOVO_CAPABILITY_DATA_00:
  531. list_size = struct_size(list, cd00, count);
  532. break;
  533. case LENOVO_CAPABILITY_DATA_01:
  534. list_size = struct_size(list, cd01, count);
  535. break;
  536. case LENOVO_FAN_TEST_DATA:
  537. count = lwmi_cd_fan_list_alloc_cache(priv, &list);
  538. if (count < 0)
  539. return count;
  540. goto got_list;
  541. default:
  542. return -EINVAL;
  543. }
  544. list = devm_kzalloc(&priv->wdev->dev, list_size, GFP_KERNEL);
  545. if (!list)
  546. return -ENOMEM;
  547. got_list:
  548. ret = devm_mutex_init(&priv->wdev->dev, &list->list_mutex);
  549. if (ret)
  550. return ret;
  551. list->type = type;
  552. list->count = count;
  553. priv->list = list;
  554. return 0;
  555. }
  556. /**
  557. * lwmi_cd_setup() - Cache all WMI data block information
  558. * @priv: lenovo-wmi-capdata driver data.
  559. * @type: The type of capability data.
  560. *
  561. * Allocate a cd_list struct large enough to contain data from all WMI data
  562. * blocks provided by the interface. Then loop through each data block and
  563. * cache the data.
  564. *
  565. * Return: 0 on success, or an error code.
  566. */
  567. static int lwmi_cd_setup(struct lwmi_cd_priv *priv, enum lwmi_cd_type type)
  568. {
  569. int ret;
  570. ret = lwmi_cd_alloc(priv, type);
  571. if (ret)
  572. return ret;
  573. return lwmi_cd_cache(priv);
  574. }
  575. /**
  576. * lwmi_cd01_notifier_call() - Call method for cd01 notifier.
  577. * block call chain.
  578. * @nb: The notifier_block registered to lenovo-wmi-events driver.
  579. * @action: Unused.
  580. * @data: The ACPI event.
  581. *
  582. * For LWMI_EVENT_THERMAL_MODE, set current_mode and notify platform_profile
  583. * of a change.
  584. *
  585. * Return: notifier_block status.
  586. */
  587. static int lwmi_cd01_notifier_call(struct notifier_block *nb, unsigned long action,
  588. void *data)
  589. {
  590. struct acpi_bus_event *event = data;
  591. struct lwmi_cd_priv *priv;
  592. int ret;
  593. if (strcmp(event->device_class, ACPI_AC_CLASS) != 0)
  594. return NOTIFY_DONE;
  595. priv = container_of(nb, struct lwmi_cd_priv, acpi_nb);
  596. switch (event->type) {
  597. case ACPI_AC_NOTIFY_STATUS:
  598. ret = lwmi_cd_cache(priv);
  599. if (ret)
  600. return NOTIFY_BAD;
  601. return NOTIFY_OK;
  602. default:
  603. return NOTIFY_DONE;
  604. }
  605. }
  606. /**
  607. * lwmi_cd01_unregister() - Unregister the cd01 ACPI notifier_block.
  608. * @data: The ACPI event notifier_block to unregister.
  609. */
  610. static void lwmi_cd01_unregister(void *data)
  611. {
  612. struct notifier_block *acpi_nb = data;
  613. unregister_acpi_notifier(acpi_nb);
  614. }
  615. static int lwmi_cd_probe(struct wmi_device *wdev, const void *context)
  616. {
  617. const struct lwmi_cd_info *info = context;
  618. struct lwmi_cd_priv *priv;
  619. int ret;
  620. if (!info)
  621. return -EINVAL;
  622. priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
  623. if (!priv)
  624. return -ENOMEM;
  625. priv->wdev = wdev;
  626. dev_set_drvdata(&wdev->dev, priv);
  627. ret = lwmi_cd_setup(priv, info->type);
  628. if (ret)
  629. goto out;
  630. switch (info->type) {
  631. case LENOVO_CAPABILITY_DATA_00: {
  632. enum lwmi_cd_type sub_component_type = LENOVO_FAN_TEST_DATA;
  633. struct capdata00 capdata00;
  634. ret = lwmi_cd00_get_data(priv->list, LWMI_ATTR_ID_FAN_TEST, &capdata00);
  635. if (ret || !(capdata00.supported & LWMI_SUPP_VALID)) {
  636. dev_dbg(&wdev->dev, "capdata00 declares no fan test support\n");
  637. sub_component_type = CD_TYPE_NONE;
  638. }
  639. /* Sub-master (capdata00) <-> sub-component (capdata_fan) */
  640. ret = lwmi_cd_sub_master_add(priv, sub_component_type);
  641. if (ret)
  642. goto out;
  643. /* Master (lenovo-wmi-other) <-> sub-master (capdata00) */
  644. ret = component_add(&wdev->dev, &lwmi_cd_component_ops);
  645. if (ret)
  646. lwmi_cd_sub_master_del(priv);
  647. goto out;
  648. }
  649. case LENOVO_CAPABILITY_DATA_01:
  650. priv->acpi_nb.notifier_call = lwmi_cd01_notifier_call;
  651. ret = register_acpi_notifier(&priv->acpi_nb);
  652. if (ret)
  653. goto out;
  654. ret = devm_add_action_or_reset(&wdev->dev, lwmi_cd01_unregister,
  655. &priv->acpi_nb);
  656. if (ret)
  657. goto out;
  658. ret = component_add(&wdev->dev, &lwmi_cd_component_ops);
  659. goto out;
  660. case LENOVO_FAN_TEST_DATA:
  661. ret = component_add(&wdev->dev, &lwmi_cd_sub_component_ops);
  662. goto out;
  663. default:
  664. return -EINVAL;
  665. }
  666. out:
  667. if (ret) {
  668. dev_err(&wdev->dev, "failed to register %s: %d\n",
  669. info->name, ret);
  670. } else {
  671. dev_dbg(&wdev->dev, "registered %s with %u items\n",
  672. info->name, priv->list->count);
  673. }
  674. return ret;
  675. }
  676. static void lwmi_cd_remove(struct wmi_device *wdev)
  677. {
  678. struct lwmi_cd_priv *priv = dev_get_drvdata(&wdev->dev);
  679. switch (priv->list->type) {
  680. case LENOVO_CAPABILITY_DATA_00:
  681. lwmi_cd_sub_master_del(priv);
  682. fallthrough;
  683. case LENOVO_CAPABILITY_DATA_01:
  684. component_del(&wdev->dev, &lwmi_cd_component_ops);
  685. break;
  686. case LENOVO_FAN_TEST_DATA:
  687. component_del(&wdev->dev, &lwmi_cd_sub_component_ops);
  688. break;
  689. default:
  690. WARN_ON(1);
  691. }
  692. }
  693. #define LWMI_CD_WDEV_ID(_type) \
  694. .guid_string = _type##_GUID, \
  695. .context = &lwmi_cd_table[_type],
  696. static const struct wmi_device_id lwmi_cd_id_table[] = {
  697. { LWMI_CD_WDEV_ID(LENOVO_CAPABILITY_DATA_00) },
  698. { LWMI_CD_WDEV_ID(LENOVO_CAPABILITY_DATA_01) },
  699. { LWMI_CD_WDEV_ID(LENOVO_FAN_TEST_DATA) },
  700. {}
  701. };
  702. static struct wmi_driver lwmi_cd_driver = {
  703. .driver = {
  704. .name = "lenovo_wmi_capdata",
  705. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  706. },
  707. .id_table = lwmi_cd_id_table,
  708. .probe = lwmi_cd_probe,
  709. .remove = lwmi_cd_remove,
  710. .no_singleton = true,
  711. };
  712. module_wmi_driver(lwmi_cd_driver);
  713. MODULE_DEVICE_TABLE(wmi, lwmi_cd_id_table);
  714. MODULE_AUTHOR("Derek J. Clark <derekjohn.clark@gmail.com>");
  715. MODULE_AUTHOR("Rong Zhang <i@rong.moe>");
  716. MODULE_DESCRIPTION("Lenovo Capability Data WMI Driver");
  717. MODULE_LICENSE("GPL");