wmi.c 35 KB

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