wmi.c 36 KB

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