core.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ACPI-WMI mapping driver
  4. *
  5. * Copyright (C) 2007-2008 Carlos Corbacho <carlos@strangeworlds.co.uk>
  6. *
  7. * GUID parsing code from ldm.c is:
  8. * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
  9. * Copyright (c) 2001-2007 Anton Altaparmakov
  10. * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
  11. *
  12. * WMI bus infrastructure by Andrew Lutomirski and Darren Hart:
  13. * Copyright (C) 2015 Andrew Lutomirski
  14. * Copyright (C) 2017 VMware, Inc. All Rights Reserved.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/acpi.h>
  18. #include <linux/bits.h>
  19. #include <linux/build_bug.h>
  20. #include <linux/device.h>
  21. #include <linux/idr.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/limits.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/rwsem.h>
  28. #include <linux/slab.h>
  29. #include <linux/sysfs.h>
  30. #include <linux/types.h>
  31. #include <linux/uuid.h>
  32. #include <linux/wmi.h>
  33. #include <linux/fs.h>
  34. #include "internal.h"
  35. MODULE_AUTHOR("Carlos Corbacho");
  36. MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
  37. MODULE_LICENSE("GPL");
  38. struct guid_block {
  39. guid_t guid;
  40. union {
  41. char object_id[2];
  42. struct {
  43. unsigned char notify_id;
  44. unsigned char reserved;
  45. };
  46. };
  47. u8 instance_count;
  48. u8 flags;
  49. } __packed;
  50. static_assert(sizeof(typeof_member(struct guid_block, guid)) == 16);
  51. static_assert(sizeof(struct guid_block) == 20);
  52. static_assert(__alignof__(struct guid_block) == 1);
  53. enum { /* wmi_block flags */
  54. WMI_READ_TAKES_NO_ARGS,
  55. WMI_GUID_DUPLICATED,
  56. WMI_NO_EVENT_DATA,
  57. };
  58. struct wmi_block {
  59. struct wmi_device dev;
  60. struct guid_block gblock;
  61. struct acpi_device *acpi_device;
  62. struct rw_semaphore notify_lock; /* Protects notify callback add/remove */
  63. wmi_notify_handler handler;
  64. void *handler_data;
  65. bool driver_ready;
  66. unsigned long flags;
  67. };
  68. struct wmi_guid_count_context {
  69. const guid_t *guid;
  70. int count;
  71. };
  72. static DEFINE_IDA(wmi_ida);
  73. /*
  74. * If the GUID data block is marked as expensive, we must enable and
  75. * explicitily disable data collection.
  76. */
  77. #define ACPI_WMI_EXPENSIVE BIT(0)
  78. #define ACPI_WMI_METHOD BIT(1) /* GUID is a method */
  79. #define ACPI_WMI_STRING BIT(2) /* GUID takes & returns a string */
  80. #define ACPI_WMI_EVENT BIT(3) /* GUID is an event */
  81. static const struct acpi_device_id wmi_device_ids[] = {
  82. {"PNP0C14", 0},
  83. {"pnp0c14", 0},
  84. { }
  85. };
  86. MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
  87. #define dev_to_wblock(__dev) container_of_const(__dev, struct wmi_block, dev.dev)
  88. /*
  89. * GUID parsing functions
  90. */
  91. static bool guid_parse_and_compare(const char *string, const guid_t *guid)
  92. {
  93. guid_t guid_input;
  94. if (guid_parse(string, &guid_input))
  95. return false;
  96. return guid_equal(&guid_input, guid);
  97. }
  98. static const void *find_guid_context(struct wmi_block *wblock,
  99. struct wmi_driver *wdriver)
  100. {
  101. const struct wmi_device_id *id;
  102. id = wdriver->id_table;
  103. if (!id)
  104. return NULL;
  105. while (*id->guid_string) {
  106. if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid))
  107. return id->context;
  108. id++;
  109. }
  110. return NULL;
  111. }
  112. #define WMI_ACPI_METHOD_NAME_SIZE 5
  113. static inline void get_acpi_method_name(const struct wmi_block *wblock,
  114. const char method,
  115. char buffer[static WMI_ACPI_METHOD_NAME_SIZE])
  116. {
  117. static_assert(ARRAY_SIZE(wblock->gblock.object_id) == 2);
  118. static_assert(WMI_ACPI_METHOD_NAME_SIZE >= 5);
  119. buffer[0] = 'W';
  120. buffer[1] = method;
  121. buffer[2] = wblock->gblock.object_id[0];
  122. buffer[3] = wblock->gblock.object_id[1];
  123. buffer[4] = '\0';
  124. }
  125. static int wmidev_match_guid(struct device *dev, const void *data)
  126. {
  127. struct wmi_block *wblock = dev_to_wblock(dev);
  128. const guid_t *guid = data;
  129. /* Legacy GUID-based functions are restricted to only see
  130. * a single WMI device for each GUID.
  131. */
  132. if (test_bit(WMI_GUID_DUPLICATED, &wblock->flags))
  133. return 0;
  134. if (guid_equal(guid, &wblock->gblock.guid))
  135. return 1;
  136. return 0;
  137. }
  138. static const struct bus_type wmi_bus_type;
  139. static const struct device_type wmi_type_event;
  140. static const struct device_type wmi_type_method;
  141. static int wmi_device_enable(struct wmi_device *wdev, bool enable)
  142. {
  143. struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
  144. char method[WMI_ACPI_METHOD_NAME_SIZE];
  145. acpi_handle handle;
  146. acpi_status status;
  147. if (wblock->dev.dev.type == &wmi_type_method)
  148. return 0;
  149. if (wblock->dev.dev.type == &wmi_type_event) {
  150. /*
  151. * Windows always enables/disables WMI events, even when they are
  152. * not marked as being expensive. We follow this behavior for
  153. * compatibility reasons.
  154. */
  155. snprintf(method, sizeof(method), "WE%02X", wblock->gblock.notify_id);
  156. } else {
  157. if (!(wblock->gblock.flags & ACPI_WMI_EXPENSIVE))
  158. return 0;
  159. get_acpi_method_name(wblock, 'C', method);
  160. }
  161. /*
  162. * Not all WMI devices marked as expensive actually implement the
  163. * necessary ACPI method. Ignore this missing ACPI method to match
  164. * the behaviour of the Windows driver.
  165. */
  166. status = acpi_get_handle(wblock->acpi_device->handle, method, &handle);
  167. if (ACPI_FAILURE(status))
  168. return 0;
  169. status = acpi_execute_simple_method(handle, NULL, enable);
  170. if (ACPI_FAILURE(status))
  171. return -EIO;
  172. return 0;
  173. }
  174. static struct wmi_device *wmi_find_device_by_guid(const char *guid_string)
  175. {
  176. struct device *dev;
  177. guid_t guid;
  178. int ret;
  179. ret = guid_parse(guid_string, &guid);
  180. if (ret < 0)
  181. return ERR_PTR(ret);
  182. dev = bus_find_device(&wmi_bus_type, NULL, &guid, wmidev_match_guid);
  183. if (!dev)
  184. return ERR_PTR(-ENODEV);
  185. return to_wmi_device(dev);
  186. }
  187. static void wmi_device_put(struct wmi_device *wdev)
  188. {
  189. put_device(&wdev->dev);
  190. }
  191. /*
  192. * Exported WMI functions
  193. */
  194. /**
  195. * wmi_instance_count - Get number of WMI object instances
  196. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  197. *
  198. * Get the number of WMI object instances.
  199. *
  200. * Returns: Number of WMI object instances or negative error code.
  201. */
  202. int wmi_instance_count(const char *guid_string)
  203. {
  204. struct wmi_device *wdev;
  205. int ret;
  206. wdev = wmi_find_device_by_guid(guid_string);
  207. if (IS_ERR(wdev))
  208. return PTR_ERR(wdev);
  209. ret = wmidev_instance_count(wdev);
  210. wmi_device_put(wdev);
  211. return ret;
  212. }
  213. EXPORT_SYMBOL_GPL(wmi_instance_count);
  214. /**
  215. * wmidev_instance_count - Get number of WMI object instances
  216. * @wdev: A wmi bus device from a driver
  217. *
  218. * Get the number of WMI object instances.
  219. *
  220. * Returns: Number of WMI object instances.
  221. */
  222. u8 wmidev_instance_count(struct wmi_device *wdev)
  223. {
  224. struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
  225. return wblock->gblock.instance_count;
  226. }
  227. EXPORT_SYMBOL_GPL(wmidev_instance_count);
  228. /**
  229. * wmi_evaluate_method - Evaluate a WMI method (deprecated)
  230. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  231. * @instance: Instance index
  232. * @method_id: Method ID to call
  233. * @in: Mandatory buffer containing input for the method call
  234. * @out: Empty buffer to return the method results
  235. *
  236. * Call an ACPI-WMI method, the caller must free @out.
  237. *
  238. * Return: acpi_status signaling success or error.
  239. */
  240. acpi_status wmi_evaluate_method(const char *guid_string, u8 instance, u32 method_id,
  241. const struct acpi_buffer *in, struct acpi_buffer *out)
  242. {
  243. struct wmi_device *wdev;
  244. acpi_status status;
  245. wdev = wmi_find_device_by_guid(guid_string);
  246. if (IS_ERR(wdev))
  247. return AE_ERROR;
  248. status = wmidev_evaluate_method(wdev, instance, method_id, in, out);
  249. wmi_device_put(wdev);
  250. return status;
  251. }
  252. EXPORT_SYMBOL_GPL(wmi_evaluate_method);
  253. /**
  254. * wmidev_evaluate_method - Evaluate a WMI method (deprecated)
  255. * @wdev: A wmi bus device from a driver
  256. * @instance: Instance index
  257. * @method_id: Method ID to call
  258. * @in: Mandatory buffer containing input for the method call
  259. * @out: Empty buffer to return the method results
  260. *
  261. * Call an ACPI-WMI method, the caller must free @out.
  262. *
  263. * Return: acpi_status signaling success or error.
  264. */
  265. acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance, u32 method_id,
  266. const struct acpi_buffer *in, struct acpi_buffer *out)
  267. {
  268. struct guid_block *block;
  269. struct wmi_block *wblock;
  270. acpi_handle handle;
  271. struct acpi_object_list input;
  272. union acpi_object params[3];
  273. char method[WMI_ACPI_METHOD_NAME_SIZE];
  274. wblock = container_of(wdev, struct wmi_block, dev);
  275. block = &wblock->gblock;
  276. handle = wblock->acpi_device->handle;
  277. if (!in)
  278. return AE_BAD_DATA;
  279. if (!(block->flags & ACPI_WMI_METHOD))
  280. return AE_BAD_DATA;
  281. if (block->instance_count <= instance)
  282. return AE_BAD_PARAMETER;
  283. input.count = 3;
  284. input.pointer = params;
  285. params[0].type = ACPI_TYPE_INTEGER;
  286. params[0].integer.value = instance;
  287. params[1].type = ACPI_TYPE_INTEGER;
  288. params[1].integer.value = method_id;
  289. if (wblock->gblock.flags & ACPI_WMI_STRING) {
  290. params[2].type = ACPI_TYPE_STRING;
  291. params[2].string.length = in->length;
  292. params[2].string.pointer = in->pointer;
  293. } else {
  294. params[2].type = ACPI_TYPE_BUFFER;
  295. params[2].buffer.length = in->length;
  296. params[2].buffer.pointer = in->pointer;
  297. }
  298. get_acpi_method_name(wblock, 'M', method);
  299. return acpi_evaluate_object(handle, method, &input, out);
  300. }
  301. EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
  302. /**
  303. * wmidev_invoke_method - Invoke a WMI method
  304. * @wdev: A wmi bus device from a driver
  305. * @instance: Instance index
  306. * @method_id: Method ID to call
  307. * @in: Mandatory WMI buffer containing input for the method call
  308. * @out: Optional WMI buffer to return the method results
  309. *
  310. * Invoke a WMI method, the caller must free the resulting data inside @out.
  311. * Said data is guaranteed to be aligned on a 8-byte boundary.
  312. *
  313. * Return: 0 on success or negative error code on failure.
  314. */
  315. int wmidev_invoke_method(struct wmi_device *wdev, u8 instance, u32 method_id,
  316. const struct wmi_buffer *in, struct wmi_buffer *out)
  317. {
  318. struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
  319. struct acpi_buffer aout = { ACPI_ALLOCATE_BUFFER, NULL };
  320. struct acpi_buffer ain;
  321. union acpi_object *obj;
  322. acpi_status status;
  323. int ret;
  324. if (wblock->gblock.flags & ACPI_WMI_STRING) {
  325. ret = wmi_marshal_string(in, &ain);
  326. if (ret < 0)
  327. return ret;
  328. } else {
  329. if (in->length > U32_MAX)
  330. return -E2BIG;
  331. ain.length = in->length;
  332. ain.pointer = in->data;
  333. }
  334. if (out)
  335. status = wmidev_evaluate_method(wdev, instance, method_id, &ain, &aout);
  336. else
  337. status = wmidev_evaluate_method(wdev, instance, method_id, &ain, NULL);
  338. if (wblock->gblock.flags & ACPI_WMI_STRING)
  339. kfree(ain.pointer);
  340. if (ACPI_FAILURE(status))
  341. return -EIO;
  342. if (!out)
  343. return 0;
  344. obj = aout.pointer;
  345. if (!obj) {
  346. out->length = 0;
  347. out->data = ZERO_SIZE_PTR;
  348. return 0;
  349. }
  350. ret = wmi_unmarshal_acpi_object(obj, out);
  351. kfree(obj);
  352. return ret;
  353. }
  354. EXPORT_SYMBOL_GPL(wmidev_invoke_method);
  355. static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
  356. struct acpi_buffer *out)
  357. {
  358. struct guid_block *block;
  359. acpi_handle handle;
  360. struct acpi_object_list input;
  361. union acpi_object wq_params[1];
  362. char method[WMI_ACPI_METHOD_NAME_SIZE];
  363. if (!out)
  364. return AE_BAD_PARAMETER;
  365. block = &wblock->gblock;
  366. handle = wblock->acpi_device->handle;
  367. if (block->instance_count <= instance)
  368. return AE_BAD_PARAMETER;
  369. /* Check GUID is a data block */
  370. if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
  371. return AE_ERROR;
  372. input.count = 1;
  373. input.pointer = wq_params;
  374. wq_params[0].type = ACPI_TYPE_INTEGER;
  375. wq_params[0].integer.value = instance;
  376. if (instance == 0 && test_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags))
  377. input.count = 0;
  378. get_acpi_method_name(wblock, 'Q', method);
  379. return acpi_evaluate_object(handle, method, &input, out);
  380. }
  381. /**
  382. * wmi_query_block - Return contents of a WMI block (deprecated)
  383. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  384. * @instance: Instance index
  385. * @out: Empty buffer to return the contents of the data block to
  386. *
  387. * Query a ACPI-WMI block, the caller must free @out.
  388. *
  389. * Return: ACPI object containing the content of the WMI block.
  390. */
  391. acpi_status wmi_query_block(const char *guid_string, u8 instance,
  392. struct acpi_buffer *out)
  393. {
  394. struct wmi_block *wblock;
  395. struct wmi_device *wdev;
  396. acpi_status status;
  397. wdev = wmi_find_device_by_guid(guid_string);
  398. if (IS_ERR(wdev))
  399. return AE_ERROR;
  400. if (wmi_device_enable(wdev, true) < 0)
  401. dev_warn(&wdev->dev, "Failed to enable device\n");
  402. wblock = container_of(wdev, struct wmi_block, dev);
  403. status = __query_block(wblock, instance, out);
  404. if (wmi_device_enable(wdev, false) < 0)
  405. dev_warn(&wdev->dev, "Failed to disable device\n");
  406. wmi_device_put(wdev);
  407. return status;
  408. }
  409. EXPORT_SYMBOL_GPL(wmi_query_block);
  410. /**
  411. * wmidev_block_query - Return contents of a WMI block (deprectated)
  412. * @wdev: A wmi bus device from a driver
  413. * @instance: Instance index
  414. *
  415. * Query an ACPI-WMI block, the caller must free the result.
  416. *
  417. * Return: ACPI object containing the content of the WMI block.
  418. */
  419. union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
  420. {
  421. struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
  422. struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
  423. if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
  424. return NULL;
  425. return out.pointer;
  426. }
  427. EXPORT_SYMBOL_GPL(wmidev_block_query);
  428. /**
  429. * wmidev_query_block - Return contents of a WMI data block
  430. * @wdev: A wmi bus device from a driver
  431. * @instance: Instance index
  432. * @out: WMI buffer to fill
  433. *
  434. * Query a WMI data block, the caller must free the resulting data inside @out.
  435. * Said data is guaranteed to be aligned on a 8-byte boundary.
  436. *
  437. * Return: 0 on success or a negative error code on failure.
  438. */
  439. int wmidev_query_block(struct wmi_device *wdev, u8 instance, struct wmi_buffer *out)
  440. {
  441. union acpi_object *obj;
  442. int ret;
  443. obj = wmidev_block_query(wdev, instance);
  444. if (!obj)
  445. return -EIO;
  446. ret = wmi_unmarshal_acpi_object(obj, out);
  447. kfree(obj);
  448. return ret;
  449. }
  450. EXPORT_SYMBOL_GPL(wmidev_query_block);
  451. /**
  452. * wmi_set_block - Write to a WMI block (deprecated)
  453. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  454. * @instance: Instance index
  455. * @in: Buffer containing new values for the data block
  456. *
  457. * Write the contents of the input buffer to an ACPI-WMI data block.
  458. *
  459. * Return: acpi_status signaling success or error.
  460. */
  461. acpi_status wmi_set_block(const char *guid_string, u8 instance, const struct acpi_buffer *in)
  462. {
  463. struct wmi_device *wdev;
  464. acpi_status status;
  465. wdev = wmi_find_device_by_guid(guid_string);
  466. if (IS_ERR(wdev))
  467. return AE_ERROR;
  468. if (wmi_device_enable(wdev, true) < 0)
  469. dev_warn(&wdev->dev, "Failed to enable device\n");
  470. status = wmidev_block_set(wdev, instance, in);
  471. if (wmi_device_enable(wdev, false) < 0)
  472. dev_warn(&wdev->dev, "Failed to disable device\n");
  473. wmi_device_put(wdev);
  474. return status;
  475. }
  476. EXPORT_SYMBOL_GPL(wmi_set_block);
  477. /**
  478. * wmidev_block_set - Write to a WMI block (deprecated)
  479. * @wdev: A wmi bus device from a driver
  480. * @instance: Instance index
  481. * @in: Buffer containing new values for the data block
  482. *
  483. * Write contents of the input buffer to an ACPI-WMI data block.
  484. *
  485. * Return: acpi_status signaling success or error.
  486. */
  487. acpi_status wmidev_block_set(struct wmi_device *wdev, u8 instance, const struct acpi_buffer *in)
  488. {
  489. struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
  490. acpi_handle handle = wblock->acpi_device->handle;
  491. struct guid_block *block = &wblock->gblock;
  492. char method[WMI_ACPI_METHOD_NAME_SIZE];
  493. struct acpi_object_list input;
  494. union acpi_object params[2];
  495. if (!in)
  496. return AE_BAD_DATA;
  497. if (block->instance_count <= instance)
  498. return AE_BAD_PARAMETER;
  499. /* Check GUID is a data block */
  500. if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
  501. return AE_ERROR;
  502. input.count = 2;
  503. input.pointer = params;
  504. params[0].type = ACPI_TYPE_INTEGER;
  505. params[0].integer.value = instance;
  506. if (wblock->gblock.flags & ACPI_WMI_STRING) {
  507. params[1].type = ACPI_TYPE_STRING;
  508. params[1].string.length = in->length;
  509. params[1].string.pointer = in->pointer;
  510. } else {
  511. params[1].type = ACPI_TYPE_BUFFER;
  512. params[1].buffer.length = in->length;
  513. params[1].buffer.pointer = in->pointer;
  514. }
  515. get_acpi_method_name(wblock, 'S', method);
  516. return acpi_evaluate_object(handle, method, &input, NULL);
  517. }
  518. EXPORT_SYMBOL_GPL(wmidev_block_set);
  519. /**
  520. * wmidev_set_block - Write to a WMI data block
  521. * @wdev: A wmi bus device from a driver
  522. * @instance: Instance index
  523. * @in: WMI buffer containing new values for the data block
  524. *
  525. * Write the content of @in into a WMI data block.
  526. *
  527. * Return: 0 on success or negative error code on failure.
  528. */
  529. int wmidev_set_block(struct wmi_device *wdev, u8 instance, const struct wmi_buffer *in)
  530. {
  531. struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
  532. struct acpi_buffer buffer;
  533. acpi_status status;
  534. int ret;
  535. if (wblock->gblock.flags & ACPI_WMI_STRING) {
  536. ret = wmi_marshal_string(in, &buffer);
  537. if (ret < 0)
  538. return ret;
  539. } else {
  540. if (in->length > U32_MAX)
  541. return -E2BIG;
  542. buffer.length = in->length;
  543. buffer.pointer = in->data;
  544. }
  545. status = wmidev_block_set(wdev, instance, &buffer);
  546. if (wblock->gblock.flags & ACPI_WMI_STRING)
  547. kfree(buffer.pointer);
  548. if (ACPI_FAILURE(status))
  549. return -EIO;
  550. return 0;
  551. }
  552. EXPORT_SYMBOL_GPL(wmidev_set_block);
  553. /**
  554. * wmi_install_notify_handler - Register handler for WMI events (deprecated)
  555. * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  556. * @handler: Function to handle notifications
  557. * @data: Data to be returned to handler when event is fired
  558. *
  559. * Register a handler for events sent to the ACPI-WMI mapper device.
  560. *
  561. * Return: acpi_status signaling success or error.
  562. */
  563. acpi_status wmi_install_notify_handler(const char *guid,
  564. wmi_notify_handler handler,
  565. void *data)
  566. {
  567. struct wmi_block *wblock;
  568. struct wmi_device *wdev;
  569. acpi_status status;
  570. wdev = wmi_find_device_by_guid(guid);
  571. if (IS_ERR(wdev))
  572. return AE_ERROR;
  573. wblock = container_of(wdev, struct wmi_block, dev);
  574. down_write(&wblock->notify_lock);
  575. if (wblock->handler) {
  576. status = AE_ALREADY_ACQUIRED;
  577. } else {
  578. wblock->handler = handler;
  579. wblock->handler_data = data;
  580. if (wmi_device_enable(wdev, true) < 0)
  581. dev_warn(&wblock->dev.dev, "Failed to enable device\n");
  582. status = AE_OK;
  583. }
  584. up_write(&wblock->notify_lock);
  585. wmi_device_put(wdev);
  586. return status;
  587. }
  588. EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
  589. /**
  590. * wmi_remove_notify_handler - Unregister handler for WMI events (deprecated)
  591. * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  592. *
  593. * Unregister handler for events sent to the ACPI-WMI mapper device.
  594. *
  595. * Return: acpi_status signaling success or error.
  596. */
  597. acpi_status wmi_remove_notify_handler(const char *guid)
  598. {
  599. struct wmi_block *wblock;
  600. struct wmi_device *wdev;
  601. acpi_status status;
  602. wdev = wmi_find_device_by_guid(guid);
  603. if (IS_ERR(wdev))
  604. return AE_ERROR;
  605. wblock = container_of(wdev, struct wmi_block, dev);
  606. down_write(&wblock->notify_lock);
  607. if (!wblock->handler) {
  608. status = AE_NULL_ENTRY;
  609. } else {
  610. if (wmi_device_enable(wdev, false) < 0)
  611. dev_warn(&wblock->dev.dev, "Failed to disable device\n");
  612. wblock->handler = NULL;
  613. wblock->handler_data = NULL;
  614. status = AE_OK;
  615. }
  616. up_write(&wblock->notify_lock);
  617. wmi_device_put(wdev);
  618. return status;
  619. }
  620. EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
  621. /**
  622. * wmi_has_guid - Check if a GUID is available
  623. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  624. *
  625. * Check if a given GUID is defined by _WDG.
  626. *
  627. * Return: True if GUID is available, false otherwise.
  628. */
  629. bool wmi_has_guid(const char *guid_string)
  630. {
  631. struct wmi_device *wdev;
  632. wdev = wmi_find_device_by_guid(guid_string);
  633. if (IS_ERR(wdev))
  634. return false;
  635. wmi_device_put(wdev);
  636. return true;
  637. }
  638. EXPORT_SYMBOL_GPL(wmi_has_guid);
  639. /**
  640. * wmi_get_acpi_device_uid() - Get _UID name of ACPI device that defines GUID (deprecated)
  641. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  642. *
  643. * Find the _UID of ACPI device associated with this WMI GUID.
  644. *
  645. * Return: The ACPI _UID field value or NULL if the WMI GUID was not found.
  646. */
  647. char *wmi_get_acpi_device_uid(const char *guid_string)
  648. {
  649. struct wmi_block *wblock;
  650. struct wmi_device *wdev;
  651. char *uid;
  652. wdev = wmi_find_device_by_guid(guid_string);
  653. if (IS_ERR(wdev))
  654. return NULL;
  655. wblock = container_of(wdev, struct wmi_block, dev);
  656. uid = acpi_device_uid(wblock->acpi_device);
  657. wmi_device_put(wdev);
  658. return uid;
  659. }
  660. EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid);
  661. /*
  662. * sysfs interface
  663. */
  664. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  665. char *buf)
  666. {
  667. struct wmi_block *wblock = dev_to_wblock(dev);
  668. return sysfs_emit(buf, "wmi:%pUL\n", &wblock->gblock.guid);
  669. }
  670. static DEVICE_ATTR_RO(modalias);
  671. static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
  672. char *buf)
  673. {
  674. struct wmi_block *wblock = dev_to_wblock(dev);
  675. return sysfs_emit(buf, "%pUL\n", &wblock->gblock.guid);
  676. }
  677. static DEVICE_ATTR_RO(guid);
  678. static ssize_t instance_count_show(struct device *dev,
  679. struct device_attribute *attr, char *buf)
  680. {
  681. struct wmi_block *wblock = dev_to_wblock(dev);
  682. return sysfs_emit(buf, "%d\n", (int)wblock->gblock.instance_count);
  683. }
  684. static DEVICE_ATTR_RO(instance_count);
  685. static ssize_t expensive_show(struct device *dev,
  686. struct device_attribute *attr, char *buf)
  687. {
  688. struct wmi_block *wblock = dev_to_wblock(dev);
  689. return sysfs_emit(buf, "%d\n",
  690. (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
  691. }
  692. static DEVICE_ATTR_RO(expensive);
  693. static ssize_t driver_override_show(struct device *dev, struct device_attribute *attr,
  694. char *buf)
  695. {
  696. struct wmi_device *wdev = to_wmi_device(dev);
  697. ssize_t ret;
  698. device_lock(dev);
  699. ret = sysfs_emit(buf, "%s\n", wdev->driver_override);
  700. device_unlock(dev);
  701. return ret;
  702. }
  703. static ssize_t driver_override_store(struct device *dev, struct device_attribute *attr,
  704. const char *buf, size_t count)
  705. {
  706. struct wmi_device *wdev = to_wmi_device(dev);
  707. int ret;
  708. ret = driver_set_override(dev, &wdev->driver_override, buf, count);
  709. if (ret < 0)
  710. return ret;
  711. return count;
  712. }
  713. static DEVICE_ATTR_RW(driver_override);
  714. static struct attribute *wmi_attrs[] = {
  715. &dev_attr_modalias.attr,
  716. &dev_attr_guid.attr,
  717. &dev_attr_instance_count.attr,
  718. &dev_attr_expensive.attr,
  719. &dev_attr_driver_override.attr,
  720. NULL
  721. };
  722. ATTRIBUTE_GROUPS(wmi);
  723. static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
  724. char *buf)
  725. {
  726. struct wmi_block *wblock = dev_to_wblock(dev);
  727. return sysfs_emit(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
  728. }
  729. static DEVICE_ATTR_RO(notify_id);
  730. static struct attribute *wmi_event_attrs[] = {
  731. &dev_attr_notify_id.attr,
  732. NULL
  733. };
  734. ATTRIBUTE_GROUPS(wmi_event);
  735. static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
  736. char *buf)
  737. {
  738. struct wmi_block *wblock = dev_to_wblock(dev);
  739. return sysfs_emit(buf, "%c%c\n", wblock->gblock.object_id[0],
  740. wblock->gblock.object_id[1]);
  741. }
  742. static DEVICE_ATTR_RO(object_id);
  743. static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
  744. char *buf)
  745. {
  746. struct wmi_device *wdev = to_wmi_device(dev);
  747. return sysfs_emit(buf, "%d\n", (int)wdev->setable);
  748. }
  749. static DEVICE_ATTR_RO(setable);
  750. static struct attribute *wmi_data_attrs[] = {
  751. &dev_attr_object_id.attr,
  752. &dev_attr_setable.attr,
  753. NULL
  754. };
  755. ATTRIBUTE_GROUPS(wmi_data);
  756. static struct attribute *wmi_method_attrs[] = {
  757. &dev_attr_object_id.attr,
  758. NULL
  759. };
  760. ATTRIBUTE_GROUPS(wmi_method);
  761. static int wmi_dev_uevent(const struct device *dev, struct kobj_uevent_env *env)
  762. {
  763. const struct wmi_block *wblock = dev_to_wblock(dev);
  764. if (add_uevent_var(env, "MODALIAS=wmi:%pUL", &wblock->gblock.guid))
  765. return -ENOMEM;
  766. if (add_uevent_var(env, "WMI_GUID=%pUL", &wblock->gblock.guid))
  767. return -ENOMEM;
  768. return 0;
  769. }
  770. static void wmi_dev_release(struct device *dev)
  771. {
  772. struct wmi_block *wblock = dev_to_wblock(dev);
  773. kfree(wblock->dev.driver_override);
  774. kfree(wblock);
  775. }
  776. static int wmi_dev_match(struct device *dev, const struct device_driver *driver)
  777. {
  778. const struct wmi_driver *wmi_driver = to_wmi_driver(driver);
  779. struct wmi_block *wblock = dev_to_wblock(dev);
  780. const struct wmi_device_id *id = wmi_driver->id_table;
  781. /* When driver_override is set, only bind to the matching driver */
  782. if (wblock->dev.driver_override)
  783. return !strcmp(wblock->dev.driver_override, driver->name);
  784. if (id == NULL)
  785. return 0;
  786. while (*id->guid_string) {
  787. if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid))
  788. return 1;
  789. id++;
  790. }
  791. return 0;
  792. }
  793. static void wmi_dev_disable(void *data)
  794. {
  795. struct device *dev = data;
  796. if (wmi_device_enable(to_wmi_device(dev), false) < 0)
  797. dev_warn(dev, "Failed to disable device\n");
  798. }
  799. static int wmi_dev_probe(struct device *dev)
  800. {
  801. struct wmi_block *wblock = dev_to_wblock(dev);
  802. struct wmi_driver *wdriver = to_wmi_driver(dev->driver);
  803. int ret;
  804. /* Some older WMI drivers will break if instantiated multiple times,
  805. * so they are blocked from probing WMI devices with a duplicated GUID.
  806. *
  807. * New WMI drivers should support being instantiated multiple times.
  808. */
  809. if (test_bit(WMI_GUID_DUPLICATED, &wblock->flags) && !wdriver->no_singleton) {
  810. dev_warn(dev, "Legacy driver %s cannot be instantiated multiple times\n",
  811. dev->driver->name);
  812. return -ENODEV;
  813. }
  814. if (wdriver->notify || wdriver->notify_new) {
  815. if (test_bit(WMI_NO_EVENT_DATA, &wblock->flags) && !wdriver->no_notify_data)
  816. return -ENODEV;
  817. }
  818. if (wmi_device_enable(to_wmi_device(dev), true) < 0)
  819. dev_warn(dev, "failed to enable device -- probing anyway\n");
  820. /*
  821. * We have to make sure that all devres-managed resources are released first because
  822. * some might still want to access the underlying WMI device.
  823. */
  824. ret = devm_add_action_or_reset(dev, wmi_dev_disable, dev);
  825. if (ret < 0)
  826. return ret;
  827. if (wdriver->probe) {
  828. ret = wdriver->probe(to_wmi_device(dev),
  829. find_guid_context(wblock, wdriver));
  830. if (ret)
  831. return ret;
  832. }
  833. down_write(&wblock->notify_lock);
  834. wblock->driver_ready = true;
  835. up_write(&wblock->notify_lock);
  836. return 0;
  837. }
  838. static void wmi_dev_remove(struct device *dev)
  839. {
  840. struct wmi_block *wblock = dev_to_wblock(dev);
  841. struct wmi_driver *wdriver = to_wmi_driver(dev->driver);
  842. down_write(&wblock->notify_lock);
  843. wblock->driver_ready = false;
  844. up_write(&wblock->notify_lock);
  845. if (wdriver->remove)
  846. wdriver->remove(to_wmi_device(dev));
  847. }
  848. static void wmi_dev_shutdown(struct device *dev)
  849. {
  850. struct wmi_driver *wdriver;
  851. struct wmi_block *wblock;
  852. if (dev->driver) {
  853. wdriver = to_wmi_driver(dev->driver);
  854. wblock = dev_to_wblock(dev);
  855. /*
  856. * Some machines return bogus WMI event data when disabling
  857. * the WMI event. Because of this we must prevent the associated
  858. * WMI driver from receiving new WMI events before disabling it.
  859. */
  860. down_write(&wblock->notify_lock);
  861. wblock->driver_ready = false;
  862. up_write(&wblock->notify_lock);
  863. if (wdriver->shutdown)
  864. wdriver->shutdown(to_wmi_device(dev));
  865. /*
  866. * We still need to disable the WMI device here since devres-managed resources
  867. * like wmi_dev_disable() will not be release during shutdown.
  868. */
  869. if (wmi_device_enable(to_wmi_device(dev), false) < 0)
  870. dev_warn(dev, "Failed to disable device\n");
  871. }
  872. }
  873. static struct class wmi_bus_class = {
  874. .name = "wmi_bus",
  875. };
  876. static const struct bus_type wmi_bus_type = {
  877. .name = "wmi",
  878. .dev_groups = wmi_groups,
  879. .match = wmi_dev_match,
  880. .uevent = wmi_dev_uevent,
  881. .probe = wmi_dev_probe,
  882. .remove = wmi_dev_remove,
  883. .shutdown = wmi_dev_shutdown,
  884. };
  885. static const struct device_type wmi_type_event = {
  886. .name = "event",
  887. .groups = wmi_event_groups,
  888. .release = wmi_dev_release,
  889. };
  890. static const struct device_type wmi_type_method = {
  891. .name = "method",
  892. .groups = wmi_method_groups,
  893. .release = wmi_dev_release,
  894. };
  895. static const struct device_type wmi_type_data = {
  896. .name = "data",
  897. .groups = wmi_data_groups,
  898. .release = wmi_dev_release,
  899. };
  900. static int wmi_count_guids(struct device *dev, void *data)
  901. {
  902. struct wmi_guid_count_context *context = data;
  903. struct wmi_block *wblock = dev_to_wblock(dev);
  904. if (guid_equal(&wblock->gblock.guid, context->guid))
  905. context->count++;
  906. return 0;
  907. }
  908. static int guid_count(const guid_t *guid)
  909. {
  910. struct wmi_guid_count_context context = {
  911. .guid = guid,
  912. .count = 0,
  913. };
  914. int ret;
  915. ret = bus_for_each_dev(&wmi_bus_type, NULL, &context, wmi_count_guids);
  916. if (ret < 0)
  917. return ret;
  918. return context.count;
  919. }
  920. static int wmi_dev_set_name(struct wmi_block *wblock, int count)
  921. {
  922. if (IS_ENABLED(CONFIG_ACPI_WMI_LEGACY_DEVICE_NAMES)) {
  923. if (count)
  924. return dev_set_name(&wblock->dev.dev, "%pUL-%d", &wblock->gblock.guid,
  925. count);
  926. else
  927. return dev_set_name(&wblock->dev.dev, "%pUL", &wblock->gblock.guid);
  928. }
  929. return dev_set_name(&wblock->dev.dev, "%pUL-%d", &wblock->gblock.guid, wblock->dev.dev.id);
  930. }
  931. static int wmi_create_device(struct device *wmi_bus_dev,
  932. struct wmi_block *wblock,
  933. struct acpi_device *device)
  934. {
  935. char method[WMI_ACPI_METHOD_NAME_SIZE];
  936. struct acpi_device_info *info;
  937. acpi_handle method_handle;
  938. acpi_status status;
  939. int count, ret;
  940. if (wblock->gblock.flags & ACPI_WMI_EVENT) {
  941. wblock->dev.dev.type = &wmi_type_event;
  942. goto out_init;
  943. }
  944. if (wblock->gblock.flags & ACPI_WMI_METHOD) {
  945. get_acpi_method_name(wblock, 'M', method);
  946. if (!acpi_has_method(device->handle, method)) {
  947. dev_warn(wmi_bus_dev,
  948. FW_BUG "%s method block execution control method not found\n",
  949. method);
  950. return -ENXIO;
  951. }
  952. wblock->dev.dev.type = &wmi_type_method;
  953. goto out_init;
  954. }
  955. /*
  956. * Data Block Query Control Method (WQxx by convention) is
  957. * required per the WMI documentation. If it is not present,
  958. * we ignore this data block.
  959. */
  960. get_acpi_method_name(wblock, 'Q', method);
  961. status = acpi_get_handle(device->handle, method, &method_handle);
  962. if (ACPI_FAILURE(status)) {
  963. dev_warn(wmi_bus_dev,
  964. FW_BUG "%s data block query control method not found\n",
  965. method);
  966. return -ENXIO;
  967. }
  968. status = acpi_get_object_info(method_handle, &info);
  969. if (ACPI_FAILURE(status))
  970. return -EIO;
  971. wblock->dev.dev.type = &wmi_type_data;
  972. /*
  973. * The Microsoft documentation specifically states:
  974. *
  975. * Data blocks registered with only a single instance
  976. * can ignore the parameter.
  977. *
  978. * ACPICA will get mad at us if we call the method with the wrong number
  979. * of arguments, so check what our method expects. (On some Dell
  980. * laptops, WQxx may not be a method at all.)
  981. */
  982. if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
  983. set_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags);
  984. kfree(info);
  985. get_acpi_method_name(wblock, 'S', method);
  986. if (acpi_has_method(device->handle, method))
  987. wblock->dev.setable = true;
  988. out_init:
  989. init_rwsem(&wblock->notify_lock);
  990. wblock->driver_ready = false;
  991. wblock->dev.dev.bus = &wmi_bus_type;
  992. wblock->dev.dev.parent = wmi_bus_dev;
  993. count = guid_count(&wblock->gblock.guid);
  994. if (count < 0)
  995. return count;
  996. if (count)
  997. set_bit(WMI_GUID_DUPLICATED, &wblock->flags);
  998. ret = ida_alloc(&wmi_ida, GFP_KERNEL);
  999. if (ret < 0)
  1000. return ret;
  1001. wblock->dev.dev.id = ret;
  1002. ret = wmi_dev_set_name(wblock, count);
  1003. if (ret < 0) {
  1004. ida_free(&wmi_ida, wblock->dev.dev.id);
  1005. return ret;
  1006. }
  1007. device_initialize(&wblock->dev.dev);
  1008. return 0;
  1009. }
  1010. static int wmi_add_device(struct platform_device *pdev, struct wmi_device *wdev)
  1011. {
  1012. struct device_link *link;
  1013. /*
  1014. * Many aggregate WMI drivers do not use -EPROBE_DEFER when they
  1015. * are unable to find a WMI device during probe, instead they require
  1016. * all WMI devices associated with an platform device to become available
  1017. * at once. This device link thus prevents WMI drivers from probing until
  1018. * the associated platform device has finished probing (and has registered
  1019. * all discovered WMI devices).
  1020. */
  1021. link = device_link_add(&wdev->dev, &pdev->dev, DL_FLAG_AUTOREMOVE_SUPPLIER);
  1022. if (!link)
  1023. return -EINVAL;
  1024. return device_add(&wdev->dev);
  1025. }
  1026. /*
  1027. * Parse the _WDG method for the GUID data blocks
  1028. */
  1029. static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev)
  1030. {
  1031. struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
  1032. struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
  1033. const struct guid_block *gblock;
  1034. bool event_data_available;
  1035. struct wmi_block *wblock;
  1036. union acpi_object *obj;
  1037. acpi_status status;
  1038. u32 i, total;
  1039. int retval;
  1040. status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
  1041. if (ACPI_FAILURE(status))
  1042. return -ENXIO;
  1043. obj = out.pointer;
  1044. if (!obj)
  1045. return -ENXIO;
  1046. if (obj->type != ACPI_TYPE_BUFFER) {
  1047. kfree(obj);
  1048. return -ENXIO;
  1049. }
  1050. event_data_available = acpi_has_method(device->handle, "_WED");
  1051. gblock = (const struct guid_block *)obj->buffer.pointer;
  1052. total = obj->buffer.length / sizeof(struct guid_block);
  1053. for (i = 0; i < total; i++) {
  1054. if (!gblock[i].instance_count) {
  1055. dev_info(wmi_bus_dev, FW_INFO "%pUL has zero instances\n", &gblock[i].guid);
  1056. continue;
  1057. }
  1058. wblock = kzalloc_obj(*wblock);
  1059. if (!wblock)
  1060. continue;
  1061. wblock->acpi_device = device;
  1062. wblock->gblock = gblock[i];
  1063. if (gblock[i].flags & ACPI_WMI_EVENT && !event_data_available)
  1064. set_bit(WMI_NO_EVENT_DATA, &wblock->flags);
  1065. retval = wmi_create_device(wmi_bus_dev, wblock, device);
  1066. if (retval) {
  1067. kfree(wblock);
  1068. continue;
  1069. }
  1070. retval = wmi_add_device(pdev, &wblock->dev);
  1071. if (retval) {
  1072. dev_err(wmi_bus_dev, "failed to register %pUL\n",
  1073. &wblock->gblock.guid);
  1074. ida_free(&wmi_ida, wblock->dev.dev.id);
  1075. put_device(&wblock->dev.dev);
  1076. }
  1077. }
  1078. kfree(obj);
  1079. return 0;
  1080. }
  1081. static int wmi_get_notify_data(struct wmi_block *wblock, union acpi_object **obj)
  1082. {
  1083. struct acpi_buffer data = { ACPI_ALLOCATE_BUFFER, NULL };
  1084. union acpi_object param = {
  1085. .integer = {
  1086. .type = ACPI_TYPE_INTEGER,
  1087. .value = wblock->gblock.notify_id,
  1088. }
  1089. };
  1090. struct acpi_object_list input = {
  1091. .count = 1,
  1092. .pointer = &param,
  1093. };
  1094. acpi_status status;
  1095. status = acpi_evaluate_object(wblock->acpi_device->handle, "_WED", &input, &data);
  1096. if (ACPI_FAILURE(status)) {
  1097. dev_warn(&wblock->dev.dev, "Failed to get event data\n");
  1098. return -EIO;
  1099. }
  1100. *obj = data.pointer;
  1101. return 0;
  1102. }
  1103. static void wmi_notify_driver(struct wmi_block *wblock, union acpi_object *obj)
  1104. {
  1105. struct wmi_driver *driver = to_wmi_driver(wblock->dev.dev.driver);
  1106. struct wmi_buffer buffer;
  1107. int ret;
  1108. if (!obj && !driver->no_notify_data) {
  1109. dev_warn(&wblock->dev.dev, "Event contains no event data\n");
  1110. return;
  1111. }
  1112. if (driver->notify)
  1113. driver->notify(&wblock->dev, obj);
  1114. if (driver->notify_new) {
  1115. if (!obj) {
  1116. driver->notify_new(&wblock->dev, NULL);
  1117. return;
  1118. }
  1119. ret = wmi_unmarshal_acpi_object(obj, &buffer);
  1120. if (ret < 0) {
  1121. dev_warn(&wblock->dev.dev, "Failed to unmarshal event data: %d\n", ret);
  1122. return;
  1123. }
  1124. driver->notify_new(&wblock->dev, &buffer);
  1125. kfree(buffer.data);
  1126. }
  1127. }
  1128. static int wmi_notify_device(struct device *dev, void *data)
  1129. {
  1130. struct wmi_block *wblock = dev_to_wblock(dev);
  1131. union acpi_object *obj = NULL;
  1132. u32 *event = data;
  1133. int ret;
  1134. if (!(wblock->gblock.flags & ACPI_WMI_EVENT && wblock->gblock.notify_id == *event))
  1135. return 0;
  1136. /* The ACPI WMI specification says that _WED should be
  1137. * evaluated every time an notification is received, even
  1138. * if no consumers are present.
  1139. *
  1140. * Some firmware implementations actually depend on this
  1141. * by using a queue for events which will fill up if the
  1142. * WMI driver core stops evaluating _WED due to missing
  1143. * WMI event consumers.
  1144. */
  1145. if (!test_bit(WMI_NO_EVENT_DATA, &wblock->flags)) {
  1146. ret = wmi_get_notify_data(wblock, &obj);
  1147. if (ret < 0)
  1148. return -EIO;
  1149. }
  1150. down_read(&wblock->notify_lock);
  1151. if (wblock->dev.dev.driver && wblock->driver_ready)
  1152. wmi_notify_driver(wblock, obj);
  1153. if (wblock->handler)
  1154. wblock->handler(obj, wblock->handler_data);
  1155. up_read(&wblock->notify_lock);
  1156. kfree(obj);
  1157. acpi_bus_generate_netlink_event("wmi", acpi_dev_name(wblock->acpi_device), *event, 0);
  1158. return -EBUSY;
  1159. }
  1160. static void acpi_wmi_notify_handler(acpi_handle handle, u32 event, void *context)
  1161. {
  1162. struct device *wmi_bus_dev = context;
  1163. device_for_each_child(wmi_bus_dev, &event, wmi_notify_device);
  1164. }
  1165. static int wmi_remove_device(struct device *dev, void *data)
  1166. {
  1167. int id = dev->id;
  1168. device_unregister(dev);
  1169. ida_free(&wmi_ida, id);
  1170. return 0;
  1171. }
  1172. static void acpi_wmi_remove(struct platform_device *device)
  1173. {
  1174. struct device *wmi_bus_device = dev_get_drvdata(&device->dev);
  1175. device_for_each_child_reverse(wmi_bus_device, NULL, wmi_remove_device);
  1176. }
  1177. static void acpi_wmi_remove_notify_handler(void *data)
  1178. {
  1179. struct acpi_device *acpi_device = data;
  1180. acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY, acpi_wmi_notify_handler);
  1181. }
  1182. static void acpi_wmi_remove_bus_device(void *data)
  1183. {
  1184. struct device *wmi_bus_dev = data;
  1185. device_unregister(wmi_bus_dev);
  1186. }
  1187. static int acpi_wmi_probe(struct platform_device *device)
  1188. {
  1189. struct acpi_device *acpi_device;
  1190. struct device *wmi_bus_dev;
  1191. acpi_status status;
  1192. int error;
  1193. acpi_device = ACPI_COMPANION(&device->dev);
  1194. if (!acpi_device) {
  1195. dev_err(&device->dev, "ACPI companion is missing\n");
  1196. return -ENODEV;
  1197. }
  1198. wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0), NULL, "wmi_bus-%s",
  1199. dev_name(&device->dev));
  1200. if (IS_ERR(wmi_bus_dev))
  1201. return PTR_ERR(wmi_bus_dev);
  1202. error = devm_add_action_or_reset(&device->dev, acpi_wmi_remove_bus_device, wmi_bus_dev);
  1203. if (error < 0)
  1204. return error;
  1205. dev_set_drvdata(&device->dev, wmi_bus_dev);
  1206. status = acpi_install_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY,
  1207. acpi_wmi_notify_handler, wmi_bus_dev);
  1208. if (ACPI_FAILURE(status)) {
  1209. dev_err(&device->dev, "Error installing notify handler\n");
  1210. return -ENODEV;
  1211. }
  1212. error = devm_add_action_or_reset(&device->dev, acpi_wmi_remove_notify_handler,
  1213. acpi_device);
  1214. if (error < 0)
  1215. return error;
  1216. error = parse_wdg(wmi_bus_dev, device);
  1217. if (error) {
  1218. dev_err(&device->dev, "Failed to parse _WDG method\n");
  1219. return error;
  1220. }
  1221. return 0;
  1222. }
  1223. int __must_check __wmi_driver_register(struct wmi_driver *driver,
  1224. struct module *owner)
  1225. {
  1226. driver->driver.owner = owner;
  1227. driver->driver.bus = &wmi_bus_type;
  1228. return driver_register(&driver->driver);
  1229. }
  1230. EXPORT_SYMBOL(__wmi_driver_register);
  1231. /**
  1232. * wmi_driver_unregister() - Unregister a WMI driver
  1233. * @driver: WMI driver to unregister
  1234. *
  1235. * Unregisters a WMI driver from the WMI bus.
  1236. */
  1237. void wmi_driver_unregister(struct wmi_driver *driver)
  1238. {
  1239. driver_unregister(&driver->driver);
  1240. }
  1241. EXPORT_SYMBOL(wmi_driver_unregister);
  1242. static struct platform_driver acpi_wmi_driver = {
  1243. .driver = {
  1244. .name = "acpi-wmi",
  1245. .acpi_match_table = wmi_device_ids,
  1246. },
  1247. .probe = acpi_wmi_probe,
  1248. .remove = acpi_wmi_remove,
  1249. };
  1250. static int __init acpi_wmi_init(void)
  1251. {
  1252. int error;
  1253. if (acpi_disabled)
  1254. return -ENODEV;
  1255. error = class_register(&wmi_bus_class);
  1256. if (error)
  1257. return error;
  1258. error = bus_register(&wmi_bus_type);
  1259. if (error)
  1260. goto err_unreg_class;
  1261. error = platform_driver_register(&acpi_wmi_driver);
  1262. if (error) {
  1263. pr_err("Error loading mapper\n");
  1264. goto err_unreg_bus;
  1265. }
  1266. return 0;
  1267. err_unreg_bus:
  1268. bus_unregister(&wmi_bus_type);
  1269. err_unreg_class:
  1270. class_unregister(&wmi_bus_class);
  1271. return error;
  1272. }
  1273. static void __exit acpi_wmi_exit(void)
  1274. {
  1275. platform_driver_unregister(&acpi_wmi_driver);
  1276. bus_unregister(&wmi_bus_type);
  1277. class_unregister(&wmi_bus_class);
  1278. }
  1279. subsys_initcall_sync(acpi_wmi_init);
  1280. module_exit(acpi_wmi_exit);