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