wmi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or (at
  16. * your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  26. *
  27. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. */
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/kernel.h>
  31. #include <linux/init.h>
  32. #include <linux/types.h>
  33. #include <linux/device.h>
  34. #include <linux/list.h>
  35. #include <linux/acpi.h>
  36. #include <linux/slab.h>
  37. #include <linux/module.h>
  38. #include <acpi/acpi_bus.h>
  39. #include <acpi/acpi_drivers.h>
  40. ACPI_MODULE_NAME("wmi");
  41. MODULE_AUTHOR("Carlos Corbacho");
  42. MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
  43. MODULE_LICENSE("GPL");
  44. #define ACPI_WMI_CLASS "wmi"
  45. static DEFINE_MUTEX(wmi_data_lock);
  46. static LIST_HEAD(wmi_block_list);
  47. struct guid_block {
  48. char guid[16];
  49. union {
  50. char object_id[2];
  51. struct {
  52. unsigned char notify_id;
  53. unsigned char reserved;
  54. };
  55. };
  56. u8 instance_count;
  57. u8 flags;
  58. };
  59. struct wmi_block {
  60. struct list_head list;
  61. struct guid_block gblock;
  62. acpi_handle handle;
  63. wmi_notify_handler handler;
  64. void *handler_data;
  65. struct device dev;
  66. };
  67. /*
  68. * If the GUID data block is marked as expensive, we must enable and
  69. * explicitily disable data collection.
  70. */
  71. #define ACPI_WMI_EXPENSIVE 0x1
  72. #define ACPI_WMI_METHOD 0x2 /* GUID is a method */
  73. #define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
  74. #define ACPI_WMI_EVENT 0x8 /* GUID is an event */
  75. static bool debug_event;
  76. module_param(debug_event, bool, 0444);
  77. MODULE_PARM_DESC(debug_event,
  78. "Log WMI Events [0/1]");
  79. static bool debug_dump_wdg;
  80. module_param(debug_dump_wdg, bool, 0444);
  81. MODULE_PARM_DESC(debug_dump_wdg,
  82. "Dump available WMI interfaces [0/1]");
  83. static int acpi_wmi_remove(struct acpi_device *device);
  84. static int acpi_wmi_add(struct acpi_device *device);
  85. static void acpi_wmi_notify(struct acpi_device *device, u32 event);
  86. static const struct acpi_device_id wmi_device_ids[] = {
  87. {"PNP0C14", 0},
  88. {"pnp0c14", 0},
  89. {"", 0},
  90. };
  91. MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
  92. static struct acpi_driver acpi_wmi_driver = {
  93. .name = "wmi",
  94. .class = ACPI_WMI_CLASS,
  95. .ids = wmi_device_ids,
  96. .ops = {
  97. .add = acpi_wmi_add,
  98. .remove = acpi_wmi_remove,
  99. .notify = acpi_wmi_notify,
  100. },
  101. };
  102. /*
  103. * GUID parsing functions
  104. */
  105. /**
  106. * wmi_parse_hexbyte - Convert a ASCII hex number to a byte
  107. * @src: Pointer to at least 2 characters to convert.
  108. *
  109. * Convert a two character ASCII hex string to a number.
  110. *
  111. * Return: 0-255 Success, the byte was parsed correctly
  112. * -1 Error, an invalid character was supplied
  113. */
  114. static int wmi_parse_hexbyte(const u8 *src)
  115. {
  116. int h;
  117. int value;
  118. /* high part */
  119. h = value = hex_to_bin(src[0]);
  120. if (value < 0)
  121. return -1;
  122. /* low part */
  123. value = hex_to_bin(src[1]);
  124. if (value >= 0)
  125. return (h << 4) | value;
  126. return -1;
  127. }
  128. /**
  129. * wmi_swap_bytes - Rearrange GUID bytes to match GUID binary
  130. * @src: Memory block holding binary GUID (16 bytes)
  131. * @dest: Memory block to hold byte swapped binary GUID (16 bytes)
  132. *
  133. * Byte swap a binary GUID to match it's real GUID value
  134. */
  135. static void wmi_swap_bytes(u8 *src, u8 *dest)
  136. {
  137. int i;
  138. for (i = 0; i <= 3; i++)
  139. memcpy(dest + i, src + (3 - i), 1);
  140. for (i = 0; i <= 1; i++)
  141. memcpy(dest + 4 + i, src + (5 - i), 1);
  142. for (i = 0; i <= 1; i++)
  143. memcpy(dest + 6 + i, src + (7 - i), 1);
  144. memcpy(dest + 8, src + 8, 8);
  145. }
  146. /**
  147. * wmi_parse_guid - Convert GUID from ASCII to binary
  148. * @src: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  149. * @dest: Memory block to hold binary GUID (16 bytes)
  150. *
  151. * N.B. The GUID need not be NULL terminated.
  152. *
  153. * Return: 'true' @dest contains binary GUID
  154. * 'false' @dest contents are undefined
  155. */
  156. static bool wmi_parse_guid(const u8 *src, u8 *dest)
  157. {
  158. static const int size[] = { 4, 2, 2, 2, 6 };
  159. int i, j, v;
  160. if (src[8] != '-' || src[13] != '-' ||
  161. src[18] != '-' || src[23] != '-')
  162. return false;
  163. for (j = 0; j < 5; j++, src++) {
  164. for (i = 0; i < size[j]; i++, src += 2, *dest++ = v) {
  165. v = wmi_parse_hexbyte(src);
  166. if (v < 0)
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. /*
  173. * Convert a raw GUID to the ACII string representation
  174. */
  175. static int wmi_gtoa(const char *in, char *out)
  176. {
  177. int i;
  178. for (i = 3; i >= 0; i--)
  179. out += sprintf(out, "%02X", in[i] & 0xFF);
  180. out += sprintf(out, "-");
  181. out += sprintf(out, "%02X", in[5] & 0xFF);
  182. out += sprintf(out, "%02X", in[4] & 0xFF);
  183. out += sprintf(out, "-");
  184. out += sprintf(out, "%02X", in[7] & 0xFF);
  185. out += sprintf(out, "%02X", in[6] & 0xFF);
  186. out += sprintf(out, "-");
  187. out += sprintf(out, "%02X", in[8] & 0xFF);
  188. out += sprintf(out, "%02X", in[9] & 0xFF);
  189. out += sprintf(out, "-");
  190. for (i = 10; i <= 15; i++)
  191. out += sprintf(out, "%02X", in[i] & 0xFF);
  192. *out = '\0';
  193. return 0;
  194. }
  195. static bool find_guid(const char *guid_string, struct wmi_block **out)
  196. {
  197. char tmp[16], guid_input[16];
  198. struct wmi_block *wblock;
  199. struct guid_block *block;
  200. struct list_head *p;
  201. wmi_parse_guid(guid_string, tmp);
  202. wmi_swap_bytes(tmp, guid_input);
  203. list_for_each(p, &wmi_block_list) {
  204. wblock = list_entry(p, struct wmi_block, list);
  205. block = &wblock->gblock;
  206. if (memcmp(block->guid, guid_input, 16) == 0) {
  207. if (out)
  208. *out = wblock;
  209. return 1;
  210. }
  211. }
  212. return 0;
  213. }
  214. static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
  215. {
  216. struct guid_block *block = NULL;
  217. char method[5];
  218. acpi_status status;
  219. acpi_handle handle;
  220. block = &wblock->gblock;
  221. handle = wblock->handle;
  222. if (!block)
  223. return AE_NOT_EXIST;
  224. snprintf(method, 5, "WE%02X", block->notify_id);
  225. status = acpi_execute_simple_method(handle, method, enable);
  226. if (status != AE_OK && status != AE_NOT_FOUND)
  227. return status;
  228. else
  229. return AE_OK;
  230. }
  231. /*
  232. * Exported WMI functions
  233. */
  234. /**
  235. * wmi_evaluate_method - Evaluate a WMI method
  236. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  237. * @instance: Instance index
  238. * @method_id: Method ID to call
  239. * &in: Buffer containing input for the method call
  240. * &out: Empty buffer to return the method results
  241. *
  242. * Call an ACPI-WMI method
  243. */
  244. acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
  245. u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
  246. {
  247. struct guid_block *block = NULL;
  248. struct wmi_block *wblock = NULL;
  249. acpi_handle handle;
  250. acpi_status status;
  251. struct acpi_object_list input;
  252. union acpi_object params[3];
  253. char method[5] = "WM";
  254. if (!find_guid(guid_string, &wblock))
  255. return AE_ERROR;
  256. block = &wblock->gblock;
  257. handle = wblock->handle;
  258. if (!(block->flags & ACPI_WMI_METHOD))
  259. return AE_BAD_DATA;
  260. if (block->instance_count < instance)
  261. return AE_BAD_PARAMETER;
  262. input.count = 2;
  263. input.pointer = params;
  264. params[0].type = ACPI_TYPE_INTEGER;
  265. params[0].integer.value = instance;
  266. params[1].type = ACPI_TYPE_INTEGER;
  267. params[1].integer.value = method_id;
  268. if (in) {
  269. input.count = 3;
  270. if (block->flags & ACPI_WMI_STRING) {
  271. params[2].type = ACPI_TYPE_STRING;
  272. } else {
  273. params[2].type = ACPI_TYPE_BUFFER;
  274. }
  275. params[2].buffer.length = in->length;
  276. params[2].buffer.pointer = in->pointer;
  277. }
  278. strncat(method, block->object_id, 2);
  279. status = acpi_evaluate_object(handle, method, &input, out);
  280. return status;
  281. }
  282. EXPORT_SYMBOL_GPL(wmi_evaluate_method);
  283. /**
  284. * wmi_query_block - Return contents of a WMI block
  285. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  286. * @instance: Instance index
  287. * &out: Empty buffer to return the contents of the data block to
  288. *
  289. * Return the contents of an ACPI-WMI data block to a buffer
  290. */
  291. acpi_status wmi_query_block(const char *guid_string, u8 instance,
  292. struct acpi_buffer *out)
  293. {
  294. struct guid_block *block = NULL;
  295. struct wmi_block *wblock = NULL;
  296. acpi_handle handle;
  297. acpi_status status, wc_status = AE_ERROR;
  298. struct acpi_object_list input;
  299. union acpi_object wq_params[1];
  300. char method[5];
  301. char wc_method[5] = "WC";
  302. if (!guid_string || !out)
  303. return AE_BAD_PARAMETER;
  304. if (!find_guid(guid_string, &wblock))
  305. return AE_ERROR;
  306. block = &wblock->gblock;
  307. handle = wblock->handle;
  308. if (block->instance_count < instance)
  309. return AE_BAD_PARAMETER;
  310. /* Check GUID is a data block */
  311. if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
  312. return AE_ERROR;
  313. input.count = 1;
  314. input.pointer = wq_params;
  315. wq_params[0].type = ACPI_TYPE_INTEGER;
  316. wq_params[0].integer.value = instance;
  317. /*
  318. * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
  319. * enable collection.
  320. */
  321. if (block->flags & ACPI_WMI_EXPENSIVE) {
  322. strncat(wc_method, block->object_id, 2);
  323. /*
  324. * Some GUIDs break the specification by declaring themselves
  325. * expensive, but have no corresponding WCxx method. So we
  326. * should not fail if this happens.
  327. */
  328. if (acpi_has_method(handle, wc_method))
  329. wc_status = acpi_execute_simple_method(handle,
  330. wc_method, 1);
  331. }
  332. strcpy(method, "WQ");
  333. strncat(method, block->object_id, 2);
  334. status = acpi_evaluate_object(handle, method, &input, out);
  335. /*
  336. * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
  337. * the WQxx method failed - we should disable collection anyway.
  338. */
  339. if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
  340. status = acpi_execute_simple_method(handle, wc_method, 0);
  341. }
  342. return status;
  343. }
  344. EXPORT_SYMBOL_GPL(wmi_query_block);
  345. /**
  346. * wmi_set_block - Write to a WMI block
  347. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  348. * @instance: Instance index
  349. * &in: Buffer containing new values for the data block
  350. *
  351. * Write the contents of the input buffer to an ACPI-WMI data block
  352. */
  353. acpi_status wmi_set_block(const char *guid_string, u8 instance,
  354. const struct acpi_buffer *in)
  355. {
  356. struct guid_block *block = NULL;
  357. struct wmi_block *wblock = NULL;
  358. acpi_handle handle;
  359. struct acpi_object_list input;
  360. union acpi_object params[2];
  361. char method[5] = "WS";
  362. if (!guid_string || !in)
  363. return AE_BAD_DATA;
  364. if (!find_guid(guid_string, &wblock))
  365. return AE_ERROR;
  366. block = &wblock->gblock;
  367. handle = wblock->handle;
  368. if (block->instance_count < instance)
  369. return AE_BAD_PARAMETER;
  370. /* Check GUID is a data block */
  371. if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
  372. return AE_ERROR;
  373. input.count = 2;
  374. input.pointer = params;
  375. params[0].type = ACPI_TYPE_INTEGER;
  376. params[0].integer.value = instance;
  377. if (block->flags & ACPI_WMI_STRING) {
  378. params[1].type = ACPI_TYPE_STRING;
  379. } else {
  380. params[1].type = ACPI_TYPE_BUFFER;
  381. }
  382. params[1].buffer.length = in->length;
  383. params[1].buffer.pointer = in->pointer;
  384. strncat(method, block->object_id, 2);
  385. return acpi_evaluate_object(handle, method, &input, NULL);
  386. }
  387. EXPORT_SYMBOL_GPL(wmi_set_block);
  388. static void wmi_dump_wdg(const struct guid_block *g)
  389. {
  390. char guid_string[37];
  391. wmi_gtoa(g->guid, guid_string);
  392. pr_info("%s:\n", guid_string);
  393. pr_info("\tobject_id: %c%c\n", g->object_id[0], g->object_id[1]);
  394. pr_info("\tnotify_id: %02X\n", g->notify_id);
  395. pr_info("\treserved: %02X\n", g->reserved);
  396. pr_info("\tinstance_count: %d\n", g->instance_count);
  397. pr_info("\tflags: %#x", g->flags);
  398. if (g->flags) {
  399. if (g->flags & ACPI_WMI_EXPENSIVE)
  400. pr_cont(" ACPI_WMI_EXPENSIVE");
  401. if (g->flags & ACPI_WMI_METHOD)
  402. pr_cont(" ACPI_WMI_METHOD");
  403. if (g->flags & ACPI_WMI_STRING)
  404. pr_cont(" ACPI_WMI_STRING");
  405. if (g->flags & ACPI_WMI_EVENT)
  406. pr_cont(" ACPI_WMI_EVENT");
  407. }
  408. pr_cont("\n");
  409. }
  410. static void wmi_notify_debug(u32 value, void *context)
  411. {
  412. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  413. union acpi_object *obj;
  414. acpi_status status;
  415. status = wmi_get_event_data(value, &response);
  416. if (status != AE_OK) {
  417. pr_info("bad event status 0x%x\n", status);
  418. return;
  419. }
  420. obj = (union acpi_object *)response.pointer;
  421. if (!obj)
  422. return;
  423. pr_info("DEBUG Event ");
  424. switch(obj->type) {
  425. case ACPI_TYPE_BUFFER:
  426. pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
  427. break;
  428. case ACPI_TYPE_STRING:
  429. pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
  430. break;
  431. case ACPI_TYPE_INTEGER:
  432. pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
  433. break;
  434. case ACPI_TYPE_PACKAGE:
  435. pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
  436. break;
  437. default:
  438. pr_cont("object type 0x%X\n", obj->type);
  439. }
  440. kfree(obj);
  441. }
  442. /**
  443. * wmi_install_notify_handler - Register handler for WMI events
  444. * @handler: Function to handle notifications
  445. * @data: Data to be returned to handler when event is fired
  446. *
  447. * Register a handler for events sent to the ACPI-WMI mapper device.
  448. */
  449. acpi_status wmi_install_notify_handler(const char *guid,
  450. wmi_notify_handler handler, void *data)
  451. {
  452. struct wmi_block *block;
  453. acpi_status status = AE_NOT_EXIST;
  454. char tmp[16], guid_input[16];
  455. struct list_head *p;
  456. if (!guid || !handler)
  457. return AE_BAD_PARAMETER;
  458. wmi_parse_guid(guid, tmp);
  459. wmi_swap_bytes(tmp, guid_input);
  460. list_for_each(p, &wmi_block_list) {
  461. acpi_status wmi_status;
  462. block = list_entry(p, struct wmi_block, list);
  463. if (memcmp(block->gblock.guid, guid_input, 16) == 0) {
  464. if (block->handler &&
  465. block->handler != wmi_notify_debug)
  466. return AE_ALREADY_ACQUIRED;
  467. block->handler = handler;
  468. block->handler_data = data;
  469. wmi_status = wmi_method_enable(block, 1);
  470. if ((wmi_status != AE_OK) ||
  471. ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
  472. status = wmi_status;
  473. }
  474. }
  475. return status;
  476. }
  477. EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
  478. /**
  479. * wmi_uninstall_notify_handler - Unregister handler for WMI events
  480. *
  481. * Unregister handler for events sent to the ACPI-WMI mapper device.
  482. */
  483. acpi_status wmi_remove_notify_handler(const char *guid)
  484. {
  485. struct wmi_block *block;
  486. acpi_status status = AE_NOT_EXIST;
  487. char tmp[16], guid_input[16];
  488. struct list_head *p;
  489. if (!guid)
  490. return AE_BAD_PARAMETER;
  491. wmi_parse_guid(guid, tmp);
  492. wmi_swap_bytes(tmp, guid_input);
  493. list_for_each(p, &wmi_block_list) {
  494. acpi_status wmi_status;
  495. block = list_entry(p, struct wmi_block, list);
  496. if (memcmp(block->gblock.guid, guid_input, 16) == 0) {
  497. if (!block->handler ||
  498. block->handler == wmi_notify_debug)
  499. return AE_NULL_ENTRY;
  500. if (debug_event) {
  501. block->handler = wmi_notify_debug;
  502. status = AE_OK;
  503. } else {
  504. wmi_status = wmi_method_enable(block, 0);
  505. block->handler = NULL;
  506. block->handler_data = NULL;
  507. if ((wmi_status != AE_OK) ||
  508. ((wmi_status == AE_OK) &&
  509. (status == AE_NOT_EXIST)))
  510. status = wmi_status;
  511. }
  512. }
  513. }
  514. return status;
  515. }
  516. EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
  517. /**
  518. * wmi_get_event_data - Get WMI data associated with an event
  519. *
  520. * @event: Event to find
  521. * @out: Buffer to hold event data. out->pointer should be freed with kfree()
  522. *
  523. * Returns extra data associated with an event in WMI.
  524. */
  525. acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
  526. {
  527. struct acpi_object_list input;
  528. union acpi_object params[1];
  529. struct guid_block *gblock;
  530. struct wmi_block *wblock;
  531. struct list_head *p;
  532. input.count = 1;
  533. input.pointer = params;
  534. params[0].type = ACPI_TYPE_INTEGER;
  535. params[0].integer.value = event;
  536. list_for_each(p, &wmi_block_list) {
  537. wblock = list_entry(p, struct wmi_block, list);
  538. gblock = &wblock->gblock;
  539. if ((gblock->flags & ACPI_WMI_EVENT) &&
  540. (gblock->notify_id == event))
  541. return acpi_evaluate_object(wblock->handle, "_WED",
  542. &input, out);
  543. }
  544. return AE_NOT_FOUND;
  545. }
  546. EXPORT_SYMBOL_GPL(wmi_get_event_data);
  547. /**
  548. * wmi_has_guid - Check if a GUID is available
  549. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  550. *
  551. * Check if a given GUID is defined by _WDG
  552. */
  553. bool wmi_has_guid(const char *guid_string)
  554. {
  555. return find_guid(guid_string, NULL);
  556. }
  557. EXPORT_SYMBOL_GPL(wmi_has_guid);
  558. /*
  559. * sysfs interface
  560. */
  561. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  562. char *buf)
  563. {
  564. char guid_string[37];
  565. struct wmi_block *wblock;
  566. wblock = dev_get_drvdata(dev);
  567. if (!wblock) {
  568. strcat(buf, "\n");
  569. return strlen(buf);
  570. }
  571. wmi_gtoa(wblock->gblock.guid, guid_string);
  572. return sprintf(buf, "wmi:%s\n", guid_string);
  573. }
  574. static DEVICE_ATTR_RO(modalias);
  575. static struct attribute *wmi_attrs[] = {
  576. &dev_attr_modalias.attr,
  577. NULL,
  578. };
  579. ATTRIBUTE_GROUPS(wmi);
  580. static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  581. {
  582. char guid_string[37];
  583. struct wmi_block *wblock;
  584. if (add_uevent_var(env, "MODALIAS="))
  585. return -ENOMEM;
  586. wblock = dev_get_drvdata(dev);
  587. if (!wblock)
  588. return -ENOMEM;
  589. wmi_gtoa(wblock->gblock.guid, guid_string);
  590. strcpy(&env->buf[env->buflen - 1], "wmi:");
  591. memcpy(&env->buf[env->buflen - 1 + 4], guid_string, 36);
  592. env->buflen += 40;
  593. return 0;
  594. }
  595. static void wmi_dev_free(struct device *dev)
  596. {
  597. struct wmi_block *wmi_block = container_of(dev, struct wmi_block, dev);
  598. kfree(wmi_block);
  599. }
  600. static struct class wmi_class = {
  601. .name = "wmi",
  602. .dev_release = wmi_dev_free,
  603. .dev_uevent = wmi_dev_uevent,
  604. .dev_groups = wmi_groups,
  605. };
  606. static int wmi_create_device(const struct guid_block *gblock,
  607. struct wmi_block *wblock, acpi_handle handle)
  608. {
  609. char guid_string[37];
  610. wblock->dev.class = &wmi_class;
  611. wmi_gtoa(gblock->guid, guid_string);
  612. dev_set_name(&wblock->dev, "%s", guid_string);
  613. dev_set_drvdata(&wblock->dev, wblock);
  614. return device_register(&wblock->dev);
  615. }
  616. static void wmi_free_devices(void)
  617. {
  618. struct wmi_block *wblock, *next;
  619. /* Delete devices for all the GUIDs */
  620. list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
  621. list_del(&wblock->list);
  622. if (wblock->dev.class)
  623. device_unregister(&wblock->dev);
  624. else
  625. kfree(wblock);
  626. }
  627. }
  628. static bool guid_already_parsed(const char *guid_string)
  629. {
  630. struct wmi_block *wblock;
  631. list_for_each_entry(wblock, &wmi_block_list, list)
  632. if (memcmp(wblock->gblock.guid, guid_string, 16) == 0)
  633. return true;
  634. return false;
  635. }
  636. /*
  637. * Parse the _WDG method for the GUID data blocks
  638. */
  639. static int parse_wdg(acpi_handle handle)
  640. {
  641. struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
  642. union acpi_object *obj;
  643. const struct guid_block *gblock;
  644. struct wmi_block *wblock;
  645. acpi_status status;
  646. int retval;
  647. u32 i, total;
  648. status = acpi_evaluate_object(handle, "_WDG", NULL, &out);
  649. if (ACPI_FAILURE(status))
  650. return -ENXIO;
  651. obj = (union acpi_object *) out.pointer;
  652. if (!obj)
  653. return -ENXIO;
  654. if (obj->type != ACPI_TYPE_BUFFER) {
  655. retval = -ENXIO;
  656. goto out_free_pointer;
  657. }
  658. gblock = (const struct guid_block *)obj->buffer.pointer;
  659. total = obj->buffer.length / sizeof(struct guid_block);
  660. for (i = 0; i < total; i++) {
  661. if (debug_dump_wdg)
  662. wmi_dump_wdg(&gblock[i]);
  663. wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
  664. if (!wblock)
  665. return -ENOMEM;
  666. wblock->handle = handle;
  667. wblock->gblock = gblock[i];
  668. /*
  669. Some WMI devices, like those for nVidia hooks, have a
  670. duplicate GUID. It's not clear what we should do in this
  671. case yet, so for now, we'll just ignore the duplicate
  672. for device creation.
  673. */
  674. if (!guid_already_parsed(gblock[i].guid)) {
  675. retval = wmi_create_device(&gblock[i], wblock, handle);
  676. if (retval) {
  677. wmi_free_devices();
  678. goto out_free_pointer;
  679. }
  680. }
  681. list_add_tail(&wblock->list, &wmi_block_list);
  682. if (debug_event) {
  683. wblock->handler = wmi_notify_debug;
  684. wmi_method_enable(wblock, 1);
  685. }
  686. }
  687. retval = 0;
  688. out_free_pointer:
  689. kfree(out.pointer);
  690. return retval;
  691. }
  692. /*
  693. * WMI can have EmbeddedControl access regions. In which case, we just want to
  694. * hand these off to the EC driver.
  695. */
  696. static acpi_status
  697. acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
  698. u32 bits, u64 *value,
  699. void *handler_context, void *region_context)
  700. {
  701. int result = 0, i = 0;
  702. u8 temp = 0;
  703. if ((address > 0xFF) || !value)
  704. return AE_BAD_PARAMETER;
  705. if (function != ACPI_READ && function != ACPI_WRITE)
  706. return AE_BAD_PARAMETER;
  707. if (bits != 8)
  708. return AE_BAD_PARAMETER;
  709. if (function == ACPI_READ) {
  710. result = ec_read(address, &temp);
  711. (*value) |= ((u64)temp) << i;
  712. } else {
  713. temp = 0xff & ((*value) >> i);
  714. result = ec_write(address, temp);
  715. }
  716. switch (result) {
  717. case -EINVAL:
  718. return AE_BAD_PARAMETER;
  719. break;
  720. case -ENODEV:
  721. return AE_NOT_FOUND;
  722. break;
  723. case -ETIME:
  724. return AE_TIME;
  725. break;
  726. default:
  727. return AE_OK;
  728. }
  729. }
  730. static void acpi_wmi_notify(struct acpi_device *device, u32 event)
  731. {
  732. struct guid_block *block;
  733. struct wmi_block *wblock;
  734. struct list_head *p;
  735. char guid_string[37];
  736. list_for_each(p, &wmi_block_list) {
  737. wblock = list_entry(p, struct wmi_block, list);
  738. block = &wblock->gblock;
  739. if ((block->flags & ACPI_WMI_EVENT) &&
  740. (block->notify_id == event)) {
  741. if (wblock->handler)
  742. wblock->handler(event, wblock->handler_data);
  743. if (debug_event) {
  744. wmi_gtoa(wblock->gblock.guid, guid_string);
  745. pr_info("DEBUG Event GUID: %s\n", guid_string);
  746. }
  747. acpi_bus_generate_netlink_event(
  748. device->pnp.device_class, dev_name(&device->dev),
  749. event, 0);
  750. break;
  751. }
  752. }
  753. }
  754. static int acpi_wmi_remove(struct acpi_device *device)
  755. {
  756. acpi_remove_address_space_handler(device->handle,
  757. ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
  758. wmi_free_devices();
  759. return 0;
  760. }
  761. static int acpi_wmi_add(struct acpi_device *device)
  762. {
  763. acpi_status status;
  764. int error;
  765. status = acpi_install_address_space_handler(device->handle,
  766. ACPI_ADR_SPACE_EC,
  767. &acpi_wmi_ec_space_handler,
  768. NULL, NULL);
  769. if (ACPI_FAILURE(status)) {
  770. pr_err("Error installing EC region handler\n");
  771. return -ENODEV;
  772. }
  773. error = parse_wdg(device->handle);
  774. if (error) {
  775. acpi_remove_address_space_handler(device->handle,
  776. ACPI_ADR_SPACE_EC,
  777. &acpi_wmi_ec_space_handler);
  778. pr_err("Failed to parse WDG method\n");
  779. return error;
  780. }
  781. return 0;
  782. }
  783. static int __init acpi_wmi_init(void)
  784. {
  785. int error;
  786. if (acpi_disabled)
  787. return -ENODEV;
  788. error = class_register(&wmi_class);
  789. if (error)
  790. return error;
  791. error = acpi_bus_register_driver(&acpi_wmi_driver);
  792. if (error) {
  793. pr_err("Error loading mapper\n");
  794. class_unregister(&wmi_class);
  795. return error;
  796. }
  797. pr_info("Mapper loaded\n");
  798. return 0;
  799. }
  800. static void __exit acpi_wmi_exit(void)
  801. {
  802. acpi_bus_unregister_driver(&acpi_wmi_driver);
  803. class_unregister(&wmi_class);
  804. pr_info("Mapper unloaded\n");
  805. }
  806. subsys_initcall(acpi_wmi_init);
  807. module_exit(acpi_wmi_exit);