198ee3771SChris Diamand /* 298ee3771SChris Diamand * BYD TouchPad PS/2 mouse driver 398ee3771SChris Diamand * 498ee3771SChris Diamand * Copyright (C) 2015 Chris Diamand <chris@diamand.org> 598ee3771SChris Diamand * 698ee3771SChris Diamand * This program is free software; you can redistribute it and/or modify it 798ee3771SChris Diamand * under the terms of the GNU General Public License version 2 as published by 898ee3771SChris Diamand * the Free Software Foundation. 998ee3771SChris Diamand */ 1098ee3771SChris Diamand 1198ee3771SChris Diamand #include <linux/delay.h> 1298ee3771SChris Diamand #include <linux/input.h> 1398ee3771SChris Diamand #include <linux/libps2.h> 1498ee3771SChris Diamand #include <linux/serio.h> 152d5f5611SRichard Pospesel #include <linux/slab.h> 1698ee3771SChris Diamand 1798ee3771SChris Diamand #include "psmouse.h" 1898ee3771SChris Diamand #include "byd.h" 1998ee3771SChris Diamand 202d5f5611SRichard Pospesel /* PS2 Bits */ 2198ee3771SChris Diamand #define PS2_Y_OVERFLOW BIT_MASK(7) 2298ee3771SChris Diamand #define PS2_X_OVERFLOW BIT_MASK(6) 2398ee3771SChris Diamand #define PS2_Y_SIGN BIT_MASK(5) 2498ee3771SChris Diamand #define PS2_X_SIGN BIT_MASK(4) 2598ee3771SChris Diamand #define PS2_ALWAYS_1 BIT_MASK(3) 2698ee3771SChris Diamand #define PS2_MIDDLE BIT_MASK(2) 2798ee3771SChris Diamand #define PS2_RIGHT BIT_MASK(1) 2898ee3771SChris Diamand #define PS2_LEFT BIT_MASK(0) 2998ee3771SChris Diamand 3098ee3771SChris Diamand /* 312d5f5611SRichard Pospesel * BYD pad constants 3298ee3771SChris Diamand */ 3398ee3771SChris Diamand 342d5f5611SRichard Pospesel /* 352d5f5611SRichard Pospesel * True device resolution is unknown, however experiments show the 362d5f5611SRichard Pospesel * resolution is about 111 units/mm. 372d5f5611SRichard Pospesel * Absolute coordinate packets are in the range 0-255 for both X and Y 382d5f5611SRichard Pospesel * we pick ABS_X/ABS_Y dimensions which are multiples of 256 and in 392d5f5611SRichard Pospesel * the right ballpark given the touchpad's physical dimensions and estimate 402d5f5611SRichard Pospesel * resolution per spec sheet, device active area dimensions are 412d5f5611SRichard Pospesel * 101.6 x 60.1 mm. 422d5f5611SRichard Pospesel */ 432d5f5611SRichard Pospesel #define BYD_PAD_WIDTH 11264 442d5f5611SRichard Pospesel #define BYD_PAD_HEIGHT 6656 452d5f5611SRichard Pospesel #define BYD_PAD_RESOLUTION 111 4698ee3771SChris Diamand 472d5f5611SRichard Pospesel /* 482d5f5611SRichard Pospesel * Given the above dimensions, relative packets velocity is in multiples of 492d5f5611SRichard Pospesel * 1 unit / 11 milliseconds. We use this dt to estimate distance traveled 502d5f5611SRichard Pospesel */ 512d5f5611SRichard Pospesel #define BYD_DT 11 522d5f5611SRichard Pospesel /* Time in jiffies used to timeout various touch events (64 ms) */ 532d5f5611SRichard Pospesel #define BYD_TOUCH_TIMEOUT msecs_to_jiffies(64) 542d5f5611SRichard Pospesel 552d5f5611SRichard Pospesel /* BYD commands reverse engineered from windows driver */ 562d5f5611SRichard Pospesel 572d5f5611SRichard Pospesel /* 582d5f5611SRichard Pospesel * Swipe gesture from off-pad to on-pad 592d5f5611SRichard Pospesel * 0 : disable 602d5f5611SRichard Pospesel * 1 : enable 612d5f5611SRichard Pospesel */ 622d5f5611SRichard Pospesel #define BYD_CMD_SET_OFFSCREEN_SWIPE 0x10cc 632d5f5611SRichard Pospesel /* 642d5f5611SRichard Pospesel * Tap and drag delay time 652d5f5611SRichard Pospesel * 0 : disable 662d5f5611SRichard Pospesel * 1 - 8 : least to most delay 672d5f5611SRichard Pospesel */ 682d5f5611SRichard Pospesel #define BYD_CMD_SET_TAP_DRAG_DELAY_TIME 0x10cf 692d5f5611SRichard Pospesel /* 702d5f5611SRichard Pospesel * Physical buttons function mapping 712d5f5611SRichard Pospesel * 0 : enable 722d5f5611SRichard Pospesel * 4 : normal 732d5f5611SRichard Pospesel * 5 : left button custom command 742d5f5611SRichard Pospesel * 6 : right button custom command 752d5f5611SRichard Pospesel * 8 : disable 762d5f5611SRichard Pospesel */ 772d5f5611SRichard Pospesel #define BYD_CMD_SET_PHYSICAL_BUTTONS 0x10d0 782d5f5611SRichard Pospesel /* 792d5f5611SRichard Pospesel * Absolute mode (1 byte X/Y resolution) 802d5f5611SRichard Pospesel * 0 : disable 812d5f5611SRichard Pospesel * 2 : enable 822d5f5611SRichard Pospesel */ 832d5f5611SRichard Pospesel #define BYD_CMD_SET_ABSOLUTE_MODE 0x10d1 842d5f5611SRichard Pospesel /* 852d5f5611SRichard Pospesel * Two finger scrolling 862d5f5611SRichard Pospesel * 1 : vertical 872d5f5611SRichard Pospesel * 2 : horizontal 882d5f5611SRichard Pospesel * 3 : vertical + horizontal 892d5f5611SRichard Pospesel * 4 : disable 902d5f5611SRichard Pospesel */ 912d5f5611SRichard Pospesel #define BYD_CMD_SET_TWO_FINGER_SCROLL 0x10d2 922d5f5611SRichard Pospesel /* 932d5f5611SRichard Pospesel * Handedness 942d5f5611SRichard Pospesel * 1 : right handed 952d5f5611SRichard Pospesel * 2 : left handed 962d5f5611SRichard Pospesel */ 972d5f5611SRichard Pospesel #define BYD_CMD_SET_HANDEDNESS 0x10d3 982d5f5611SRichard Pospesel /* 992d5f5611SRichard Pospesel * Tap to click 1002d5f5611SRichard Pospesel * 1 : enable 1012d5f5611SRichard Pospesel * 2 : disable 1022d5f5611SRichard Pospesel */ 1032d5f5611SRichard Pospesel #define BYD_CMD_SET_TAP 0x10d4 1042d5f5611SRichard Pospesel /* 1052d5f5611SRichard Pospesel * Tap and drag 1062d5f5611SRichard Pospesel * 1 : tap and hold to drag 1072d5f5611SRichard Pospesel * 2 : tap and hold to drag + lock 1082d5f5611SRichard Pospesel * 3 : disable 1092d5f5611SRichard Pospesel */ 1102d5f5611SRichard Pospesel #define BYD_CMD_SET_TAP_DRAG 0x10d5 1112d5f5611SRichard Pospesel /* 1122d5f5611SRichard Pospesel * Touch sensitivity 1132d5f5611SRichard Pospesel * 1 - 7 : least to most sensitive 1142d5f5611SRichard Pospesel */ 1152d5f5611SRichard Pospesel #define BYD_CMD_SET_TOUCH_SENSITIVITY 0x10d6 1162d5f5611SRichard Pospesel /* 1172d5f5611SRichard Pospesel * One finger scrolling 1182d5f5611SRichard Pospesel * 1 : vertical 1192d5f5611SRichard Pospesel * 2 : horizontal 1202d5f5611SRichard Pospesel * 3 : vertical + horizontal 1212d5f5611SRichard Pospesel * 4 : disable 1222d5f5611SRichard Pospesel */ 1232d5f5611SRichard Pospesel #define BYD_CMD_SET_ONE_FINGER_SCROLL 0x10d7 1242d5f5611SRichard Pospesel /* 1252d5f5611SRichard Pospesel * One finger scrolling function 1262d5f5611SRichard Pospesel * 1 : free scrolling 1272d5f5611SRichard Pospesel * 2 : edge motion 1282d5f5611SRichard Pospesel * 3 : free scrolling + edge motion 1292d5f5611SRichard Pospesel * 4 : disable 1302d5f5611SRichard Pospesel */ 1312d5f5611SRichard Pospesel #define BYD_CMD_SET_ONE_FINGER_SCROLL_FUNC 0x10d8 1322d5f5611SRichard Pospesel /* 1332d5f5611SRichard Pospesel * Sliding speed 1342d5f5611SRichard Pospesel * 1 - 5 : slowest to fastest 1352d5f5611SRichard Pospesel */ 1362d5f5611SRichard Pospesel #define BYD_CMD_SET_SLIDING_SPEED 0x10da 1372d5f5611SRichard Pospesel /* 1382d5f5611SRichard Pospesel * Edge motion 1392d5f5611SRichard Pospesel * 1 : disable 1402d5f5611SRichard Pospesel * 2 : enable when dragging 1412d5f5611SRichard Pospesel * 3 : enable when dragging and pointing 1422d5f5611SRichard Pospesel */ 1432d5f5611SRichard Pospesel #define BYD_CMD_SET_EDGE_MOTION 0x10db 1442d5f5611SRichard Pospesel /* 1452d5f5611SRichard Pospesel * Left edge region size 1462d5f5611SRichard Pospesel * 0 - 7 : smallest to largest width 1472d5f5611SRichard Pospesel */ 1482d5f5611SRichard Pospesel #define BYD_CMD_SET_LEFT_EDGE_REGION 0x10dc 1492d5f5611SRichard Pospesel /* 1502d5f5611SRichard Pospesel * Top edge region size 1512d5f5611SRichard Pospesel * 0 - 9 : smallest to largest height 1522d5f5611SRichard Pospesel */ 1532d5f5611SRichard Pospesel #define BYD_CMD_SET_TOP_EDGE_REGION 0x10dd 1542d5f5611SRichard Pospesel /* 1552d5f5611SRichard Pospesel * Disregard palm press as clicks 1562d5f5611SRichard Pospesel * 1 - 6 : smallest to largest 1572d5f5611SRichard Pospesel */ 1582d5f5611SRichard Pospesel #define BYD_CMD_SET_PALM_CHECK 0x10de 1592d5f5611SRichard Pospesel /* 1602d5f5611SRichard Pospesel * Right edge region size 1612d5f5611SRichard Pospesel * 0 - 7 : smallest to largest width 1622d5f5611SRichard Pospesel */ 1632d5f5611SRichard Pospesel #define BYD_CMD_SET_RIGHT_EDGE_REGION 0x10df 1642d5f5611SRichard Pospesel /* 1652d5f5611SRichard Pospesel * Bottom edge region size 1662d5f5611SRichard Pospesel * 0 - 9 : smallest to largest height 1672d5f5611SRichard Pospesel */ 1682d5f5611SRichard Pospesel #define BYD_CMD_SET_BOTTOM_EDGE_REGION 0x10e1 1692d5f5611SRichard Pospesel /* 1702d5f5611SRichard Pospesel * Multitouch gestures 1712d5f5611SRichard Pospesel * 1 : enable 1722d5f5611SRichard Pospesel * 2 : disable 1732d5f5611SRichard Pospesel */ 1742d5f5611SRichard Pospesel #define BYD_CMD_SET_MULTITOUCH 0x10e3 1752d5f5611SRichard Pospesel /* 1762d5f5611SRichard Pospesel * Edge motion speed 1772d5f5611SRichard Pospesel * 0 : control with finger pressure 1782d5f5611SRichard Pospesel * 1 - 9 : slowest to fastest 1792d5f5611SRichard Pospesel */ 1802d5f5611SRichard Pospesel #define BYD_CMD_SET_EDGE_MOTION_SPEED 0x10e4 1812d5f5611SRichard Pospesel /* 1822d5f5611SRichard Pospesel * Two finger scolling function 1832d5f5611SRichard Pospesel * 0 : free scrolling 1842d5f5611SRichard Pospesel * 1 : free scrolling (with momentum) 1852d5f5611SRichard Pospesel * 2 : edge motion 1862d5f5611SRichard Pospesel * 3 : free scrolling (with momentum) + edge motion 1872d5f5611SRichard Pospesel * 4 : disable 1882d5f5611SRichard Pospesel */ 1892d5f5611SRichard Pospesel #define BYD_CMD_SET_TWO_FINGER_SCROLL_FUNC 0x10e5 1902d5f5611SRichard Pospesel 1912d5f5611SRichard Pospesel /* 1922d5f5611SRichard Pospesel * The touchpad generates a mixture of absolute and relative packets, indicated 1932d5f5611SRichard Pospesel * by the the last byte of each packet being set to one of the following: 1942d5f5611SRichard Pospesel */ 1952d5f5611SRichard Pospesel #define BYD_PACKET_ABSOLUTE 0xf8 1962d5f5611SRichard Pospesel #define BYD_PACKET_RELATIVE 0x00 1972d5f5611SRichard Pospesel /* Multitouch gesture packets */ 1982d5f5611SRichard Pospesel #define BYD_PACKET_PINCH_IN 0xd8 1992d5f5611SRichard Pospesel #define BYD_PACKET_PINCH_OUT 0x28 2002d5f5611SRichard Pospesel #define BYD_PACKET_ROTATE_CLOCKWISE 0x29 2012d5f5611SRichard Pospesel #define BYD_PACKET_ROTATE_ANTICLOCKWISE 0xd7 2022d5f5611SRichard Pospesel #define BYD_PACKET_TWO_FINGER_SCROLL_RIGHT 0x2a 2032d5f5611SRichard Pospesel #define BYD_PACKET_TWO_FINGER_SCROLL_DOWN 0x2b 2042d5f5611SRichard Pospesel #define BYD_PACKET_TWO_FINGER_SCROLL_UP 0xd5 2052d5f5611SRichard Pospesel #define BYD_PACKET_TWO_FINGER_SCROLL_LEFT 0xd6 2062d5f5611SRichard Pospesel #define BYD_PACKET_THREE_FINGER_SWIPE_RIGHT 0x2c 2072d5f5611SRichard Pospesel #define BYD_PACKET_THREE_FINGER_SWIPE_DOWN 0x2d 2082d5f5611SRichard Pospesel #define BYD_PACKET_THREE_FINGER_SWIPE_UP 0xd3 2092d5f5611SRichard Pospesel #define BYD_PACKET_THREE_FINGER_SWIPE_LEFT 0xd4 2102d5f5611SRichard Pospesel #define BYD_PACKET_FOUR_FINGER_DOWN 0x33 2112d5f5611SRichard Pospesel #define BYD_PACKET_FOUR_FINGER_UP 0xcd 2122d5f5611SRichard Pospesel #define BYD_PACKET_REGION_SCROLL_RIGHT 0x35 2132d5f5611SRichard Pospesel #define BYD_PACKET_REGION_SCROLL_DOWN 0x36 2142d5f5611SRichard Pospesel #define BYD_PACKET_REGION_SCROLL_UP 0xca 2152d5f5611SRichard Pospesel #define BYD_PACKET_REGION_SCROLL_LEFT 0xcb 2162d5f5611SRichard Pospesel #define BYD_PACKET_RIGHT_CORNER_CLICK 0xd2 2172d5f5611SRichard Pospesel #define BYD_PACKET_LEFT_CORNER_CLICK 0x2e 2182d5f5611SRichard Pospesel #define BYD_PACKET_LEFT_AND_RIGHT_CORNER_CLICK 0x2f 2192d5f5611SRichard Pospesel #define BYD_PACKET_ONTO_PAD_SWIPE_RIGHT 0x37 2202d5f5611SRichard Pospesel #define BYD_PACKET_ONTO_PAD_SWIPE_DOWN 0x30 2212d5f5611SRichard Pospesel #define BYD_PACKET_ONTO_PAD_SWIPE_UP 0xd0 2222d5f5611SRichard Pospesel #define BYD_PACKET_ONTO_PAD_SWIPE_LEFT 0xc9 2232d5f5611SRichard Pospesel 2242d5f5611SRichard Pospesel struct byd_data { 2252d5f5611SRichard Pospesel struct timer_list timer; 2262d5f5611SRichard Pospesel s32 abs_x; 2272d5f5611SRichard Pospesel s32 abs_y; 2282d5f5611SRichard Pospesel typeof(jiffies) last_touch_time; 2292d5f5611SRichard Pospesel bool btn_left; 2302d5f5611SRichard Pospesel bool btn_right; 2312d5f5611SRichard Pospesel bool touch; 2322d5f5611SRichard Pospesel }; 2332d5f5611SRichard Pospesel 2342d5f5611SRichard Pospesel static void byd_report_input(struct psmouse *psmouse) 23598ee3771SChris Diamand { 2362d5f5611SRichard Pospesel struct byd_data *priv = psmouse->private; 2372d5f5611SRichard Pospesel struct input_dev *dev = psmouse->dev; 23898ee3771SChris Diamand 2392d5f5611SRichard Pospesel input_report_key(dev, BTN_TOUCH, priv->touch); 2402d5f5611SRichard Pospesel input_report_key(dev, BTN_TOOL_FINGER, priv->touch); 24198ee3771SChris Diamand 2422d5f5611SRichard Pospesel input_report_abs(dev, ABS_X, priv->abs_x); 2432d5f5611SRichard Pospesel input_report_abs(dev, ABS_Y, priv->abs_y); 2442d5f5611SRichard Pospesel input_report_key(dev, BTN_LEFT, priv->btn_left); 2452d5f5611SRichard Pospesel input_report_key(dev, BTN_RIGHT, priv->btn_right); 24698ee3771SChris Diamand 2472d5f5611SRichard Pospesel input_sync(dev); 24898ee3771SChris Diamand } 24998ee3771SChris Diamand 2502d5f5611SRichard Pospesel static void byd_clear_touch(unsigned long data) 2512d5f5611SRichard Pospesel { 2522d5f5611SRichard Pospesel struct psmouse *psmouse = (struct psmouse *)data; 2532d5f5611SRichard Pospesel struct byd_data *priv = psmouse->private; 2542d5f5611SRichard Pospesel 2552d5f5611SRichard Pospesel serio_pause_rx(psmouse->ps2dev.serio); 2562d5f5611SRichard Pospesel priv->touch = false; 2572d5f5611SRichard Pospesel 2582d5f5611SRichard Pospesel byd_report_input(psmouse); 2592d5f5611SRichard Pospesel 2602d5f5611SRichard Pospesel serio_continue_rx(psmouse->ps2dev.serio); 2612d5f5611SRichard Pospesel 2622d5f5611SRichard Pospesel /* 2632d5f5611SRichard Pospesel * Move cursor back to center of pad when we lose touch - this 2642d5f5611SRichard Pospesel * specifically improves user experience when moving cursor with one 2652d5f5611SRichard Pospesel * finger, and pressing a button with another. 2662d5f5611SRichard Pospesel */ 2672d5f5611SRichard Pospesel priv->abs_x = BYD_PAD_WIDTH / 2; 2682d5f5611SRichard Pospesel priv->abs_y = BYD_PAD_HEIGHT / 2; 26998ee3771SChris Diamand } 27098ee3771SChris Diamand 27198ee3771SChris Diamand static psmouse_ret_t byd_process_byte(struct psmouse *psmouse) 27298ee3771SChris Diamand { 2732d5f5611SRichard Pospesel struct byd_data *priv = psmouse->private; 27498ee3771SChris Diamand u8 *pkt = psmouse->packet; 27598ee3771SChris Diamand 27698ee3771SChris Diamand if (psmouse->pktcnt > 0 && !(pkt[0] & PS2_ALWAYS_1)) { 27798ee3771SChris Diamand psmouse_warn(psmouse, "Always_1 bit not 1. pkt[0] = %02x\n", 27898ee3771SChris Diamand pkt[0]); 27998ee3771SChris Diamand return PSMOUSE_BAD_DATA; 28098ee3771SChris Diamand } 28198ee3771SChris Diamand 28298ee3771SChris Diamand if (psmouse->pktcnt < psmouse->pktsize) 28398ee3771SChris Diamand return PSMOUSE_GOOD_DATA; 28498ee3771SChris Diamand 28598ee3771SChris Diamand /* Otherwise, a full packet has been received */ 28698ee3771SChris Diamand switch (pkt[3]) { 2872d5f5611SRichard Pospesel case BYD_PACKET_ABSOLUTE: 2882d5f5611SRichard Pospesel /* Only use absolute packets for the start of movement. */ 2892d5f5611SRichard Pospesel if (!priv->touch) { 2902d5f5611SRichard Pospesel /* needed to detect tap */ 2912d5f5611SRichard Pospesel typeof(jiffies) tap_time = 2922d5f5611SRichard Pospesel priv->last_touch_time + BYD_TOUCH_TIMEOUT; 2932d5f5611SRichard Pospesel priv->touch = time_after(jiffies, tap_time); 2942d5f5611SRichard Pospesel 2952d5f5611SRichard Pospesel /* init abs position */ 2962d5f5611SRichard Pospesel priv->abs_x = pkt[1] * (BYD_PAD_WIDTH / 256); 2972d5f5611SRichard Pospesel priv->abs_y = (255 - pkt[2]) * (BYD_PAD_HEIGHT / 256); 2982d5f5611SRichard Pospesel } 2992d5f5611SRichard Pospesel break; 3002d5f5611SRichard Pospesel case BYD_PACKET_RELATIVE: { 30198ee3771SChris Diamand /* Standard packet */ 30298ee3771SChris Diamand /* Sign-extend if a sign bit is set. */ 3032d5f5611SRichard Pospesel u32 signx = pkt[0] & PS2_X_SIGN ? ~0xFF : 0; 3042d5f5611SRichard Pospesel u32 signy = pkt[0] & PS2_Y_SIGN ? ~0xFF : 0; 3052d5f5611SRichard Pospesel s32 dx = signx | (int) pkt[1]; 3062d5f5611SRichard Pospesel s32 dy = signy | (int) pkt[2]; 30798ee3771SChris Diamand 3082d5f5611SRichard Pospesel /* Update position based on velocity */ 3092d5f5611SRichard Pospesel priv->abs_x += dx * BYD_DT; 3102d5f5611SRichard Pospesel priv->abs_y -= dy * BYD_DT; 31198ee3771SChris Diamand 3122d5f5611SRichard Pospesel priv->touch = true; 31398ee3771SChris Diamand break; 31498ee3771SChris Diamand } 31598ee3771SChris Diamand default: 31698ee3771SChris Diamand psmouse_warn(psmouse, 31798ee3771SChris Diamand "Unrecognized Z: pkt = %02x %02x %02x %02x\n", 31898ee3771SChris Diamand psmouse->packet[0], psmouse->packet[1], 31998ee3771SChris Diamand psmouse->packet[2], psmouse->packet[3]); 32098ee3771SChris Diamand return PSMOUSE_BAD_DATA; 32198ee3771SChris Diamand } 32298ee3771SChris Diamand 3232d5f5611SRichard Pospesel priv->btn_left = pkt[0] & PS2_LEFT; 3242d5f5611SRichard Pospesel priv->btn_right = pkt[0] & PS2_RIGHT; 3252d5f5611SRichard Pospesel 3262d5f5611SRichard Pospesel byd_report_input(psmouse); 3272d5f5611SRichard Pospesel 3282d5f5611SRichard Pospesel /* Reset time since last touch. */ 3292d5f5611SRichard Pospesel if (priv->touch) { 3302d5f5611SRichard Pospesel priv->last_touch_time = jiffies; 3312d5f5611SRichard Pospesel mod_timer(&priv->timer, jiffies + BYD_TOUCH_TIMEOUT); 3322d5f5611SRichard Pospesel } 33398ee3771SChris Diamand 33498ee3771SChris Diamand return PSMOUSE_FULL_PACKET; 33598ee3771SChris Diamand } 33698ee3771SChris Diamand 3372d5f5611SRichard Pospesel static int byd_reset_touchpad(struct psmouse *psmouse) 33898ee3771SChris Diamand { 33998ee3771SChris Diamand struct ps2dev *ps2dev = &psmouse->ps2dev; 34098ee3771SChris Diamand u8 param[4]; 3412d5f5611SRichard Pospesel size_t i; 3422d5f5611SRichard Pospesel 34398ee3771SChris Diamand const struct { 34498ee3771SChris Diamand u16 command; 34598ee3771SChris Diamand u8 arg; 34698ee3771SChris Diamand } seq[] = { 3472d5f5611SRichard Pospesel /* 3482d5f5611SRichard Pospesel * Intellimouse initialization sequence, to get 4-byte instead 3492d5f5611SRichard Pospesel * of 3-byte packets. 3502d5f5611SRichard Pospesel */ 35198ee3771SChris Diamand { PSMOUSE_CMD_SETRATE, 0xC8 }, 35298ee3771SChris Diamand { PSMOUSE_CMD_SETRATE, 0x64 }, 35398ee3771SChris Diamand { PSMOUSE_CMD_SETRATE, 0x50 }, 35498ee3771SChris Diamand { PSMOUSE_CMD_GETID, 0 }, 3552d5f5611SRichard Pospesel { PSMOUSE_CMD_ENABLE, 0 }, 3562d5f5611SRichard Pospesel /* 3572d5f5611SRichard Pospesel * BYD-specific initialization, which enables absolute mode and 3582d5f5611SRichard Pospesel * (if desired), the touchpad's built-in gesture detection. 3592d5f5611SRichard Pospesel */ 3602d5f5611SRichard Pospesel { 0x10E2, 0x00 }, 3612d5f5611SRichard Pospesel { 0x10E0, 0x02 }, 3622d5f5611SRichard Pospesel /* The touchpad should reply with 4 seemingly-random bytes */ 3632d5f5611SRichard Pospesel { 0x14E0, 0x01 }, 3642d5f5611SRichard Pospesel /* Pairs of parameters and values. */ 3652d5f5611SRichard Pospesel { BYD_CMD_SET_HANDEDNESS, 0x01 }, 3662d5f5611SRichard Pospesel { BYD_CMD_SET_PHYSICAL_BUTTONS, 0x04 }, 3672d5f5611SRichard Pospesel { BYD_CMD_SET_TAP, 0x02 }, 3682d5f5611SRichard Pospesel { BYD_CMD_SET_ONE_FINGER_SCROLL, 0x04 }, 3692d5f5611SRichard Pospesel { BYD_CMD_SET_ONE_FINGER_SCROLL_FUNC, 0x04 }, 3702d5f5611SRichard Pospesel { BYD_CMD_SET_EDGE_MOTION, 0x01 }, 3712d5f5611SRichard Pospesel { BYD_CMD_SET_PALM_CHECK, 0x00 }, 3722d5f5611SRichard Pospesel { BYD_CMD_SET_MULTITOUCH, 0x02 }, 3732d5f5611SRichard Pospesel { BYD_CMD_SET_TWO_FINGER_SCROLL, 0x04 }, 3742d5f5611SRichard Pospesel { BYD_CMD_SET_TWO_FINGER_SCROLL_FUNC, 0x04 }, 3752d5f5611SRichard Pospesel { BYD_CMD_SET_LEFT_EDGE_REGION, 0x00 }, 3762d5f5611SRichard Pospesel { BYD_CMD_SET_TOP_EDGE_REGION, 0x00 }, 3772d5f5611SRichard Pospesel { BYD_CMD_SET_RIGHT_EDGE_REGION, 0x00 }, 3782d5f5611SRichard Pospesel { BYD_CMD_SET_BOTTOM_EDGE_REGION, 0x00 }, 3792d5f5611SRichard Pospesel { BYD_CMD_SET_ABSOLUTE_MODE, 0x02 }, 3802d5f5611SRichard Pospesel /* Finalize initialization. */ 3812d5f5611SRichard Pospesel { 0x10E0, 0x00 }, 3822d5f5611SRichard Pospesel { 0x10E2, 0x01 }, 38398ee3771SChris Diamand }; 38498ee3771SChris Diamand 38598ee3771SChris Diamand for (i = 0; i < ARRAY_SIZE(seq); ++i) { 3862d5f5611SRichard Pospesel memset(param, 0, sizeof(param)); 38798ee3771SChris Diamand param[0] = seq[i].arg; 38898ee3771SChris Diamand if (ps2_command(ps2dev, param, seq[i].command)) 3892d5f5611SRichard Pospesel return -EIO; 39098ee3771SChris Diamand } 39198ee3771SChris Diamand 3922d5f5611SRichard Pospesel psmouse_set_state(psmouse, PSMOUSE_ACTIVATED); 39398ee3771SChris Diamand return 0; 39498ee3771SChris Diamand } 39598ee3771SChris Diamand 39698ee3771SChris Diamand static int byd_reconnect(struct psmouse *psmouse) 39798ee3771SChris Diamand { 39898ee3771SChris Diamand int retry = 0, error = 0; 39998ee3771SChris Diamand 40098ee3771SChris Diamand psmouse_dbg(psmouse, "Reconnect\n"); 40198ee3771SChris Diamand do { 40298ee3771SChris Diamand psmouse_reset(psmouse); 40398ee3771SChris Diamand if (retry) 40498ee3771SChris Diamand ssleep(1); 40598ee3771SChris Diamand error = byd_detect(psmouse, 0); 40698ee3771SChris Diamand } while (error && ++retry < 3); 40798ee3771SChris Diamand 40898ee3771SChris Diamand if (error) 40998ee3771SChris Diamand return error; 41098ee3771SChris Diamand 41198ee3771SChris Diamand psmouse_dbg(psmouse, "Reconnected after %d attempts\n", retry); 41298ee3771SChris Diamand 41398ee3771SChris Diamand error = byd_reset_touchpad(psmouse); 41498ee3771SChris Diamand if (error) { 41598ee3771SChris Diamand psmouse_err(psmouse, "Unable to initialize device\n"); 41698ee3771SChris Diamand return error; 41798ee3771SChris Diamand } 41898ee3771SChris Diamand 41998ee3771SChris Diamand return 0; 42098ee3771SChris Diamand } 42198ee3771SChris Diamand 4222d5f5611SRichard Pospesel static void byd_disconnect(struct psmouse *psmouse) 4232d5f5611SRichard Pospesel { 4242d5f5611SRichard Pospesel struct byd_data *priv = psmouse->private; 4252d5f5611SRichard Pospesel 4262d5f5611SRichard Pospesel if (priv) { 4272d5f5611SRichard Pospesel del_timer(&priv->timer); 4282d5f5611SRichard Pospesel kfree(psmouse->private); 4292d5f5611SRichard Pospesel psmouse->private = NULL; 4302d5f5611SRichard Pospesel } 4312d5f5611SRichard Pospesel } 4322d5f5611SRichard Pospesel 4332d5f5611SRichard Pospesel int byd_detect(struct psmouse *psmouse, bool set_properties) 4342d5f5611SRichard Pospesel { 4352d5f5611SRichard Pospesel struct ps2dev *ps2dev = &psmouse->ps2dev; 4362d5f5611SRichard Pospesel u8 param[4] = {0x03, 0x00, 0x00, 0x00}; 4372d5f5611SRichard Pospesel 4382d5f5611SRichard Pospesel if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES)) 4392d5f5611SRichard Pospesel return -1; 4402d5f5611SRichard Pospesel if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES)) 4412d5f5611SRichard Pospesel return -1; 4422d5f5611SRichard Pospesel if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES)) 4432d5f5611SRichard Pospesel return -1; 4442d5f5611SRichard Pospesel if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES)) 4452d5f5611SRichard Pospesel return -1; 4462d5f5611SRichard Pospesel if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) 4472d5f5611SRichard Pospesel return -1; 4482d5f5611SRichard Pospesel 4492d5f5611SRichard Pospesel if (param[1] != 0x03 || param[2] != 0x64) 4502d5f5611SRichard Pospesel return -ENODEV; 4512d5f5611SRichard Pospesel 4522d5f5611SRichard Pospesel psmouse_dbg(psmouse, "BYD touchpad detected\n"); 4532d5f5611SRichard Pospesel 4542d5f5611SRichard Pospesel if (set_properties) { 4552d5f5611SRichard Pospesel psmouse->vendor = "BYD"; 4562d5f5611SRichard Pospesel psmouse->name = "TouchPad"; 4572d5f5611SRichard Pospesel } 4582d5f5611SRichard Pospesel 4592d5f5611SRichard Pospesel return 0; 4602d5f5611SRichard Pospesel } 4612d5f5611SRichard Pospesel 46298ee3771SChris Diamand int byd_init(struct psmouse *psmouse) 46398ee3771SChris Diamand { 46498ee3771SChris Diamand struct input_dev *dev = psmouse->dev; 4652d5f5611SRichard Pospesel struct byd_data *priv; 46698ee3771SChris Diamand 46798ee3771SChris Diamand if (psmouse_reset(psmouse)) 46898ee3771SChris Diamand return -EIO; 46998ee3771SChris Diamand 47098ee3771SChris Diamand if (byd_reset_touchpad(psmouse)) 47198ee3771SChris Diamand return -EIO; 47298ee3771SChris Diamand 4732d5f5611SRichard Pospesel priv = kzalloc(sizeof(*priv), GFP_KERNEL); 4742d5f5611SRichard Pospesel if (!priv) 4752d5f5611SRichard Pospesel return -ENOMEM; 4762d5f5611SRichard Pospesel 4772d5f5611SRichard Pospesel memset(priv, 0, sizeof(*priv)); 4782d5f5611SRichard Pospesel setup_timer(&priv->timer, byd_clear_touch, (unsigned long) psmouse); 4792d5f5611SRichard Pospesel 4802d5f5611SRichard Pospesel psmouse->private = priv; 4812d5f5611SRichard Pospesel psmouse->disconnect = byd_disconnect; 48298ee3771SChris Diamand psmouse->reconnect = byd_reconnect; 48398ee3771SChris Diamand psmouse->protocol_handler = byd_process_byte; 48498ee3771SChris Diamand psmouse->pktsize = 4; 48598ee3771SChris Diamand psmouse->resync_time = 0; 48698ee3771SChris Diamand 4872d5f5611SRichard Pospesel __set_bit(INPUT_PROP_POINTER, dev->propbit); 4882d5f5611SRichard Pospesel /* Touchpad */ 4892d5f5611SRichard Pospesel __set_bit(BTN_TOUCH, dev->keybit); 4902d5f5611SRichard Pospesel __set_bit(BTN_TOOL_FINGER, dev->keybit); 4912d5f5611SRichard Pospesel /* Buttons */ 4922d5f5611SRichard Pospesel __set_bit(BTN_LEFT, dev->keybit); 4932d5f5611SRichard Pospesel __set_bit(BTN_RIGHT, dev->keybit); 4942d5f5611SRichard Pospesel __clear_bit(BTN_MIDDLE, dev->keybit); 4952d5f5611SRichard Pospesel 4962d5f5611SRichard Pospesel /* Absolute position */ 4972d5f5611SRichard Pospesel __set_bit(EV_ABS, dev->evbit); 4982d5f5611SRichard Pospesel input_set_abs_params(dev, ABS_X, 0, BYD_PAD_WIDTH, 0, 0); 4992d5f5611SRichard Pospesel input_set_abs_params(dev, ABS_Y, 0, BYD_PAD_HEIGHT, 0, 0); 5002d5f5611SRichard Pospesel input_abs_set_res(dev, ABS_X, BYD_PAD_RESOLUTION); 5012d5f5611SRichard Pospesel input_abs_set_res(dev, ABS_Y, BYD_PAD_RESOLUTION); 5022d5f5611SRichard Pospesel /* No relative support */ 5032d5f5611SRichard Pospesel __clear_bit(EV_REL, dev->evbit); 5042d5f5611SRichard Pospesel __clear_bit(REL_X, dev->relbit); 5052d5f5611SRichard Pospesel __clear_bit(REL_Y, dev->relbit); 50698ee3771SChris Diamand 50798ee3771SChris Diamand return 0; 50898ee3771SChris Diamand } 509