wmi.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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. * WMI bus infrastructure by Andrew Lutomirski and Darren Hart:
  12. * Copyright (C) 2015 Andrew Lutomirski
  13. * Copyright (C) 2017 VMware, Inc. All Rights Reserved.
  14. *
  15. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or (at
  20. * your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful, but
  23. * WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. * General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License along
  28. * with this program; if not, write to the Free Software Foundation, Inc.,
  29. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  30. *
  31. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  32. */
  33. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  34. #include <linux/acpi.h>
  35. #include <linux/device.h>
  36. #include <linux/init.h>
  37. #include <linux/kernel.h>
  38. #include <linux/list.h>
  39. #include <linux/miscdevice.h>
  40. #include <linux/module.h>
  41. #include <linux/platform_device.h>
  42. #include <linux/slab.h>
  43. #include <linux/types.h>
  44. #include <linux/uaccess.h>
  45. #include <linux/uuid.h>
  46. #include <linux/wmi.h>
  47. #include <uapi/linux/wmi.h>
  48. ACPI_MODULE_NAME("wmi");
  49. MODULE_AUTHOR("Carlos Corbacho");
  50. MODULE_DESCRIPTION("ACPI-WMI Mapping Driver");
  51. MODULE_LICENSE("GPL");
  52. static LIST_HEAD(wmi_block_list);
  53. struct guid_block {
  54. char guid[16];
  55. union {
  56. char object_id[2];
  57. struct {
  58. unsigned char notify_id;
  59. unsigned char reserved;
  60. };
  61. };
  62. u8 instance_count;
  63. u8 flags;
  64. };
  65. struct wmi_block {
  66. struct wmi_device dev;
  67. struct list_head list;
  68. struct guid_block gblock;
  69. struct miscdevice char_dev;
  70. struct mutex char_mutex;
  71. struct acpi_device *acpi_device;
  72. wmi_notify_handler handler;
  73. void *handler_data;
  74. u64 req_buf_size;
  75. bool read_takes_no_args;
  76. };
  77. /*
  78. * If the GUID data block is marked as expensive, we must enable and
  79. * explicitily disable data collection.
  80. */
  81. #define ACPI_WMI_EXPENSIVE 0x1
  82. #define ACPI_WMI_METHOD 0x2 /* GUID is a method */
  83. #define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */
  84. #define ACPI_WMI_EVENT 0x8 /* GUID is an event */
  85. static bool debug_event;
  86. module_param(debug_event, bool, 0444);
  87. MODULE_PARM_DESC(debug_event,
  88. "Log WMI Events [0/1]");
  89. static bool debug_dump_wdg;
  90. module_param(debug_dump_wdg, bool, 0444);
  91. MODULE_PARM_DESC(debug_dump_wdg,
  92. "Dump available WMI interfaces [0/1]");
  93. static int acpi_wmi_remove(struct platform_device *device);
  94. static int acpi_wmi_probe(struct platform_device *device);
  95. static const struct acpi_device_id wmi_device_ids[] = {
  96. {"PNP0C14", 0},
  97. {"pnp0c14", 0},
  98. {"", 0},
  99. };
  100. MODULE_DEVICE_TABLE(acpi, wmi_device_ids);
  101. static struct platform_driver acpi_wmi_driver = {
  102. .driver = {
  103. .name = "acpi-wmi",
  104. .acpi_match_table = wmi_device_ids,
  105. },
  106. .probe = acpi_wmi_probe,
  107. .remove = acpi_wmi_remove,
  108. };
  109. /*
  110. * GUID parsing functions
  111. */
  112. static bool find_guid(const char *guid_string, struct wmi_block **out)
  113. {
  114. uuid_le guid_input;
  115. struct wmi_block *wblock;
  116. struct guid_block *block;
  117. struct list_head *p;
  118. if (uuid_le_to_bin(guid_string, &guid_input))
  119. return false;
  120. list_for_each(p, &wmi_block_list) {
  121. wblock = list_entry(p, struct wmi_block, list);
  122. block = &wblock->gblock;
  123. if (memcmp(block->guid, &guid_input, 16) == 0) {
  124. if (out)
  125. *out = wblock;
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. static int get_subobj_info(acpi_handle handle, const char *pathname,
  132. struct acpi_device_info **info)
  133. {
  134. struct acpi_device_info *dummy_info, **info_ptr;
  135. acpi_handle subobj_handle;
  136. acpi_status status;
  137. status = acpi_get_handle(handle, (char *)pathname, &subobj_handle);
  138. if (status == AE_NOT_FOUND)
  139. return -ENOENT;
  140. else if (ACPI_FAILURE(status))
  141. return -EIO;
  142. info_ptr = info ? info : &dummy_info;
  143. status = acpi_get_object_info(subobj_handle, info_ptr);
  144. if (ACPI_FAILURE(status))
  145. return -EIO;
  146. if (!info)
  147. kfree(dummy_info);
  148. return 0;
  149. }
  150. static acpi_status wmi_method_enable(struct wmi_block *wblock, int enable)
  151. {
  152. struct guid_block *block = NULL;
  153. char method[5];
  154. acpi_status status;
  155. acpi_handle handle;
  156. block = &wblock->gblock;
  157. handle = wblock->acpi_device->handle;
  158. snprintf(method, 5, "WE%02X", block->notify_id);
  159. status = acpi_execute_simple_method(handle, method, enable);
  160. if (status != AE_OK && status != AE_NOT_FOUND)
  161. return status;
  162. else
  163. return AE_OK;
  164. }
  165. /*
  166. * Exported WMI functions
  167. */
  168. /**
  169. * set_required_buffer_size - Sets the buffer size needed for performing IOCTL
  170. * @wdev: A wmi bus device from a driver
  171. * @instance: Instance index
  172. *
  173. * Allocates memory needed for buffer, stores the buffer size in that memory
  174. */
  175. int set_required_buffer_size(struct wmi_device *wdev, u64 length)
  176. {
  177. struct wmi_block *wblock;
  178. wblock = container_of(wdev, struct wmi_block, dev);
  179. wblock->req_buf_size = length;
  180. return 0;
  181. }
  182. EXPORT_SYMBOL_GPL(set_required_buffer_size);
  183. /**
  184. * wmi_evaluate_method - Evaluate a WMI method
  185. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  186. * @instance: Instance index
  187. * @method_id: Method ID to call
  188. * &in: Buffer containing input for the method call
  189. * &out: Empty buffer to return the method results
  190. *
  191. * Call an ACPI-WMI method
  192. */
  193. acpi_status wmi_evaluate_method(const char *guid_string, u8 instance,
  194. u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
  195. {
  196. struct wmi_block *wblock = NULL;
  197. if (!find_guid(guid_string, &wblock))
  198. return AE_ERROR;
  199. return wmidev_evaluate_method(&wblock->dev, instance, method_id,
  200. in, out);
  201. }
  202. EXPORT_SYMBOL_GPL(wmi_evaluate_method);
  203. /**
  204. * wmidev_evaluate_method - Evaluate a WMI method
  205. * @wdev: A wmi bus device from a driver
  206. * @instance: Instance index
  207. * @method_id: Method ID to call
  208. * &in: Buffer containing input for the method call
  209. * &out: Empty buffer to return the method results
  210. *
  211. * Call an ACPI-WMI method
  212. */
  213. acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance,
  214. u32 method_id, const struct acpi_buffer *in, struct acpi_buffer *out)
  215. {
  216. struct guid_block *block = NULL;
  217. struct wmi_block *wblock = NULL;
  218. acpi_handle handle;
  219. acpi_status status;
  220. struct acpi_object_list input;
  221. union acpi_object params[3];
  222. char method[5] = "WM";
  223. wblock = container_of(wdev, struct wmi_block, dev);
  224. block = &wblock->gblock;
  225. handle = wblock->acpi_device->handle;
  226. if (!(block->flags & ACPI_WMI_METHOD))
  227. return AE_BAD_DATA;
  228. if (block->instance_count <= instance)
  229. return AE_BAD_PARAMETER;
  230. input.count = 2;
  231. input.pointer = params;
  232. params[0].type = ACPI_TYPE_INTEGER;
  233. params[0].integer.value = instance;
  234. params[1].type = ACPI_TYPE_INTEGER;
  235. params[1].integer.value = method_id;
  236. if (in) {
  237. input.count = 3;
  238. if (block->flags & ACPI_WMI_STRING) {
  239. params[2].type = ACPI_TYPE_STRING;
  240. } else {
  241. params[2].type = ACPI_TYPE_BUFFER;
  242. }
  243. params[2].buffer.length = in->length;
  244. params[2].buffer.pointer = in->pointer;
  245. }
  246. strncat(method, block->object_id, 2);
  247. status = acpi_evaluate_object(handle, method, &input, out);
  248. return status;
  249. }
  250. EXPORT_SYMBOL_GPL(wmidev_evaluate_method);
  251. static acpi_status __query_block(struct wmi_block *wblock, u8 instance,
  252. struct acpi_buffer *out)
  253. {
  254. struct guid_block *block = NULL;
  255. acpi_handle handle;
  256. acpi_status status, wc_status = AE_ERROR;
  257. struct acpi_object_list input;
  258. union acpi_object wq_params[1];
  259. char method[5];
  260. char wc_method[5] = "WC";
  261. if (!out)
  262. return AE_BAD_PARAMETER;
  263. block = &wblock->gblock;
  264. handle = wblock->acpi_device->handle;
  265. if (block->instance_count <= instance)
  266. return AE_BAD_PARAMETER;
  267. /* Check GUID is a data block */
  268. if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
  269. return AE_ERROR;
  270. input.count = 1;
  271. input.pointer = wq_params;
  272. wq_params[0].type = ACPI_TYPE_INTEGER;
  273. wq_params[0].integer.value = instance;
  274. if (instance == 0 && wblock->read_takes_no_args)
  275. input.count = 0;
  276. /*
  277. * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to
  278. * enable collection.
  279. */
  280. if (block->flags & ACPI_WMI_EXPENSIVE) {
  281. strncat(wc_method, block->object_id, 2);
  282. /*
  283. * Some GUIDs break the specification by declaring themselves
  284. * expensive, but have no corresponding WCxx method. So we
  285. * should not fail if this happens.
  286. */
  287. if (acpi_has_method(handle, wc_method))
  288. wc_status = acpi_execute_simple_method(handle,
  289. wc_method, 1);
  290. }
  291. strcpy(method, "WQ");
  292. strncat(method, block->object_id, 2);
  293. status = acpi_evaluate_object(handle, method, &input, out);
  294. /*
  295. * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if
  296. * the WQxx method failed - we should disable collection anyway.
  297. */
  298. if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) {
  299. status = acpi_execute_simple_method(handle, wc_method, 0);
  300. }
  301. return status;
  302. }
  303. /**
  304. * wmi_query_block - Return contents of a WMI block (deprecated)
  305. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  306. * @instance: Instance index
  307. * &out: Empty buffer to return the contents of the data block to
  308. *
  309. * Return the contents of an ACPI-WMI data block to a buffer
  310. */
  311. acpi_status wmi_query_block(const char *guid_string, u8 instance,
  312. struct acpi_buffer *out)
  313. {
  314. struct wmi_block *wblock;
  315. if (!guid_string)
  316. return AE_BAD_PARAMETER;
  317. if (!find_guid(guid_string, &wblock))
  318. return AE_ERROR;
  319. return __query_block(wblock, instance, out);
  320. }
  321. EXPORT_SYMBOL_GPL(wmi_query_block);
  322. union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance)
  323. {
  324. struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
  325. struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev);
  326. if (ACPI_FAILURE(__query_block(wblock, instance, &out)))
  327. return NULL;
  328. return (union acpi_object *)out.pointer;
  329. }
  330. EXPORT_SYMBOL_GPL(wmidev_block_query);
  331. /**
  332. * wmi_set_block - Write to a WMI block
  333. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  334. * @instance: Instance index
  335. * &in: Buffer containing new values for the data block
  336. *
  337. * Write the contents of the input buffer to an ACPI-WMI data block
  338. */
  339. acpi_status wmi_set_block(const char *guid_string, u8 instance,
  340. const struct acpi_buffer *in)
  341. {
  342. struct guid_block *block = NULL;
  343. struct wmi_block *wblock = NULL;
  344. acpi_handle handle;
  345. struct acpi_object_list input;
  346. union acpi_object params[2];
  347. char method[5] = "WS";
  348. if (!guid_string || !in)
  349. return AE_BAD_DATA;
  350. if (!find_guid(guid_string, &wblock))
  351. return AE_ERROR;
  352. block = &wblock->gblock;
  353. handle = wblock->acpi_device->handle;
  354. if (block->instance_count <= instance)
  355. return AE_BAD_PARAMETER;
  356. /* Check GUID is a data block */
  357. if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD))
  358. return AE_ERROR;
  359. input.count = 2;
  360. input.pointer = params;
  361. params[0].type = ACPI_TYPE_INTEGER;
  362. params[0].integer.value = instance;
  363. if (block->flags & ACPI_WMI_STRING) {
  364. params[1].type = ACPI_TYPE_STRING;
  365. } else {
  366. params[1].type = ACPI_TYPE_BUFFER;
  367. }
  368. params[1].buffer.length = in->length;
  369. params[1].buffer.pointer = in->pointer;
  370. strncat(method, block->object_id, 2);
  371. return acpi_evaluate_object(handle, method, &input, NULL);
  372. }
  373. EXPORT_SYMBOL_GPL(wmi_set_block);
  374. static void wmi_dump_wdg(const struct guid_block *g)
  375. {
  376. pr_info("%pUL:\n", g->guid);
  377. if (g->flags & ACPI_WMI_EVENT)
  378. pr_info("\tnotify_id: 0x%02X\n", g->notify_id);
  379. else
  380. pr_info("\tobject_id: %2pE\n", g->object_id);
  381. pr_info("\tinstance_count: %d\n", g->instance_count);
  382. pr_info("\tflags: %#x", g->flags);
  383. if (g->flags) {
  384. if (g->flags & ACPI_WMI_EXPENSIVE)
  385. pr_cont(" ACPI_WMI_EXPENSIVE");
  386. if (g->flags & ACPI_WMI_METHOD)
  387. pr_cont(" ACPI_WMI_METHOD");
  388. if (g->flags & ACPI_WMI_STRING)
  389. pr_cont(" ACPI_WMI_STRING");
  390. if (g->flags & ACPI_WMI_EVENT)
  391. pr_cont(" ACPI_WMI_EVENT");
  392. }
  393. pr_cont("\n");
  394. }
  395. static void wmi_notify_debug(u32 value, void *context)
  396. {
  397. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  398. union acpi_object *obj;
  399. acpi_status status;
  400. status = wmi_get_event_data(value, &response);
  401. if (status != AE_OK) {
  402. pr_info("bad event status 0x%x\n", status);
  403. return;
  404. }
  405. obj = (union acpi_object *)response.pointer;
  406. if (!obj)
  407. return;
  408. pr_info("DEBUG Event ");
  409. switch(obj->type) {
  410. case ACPI_TYPE_BUFFER:
  411. pr_cont("BUFFER_TYPE - length %d\n", obj->buffer.length);
  412. break;
  413. case ACPI_TYPE_STRING:
  414. pr_cont("STRING_TYPE - %s\n", obj->string.pointer);
  415. break;
  416. case ACPI_TYPE_INTEGER:
  417. pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value);
  418. break;
  419. case ACPI_TYPE_PACKAGE:
  420. pr_cont("PACKAGE_TYPE - %d elements\n", obj->package.count);
  421. break;
  422. default:
  423. pr_cont("object type 0x%X\n", obj->type);
  424. }
  425. kfree(obj);
  426. }
  427. /**
  428. * wmi_install_notify_handler - Register handler for WMI events
  429. * @handler: Function to handle notifications
  430. * @data: Data to be returned to handler when event is fired
  431. *
  432. * Register a handler for events sent to the ACPI-WMI mapper device.
  433. */
  434. acpi_status wmi_install_notify_handler(const char *guid,
  435. wmi_notify_handler handler, void *data)
  436. {
  437. struct wmi_block *block;
  438. acpi_status status = AE_NOT_EXIST;
  439. uuid_le guid_input;
  440. struct list_head *p;
  441. if (!guid || !handler)
  442. return AE_BAD_PARAMETER;
  443. if (uuid_le_to_bin(guid, &guid_input))
  444. return AE_BAD_PARAMETER;
  445. list_for_each(p, &wmi_block_list) {
  446. acpi_status wmi_status;
  447. block = list_entry(p, struct wmi_block, list);
  448. if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
  449. if (block->handler &&
  450. block->handler != wmi_notify_debug)
  451. return AE_ALREADY_ACQUIRED;
  452. block->handler = handler;
  453. block->handler_data = data;
  454. wmi_status = wmi_method_enable(block, 1);
  455. if ((wmi_status != AE_OK) ||
  456. ((wmi_status == AE_OK) && (status == AE_NOT_EXIST)))
  457. status = wmi_status;
  458. }
  459. }
  460. return status;
  461. }
  462. EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
  463. /**
  464. * wmi_uninstall_notify_handler - Unregister handler for WMI events
  465. *
  466. * Unregister handler for events sent to the ACPI-WMI mapper device.
  467. */
  468. acpi_status wmi_remove_notify_handler(const char *guid)
  469. {
  470. struct wmi_block *block;
  471. acpi_status status = AE_NOT_EXIST;
  472. uuid_le guid_input;
  473. struct list_head *p;
  474. if (!guid)
  475. return AE_BAD_PARAMETER;
  476. if (uuid_le_to_bin(guid, &guid_input))
  477. return AE_BAD_PARAMETER;
  478. list_for_each(p, &wmi_block_list) {
  479. acpi_status wmi_status;
  480. block = list_entry(p, struct wmi_block, list);
  481. if (memcmp(block->gblock.guid, &guid_input, 16) == 0) {
  482. if (!block->handler ||
  483. block->handler == wmi_notify_debug)
  484. return AE_NULL_ENTRY;
  485. if (debug_event) {
  486. block->handler = wmi_notify_debug;
  487. status = AE_OK;
  488. } else {
  489. wmi_status = wmi_method_enable(block, 0);
  490. block->handler = NULL;
  491. block->handler_data = NULL;
  492. if ((wmi_status != AE_OK) ||
  493. ((wmi_status == AE_OK) &&
  494. (status == AE_NOT_EXIST)))
  495. status = wmi_status;
  496. }
  497. }
  498. }
  499. return status;
  500. }
  501. EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
  502. /**
  503. * wmi_get_event_data - Get WMI data associated with an event
  504. *
  505. * @event: Event to find
  506. * @out: Buffer to hold event data. out->pointer should be freed with kfree()
  507. *
  508. * Returns extra data associated with an event in WMI.
  509. */
  510. acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
  511. {
  512. struct acpi_object_list input;
  513. union acpi_object params[1];
  514. struct guid_block *gblock;
  515. struct wmi_block *wblock;
  516. struct list_head *p;
  517. input.count = 1;
  518. input.pointer = params;
  519. params[0].type = ACPI_TYPE_INTEGER;
  520. params[0].integer.value = event;
  521. list_for_each(p, &wmi_block_list) {
  522. wblock = list_entry(p, struct wmi_block, list);
  523. gblock = &wblock->gblock;
  524. if ((gblock->flags & ACPI_WMI_EVENT) &&
  525. (gblock->notify_id == event))
  526. return acpi_evaluate_object(wblock->acpi_device->handle,
  527. "_WED", &input, out);
  528. }
  529. return AE_NOT_FOUND;
  530. }
  531. EXPORT_SYMBOL_GPL(wmi_get_event_data);
  532. /**
  533. * wmi_has_guid - Check if a GUID is available
  534. * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  535. *
  536. * Check if a given GUID is defined by _WDG
  537. */
  538. bool wmi_has_guid(const char *guid_string)
  539. {
  540. return find_guid(guid_string, NULL);
  541. }
  542. EXPORT_SYMBOL_GPL(wmi_has_guid);
  543. static struct wmi_block *dev_to_wblock(struct device *dev)
  544. {
  545. return container_of(dev, struct wmi_block, dev.dev);
  546. }
  547. static struct wmi_device *dev_to_wdev(struct device *dev)
  548. {
  549. return container_of(dev, struct wmi_device, dev);
  550. }
  551. /*
  552. * sysfs interface
  553. */
  554. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  555. char *buf)
  556. {
  557. struct wmi_block *wblock = dev_to_wblock(dev);
  558. return sprintf(buf, "wmi:%pUL\n", wblock->gblock.guid);
  559. }
  560. static DEVICE_ATTR_RO(modalias);
  561. static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
  562. char *buf)
  563. {
  564. struct wmi_block *wblock = dev_to_wblock(dev);
  565. return sprintf(buf, "%pUL\n", wblock->gblock.guid);
  566. }
  567. static DEVICE_ATTR_RO(guid);
  568. static ssize_t instance_count_show(struct device *dev,
  569. struct device_attribute *attr, char *buf)
  570. {
  571. struct wmi_block *wblock = dev_to_wblock(dev);
  572. return sprintf(buf, "%d\n", (int)wblock->gblock.instance_count);
  573. }
  574. static DEVICE_ATTR_RO(instance_count);
  575. static ssize_t expensive_show(struct device *dev,
  576. struct device_attribute *attr, char *buf)
  577. {
  578. struct wmi_block *wblock = dev_to_wblock(dev);
  579. return sprintf(buf, "%d\n",
  580. (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0);
  581. }
  582. static DEVICE_ATTR_RO(expensive);
  583. static struct attribute *wmi_attrs[] = {
  584. &dev_attr_modalias.attr,
  585. &dev_attr_guid.attr,
  586. &dev_attr_instance_count.attr,
  587. &dev_attr_expensive.attr,
  588. NULL,
  589. };
  590. ATTRIBUTE_GROUPS(wmi);
  591. static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr,
  592. char *buf)
  593. {
  594. struct wmi_block *wblock = dev_to_wblock(dev);
  595. return sprintf(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id);
  596. }
  597. static DEVICE_ATTR_RO(notify_id);
  598. static struct attribute *wmi_event_attrs[] = {
  599. &dev_attr_notify_id.attr,
  600. NULL,
  601. };
  602. ATTRIBUTE_GROUPS(wmi_event);
  603. static ssize_t object_id_show(struct device *dev, struct device_attribute *attr,
  604. char *buf)
  605. {
  606. struct wmi_block *wblock = dev_to_wblock(dev);
  607. return sprintf(buf, "%c%c\n", wblock->gblock.object_id[0],
  608. wblock->gblock.object_id[1]);
  609. }
  610. static DEVICE_ATTR_RO(object_id);
  611. static ssize_t setable_show(struct device *dev, struct device_attribute *attr,
  612. char *buf)
  613. {
  614. struct wmi_device *wdev = dev_to_wdev(dev);
  615. return sprintf(buf, "%d\n", (int)wdev->setable);
  616. }
  617. static DEVICE_ATTR_RO(setable);
  618. static struct attribute *wmi_data_attrs[] = {
  619. &dev_attr_object_id.attr,
  620. &dev_attr_setable.attr,
  621. NULL,
  622. };
  623. ATTRIBUTE_GROUPS(wmi_data);
  624. static struct attribute *wmi_method_attrs[] = {
  625. &dev_attr_object_id.attr,
  626. NULL,
  627. };
  628. ATTRIBUTE_GROUPS(wmi_method);
  629. static int wmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  630. {
  631. struct wmi_block *wblock = dev_to_wblock(dev);
  632. if (add_uevent_var(env, "MODALIAS=wmi:%pUL", wblock->gblock.guid))
  633. return -ENOMEM;
  634. if (add_uevent_var(env, "WMI_GUID=%pUL", wblock->gblock.guid))
  635. return -ENOMEM;
  636. return 0;
  637. }
  638. static void wmi_dev_release(struct device *dev)
  639. {
  640. struct wmi_block *wblock = dev_to_wblock(dev);
  641. kfree(wblock);
  642. }
  643. static int wmi_dev_match(struct device *dev, struct device_driver *driver)
  644. {
  645. struct wmi_driver *wmi_driver =
  646. container_of(driver, struct wmi_driver, driver);
  647. struct wmi_block *wblock = dev_to_wblock(dev);
  648. const struct wmi_device_id *id = wmi_driver->id_table;
  649. while (id->guid_string) {
  650. uuid_le driver_guid;
  651. if (WARN_ON(uuid_le_to_bin(id->guid_string, &driver_guid)))
  652. continue;
  653. if (!memcmp(&driver_guid, wblock->gblock.guid, 16))
  654. return 1;
  655. id++;
  656. }
  657. return 0;
  658. }
  659. static int wmi_char_open(struct inode *inode, struct file *filp)
  660. {
  661. const char *driver_name = filp->f_path.dentry->d_iname;
  662. struct wmi_block *wblock = NULL;
  663. struct wmi_block *next = NULL;
  664. list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
  665. if (!wblock->dev.dev.driver)
  666. continue;
  667. if (strcmp(driver_name, wblock->dev.dev.driver->name) == 0) {
  668. filp->private_data = wblock;
  669. break;
  670. }
  671. }
  672. if (!filp->private_data)
  673. return -ENODEV;
  674. return nonseekable_open(inode, filp);
  675. }
  676. static ssize_t wmi_char_read(struct file *filp, char __user *buffer,
  677. size_t length, loff_t *offset)
  678. {
  679. struct wmi_block *wblock = filp->private_data;
  680. return simple_read_from_buffer(buffer, length, offset,
  681. &wblock->req_buf_size,
  682. sizeof(wblock->req_buf_size));
  683. }
  684. static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  685. {
  686. struct wmi_ioctl_buffer __user *input =
  687. (struct wmi_ioctl_buffer __user *) arg;
  688. struct wmi_block *wblock = filp->private_data;
  689. struct wmi_ioctl_buffer *buf = NULL;
  690. struct wmi_driver *wdriver = NULL;
  691. int ret;
  692. if (_IOC_TYPE(cmd) != WMI_IOC)
  693. return -ENOTTY;
  694. /* make sure we're not calling a higher instance than exists*/
  695. if (_IOC_NR(cmd) >= wblock->gblock.instance_count)
  696. return -EINVAL;
  697. mutex_lock(&wblock->char_mutex);
  698. buf = wblock->handler_data;
  699. if (get_user(buf->length, &input->length)) {
  700. dev_dbg(&wblock->dev.dev, "Read length from user failed\n");
  701. ret = -EFAULT;
  702. goto out_ioctl;
  703. }
  704. /* if it's too small, abort */
  705. if (buf->length < wblock->req_buf_size) {
  706. dev_err(&wblock->dev.dev,
  707. "Buffer %lld too small, need at least %lld\n",
  708. buf->length, wblock->req_buf_size);
  709. ret = -EINVAL;
  710. goto out_ioctl;
  711. }
  712. /* if it's too big, warn, driver will only use what is needed */
  713. if (buf->length > wblock->req_buf_size)
  714. dev_warn(&wblock->dev.dev,
  715. "Buffer %lld is bigger than required %lld\n",
  716. buf->length, wblock->req_buf_size);
  717. /* copy the structure from userspace */
  718. if (copy_from_user(buf, input, wblock->req_buf_size)) {
  719. dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n",
  720. wblock->req_buf_size);
  721. ret = -EFAULT;
  722. goto out_ioctl;
  723. }
  724. /* let the driver do any filtering and do the call */
  725. wdriver = container_of(wblock->dev.dev.driver,
  726. struct wmi_driver, driver);
  727. if (!try_module_get(wdriver->driver.owner)) {
  728. ret = -EBUSY;
  729. goto out_ioctl;
  730. }
  731. ret = wdriver->filter_callback(&wblock->dev, cmd, buf);
  732. module_put(wdriver->driver.owner);
  733. if (ret)
  734. goto out_ioctl;
  735. /* return the result (only up to our internal buffer size) */
  736. if (copy_to_user(input, buf, wblock->req_buf_size)) {
  737. dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n",
  738. wblock->req_buf_size);
  739. ret = -EFAULT;
  740. }
  741. out_ioctl:
  742. mutex_unlock(&wblock->char_mutex);
  743. return ret;
  744. }
  745. static const struct file_operations wmi_fops = {
  746. .owner = THIS_MODULE,
  747. .read = wmi_char_read,
  748. .open = wmi_char_open,
  749. .unlocked_ioctl = wmi_ioctl,
  750. .compat_ioctl = wmi_ioctl,
  751. };
  752. static int wmi_dev_probe(struct device *dev)
  753. {
  754. struct wmi_block *wblock = dev_to_wblock(dev);
  755. struct wmi_driver *wdriver =
  756. container_of(dev->driver, struct wmi_driver, driver);
  757. int ret = 0;
  758. int count;
  759. char *buf;
  760. if (ACPI_FAILURE(wmi_method_enable(wblock, 1)))
  761. dev_warn(dev, "failed to enable device -- probing anyway\n");
  762. if (wdriver->probe) {
  763. ret = wdriver->probe(dev_to_wdev(dev));
  764. if (ret != 0)
  765. goto probe_failure;
  766. }
  767. /* driver wants a character device made */
  768. if (wdriver->filter_callback) {
  769. /* check that required buffer size declared by driver or MOF */
  770. if (!wblock->req_buf_size) {
  771. dev_err(&wblock->dev.dev,
  772. "Required buffer size not set\n");
  773. ret = -EINVAL;
  774. goto probe_failure;
  775. }
  776. count = get_order(wblock->req_buf_size);
  777. wblock->handler_data = (void *)__get_free_pages(GFP_KERNEL,
  778. count);
  779. if (!wblock->handler_data) {
  780. ret = -ENOMEM;
  781. goto probe_failure;
  782. }
  783. buf = kmalloc(strlen(wdriver->driver.name) + 4, GFP_KERNEL);
  784. if (!buf) {
  785. ret = -ENOMEM;
  786. goto probe_string_failure;
  787. }
  788. sprintf(buf, "wmi/%s", wdriver->driver.name);
  789. wblock->char_dev.minor = MISC_DYNAMIC_MINOR;
  790. wblock->char_dev.name = buf;
  791. wblock->char_dev.fops = &wmi_fops;
  792. wblock->char_dev.mode = 0444;
  793. ret = misc_register(&wblock->char_dev);
  794. if (ret) {
  795. dev_warn(dev, "failed to register char dev: %d", ret);
  796. ret = -ENOMEM;
  797. goto probe_misc_failure;
  798. }
  799. }
  800. return 0;
  801. probe_misc_failure:
  802. kfree(buf);
  803. probe_string_failure:
  804. kfree(wblock->handler_data);
  805. probe_failure:
  806. if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
  807. dev_warn(dev, "failed to disable device\n");
  808. return ret;
  809. }
  810. static int wmi_dev_remove(struct device *dev)
  811. {
  812. struct wmi_block *wblock = dev_to_wblock(dev);
  813. struct wmi_driver *wdriver =
  814. container_of(dev->driver, struct wmi_driver, driver);
  815. int ret = 0;
  816. if (wdriver->filter_callback) {
  817. misc_deregister(&wblock->char_dev);
  818. kfree(wblock->char_dev.name);
  819. free_pages((unsigned long)wblock->handler_data,
  820. get_order(wblock->req_buf_size));
  821. }
  822. if (wdriver->remove)
  823. ret = wdriver->remove(dev_to_wdev(dev));
  824. if (ACPI_FAILURE(wmi_method_enable(wblock, 0)))
  825. dev_warn(dev, "failed to disable device\n");
  826. return ret;
  827. }
  828. static struct class wmi_bus_class = {
  829. .name = "wmi_bus",
  830. };
  831. static struct bus_type wmi_bus_type = {
  832. .name = "wmi",
  833. .dev_groups = wmi_groups,
  834. .match = wmi_dev_match,
  835. .uevent = wmi_dev_uevent,
  836. .probe = wmi_dev_probe,
  837. .remove = wmi_dev_remove,
  838. };
  839. static struct device_type wmi_type_event = {
  840. .name = "event",
  841. .groups = wmi_event_groups,
  842. .release = wmi_dev_release,
  843. };
  844. static struct device_type wmi_type_method = {
  845. .name = "method",
  846. .groups = wmi_method_groups,
  847. .release = wmi_dev_release,
  848. };
  849. static struct device_type wmi_type_data = {
  850. .name = "data",
  851. .groups = wmi_data_groups,
  852. .release = wmi_dev_release,
  853. };
  854. static int wmi_create_device(struct device *wmi_bus_dev,
  855. const struct guid_block *gblock,
  856. struct wmi_block *wblock,
  857. struct acpi_device *device)
  858. {
  859. struct acpi_device_info *info;
  860. char method[5];
  861. int result;
  862. if (gblock->flags & ACPI_WMI_EVENT) {
  863. wblock->dev.dev.type = &wmi_type_event;
  864. goto out_init;
  865. }
  866. if (gblock->flags & ACPI_WMI_METHOD) {
  867. wblock->dev.dev.type = &wmi_type_method;
  868. mutex_init(&wblock->char_mutex);
  869. goto out_init;
  870. }
  871. /*
  872. * Data Block Query Control Method (WQxx by convention) is
  873. * required per the WMI documentation. If it is not present,
  874. * we ignore this data block.
  875. */
  876. strcpy(method, "WQ");
  877. strncat(method, wblock->gblock.object_id, 2);
  878. result = get_subobj_info(device->handle, method, &info);
  879. if (result) {
  880. dev_warn(wmi_bus_dev,
  881. "%s data block query control method not found",
  882. method);
  883. return result;
  884. }
  885. wblock->dev.dev.type = &wmi_type_data;
  886. /*
  887. * The Microsoft documentation specifically states:
  888. *
  889. * Data blocks registered with only a single instance
  890. * can ignore the parameter.
  891. *
  892. * ACPICA will get mad at us if we call the method with the wrong number
  893. * of arguments, so check what our method expects. (On some Dell
  894. * laptops, WQxx may not be a method at all.)
  895. */
  896. if (info->type != ACPI_TYPE_METHOD || info->param_count == 0)
  897. wblock->read_takes_no_args = true;
  898. kfree(info);
  899. strcpy(method, "WS");
  900. strncat(method, wblock->gblock.object_id, 2);
  901. result = get_subobj_info(device->handle, method, NULL);
  902. if (result == 0)
  903. wblock->dev.setable = true;
  904. out_init:
  905. wblock->dev.dev.bus = &wmi_bus_type;
  906. wblock->dev.dev.parent = wmi_bus_dev;
  907. dev_set_name(&wblock->dev.dev, "%pUL", gblock->guid);
  908. device_initialize(&wblock->dev.dev);
  909. return 0;
  910. }
  911. static void wmi_free_devices(struct acpi_device *device)
  912. {
  913. struct wmi_block *wblock, *next;
  914. /* Delete devices for all the GUIDs */
  915. list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
  916. if (wblock->acpi_device == device) {
  917. list_del(&wblock->list);
  918. device_unregister(&wblock->dev.dev);
  919. }
  920. }
  921. }
  922. static bool guid_already_parsed(struct acpi_device *device,
  923. const u8 *guid)
  924. {
  925. struct wmi_block *wblock;
  926. list_for_each_entry(wblock, &wmi_block_list, list) {
  927. if (memcmp(wblock->gblock.guid, guid, 16) == 0) {
  928. /*
  929. * Because we historically didn't track the relationship
  930. * between GUIDs and ACPI nodes, we don't know whether
  931. * we need to suppress GUIDs that are unique on a
  932. * given node but duplicated across nodes.
  933. */
  934. dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n",
  935. guid, dev_name(&wblock->acpi_device->dev));
  936. return true;
  937. }
  938. }
  939. return false;
  940. }
  941. /*
  942. * Parse the _WDG method for the GUID data blocks
  943. */
  944. static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device)
  945. {
  946. struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
  947. const struct guid_block *gblock;
  948. struct wmi_block *wblock, *next;
  949. union acpi_object *obj;
  950. acpi_status status;
  951. int retval = 0;
  952. u32 i, total;
  953. status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out);
  954. if (ACPI_FAILURE(status))
  955. return -ENXIO;
  956. obj = (union acpi_object *) out.pointer;
  957. if (!obj)
  958. return -ENXIO;
  959. if (obj->type != ACPI_TYPE_BUFFER) {
  960. retval = -ENXIO;
  961. goto out_free_pointer;
  962. }
  963. gblock = (const struct guid_block *)obj->buffer.pointer;
  964. total = obj->buffer.length / sizeof(struct guid_block);
  965. for (i = 0; i < total; i++) {
  966. if (debug_dump_wdg)
  967. wmi_dump_wdg(&gblock[i]);
  968. /*
  969. * Some WMI devices, like those for nVidia hooks, have a
  970. * duplicate GUID. It's not clear what we should do in this
  971. * case yet, so for now, we'll just ignore the duplicate
  972. * for device creation.
  973. */
  974. if (guid_already_parsed(device, gblock[i].guid))
  975. continue;
  976. wblock = kzalloc(sizeof(struct wmi_block), GFP_KERNEL);
  977. if (!wblock) {
  978. retval = -ENOMEM;
  979. break;
  980. }
  981. wblock->acpi_device = device;
  982. wblock->gblock = gblock[i];
  983. retval = wmi_create_device(wmi_bus_dev, &gblock[i], wblock, device);
  984. if (retval) {
  985. kfree(wblock);
  986. continue;
  987. }
  988. list_add_tail(&wblock->list, &wmi_block_list);
  989. if (debug_event) {
  990. wblock->handler = wmi_notify_debug;
  991. wmi_method_enable(wblock, 1);
  992. }
  993. }
  994. /*
  995. * Now that all of the devices are created, add them to the
  996. * device tree and probe subdrivers.
  997. */
  998. list_for_each_entry_safe(wblock, next, &wmi_block_list, list) {
  999. if (wblock->acpi_device != device)
  1000. continue;
  1001. retval = device_add(&wblock->dev.dev);
  1002. if (retval) {
  1003. dev_err(wmi_bus_dev, "failed to register %pULL\n",
  1004. wblock->gblock.guid);
  1005. if (debug_event)
  1006. wmi_method_enable(wblock, 0);
  1007. list_del(&wblock->list);
  1008. put_device(&wblock->dev.dev);
  1009. }
  1010. }
  1011. out_free_pointer:
  1012. kfree(out.pointer);
  1013. return retval;
  1014. }
  1015. /*
  1016. * WMI can have EmbeddedControl access regions. In which case, we just want to
  1017. * hand these off to the EC driver.
  1018. */
  1019. static acpi_status
  1020. acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
  1021. u32 bits, u64 *value,
  1022. void *handler_context, void *region_context)
  1023. {
  1024. int result = 0, i = 0;
  1025. u8 temp = 0;
  1026. if ((address > 0xFF) || !value)
  1027. return AE_BAD_PARAMETER;
  1028. if (function != ACPI_READ && function != ACPI_WRITE)
  1029. return AE_BAD_PARAMETER;
  1030. if (bits != 8)
  1031. return AE_BAD_PARAMETER;
  1032. if (function == ACPI_READ) {
  1033. result = ec_read(address, &temp);
  1034. (*value) |= ((u64)temp) << i;
  1035. } else {
  1036. temp = 0xff & ((*value) >> i);
  1037. result = ec_write(address, temp);
  1038. }
  1039. switch (result) {
  1040. case -EINVAL:
  1041. return AE_BAD_PARAMETER;
  1042. break;
  1043. case -ENODEV:
  1044. return AE_NOT_FOUND;
  1045. break;
  1046. case -ETIME:
  1047. return AE_TIME;
  1048. break;
  1049. default:
  1050. return AE_OK;
  1051. }
  1052. }
  1053. static void acpi_wmi_notify_handler(acpi_handle handle, u32 event,
  1054. void *context)
  1055. {
  1056. struct guid_block *block;
  1057. struct wmi_block *wblock;
  1058. struct list_head *p;
  1059. bool found_it = false;
  1060. list_for_each(p, &wmi_block_list) {
  1061. wblock = list_entry(p, struct wmi_block, list);
  1062. block = &wblock->gblock;
  1063. if (wblock->acpi_device->handle == handle &&
  1064. (block->flags & ACPI_WMI_EVENT) &&
  1065. (block->notify_id == event))
  1066. {
  1067. found_it = true;
  1068. break;
  1069. }
  1070. }
  1071. if (!found_it)
  1072. return;
  1073. /* If a driver is bound, then notify the driver. */
  1074. if (wblock->dev.dev.driver) {
  1075. struct wmi_driver *driver;
  1076. struct acpi_object_list input;
  1077. union acpi_object params[1];
  1078. struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL };
  1079. acpi_status status;
  1080. driver = container_of(wblock->dev.dev.driver,
  1081. struct wmi_driver, driver);
  1082. input.count = 1;
  1083. input.pointer = params;
  1084. params[0].type = ACPI_TYPE_INTEGER;
  1085. params[0].integer.value = event;
  1086. status = acpi_evaluate_object(wblock->acpi_device->handle,
  1087. "_WED", &input, &evdata);
  1088. if (ACPI_FAILURE(status)) {
  1089. dev_warn(&wblock->dev.dev,
  1090. "failed to get event data\n");
  1091. return;
  1092. }
  1093. if (driver->notify)
  1094. driver->notify(&wblock->dev,
  1095. (union acpi_object *)evdata.pointer);
  1096. kfree(evdata.pointer);
  1097. } else if (wblock->handler) {
  1098. /* Legacy handler */
  1099. wblock->handler(event, wblock->handler_data);
  1100. }
  1101. if (debug_event) {
  1102. pr_info("DEBUG Event GUID: %pUL\n",
  1103. wblock->gblock.guid);
  1104. }
  1105. acpi_bus_generate_netlink_event(
  1106. wblock->acpi_device->pnp.device_class,
  1107. dev_name(&wblock->dev.dev),
  1108. event, 0);
  1109. }
  1110. static int acpi_wmi_remove(struct platform_device *device)
  1111. {
  1112. struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev);
  1113. acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
  1114. acpi_wmi_notify_handler);
  1115. acpi_remove_address_space_handler(acpi_device->handle,
  1116. ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler);
  1117. wmi_free_devices(acpi_device);
  1118. device_destroy(&wmi_bus_class, MKDEV(0, 0));
  1119. return 0;
  1120. }
  1121. static int acpi_wmi_probe(struct platform_device *device)
  1122. {
  1123. struct acpi_device *acpi_device;
  1124. struct device *wmi_bus_dev;
  1125. acpi_status status;
  1126. int error;
  1127. acpi_device = ACPI_COMPANION(&device->dev);
  1128. if (!acpi_device) {
  1129. dev_err(&device->dev, "ACPI companion is missing\n");
  1130. return -ENODEV;
  1131. }
  1132. status = acpi_install_address_space_handler(acpi_device->handle,
  1133. ACPI_ADR_SPACE_EC,
  1134. &acpi_wmi_ec_space_handler,
  1135. NULL, NULL);
  1136. if (ACPI_FAILURE(status)) {
  1137. dev_err(&device->dev, "Error installing EC region handler\n");
  1138. return -ENODEV;
  1139. }
  1140. status = acpi_install_notify_handler(acpi_device->handle,
  1141. ACPI_DEVICE_NOTIFY,
  1142. acpi_wmi_notify_handler,
  1143. NULL);
  1144. if (ACPI_FAILURE(status)) {
  1145. dev_err(&device->dev, "Error installing notify handler\n");
  1146. error = -ENODEV;
  1147. goto err_remove_ec_handler;
  1148. }
  1149. wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0),
  1150. NULL, "wmi_bus-%s", dev_name(&device->dev));
  1151. if (IS_ERR(wmi_bus_dev)) {
  1152. error = PTR_ERR(wmi_bus_dev);
  1153. goto err_remove_notify_handler;
  1154. }
  1155. dev_set_drvdata(&device->dev, wmi_bus_dev);
  1156. error = parse_wdg(wmi_bus_dev, acpi_device);
  1157. if (error) {
  1158. pr_err("Failed to parse WDG method\n");
  1159. goto err_remove_busdev;
  1160. }
  1161. return 0;
  1162. err_remove_busdev:
  1163. device_destroy(&wmi_bus_class, MKDEV(0, 0));
  1164. err_remove_notify_handler:
  1165. acpi_remove_notify_handler(acpi_device->handle, ACPI_DEVICE_NOTIFY,
  1166. acpi_wmi_notify_handler);
  1167. err_remove_ec_handler:
  1168. acpi_remove_address_space_handler(acpi_device->handle,
  1169. ACPI_ADR_SPACE_EC,
  1170. &acpi_wmi_ec_space_handler);
  1171. return error;
  1172. }
  1173. int __must_check __wmi_driver_register(struct wmi_driver *driver,
  1174. struct module *owner)
  1175. {
  1176. driver->driver.owner = owner;
  1177. driver->driver.bus = &wmi_bus_type;
  1178. return driver_register(&driver->driver);
  1179. }
  1180. EXPORT_SYMBOL(__wmi_driver_register);
  1181. void wmi_driver_unregister(struct wmi_driver *driver)
  1182. {
  1183. driver_unregister(&driver->driver);
  1184. }
  1185. EXPORT_SYMBOL(wmi_driver_unregister);
  1186. static int __init acpi_wmi_init(void)
  1187. {
  1188. int error;
  1189. if (acpi_disabled)
  1190. return -ENODEV;
  1191. error = class_register(&wmi_bus_class);
  1192. if (error)
  1193. return error;
  1194. error = bus_register(&wmi_bus_type);
  1195. if (error)
  1196. goto err_unreg_class;
  1197. error = platform_driver_register(&acpi_wmi_driver);
  1198. if (error) {
  1199. pr_err("Error loading mapper\n");
  1200. goto err_unreg_bus;
  1201. }
  1202. return 0;
  1203. err_unreg_bus:
  1204. bus_unregister(&wmi_bus_type);
  1205. err_unreg_class:
  1206. class_unregister(&wmi_bus_class);
  1207. return error;
  1208. }
  1209. static void __exit acpi_wmi_exit(void)
  1210. {
  1211. platform_driver_unregister(&acpi_wmi_driver);
  1212. bus_unregister(&wmi_bus_type);
  1213. class_unregister(&wmi_bus_class);
  1214. }
  1215. subsys_initcall(acpi_wmi_init);
  1216. module_exit(acpi_wmi_exit);