ideapad_slidebar.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Input driver for slidebars on some Lenovo IdeaPad laptops
  4. *
  5. * Copyright (C) 2013 Andrey Moiseev <o2g.org.ru@gmail.com>
  6. *
  7. * Reverse-engineered from Lenovo SlideNav software (SBarHook.dll).
  8. *
  9. * Trademarks are the property of their respective owners.
  10. */
  11. /*
  12. * Currently tested and works on:
  13. * Lenovo IdeaPad Y550
  14. * Lenovo IdeaPad Y550P
  15. *
  16. * Other models can be added easily. To test,
  17. * load with 'force' parameter set 'true'.
  18. *
  19. * LEDs blinking and input mode are managed via sysfs,
  20. * (hex, unsigned byte value):
  21. * /sys/devices/platform/ideapad_slidebar/slidebar_mode
  22. *
  23. * The value is in byte range, however, I only figured out
  24. * how bits 0b10011001 work. Some other bits, probably,
  25. * are meaningful too.
  26. *
  27. * Possible states:
  28. *
  29. * STD_INT, ONMOV_INT, OFF_INT, LAST_POLL, OFF_POLL
  30. *
  31. * Meaning:
  32. * released touched
  33. * STD 'heartbeat' lights follow the finger
  34. * ONMOV no lights lights follow the finger
  35. * LAST at last pos lights follow the finger
  36. * OFF no lights no lights
  37. *
  38. * INT all input events are generated, interrupts are used
  39. * POLL no input events by default, to get them,
  40. * send 0b10000000 (read below)
  41. *
  42. * Commands: write
  43. *
  44. * All | 0b01001 -> STD_INT
  45. * possible | 0b10001 -> ONMOV_INT
  46. * states | 0b01000 -> OFF_INT
  47. *
  48. * | 0b0 -> LAST_POLL
  49. * STD_INT or ONMOV_INT |
  50. * | 0b1 -> STD_INT
  51. *
  52. * | 0b0 -> OFF_POLL
  53. * OFF_INT or OFF_POLL |
  54. * | 0b1 -> OFF_INT
  55. *
  56. * Any state | 0b10000000 -> if the slidebar has updated data,
  57. * produce one input event (last position),
  58. * switch to respective POLL mode
  59. * (like 0x0), if not in POLL mode yet.
  60. *
  61. * Get current state: read
  62. *
  63. * masked by 0x11 read value means:
  64. *
  65. * 0x00 LAST
  66. * 0x01 STD
  67. * 0x10 OFF
  68. * 0x11 ONMOV
  69. */
  70. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  71. #include <linux/module.h>
  72. #include <linux/kernel.h>
  73. #include <linux/dmi.h>
  74. #include <linux/spinlock.h>
  75. #include <linux/platform_device.h>
  76. #include <linux/input.h>
  77. #include <linux/io.h>
  78. #include <linux/ioport.h>
  79. #include <linux/i8042.h>
  80. #include <linux/serio.h>
  81. #define IDEAPAD_BASE 0xff29
  82. static bool force;
  83. module_param(force, bool, 0);
  84. MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
  85. static DEFINE_SPINLOCK(io_lock);
  86. static struct input_dev *slidebar_input_dev;
  87. static struct platform_device *slidebar_platform_dev;
  88. static u8 slidebar_pos_get(void)
  89. {
  90. guard(spinlock_irqsave)(&io_lock);
  91. outb(0xf4, 0xff29);
  92. outb(0xbf, 0xff2a);
  93. return inb(0xff2b);
  94. }
  95. static u8 slidebar_mode_get(void)
  96. {
  97. guard(spinlock_irqsave)(&io_lock);
  98. outb(0xf7, 0xff29);
  99. outb(0x8b, 0xff2a);
  100. return inb(0xff2b);
  101. }
  102. static void slidebar_mode_set(u8 mode)
  103. {
  104. guard(spinlock_irqsave)(&io_lock);
  105. outb(0xf7, 0xff29);
  106. outb(0x8b, 0xff2a);
  107. outb(mode, 0xff2b);
  108. }
  109. static bool slidebar_i8042_filter(unsigned char data, unsigned char str,
  110. struct serio *port, void *context)
  111. {
  112. static bool extended = false;
  113. /* We are only interested in data coming form KBC port */
  114. if (str & I8042_STR_AUXDATA)
  115. return false;
  116. /* Scancodes: e03b on move, e0bb on release. */
  117. if (data == 0xe0) {
  118. extended = true;
  119. return true;
  120. }
  121. if (!extended)
  122. return false;
  123. extended = false;
  124. if (likely((data & 0x7f) != 0x3b)) {
  125. serio_interrupt(port, 0xe0, 0);
  126. return false;
  127. }
  128. if (data & 0x80) {
  129. input_report_key(slidebar_input_dev, BTN_TOUCH, 0);
  130. } else {
  131. input_report_key(slidebar_input_dev, BTN_TOUCH, 1);
  132. input_report_abs(slidebar_input_dev, ABS_X, slidebar_pos_get());
  133. }
  134. input_sync(slidebar_input_dev);
  135. return true;
  136. }
  137. static ssize_t show_slidebar_mode(struct device *dev,
  138. struct device_attribute *attr,
  139. char *buf)
  140. {
  141. return sprintf(buf, "%x\n", slidebar_mode_get());
  142. }
  143. static ssize_t store_slidebar_mode(struct device *dev,
  144. struct device_attribute *attr,
  145. const char *buf, size_t count)
  146. {
  147. u8 mode;
  148. int error;
  149. error = kstrtou8(buf, 0, &mode);
  150. if (error)
  151. return error;
  152. slidebar_mode_set(mode);
  153. return count;
  154. }
  155. static DEVICE_ATTR(slidebar_mode, S_IWUSR | S_IRUGO,
  156. show_slidebar_mode, store_slidebar_mode);
  157. static struct attribute *ideapad_attrs[] = {
  158. &dev_attr_slidebar_mode.attr,
  159. NULL
  160. };
  161. static struct attribute_group ideapad_attr_group = {
  162. .attrs = ideapad_attrs
  163. };
  164. static const struct attribute_group *ideapad_attr_groups[] = {
  165. &ideapad_attr_group,
  166. NULL
  167. };
  168. static int __init ideapad_probe(struct platform_device* pdev)
  169. {
  170. int err;
  171. if (!request_region(IDEAPAD_BASE, 3, "ideapad_slidebar")) {
  172. dev_err(&pdev->dev, "IO ports are busy\n");
  173. return -EBUSY;
  174. }
  175. slidebar_input_dev = input_allocate_device();
  176. if (!slidebar_input_dev) {
  177. dev_err(&pdev->dev, "Failed to allocate input device\n");
  178. err = -ENOMEM;
  179. goto err_release_ports;
  180. }
  181. slidebar_input_dev->name = "IdeaPad Slidebar";
  182. slidebar_input_dev->id.bustype = BUS_HOST;
  183. slidebar_input_dev->dev.parent = &pdev->dev;
  184. input_set_capability(slidebar_input_dev, EV_KEY, BTN_TOUCH);
  185. input_set_capability(slidebar_input_dev, EV_ABS, ABS_X);
  186. input_set_abs_params(slidebar_input_dev, ABS_X, 0, 0xff, 0, 0);
  187. err = i8042_install_filter(slidebar_i8042_filter, NULL);
  188. if (err) {
  189. dev_err(&pdev->dev,
  190. "Failed to install i8042 filter: %d\n", err);
  191. goto err_free_dev;
  192. }
  193. err = input_register_device(slidebar_input_dev);
  194. if (err) {
  195. dev_err(&pdev->dev,
  196. "Failed to register input device: %d\n", err);
  197. goto err_remove_filter;
  198. }
  199. return 0;
  200. err_remove_filter:
  201. i8042_remove_filter(slidebar_i8042_filter);
  202. err_free_dev:
  203. input_free_device(slidebar_input_dev);
  204. err_release_ports:
  205. release_region(IDEAPAD_BASE, 3);
  206. return err;
  207. }
  208. static void ideapad_remove(struct platform_device *pdev)
  209. {
  210. i8042_remove_filter(slidebar_i8042_filter);
  211. input_unregister_device(slidebar_input_dev);
  212. release_region(IDEAPAD_BASE, 3);
  213. }
  214. static struct platform_driver slidebar_drv = {
  215. .driver = {
  216. .name = "ideapad_slidebar",
  217. },
  218. .remove = ideapad_remove,
  219. };
  220. static int __init ideapad_dmi_check(const struct dmi_system_id *id)
  221. {
  222. pr_info("Laptop model '%s'\n", id->ident);
  223. return 1;
  224. }
  225. static const struct dmi_system_id ideapad_dmi[] __initconst = {
  226. {
  227. .ident = "Lenovo IdeaPad Y550",
  228. .matches = {
  229. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  230. DMI_MATCH(DMI_PRODUCT_NAME, "20017"),
  231. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo IdeaPad Y550")
  232. },
  233. .callback = ideapad_dmi_check
  234. },
  235. {
  236. .ident = "Lenovo IdeaPad Y550P",
  237. .matches = {
  238. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  239. DMI_MATCH(DMI_PRODUCT_NAME, "20035"),
  240. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo IdeaPad Y550P")
  241. },
  242. .callback = ideapad_dmi_check
  243. },
  244. { NULL, }
  245. };
  246. MODULE_DEVICE_TABLE(dmi, ideapad_dmi);
  247. static int __init slidebar_init(void)
  248. {
  249. int err;
  250. if (!force && !dmi_check_system(ideapad_dmi)) {
  251. pr_err("DMI does not match\n");
  252. return -ENODEV;
  253. }
  254. slidebar_platform_dev = platform_device_alloc("ideapad_slidebar", -1);
  255. if (!slidebar_platform_dev) {
  256. pr_err("Not enough memory\n");
  257. return -ENOMEM;
  258. }
  259. slidebar_platform_dev->dev.groups = ideapad_attr_groups;
  260. err = platform_device_add(slidebar_platform_dev);
  261. if (err) {
  262. pr_err("Failed to register platform device\n");
  263. goto err_free_dev;
  264. }
  265. err = platform_driver_probe(&slidebar_drv, ideapad_probe);
  266. if (err) {
  267. pr_err("Failed to register platform driver\n");
  268. goto err_delete_dev;
  269. }
  270. return 0;
  271. err_delete_dev:
  272. platform_device_del(slidebar_platform_dev);
  273. err_free_dev:
  274. platform_device_put(slidebar_platform_dev);
  275. return err;
  276. }
  277. static void __exit slidebar_exit(void)
  278. {
  279. platform_device_unregister(slidebar_platform_dev);
  280. platform_driver_unregister(&slidebar_drv);
  281. }
  282. module_init(slidebar_init);
  283. module_exit(slidebar_exit);
  284. MODULE_AUTHOR("Andrey Moiseev <o2g.org.ru@gmail.com>");
  285. MODULE_DESCRIPTION("Slidebar input support for some Lenovo IdeaPad laptops");
  286. MODULE_LICENSE("GPL");