wmi.c 20 KB

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