yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ALPS touchpad PS/2 mouse driver |
| 3 | * |
| 4 | * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au> |
| 5 | * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com> |
| 6 | * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru> |
| 7 | * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz> |
| 8 | * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net> |
| 9 | * |
| 10 | * ALPS detection, tap switching and status querying info is taken from |
| 11 | * tpconfig utility (by C. Scott Ananian and Bruce Kall). |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify it |
| 14 | * under the terms of the GNU General Public License version 2 as published by |
| 15 | * the Free Software Foundation. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/input.h> |
| 20 | #include <linux/input/mt.h> |
| 21 | #include <linux/serio.h> |
| 22 | #include <linux/libps2.h> |
| 23 | |
| 24 | #include "psmouse.h" |
| 25 | #include "alps.h" |
| 26 | |
| 27 | /* |
| 28 | * Definitions for ALPS version 3 and 4 command mode protocol |
| 29 | */ |
| 30 | #define ALPS_V3_X_MAX 2000 |
| 31 | #define ALPS_V3_Y_MAX 1400 |
| 32 | |
| 33 | #define ALPS_BITMAP_X_BITS 15 |
| 34 | #define ALPS_BITMAP_Y_BITS 11 |
| 35 | |
| 36 | #define ALPS_CMD_NIBBLE_10 0x01f2 |
| 37 | |
| 38 | static const struct alps_nibble_commands alps_v3_nibble_commands[] = { |
| 39 | { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */ |
| 40 | { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */ |
| 41 | { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */ |
| 42 | { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */ |
| 43 | { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */ |
| 44 | { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */ |
| 45 | { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */ |
| 46 | { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */ |
| 47 | { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */ |
| 48 | { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */ |
| 49 | { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */ |
| 50 | { PSMOUSE_CMD_SETRES, 0x00 }, /* b */ |
| 51 | { PSMOUSE_CMD_SETRES, 0x01 }, /* c */ |
| 52 | { PSMOUSE_CMD_SETRES, 0x02 }, /* d */ |
| 53 | { PSMOUSE_CMD_SETRES, 0x03 }, /* e */ |
| 54 | { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */ |
| 55 | }; |
| 56 | |
| 57 | static const struct alps_nibble_commands alps_v4_nibble_commands[] = { |
| 58 | { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */ |
| 59 | { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */ |
| 60 | { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */ |
| 61 | { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */ |
| 62 | { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */ |
| 63 | { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */ |
| 64 | { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */ |
| 65 | { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */ |
| 66 | { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */ |
| 67 | { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */ |
| 68 | { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */ |
| 69 | { PSMOUSE_CMD_SETRES, 0x00 }, /* b */ |
| 70 | { PSMOUSE_CMD_SETRES, 0x01 }, /* c */ |
| 71 | { PSMOUSE_CMD_SETRES, 0x02 }, /* d */ |
| 72 | { PSMOUSE_CMD_SETRES, 0x03 }, /* e */ |
| 73 | { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */ |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */ |
| 78 | #define ALPS_PASS 0x04 /* device has a pass-through port */ |
| 79 | |
| 80 | #define ALPS_WHEEL 0x08 /* hardware wheel present */ |
| 81 | #define ALPS_FW_BK_1 0x10 /* front & back buttons present */ |
| 82 | #define ALPS_FW_BK_2 0x20 /* front & back buttons present */ |
| 83 | #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */ |
| 84 | #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with |
| 85 | 6-byte ALPS packet */ |
| 86 | |
| 87 | static const struct alps_model_info alps_model_data[] = { |
| 88 | { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */ |
| 89 | { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */ |
| 90 | { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, |
| 91 | { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, |
| 92 | { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */ |
| 93 | { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, |
| 94 | { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, |
| 95 | { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */ |
| 96 | { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */ |
| 97 | { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */ |
| 98 | { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, |
| 99 | { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */ |
| 100 | { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */ |
| 101 | { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, |
| 102 | { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */ |
| 103 | { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */ |
| 104 | { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, |
| 105 | { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */ |
| 106 | /* Dell Latitude E5500, E6400, E6500, Precision M4400 */ |
| 107 | { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, |
| 108 | ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, |
| 109 | { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */ |
| 110 | { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, |
| 111 | ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */ |
| 112 | { { 0x73, 0x02, 0x64 }, 0x9b, ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT }, |
| 113 | { { 0x73, 0x02, 0x64 }, 0x9d, ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT }, |
| 114 | { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 }, |
| 115 | }; |
| 116 | |
| 117 | /* |
| 118 | * XXX - this entry is suspicious. First byte has zero lower nibble, |
| 119 | * which is what a normal mouse would report. Also, the value 0x0e |
| 120 | * isn't valid per PS/2 spec. |
| 121 | */ |
| 122 | |
| 123 | /* Packet formats are described in Documentation/input/alps.txt */ |
| 124 | |
| 125 | static bool alps_is_valid_first_byte(const struct alps_model_info *model, |
| 126 | unsigned char data) |
| 127 | { |
| 128 | return (data & model->mask0) == model->byte0; |
| 129 | } |
| 130 | |
| 131 | static void alps_report_buttons(struct psmouse *psmouse, |
| 132 | struct input_dev *dev1, struct input_dev *dev2, |
| 133 | int left, int right, int middle) |
| 134 | { |
| 135 | struct input_dev *dev; |
| 136 | |
| 137 | /* |
| 138 | * If shared button has already been reported on the |
| 139 | * other device (dev2) then this event should be also |
| 140 | * sent through that device. |
| 141 | */ |
| 142 | dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1; |
| 143 | input_report_key(dev, BTN_LEFT, left); |
| 144 | |
| 145 | dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1; |
| 146 | input_report_key(dev, BTN_RIGHT, right); |
| 147 | |
| 148 | dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1; |
| 149 | input_report_key(dev, BTN_MIDDLE, middle); |
| 150 | |
| 151 | /* |
| 152 | * Sync the _other_ device now, we'll do the first |
| 153 | * device later once we report the rest of the events. |
| 154 | */ |
| 155 | input_sync(dev2); |
| 156 | } |
| 157 | |
| 158 | static void alps_process_packet_v1_v2(struct psmouse *psmouse) |
| 159 | { |
| 160 | struct alps_data *priv = psmouse->private; |
| 161 | const struct alps_model_info *model = priv->i; |
| 162 | unsigned char *packet = psmouse->packet; |
| 163 | struct input_dev *dev = psmouse->dev; |
| 164 | struct input_dev *dev2 = priv->dev2; |
| 165 | int x, y, z, ges, fin, left, right, middle; |
| 166 | int back = 0, forward = 0; |
| 167 | |
| 168 | if (model->proto_version == ALPS_PROTO_V1) { |
| 169 | left = packet[2] & 0x10; |
| 170 | right = packet[2] & 0x08; |
| 171 | middle = 0; |
| 172 | x = packet[1] | ((packet[0] & 0x07) << 7); |
| 173 | y = packet[4] | ((packet[3] & 0x07) << 7); |
| 174 | z = packet[5]; |
| 175 | } else { |
| 176 | left = packet[3] & 1; |
| 177 | right = packet[3] & 2; |
| 178 | middle = packet[3] & 4; |
| 179 | x = packet[1] | ((packet[2] & 0x78) << (7 - 3)); |
| 180 | y = packet[4] | ((packet[3] & 0x70) << (7 - 4)); |
| 181 | z = packet[5]; |
| 182 | } |
| 183 | |
| 184 | if (model->flags & ALPS_FW_BK_1) { |
| 185 | back = packet[0] & 0x10; |
| 186 | forward = packet[2] & 4; |
| 187 | } |
| 188 | |
| 189 | if (model->flags & ALPS_FW_BK_2) { |
| 190 | back = packet[3] & 4; |
| 191 | forward = packet[2] & 4; |
| 192 | if ((middle = forward && back)) |
| 193 | forward = back = 0; |
| 194 | } |
| 195 | |
| 196 | ges = packet[2] & 1; |
| 197 | fin = packet[2] & 2; |
| 198 | |
| 199 | if ((model->flags & ALPS_DUALPOINT) && z == 127) { |
| 200 | input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x)); |
| 201 | input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y)); |
| 202 | |
| 203 | alps_report_buttons(psmouse, dev2, dev, left, right, middle); |
| 204 | |
| 205 | input_sync(dev2); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | alps_report_buttons(psmouse, dev, dev2, left, right, middle); |
| 210 | |
| 211 | /* Convert hardware tap to a reasonable Z value */ |
| 212 | if (ges && !fin) |
| 213 | z = 40; |
| 214 | |
| 215 | /* |
| 216 | * A "tap and drag" operation is reported by the hardware as a transition |
| 217 | * from (!fin && ges) to (fin && ges). This should be translated to the |
| 218 | * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually. |
| 219 | */ |
| 220 | if (ges && fin && !priv->prev_fin) { |
| 221 | input_report_abs(dev, ABS_X, x); |
| 222 | input_report_abs(dev, ABS_Y, y); |
| 223 | input_report_abs(dev, ABS_PRESSURE, 0); |
| 224 | input_report_key(dev, BTN_TOOL_FINGER, 0); |
| 225 | input_sync(dev); |
| 226 | } |
| 227 | priv->prev_fin = fin; |
| 228 | |
| 229 | if (z > 30) |
| 230 | input_report_key(dev, BTN_TOUCH, 1); |
| 231 | if (z < 25) |
| 232 | input_report_key(dev, BTN_TOUCH, 0); |
| 233 | |
| 234 | if (z > 0) { |
| 235 | input_report_abs(dev, ABS_X, x); |
| 236 | input_report_abs(dev, ABS_Y, y); |
| 237 | } |
| 238 | |
| 239 | input_report_abs(dev, ABS_PRESSURE, z); |
| 240 | input_report_key(dev, BTN_TOOL_FINGER, z > 0); |
| 241 | |
| 242 | if (model->flags & ALPS_WHEEL) |
| 243 | input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07)); |
| 244 | |
| 245 | if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) { |
| 246 | input_report_key(dev, BTN_FORWARD, forward); |
| 247 | input_report_key(dev, BTN_BACK, back); |
| 248 | } |
| 249 | |
| 250 | if (model->flags & ALPS_FOUR_BUTTONS) { |
| 251 | input_report_key(dev, BTN_0, packet[2] & 4); |
| 252 | input_report_key(dev, BTN_1, packet[0] & 0x10); |
| 253 | input_report_key(dev, BTN_2, packet[3] & 4); |
| 254 | input_report_key(dev, BTN_3, packet[0] & 0x20); |
| 255 | } |
| 256 | |
| 257 | input_sync(dev); |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Process bitmap data from v3 and v4 protocols. Returns the number of |
| 262 | * fingers detected. A return value of 0 means at least one of the |
| 263 | * bitmaps was empty. |
| 264 | * |
| 265 | * The bitmaps don't have enough data to track fingers, so this function |
| 266 | * only generates points representing a bounding box of all contacts. |
| 267 | * These points are returned in x1, y1, x2, and y2 when the return value |
| 268 | * is greater than 0. |
| 269 | */ |
| 270 | static int alps_process_bitmap(unsigned int x_map, unsigned int y_map, |
| 271 | int *x1, int *y1, int *x2, int *y2) |
| 272 | { |
| 273 | struct alps_bitmap_point { |
| 274 | int start_bit; |
| 275 | int num_bits; |
| 276 | }; |
| 277 | |
| 278 | int fingers_x = 0, fingers_y = 0, fingers; |
| 279 | int i, bit, prev_bit; |
| 280 | struct alps_bitmap_point x_low = {0,}, x_high = {0,}; |
| 281 | struct alps_bitmap_point y_low = {0,}, y_high = {0,}; |
| 282 | struct alps_bitmap_point *point; |
| 283 | |
| 284 | if (!x_map || !y_map) |
| 285 | return 0; |
| 286 | |
| 287 | *x1 = *y1 = *x2 = *y2 = 0; |
| 288 | |
| 289 | prev_bit = 0; |
| 290 | point = &x_low; |
| 291 | for (i = 0; x_map != 0; i++, x_map >>= 1) { |
| 292 | bit = x_map & 1; |
| 293 | if (bit) { |
| 294 | if (!prev_bit) { |
| 295 | point->start_bit = i; |
| 296 | fingers_x++; |
| 297 | } |
| 298 | point->num_bits++; |
| 299 | } else { |
| 300 | if (prev_bit) |
| 301 | point = &x_high; |
| 302 | else |
| 303 | point->num_bits = 0; |
| 304 | } |
| 305 | prev_bit = bit; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * y bitmap is reversed for what we need (lower positions are in |
| 310 | * higher bits), so we process from the top end. |
| 311 | */ |
| 312 | y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - ALPS_BITMAP_Y_BITS); |
| 313 | prev_bit = 0; |
| 314 | point = &y_low; |
| 315 | for (i = 0; y_map != 0; i++, y_map <<= 1) { |
| 316 | bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1)); |
| 317 | if (bit) { |
| 318 | if (!prev_bit) { |
| 319 | point->start_bit = i; |
| 320 | fingers_y++; |
| 321 | } |
| 322 | point->num_bits++; |
| 323 | } else { |
| 324 | if (prev_bit) |
| 325 | point = &y_high; |
| 326 | else |
| 327 | point->num_bits = 0; |
| 328 | } |
| 329 | prev_bit = bit; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Fingers can overlap, so we use the maximum count of fingers |
| 334 | * on either axis as the finger count. |
| 335 | */ |
| 336 | fingers = max(fingers_x, fingers_y); |
| 337 | |
| 338 | /* |
| 339 | * If total fingers is > 1 but either axis reports only a single |
| 340 | * contact, we have overlapping or adjacent fingers. For the |
| 341 | * purposes of creating a bounding box, divide the single contact |
| 342 | * (roughly) equally between the two points. |
| 343 | */ |
| 344 | if (fingers > 1) { |
| 345 | if (fingers_x == 1) { |
| 346 | i = x_low.num_bits / 2; |
| 347 | x_low.num_bits = x_low.num_bits - i; |
| 348 | x_high.start_bit = x_low.start_bit + i; |
| 349 | x_high.num_bits = max(i, 1); |
| 350 | } else if (fingers_y == 1) { |
| 351 | i = y_low.num_bits / 2; |
| 352 | y_low.num_bits = y_low.num_bits - i; |
| 353 | y_high.start_bit = y_low.start_bit + i; |
| 354 | y_high.num_bits = max(i, 1); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | *x1 = (ALPS_V3_X_MAX * (2 * x_low.start_bit + x_low.num_bits - 1)) / |
| 359 | (2 * (ALPS_BITMAP_X_BITS - 1)); |
| 360 | *y1 = (ALPS_V3_Y_MAX * (2 * y_low.start_bit + y_low.num_bits - 1)) / |
| 361 | (2 * (ALPS_BITMAP_Y_BITS - 1)); |
| 362 | |
| 363 | if (fingers > 1) { |
| 364 | *x2 = (ALPS_V3_X_MAX * (2 * x_high.start_bit + x_high.num_bits - 1)) / |
| 365 | (2 * (ALPS_BITMAP_X_BITS - 1)); |
| 366 | *y2 = (ALPS_V3_Y_MAX * (2 * y_high.start_bit + y_high.num_bits - 1)) / |
| 367 | (2 * (ALPS_BITMAP_Y_BITS - 1)); |
| 368 | } |
| 369 | |
| 370 | return fingers; |
| 371 | } |
| 372 | |
| 373 | static void alps_set_slot(struct input_dev *dev, int slot, bool active, |
| 374 | int x, int y) |
| 375 | { |
| 376 | input_mt_slot(dev, slot); |
| 377 | input_mt_report_slot_state(dev, MT_TOOL_FINGER, active); |
| 378 | if (active) { |
| 379 | input_report_abs(dev, ABS_MT_POSITION_X, x); |
| 380 | input_report_abs(dev, ABS_MT_POSITION_Y, y); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers, |
| 385 | int x1, int y1, int x2, int y2) |
| 386 | { |
| 387 | alps_set_slot(dev, 0, num_fingers != 0, x1, y1); |
| 388 | alps_set_slot(dev, 1, num_fingers == 2, x2, y2); |
| 389 | } |
| 390 | |
| 391 | static void alps_process_trackstick_packet_v3(struct psmouse *psmouse) |
| 392 | { |
| 393 | struct alps_data *priv = psmouse->private; |
| 394 | unsigned char *packet = psmouse->packet; |
| 395 | struct input_dev *dev = priv->dev2; |
| 396 | int x, y, z, left, right, middle; |
| 397 | |
| 398 | /* Sanity check packet */ |
| 399 | if (!(packet[0] & 0x40)) { |
| 400 | psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n"); |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * There's a special packet that seems to indicate the end |
| 406 | * of a stream of trackstick data. Filter these out. |
| 407 | */ |
| 408 | if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f) |
| 409 | return; |
| 410 | |
| 411 | x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f)); |
| 412 | y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f)); |
| 413 | z = (packet[4] & 0x7c) >> 2; |
| 414 | |
| 415 | /* |
| 416 | * The x and y values tend to be quite large, and when used |
| 417 | * alone the trackstick is difficult to use. Scale them down |
| 418 | * to compensate. |
| 419 | */ |
| 420 | x /= 8; |
| 421 | y /= 8; |
| 422 | |
| 423 | input_report_rel(dev, REL_X, x); |
| 424 | input_report_rel(dev, REL_Y, -y); |
| 425 | |
| 426 | /* |
| 427 | * Most ALPS models report the trackstick buttons in the touchpad |
| 428 | * packets, but a few report them here. No reliable way has been |
| 429 | * found to differentiate between the models upfront, so we enable |
| 430 | * the quirk in response to seeing a button press in the trackstick |
| 431 | * packet. |
| 432 | */ |
| 433 | left = packet[3] & 0x01; |
| 434 | right = packet[3] & 0x02; |
| 435 | middle = packet[3] & 0x04; |
| 436 | |
| 437 | if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) && |
| 438 | (left || right || middle)) |
| 439 | priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS; |
| 440 | |
| 441 | if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) { |
| 442 | input_report_key(dev, BTN_LEFT, left); |
| 443 | input_report_key(dev, BTN_RIGHT, right); |
| 444 | input_report_key(dev, BTN_MIDDLE, middle); |
| 445 | } |
| 446 | |
| 447 | input_sync(dev); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | static void alps_process_touchpad_packet_v3(struct psmouse *psmouse) |
| 452 | { |
| 453 | struct alps_data *priv = psmouse->private; |
| 454 | unsigned char *packet = psmouse->packet; |
| 455 | struct input_dev *dev = psmouse->dev; |
| 456 | struct input_dev *dev2 = priv->dev2; |
| 457 | int x, y, z; |
| 458 | int left, right, middle; |
| 459 | int x1 = 0, y1 = 0, x2 = 0, y2 = 0; |
| 460 | int fingers = 0, bmap_fingers; |
| 461 | unsigned int x_bitmap, y_bitmap; |
| 462 | |
| 463 | /* |
| 464 | * There's no single feature of touchpad position and bitmap packets |
| 465 | * that can be used to distinguish between them. We rely on the fact |
| 466 | * that a bitmap packet should always follow a position packet with |
| 467 | * bit 6 of packet[4] set. |
| 468 | */ |
| 469 | if (priv->multi_packet) { |
| 470 | /* |
| 471 | * Sometimes a position packet will indicate a multi-packet |
| 472 | * sequence, but then what follows is another position |
| 473 | * packet. Check for this, and when it happens process the |
| 474 | * position packet as usual. |
| 475 | */ |
| 476 | if (packet[0] & 0x40) { |
| 477 | fingers = (packet[5] & 0x3) + 1; |
| 478 | x_bitmap = ((packet[4] & 0x7e) << 8) | |
| 479 | ((packet[1] & 0x7f) << 2) | |
| 480 | ((packet[0] & 0x30) >> 4); |
| 481 | y_bitmap = ((packet[3] & 0x70) << 4) | |
| 482 | ((packet[2] & 0x7f) << 1) | |
| 483 | (packet[4] & 0x01); |
| 484 | |
| 485 | bmap_fingers = alps_process_bitmap(x_bitmap, y_bitmap, |
| 486 | &x1, &y1, &x2, &y2); |
| 487 | |
| 488 | /* |
| 489 | * We shouldn't report more than one finger if |
| 490 | * we don't have two coordinates. |
| 491 | */ |
| 492 | if (fingers > 1 && bmap_fingers < 2) |
| 493 | fingers = bmap_fingers; |
| 494 | |
| 495 | /* Now process position packet */ |
| 496 | packet = priv->multi_data; |
| 497 | } else { |
| 498 | priv->multi_packet = 0; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * Bit 6 of byte 0 is not usually set in position packets. The only |
| 504 | * times it seems to be set is in situations where the data is |
| 505 | * suspect anyway, e.g. a palm resting flat on the touchpad. Given |
| 506 | * this combined with the fact that this bit is useful for filtering |
| 507 | * out misidentified bitmap packets, we reject anything with this |
| 508 | * bit set. |
| 509 | */ |
| 510 | if (packet[0] & 0x40) |
| 511 | return; |
| 512 | |
| 513 | if (!priv->multi_packet && (packet[4] & 0x40)) { |
| 514 | priv->multi_packet = 1; |
| 515 | memcpy(priv->multi_data, packet, sizeof(priv->multi_data)); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | priv->multi_packet = 0; |
| 520 | |
| 521 | left = packet[3] & 0x01; |
| 522 | right = packet[3] & 0x02; |
| 523 | middle = packet[3] & 0x04; |
| 524 | |
| 525 | x = ((packet[1] & 0x7f) << 4) | ((packet[4] & 0x30) >> 2) | |
| 526 | ((packet[0] & 0x30) >> 4); |
| 527 | y = ((packet[2] & 0x7f) << 4) | (packet[4] & 0x0f); |
| 528 | z = packet[5] & 0x7f; |
| 529 | |
| 530 | /* |
| 531 | * Sometimes the hardware sends a single packet with z = 0 |
| 532 | * in the middle of a stream. Real releases generate packets |
| 533 | * with x, y, and z all zero, so these seem to be flukes. |
| 534 | * Ignore them. |
| 535 | */ |
| 536 | if (x && y && !z) |
| 537 | return; |
| 538 | |
| 539 | /* |
| 540 | * If we don't have MT data or the bitmaps were empty, we have |
| 541 | * to rely on ST data. |
| 542 | */ |
| 543 | if (!fingers) { |
| 544 | x1 = x; |
| 545 | y1 = y; |
| 546 | fingers = z > 0 ? 1 : 0; |
| 547 | } |
| 548 | |
| 549 | if (z >= 64) |
| 550 | input_report_key(dev, BTN_TOUCH, 1); |
| 551 | else |
| 552 | input_report_key(dev, BTN_TOUCH, 0); |
| 553 | |
| 554 | alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2); |
| 555 | |
| 556 | input_report_key(dev, BTN_TOOL_FINGER, fingers == 1); |
| 557 | input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2); |
| 558 | input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3); |
| 559 | input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4); |
| 560 | |
| 561 | input_report_key(dev, BTN_LEFT, left); |
| 562 | input_report_key(dev, BTN_RIGHT, right); |
| 563 | input_report_key(dev, BTN_MIDDLE, middle); |
| 564 | |
| 565 | if (z > 0) { |
| 566 | input_report_abs(dev, ABS_X, x); |
| 567 | input_report_abs(dev, ABS_Y, y); |
| 568 | } |
| 569 | input_report_abs(dev, ABS_PRESSURE, z); |
| 570 | |
| 571 | input_sync(dev); |
| 572 | |
| 573 | if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) { |
| 574 | left = packet[3] & 0x10; |
| 575 | right = packet[3] & 0x20; |
| 576 | middle = packet[3] & 0x40; |
| 577 | |
| 578 | input_report_key(dev2, BTN_LEFT, left); |
| 579 | input_report_key(dev2, BTN_RIGHT, right); |
| 580 | input_report_key(dev2, BTN_MIDDLE, middle); |
| 581 | input_sync(dev2); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | static void alps_process_packet_v3(struct psmouse *psmouse) |
| 586 | { |
| 587 | unsigned char *packet = psmouse->packet; |
| 588 | |
| 589 | /* |
| 590 | * v3 protocol packets come in three types, two representing |
| 591 | * touchpad data and one representing trackstick data. |
| 592 | * Trackstick packets seem to be distinguished by always |
| 593 | * having 0x3f in the last byte. This value has never been |
| 594 | * observed in the last byte of either of the other types |
| 595 | * of packets. |
| 596 | */ |
| 597 | if (packet[5] == 0x3f) { |
| 598 | alps_process_trackstick_packet_v3(psmouse); |
| 599 | return; |
| 600 | } |
| 601 | |
| 602 | alps_process_touchpad_packet_v3(psmouse); |
| 603 | } |
| 604 | |
| 605 | static void alps_process_packet_v4(struct psmouse *psmouse) |
| 606 | { |
| 607 | unsigned char *packet = psmouse->packet; |
| 608 | struct input_dev *dev = psmouse->dev; |
| 609 | int x, y, z; |
| 610 | int left, right; |
| 611 | |
| 612 | left = packet[4] & 0x01; |
| 613 | right = packet[4] & 0x02; |
| 614 | |
| 615 | x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) | |
| 616 | ((packet[0] & 0x30) >> 4); |
| 617 | y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f); |
| 618 | z = packet[5] & 0x7f; |
| 619 | |
| 620 | if (z >= 64) |
| 621 | input_report_key(dev, BTN_TOUCH, 1); |
| 622 | else |
| 623 | input_report_key(dev, BTN_TOUCH, 0); |
| 624 | |
| 625 | if (z > 0) { |
| 626 | input_report_abs(dev, ABS_X, x); |
| 627 | input_report_abs(dev, ABS_Y, y); |
| 628 | } |
| 629 | input_report_abs(dev, ABS_PRESSURE, z); |
| 630 | |
| 631 | input_report_key(dev, BTN_TOOL_FINGER, z > 0); |
| 632 | input_report_key(dev, BTN_LEFT, left); |
| 633 | input_report_key(dev, BTN_RIGHT, right); |
| 634 | |
| 635 | input_sync(dev); |
| 636 | } |
| 637 | |
| 638 | static void alps_process_packet(struct psmouse *psmouse) |
| 639 | { |
| 640 | struct alps_data *priv = psmouse->private; |
| 641 | const struct alps_model_info *model = priv->i; |
| 642 | |
| 643 | switch (model->proto_version) { |
| 644 | case ALPS_PROTO_V1: |
| 645 | case ALPS_PROTO_V2: |
| 646 | alps_process_packet_v1_v2(psmouse); |
| 647 | break; |
| 648 | case ALPS_PROTO_V3: |
| 649 | alps_process_packet_v3(psmouse); |
| 650 | break; |
| 651 | case ALPS_PROTO_V4: |
| 652 | alps_process_packet_v4(psmouse); |
| 653 | break; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | static void alps_report_bare_ps2_packet(struct psmouse *psmouse, |
| 658 | unsigned char packet[], |
| 659 | bool report_buttons) |
| 660 | { |
| 661 | struct alps_data *priv = psmouse->private; |
| 662 | struct input_dev *dev2 = priv->dev2; |
| 663 | |
| 664 | if (report_buttons) |
| 665 | alps_report_buttons(psmouse, dev2, psmouse->dev, |
| 666 | packet[0] & 1, packet[0] & 2, packet[0] & 4); |
| 667 | |
| 668 | input_report_rel(dev2, REL_X, |
| 669 | packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0); |
| 670 | input_report_rel(dev2, REL_Y, |
| 671 | packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0); |
| 672 | |
| 673 | input_sync(dev2); |
| 674 | } |
| 675 | |
| 676 | static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse) |
| 677 | { |
| 678 | struct alps_data *priv = psmouse->private; |
| 679 | |
| 680 | if (psmouse->pktcnt < 6) |
| 681 | return PSMOUSE_GOOD_DATA; |
| 682 | |
| 683 | if (psmouse->pktcnt == 6) { |
| 684 | /* |
| 685 | * Start a timer to flush the packet if it ends up last |
| 686 | * 6-byte packet in the stream. Timer needs to fire |
| 687 | * psmouse core times out itself. 20 ms should be enough |
| 688 | * to decide if we are getting more data or not. |
| 689 | */ |
| 690 | mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20)); |
| 691 | return PSMOUSE_GOOD_DATA; |
| 692 | } |
| 693 | |
| 694 | del_timer(&priv->timer); |
| 695 | |
| 696 | if (psmouse->packet[6] & 0x80) { |
| 697 | |
| 698 | /* |
| 699 | * Highest bit is set - that means we either had |
| 700 | * complete ALPS packet and this is start of the |
| 701 | * next packet or we got garbage. |
| 702 | */ |
| 703 | |
| 704 | if (((psmouse->packet[3] | |
| 705 | psmouse->packet[4] | |
| 706 | psmouse->packet[5]) & 0x80) || |
| 707 | (!alps_is_valid_first_byte(priv->i, psmouse->packet[6]))) { |
| 708 | psmouse_dbg(psmouse, |
| 709 | "refusing packet %x %x %x %x (suspected interleaved ps/2)\n", |
| 710 | psmouse->packet[3], psmouse->packet[4], |
| 711 | psmouse->packet[5], psmouse->packet[6]); |
| 712 | return PSMOUSE_BAD_DATA; |
| 713 | } |
| 714 | |
| 715 | alps_process_packet(psmouse); |
| 716 | |
| 717 | /* Continue with the next packet */ |
| 718 | psmouse->packet[0] = psmouse->packet[6]; |
| 719 | psmouse->pktcnt = 1; |
| 720 | |
| 721 | } else { |
| 722 | |
| 723 | /* |
| 724 | * High bit is 0 - that means that we indeed got a PS/2 |
| 725 | * packet in the middle of ALPS packet. |
| 726 | * |
| 727 | * There is also possibility that we got 6-byte ALPS |
| 728 | * packet followed by 3-byte packet from trackpoint. We |
| 729 | * can not distinguish between these 2 scenarios but |
| 730 | * because the latter is unlikely to happen in course of |
| 731 | * normal operation (user would need to press all |
| 732 | * buttons on the pad and start moving trackpoint |
| 733 | * without touching the pad surface) we assume former. |
| 734 | * Even if we are wrong the wost thing that would happen |
| 735 | * the cursor would jump but we should not get protocol |
| 736 | * de-synchronization. |
| 737 | */ |
| 738 | |
| 739 | alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3], |
| 740 | false); |
| 741 | |
| 742 | /* |
| 743 | * Continue with the standard ALPS protocol handling, |
| 744 | * but make sure we won't process it as an interleaved |
| 745 | * packet again, which may happen if all buttons are |
| 746 | * pressed. To avoid this let's reset the 4th bit which |
| 747 | * is normally 1. |
| 748 | */ |
| 749 | psmouse->packet[3] = psmouse->packet[6] & 0xf7; |
| 750 | psmouse->pktcnt = 4; |
| 751 | } |
| 752 | |
| 753 | return PSMOUSE_GOOD_DATA; |
| 754 | } |
| 755 | |
| 756 | static void alps_flush_packet(unsigned long data) |
| 757 | { |
| 758 | struct psmouse *psmouse = (struct psmouse *)data; |
| 759 | |
| 760 | serio_pause_rx(psmouse->ps2dev.serio); |
| 761 | |
| 762 | if (psmouse->pktcnt == psmouse->pktsize) { |
| 763 | |
| 764 | /* |
| 765 | * We did not any more data in reasonable amount of time. |
| 766 | * Validate the last 3 bytes and process as a standard |
| 767 | * ALPS packet. |
| 768 | */ |
| 769 | if ((psmouse->packet[3] | |
| 770 | psmouse->packet[4] | |
| 771 | psmouse->packet[5]) & 0x80) { |
| 772 | psmouse_dbg(psmouse, |
| 773 | "refusing packet %x %x %x (suspected interleaved ps/2)\n", |
| 774 | psmouse->packet[3], psmouse->packet[4], |
| 775 | psmouse->packet[5]); |
| 776 | } else { |
| 777 | alps_process_packet(psmouse); |
| 778 | } |
| 779 | psmouse->pktcnt = 0; |
| 780 | } |
| 781 | |
| 782 | serio_continue_rx(psmouse->ps2dev.serio); |
| 783 | } |
| 784 | |
| 785 | static psmouse_ret_t alps_process_byte(struct psmouse *psmouse) |
| 786 | { |
| 787 | struct alps_data *priv = psmouse->private; |
| 788 | const struct alps_model_info *model = priv->i; |
| 789 | |
| 790 | /* |
| 791 | * Check if we are dealing with a bare PS/2 packet, presumably from |
| 792 | * a device connected to the external PS/2 port. Because bare PS/2 |
| 793 | * protocol does not have enough constant bits to self-synchronize |
| 794 | * properly we only do this if the device is fully synchronized. |
| 795 | */ |
| 796 | if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) { |
| 797 | if (psmouse->pktcnt == 3) { |
| 798 | alps_report_bare_ps2_packet(psmouse, psmouse->packet, |
| 799 | true); |
| 800 | return PSMOUSE_FULL_PACKET; |
| 801 | } |
| 802 | return PSMOUSE_GOOD_DATA; |
| 803 | } |
| 804 | |
| 805 | /* Check for PS/2 packet stuffed in the middle of ALPS packet. */ |
| 806 | |
| 807 | if ((model->flags & ALPS_PS2_INTERLEAVED) && |
| 808 | psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) { |
| 809 | return alps_handle_interleaved_ps2(psmouse); |
| 810 | } |
| 811 | |
| 812 | if (!alps_is_valid_first_byte(model, psmouse->packet[0])) { |
| 813 | psmouse_dbg(psmouse, |
| 814 | "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n", |
| 815 | psmouse->packet[0], model->mask0, model->byte0); |
| 816 | return PSMOUSE_BAD_DATA; |
| 817 | } |
| 818 | |
| 819 | /* Bytes 2 - pktsize should have 0 in the highest bit */ |
| 820 | if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize && |
| 821 | (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) { |
| 822 | psmouse_dbg(psmouse, "refusing packet[%i] = %x\n", |
| 823 | psmouse->pktcnt - 1, |
| 824 | psmouse->packet[psmouse->pktcnt - 1]); |
| 825 | return PSMOUSE_BAD_DATA; |
| 826 | } |
| 827 | |
| 828 | if (psmouse->pktcnt == psmouse->pktsize) { |
| 829 | alps_process_packet(psmouse); |
| 830 | return PSMOUSE_FULL_PACKET; |
| 831 | } |
| 832 | |
| 833 | return PSMOUSE_GOOD_DATA; |
| 834 | } |
| 835 | |
| 836 | static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble) |
| 837 | { |
| 838 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 839 | struct alps_data *priv = psmouse->private; |
| 840 | int command; |
| 841 | unsigned char *param; |
| 842 | unsigned char dummy[4]; |
| 843 | |
| 844 | BUG_ON(nibble > 0xf); |
| 845 | |
| 846 | command = priv->nibble_commands[nibble].command; |
| 847 | param = (command & 0x0f00) ? |
| 848 | dummy : (unsigned char *)&priv->nibble_commands[nibble].data; |
| 849 | |
| 850 | if (ps2_command(ps2dev, param, command)) |
| 851 | return -1; |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr) |
| 857 | { |
| 858 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 859 | struct alps_data *priv = psmouse->private; |
| 860 | int i, nibble; |
| 861 | |
| 862 | if (ps2_command(ps2dev, NULL, priv->addr_command)) |
| 863 | return -1; |
| 864 | |
| 865 | for (i = 12; i >= 0; i -= 4) { |
| 866 | nibble = (addr >> i) & 0xf; |
| 867 | if (alps_command_mode_send_nibble(psmouse, nibble)) |
| 868 | return -1; |
| 869 | } |
| 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr) |
| 875 | { |
| 876 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 877 | unsigned char param[4]; |
| 878 | |
| 879 | if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) |
| 880 | return -1; |
| 881 | |
| 882 | /* |
| 883 | * The address being read is returned in the first two bytes |
| 884 | * of the result. Check that this address matches the expected |
| 885 | * address. |
| 886 | */ |
| 887 | if (addr != ((param[0] << 8) | param[1])) |
| 888 | return -1; |
| 889 | |
| 890 | return param[2]; |
| 891 | } |
| 892 | |
| 893 | static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr) |
| 894 | { |
| 895 | if (alps_command_mode_set_addr(psmouse, addr)) |
| 896 | return -1; |
| 897 | return __alps_command_mode_read_reg(psmouse, addr); |
| 898 | } |
| 899 | |
| 900 | static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value) |
| 901 | { |
| 902 | if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf)) |
| 903 | return -1; |
| 904 | if (alps_command_mode_send_nibble(psmouse, value & 0xf)) |
| 905 | return -1; |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr, |
| 910 | u8 value) |
| 911 | { |
| 912 | if (alps_command_mode_set_addr(psmouse, addr)) |
| 913 | return -1; |
| 914 | return __alps_command_mode_write_reg(psmouse, value); |
| 915 | } |
| 916 | |
| 917 | static int alps_enter_command_mode(struct psmouse *psmouse, |
| 918 | unsigned char *resp) |
| 919 | { |
| 920 | unsigned char param[4]; |
| 921 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 922 | |
| 923 | if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) || |
| 924 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) || |
| 925 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) || |
| 926 | ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) { |
| 927 | psmouse_err(psmouse, "failed to enter command mode\n"); |
| 928 | return -1; |
| 929 | } |
| 930 | |
| 931 | if (param[0] != 0x88 && param[1] != 0x07) { |
| 932 | psmouse_dbg(psmouse, |
| 933 | "unknown response while entering command mode: %2.2x %2.2x %2.2x\n", |
| 934 | param[0], param[1], param[2]); |
| 935 | return -1; |
| 936 | } |
| 937 | |
| 938 | if (resp) |
| 939 | *resp = param[2]; |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | static inline int alps_exit_command_mode(struct psmouse *psmouse) |
| 944 | { |
| 945 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 946 | if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) |
| 947 | return -1; |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | static const struct alps_model_info *alps_get_model(struct psmouse *psmouse, int *version) |
| 952 | { |
| 953 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 954 | static const unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 }; |
| 955 | unsigned char param[4]; |
| 956 | const struct alps_model_info *model = NULL; |
| 957 | int i; |
| 958 | |
| 959 | /* |
| 960 | * First try "E6 report". |
| 961 | * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed. |
| 962 | * The bits 0-2 of the first byte will be 1s if some buttons are |
| 963 | * pressed. |
| 964 | */ |
| 965 | param[0] = 0; |
| 966 | if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) || |
| 967 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) || |
| 968 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) || |
| 969 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) |
| 970 | return NULL; |
| 971 | |
| 972 | param[0] = param[1] = param[2] = 0xff; |
| 973 | if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) |
| 974 | return NULL; |
| 975 | |
| 976 | psmouse_dbg(psmouse, "E6 report: %2.2x %2.2x %2.2x", |
| 977 | param[0], param[1], param[2]); |
| 978 | |
| 979 | if ((param[0] & 0xf8) != 0 || param[1] != 0 || |
| 980 | (param[2] != 10 && param[2] != 100)) |
| 981 | return NULL; |
| 982 | |
| 983 | /* |
| 984 | * Now try "E7 report". Allowed responses are in |
| 985 | * alps_model_data[].signature |
| 986 | */ |
| 987 | param[0] = 0; |
| 988 | if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) || |
| 989 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || |
| 990 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || |
| 991 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21)) |
| 992 | return NULL; |
| 993 | |
| 994 | param[0] = param[1] = param[2] = 0xff; |
| 995 | if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) |
| 996 | return NULL; |
| 997 | |
| 998 | psmouse_dbg(psmouse, "E7 report: %2.2x %2.2x %2.2x", |
| 999 | param[0], param[1], param[2]); |
| 1000 | |
| 1001 | if (version) { |
| 1002 | for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++) |
| 1003 | /* empty */; |
| 1004 | *version = (param[0] << 8) | (param[1] << 4) | i; |
| 1005 | } |
| 1006 | |
| 1007 | for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) { |
| 1008 | if (!memcmp(param, alps_model_data[i].signature, |
| 1009 | sizeof(alps_model_data[i].signature))) { |
| 1010 | model = alps_model_data + i; |
| 1011 | break; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | if (model && model->proto_version > ALPS_PROTO_V2) { |
| 1016 | /* |
| 1017 | * Need to check command mode response to identify |
| 1018 | * model |
| 1019 | */ |
| 1020 | model = NULL; |
| 1021 | if (alps_enter_command_mode(psmouse, param)) { |
| 1022 | psmouse_warn(psmouse, |
| 1023 | "touchpad failed to enter command mode\n"); |
| 1024 | } else { |
| 1025 | for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) { |
| 1026 | if (alps_model_data[i].proto_version > ALPS_PROTO_V2 && |
| 1027 | alps_model_data[i].command_mode_resp == param[0]) { |
| 1028 | model = alps_model_data + i; |
| 1029 | break; |
| 1030 | } |
| 1031 | } |
| 1032 | alps_exit_command_mode(psmouse); |
| 1033 | |
| 1034 | if (!model) |
| 1035 | psmouse_dbg(psmouse, |
| 1036 | "Unknown command mode response %2.2x\n", |
| 1037 | param[0]); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | return model; |
| 1042 | } |
| 1043 | |
| 1044 | /* |
| 1045 | * For DualPoint devices select the device that should respond to |
| 1046 | * subsequent commands. It looks like glidepad is behind stickpointer, |
| 1047 | * I'd thought it would be other way around... |
| 1048 | */ |
| 1049 | static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable) |
| 1050 | { |
| 1051 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 1052 | int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11; |
| 1053 | |
| 1054 | if (ps2_command(ps2dev, NULL, cmd) || |
| 1055 | ps2_command(ps2dev, NULL, cmd) || |
| 1056 | ps2_command(ps2dev, NULL, cmd) || |
| 1057 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE)) |
| 1058 | return -1; |
| 1059 | |
| 1060 | /* we may get 3 more bytes, just ignore them */ |
| 1061 | ps2_drain(ps2dev, 3, 100); |
| 1062 | |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
| 1066 | static int alps_absolute_mode_v1_v2(struct psmouse *psmouse) |
| 1067 | { |
| 1068 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 1069 | |
| 1070 | /* Try ALPS magic knock - 4 disable before enable */ |
| 1071 | if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || |
| 1072 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || |
| 1073 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || |
| 1074 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || |
| 1075 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) |
| 1076 | return -1; |
| 1077 | |
| 1078 | /* |
| 1079 | * Switch mouse to poll (remote) mode so motion data will not |
| 1080 | * get in our way |
| 1081 | */ |
| 1082 | return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL); |
| 1083 | } |
| 1084 | |
| 1085 | static int alps_get_status(struct psmouse *psmouse, char *param) |
| 1086 | { |
| 1087 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 1088 | |
| 1089 | /* Get status: 0xF5 0xF5 0xF5 0xE9 */ |
| 1090 | if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || |
| 1091 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || |
| 1092 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || |
| 1093 | ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) |
| 1094 | return -1; |
| 1095 | |
| 1096 | psmouse_dbg(psmouse, "Status: %2.2x %2.2x %2.2x", |
| 1097 | param[0], param[1], param[2]); |
| 1098 | |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | /* |
| 1103 | * Turn touchpad tapping on or off. The sequences are: |
| 1104 | * 0xE9 0xF5 0xF5 0xF3 0x0A to enable, |
| 1105 | * 0xE9 0xF5 0xF5 0xE8 0x00 to disable. |
| 1106 | * My guess that 0xE9 (GetInfo) is here as a sync point. |
| 1107 | * For models that also have stickpointer (DualPoints) its tapping |
| 1108 | * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but |
| 1109 | * we don't fiddle with it. |
| 1110 | */ |
| 1111 | static int alps_tap_mode(struct psmouse *psmouse, int enable) |
| 1112 | { |
| 1113 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 1114 | int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES; |
| 1115 | unsigned char tap_arg = enable ? 0x0A : 0x00; |
| 1116 | unsigned char param[4]; |
| 1117 | |
| 1118 | if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) || |
| 1119 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || |
| 1120 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || |
| 1121 | ps2_command(ps2dev, &tap_arg, cmd)) |
| 1122 | return -1; |
| 1123 | |
| 1124 | if (alps_get_status(psmouse, param)) |
| 1125 | return -1; |
| 1126 | |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | * alps_poll() - poll the touchpad for current motion packet. |
| 1132 | * Used in resync. |
| 1133 | */ |
| 1134 | static int alps_poll(struct psmouse *psmouse) |
| 1135 | { |
| 1136 | struct alps_data *priv = psmouse->private; |
| 1137 | unsigned char buf[sizeof(psmouse->packet)]; |
| 1138 | bool poll_failed; |
| 1139 | |
| 1140 | if (priv->i->flags & ALPS_PASS) |
| 1141 | alps_passthrough_mode_v2(psmouse, true); |
| 1142 | |
| 1143 | poll_failed = ps2_command(&psmouse->ps2dev, buf, |
| 1144 | PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0; |
| 1145 | |
| 1146 | if (priv->i->flags & ALPS_PASS) |
| 1147 | alps_passthrough_mode_v2(psmouse, false); |
| 1148 | |
| 1149 | if (poll_failed || (buf[0] & priv->i->mask0) != priv->i->byte0) |
| 1150 | return -1; |
| 1151 | |
| 1152 | if ((psmouse->badbyte & 0xc8) == 0x08) { |
| 1153 | /* |
| 1154 | * Poll the track stick ... |
| 1155 | */ |
| 1156 | if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8))) |
| 1157 | return -1; |
| 1158 | } |
| 1159 | |
| 1160 | memcpy(psmouse->packet, buf, sizeof(buf)); |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | static int alps_hw_init_v1_v2(struct psmouse *psmouse) |
| 1165 | { |
| 1166 | struct alps_data *priv = psmouse->private; |
| 1167 | const struct alps_model_info *model = priv->i; |
| 1168 | |
| 1169 | if ((model->flags & ALPS_PASS) && |
| 1170 | alps_passthrough_mode_v2(psmouse, true)) { |
| 1171 | return -1; |
| 1172 | } |
| 1173 | |
| 1174 | if (alps_tap_mode(psmouse, true)) { |
| 1175 | psmouse_warn(psmouse, "Failed to enable hardware tapping\n"); |
| 1176 | return -1; |
| 1177 | } |
| 1178 | |
| 1179 | if (alps_absolute_mode_v1_v2(psmouse)) { |
| 1180 | psmouse_err(psmouse, "Failed to enable absolute mode\n"); |
| 1181 | return -1; |
| 1182 | } |
| 1183 | |
| 1184 | if ((model->flags & ALPS_PASS) && |
| 1185 | alps_passthrough_mode_v2(psmouse, false)) { |
| 1186 | return -1; |
| 1187 | } |
| 1188 | |
| 1189 | /* ALPS needs stream mode, otherwise it won't report any data */ |
| 1190 | if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) { |
| 1191 | psmouse_err(psmouse, "Failed to enable stream mode\n"); |
| 1192 | return -1; |
| 1193 | } |
| 1194 | |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
| 1198 | /* |
| 1199 | * Enable or disable passthrough mode to the trackstick. Must be in |
| 1200 | * command mode when calling this function. |
| 1201 | */ |
| 1202 | static int alps_passthrough_mode_v3(struct psmouse *psmouse, bool enable) |
| 1203 | { |
| 1204 | int reg_val; |
| 1205 | |
| 1206 | reg_val = alps_command_mode_read_reg(psmouse, 0x0008); |
| 1207 | if (reg_val == -1) |
| 1208 | return -1; |
| 1209 | |
| 1210 | if (enable) |
| 1211 | reg_val |= 0x01; |
| 1212 | else |
| 1213 | reg_val &= ~0x01; |
| 1214 | |
| 1215 | if (__alps_command_mode_write_reg(psmouse, reg_val)) |
| 1216 | return -1; |
| 1217 | |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | /* Must be in command mode when calling this function */ |
| 1222 | static int alps_absolute_mode_v3(struct psmouse *psmouse) |
| 1223 | { |
| 1224 | int reg_val; |
| 1225 | |
| 1226 | reg_val = alps_command_mode_read_reg(psmouse, 0x0004); |
| 1227 | if (reg_val == -1) |
| 1228 | return -1; |
| 1229 | |
| 1230 | reg_val |= 0x06; |
| 1231 | if (__alps_command_mode_write_reg(psmouse, reg_val)) |
| 1232 | return -1; |
| 1233 | |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | static int alps_hw_init_v3(struct psmouse *psmouse) |
| 1238 | { |
| 1239 | struct alps_data *priv = psmouse->private; |
| 1240 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 1241 | int reg_val; |
| 1242 | unsigned char param[4]; |
| 1243 | |
| 1244 | priv->nibble_commands = alps_v3_nibble_commands; |
| 1245 | priv->addr_command = PSMOUSE_CMD_RESET_WRAP; |
| 1246 | |
| 1247 | if (alps_enter_command_mode(psmouse, NULL)) |
| 1248 | goto error; |
| 1249 | |
| 1250 | /* Check for trackstick */ |
| 1251 | reg_val = alps_command_mode_read_reg(psmouse, 0x0008); |
| 1252 | if (reg_val == -1) |
| 1253 | goto error; |
| 1254 | if (reg_val & 0x80) { |
| 1255 | if (alps_passthrough_mode_v3(psmouse, true)) |
| 1256 | goto error; |
| 1257 | if (alps_exit_command_mode(psmouse)) |
| 1258 | goto error; |
| 1259 | |
| 1260 | /* |
| 1261 | * E7 report for the trackstick |
| 1262 | * |
| 1263 | * There have been reports of failures to seem to trace back |
| 1264 | * to the above trackstick check failing. When these occur |
| 1265 | * this E7 report fails, so when that happens we continue |
| 1266 | * with the assumption that there isn't a trackstick after |
| 1267 | * all. |
| 1268 | */ |
| 1269 | param[0] = 0x64; |
| 1270 | if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || |
| 1271 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || |
| 1272 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || |
| 1273 | ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) { |
| 1274 | psmouse_warn(psmouse, "trackstick E7 report failed\n"); |
| 1275 | } else { |
| 1276 | psmouse_dbg(psmouse, |
| 1277 | "trackstick E7 report: %2.2x %2.2x %2.2x\n", |
| 1278 | param[0], param[1], param[2]); |
| 1279 | |
| 1280 | /* |
| 1281 | * Not sure what this does, but it is absolutely |
| 1282 | * essential. Without it, the touchpad does not |
| 1283 | * work at all and the trackstick just emits normal |
| 1284 | * PS/2 packets. |
| 1285 | */ |
| 1286 | if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) || |
| 1287 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) || |
| 1288 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) || |
| 1289 | alps_command_mode_send_nibble(psmouse, 0x9) || |
| 1290 | alps_command_mode_send_nibble(psmouse, 0x4)) { |
| 1291 | psmouse_err(psmouse, |
| 1292 | "Error sending magic E6 sequence\n"); |
| 1293 | goto error_passthrough; |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | if (alps_enter_command_mode(psmouse, NULL)) |
| 1298 | goto error_passthrough; |
| 1299 | if (alps_passthrough_mode_v3(psmouse, false)) |
| 1300 | goto error; |
| 1301 | } |
| 1302 | |
| 1303 | if (alps_absolute_mode_v3(psmouse)) { |
| 1304 | psmouse_err(psmouse, "Failed to enter absolute mode\n"); |
| 1305 | goto error; |
| 1306 | } |
| 1307 | |
| 1308 | reg_val = alps_command_mode_read_reg(psmouse, 0x0006); |
| 1309 | if (reg_val == -1) |
| 1310 | goto error; |
| 1311 | if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01)) |
| 1312 | goto error; |
| 1313 | |
| 1314 | reg_val = alps_command_mode_read_reg(psmouse, 0x0007); |
| 1315 | if (reg_val == -1) |
| 1316 | goto error; |
| 1317 | if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01)) |
| 1318 | goto error; |
| 1319 | |
| 1320 | if (alps_command_mode_read_reg(psmouse, 0x0144) == -1) |
| 1321 | goto error; |
| 1322 | if (__alps_command_mode_write_reg(psmouse, 0x04)) |
| 1323 | goto error; |
| 1324 | |
| 1325 | if (alps_command_mode_read_reg(psmouse, 0x0159) == -1) |
| 1326 | goto error; |
| 1327 | if (__alps_command_mode_write_reg(psmouse, 0x03)) |
| 1328 | goto error; |
| 1329 | |
| 1330 | if (alps_command_mode_read_reg(psmouse, 0x0163) == -1) |
| 1331 | goto error; |
| 1332 | if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03)) |
| 1333 | goto error; |
| 1334 | |
| 1335 | if (alps_command_mode_read_reg(psmouse, 0x0162) == -1) |
| 1336 | goto error; |
| 1337 | if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04)) |
| 1338 | goto error; |
| 1339 | |
| 1340 | /* |
| 1341 | * This ensures the trackstick packets are in the format |
| 1342 | * supported by this driver. If bit 1 isn't set the packet |
| 1343 | * format is different. |
| 1344 | */ |
| 1345 | if (alps_command_mode_write_reg(psmouse, 0x0008, 0x82)) |
| 1346 | goto error; |
| 1347 | |
| 1348 | alps_exit_command_mode(psmouse); |
| 1349 | |
| 1350 | /* Set rate and enable data reporting */ |
| 1351 | param[0] = 0x64; |
| 1352 | if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) || |
| 1353 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) { |
| 1354 | psmouse_err(psmouse, "Failed to enable data reporting\n"); |
| 1355 | return -1; |
| 1356 | } |
| 1357 | |
| 1358 | return 0; |
| 1359 | |
| 1360 | error_passthrough: |
| 1361 | /* Something failed while in passthrough mode, so try to get out */ |
| 1362 | if (!alps_enter_command_mode(psmouse, NULL)) |
| 1363 | alps_passthrough_mode_v3(psmouse, false); |
| 1364 | error: |
| 1365 | /* |
| 1366 | * Leaving the touchpad in command mode will essentially render |
| 1367 | * it unusable until the machine reboots, so exit it here just |
| 1368 | * to be safe |
| 1369 | */ |
| 1370 | alps_exit_command_mode(psmouse); |
| 1371 | return -1; |
| 1372 | } |
| 1373 | |
| 1374 | /* Must be in command mode when calling this function */ |
| 1375 | static int alps_absolute_mode_v4(struct psmouse *psmouse) |
| 1376 | { |
| 1377 | int reg_val; |
| 1378 | |
| 1379 | reg_val = alps_command_mode_read_reg(psmouse, 0x0004); |
| 1380 | if (reg_val == -1) |
| 1381 | return -1; |
| 1382 | |
| 1383 | reg_val |= 0x02; |
| 1384 | if (__alps_command_mode_write_reg(psmouse, reg_val)) |
| 1385 | return -1; |
| 1386 | |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | static int alps_hw_init_v4(struct psmouse *psmouse) |
| 1391 | { |
| 1392 | struct alps_data *priv = psmouse->private; |
| 1393 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 1394 | unsigned char param[4]; |
| 1395 | |
| 1396 | priv->nibble_commands = alps_v4_nibble_commands; |
| 1397 | priv->addr_command = PSMOUSE_CMD_DISABLE; |
| 1398 | |
| 1399 | if (alps_enter_command_mode(psmouse, NULL)) |
| 1400 | goto error; |
| 1401 | |
| 1402 | if (alps_absolute_mode_v4(psmouse)) { |
| 1403 | psmouse_err(psmouse, "Failed to enter absolute mode\n"); |
| 1404 | goto error; |
| 1405 | } |
| 1406 | |
| 1407 | if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c)) |
| 1408 | goto error; |
| 1409 | |
| 1410 | if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03)) |
| 1411 | goto error; |
| 1412 | |
| 1413 | if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03)) |
| 1414 | goto error; |
| 1415 | |
| 1416 | if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15)) |
| 1417 | goto error; |
| 1418 | |
| 1419 | if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01)) |
| 1420 | goto error; |
| 1421 | |
| 1422 | if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03)) |
| 1423 | goto error; |
| 1424 | |
| 1425 | if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03)) |
| 1426 | goto error; |
| 1427 | |
| 1428 | if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03)) |
| 1429 | goto error; |
| 1430 | |
| 1431 | alps_exit_command_mode(psmouse); |
| 1432 | |
| 1433 | /* |
| 1434 | * This sequence changes the output from a 9-byte to an |
| 1435 | * 8-byte format. All the same data seems to be present, |
| 1436 | * just in a more compact format. |
| 1437 | */ |
| 1438 | param[0] = 0xc8; |
| 1439 | param[1] = 0x64; |
| 1440 | param[2] = 0x50; |
| 1441 | if (ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) || |
| 1442 | ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE) || |
| 1443 | ps2_command(ps2dev, ¶m[2], PSMOUSE_CMD_SETRATE) || |
| 1444 | ps2_command(ps2dev, param, PSMOUSE_CMD_GETID)) |
| 1445 | return -1; |
| 1446 | |
| 1447 | /* Set rate and enable data reporting */ |
| 1448 | param[0] = 0x64; |
| 1449 | if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) || |
| 1450 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) { |
| 1451 | psmouse_err(psmouse, "Failed to enable data reporting\n"); |
| 1452 | return -1; |
| 1453 | } |
| 1454 | |
| 1455 | return 0; |
| 1456 | |
| 1457 | error: |
| 1458 | /* |
| 1459 | * Leaving the touchpad in command mode will essentially render |
| 1460 | * it unusable until the machine reboots, so exit it here just |
| 1461 | * to be safe |
| 1462 | */ |
| 1463 | alps_exit_command_mode(psmouse); |
| 1464 | return -1; |
| 1465 | } |
| 1466 | |
| 1467 | static int alps_hw_init(struct psmouse *psmouse) |
| 1468 | { |
| 1469 | struct alps_data *priv = psmouse->private; |
| 1470 | const struct alps_model_info *model = priv->i; |
| 1471 | int ret = -1; |
| 1472 | |
| 1473 | switch (model->proto_version) { |
| 1474 | case ALPS_PROTO_V1: |
| 1475 | case ALPS_PROTO_V2: |
| 1476 | ret = alps_hw_init_v1_v2(psmouse); |
| 1477 | break; |
| 1478 | case ALPS_PROTO_V3: |
| 1479 | ret = alps_hw_init_v3(psmouse); |
| 1480 | break; |
| 1481 | case ALPS_PROTO_V4: |
| 1482 | ret = alps_hw_init_v4(psmouse); |
| 1483 | break; |
| 1484 | } |
| 1485 | |
| 1486 | return ret; |
| 1487 | } |
| 1488 | |
| 1489 | static int alps_reconnect(struct psmouse *psmouse) |
| 1490 | { |
| 1491 | const struct alps_model_info *model; |
| 1492 | |
| 1493 | psmouse_reset(psmouse); |
| 1494 | |
| 1495 | model = alps_get_model(psmouse, NULL); |
| 1496 | if (!model) |
| 1497 | return -1; |
| 1498 | |
| 1499 | return alps_hw_init(psmouse); |
| 1500 | } |
| 1501 | |
| 1502 | static void alps_disconnect(struct psmouse *psmouse) |
| 1503 | { |
| 1504 | struct alps_data *priv = psmouse->private; |
| 1505 | |
| 1506 | psmouse_reset(psmouse); |
| 1507 | del_timer_sync(&priv->timer); |
| 1508 | input_unregister_device(priv->dev2); |
| 1509 | kfree(priv); |
| 1510 | } |
| 1511 | |
| 1512 | int alps_init(struct psmouse *psmouse) |
| 1513 | { |
| 1514 | struct alps_data *priv; |
| 1515 | const struct alps_model_info *model; |
| 1516 | struct input_dev *dev1 = psmouse->dev, *dev2; |
| 1517 | int version; |
| 1518 | |
| 1519 | priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL); |
| 1520 | dev2 = input_allocate_device(); |
| 1521 | if (!priv || !dev2) |
| 1522 | goto init_fail; |
| 1523 | |
| 1524 | priv->dev2 = dev2; |
| 1525 | setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse); |
| 1526 | |
| 1527 | psmouse->private = priv; |
| 1528 | |
| 1529 | psmouse_reset(psmouse); |
| 1530 | |
| 1531 | model = alps_get_model(psmouse, &version); |
| 1532 | if (!model) |
| 1533 | goto init_fail; |
| 1534 | |
| 1535 | priv->i = model; |
| 1536 | |
| 1537 | if (alps_hw_init(psmouse)) |
| 1538 | goto init_fail; |
| 1539 | |
| 1540 | /* |
| 1541 | * Undo part of setup done for us by psmouse core since touchpad |
| 1542 | * is not a relative device. |
| 1543 | */ |
| 1544 | __clear_bit(EV_REL, dev1->evbit); |
| 1545 | __clear_bit(REL_X, dev1->relbit); |
| 1546 | __clear_bit(REL_Y, dev1->relbit); |
| 1547 | |
| 1548 | /* |
| 1549 | * Now set up our capabilities. |
| 1550 | */ |
| 1551 | dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY); |
| 1552 | dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH); |
| 1553 | dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER); |
| 1554 | dev1->keybit[BIT_WORD(BTN_LEFT)] |= |
| 1555 | BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT); |
| 1556 | |
| 1557 | dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS); |
| 1558 | |
| 1559 | switch (model->proto_version) { |
| 1560 | case ALPS_PROTO_V1: |
| 1561 | case ALPS_PROTO_V2: |
| 1562 | input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0); |
| 1563 | input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0); |
| 1564 | break; |
| 1565 | case ALPS_PROTO_V3: |
| 1566 | set_bit(INPUT_PROP_SEMI_MT, dev1->propbit); |
| 1567 | input_mt_init_slots(dev1, 2); |
| 1568 | input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, ALPS_V3_X_MAX, 0, 0); |
| 1569 | input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, ALPS_V3_Y_MAX, 0, 0); |
| 1570 | |
| 1571 | set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit); |
| 1572 | set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit); |
| 1573 | set_bit(BTN_TOOL_QUADTAP, dev1->keybit); |
| 1574 | /* fall through */ |
| 1575 | case ALPS_PROTO_V4: |
| 1576 | input_set_abs_params(dev1, ABS_X, 0, ALPS_V3_X_MAX, 0, 0); |
| 1577 | input_set_abs_params(dev1, ABS_Y, 0, ALPS_V3_Y_MAX, 0, 0); |
| 1578 | break; |
| 1579 | } |
| 1580 | |
| 1581 | input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0); |
| 1582 | |
| 1583 | if (model->flags & ALPS_WHEEL) { |
| 1584 | dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL); |
| 1585 | dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL); |
| 1586 | } |
| 1587 | |
| 1588 | if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) { |
| 1589 | dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD); |
| 1590 | dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK); |
| 1591 | } |
| 1592 | |
| 1593 | if (model->flags & ALPS_FOUR_BUTTONS) { |
| 1594 | dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0); |
| 1595 | dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1); |
| 1596 | dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2); |
| 1597 | dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3); |
| 1598 | } else { |
| 1599 | dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE); |
| 1600 | } |
| 1601 | |
| 1602 | snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys); |
| 1603 | dev2->phys = priv->phys; |
| 1604 | dev2->name = (model->flags & ALPS_DUALPOINT) ? "DualPoint Stick" : "PS/2 Mouse"; |
| 1605 | dev2->id.bustype = BUS_I8042; |
| 1606 | dev2->id.vendor = 0x0002; |
| 1607 | dev2->id.product = PSMOUSE_ALPS; |
| 1608 | dev2->id.version = 0x0000; |
| 1609 | dev2->dev.parent = &psmouse->ps2dev.serio->dev; |
| 1610 | |
| 1611 | dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL); |
| 1612 | dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y); |
| 1613 | dev2->keybit[BIT_WORD(BTN_LEFT)] = |
| 1614 | BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT); |
| 1615 | |
| 1616 | if (input_register_device(priv->dev2)) |
| 1617 | goto init_fail; |
| 1618 | |
| 1619 | psmouse->protocol_handler = alps_process_byte; |
| 1620 | psmouse->poll = alps_poll; |
| 1621 | psmouse->disconnect = alps_disconnect; |
| 1622 | psmouse->reconnect = alps_reconnect; |
| 1623 | psmouse->pktsize = model->proto_version == ALPS_PROTO_V4 ? 8 : 6; |
| 1624 | |
| 1625 | /* We are having trouble resyncing ALPS touchpads so disable it for now */ |
| 1626 | psmouse->resync_time = 0; |
| 1627 | |
| 1628 | /* Allow 2 invalid packets without resetting device */ |
| 1629 | psmouse->resetafter = psmouse->pktsize * 2; |
| 1630 | |
| 1631 | return 0; |
| 1632 | |
| 1633 | init_fail: |
| 1634 | psmouse_reset(psmouse); |
| 1635 | input_free_device(dev2); |
| 1636 | kfree(priv); |
| 1637 | psmouse->private = NULL; |
| 1638 | return -1; |
| 1639 | } |
| 1640 | |
| 1641 | int alps_detect(struct psmouse *psmouse, bool set_properties) |
| 1642 | { |
| 1643 | int version; |
| 1644 | const struct alps_model_info *model; |
| 1645 | |
| 1646 | model = alps_get_model(psmouse, &version); |
| 1647 | if (!model) |
| 1648 | return -1; |
| 1649 | |
| 1650 | if (set_properties) { |
| 1651 | psmouse->vendor = "ALPS"; |
| 1652 | psmouse->name = model->flags & ALPS_DUALPOINT ? |
| 1653 | "DualPoint TouchPad" : "GlidePoint"; |
| 1654 | psmouse->model = version; |
| 1655 | } |
| 1656 | return 0; |
| 1657 | } |
| 1658 | |