wmi.c 23 KB

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