wmi.c 22 KB

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