wmi.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /*
  2. * ACPI-WMI mapping driver
  3. *
  4. * Copyright (C) 2007-2008 Carlos Corbacho <carlos@strangeworlds.co.uk>
  5. *
  6. * GUID parsing code from ldm.c is:
  7. * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
  8. * Copyright (c) 2001-2007 Anton Altaparmakov
  9. * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
  10. *
  11. * WMI bus infrastructure by Andrew Lutomirski and Darren Hart:
  12. * Copyright (C) 2015 Andrew Lutomirski
  13. * Copyright (C) 2017 VMware, Inc. All Rights Reserved.
  14. *
  15. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or (at
  20. * your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful, but
  23. * WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. * General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License along
  28. * with this program; if not, write to the Free Software Foundation, Inc.,
  29. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  30. *
  31. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  32. */
  33. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  34. #include <linux/kernel.h>
  35. #include <linux/init.h>
  36. #include <linux/types.h>
  37. #include <linux/device.h>
  38. #include <linux/list.h>
  39. #include <linux/acpi.h>
  40. #include <linux/slab.h>
  41. #include <linux/module.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/wmi.h>
  44. #include <linux/uuid.h>
  45. ACPI_MODULE_NAME("wmi");
  46. MODULE_AUTHOR("Carlos Corbacho");
  47. MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
  48. MODULE_LICENSE("GPL");
  49. static LIST_HEAD(wmi_block_list);
  50. struct guid_block {
  51. char guid[16];
  52. union {
  53. char object_id[2];
  54. struct {
  55. unsigned char notify_id;
  56. unsigned char reserved;
  57. };
  58. };
  59. u8 instance_count;
  60. u8 flags;
  61. };
  62. struct wmi_block {
  63. struct wmi_device dev;
  64. struct list_head list;
  65. struct guid_block gblock;
  66. struct acpi_device *acpi_device;
  67. wmi_notify_handler handler;
  68. void *handler_data;
  69. bool read_takes_no_args;
  70. };
  71. /*
  72. * If the GUID data block is marked as expensive, we must enable and
  73. * explicitily disable data collection.
  74. */
  75. #define ACPI_WMI_EXPENSIVE 0x1
  76. #define ACPI_WMI_METHOD 0x2 /* GUID is a method */
  77. #define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
  78. #define ACPI_WMI_EVENT 0x8 /* GUID is an event */
  79. static bool debug_event;
  80. module_param(debug_event, bool, 0444);
  81. MODULE_PARM_DESC(debug_event,
  82. "Log WMI Events [0/1]");
  83. static bool debug_dump_wdg;
  84. module_param(debug_dump_wdg, bool, 0444);
  85. MODULE_PARM_DESC(debug_dump_wdg,
  86. "Dump available WMI interfaces [0/1]");
  87. static int acpi_wmi_remove(struct platform_device *device);
  88. static int acpi_wmi_probe(struct platform_device *device);
  89. static const struct acpi_device_id wmi_device_ids[] = {
  90. {"PNP0C14", 0},
  91. {"pnp0c14", 0},
  92. {"", 0},
  93. };
  94. MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
  95. static struct platform_driver acpi_wmi_driver = {
  96. .driver = {
  97. .name = "acpi-wmi",
  98. .acpi_match_table = wmi_device_ids,
  99. },
  100. .probe = acpi_wmi_probe,
  101. .remove = acpi_wmi_remove,
  102. };
  103. /*
  104. * GUID parsing functions
  105. */
  106. static bool find_guid(const char *guid_string, struct wmi_block **out)
  107. {
  108. uuid_le guid_input;
  109. struct wmi_block *wblock;
  110. struct guid_block *block;
  111. struct list_head *p;
  112. if (uuid_le_to_bin(guid_string, &guid_input))
  113. return false;
  114. list_for_each(p, &wmi_block_list) {
  115. wblock = list_entry(p, struct wmi_block, list);
  116. block = &wblock->gblock;
  117. if (memcmp(block->guid, &guid_input, 16) == 0) {
  118. if (out)
  119. *out = wblock;
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. static int get_subobj_info(acpi_handle handle, const char *pathname,
  126. struct acpi_device_info **info)
  127. {
  128. struct acpi_device_info *dummy_info, **info_ptr;
  129. acpi_handle subobj_handle;
  130. acpi_status status;
  131. status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
  132. if (status == AE_NOT_FOUND)
  133. return -ENOENT;
  134. else if (ACPI_FAILURE(status))
  135. return -EIO;
  136. info_ptr = info ? info : &dummy_info;
  137. status = acpi_get_object_info(subobj_handle, info_ptr);
  138. if (ACPI_FAILURE(status))
  139. return -EIO;
  140. if (!info)
  141. kfree(dummy_info);
  142. return 0;
  143. }
  144. static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
  145. {
  146. struct guid_block *block = NULL;
  147. char method[5];
  148. acpi_status status;
  149. acpi_handle handle;
  150. block = &wblock->gblock;
  151. handle = wblock->acpi_device->handle;
  152. snprintf(method, 5, "WE%02X", block->notify_id);
  153. status = acpi_execute_simple_method(handle, method, enable);
  154. if (status != AE_OK && status != AE_NOT_FOUND)
  155. return status;
  156. else
  157. return AE_OK;
  158. }
  159. /*
  160. * Exported WMI functions
  161. */
  162. /**
  163. * wmi_evaluate_method - Evaluate a WMI method
  164. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  165. * @instance: Instance index
  166. * @method_id: Method ID to call
  167. * &in: Buffer containing input for the method call
  168. * &out: Empty buffer to return the method results
  169. *
  170. * Call an ACPI-WMI method
  171. */
  172. acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
  173. u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
  174. {
  175. struct guid_block *block = NULL;
  176. struct wmi_block *wblock = NULL;
  177. acpi_handle handle;
  178. acpi_status status;
  179. struct acpi_object_list input;
  180. union acpi_object params[3];
  181. char method[5] = "WM";
  182. if (!find_guid(guid_string, &wblock))
  183. return AE_ERROR;
  184. block = &wblock->gblock;
  185. handle = wblock->acpi_device->handle;
  186. if (!(block->flags & ACPI_WMI_METHOD))
  187. return AE_BAD_DATA;
  188. if (block->instance_count < instance)
  189. return AE_BAD_PARAMETER;
  190. input.count = 2;
  191. input.pointer = params;
  192. params[0].type = ACPI_TYPE_INTEGER;
  193. params[0].integer.value = instance;
  194. params[1].type = ACPI_TYPE_INTEGER;
  195. params[1].integer.value = method_id;
  196. if (in) {
  197. input.count = 3;
  198. if (block->flags & ACPI_WMI_STRING) {
  199. params[2].type = ACPI_TYPE_STRING;
  200. } else {
  201. params[2].type = ACPI_TYPE_BUFFER;
  202. }
  203. params[2].buffer.length = in->length;
  204. params[2].buffer.pointer = in->pointer;
  205. }
  206. strncat(method, block->object_id, 2);
  207. status = acpi_evaluate_object(handle, method, &input, out);
  208. return status;
  209. }
  210. EXPORT_SYMBOL_GPL(wmi_evaluate_method);
  211. static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
  212. struct acpi_buffer *out)
  213. {
  214. struct guid_block *block = NULL;
  215. acpi_handle handle;
  216. acpi_status status, wc_status = AE_ERROR;
  217. struct acpi_object_list input;
  218. union acpi_object wq_params[1];
  219. char method[5];
  220. char wc_method[5] = "WC";
  221. if (!out)
  222. return AE_BAD_PARAMETER;
  223. block = &wblock->gblock;
  224. handle = wblock->acpi_device->handle;
  225. if (block->instance_count < instance)
  226. return AE_BAD_PARAMETER;
  227. /* Check GUID is a data block */
  228. if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
  229. return AE_ERROR;
  230. input.count = 1;
  231. input.pointer = wq_params;
  232. wq_params[0].type = ACPI_TYPE_INTEGER;
  233. wq_params[0].integer.value = instance;
  234. if (instance == 0 && wblock->read_takes_no_args)
  235. input.count = 0;
  236. /*
  237. * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
  238. * enable collection.
  239. */
  240. if (block->flags & ACPI_WMI_EXPENSIVE) {
  241. strncat(wc_method, block->object_id, 2);
  242. /*
  243. * Some GUIDs break the specification by declaring themselves
  244. * expensive, but have no corresponding WCxx method. So we
  245. * should not fail if this happens.
  246. */
  247. if (acpi_has_method(handle, wc_method))
  248. wc_status = acpi_execute_simple_method(handle,
  249. wc_method, 1);
  250. }
  251. strcpy(method, "WQ");
  252. strncat(method, block->object_id, 2);
  253. status = acpi_evaluate_object(handle, method, &input, out);
  254. /*
  255. * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
  256. * the WQxx method failed - we should disable collection anyway.
  257. */
  258. if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
  259. status = acpi_execute_simple_method(handle, wc_method, 0);
  260. }
  261. return status;
  262. }
  263. /**
  264. * wmi_query_block - Return contents of a WMI block (deprecated)
  265. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  266. * @instance: Instance index
  267. * &out: Empty buffer to return the contents of the data block to
  268. *
  269. * Return the contents of an ACPI-WMI data block to a buffer
  270. */
  271. acpi_status wmi_query_block(const char *guid_string, u8 instance,
  272. struct acpi_buffer *out)
  273. {
  274. struct wmi_block *wblock;
  275. if (!guid_string)
  276. return AE_BAD_PARAMETER;
  277. if (!find_guid(guid_string, &wblock))
  278. return AE_ERROR;
  279. return __query_block(wblock, instance, out);
  280. }
  281. EXPORT_SYMBOL_GPL(wmi_query_block);
  282. union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
  283. {
  284. struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
  285. struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
  286. if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
  287. return NULL;
  288. return (union acpi_object *)out.pointer;
  289. }
  290. EXPORT_SYMBOL_GPL(wmidev_block_query);
  291. struct wmi_device *wmidev_get_other_guid(struct wmi_device *wdev,
  292. const char *guid_string)
  293. {
  294. struct wmi_block *this_wb = container_of(wdev, struct wmi_block, dev);
  295. struct wmi_block *other_wb;
  296. if (!find_guid(guid_string, &other_wb))
  297. return NULL;
  298. if (other_wb->acpi_device != this_wb->acpi_device)
  299. return NULL;
  300. get_device(&other_wb->dev.dev);
  301. return &other_wb->dev;
  302. }
  303. EXPORT_SYMBOL_GPL(wmidev_get_other_guid);
  304. /**
  305. * wmi_set_block - Write to a WMI block
  306. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  307. * @instance: Instance index
  308. * &in: Buffer containing new values for the data block
  309. *
  310. * Write the contents of the input buffer to an ACPI-WMI data block
  311. */
  312. acpi_status wmi_set_block(const char *guid_string, u8 instance,
  313. const struct acpi_buffer *in)
  314. {
  315. struct guid_block *block = NULL;
  316. struct wmi_block *wblock = NULL;
  317. acpi_handle handle;
  318. struct acpi_object_list input;
  319. union acpi_object params[2];
  320. char method[5] = "WS";
  321. if (!guid_string || !in)
  322. return AE_BAD_DATA;
  323. if (!find_guid(guid_string, &wblock))
  324. return AE_ERROR;
  325. block = &wblock->gblock;
  326. handle = wblock->acpi_device->handle;
  327. if (block->instance_count < instance)
  328. return AE_BAD_PARAMETER;
  329. /* Check GUID is a data block */
  330. if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
  331. return AE_ERROR;
  332. input.count = 2;
  333. input.pointer = params;
  334. params[0].type = ACPI_TYPE_INTEGER;
  335. params[0].integer.value = instance;
  336. if (block->flags & ACPI_WMI_STRING) {
  337. params[1].type = ACPI_TYPE_STRING;
  338. } else {
  339. params[1].type = ACPI_TYPE_BUFFER;
  340. }
  341. params[1].buffer.length = in->length;
  342. params[1].buffer.pointer = in->pointer;
  343. strncat(method, block->object_id, 2);
  344. return acpi_evaluate_object(handle, method, &input, NULL);
  345. }
  346. EXPORT_SYMBOL_GPL(wmi_set_block);
  347. static void wmi_dump_wdg(const struct guid_block *g)
  348. {
  349. pr_info("%pUL:\n", g->guid);
  350. if (g->flags & ACPI_WMI_EVENT)
  351. pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
  352. else
  353. pr_info("\tobject_id: %2pE\n", g->object_id);
  354. pr_info("\tinstance_count: %d\n", g->instance_count);
  355. pr_info("\tflags: %#x", g->flags);
  356. if (g->flags) {
  357. if (g->flags & ACPI_WMI_EXPENSIVE)
  358. pr_cont(" ACPI_WMI_EXPENSIVE");
  359. if (g->flags & ACPI_WMI_METHOD)
  360. pr_cont(" ACPI_WMI_METHOD");
  361. if (g->flags & ACPI_WMI_STRING)
  362. pr_cont(" ACPI_WMI_STRING");
  363. if (g->flags & ACPI_WMI_EVENT)
  364. pr_cont(" ACPI_WMI_EVENT");
  365. }
  366. pr_cont("\n");
  367. }
  368. static void wmi_notify_debug(u32 value, void *context)
  369. {
  370. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  371. union acpi_object *obj;
  372. acpi_status status;
  373. status = wmi_get_event_data(value, &response);
  374. if (status != AE_OK) {
  375. pr_info("bad event status 0x%x\n", status);
  376. return;
  377. }
  378. obj = (union acpi_object *)response.pointer;
  379. if (!obj)
  380. return;
  381. pr_info("DEBUG Event ");
  382. switch(obj->type) {
  383. case ACPI_TYPE_BUFFER:
  384. pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
  385. break;
  386. case ACPI_TYPE_STRING:
  387. pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
  388. break;
  389. case ACPI_TYPE_INTEGER:
  390. pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
  391. break;
  392. case ACPI_TYPE_PACKAGE:
  393. pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
  394. break;
  395. default:
  396. pr_cont("object type 0x%X\n", obj->type);
  397. }
  398. kfree(obj);
  399. }
  400. /**
  401. * wmi_install_notify_handler - Register handler for WMI events
  402. * @handler: Function to handle notifications
  403. * @data: Data to be returned to handler when event is fired
  404. *
  405. * Register a handler for events sent to the ACPI-WMI mapper device.
  406. */
  407. acpi_status wmi_install_notify_handler(const char *guid,
  408. wmi_notify_handler handler, void *data)
  409. {
  410. struct wmi_block *block;
  411. acpi_status status = AE_NOT_EXIST;
  412. uuid_le guid_input;
  413. struct list_head *p;
  414. if (!guid || !handler)
  415. return AE_BAD_PARAMETER;
  416. if (uuid_le_to_bin(guid, &guid_input))
  417. return AE_BAD_PARAMETER;
  418. list_for_each(p, &wmi_block_list) {
  419. acpi_status wmi_status;
  420. block = list_entry(p, struct wmi_block, list);
  421. if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
  422. if (block->handler &&
  423. block->handler != wmi_notify_debug)
  424. return AE_ALREADY_ACQUIRED;
  425. block->handler = handler;
  426. block->handler_data = data;
  427. wmi_status = wmi_method_enable(block, 1);
  428. if ((wmi_status != AE_OK) ||
  429. ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
  430. status = wmi_status;
  431. }
  432. }
  433. return status;
  434. }
  435. EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
  436. /**
  437. * wmi_uninstall_notify_handler - Unregister handler for WMI events
  438. *
  439. * Unregister handler for events sent to the ACPI-WMI mapper device.
  440. */
  441. acpi_status wmi_remove_notify_handler(const char *guid)
  442. {
  443. struct wmi_block *block;
  444. acpi_status status = AE_NOT_EXIST;
  445. uuid_le guid_input;
  446. struct list_head *p;
  447. if (!guid)
  448. return AE_BAD_PARAMETER;
  449. if (uuid_le_to_bin(guid, &guid_input))
  450. return AE_BAD_PARAMETER;
  451. list_for_each(p, &wmi_block_list) {
  452. acpi_status wmi_status;
  453. block = list_entry(p, struct wmi_block, list);
  454. if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
  455. if (!block->handler ||
  456. block->handler == wmi_notify_debug)
  457. return AE_NULL_ENTRY;
  458. if (debug_event) {
  459. block->handler = wmi_notify_debug;
  460. status = AE_OK;
  461. } else {
  462. wmi_status = wmi_method_enable(block, 0);
  463. block->handler = NULL;
  464. block->handler_data = NULL;
  465. if ((wmi_status != AE_OK) ||
  466. ((wmi_status == AE_OK) &&
  467. (status == AE_NOT_EXIST)))
  468. status = wmi_status;
  469. }
  470. }
  471. }
  472. return status;
  473. }
  474. EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
  475. /**
  476. * wmi_get_event_data - Get WMI data associated with an event
  477. *
  478. * @event: Event to find
  479. * @out: Buffer to hold event data. out->pointer should be freed with kfree()
  480. *
  481. * Returns extra data associated with an event in WMI.
  482. */
  483. acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
  484. {
  485. struct acpi_object_list input;
  486. union acpi_object params[1];
  487. struct guid_block *gblock;
  488. struct wmi_block *wblock;
  489. struct list_head *p;
  490. input.count = 1;
  491. input.pointer = params;
  492. params[0].type = ACPI_TYPE_INTEGER;
  493. params[0].integer.value = event;
  494. list_for_each(p, &wmi_block_list) {
  495. wblock = list_entry(p, struct wmi_block, list);
  496. gblock = &wblock->gblock;
  497. if ((gblock->flags & ACPI_WMI_EVENT) &&
  498. (gblock->notify_id == event))
  499. return acpi_evaluate_object(wblock->acpi_device->handle,
  500. "_WED", &input, out);
  501. }
  502. return AE_NOT_FOUND;
  503. }
  504. EXPORT_SYMBOL_GPL(wmi_get_event_data);
  505. /**
  506. * wmi_has_guid - Check if a GUID is available
  507. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  508. *
  509. * Check if a given GUID is defined by _WDG
  510. */
  511. bool wmi_has_guid(const char *guid_string)
  512. {
  513. return find_guid(guid_string, NULL);
  514. }
  515. EXPORT_SYMBOL_GPL(wmi_has_guid);
  516. static struct wmi_block *dev_to_wblock(struct device *dev)
  517. {
  518. return container_of(dev, struct wmi_block, dev.dev);
  519. }
  520. static struct wmi_device *dev_to_wdev(struct device *dev)
  521. {
  522. return container_of(dev, struct wmi_device, dev);
  523. }
  524. /*
  525. * sysfs interface
  526. */
  527. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  528. char *buf)
  529. {
  530. struct wmi_block *wblock = dev_to_wblock(dev);
  531. return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
  532. }
  533. static DEVICE_ATTR_RO(modalias);
  534. static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
  535. char *buf)
  536. {
  537. struct wmi_block *wblock = dev_to_wblock(dev);
  538. return sprintf(buf, "%pUL\n", wblock->gblock.guid);
  539. }
  540. static DEVICE_ATTR_RO(guid);
  541. static ssize_t instance_count_show(struct device *dev,
  542. struct device_attribute *attr, char *buf)
  543. {
  544. struct wmi_block *wblock = dev_to_wblock(dev);
  545. return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
  546. }
  547. static DEVICE_ATTR_RO(instance_count);
  548. static ssize_t expensive_show(struct device *dev,
  549. struct device_attribute *attr, char *buf)
  550. {
  551. struct wmi_block *wblock = dev_to_wblock(dev);
  552. return sprintf(buf, "%d\n",
  553. (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
  554. }
  555. static DEVICE_ATTR_RO(expensive);
  556. static struct attribute *wmi_attrs[] = {
  557. &dev_attr_modalias.attr,
  558. &dev_attr_guid.attr,
  559. &dev_attr_instance_count.attr,
  560. &dev_attr_expensive.attr,
  561. NULL,
  562. };
  563. ATTRIBUTE_GROUPS(wmi);
  564. static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
  565. char *buf)
  566. {
  567. struct wmi_block *wblock = dev_to_wblock(dev);
  568. return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
  569. }
  570. static DEVICE_ATTR_RO(notify_id);
  571. static struct attribute *wmi_event_attrs[] = {
  572. &dev_attr_notify_id.attr,
  573. NULL,
  574. };
  575. ATTRIBUTE_GROUPS(wmi_event);
  576. static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
  577. char *buf)
  578. {
  579. struct wmi_block *wblock = dev_to_wblock(dev);
  580. return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
  581. wblock->gblock.object_id[1]);
  582. }
  583. static DEVICE_ATTR_RO(object_id);
  584. static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
  585. char *buf)
  586. {
  587. struct wmi_device *wdev = dev_to_wdev(dev);
  588. return sprintf(buf, "%d\n", (int)wdev->setable);
  589. }
  590. static DEVICE_ATTR_RO(setable);
  591. static struct attribute *wmi_data_attrs[] = {
  592. &dev_attr_object_id.attr,
  593. &dev_attr_setable.attr,
  594. NULL,
  595. };
  596. ATTRIBUTE_GROUPS(wmi_data);
  597. static struct attribute *wmi_method_attrs[] = {
  598. &dev_attr_object_id.attr,
  599. NULL,
  600. };
  601. ATTRIBUTE_GROUPS(wmi_method);
  602. static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  603. {
  604. struct wmi_block *wblock = dev_to_wblock(dev);
  605. if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
  606. return -ENOMEM;
  607. if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
  608. return -ENOMEM;
  609. return 0;
  610. }
  611. static void wmi_dev_release(struct device *dev)
  612. {
  613. struct wmi_block *wblock = dev_to_wblock(dev);
  614. kfree(wblock);
  615. }
  616. static int wmi_dev_match(struct device *dev, struct device_driver *driver)
  617. {
  618. struct wmi_driver *wmi_driver =
  619. container_of(driver, struct wmi_driver, driver);
  620. struct wmi_block *wblock = dev_to_wblock(dev);
  621. const struct wmi_device_id *id = wmi_driver->id_table;
  622. while (id->guid_string) {
  623. uuid_le driver_guid;
  624. if (WARN_ON(uuid_le_to_bin(id->guid_string, &driver_guid)))
  625. continue;
  626. if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
  627. return 1;
  628. id++;
  629. }
  630. return 0;
  631. }
  632. static int wmi_dev_probe(struct device *dev)
  633. {
  634. struct wmi_block *wblock = dev_to_wblock(dev);
  635. struct wmi_driver *wdriver =
  636. container_of(dev->driver, struct wmi_driver, driver);
  637. int ret = 0;
  638. if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
  639. dev_warn(dev, "failed to enable device -- probing anyway\n");
  640. if (wdriver->probe) {
  641. ret = wdriver->probe(dev_to_wdev(dev));
  642. if (ret != 0 && ACPI_FAILURE(wmi_method_enable(wblock, 0)))
  643. dev_warn(dev, "failed to disable device\n");
  644. }
  645. return ret;
  646. }
  647. static int wmi_dev_remove(struct device *dev)
  648. {
  649. struct wmi_block *wblock = dev_to_wblock(dev);
  650. struct wmi_driver *wdriver =
  651. container_of(dev->driver, struct wmi_driver, driver);
  652. int ret = 0;
  653. if (wdriver->remove)
  654. ret = wdriver->remove(dev_to_wdev(dev));
  655. if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
  656. dev_warn(dev, "failed to disable device\n");
  657. return ret;
  658. }
  659. static struct class wmi_bus_class = {
  660. .name = "wmi_bus",
  661. };
  662. static struct bus_type wmi_bus_type = {
  663. .name = "wmi",
  664. .dev_groups = wmi_groups,
  665. .match = wmi_dev_match,
  666. .uevent = wmi_dev_uevent,
  667. .probe = wmi_dev_probe,
  668. .remove = wmi_dev_remove,
  669. };
  670. static struct device_type wmi_type_event = {
  671. .name = "event",
  672. .groups = wmi_event_groups,
  673. .release = wmi_dev_release,
  674. };
  675. static struct device_type wmi_type_method = {
  676. .name = "method",
  677. .groups = wmi_method_groups,
  678. .release = wmi_dev_release,
  679. };
  680. static struct device_type wmi_type_data = {
  681. .name = "data",
  682. .groups = wmi_data_groups,
  683. .release = wmi_dev_release,
  684. };
  685. static int wmi_create_device(struct device *wmi_bus_dev,
  686. const struct guid_block *gblock,
  687. struct wmi_block *wblock,
  688. struct acpi_device *device)
  689. {
  690. struct acpi_device_info *info;
  691. char method[5];
  692. int result;
  693. if (gblock->flags & ACPI_WMI_EVENT) {
  694. wblock->dev.dev.type = &wmi_type_event;
  695. goto out_init;
  696. }
  697. if (gblock->flags & ACPI_WMI_METHOD) {
  698. wblock->dev.dev.type = &wmi_type_method;
  699. goto out_init;
  700. }
  701. /*
  702. * Data Block Query Control Method (WQxx by convention) is
  703. * required per the WMI documentation. If it is not present,
  704. * we ignore this data block.
  705. */
  706. strcpy(method, "WQ");
  707. strncat(method, wblock->gblock.object_id, 2);
  708. result = get_subobj_info(device->handle, method, &info);
  709. if (result) {
  710. dev_warn(wmi_bus_dev,
  711. "%s data block query control method not found",
  712. method);
  713. return result;
  714. }
  715. wblock->dev.dev.type = &wmi_type_data;
  716. /*
  717. * The Microsoft documentation specifically states:
  718. *
  719. * Data blocks registered with only a single instance
  720. * can ignore the parameter.
  721. *
  722. * ACPICA will get mad at us if we call the method with the wrong number
  723. * of arguments, so check what our method expects. (On some Dell
  724. * laptops, WQxx may not be a method at all.)
  725. */
  726. if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
  727. wblock->read_takes_no_args = true;
  728. kfree(info);
  729. strcpy(method, "WS");
  730. strncat(method, wblock->gblock.object_id, 2);
  731. result = get_subobj_info(device->handle, method, NULL);
  732. if (result == 0)
  733. wblock->dev.setable = true;
  734. out_init:
  735. wblock->dev.dev.bus = &wmi_bus_type;
  736. wblock->dev.dev.parent = wmi_bus_dev;
  737. dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
  738. device_initialize(&wblock->dev.dev);
  739. return 0;
  740. }
  741. static void wmi_free_devices(struct acpi_device *device)
  742. {
  743. struct wmi_block *wblock, *next;
  744. /* Delete devices for all the GUIDs */
  745. list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
  746. if (wblock->acpi_device == device) {
  747. list_del(&wblock->list);
  748. device_unregister(&wblock->dev.dev);
  749. }
  750. }
  751. }
  752. static bool guid_already_parsed(struct acpi_device *device,
  753. const u8 *guid)
  754. {
  755. struct wmi_block *wblock;
  756. list_for_each_entry(wblock, &wmi_block_list, list) {
  757. if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
  758. /*
  759. * Because we historically didn't track the relationship
  760. * between GUIDs and ACPI nodes, we don't know whether
  761. * we need to suppress GUIDs that are unique on a
  762. * given node but duplicated across nodes.
  763. */
  764. dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
  765. guid, dev_name(&wblock->acpi_device->dev));
  766. return true;
  767. }
  768. }
  769. return false;
  770. }
  771. /*
  772. * Parse the _WDG method for the GUID data blocks
  773. */
  774. static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
  775. {
  776. struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
  777. const struct guid_block *gblock;
  778. struct wmi_block *wblock, *next;
  779. union acpi_object *obj;
  780. acpi_status status;
  781. int retval = 0;
  782. u32 i, total;
  783. status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
  784. if (ACPI_FAILURE(status))
  785. return -ENXIO;
  786. obj = (union acpi_object *) out.pointer;
  787. if (!obj)
  788. return -ENXIO;
  789. if (obj->type != ACPI_TYPE_BUFFER) {
  790. retval = -ENXIO;
  791. goto out_free_pointer;
  792. }
  793. gblock = (const struct guid_block *)obj->buffer.pointer;
  794. total = obj->buffer.length / sizeof(struct guid_block);
  795. for (i = 0; i < total; i++) {
  796. if (debug_dump_wdg)
  797. wmi_dump_wdg(&gblock[i]);
  798. /*
  799. * Some WMI devices, like those for nVidia hooks, have a
  800. * duplicate GUID. It's not clear what we should do in this
  801. * case yet, so for now, we'll just ignore the duplicate
  802. * for device creation.
  803. */
  804. if (guid_already_parsed(device, gblock[i].guid))
  805. continue;
  806. wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
  807. if (!wblock) {
  808. retval = -ENOMEM;
  809. break;
  810. }
  811. wblock->acpi_device = device;
  812. wblock->gblock = gblock[i];
  813. retval = wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
  814. if (retval) {
  815. kfree(wblock);
  816. continue;
  817. }
  818. list_add_tail(&wblock->list, &wmi_block_list);
  819. if (debug_event) {
  820. wblock->handler = wmi_notify_debug;
  821. wmi_method_enable(wblock, 1);
  822. }
  823. }
  824. /*
  825. * Now that all of the devices are created, add them to the
  826. * device tree and probe subdrivers.
  827. */
  828. list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
  829. if (wblock->acpi_device != device)
  830. continue;
  831. retval = device_add(&wblock->dev.dev);
  832. if (retval) {
  833. dev_err(wmi_bus_dev, "failed to register %pULL\n",
  834. wblock->gblock.guid);
  835. if (debug_event)
  836. wmi_method_enable(wblock, 0);
  837. list_del(&wblock->list);
  838. put_device(&wblock->dev.dev);
  839. }
  840. }
  841. out_free_pointer:
  842. kfree(out.pointer);
  843. return retval;
  844. }
  845. /*
  846. * WMI can have EmbeddedControl access regions. In which case, we just want to
  847. * hand these off to the EC driver.
  848. */
  849. static acpi_status
  850. acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
  851. u32 bits, u64 *value,
  852. void *handler_context, void *region_context)
  853. {
  854. int result = 0, i = 0;
  855. u8 temp = 0;
  856. if ((address > 0xFF) || !value)
  857. return AE_BAD_PARAMETER;
  858. if (function != ACPI_READ && function != ACPI_WRITE)
  859. return AE_BAD_PARAMETER;
  860. if (bits != 8)
  861. return AE_BAD_PARAMETER;
  862. if (function == ACPI_READ) {
  863. result = ec_read(address, &temp);
  864. (*value) |= ((u64)temp) << i;
  865. } else {
  866. temp = 0xff & ((*value) >> i);
  867. result = ec_write(address, temp);
  868. }
  869. switch (result) {
  870. case -EINVAL:
  871. return AE_BAD_PARAMETER;
  872. break;
  873. case -ENODEV:
  874. return AE_NOT_FOUND;
  875. break;
  876. case -ETIME:
  877. return AE_TIME;
  878. break;
  879. default:
  880. return AE_OK;
  881. }
  882. }
  883. static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
  884. void *context)
  885. {
  886. struct guid_block *block;
  887. struct wmi_block *wblock;
  888. struct list_head *p;
  889. bool found_it = false;
  890. list_for_each(p, &wmi_block_list) {
  891. wblock = list_entry(p, struct wmi_block, list);
  892. block = &wblock->gblock;
  893. if (wblock->acpi_device->handle == handle &&
  894. (block->flags & ACPI_WMI_EVENT) &&
  895. (block->notify_id == event))
  896. {
  897. found_it = true;
  898. break;
  899. }
  900. }
  901. if (!found_it)
  902. return;
  903. /* If a driver is bound, then notify the driver. */
  904. if (wblock->dev.dev.driver) {
  905. struct wmi_driver *driver;
  906. struct acpi_object_list input;
  907. union acpi_object params[1];
  908. struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
  909. acpi_status status;
  910. driver = container_of(wblock->dev.dev.driver,
  911. struct wmi_driver, driver);
  912. input.count = 1;
  913. input.pointer = params;
  914. params[0].type = ACPI_TYPE_INTEGER;
  915. params[0].integer.value = event;
  916. status = acpi_evaluate_object(wblock->acpi_device->handle,
  917. "_WED", &input, &evdata);
  918. if (ACPI_FAILURE(status)) {
  919. dev_warn(&wblock->dev.dev,
  920. "failed to get event data\n");
  921. return;
  922. }
  923. if (driver->notify)
  924. driver->notify(&wblock->dev,
  925. (union acpi_object *)evdata.pointer);
  926. kfree(evdata.pointer);
  927. } else if (wblock->handler) {
  928. /* Legacy handler */
  929. wblock->handler(event, wblock->handler_data);
  930. }
  931. if (debug_event) {
  932. pr_info("DEBUG Event GUID: %pUL\n",
  933. wblock->gblock.guid);
  934. }
  935. acpi_bus_generate_netlink_event(
  936. wblock->acpi_device->pnp.device_class,
  937. dev_name(&wblock->dev.dev),
  938. event, 0);
  939. }
  940. static int acpi_wmi_remove(struct platform_device *device)
  941. {
  942. struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
  943. acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
  944. acpi_wmi_notify_handler);
  945. acpi_remove_address_space_handler(acpi_device->handle,
  946. ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
  947. wmi_free_devices(acpi_device);
  948. device_unregister((struct device *)dev_get_drvdata(&device->dev));
  949. return 0;
  950. }
  951. static int acpi_wmi_probe(struct platform_device *device)
  952. {
  953. struct acpi_device *acpi_device;
  954. struct device *wmi_bus_dev;
  955. acpi_status status;
  956. int error;
  957. acpi_device = ACPI_COMPANION(&device->dev);
  958. if (!acpi_device) {
  959. dev_err(&device->dev, "ACPI companion is missing\n");
  960. return -ENODEV;
  961. }
  962. status = acpi_install_address_space_handler(acpi_device->handle,
  963. ACPI_ADR_SPACE_EC,
  964. &acpi_wmi_ec_space_handler,
  965. NULL, NULL);
  966. if (ACPI_FAILURE(status)) {
  967. dev_err(&device->dev, "Error installing EC region handler\n");
  968. return -ENODEV;
  969. }
  970. status = acpi_install_notify_handler(acpi_device->handle,
  971. ACPI_DEVICE_NOTIFY,
  972. acpi_wmi_notify_handler,
  973. NULL);
  974. if (ACPI_FAILURE(status)) {
  975. dev_err(&device->dev, "Error installing notify handler\n");
  976. error = -ENODEV;
  977. goto err_remove_ec_handler;
  978. }
  979. wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
  980. NULL, "wmi_bus-%s", dev_name(&device->dev));
  981. if (IS_ERR(wmi_bus_dev)) {
  982. error = PTR_ERR(wmi_bus_dev);
  983. goto err_remove_notify_handler;
  984. }
  985. dev_set_drvdata(&device->dev, wmi_bus_dev);
  986. error = parse_wdg(wmi_bus_dev, acpi_device);
  987. if (error) {
  988. pr_err("Failed to parse WDG method\n");
  989. goto err_remove_busdev;
  990. }
  991. return 0;
  992. err_remove_busdev:
  993. device_unregister(wmi_bus_dev);
  994. err_remove_notify_handler:
  995. acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
  996. acpi_wmi_notify_handler);
  997. err_remove_ec_handler:
  998. acpi_remove_address_space_handler(acpi_device->handle,
  999. ACPI_ADR_SPACE_EC,
  1000. &acpi_wmi_ec_space_handler);
  1001. return error;
  1002. }
  1003. int __must_check __wmi_driver_register(struct wmi_driver *driver,
  1004. struct module *owner)
  1005. {
  1006. driver->driver.owner = owner;
  1007. driver->driver.bus = &wmi_bus_type;
  1008. return driver_register(&driver->driver);
  1009. }
  1010. EXPORT_SYMBOL(__wmi_driver_register);
  1011. void wmi_driver_unregister(struct wmi_driver *driver)
  1012. {
  1013. driver_unregister(&driver->driver);
  1014. }
  1015. EXPORT_SYMBOL(wmi_driver_unregister);
  1016. static int __init acpi_wmi_init(void)
  1017. {
  1018. int error;
  1019. if (acpi_disabled)
  1020. return -ENODEV;
  1021. error = class_register(&wmi_bus_class);
  1022. if (error)
  1023. return error;
  1024. error = bus_register(&wmi_bus_type);
  1025. if (error)
  1026. goto err_unreg_class;
  1027. error = platform_driver_register(&acpi_wmi_driver);
  1028. if (error) {
  1029. pr_err("Error loading mapper\n");
  1030. goto err_unreg_bus;
  1031. }
  1032. return 0;
  1033. err_unreg_class:
  1034. class_unregister(&wmi_bus_class);
  1035. err_unreg_bus:
  1036. bus_unregister(&wmi_bus_type);
  1037. return error;
  1038. }
  1039. static void __exit acpi_wmi_exit(void)
  1040. {
  1041. platform_driver_unregister(&acpi_wmi_driver);
  1042. class_unregister(&wmi_bus_class);
  1043. bus_unregister(&wmi_bus_type);
  1044. }
  1045. subsys_initcall(acpi_wmi_init);
  1046. module_exit(acpi_wmi_exit);