huawei-wmi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Huawei WMI laptop extras driver
  4. *
  5. * Copyright (C) 2018 Ayman Bagabas <[email protected]>
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/delay.h>
  10. #include <linux/dmi.h>
  11. #include <linux/input.h>
  12. #include <linux/input/sparse-keymap.h>
  13. #include <linux/leds.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/power_supply.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/wmi.h>
  20. #include <acpi/battery.h>
  21. /*
  22. * Huawei WMI GUIDs
  23. */
  24. #define HWMI_METHOD_GUID "ABBC0F5B-8EA1-11D1-A000-C90629100000"
  25. #define HWMI_EVENT_GUID "ABBC0F5C-8EA1-11D1-A000-C90629100000"
  26. /* Legacy GUIDs */
  27. #define WMI0_EXPENSIVE_GUID "39142400-C6A3-40fa-BADB-8A2652834100"
  28. #define WMI0_EVENT_GUID "59142400-C6A3-40fa-BADB-8A2652834100"
  29. /* HWMI commands */
  30. enum {
  31. BATTERY_THRESH_GET = 0x00001103, /* \GBTT */
  32. BATTERY_THRESH_SET = 0x00001003, /* \SBTT */
  33. FN_LOCK_GET = 0x00000604, /* \GFRS */
  34. FN_LOCK_SET = 0x00000704, /* \SFRS */
  35. MICMUTE_LED_SET = 0x00000b04, /* \SMLS */
  36. };
  37. union hwmi_arg {
  38. u64 cmd;
  39. u8 args[8];
  40. };
  41. struct quirk_entry {
  42. bool battery_reset;
  43. bool ec_micmute;
  44. bool report_brightness;
  45. };
  46. static struct quirk_entry *quirks;
  47. struct huawei_wmi_debug {
  48. struct dentry *root;
  49. u64 arg;
  50. };
  51. struct huawei_wmi {
  52. bool battery_available;
  53. bool fn_lock_available;
  54. struct huawei_wmi_debug debug;
  55. struct input_dev *idev[2];
  56. struct led_classdev cdev;
  57. struct device *dev;
  58. struct mutex wmi_lock;
  59. };
  60. static struct huawei_wmi *huawei_wmi;
  61. static const struct key_entry huawei_wmi_keymap[] = {
  62. { KE_KEY, 0x281, { KEY_BRIGHTNESSDOWN } },
  63. { KE_KEY, 0x282, { KEY_BRIGHTNESSUP } },
  64. { KE_KEY, 0x284, { KEY_MUTE } },
  65. { KE_KEY, 0x285, { KEY_VOLUMEDOWN } },
  66. { KE_KEY, 0x286, { KEY_VOLUMEUP } },
  67. { KE_KEY, 0x287, { KEY_MICMUTE } },
  68. { KE_KEY, 0x289, { KEY_WLAN } },
  69. // Huawei |M| key
  70. { KE_KEY, 0x28a, { KEY_CONFIG } },
  71. // Keyboard backlit
  72. { KE_IGNORE, 0x293, { KEY_KBDILLUMTOGGLE } },
  73. { KE_IGNORE, 0x294, { KEY_KBDILLUMUP } },
  74. { KE_IGNORE, 0x295, { KEY_KBDILLUMUP } },
  75. // Ignore Ambient Light Sensoring
  76. { KE_KEY, 0x2c1, { KEY_RESERVED } },
  77. { KE_END, 0 }
  78. };
  79. static int battery_reset = -1;
  80. static int report_brightness = -1;
  81. module_param(battery_reset, bint, 0444);
  82. MODULE_PARM_DESC(battery_reset,
  83. "Reset battery charge values to (0-0) before disabling it using (0-100)");
  84. module_param(report_brightness, bint, 0444);
  85. MODULE_PARM_DESC(report_brightness,
  86. "Report brightness keys.");
  87. /* Quirks */
  88. static int __init dmi_matched(const struct dmi_system_id *dmi)
  89. {
  90. quirks = dmi->driver_data;
  91. return 1;
  92. }
  93. static struct quirk_entry quirk_unknown = {
  94. };
  95. static struct quirk_entry quirk_battery_reset = {
  96. .battery_reset = true,
  97. };
  98. static struct quirk_entry quirk_matebook_x = {
  99. .ec_micmute = true,
  100. .report_brightness = true,
  101. };
  102. static const struct dmi_system_id huawei_quirks[] = {
  103. {
  104. .callback = dmi_matched,
  105. .ident = "Huawei MACH-WX9",
  106. .matches = {
  107. DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
  108. DMI_MATCH(DMI_PRODUCT_NAME, "MACH-WX9"),
  109. },
  110. .driver_data = &quirk_battery_reset
  111. },
  112. {
  113. .callback = dmi_matched,
  114. .ident = "Huawei MateBook X",
  115. .matches = {
  116. DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
  117. DMI_MATCH(DMI_PRODUCT_NAME, "HUAWEI MateBook X")
  118. },
  119. .driver_data = &quirk_matebook_x
  120. },
  121. { }
  122. };
  123. /* Utils */
  124. static int huawei_wmi_call(struct huawei_wmi *huawei,
  125. struct acpi_buffer *in, struct acpi_buffer *out)
  126. {
  127. acpi_status status;
  128. mutex_lock(&huawei->wmi_lock);
  129. status = wmi_evaluate_method(HWMI_METHOD_GUID, 0, 1, in, out);
  130. mutex_unlock(&huawei->wmi_lock);
  131. if (ACPI_FAILURE(status)) {
  132. dev_err(huawei->dev, "Failed to evaluate wmi method\n");
  133. return -ENODEV;
  134. }
  135. return 0;
  136. }
  137. /* HWMI takes a 64 bit input and returns either a package with 2 buffers, one of
  138. * 4 bytes and the other of 256 bytes, or one buffer of size 0x104 (260) bytes.
  139. * The first 4 bytes are ignored, we ignore the first 4 bytes buffer if we got a
  140. * package, or skip the first 4 if a buffer of 0x104 is used. The first byte of
  141. * the remaining 0x100 sized buffer has the return status of every call. In case
  142. * the return status is non-zero, we return -ENODEV but still copy the returned
  143. * buffer to the given buffer parameter (buf).
  144. */
  145. static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
  146. {
  147. struct huawei_wmi *huawei = huawei_wmi;
  148. struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
  149. struct acpi_buffer in;
  150. union acpi_object *obj;
  151. size_t len;
  152. int err, i;
  153. in.length = sizeof(arg);
  154. in.pointer = &arg;
  155. /* Some models require calling HWMI twice to execute a command. We evaluate
  156. * HWMI and if we get a non-zero return status we evaluate it again.
  157. */
  158. for (i = 0; i < 2; i++) {
  159. err = huawei_wmi_call(huawei, &in, &out);
  160. if (err)
  161. goto fail_cmd;
  162. obj = out.pointer;
  163. if (!obj) {
  164. err = -EIO;
  165. goto fail_cmd;
  166. }
  167. switch (obj->type) {
  168. /* Models that implement both "legacy" and HWMI tend to return a 0x104
  169. * sized buffer instead of a package of 0x4 and 0x100 buffers.
  170. */
  171. case ACPI_TYPE_BUFFER:
  172. if (obj->buffer.length == 0x104) {
  173. // Skip the first 4 bytes.
  174. obj->buffer.pointer += 4;
  175. len = 0x100;
  176. } else {
  177. dev_err(huawei->dev, "Bad buffer length, got %d\n", obj->buffer.length);
  178. err = -EIO;
  179. goto fail_cmd;
  180. }
  181. break;
  182. /* HWMI returns a package with 2 buffer elements, one of 4 bytes and the
  183. * other is 256 bytes.
  184. */
  185. case ACPI_TYPE_PACKAGE:
  186. if (obj->package.count != 2) {
  187. dev_err(huawei->dev, "Bad package count, got %d\n", obj->package.count);
  188. err = -EIO;
  189. goto fail_cmd;
  190. }
  191. obj = &obj->package.elements[1];
  192. if (obj->type != ACPI_TYPE_BUFFER) {
  193. dev_err(huawei->dev, "Bad package element type, got %d\n", obj->type);
  194. err = -EIO;
  195. goto fail_cmd;
  196. }
  197. len = obj->buffer.length;
  198. break;
  199. /* Shouldn't get here! */
  200. default:
  201. dev_err(huawei->dev, "Unexpected obj type, got: %d\n", obj->type);
  202. err = -EIO;
  203. goto fail_cmd;
  204. }
  205. if (!*obj->buffer.pointer)
  206. break;
  207. }
  208. err = (*obj->buffer.pointer) ? -ENODEV : 0;
  209. if (buf) {
  210. len = min(buflen, len);
  211. memcpy(buf, obj->buffer.pointer, len);
  212. }
  213. fail_cmd:
  214. kfree(out.pointer);
  215. return err;
  216. }
  217. /* LEDs */
  218. static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev,
  219. enum led_brightness brightness)
  220. {
  221. /* This is a workaround until the "legacy" interface is implemented. */
  222. if (quirks && quirks->ec_micmute) {
  223. char *acpi_method;
  224. acpi_handle handle;
  225. acpi_status status;
  226. union acpi_object args[3];
  227. struct acpi_object_list arg_list = {
  228. .pointer = args,
  229. .count = ARRAY_SIZE(args),
  230. };
  231. handle = ec_get_handle();
  232. if (!handle)
  233. return -ENODEV;
  234. args[0].type = args[1].type = args[2].type = ACPI_TYPE_INTEGER;
  235. args[1].integer.value = 0x04;
  236. if (acpi_has_method(handle, "SPIN")) {
  237. acpi_method = "SPIN";
  238. args[0].integer.value = 0;
  239. args[2].integer.value = brightness ? 1 : 0;
  240. } else if (acpi_has_method(handle, "WPIN")) {
  241. acpi_method = "WPIN";
  242. args[0].integer.value = 1;
  243. args[2].integer.value = brightness ? 0 : 1;
  244. } else {
  245. return -ENODEV;
  246. }
  247. status = acpi_evaluate_object(handle, acpi_method, &arg_list, NULL);
  248. if (ACPI_FAILURE(status))
  249. return -ENODEV;
  250. return 0;
  251. } else {
  252. union hwmi_arg arg;
  253. arg.cmd = MICMUTE_LED_SET;
  254. arg.args[2] = brightness;
  255. return huawei_wmi_cmd(arg.cmd, NULL, 0);
  256. }
  257. }
  258. static void huawei_wmi_leds_setup(struct device *dev)
  259. {
  260. struct huawei_wmi *huawei = dev_get_drvdata(dev);
  261. huawei->cdev.name = "platform::micmute";
  262. huawei->cdev.max_brightness = 1;
  263. huawei->cdev.brightness_set_blocking = &huawei_wmi_micmute_led_set;
  264. huawei->cdev.default_trigger = "audio-micmute";
  265. huawei->cdev.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
  266. huawei->cdev.dev = dev;
  267. huawei->cdev.flags = LED_CORE_SUSPENDRESUME;
  268. devm_led_classdev_register(dev, &huawei->cdev);
  269. }
  270. /* Battery protection */
  271. static int huawei_wmi_battery_get(int *start, int *end)
  272. {
  273. u8 ret[0x100];
  274. int err, i;
  275. err = huawei_wmi_cmd(BATTERY_THRESH_GET, ret, 0x100);
  276. if (err)
  277. return err;
  278. /* Find the last two non-zero values. Return status is ignored. */
  279. i = 0xff;
  280. do {
  281. if (start)
  282. *start = ret[i-1];
  283. if (end)
  284. *end = ret[i];
  285. } while (i > 2 && !ret[i--]);
  286. return 0;
  287. }
  288. static int huawei_wmi_battery_set(int start, int end)
  289. {
  290. union hwmi_arg arg;
  291. int err;
  292. if (start < 0 || end < 0 || start > 100 || end > 100)
  293. return -EINVAL;
  294. arg.cmd = BATTERY_THRESH_SET;
  295. arg.args[2] = start;
  296. arg.args[3] = end;
  297. /* This is an edge case were some models turn battery protection
  298. * off without changing their thresholds values. We clear the
  299. * values before turning off protection. Sometimes we need a sleep delay to
  300. * make sure these values make their way to EC memory.
  301. */
  302. if (quirks && quirks->battery_reset && start == 0 && end == 100) {
  303. err = huawei_wmi_battery_set(0, 0);
  304. if (err)
  305. return err;
  306. msleep(1000);
  307. }
  308. err = huawei_wmi_cmd(arg.cmd, NULL, 0);
  309. return err;
  310. }
  311. static ssize_t charge_control_start_threshold_show(struct device *dev,
  312. struct device_attribute *attr,
  313. char *buf)
  314. {
  315. int err, start;
  316. err = huawei_wmi_battery_get(&start, NULL);
  317. if (err)
  318. return err;
  319. return sprintf(buf, "%d\n", start);
  320. }
  321. static ssize_t charge_control_end_threshold_show(struct device *dev,
  322. struct device_attribute *attr,
  323. char *buf)
  324. {
  325. int err, end;
  326. err = huawei_wmi_battery_get(NULL, &end);
  327. if (err)
  328. return err;
  329. return sprintf(buf, "%d\n", end);
  330. }
  331. static ssize_t charge_control_thresholds_show(struct device *dev,
  332. struct device_attribute *attr,
  333. char *buf)
  334. {
  335. int err, start, end;
  336. err = huawei_wmi_battery_get(&start, &end);
  337. if (err)
  338. return err;
  339. return sprintf(buf, "%d %d\n", start, end);
  340. }
  341. static ssize_t charge_control_start_threshold_store(struct device *dev,
  342. struct device_attribute *attr,
  343. const char *buf, size_t size)
  344. {
  345. int err, start, end;
  346. err = huawei_wmi_battery_get(NULL, &end);
  347. if (err)
  348. return err;
  349. if (sscanf(buf, "%d", &start) != 1)
  350. return -EINVAL;
  351. err = huawei_wmi_battery_set(start, end);
  352. if (err)
  353. return err;
  354. return size;
  355. }
  356. static ssize_t charge_control_end_threshold_store(struct device *dev,
  357. struct device_attribute *attr,
  358. const char *buf, size_t size)
  359. {
  360. int err, start, end;
  361. err = huawei_wmi_battery_get(&start, NULL);
  362. if (err)
  363. return err;
  364. if (sscanf(buf, "%d", &end) != 1)
  365. return -EINVAL;
  366. err = huawei_wmi_battery_set(start, end);
  367. if (err)
  368. return err;
  369. return size;
  370. }
  371. static ssize_t charge_control_thresholds_store(struct device *dev,
  372. struct device_attribute *attr,
  373. const char *buf, size_t size)
  374. {
  375. int err, start, end;
  376. if (sscanf(buf, "%d %d", &start, &end) != 2)
  377. return -EINVAL;
  378. err = huawei_wmi_battery_set(start, end);
  379. if (err)
  380. return err;
  381. return size;
  382. }
  383. static DEVICE_ATTR_RW(charge_control_start_threshold);
  384. static DEVICE_ATTR_RW(charge_control_end_threshold);
  385. static DEVICE_ATTR_RW(charge_control_thresholds);
  386. static int huawei_wmi_battery_add(struct power_supply *battery)
  387. {
  388. int err = 0;
  389. err = device_create_file(&battery->dev, &dev_attr_charge_control_start_threshold);
  390. if (err)
  391. return err;
  392. err = device_create_file(&battery->dev, &dev_attr_charge_control_end_threshold);
  393. if (err)
  394. device_remove_file(&battery->dev, &dev_attr_charge_control_start_threshold);
  395. return err;
  396. }
  397. static int huawei_wmi_battery_remove(struct power_supply *battery)
  398. {
  399. device_remove_file(&battery->dev, &dev_attr_charge_control_start_threshold);
  400. device_remove_file(&battery->dev, &dev_attr_charge_control_end_threshold);
  401. return 0;
  402. }
  403. static struct acpi_battery_hook huawei_wmi_battery_hook = {
  404. .add_battery = huawei_wmi_battery_add,
  405. .remove_battery = huawei_wmi_battery_remove,
  406. .name = "Huawei Battery Extension"
  407. };
  408. static void huawei_wmi_battery_setup(struct device *dev)
  409. {
  410. struct huawei_wmi *huawei = dev_get_drvdata(dev);
  411. huawei->battery_available = true;
  412. if (huawei_wmi_battery_get(NULL, NULL)) {
  413. huawei->battery_available = false;
  414. return;
  415. }
  416. battery_hook_register(&huawei_wmi_battery_hook);
  417. device_create_file(dev, &dev_attr_charge_control_thresholds);
  418. }
  419. static void huawei_wmi_battery_exit(struct device *dev)
  420. {
  421. struct huawei_wmi *huawei = dev_get_drvdata(dev);
  422. if (huawei->battery_available) {
  423. battery_hook_unregister(&huawei_wmi_battery_hook);
  424. device_remove_file(dev, &dev_attr_charge_control_thresholds);
  425. }
  426. }
  427. /* Fn lock */
  428. static int huawei_wmi_fn_lock_get(int *on)
  429. {
  430. u8 ret[0x100] = { 0 };
  431. int err, i;
  432. err = huawei_wmi_cmd(FN_LOCK_GET, ret, 0x100);
  433. if (err)
  434. return err;
  435. /* Find the first non-zero value. Return status is ignored. */
  436. i = 1;
  437. do {
  438. if (on)
  439. *on = ret[i] - 1; // -1 undefined, 0 off, 1 on.
  440. } while (i < 0xff && !ret[i++]);
  441. return 0;
  442. }
  443. static int huawei_wmi_fn_lock_set(int on)
  444. {
  445. union hwmi_arg arg;
  446. arg.cmd = FN_LOCK_SET;
  447. arg.args[2] = on + 1; // 0 undefined, 1 off, 2 on.
  448. return huawei_wmi_cmd(arg.cmd, NULL, 0);
  449. }
  450. static ssize_t fn_lock_state_show(struct device *dev,
  451. struct device_attribute *attr,
  452. char *buf)
  453. {
  454. int err, on;
  455. err = huawei_wmi_fn_lock_get(&on);
  456. if (err)
  457. return err;
  458. return sprintf(buf, "%d\n", on);
  459. }
  460. static ssize_t fn_lock_state_store(struct device *dev,
  461. struct device_attribute *attr,
  462. const char *buf, size_t size)
  463. {
  464. int on, err;
  465. if (kstrtoint(buf, 10, &on) ||
  466. on < 0 || on > 1)
  467. return -EINVAL;
  468. err = huawei_wmi_fn_lock_set(on);
  469. if (err)
  470. return err;
  471. return size;
  472. }
  473. static DEVICE_ATTR_RW(fn_lock_state);
  474. static void huawei_wmi_fn_lock_setup(struct device *dev)
  475. {
  476. struct huawei_wmi *huawei = dev_get_drvdata(dev);
  477. huawei->fn_lock_available = true;
  478. if (huawei_wmi_fn_lock_get(NULL)) {
  479. huawei->fn_lock_available = false;
  480. return;
  481. }
  482. device_create_file(dev, &dev_attr_fn_lock_state);
  483. }
  484. static void huawei_wmi_fn_lock_exit(struct device *dev)
  485. {
  486. struct huawei_wmi *huawei = dev_get_drvdata(dev);
  487. if (huawei->fn_lock_available)
  488. device_remove_file(dev, &dev_attr_fn_lock_state);
  489. }
  490. /* debugfs */
  491. static void huawei_wmi_debugfs_call_dump(struct seq_file *m, void *data,
  492. union acpi_object *obj)
  493. {
  494. struct huawei_wmi *huawei = m->private;
  495. int i;
  496. switch (obj->type) {
  497. case ACPI_TYPE_INTEGER:
  498. seq_printf(m, "0x%llx", obj->integer.value);
  499. break;
  500. case ACPI_TYPE_STRING:
  501. seq_printf(m, "\"%.*s\"", obj->string.length, obj->string.pointer);
  502. break;
  503. case ACPI_TYPE_BUFFER:
  504. seq_puts(m, "{");
  505. for (i = 0; i < obj->buffer.length; i++) {
  506. seq_printf(m, "0x%02x", obj->buffer.pointer[i]);
  507. if (i < obj->buffer.length - 1)
  508. seq_puts(m, ",");
  509. }
  510. seq_puts(m, "}");
  511. break;
  512. case ACPI_TYPE_PACKAGE:
  513. seq_puts(m, "[");
  514. for (i = 0; i < obj->package.count; i++) {
  515. huawei_wmi_debugfs_call_dump(m, huawei, &obj->package.elements[i]);
  516. if (i < obj->package.count - 1)
  517. seq_puts(m, ",");
  518. }
  519. seq_puts(m, "]");
  520. break;
  521. default:
  522. dev_err(huawei->dev, "Unexpected obj type, got %d\n", obj->type);
  523. return;
  524. }
  525. }
  526. static int huawei_wmi_debugfs_call_show(struct seq_file *m, void *data)
  527. {
  528. struct huawei_wmi *huawei = m->private;
  529. struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
  530. struct acpi_buffer in;
  531. union acpi_object *obj;
  532. int err;
  533. in.length = sizeof(u64);
  534. in.pointer = &huawei->debug.arg;
  535. err = huawei_wmi_call(huawei, &in, &out);
  536. if (err)
  537. return err;
  538. obj = out.pointer;
  539. if (!obj) {
  540. err = -EIO;
  541. goto fail_debugfs_call;
  542. }
  543. huawei_wmi_debugfs_call_dump(m, huawei, obj);
  544. fail_debugfs_call:
  545. kfree(out.pointer);
  546. return err;
  547. }
  548. DEFINE_SHOW_ATTRIBUTE(huawei_wmi_debugfs_call);
  549. static void huawei_wmi_debugfs_setup(struct device *dev)
  550. {
  551. struct huawei_wmi *huawei = dev_get_drvdata(dev);
  552. huawei->debug.root = debugfs_create_dir("huawei-wmi", NULL);
  553. debugfs_create_x64("arg", 0644, huawei->debug.root,
  554. &huawei->debug.arg);
  555. debugfs_create_file("call", 0400,
  556. huawei->debug.root, huawei, &huawei_wmi_debugfs_call_fops);
  557. }
  558. static void huawei_wmi_debugfs_exit(struct device *dev)
  559. {
  560. struct huawei_wmi *huawei = dev_get_drvdata(dev);
  561. debugfs_remove_recursive(huawei->debug.root);
  562. }
  563. /* Input */
  564. static void huawei_wmi_process_key(struct input_dev *idev, int code)
  565. {
  566. const struct key_entry *key;
  567. /*
  568. * WMI0 uses code 0x80 to indicate a hotkey event.
  569. * The actual key is fetched from the method WQ00
  570. * using WMI0_EXPENSIVE_GUID.
  571. */
  572. if (code == 0x80) {
  573. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  574. union acpi_object *obj;
  575. acpi_status status;
  576. status = wmi_query_block(WMI0_EXPENSIVE_GUID, 0, &response);
  577. if (ACPI_FAILURE(status))
  578. return;
  579. obj = (union acpi_object *)response.pointer;
  580. if (obj && obj->type == ACPI_TYPE_INTEGER)
  581. code = obj->integer.value;
  582. kfree(response.pointer);
  583. }
  584. key = sparse_keymap_entry_from_scancode(idev, code);
  585. if (!key) {
  586. dev_info(&idev->dev, "Unknown key pressed, code: 0x%04x\n", code);
  587. return;
  588. }
  589. if (quirks && !quirks->report_brightness &&
  590. (key->sw.code == KEY_BRIGHTNESSDOWN ||
  591. key->sw.code == KEY_BRIGHTNESSUP))
  592. return;
  593. sparse_keymap_report_entry(idev, key, 1, true);
  594. }
  595. static void huawei_wmi_input_notify(u32 value, void *context)
  596. {
  597. struct input_dev *idev = (struct input_dev *)context;
  598. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  599. union acpi_object *obj;
  600. acpi_status status;
  601. status = wmi_get_event_data(value, &response);
  602. if (ACPI_FAILURE(status)) {
  603. dev_err(&idev->dev, "Unable to get event data\n");
  604. return;
  605. }
  606. obj = (union acpi_object *)response.pointer;
  607. if (obj && obj->type == ACPI_TYPE_INTEGER)
  608. huawei_wmi_process_key(idev, obj->integer.value);
  609. else
  610. dev_err(&idev->dev, "Bad response type\n");
  611. kfree(response.pointer);
  612. }
  613. static int huawei_wmi_input_setup(struct device *dev,
  614. const char *guid,
  615. struct input_dev **idev)
  616. {
  617. acpi_status status;
  618. int err;
  619. *idev = devm_input_allocate_device(dev);
  620. if (!*idev)
  621. return -ENOMEM;
  622. (*idev)->name = "Huawei WMI hotkeys";
  623. (*idev)->phys = "wmi/input0";
  624. (*idev)->id.bustype = BUS_HOST;
  625. (*idev)->dev.parent = dev;
  626. err = sparse_keymap_setup(*idev, huawei_wmi_keymap, NULL);
  627. if (err)
  628. return err;
  629. err = input_register_device(*idev);
  630. if (err)
  631. return err;
  632. status = wmi_install_notify_handler(guid, huawei_wmi_input_notify, *idev);
  633. if (ACPI_FAILURE(status))
  634. return -EIO;
  635. return 0;
  636. }
  637. static void huawei_wmi_input_exit(struct device *dev, const char *guid)
  638. {
  639. wmi_remove_notify_handler(guid);
  640. }
  641. /* Huawei driver */
  642. static const struct wmi_device_id huawei_wmi_events_id_table[] = {
  643. { .guid_string = WMI0_EVENT_GUID },
  644. { .guid_string = HWMI_EVENT_GUID },
  645. { }
  646. };
  647. static int huawei_wmi_probe(struct platform_device *pdev)
  648. {
  649. const struct wmi_device_id *guid = huawei_wmi_events_id_table;
  650. int err;
  651. platform_set_drvdata(pdev, huawei_wmi);
  652. huawei_wmi->dev = &pdev->dev;
  653. while (*guid->guid_string) {
  654. struct input_dev *idev = *huawei_wmi->idev;
  655. if (wmi_has_guid(guid->guid_string)) {
  656. err = huawei_wmi_input_setup(&pdev->dev, guid->guid_string, &idev);
  657. if (err) {
  658. dev_err(&pdev->dev, "Failed to setup input on %s\n", guid->guid_string);
  659. return err;
  660. }
  661. }
  662. idev++;
  663. guid++;
  664. }
  665. if (wmi_has_guid(HWMI_METHOD_GUID)) {
  666. mutex_init(&huawei_wmi->wmi_lock);
  667. huawei_wmi_leds_setup(&pdev->dev);
  668. huawei_wmi_fn_lock_setup(&pdev->dev);
  669. huawei_wmi_battery_setup(&pdev->dev);
  670. huawei_wmi_debugfs_setup(&pdev->dev);
  671. }
  672. return 0;
  673. }
  674. static int huawei_wmi_remove(struct platform_device *pdev)
  675. {
  676. const struct wmi_device_id *guid = huawei_wmi_events_id_table;
  677. while (*guid->guid_string) {
  678. if (wmi_has_guid(guid->guid_string))
  679. huawei_wmi_input_exit(&pdev->dev, guid->guid_string);
  680. guid++;
  681. }
  682. if (wmi_has_guid(HWMI_METHOD_GUID)) {
  683. huawei_wmi_debugfs_exit(&pdev->dev);
  684. huawei_wmi_battery_exit(&pdev->dev);
  685. huawei_wmi_fn_lock_exit(&pdev->dev);
  686. }
  687. return 0;
  688. }
  689. static struct platform_driver huawei_wmi_driver = {
  690. .driver = {
  691. .name = "huawei-wmi",
  692. },
  693. .probe = huawei_wmi_probe,
  694. .remove = huawei_wmi_remove,
  695. };
  696. static __init int huawei_wmi_init(void)
  697. {
  698. struct platform_device *pdev;
  699. int err;
  700. huawei_wmi = kzalloc(sizeof(struct huawei_wmi), GFP_KERNEL);
  701. if (!huawei_wmi)
  702. return -ENOMEM;
  703. quirks = &quirk_unknown;
  704. dmi_check_system(huawei_quirks);
  705. if (battery_reset != -1)
  706. quirks->battery_reset = battery_reset;
  707. if (report_brightness != -1)
  708. quirks->report_brightness = report_brightness;
  709. err = platform_driver_register(&huawei_wmi_driver);
  710. if (err)
  711. goto pdrv_err;
  712. pdev = platform_device_register_simple("huawei-wmi", PLATFORM_DEVID_NONE, NULL, 0);
  713. if (IS_ERR(pdev)) {
  714. err = PTR_ERR(pdev);
  715. goto pdev_err;
  716. }
  717. return 0;
  718. pdev_err:
  719. platform_driver_unregister(&huawei_wmi_driver);
  720. pdrv_err:
  721. kfree(huawei_wmi);
  722. return err;
  723. }
  724. static __exit void huawei_wmi_exit(void)
  725. {
  726. struct platform_device *pdev = to_platform_device(huawei_wmi->dev);
  727. platform_device_unregister(pdev);
  728. platform_driver_unregister(&huawei_wmi_driver);
  729. kfree(huawei_wmi);
  730. }
  731. module_init(huawei_wmi_init);
  732. module_exit(huawei_wmi_exit);
  733. MODULE_ALIAS("wmi:"HWMI_METHOD_GUID);
  734. MODULE_DEVICE_TABLE(wmi, huawei_wmi_events_id_table);
  735. MODULE_AUTHOR("Ayman Bagabas <[email protected]>");
  736. MODULE_DESCRIPTION("Huawei WMI laptop extras driver");
  737. MODULE_LICENSE("GPL v2");