wmi.c 37 KB

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