1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21ea4c161SAndrey Moiseev /*
31ea4c161SAndrey Moiseev * Input driver for slidebars on some Lenovo IdeaPad laptops
41ea4c161SAndrey Moiseev *
51ea4c161SAndrey Moiseev * Copyright (C) 2013 Andrey Moiseev <o2g.org.ru@gmail.com>
61ea4c161SAndrey Moiseev *
71ea4c161SAndrey Moiseev * Reverse-engineered from Lenovo SlideNav software (SBarHook.dll).
81ea4c161SAndrey Moiseev *
91ea4c161SAndrey Moiseev * Trademarks are the property of their respective owners.
101ea4c161SAndrey Moiseev */
111ea4c161SAndrey Moiseev
121ea4c161SAndrey Moiseev /*
131ea4c161SAndrey Moiseev * Currently tested and works on:
141ea4c161SAndrey Moiseev * Lenovo IdeaPad Y550
151ea4c161SAndrey Moiseev * Lenovo IdeaPad Y550P
161ea4c161SAndrey Moiseev *
171ea4c161SAndrey Moiseev * Other models can be added easily. To test,
181ea4c161SAndrey Moiseev * load with 'force' parameter set 'true'.
191ea4c161SAndrey Moiseev *
201ea4c161SAndrey Moiseev * LEDs blinking and input mode are managed via sysfs,
211ea4c161SAndrey Moiseev * (hex, unsigned byte value):
221ea4c161SAndrey Moiseev * /sys/devices/platform/ideapad_slidebar/slidebar_mode
231ea4c161SAndrey Moiseev *
241ea4c161SAndrey Moiseev * The value is in byte range, however, I only figured out
251ea4c161SAndrey Moiseev * how bits 0b10011001 work. Some other bits, probably,
261ea4c161SAndrey Moiseev * are meaningfull too.
271ea4c161SAndrey Moiseev *
281ea4c161SAndrey Moiseev * Possible states:
291ea4c161SAndrey Moiseev *
301ea4c161SAndrey Moiseev * STD_INT, ONMOV_INT, OFF_INT, LAST_POLL, OFF_POLL
311ea4c161SAndrey Moiseev *
321ea4c161SAndrey Moiseev * Meaning:
331ea4c161SAndrey Moiseev * released touched
341ea4c161SAndrey Moiseev * STD 'heartbeat' lights follow the finger
351ea4c161SAndrey Moiseev * ONMOV no lights lights follow the finger
361ea4c161SAndrey Moiseev * LAST at last pos lights follow the finger
371ea4c161SAndrey Moiseev * OFF no lights no lights
381ea4c161SAndrey Moiseev *
391ea4c161SAndrey Moiseev * INT all input events are generated, interrupts are used
401ea4c161SAndrey Moiseev * POLL no input events by default, to get them,
411ea4c161SAndrey Moiseev * send 0b10000000 (read below)
421ea4c161SAndrey Moiseev *
431ea4c161SAndrey Moiseev * Commands: write
441ea4c161SAndrey Moiseev *
451ea4c161SAndrey Moiseev * All | 0b01001 -> STD_INT
461ea4c161SAndrey Moiseev * possible | 0b10001 -> ONMOV_INT
471ea4c161SAndrey Moiseev * states | 0b01000 -> OFF_INT
481ea4c161SAndrey Moiseev *
491ea4c161SAndrey Moiseev * | 0b0 -> LAST_POLL
501ea4c161SAndrey Moiseev * STD_INT or ONMOV_INT |
511ea4c161SAndrey Moiseev * | 0b1 -> STD_INT
521ea4c161SAndrey Moiseev *
531ea4c161SAndrey Moiseev * | 0b0 -> OFF_POLL
541ea4c161SAndrey Moiseev * OFF_INT or OFF_POLL |
551ea4c161SAndrey Moiseev * | 0b1 -> OFF_INT
561ea4c161SAndrey Moiseev *
571ea4c161SAndrey Moiseev * Any state | 0b10000000 -> if the slidebar has updated data,
581ea4c161SAndrey Moiseev * produce one input event (last position),
591ea4c161SAndrey Moiseev * switch to respective POLL mode
601ea4c161SAndrey Moiseev * (like 0x0), if not in POLL mode yet.
611ea4c161SAndrey Moiseev *
621ea4c161SAndrey Moiseev * Get current state: read
631ea4c161SAndrey Moiseev *
641ea4c161SAndrey Moiseev * masked by 0x11 read value means:
651ea4c161SAndrey Moiseev *
661ea4c161SAndrey Moiseev * 0x00 LAST
671ea4c161SAndrey Moiseev * 0x01 STD
681ea4c161SAndrey Moiseev * 0x10 OFF
691ea4c161SAndrey Moiseev * 0x11 ONMOV
701ea4c161SAndrey Moiseev */
711ea4c161SAndrey Moiseev
721ea4c161SAndrey Moiseev #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
731ea4c161SAndrey Moiseev
741ea4c161SAndrey Moiseev #include <linux/module.h>
751ea4c161SAndrey Moiseev #include <linux/kernel.h>
761ea4c161SAndrey Moiseev #include <linux/dmi.h>
771ea4c161SAndrey Moiseev #include <linux/spinlock.h>
781ea4c161SAndrey Moiseev #include <linux/platform_device.h>
791ea4c161SAndrey Moiseev #include <linux/input.h>
801ea4c161SAndrey Moiseev #include <linux/io.h>
811ea4c161SAndrey Moiseev #include <linux/ioport.h>
821ea4c161SAndrey Moiseev #include <linux/i8042.h>
831ea4c161SAndrey Moiseev #include <linux/serio.h>
841ea4c161SAndrey Moiseev
851ea4c161SAndrey Moiseev #define IDEAPAD_BASE 0xff29
861ea4c161SAndrey Moiseev
871ea4c161SAndrey Moiseev static bool force;
881ea4c161SAndrey Moiseev module_param(force, bool, 0);
891ea4c161SAndrey Moiseev MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
901ea4c161SAndrey Moiseev
911ea4c161SAndrey Moiseev static DEFINE_SPINLOCK(io_lock);
921ea4c161SAndrey Moiseev
931ea4c161SAndrey Moiseev static struct input_dev *slidebar_input_dev;
941ea4c161SAndrey Moiseev static struct platform_device *slidebar_platform_dev;
951ea4c161SAndrey Moiseev
slidebar_pos_get(void)961ea4c161SAndrey Moiseev static u8 slidebar_pos_get(void)
971ea4c161SAndrey Moiseev {
981ea4c161SAndrey Moiseev u8 res;
991ea4c161SAndrey Moiseev unsigned long flags;
1001ea4c161SAndrey Moiseev
1011ea4c161SAndrey Moiseev spin_lock_irqsave(&io_lock, flags);
1021ea4c161SAndrey Moiseev outb(0xf4, 0xff29);
1031ea4c161SAndrey Moiseev outb(0xbf, 0xff2a);
1041ea4c161SAndrey Moiseev res = inb(0xff2b);
1051ea4c161SAndrey Moiseev spin_unlock_irqrestore(&io_lock, flags);
1061ea4c161SAndrey Moiseev
1071ea4c161SAndrey Moiseev return res;
1081ea4c161SAndrey Moiseev }
1091ea4c161SAndrey Moiseev
slidebar_mode_get(void)1101ea4c161SAndrey Moiseev static u8 slidebar_mode_get(void)
1111ea4c161SAndrey Moiseev {
1121ea4c161SAndrey Moiseev u8 res;
1131ea4c161SAndrey Moiseev unsigned long flags;
1141ea4c161SAndrey Moiseev
1151ea4c161SAndrey Moiseev spin_lock_irqsave(&io_lock, flags);
1161ea4c161SAndrey Moiseev outb(0xf7, 0xff29);
1171ea4c161SAndrey Moiseev outb(0x8b, 0xff2a);
1181ea4c161SAndrey Moiseev res = inb(0xff2b);
1191ea4c161SAndrey Moiseev spin_unlock_irqrestore(&io_lock, flags);
1201ea4c161SAndrey Moiseev
1211ea4c161SAndrey Moiseev return res;
1221ea4c161SAndrey Moiseev }
1231ea4c161SAndrey Moiseev
slidebar_mode_set(u8 mode)1241ea4c161SAndrey Moiseev static void slidebar_mode_set(u8 mode)
1251ea4c161SAndrey Moiseev {
1261ea4c161SAndrey Moiseev unsigned long flags;
1271ea4c161SAndrey Moiseev
1281ea4c161SAndrey Moiseev spin_lock_irqsave(&io_lock, flags);
1291ea4c161SAndrey Moiseev outb(0xf7, 0xff29);
1301ea4c161SAndrey Moiseev outb(0x8b, 0xff2a);
1311ea4c161SAndrey Moiseev outb(mode, 0xff2b);
1321ea4c161SAndrey Moiseev spin_unlock_irqrestore(&io_lock, flags);
1331ea4c161SAndrey Moiseev }
1341ea4c161SAndrey Moiseev
slidebar_i8042_filter(unsigned char data,unsigned char str,struct serio * port)1351ea4c161SAndrey Moiseev static bool slidebar_i8042_filter(unsigned char data, unsigned char str,
1361ea4c161SAndrey Moiseev struct serio *port)
1371ea4c161SAndrey Moiseev {
1381ea4c161SAndrey Moiseev static bool extended = false;
1391ea4c161SAndrey Moiseev
1401ea4c161SAndrey Moiseev /* We are only interested in data coming form KBC port */
1411ea4c161SAndrey Moiseev if (str & I8042_STR_AUXDATA)
1421ea4c161SAndrey Moiseev return false;
1431ea4c161SAndrey Moiseev
1441ea4c161SAndrey Moiseev /* Scancodes: e03b on move, e0bb on release. */
1451ea4c161SAndrey Moiseev if (data == 0xe0) {
1461ea4c161SAndrey Moiseev extended = true;
1471ea4c161SAndrey Moiseev return true;
1481ea4c161SAndrey Moiseev }
1491ea4c161SAndrey Moiseev
1501ea4c161SAndrey Moiseev if (!extended)
1511ea4c161SAndrey Moiseev return false;
1521ea4c161SAndrey Moiseev
1531ea4c161SAndrey Moiseev extended = false;
1541ea4c161SAndrey Moiseev
1551ea4c161SAndrey Moiseev if (likely((data & 0x7f) != 0x3b)) {
1561ea4c161SAndrey Moiseev serio_interrupt(port, 0xe0, 0);
1571ea4c161SAndrey Moiseev return false;
1581ea4c161SAndrey Moiseev }
1591ea4c161SAndrey Moiseev
1601ea4c161SAndrey Moiseev if (data & 0x80) {
1611ea4c161SAndrey Moiseev input_report_key(slidebar_input_dev, BTN_TOUCH, 0);
1621ea4c161SAndrey Moiseev } else {
1631ea4c161SAndrey Moiseev input_report_key(slidebar_input_dev, BTN_TOUCH, 1);
1641ea4c161SAndrey Moiseev input_report_abs(slidebar_input_dev, ABS_X, slidebar_pos_get());
1651ea4c161SAndrey Moiseev }
1661ea4c161SAndrey Moiseev input_sync(slidebar_input_dev);
1671ea4c161SAndrey Moiseev
1681ea4c161SAndrey Moiseev return true;
1691ea4c161SAndrey Moiseev }
1701ea4c161SAndrey Moiseev
show_slidebar_mode(struct device * dev,struct device_attribute * attr,char * buf)1711ea4c161SAndrey Moiseev static ssize_t show_slidebar_mode(struct device *dev,
1721ea4c161SAndrey Moiseev struct device_attribute *attr,
1731ea4c161SAndrey Moiseev char *buf)
1741ea4c161SAndrey Moiseev {
1751ea4c161SAndrey Moiseev return sprintf(buf, "%x\n", slidebar_mode_get());
1761ea4c161SAndrey Moiseev }
1771ea4c161SAndrey Moiseev
store_slidebar_mode(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1781ea4c161SAndrey Moiseev static ssize_t store_slidebar_mode(struct device *dev,
1791ea4c161SAndrey Moiseev struct device_attribute *attr,
1801ea4c161SAndrey Moiseev const char *buf, size_t count)
1811ea4c161SAndrey Moiseev {
1821ea4c161SAndrey Moiseev u8 mode;
1831ea4c161SAndrey Moiseev int error;
1841ea4c161SAndrey Moiseev
1851ea4c161SAndrey Moiseev error = kstrtou8(buf, 0, &mode);
1861ea4c161SAndrey Moiseev if (error)
1871ea4c161SAndrey Moiseev return error;
1881ea4c161SAndrey Moiseev
1891ea4c161SAndrey Moiseev slidebar_mode_set(mode);
1901ea4c161SAndrey Moiseev
1911ea4c161SAndrey Moiseev return count;
1921ea4c161SAndrey Moiseev }
1931ea4c161SAndrey Moiseev
1941ea4c161SAndrey Moiseev static DEVICE_ATTR(slidebar_mode, S_IWUSR | S_IRUGO,
1951ea4c161SAndrey Moiseev show_slidebar_mode, store_slidebar_mode);
1961ea4c161SAndrey Moiseev
1971ea4c161SAndrey Moiseev static struct attribute *ideapad_attrs[] = {
1981ea4c161SAndrey Moiseev &dev_attr_slidebar_mode.attr,
1991ea4c161SAndrey Moiseev NULL
2001ea4c161SAndrey Moiseev };
2011ea4c161SAndrey Moiseev
2021ea4c161SAndrey Moiseev static struct attribute_group ideapad_attr_group = {
2031ea4c161SAndrey Moiseev .attrs = ideapad_attrs
2041ea4c161SAndrey Moiseev };
2051ea4c161SAndrey Moiseev
2061ea4c161SAndrey Moiseev static const struct attribute_group *ideapad_attr_groups[] = {
2071ea4c161SAndrey Moiseev &ideapad_attr_group,
2081ea4c161SAndrey Moiseev NULL
2091ea4c161SAndrey Moiseev };
2101ea4c161SAndrey Moiseev
ideapad_probe(struct platform_device * pdev)2111ea4c161SAndrey Moiseev static int __init ideapad_probe(struct platform_device* pdev)
2121ea4c161SAndrey Moiseev {
2131ea4c161SAndrey Moiseev int err;
2141ea4c161SAndrey Moiseev
2151ea4c161SAndrey Moiseev if (!request_region(IDEAPAD_BASE, 3, "ideapad_slidebar")) {
2161ea4c161SAndrey Moiseev dev_err(&pdev->dev, "IO ports are busy\n");
2171ea4c161SAndrey Moiseev return -EBUSY;
2181ea4c161SAndrey Moiseev }
2191ea4c161SAndrey Moiseev
2201ea4c161SAndrey Moiseev slidebar_input_dev = input_allocate_device();
2211ea4c161SAndrey Moiseev if (!slidebar_input_dev) {
2221ea4c161SAndrey Moiseev dev_err(&pdev->dev, "Failed to allocate input device\n");
2231ea4c161SAndrey Moiseev err = -ENOMEM;
2241ea4c161SAndrey Moiseev goto err_release_ports;
2251ea4c161SAndrey Moiseev }
2261ea4c161SAndrey Moiseev
2271ea4c161SAndrey Moiseev slidebar_input_dev->name = "IdeaPad Slidebar";
2281ea4c161SAndrey Moiseev slidebar_input_dev->id.bustype = BUS_HOST;
2291ea4c161SAndrey Moiseev slidebar_input_dev->dev.parent = &pdev->dev;
2301ea4c161SAndrey Moiseev input_set_capability(slidebar_input_dev, EV_KEY, BTN_TOUCH);
2311ea4c161SAndrey Moiseev input_set_capability(slidebar_input_dev, EV_ABS, ABS_X);
2321ea4c161SAndrey Moiseev input_set_abs_params(slidebar_input_dev, ABS_X, 0, 0xff, 0, 0);
2331ea4c161SAndrey Moiseev
2341ea4c161SAndrey Moiseev err = i8042_install_filter(slidebar_i8042_filter);
2351ea4c161SAndrey Moiseev if (err) {
2361ea4c161SAndrey Moiseev dev_err(&pdev->dev,
2371ea4c161SAndrey Moiseev "Failed to install i8042 filter: %d\n", err);
2381ea4c161SAndrey Moiseev goto err_free_dev;
2391ea4c161SAndrey Moiseev }
2401ea4c161SAndrey Moiseev
2411ea4c161SAndrey Moiseev err = input_register_device(slidebar_input_dev);
2421ea4c161SAndrey Moiseev if (err) {
2431ea4c161SAndrey Moiseev dev_err(&pdev->dev,
2441ea4c161SAndrey Moiseev "Failed to register input device: %d\n", err);
2451ea4c161SAndrey Moiseev goto err_remove_filter;
2461ea4c161SAndrey Moiseev }
2471ea4c161SAndrey Moiseev
2481ea4c161SAndrey Moiseev return 0;
2491ea4c161SAndrey Moiseev
2501ea4c161SAndrey Moiseev err_remove_filter:
2511ea4c161SAndrey Moiseev i8042_remove_filter(slidebar_i8042_filter);
2521ea4c161SAndrey Moiseev err_free_dev:
2531ea4c161SAndrey Moiseev input_free_device(slidebar_input_dev);
2541ea4c161SAndrey Moiseev err_release_ports:
2551ea4c161SAndrey Moiseev release_region(IDEAPAD_BASE, 3);
2561ea4c161SAndrey Moiseev return err;
2571ea4c161SAndrey Moiseev }
2581ea4c161SAndrey Moiseev
ideapad_remove(struct platform_device * pdev)2591ea4c161SAndrey Moiseev static int ideapad_remove(struct platform_device *pdev)
2601ea4c161SAndrey Moiseev {
2611ea4c161SAndrey Moiseev i8042_remove_filter(slidebar_i8042_filter);
2621ea4c161SAndrey Moiseev input_unregister_device(slidebar_input_dev);
2631ea4c161SAndrey Moiseev release_region(IDEAPAD_BASE, 3);
2641ea4c161SAndrey Moiseev
2651ea4c161SAndrey Moiseev return 0;
2661ea4c161SAndrey Moiseev }
2671ea4c161SAndrey Moiseev
2681ea4c161SAndrey Moiseev static struct platform_driver slidebar_drv = {
2691ea4c161SAndrey Moiseev .driver = {
2701ea4c161SAndrey Moiseev .name = "ideapad_slidebar",
2711ea4c161SAndrey Moiseev },
2721ea4c161SAndrey Moiseev .remove = ideapad_remove,
2731ea4c161SAndrey Moiseev };
2741ea4c161SAndrey Moiseev
ideapad_dmi_check(const struct dmi_system_id * id)2751ea4c161SAndrey Moiseev static int __init ideapad_dmi_check(const struct dmi_system_id *id)
2761ea4c161SAndrey Moiseev {
2771ea4c161SAndrey Moiseev pr_info("Laptop model '%s'\n", id->ident);
2781ea4c161SAndrey Moiseev return 1;
2791ea4c161SAndrey Moiseev }
2801ea4c161SAndrey Moiseev
2811ea4c161SAndrey Moiseev static const struct dmi_system_id ideapad_dmi[] __initconst = {
2821ea4c161SAndrey Moiseev {
2831ea4c161SAndrey Moiseev .ident = "Lenovo IdeaPad Y550",
2841ea4c161SAndrey Moiseev .matches = {
2851ea4c161SAndrey Moiseev DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
2861ea4c161SAndrey Moiseev DMI_MATCH(DMI_PRODUCT_NAME, "20017"),
2871ea4c161SAndrey Moiseev DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo IdeaPad Y550")
2881ea4c161SAndrey Moiseev },
2891ea4c161SAndrey Moiseev .callback = ideapad_dmi_check
2901ea4c161SAndrey Moiseev },
2911ea4c161SAndrey Moiseev {
2921ea4c161SAndrey Moiseev .ident = "Lenovo IdeaPad Y550P",
2931ea4c161SAndrey Moiseev .matches = {
2941ea4c161SAndrey Moiseev DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
2951ea4c161SAndrey Moiseev DMI_MATCH(DMI_PRODUCT_NAME, "20035"),
2961ea4c161SAndrey Moiseev DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo IdeaPad Y550P")
2971ea4c161SAndrey Moiseev },
2981ea4c161SAndrey Moiseev .callback = ideapad_dmi_check
2991ea4c161SAndrey Moiseev },
3001ea4c161SAndrey Moiseev { NULL, }
3011ea4c161SAndrey Moiseev };
3021ea4c161SAndrey Moiseev MODULE_DEVICE_TABLE(dmi, ideapad_dmi);
3031ea4c161SAndrey Moiseev
slidebar_init(void)3041ea4c161SAndrey Moiseev static int __init slidebar_init(void)
3051ea4c161SAndrey Moiseev {
3061ea4c161SAndrey Moiseev int err;
3071ea4c161SAndrey Moiseev
3081ea4c161SAndrey Moiseev if (!force && !dmi_check_system(ideapad_dmi)) {
3091ea4c161SAndrey Moiseev pr_err("DMI does not match\n");
3101ea4c161SAndrey Moiseev return -ENODEV;
3111ea4c161SAndrey Moiseev }
3121ea4c161SAndrey Moiseev
3131ea4c161SAndrey Moiseev slidebar_platform_dev = platform_device_alloc("ideapad_slidebar", -1);
3141ea4c161SAndrey Moiseev if (!slidebar_platform_dev) {
3151ea4c161SAndrey Moiseev pr_err("Not enough memory\n");
3161ea4c161SAndrey Moiseev return -ENOMEM;
3171ea4c161SAndrey Moiseev }
3181ea4c161SAndrey Moiseev
3191ea4c161SAndrey Moiseev slidebar_platform_dev->dev.groups = ideapad_attr_groups;
3201ea4c161SAndrey Moiseev
3211ea4c161SAndrey Moiseev err = platform_device_add(slidebar_platform_dev);
3221ea4c161SAndrey Moiseev if (err) {
3231ea4c161SAndrey Moiseev pr_err("Failed to register platform device\n");
3241ea4c161SAndrey Moiseev goto err_free_dev;
3251ea4c161SAndrey Moiseev }
3261ea4c161SAndrey Moiseev
3271ea4c161SAndrey Moiseev err = platform_driver_probe(&slidebar_drv, ideapad_probe);
3281ea4c161SAndrey Moiseev if (err) {
3291ea4c161SAndrey Moiseev pr_err("Failed to register platform driver\n");
3301ea4c161SAndrey Moiseev goto err_delete_dev;
3311ea4c161SAndrey Moiseev }
3321ea4c161SAndrey Moiseev
3331ea4c161SAndrey Moiseev return 0;
3341ea4c161SAndrey Moiseev
3351ea4c161SAndrey Moiseev err_delete_dev:
3361ea4c161SAndrey Moiseev platform_device_del(slidebar_platform_dev);
3371ea4c161SAndrey Moiseev err_free_dev:
3381ea4c161SAndrey Moiseev platform_device_put(slidebar_platform_dev);
3391ea4c161SAndrey Moiseev return err;
3401ea4c161SAndrey Moiseev }
3411ea4c161SAndrey Moiseev
slidebar_exit(void)3421ea4c161SAndrey Moiseev static void __exit slidebar_exit(void)
3431ea4c161SAndrey Moiseev {
3441ea4c161SAndrey Moiseev platform_device_unregister(slidebar_platform_dev);
3451ea4c161SAndrey Moiseev platform_driver_unregister(&slidebar_drv);
3461ea4c161SAndrey Moiseev }
3471ea4c161SAndrey Moiseev
3481ea4c161SAndrey Moiseev module_init(slidebar_init);
3491ea4c161SAndrey Moiseev module_exit(slidebar_exit);
3501ea4c161SAndrey Moiseev
3511ea4c161SAndrey Moiseev MODULE_AUTHOR("Andrey Moiseev <o2g.org.ru@gmail.com>");
3521ea4c161SAndrey Moiseev MODULE_DESCRIPTION("Slidebar input support for some Lenovo IdeaPad laptops");
3531ea4c161SAndrey Moiseev MODULE_LICENSE("GPL");
354