topstar-laptop.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Topstar Laptop ACPI Extras driver
  4. *
  5. * Copyright (c) 2009 Herton Ronaldo Krzesinski <herton@mandriva.com.br>
  6. * Copyright (c) 2018 Guillaume Douézan-Grard
  7. *
  8. * Implementation inspired by existing x86 platform drivers, in special
  9. * asus/eepc/fujitsu-laptop, thanks to their authors.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/acpi.h>
  17. #include <linux/dmi.h>
  18. #include <linux/input.h>
  19. #include <linux/input/sparse-keymap.h>
  20. #include <linux/leds.h>
  21. #include <linux/platform_device.h>
  22. #define TOPSTAR_LAPTOP_CLASS "topstar"
  23. struct topstar_laptop {
  24. struct acpi_device *device;
  25. struct platform_device *platform;
  26. struct input_dev *input;
  27. struct led_classdev led;
  28. };
  29. /*
  30. * LED
  31. */
  32. static enum led_brightness topstar_led_get(struct led_classdev *led)
  33. {
  34. return led->brightness;
  35. }
  36. static int topstar_led_set(struct led_classdev *led,
  37. enum led_brightness state)
  38. {
  39. struct topstar_laptop *topstar = container_of(led,
  40. struct topstar_laptop, led);
  41. struct acpi_object_list params;
  42. union acpi_object in_obj;
  43. unsigned long long int ret;
  44. acpi_status status;
  45. params.count = 1;
  46. params.pointer = &in_obj;
  47. in_obj.type = ACPI_TYPE_INTEGER;
  48. in_obj.integer.value = 0x83;
  49. /*
  50. * Topstar ACPI returns 0x30001 when the LED is ON and 0x30000 when it
  51. * is OFF.
  52. */
  53. status = acpi_evaluate_integer(topstar->device->handle,
  54. "GETX", &params, &ret);
  55. if (ACPI_FAILURE(status))
  56. return -1;
  57. /*
  58. * FNCX(0x83) toggles the LED (more precisely, it is supposed to
  59. * act as an hardware switch and disconnect the WLAN adapter but
  60. * it seems to be faulty on some models like the Topstar U931
  61. * Notebook).
  62. */
  63. if ((ret == 0x30001 && state == LED_OFF)
  64. || (ret == 0x30000 && state != LED_OFF)) {
  65. status = acpi_execute_simple_method(topstar->device->handle,
  66. "FNCX", 0x83);
  67. if (ACPI_FAILURE(status))
  68. return -1;
  69. }
  70. return 0;
  71. }
  72. static int topstar_led_init(struct topstar_laptop *topstar)
  73. {
  74. topstar->led = (struct led_classdev) {
  75. .default_trigger = "rfkill0",
  76. .brightness_get = topstar_led_get,
  77. .brightness_set_blocking = topstar_led_set,
  78. .name = TOPSTAR_LAPTOP_CLASS "::wlan",
  79. };
  80. return led_classdev_register(&topstar->platform->dev, &topstar->led);
  81. }
  82. static void topstar_led_exit(struct topstar_laptop *topstar)
  83. {
  84. led_classdev_unregister(&topstar->led);
  85. }
  86. /*
  87. * Input
  88. */
  89. static const struct key_entry topstar_keymap[] = {
  90. { KE_KEY, 0x80, { KEY_BRIGHTNESSUP } },
  91. { KE_KEY, 0x81, { KEY_BRIGHTNESSDOWN } },
  92. { KE_KEY, 0x83, { KEY_VOLUMEUP } },
  93. { KE_KEY, 0x84, { KEY_VOLUMEDOWN } },
  94. { KE_KEY, 0x85, { KEY_MUTE } },
  95. { KE_KEY, 0x86, { KEY_SWITCHVIDEOMODE } },
  96. { KE_KEY, 0x87, { KEY_F13 } }, /* touchpad enable/disable key */
  97. { KE_KEY, 0x88, { KEY_WLAN } },
  98. { KE_KEY, 0x8a, { KEY_WWW } },
  99. { KE_KEY, 0x8b, { KEY_MAIL } },
  100. { KE_KEY, 0x8c, { KEY_MEDIA } },
  101. /* Known non hotkey events don't handled or that we don't care yet */
  102. { KE_IGNORE, 0x82, }, /* backlight event */
  103. { KE_IGNORE, 0x8e, },
  104. { KE_IGNORE, 0x8f, },
  105. { KE_IGNORE, 0x90, },
  106. /*
  107. * 'G key' generate two event codes, convert to only
  108. * one event/key code for now, consider replacing by
  109. * a switch (3G switch - SW_3G?)
  110. */
  111. { KE_KEY, 0x96, { KEY_F14 } },
  112. { KE_KEY, 0x97, { KEY_F14 } },
  113. { KE_END, 0 }
  114. };
  115. static void topstar_input_notify(struct topstar_laptop *topstar, int event)
  116. {
  117. if (!sparse_keymap_report_event(topstar->input, event, 1, true))
  118. pr_info("unknown event = 0x%02x\n", event);
  119. }
  120. static int topstar_input_init(struct topstar_laptop *topstar)
  121. {
  122. struct input_dev *input;
  123. int err;
  124. input = input_allocate_device();
  125. if (!input)
  126. return -ENOMEM;
  127. input->name = "Topstar Laptop extra buttons";
  128. input->phys = TOPSTAR_LAPTOP_CLASS "/input0";
  129. input->id.bustype = BUS_HOST;
  130. input->dev.parent = &topstar->platform->dev;
  131. err = sparse_keymap_setup(input, topstar_keymap, NULL);
  132. if (err) {
  133. pr_err("Unable to setup input device keymap\n");
  134. goto err_free_dev;
  135. }
  136. err = input_register_device(input);
  137. if (err) {
  138. pr_err("Unable to register input device\n");
  139. goto err_free_dev;
  140. }
  141. topstar->input = input;
  142. return 0;
  143. err_free_dev:
  144. input_free_device(input);
  145. return err;
  146. }
  147. static void topstar_input_exit(struct topstar_laptop *topstar)
  148. {
  149. input_unregister_device(topstar->input);
  150. }
  151. /*
  152. * Platform
  153. */
  154. static struct platform_driver topstar_platform_driver = {
  155. .driver = {
  156. .name = TOPSTAR_LAPTOP_CLASS,
  157. },
  158. };
  159. static int topstar_platform_init(struct topstar_laptop *topstar)
  160. {
  161. int err;
  162. topstar->platform = platform_device_alloc(TOPSTAR_LAPTOP_CLASS, -1);
  163. if (!topstar->platform)
  164. return -ENOMEM;
  165. platform_set_drvdata(topstar->platform, topstar);
  166. err = platform_device_add(topstar->platform);
  167. if (err)
  168. goto err_device_put;
  169. return 0;
  170. err_device_put:
  171. platform_device_put(topstar->platform);
  172. return err;
  173. }
  174. static void topstar_platform_exit(struct topstar_laptop *topstar)
  175. {
  176. platform_device_unregister(topstar->platform);
  177. }
  178. /*
  179. * ACPI
  180. */
  181. static int topstar_acpi_fncx_switch(struct acpi_device *device, bool state)
  182. {
  183. acpi_status status;
  184. u64 arg = state ? 0x86 : 0x87;
  185. status = acpi_execute_simple_method(device->handle, "FNCX", arg);
  186. if (ACPI_FAILURE(status)) {
  187. pr_err("Unable to switch FNCX notifications\n");
  188. return -ENODEV;
  189. }
  190. return 0;
  191. }
  192. static void topstar_acpi_notify(struct acpi_device *device, u32 event)
  193. {
  194. struct topstar_laptop *topstar = acpi_driver_data(device);
  195. static bool dup_evnt[2];
  196. bool *dup;
  197. /* 0x83 and 0x84 key events comes duplicated... */
  198. if (event == 0x83 || event == 0x84) {
  199. dup = &dup_evnt[event - 0x83];
  200. if (*dup) {
  201. *dup = false;
  202. return;
  203. }
  204. *dup = true;
  205. }
  206. topstar_input_notify(topstar, event);
  207. }
  208. static int topstar_acpi_init(struct topstar_laptop *topstar)
  209. {
  210. return topstar_acpi_fncx_switch(topstar->device, true);
  211. }
  212. static void topstar_acpi_exit(struct topstar_laptop *topstar)
  213. {
  214. topstar_acpi_fncx_switch(topstar->device, false);
  215. }
  216. /*
  217. * Enable software-based WLAN LED control on systems with defective
  218. * hardware switch.
  219. */
  220. static bool led_workaround;
  221. static int dmi_led_workaround(const struct dmi_system_id *id)
  222. {
  223. led_workaround = true;
  224. return 0;
  225. }
  226. static const struct dmi_system_id topstar_dmi_ids[] = {
  227. {
  228. .callback = dmi_led_workaround,
  229. .ident = "Topstar U931/RVP7",
  230. .matches = {
  231. DMI_MATCH(DMI_BOARD_NAME, "U931"),
  232. DMI_MATCH(DMI_BOARD_VERSION, "RVP7"),
  233. },
  234. },
  235. {}
  236. };
  237. static int topstar_acpi_add(struct acpi_device *device)
  238. {
  239. struct topstar_laptop *topstar;
  240. int err;
  241. dmi_check_system(topstar_dmi_ids);
  242. topstar = kzalloc(sizeof(struct topstar_laptop), GFP_KERNEL);
  243. if (!topstar)
  244. return -ENOMEM;
  245. strcpy(acpi_device_name(device), "Topstar TPSACPI");
  246. strcpy(acpi_device_class(device), TOPSTAR_LAPTOP_CLASS);
  247. device->driver_data = topstar;
  248. topstar->device = device;
  249. err = topstar_acpi_init(topstar);
  250. if (err)
  251. goto err_free;
  252. err = topstar_platform_init(topstar);
  253. if (err)
  254. goto err_acpi_exit;
  255. err = topstar_input_init(topstar);
  256. if (err)
  257. goto err_platform_exit;
  258. if (led_workaround) {
  259. err = topstar_led_init(topstar);
  260. if (err)
  261. goto err_input_exit;
  262. }
  263. return 0;
  264. err_input_exit:
  265. topstar_input_exit(topstar);
  266. err_platform_exit:
  267. topstar_platform_exit(topstar);
  268. err_acpi_exit:
  269. topstar_acpi_exit(topstar);
  270. err_free:
  271. kfree(topstar);
  272. return err;
  273. }
  274. static int topstar_acpi_remove(struct acpi_device *device)
  275. {
  276. struct topstar_laptop *topstar = acpi_driver_data(device);
  277. if (led_workaround)
  278. topstar_led_exit(topstar);
  279. topstar_input_exit(topstar);
  280. topstar_platform_exit(topstar);
  281. topstar_acpi_exit(topstar);
  282. kfree(topstar);
  283. return 0;
  284. }
  285. static const struct acpi_device_id topstar_device_ids[] = {
  286. { "TPS0001", 0 },
  287. { "TPSACPI01", 0 },
  288. { "", 0 },
  289. };
  290. MODULE_DEVICE_TABLE(acpi, topstar_device_ids);
  291. static struct acpi_driver topstar_acpi_driver = {
  292. .name = "Topstar laptop ACPI driver",
  293. .class = TOPSTAR_LAPTOP_CLASS,
  294. .ids = topstar_device_ids,
  295. .ops = {
  296. .add = topstar_acpi_add,
  297. .remove = topstar_acpi_remove,
  298. .notify = topstar_acpi_notify,
  299. },
  300. };
  301. static int __init topstar_laptop_init(void)
  302. {
  303. int ret;
  304. ret = platform_driver_register(&topstar_platform_driver);
  305. if (ret < 0)
  306. return ret;
  307. ret = acpi_bus_register_driver(&topstar_acpi_driver);
  308. if (ret < 0)
  309. goto err_driver_unreg;
  310. pr_info("ACPI extras driver loaded\n");
  311. return 0;
  312. err_driver_unreg:
  313. platform_driver_unregister(&topstar_platform_driver);
  314. return ret;
  315. }
  316. static void __exit topstar_laptop_exit(void)
  317. {
  318. acpi_bus_unregister_driver(&topstar_acpi_driver);
  319. platform_driver_unregister(&topstar_platform_driver);
  320. }
  321. module_init(topstar_laptop_init);
  322. module_exit(topstar_laptop_exit);
  323. MODULE_AUTHOR("Herton Ronaldo Krzesinski");
  324. MODULE_AUTHOR("Guillaume Douézan-Grard");
  325. MODULE_DESCRIPTION("Topstar Laptop ACPI Extras driver");
  326. MODULE_LICENSE("GPL");