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