wmi.c 20 KB

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