wmi.c 21 KB

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