ideapad_slidebar.c 8.2 KB

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