wmi.c 24 KB

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