1df08ef27SAndres Salomon /* 2df08ef27SAndres Salomon * OLPC HGPK (XO-1) touchpad PS/2 mouse driver 3df08ef27SAndres Salomon * 4df08ef27SAndres Salomon * Copyright (c) 2006-2008 One Laptop Per Child 5df08ef27SAndres Salomon * Authors: 6df08ef27SAndres Salomon * Zephaniah E. Hull 7df08ef27SAndres Salomon * Andres Salomon <dilinger@debian.org> 8df08ef27SAndres Salomon * 9df08ef27SAndres Salomon * This driver is partly based on the ALPS driver, which is: 10df08ef27SAndres Salomon * 11df08ef27SAndres Salomon * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au> 12df08ef27SAndres Salomon * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com> 13df08ef27SAndres Salomon * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru> 14df08ef27SAndres Salomon * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz> 15df08ef27SAndres Salomon * 16df08ef27SAndres Salomon * This program is free software; you can redistribute it and/or modify 17df08ef27SAndres Salomon * it under the terms of the GNU General Public License version 2 as 18df08ef27SAndres Salomon * published by the Free Software Foundation. 19df08ef27SAndres Salomon */ 20df08ef27SAndres Salomon 21df08ef27SAndres Salomon /* 22df08ef27SAndres Salomon * The spec from ALPS is available from 23df08ef27SAndres Salomon * <http://wiki.laptop.org/go/Touch_Pad/Tablet>. It refers to this 24df08ef27SAndres Salomon * device as HGPK (Hybrid GS, PT, and Keymatrix). 25df08ef27SAndres Salomon * 26df08ef27SAndres Salomon * The earliest versions of the device had simultaneous reporting; that 27df08ef27SAndres Salomon * was removed. After that, the device used the Advanced Mode GS/PT streaming 28df08ef27SAndres Salomon * stuff. That turned out to be too buggy to support, so we've finally 29df08ef27SAndres Salomon * switched to Mouse Mode (which utilizes only the center 1/3 of the touchpad). 30df08ef27SAndres Salomon */ 31df08ef27SAndres Salomon 32df08ef27SAndres Salomon #define DEBUG 335a0e3ad6STejun Heo #include <linux/slab.h> 34df08ef27SAndres Salomon #include <linux/input.h> 35df08ef27SAndres Salomon #include <linux/serio.h> 36df08ef27SAndres Salomon #include <linux/libps2.h> 37df08ef27SAndres Salomon #include <linux/delay.h> 38df08ef27SAndres Salomon #include <asm/olpc.h> 39df08ef27SAndres Salomon 40df08ef27SAndres Salomon #include "psmouse.h" 41df08ef27SAndres Salomon #include "hgpk.h" 42df08ef27SAndres Salomon 43*a309cdc7SDaniel Drake #define ILLEGAL_XY 999999 44*a309cdc7SDaniel Drake 45a62f0d27SDmitry Torokhov static bool tpdebug; 46a62f0d27SDmitry Torokhov module_param(tpdebug, bool, 0644); 47df08ef27SAndres Salomon MODULE_PARM_DESC(tpdebug, "enable debugging, dumping packets to KERN_DEBUG."); 48df08ef27SAndres Salomon 49df08ef27SAndres Salomon static int recalib_delta = 100; 50df08ef27SAndres Salomon module_param(recalib_delta, int, 0644); 51df08ef27SAndres Salomon MODULE_PARM_DESC(recalib_delta, 52*a309cdc7SDaniel Drake "packets containing a delta this large will be discarded, and a " 53*a309cdc7SDaniel Drake "recalibration may be scheduled."); 54df08ef27SAndres Salomon 55*a309cdc7SDaniel Drake static int jumpy_delay = 20; 568bbf2703SPaul Fox module_param(jumpy_delay, int, 0644); 578bbf2703SPaul Fox MODULE_PARM_DESC(jumpy_delay, 588bbf2703SPaul Fox "delay (ms) before recal after jumpiness detected"); 598bbf2703SPaul Fox 60c0dc8342SDaniel Drake static int spew_delay = 1; 618bbf2703SPaul Fox module_param(spew_delay, int, 0644); 628bbf2703SPaul Fox MODULE_PARM_DESC(spew_delay, 638bbf2703SPaul Fox "delay (ms) before recal after packet spew detected"); 648bbf2703SPaul Fox 658bbf2703SPaul Fox static int recal_guard_time = 2000; 668bbf2703SPaul Fox module_param(recal_guard_time, int, 0644); 678bbf2703SPaul Fox MODULE_PARM_DESC(recal_guard_time, 688bbf2703SPaul Fox "interval (ms) during which recal will be restarted if packet received"); 698bbf2703SPaul Fox 708bbf2703SPaul Fox static int post_interrupt_delay = 1000; 718bbf2703SPaul Fox module_param(post_interrupt_delay, int, 0644); 728bbf2703SPaul Fox MODULE_PARM_DESC(post_interrupt_delay, 738bbf2703SPaul Fox "delay (ms) before recal after recal interrupt detected"); 748bbf2703SPaul Fox 75ca94ec43SDaniel Drake static char hgpk_mode_name[16]; 76ca94ec43SDaniel Drake module_param_string(hgpk_mode, hgpk_mode_name, sizeof(hgpk_mode_name), 0644); 77ca94ec43SDaniel Drake MODULE_PARM_DESC(hgpk_mode, 78ca94ec43SDaniel Drake "default hgpk mode: mouse, glidesensor or pentablet"); 79ca94ec43SDaniel Drake 80ca94ec43SDaniel Drake static int hgpk_default_mode = HGPK_MODE_MOUSE; 81ca94ec43SDaniel Drake 82ca94ec43SDaniel Drake static const char * const hgpk_mode_names[] = { 83ca94ec43SDaniel Drake [HGPK_MODE_MOUSE] = "Mouse", 84ca94ec43SDaniel Drake [HGPK_MODE_GLIDESENSOR] = "GlideSensor", 85ca94ec43SDaniel Drake [HGPK_MODE_PENTABLET] = "PenTablet", 86ca94ec43SDaniel Drake }; 87ca94ec43SDaniel Drake 88ca94ec43SDaniel Drake static int hgpk_mode_from_name(const char *buf, int len) 89ca94ec43SDaniel Drake { 90ca94ec43SDaniel Drake int i; 91ca94ec43SDaniel Drake 92ca94ec43SDaniel Drake for (i = 0; i < ARRAY_SIZE(hgpk_mode_names); i++) { 93ca94ec43SDaniel Drake const char *name = hgpk_mode_names[i]; 94ca94ec43SDaniel Drake if (strlen(name) == len && !strncasecmp(name, buf, len)) 95ca94ec43SDaniel Drake return i; 96ca94ec43SDaniel Drake } 97ca94ec43SDaniel Drake 98ca94ec43SDaniel Drake return HGPK_MODE_INVALID; 99ca94ec43SDaniel Drake } 100ca94ec43SDaniel Drake 101df08ef27SAndres Salomon /* 102*a309cdc7SDaniel Drake * see if new value is within 20% of half of old value 103df08ef27SAndres Salomon */ 104*a309cdc7SDaniel Drake static int approx_half(int curr, int prev) 105*a309cdc7SDaniel Drake { 106*a309cdc7SDaniel Drake int belowhalf, abovehalf; 107*a309cdc7SDaniel Drake 108*a309cdc7SDaniel Drake if (curr < 5 || prev < 5) 109*a309cdc7SDaniel Drake return 0; 110*a309cdc7SDaniel Drake 111*a309cdc7SDaniel Drake belowhalf = (prev * 8) / 20; 112*a309cdc7SDaniel Drake abovehalf = (prev * 12) / 20; 113*a309cdc7SDaniel Drake 114*a309cdc7SDaniel Drake return belowhalf < curr && curr <= abovehalf; 115*a309cdc7SDaniel Drake } 116*a309cdc7SDaniel Drake 117*a309cdc7SDaniel Drake /* 118*a309cdc7SDaniel Drake * Throw out oddly large delta packets, and any that immediately follow whose 119*a309cdc7SDaniel Drake * values are each approximately half of the previous. It seems that the ALPS 120*a309cdc7SDaniel Drake * firmware emits errant packets, and they get averaged out slowly. 121*a309cdc7SDaniel Drake */ 122*a309cdc7SDaniel Drake static int hgpk_discard_decay_hack(struct psmouse *psmouse, int x, int y) 123df08ef27SAndres Salomon { 124df08ef27SAndres Salomon struct hgpk_data *priv = psmouse->private; 125*a309cdc7SDaniel Drake int avx, avy; 126*a309cdc7SDaniel Drake bool do_recal = false; 127df08ef27SAndres Salomon 128*a309cdc7SDaniel Drake avx = abs(x); 129*a309cdc7SDaniel Drake avy = abs(y); 130*a309cdc7SDaniel Drake 131*a309cdc7SDaniel Drake /* discard if too big, or half that but > 4 times the prev delta */ 132*a309cdc7SDaniel Drake if (avx > recalib_delta || 133*a309cdc7SDaniel Drake (avx > recalib_delta / 2 && ((avx / 4) > priv->xlast))) { 134*a309cdc7SDaniel Drake hgpk_err(psmouse, "detected %dpx jump in x\n", x); 135*a309cdc7SDaniel Drake priv->xbigj = avx; 136*a309cdc7SDaniel Drake } else if (approx_half(avx, priv->xbigj)) { 137*a309cdc7SDaniel Drake hgpk_err(psmouse, "detected secondary %dpx jump in x\n", x); 138*a309cdc7SDaniel Drake priv->xbigj = avx; 139*a309cdc7SDaniel Drake priv->xsaw_secondary++; 140*a309cdc7SDaniel Drake } else { 141*a309cdc7SDaniel Drake if (priv->xbigj && priv->xsaw_secondary > 1) 142*a309cdc7SDaniel Drake do_recal = true; 143*a309cdc7SDaniel Drake priv->xbigj = 0; 144*a309cdc7SDaniel Drake priv->xsaw_secondary = 0; 145*a309cdc7SDaniel Drake } 146*a309cdc7SDaniel Drake 147*a309cdc7SDaniel Drake if (avy > recalib_delta || 148*a309cdc7SDaniel Drake (avy > recalib_delta / 2 && ((avy / 4) > priv->ylast))) { 149*a309cdc7SDaniel Drake hgpk_err(psmouse, "detected %dpx jump in y\n", y); 150*a309cdc7SDaniel Drake priv->ybigj = avy; 151*a309cdc7SDaniel Drake } else if (approx_half(avy, priv->ybigj)) { 152*a309cdc7SDaniel Drake hgpk_err(psmouse, "detected secondary %dpx jump in y\n", y); 153*a309cdc7SDaniel Drake priv->ybigj = avy; 154*a309cdc7SDaniel Drake priv->ysaw_secondary++; 155*a309cdc7SDaniel Drake } else { 156*a309cdc7SDaniel Drake if (priv->ybigj && priv->ysaw_secondary > 1) 157*a309cdc7SDaniel Drake do_recal = true; 158*a309cdc7SDaniel Drake priv->ybigj = 0; 159*a309cdc7SDaniel Drake priv->ysaw_secondary = 0; 160*a309cdc7SDaniel Drake } 161*a309cdc7SDaniel Drake 162*a309cdc7SDaniel Drake priv->xlast = avx; 163*a309cdc7SDaniel Drake priv->ylast = avy; 164*a309cdc7SDaniel Drake 165*a309cdc7SDaniel Drake if (do_recal && jumpy_delay) { 166*a309cdc7SDaniel Drake hgpk_err(psmouse, "scheduling recalibration\n"); 167df08ef27SAndres Salomon psmouse_queue_work(psmouse, &priv->recalib_wq, 1688bbf2703SPaul Fox msecs_to_jiffies(jumpy_delay)); 169df08ef27SAndres Salomon } 170*a309cdc7SDaniel Drake 171*a309cdc7SDaniel Drake return priv->xbigj || priv->ybigj; 172df08ef27SAndres Salomon } 173df08ef27SAndres Salomon 174c0dc8342SDaniel Drake static void hgpk_reset_spew_detection(struct hgpk_data *priv) 175c0dc8342SDaniel Drake { 176c0dc8342SDaniel Drake priv->spew_count = 0; 177c0dc8342SDaniel Drake priv->dupe_count = 0; 178c0dc8342SDaniel Drake priv->x_tally = 0; 179c0dc8342SDaniel Drake priv->y_tally = 0; 180c0dc8342SDaniel Drake priv->spew_flag = NO_SPEW; 181c0dc8342SDaniel Drake } 182c0dc8342SDaniel Drake 183c0dc8342SDaniel Drake static void hgpk_reset_hack_state(struct psmouse *psmouse) 184c0dc8342SDaniel Drake { 185c0dc8342SDaniel Drake struct hgpk_data *priv = psmouse->private; 186c0dc8342SDaniel Drake 187c0dc8342SDaniel Drake priv->abs_x = priv->abs_y = -1; 188*a309cdc7SDaniel Drake priv->xlast = priv->ylast = ILLEGAL_XY; 189*a309cdc7SDaniel Drake priv->xbigj = priv->ybigj = 0; 190*a309cdc7SDaniel Drake priv->xsaw_secondary = priv->ysaw_secondary = 0; 191c0dc8342SDaniel Drake hgpk_reset_spew_detection(priv); 192c0dc8342SDaniel Drake } 193c0dc8342SDaniel Drake 194df08ef27SAndres Salomon /* 195df08ef27SAndres Salomon * We have no idea why this particular hardware bug occurs. The touchpad 196df08ef27SAndres Salomon * will randomly start spewing packets without anything touching the 197df08ef27SAndres Salomon * pad. This wouldn't necessarily be bad, but it's indicative of a 198df08ef27SAndres Salomon * severely miscalibrated pad; attempting to use the touchpad while it's 199df08ef27SAndres Salomon * spewing means the cursor will jump all over the place, and act "drunk". 200df08ef27SAndres Salomon * 201df08ef27SAndres Salomon * The packets that are spewed tend to all have deltas between -2 and 2, and 202df08ef27SAndres Salomon * the cursor will move around without really going very far. It will 203df08ef27SAndres Salomon * tend to end up in the same location; if we tally up the changes over 204df08ef27SAndres Salomon * 100 packets, we end up w/ a final delta of close to 0. This happens 205df08ef27SAndres Salomon * pretty regularly when the touchpad is spewing, and is pretty hard to 206df08ef27SAndres Salomon * manually trigger (at least for *my* fingers). So, it makes a perfect 207df08ef27SAndres Salomon * scheme for detecting spews. 208df08ef27SAndres Salomon */ 209df08ef27SAndres Salomon static void hgpk_spewing_hack(struct psmouse *psmouse, 210df08ef27SAndres Salomon int l, int r, int x, int y) 211df08ef27SAndres Salomon { 212df08ef27SAndres Salomon struct hgpk_data *priv = psmouse->private; 213df08ef27SAndres Salomon 214df08ef27SAndres Salomon /* ignore button press packets; many in a row could trigger 215df08ef27SAndres Salomon * a false-positive! */ 216df08ef27SAndres Salomon if (l || r) 217df08ef27SAndres Salomon return; 218df08ef27SAndres Salomon 219c0dc8342SDaniel Drake /* don't track spew if the workaround feature has been turned off */ 220c0dc8342SDaniel Drake if (!spew_delay) 221c0dc8342SDaniel Drake return; 222c0dc8342SDaniel Drake 223c0dc8342SDaniel Drake if (abs(x) > 3 || abs(y) > 3) { 224c0dc8342SDaniel Drake /* no spew, or spew ended */ 225c0dc8342SDaniel Drake hgpk_reset_spew_detection(priv); 226c0dc8342SDaniel Drake return; 227c0dc8342SDaniel Drake } 228c0dc8342SDaniel Drake 229c0dc8342SDaniel Drake /* Keep a tally of the overall delta to the cursor position caused by 230c0dc8342SDaniel Drake * the spew */ 231df08ef27SAndres Salomon priv->x_tally += x; 232df08ef27SAndres Salomon priv->y_tally += y; 233df08ef27SAndres Salomon 234c0dc8342SDaniel Drake switch (priv->spew_flag) { 235c0dc8342SDaniel Drake case NO_SPEW: 236c0dc8342SDaniel Drake /* we're not spewing, but this packet might be the start */ 237c0dc8342SDaniel Drake priv->spew_flag = MAYBE_SPEWING; 238c0dc8342SDaniel Drake 239c0dc8342SDaniel Drake /* fall-through */ 240c0dc8342SDaniel Drake 241c0dc8342SDaniel Drake case MAYBE_SPEWING: 242c0dc8342SDaniel Drake priv->spew_count++; 243c0dc8342SDaniel Drake 244c0dc8342SDaniel Drake if (priv->spew_count < SPEW_WATCH_COUNT) 245c0dc8342SDaniel Drake break; 246c0dc8342SDaniel Drake 247c0dc8342SDaniel Drake /* excessive spew detected, request recalibration */ 248c0dc8342SDaniel Drake priv->spew_flag = SPEW_DETECTED; 249c0dc8342SDaniel Drake 250c0dc8342SDaniel Drake /* fall-through */ 251c0dc8342SDaniel Drake 252c0dc8342SDaniel Drake case SPEW_DETECTED: 253c0dc8342SDaniel Drake /* only recalibrate when the overall delta to the cursor 254c0dc8342SDaniel Drake * is really small. if the spew is causing significant cursor 255c0dc8342SDaniel Drake * movement, it is probably a case of the user moving the 256c0dc8342SDaniel Drake * cursor very slowly across the screen. */ 257df08ef27SAndres Salomon if (abs(priv->x_tally) < 3 && abs(priv->y_tally) < 3) { 258c0dc8342SDaniel Drake hgpk_err(psmouse, "packet spew detected (%d,%d)\n", 259df08ef27SAndres Salomon priv->x_tally, priv->y_tally); 260c0dc8342SDaniel Drake priv->spew_flag = RECALIBRATING; 261df08ef27SAndres Salomon psmouse_queue_work(psmouse, &priv->recalib_wq, 2628bbf2703SPaul Fox msecs_to_jiffies(spew_delay)); 263df08ef27SAndres Salomon } 264c0dc8342SDaniel Drake 265c0dc8342SDaniel Drake break; 266c0dc8342SDaniel Drake case RECALIBRATING: 267c0dc8342SDaniel Drake /* we already detected a spew and requested a recalibration, 268c0dc8342SDaniel Drake * just wait for the queue to kick into action. */ 269c0dc8342SDaniel Drake break; 270df08ef27SAndres Salomon } 271df08ef27SAndres Salomon } 272df08ef27SAndres Salomon 273df08ef27SAndres Salomon /* 274df08ef27SAndres Salomon * HGPK Mouse Mode format (standard mouse format, sans middle button) 275df08ef27SAndres Salomon * 276df08ef27SAndres Salomon * byte 0: y-over x-over y-neg x-neg 1 0 swr swl 277df08ef27SAndres Salomon * byte 1: x7 x6 x5 x4 x3 x2 x1 x0 278df08ef27SAndres Salomon * byte 2: y7 y6 y5 y4 y3 y2 y1 y0 279df08ef27SAndres Salomon * 280df08ef27SAndres Salomon * swr/swl are the left/right buttons. 281df08ef27SAndres Salomon * x-neg/y-neg are the x and y delta negative bits 282df08ef27SAndres Salomon * x-over/y-over are the x and y overflow bits 283ca94ec43SDaniel Drake * 284ca94ec43SDaniel Drake * --- 285ca94ec43SDaniel Drake * 286ca94ec43SDaniel Drake * HGPK Advanced Mode - single-mode format 287ca94ec43SDaniel Drake * 288ca94ec43SDaniel Drake * byte 0(PT): 1 1 0 0 1 1 1 1 289ca94ec43SDaniel Drake * byte 0(GS): 1 1 1 1 1 1 1 1 290ca94ec43SDaniel Drake * byte 1: 0 x6 x5 x4 x3 x2 x1 x0 291ca94ec43SDaniel Drake * byte 2(PT): 0 0 x9 x8 x7 ? pt-dsw 0 292ca94ec43SDaniel Drake * byte 2(GS): 0 x10 x9 x8 x7 ? gs-dsw pt-dsw 293ca94ec43SDaniel Drake * byte 3: 0 y9 y8 y7 1 0 swr swl 294ca94ec43SDaniel Drake * byte 4: 0 y6 y5 y4 y3 y2 y1 y0 295ca94ec43SDaniel Drake * byte 5: 0 z6 z5 z4 z3 z2 z1 z0 296ca94ec43SDaniel Drake * 297ca94ec43SDaniel Drake * ?'s are not defined in the protocol spec, may vary between models. 298ca94ec43SDaniel Drake * 299ca94ec43SDaniel Drake * swr/swl are the left/right buttons. 300ca94ec43SDaniel Drake * 301ca94ec43SDaniel Drake * pt-dsw/gs-dsw indicate that the pt/gs sensor is detecting a 302ca94ec43SDaniel Drake * pen/finger 303df08ef27SAndres Salomon */ 304ca94ec43SDaniel Drake static bool hgpk_is_byte_valid(struct psmouse *psmouse, unsigned char *packet) 305df08ef27SAndres Salomon { 306ca94ec43SDaniel Drake struct hgpk_data *priv = psmouse->private; 307ca94ec43SDaniel Drake int pktcnt = psmouse->pktcnt; 308ca94ec43SDaniel Drake bool valid; 309ca94ec43SDaniel Drake 310ca94ec43SDaniel Drake switch (priv->mode) { 311ca94ec43SDaniel Drake case HGPK_MODE_MOUSE: 312ca94ec43SDaniel Drake valid = (packet[0] & 0x0C) == 0x08; 313ca94ec43SDaniel Drake break; 314ca94ec43SDaniel Drake 315ca94ec43SDaniel Drake case HGPK_MODE_GLIDESENSOR: 316ca94ec43SDaniel Drake valid = pktcnt == 1 ? 317ca94ec43SDaniel Drake packet[0] == HGPK_GS : !(packet[pktcnt - 1] & 0x80); 318ca94ec43SDaniel Drake break; 319ca94ec43SDaniel Drake 320ca94ec43SDaniel Drake case HGPK_MODE_PENTABLET: 321ca94ec43SDaniel Drake valid = pktcnt == 1 ? 322ca94ec43SDaniel Drake packet[0] == HGPK_PT : !(packet[pktcnt - 1] & 0x80); 323ca94ec43SDaniel Drake break; 324ca94ec43SDaniel Drake 325ca94ec43SDaniel Drake default: 326ca94ec43SDaniel Drake valid = false; 327ca94ec43SDaniel Drake break; 328df08ef27SAndres Salomon } 329df08ef27SAndres Salomon 330ca94ec43SDaniel Drake if (!valid) 331ca94ec43SDaniel Drake hgpk_dbg(psmouse, 332ca94ec43SDaniel Drake "bad data, mode %d (%d) %02x %02x %02x %02x %02x %02x\n", 333ca94ec43SDaniel Drake priv->mode, pktcnt, 334ca94ec43SDaniel Drake psmouse->packet[0], psmouse->packet[1], 335ca94ec43SDaniel Drake psmouse->packet[2], psmouse->packet[3], 336ca94ec43SDaniel Drake psmouse->packet[4], psmouse->packet[5]); 337ca94ec43SDaniel Drake 338ca94ec43SDaniel Drake return valid; 339ca94ec43SDaniel Drake } 340ca94ec43SDaniel Drake 341ca94ec43SDaniel Drake static void hgpk_process_advanced_packet(struct psmouse *psmouse) 342ca94ec43SDaniel Drake { 343ca94ec43SDaniel Drake struct hgpk_data *priv = psmouse->private; 344ca94ec43SDaniel Drake struct input_dev *idev = psmouse->dev; 345ca94ec43SDaniel Drake unsigned char *packet = psmouse->packet; 346ca94ec43SDaniel Drake int down = !!(packet[2] & 2); 347ca94ec43SDaniel Drake int left = !!(packet[3] & 1); 348ca94ec43SDaniel Drake int right = !!(packet[3] & 2); 349ca94ec43SDaniel Drake int x = packet[1] | ((packet[2] & 0x78) << 4); 350ca94ec43SDaniel Drake int y = packet[4] | ((packet[3] & 0x70) << 3); 351ca94ec43SDaniel Drake 352ca94ec43SDaniel Drake if (priv->mode == HGPK_MODE_GLIDESENSOR) { 353ca94ec43SDaniel Drake int pt_down = !!(packet[2] & 1); 354ca94ec43SDaniel Drake int finger_down = !!(packet[2] & 2); 355ca94ec43SDaniel Drake int z = packet[5]; 356ca94ec43SDaniel Drake 357ca94ec43SDaniel Drake input_report_abs(idev, ABS_PRESSURE, z); 358ca94ec43SDaniel Drake if (tpdebug) 359ca94ec43SDaniel Drake hgpk_dbg(psmouse, "pd=%d fd=%d z=%d", 360ca94ec43SDaniel Drake pt_down, finger_down, z); 361ca94ec43SDaniel Drake } else { 362ca94ec43SDaniel Drake /* 363ca94ec43SDaniel Drake * PenTablet mode does not report pressure, so we don't 364ca94ec43SDaniel Drake * report it here 365ca94ec43SDaniel Drake */ 366ca94ec43SDaniel Drake if (tpdebug) 367ca94ec43SDaniel Drake hgpk_dbg(psmouse, "pd=%d ", down); 368ca94ec43SDaniel Drake } 369ca94ec43SDaniel Drake 370ca94ec43SDaniel Drake if (tpdebug) 371ca94ec43SDaniel Drake hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y); 372ca94ec43SDaniel Drake 373ca94ec43SDaniel Drake input_report_key(idev, BTN_TOUCH, down); 374ca94ec43SDaniel Drake input_report_key(idev, BTN_LEFT, left); 375ca94ec43SDaniel Drake input_report_key(idev, BTN_RIGHT, right); 376ca94ec43SDaniel Drake 377ca94ec43SDaniel Drake /* 378ca94ec43SDaniel Drake * If this packet says that the finger was removed, reset our position 379ca94ec43SDaniel Drake * tracking so that we don't erroneously detect a jump on next press. 380ca94ec43SDaniel Drake */ 381c0dc8342SDaniel Drake if (!down) { 382*a309cdc7SDaniel Drake hgpk_reset_hack_state(psmouse); 383c0dc8342SDaniel Drake goto done; 384c0dc8342SDaniel Drake } 385ca94ec43SDaniel Drake 386ca94ec43SDaniel Drake /* 387c0dc8342SDaniel Drake * Weed out duplicate packets (we get quite a few, and they mess up 388c0dc8342SDaniel Drake * our jump detection) 389ca94ec43SDaniel Drake */ 390c0dc8342SDaniel Drake if (x == priv->abs_x && y == priv->abs_y) { 391c0dc8342SDaniel Drake if (++priv->dupe_count > SPEW_WATCH_COUNT) { 392c0dc8342SDaniel Drake if (tpdebug) 393c0dc8342SDaniel Drake hgpk_dbg(psmouse, "hard spew detected\n"); 394c0dc8342SDaniel Drake priv->spew_flag = RECALIBRATING; 395c0dc8342SDaniel Drake psmouse_queue_work(psmouse, &priv->recalib_wq, 396c0dc8342SDaniel Drake msecs_to_jiffies(spew_delay)); 397c0dc8342SDaniel Drake } 398c0dc8342SDaniel Drake goto done; 399c0dc8342SDaniel Drake } 400c0dc8342SDaniel Drake 401c0dc8342SDaniel Drake /* not a duplicate, continue with position reporting */ 402c0dc8342SDaniel Drake priv->dupe_count = 0; 403ca94ec43SDaniel Drake 404ca94ec43SDaniel Drake /* Don't apply hacks in PT mode, it seems reliable */ 405ca94ec43SDaniel Drake if (priv->mode != HGPK_MODE_PENTABLET && priv->abs_x != -1) { 406*a309cdc7SDaniel Drake int x_diff = priv->abs_x - x; 407*a309cdc7SDaniel Drake int y_diff = priv->abs_y - y; 408*a309cdc7SDaniel Drake if (hgpk_discard_decay_hack(psmouse, x_diff, y_diff)) { 409*a309cdc7SDaniel Drake if (tpdebug) 410*a309cdc7SDaniel Drake hgpk_dbg(psmouse, "discarding\n"); 411*a309cdc7SDaniel Drake goto done; 412*a309cdc7SDaniel Drake } 413*a309cdc7SDaniel Drake hgpk_spewing_hack(psmouse, left, right, x_diff, y_diff); 414ca94ec43SDaniel Drake } 415ca94ec43SDaniel Drake 416ca94ec43SDaniel Drake input_report_abs(idev, ABS_X, x); 417ca94ec43SDaniel Drake input_report_abs(idev, ABS_Y, y); 418ca94ec43SDaniel Drake priv->abs_x = x; 419ca94ec43SDaniel Drake priv->abs_y = y; 420ca94ec43SDaniel Drake 421c0dc8342SDaniel Drake done: 422ca94ec43SDaniel Drake input_sync(idev); 423ca94ec43SDaniel Drake } 424ca94ec43SDaniel Drake 425ca94ec43SDaniel Drake static void hgpk_process_simple_packet(struct psmouse *psmouse) 426df08ef27SAndres Salomon { 427df08ef27SAndres Salomon struct input_dev *dev = psmouse->dev; 428df08ef27SAndres Salomon unsigned char *packet = psmouse->packet; 429ca94ec43SDaniel Drake int left = packet[0] & 1; 430ca94ec43SDaniel Drake int right = (packet[0] >> 1) & 1; 431ca94ec43SDaniel Drake int x = packet[1] - ((packet[0] << 4) & 0x100); 432ca94ec43SDaniel Drake int y = ((packet[0] << 3) & 0x100) - packet[2]; 433df08ef27SAndres Salomon 434*a309cdc7SDaniel Drake if (hgpk_discard_decay_hack(psmouse, x, y)) { 435*a309cdc7SDaniel Drake if (tpdebug) 436*a309cdc7SDaniel Drake hgpk_dbg(psmouse, "discarding\n"); 437*a309cdc7SDaniel Drake return; 438*a309cdc7SDaniel Drake } 439*a309cdc7SDaniel Drake 440df08ef27SAndres Salomon hgpk_spewing_hack(psmouse, left, right, x, y); 441df08ef27SAndres Salomon 442df08ef27SAndres Salomon if (tpdebug) 443df08ef27SAndres Salomon hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y); 444df08ef27SAndres Salomon 445df08ef27SAndres Salomon input_report_key(dev, BTN_LEFT, left); 446df08ef27SAndres Salomon input_report_key(dev, BTN_RIGHT, right); 447df08ef27SAndres Salomon 448df08ef27SAndres Salomon input_report_rel(dev, REL_X, x); 449df08ef27SAndres Salomon input_report_rel(dev, REL_Y, y); 450df08ef27SAndres Salomon 451df08ef27SAndres Salomon input_sync(dev); 452df08ef27SAndres Salomon } 453df08ef27SAndres Salomon 454df08ef27SAndres Salomon static psmouse_ret_t hgpk_process_byte(struct psmouse *psmouse) 455df08ef27SAndres Salomon { 456df08ef27SAndres Salomon struct hgpk_data *priv = psmouse->private; 457df08ef27SAndres Salomon 458ca94ec43SDaniel Drake if (!hgpk_is_byte_valid(psmouse, psmouse->packet)) 459df08ef27SAndres Salomon return PSMOUSE_BAD_DATA; 460df08ef27SAndres Salomon 461df08ef27SAndres Salomon if (psmouse->pktcnt >= psmouse->pktsize) { 462ca94ec43SDaniel Drake if (priv->mode == HGPK_MODE_MOUSE) 463ca94ec43SDaniel Drake hgpk_process_simple_packet(psmouse); 464ca94ec43SDaniel Drake else 465ca94ec43SDaniel Drake hgpk_process_advanced_packet(psmouse); 466df08ef27SAndres Salomon return PSMOUSE_FULL_PACKET; 467df08ef27SAndres Salomon } 468df08ef27SAndres Salomon 469df08ef27SAndres Salomon if (priv->recalib_window) { 470df08ef27SAndres Salomon if (time_before(jiffies, priv->recalib_window)) { 471df08ef27SAndres Salomon /* 472df08ef27SAndres Salomon * ugh, got a packet inside our recalibration 473df08ef27SAndres Salomon * window, schedule another recalibration. 474df08ef27SAndres Salomon */ 475df08ef27SAndres Salomon hgpk_dbg(psmouse, 476df08ef27SAndres Salomon "packet inside calibration window, " 477df08ef27SAndres Salomon "queueing another recalibration\n"); 478df08ef27SAndres Salomon psmouse_queue_work(psmouse, &priv->recalib_wq, 4798bbf2703SPaul Fox msecs_to_jiffies(post_interrupt_delay)); 480df08ef27SAndres Salomon } 481df08ef27SAndres Salomon priv->recalib_window = 0; 482df08ef27SAndres Salomon } 483df08ef27SAndres Salomon 484df08ef27SAndres Salomon return PSMOUSE_GOOD_DATA; 485df08ef27SAndres Salomon } 486df08ef27SAndres Salomon 487ca94ec43SDaniel Drake static int hgpk_select_mode(struct psmouse *psmouse) 488df08ef27SAndres Salomon { 489df08ef27SAndres Salomon struct ps2dev *ps2dev = &psmouse->ps2dev; 490df08ef27SAndres Salomon struct hgpk_data *priv = psmouse->private; 491ca94ec43SDaniel Drake int i; 492ca94ec43SDaniel Drake int cmd; 493df08ef27SAndres Salomon 494ca94ec43SDaniel Drake /* 495ca94ec43SDaniel Drake * 4 disables to enable advanced mode 496ca94ec43SDaniel Drake * then 3 0xf2 bytes as the preamble for GS/PT selection 497ca94ec43SDaniel Drake */ 498ca94ec43SDaniel Drake const int advanced_init[] = { 499ca94ec43SDaniel Drake PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE, 500ca94ec43SDaniel Drake PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE, 501ca94ec43SDaniel Drake 0xf2, 0xf2, 0xf2, 502ca94ec43SDaniel Drake }; 503ca94ec43SDaniel Drake 504ca94ec43SDaniel Drake switch (priv->mode) { 505ca94ec43SDaniel Drake case HGPK_MODE_MOUSE: 506ca94ec43SDaniel Drake psmouse->pktsize = 3; 507ca94ec43SDaniel Drake break; 508ca94ec43SDaniel Drake 509ca94ec43SDaniel Drake case HGPK_MODE_GLIDESENSOR: 510ca94ec43SDaniel Drake case HGPK_MODE_PENTABLET: 511ca94ec43SDaniel Drake psmouse->pktsize = 6; 512ca94ec43SDaniel Drake 513ca94ec43SDaniel Drake /* Switch to 'Advanced mode.', four disables in a row. */ 514ca94ec43SDaniel Drake for (i = 0; i < ARRAY_SIZE(advanced_init); i++) 515ca94ec43SDaniel Drake if (ps2_command(ps2dev, NULL, advanced_init[i])) 516ca94ec43SDaniel Drake return -EIO; 517ca94ec43SDaniel Drake 518ca94ec43SDaniel Drake /* select between GlideSensor (mouse) or PenTablet */ 519ca94ec43SDaniel Drake cmd = priv->mode == HGPK_MODE_GLIDESENSOR ? 520ca94ec43SDaniel Drake PSMOUSE_CMD_SETSCALE11 : PSMOUSE_CMD_SETSCALE21; 521ca94ec43SDaniel Drake 522ca94ec43SDaniel Drake if (ps2_command(ps2dev, NULL, cmd)) 523ca94ec43SDaniel Drake return -EIO; 524ca94ec43SDaniel Drake break; 525ca94ec43SDaniel Drake 526ca94ec43SDaniel Drake default: 527ca94ec43SDaniel Drake return -EINVAL; 528ca94ec43SDaniel Drake } 529ca94ec43SDaniel Drake 530df08ef27SAndres Salomon return 0; 531ca94ec43SDaniel Drake } 532df08ef27SAndres Salomon 533ca94ec43SDaniel Drake static void hgpk_setup_input_device(struct input_dev *input, 534ca94ec43SDaniel Drake struct input_dev *old_input, 535ca94ec43SDaniel Drake enum hgpk_mode mode) 536ca94ec43SDaniel Drake { 537ca94ec43SDaniel Drake if (old_input) { 538ca94ec43SDaniel Drake input->name = old_input->name; 539ca94ec43SDaniel Drake input->phys = old_input->phys; 540ca94ec43SDaniel Drake input->id = old_input->id; 541ca94ec43SDaniel Drake input->dev.parent = old_input->dev.parent; 542ca94ec43SDaniel Drake } 543df08ef27SAndres Salomon 544ca94ec43SDaniel Drake memset(input->evbit, 0, sizeof(input->evbit)); 545ca94ec43SDaniel Drake memset(input->relbit, 0, sizeof(input->relbit)); 546ca94ec43SDaniel Drake memset(input->keybit, 0, sizeof(input->keybit)); 547ca94ec43SDaniel Drake 548ca94ec43SDaniel Drake /* All modes report left and right buttons */ 549ca94ec43SDaniel Drake __set_bit(EV_KEY, input->evbit); 550ca94ec43SDaniel Drake __set_bit(BTN_LEFT, input->keybit); 551ca94ec43SDaniel Drake __set_bit(BTN_RIGHT, input->keybit); 552ca94ec43SDaniel Drake 553ca94ec43SDaniel Drake switch (mode) { 554ca94ec43SDaniel Drake case HGPK_MODE_MOUSE: 555ca94ec43SDaniel Drake __set_bit(EV_REL, input->evbit); 556ca94ec43SDaniel Drake __set_bit(REL_X, input->relbit); 557ca94ec43SDaniel Drake __set_bit(REL_Y, input->relbit); 558ca94ec43SDaniel Drake break; 559ca94ec43SDaniel Drake 560ca94ec43SDaniel Drake case HGPK_MODE_GLIDESENSOR: 561ca94ec43SDaniel Drake __set_bit(BTN_TOUCH, input->keybit); 562ca94ec43SDaniel Drake __set_bit(BTN_TOOL_FINGER, input->keybit); 563ca94ec43SDaniel Drake 564ca94ec43SDaniel Drake __set_bit(EV_ABS, input->evbit); 565ca94ec43SDaniel Drake 566ca94ec43SDaniel Drake /* GlideSensor has pressure sensor, PenTablet does not */ 567ca94ec43SDaniel Drake input_set_abs_params(input, ABS_PRESSURE, 0, 15, 0, 0); 568ca94ec43SDaniel Drake 569ca94ec43SDaniel Drake /* From device specs */ 570ca94ec43SDaniel Drake input_set_abs_params(input, ABS_X, 0, 399, 0, 0); 571ca94ec43SDaniel Drake input_set_abs_params(input, ABS_Y, 0, 290, 0, 0); 572ca94ec43SDaniel Drake 573ca94ec43SDaniel Drake /* Calculated by hand based on usable size (52mm x 38mm) */ 574ca94ec43SDaniel Drake input_abs_set_res(input, ABS_X, 8); 575ca94ec43SDaniel Drake input_abs_set_res(input, ABS_Y, 8); 576ca94ec43SDaniel Drake break; 577ca94ec43SDaniel Drake 578ca94ec43SDaniel Drake case HGPK_MODE_PENTABLET: 579ca94ec43SDaniel Drake __set_bit(BTN_TOUCH, input->keybit); 580ca94ec43SDaniel Drake __set_bit(BTN_TOOL_FINGER, input->keybit); 581ca94ec43SDaniel Drake 582ca94ec43SDaniel Drake __set_bit(EV_ABS, input->evbit); 583ca94ec43SDaniel Drake 584ca94ec43SDaniel Drake /* From device specs */ 585ca94ec43SDaniel Drake input_set_abs_params(input, ABS_X, 0, 999, 0, 0); 586ca94ec43SDaniel Drake input_set_abs_params(input, ABS_Y, 5, 239, 0, 0); 587ca94ec43SDaniel Drake 588ca94ec43SDaniel Drake /* Calculated by hand based on usable size (156mm x 38mm) */ 589ca94ec43SDaniel Drake input_abs_set_res(input, ABS_X, 6); 590ca94ec43SDaniel Drake input_abs_set_res(input, ABS_Y, 8); 591ca94ec43SDaniel Drake break; 592ca94ec43SDaniel Drake 593ca94ec43SDaniel Drake default: 594ca94ec43SDaniel Drake BUG(); 595ca94ec43SDaniel Drake } 596ca94ec43SDaniel Drake } 597ca94ec43SDaniel Drake 598ca94ec43SDaniel Drake static int hgpk_reset_device(struct psmouse *psmouse, bool recalibrate) 599ca94ec43SDaniel Drake { 600ca94ec43SDaniel Drake int err; 601ca94ec43SDaniel Drake 602df08ef27SAndres Salomon psmouse_reset(psmouse); 603df08ef27SAndres Salomon 604ca94ec43SDaniel Drake if (recalibrate) { 605ca94ec43SDaniel Drake struct ps2dev *ps2dev = &psmouse->ps2dev; 606ca94ec43SDaniel Drake 607df08ef27SAndres Salomon /* send the recalibrate request */ 608df08ef27SAndres Salomon if (ps2_command(ps2dev, NULL, 0xf5) || 609df08ef27SAndres Salomon ps2_command(ps2dev, NULL, 0xf5) || 610df08ef27SAndres Salomon ps2_command(ps2dev, NULL, 0xe6) || 611df08ef27SAndres Salomon ps2_command(ps2dev, NULL, 0xf5)) { 612df08ef27SAndres Salomon return -1; 613df08ef27SAndres Salomon } 614df08ef27SAndres Salomon 615df08ef27SAndres Salomon /* according to ALPS, 150mS is required for recalibration */ 616df08ef27SAndres Salomon msleep(150); 617ca94ec43SDaniel Drake } 618df08ef27SAndres Salomon 619ca94ec43SDaniel Drake err = hgpk_select_mode(psmouse); 620ca94ec43SDaniel Drake if (err) { 621ca94ec43SDaniel Drake hgpk_err(psmouse, "failed to select mode\n"); 622ca94ec43SDaniel Drake return err; 623ca94ec43SDaniel Drake } 624ca94ec43SDaniel Drake 625ca94ec43SDaniel Drake hgpk_reset_hack_state(psmouse); 626ca94ec43SDaniel Drake 627ca94ec43SDaniel Drake return 0; 628ca94ec43SDaniel Drake } 629ca94ec43SDaniel Drake 630ca94ec43SDaniel Drake static int hgpk_force_recalibrate(struct psmouse *psmouse) 631ca94ec43SDaniel Drake { 632ca94ec43SDaniel Drake struct ps2dev *ps2dev = &psmouse->ps2dev; 633ca94ec43SDaniel Drake struct hgpk_data *priv = psmouse->private; 634ca94ec43SDaniel Drake int err; 635ca94ec43SDaniel Drake 636ca94ec43SDaniel Drake /* C-series touchpads added the recalibrate command */ 637ca94ec43SDaniel Drake if (psmouse->model < HGPK_MODEL_C) 638ca94ec43SDaniel Drake return 0; 639ca94ec43SDaniel Drake 640ca94ec43SDaniel Drake /* we don't want to race with the irq handler, nor with resyncs */ 641ca94ec43SDaniel Drake psmouse_set_state(psmouse, PSMOUSE_INITIALIZING); 642ca94ec43SDaniel Drake 643ca94ec43SDaniel Drake /* start by resetting the device */ 644ca94ec43SDaniel Drake err = hgpk_reset_device(psmouse, true); 645ca94ec43SDaniel Drake if (err) 646ca94ec43SDaniel Drake return err; 647ca94ec43SDaniel Drake 648ca94ec43SDaniel Drake /* 649ca94ec43SDaniel Drake * XXX: If a finger is down during this delay, recalibration will 650df08ef27SAndres Salomon * detect capacitance incorrectly. This is a hardware bug, and 651df08ef27SAndres Salomon * we don't have a good way to deal with it. The 2s window stuff 652df08ef27SAndres Salomon * (below) is our best option for now. 653df08ef27SAndres Salomon */ 654df08ef27SAndres Salomon 655df08ef27SAndres Salomon if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) 656df08ef27SAndres Salomon return -1; 657df08ef27SAndres Salomon 658df08ef27SAndres Salomon psmouse_set_state(psmouse, PSMOUSE_ACTIVATED); 659df08ef27SAndres Salomon 660ca94ec43SDaniel Drake /* 661ca94ec43SDaniel Drake * After we recalibrate, we shouldn't get any packets for 2s. If 662df08ef27SAndres Salomon * we do, it's likely that someone's finger was on the touchpad. 663df08ef27SAndres Salomon * If someone's finger *was* on the touchpad, it's probably 664df08ef27SAndres Salomon * miscalibrated. So, we should schedule another recalibration 665df08ef27SAndres Salomon */ 6668bbf2703SPaul Fox priv->recalib_window = jiffies + msecs_to_jiffies(recal_guard_time); 667df08ef27SAndres Salomon 668df08ef27SAndres Salomon return 0; 669df08ef27SAndres Salomon } 670df08ef27SAndres Salomon 671df08ef27SAndres Salomon /* 672df08ef27SAndres Salomon * This kills power to the touchpad; according to ALPS, current consumption 673df08ef27SAndres Salomon * goes down to 50uA after running this. To turn power back on, we drive 674df08ef27SAndres Salomon * MS-DAT low. 675df08ef27SAndres Salomon */ 676df08ef27SAndres Salomon static int hgpk_toggle_power(struct psmouse *psmouse, int enable) 677df08ef27SAndres Salomon { 678df08ef27SAndres Salomon struct ps2dev *ps2dev = &psmouse->ps2dev; 679df08ef27SAndres Salomon int timeo; 680ca94ec43SDaniel Drake int err; 681df08ef27SAndres Salomon 682df08ef27SAndres Salomon /* Added on D-series touchpads */ 683df08ef27SAndres Salomon if (psmouse->model < HGPK_MODEL_D) 684df08ef27SAndres Salomon return 0; 685df08ef27SAndres Salomon 686df08ef27SAndres Salomon if (enable) { 687df08ef27SAndres Salomon psmouse_set_state(psmouse, PSMOUSE_INITIALIZING); 688df08ef27SAndres Salomon 689df08ef27SAndres Salomon /* 690df08ef27SAndres Salomon * Sending a byte will drive MS-DAT low; this will wake up 691df08ef27SAndres Salomon * the controller. Once we get an ACK back from it, it 692df08ef27SAndres Salomon * means we can continue with the touchpad re-init. ALPS 693df08ef27SAndres Salomon * tells us that 1s should be long enough, so set that as 694df08ef27SAndres Salomon * the upper bound. 695df08ef27SAndres Salomon */ 696df08ef27SAndres Salomon for (timeo = 20; timeo > 0; timeo--) { 697df08ef27SAndres Salomon if (!ps2_sendbyte(&psmouse->ps2dev, 698df08ef27SAndres Salomon PSMOUSE_CMD_DISABLE, 20)) 699df08ef27SAndres Salomon break; 700df08ef27SAndres Salomon msleep(50); 701df08ef27SAndres Salomon } 702df08ef27SAndres Salomon 703ca94ec43SDaniel Drake err = hgpk_reset_device(psmouse, false); 704ca94ec43SDaniel Drake if (err) { 705ca94ec43SDaniel Drake hgpk_err(psmouse, "Failed to reset device!\n"); 706ca94ec43SDaniel Drake return err; 707ca94ec43SDaniel Drake } 708df08ef27SAndres Salomon 709df08ef27SAndres Salomon /* should be all set, enable the touchpad */ 710df08ef27SAndres Salomon ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE); 711df08ef27SAndres Salomon psmouse_set_state(psmouse, PSMOUSE_ACTIVATED); 712df08ef27SAndres Salomon 713df08ef27SAndres Salomon } else { 714df08ef27SAndres Salomon hgpk_dbg(psmouse, "Powering off touchpad.\n"); 715df08ef27SAndres Salomon psmouse_set_state(psmouse, PSMOUSE_IGNORE); 716df08ef27SAndres Salomon 717df08ef27SAndres Salomon if (ps2_command(ps2dev, NULL, 0xec) || 718df08ef27SAndres Salomon ps2_command(ps2dev, NULL, 0xec) || 719df08ef27SAndres Salomon ps2_command(ps2dev, NULL, 0xea)) { 720df08ef27SAndres Salomon return -1; 721df08ef27SAndres Salomon } 722df08ef27SAndres Salomon 723df08ef27SAndres Salomon /* probably won't see an ACK, the touchpad will be off */ 724df08ef27SAndres Salomon ps2_sendbyte(&psmouse->ps2dev, 0xec, 20); 725df08ef27SAndres Salomon } 726df08ef27SAndres Salomon 727df08ef27SAndres Salomon return 0; 728df08ef27SAndres Salomon } 729df08ef27SAndres Salomon 730df08ef27SAndres Salomon static int hgpk_poll(struct psmouse *psmouse) 731df08ef27SAndres Salomon { 732df08ef27SAndres Salomon /* We can't poll, so always return failure. */ 733df08ef27SAndres Salomon return -1; 734df08ef27SAndres Salomon } 735df08ef27SAndres Salomon 736df08ef27SAndres Salomon static int hgpk_reconnect(struct psmouse *psmouse) 737df08ef27SAndres Salomon { 738ca94ec43SDaniel Drake /* 739ca94ec43SDaniel Drake * During suspend/resume the ps2 rails remain powered. We don't want 740df08ef27SAndres Salomon * to do a reset because it's flush data out of buffers; however, 741ca94ec43SDaniel Drake * earlier prototypes (B1) had some brokenness that required a reset. 742ca94ec43SDaniel Drake */ 743df08ef27SAndres Salomon if (olpc_board_at_least(olpc_board(0xb2))) 744df08ef27SAndres Salomon if (psmouse->ps2dev.serio->dev.power.power_state.event != 745df08ef27SAndres Salomon PM_EVENT_ON) 746df08ef27SAndres Salomon return 0; 747df08ef27SAndres Salomon 748ca94ec43SDaniel Drake return hgpk_reset_device(psmouse, false); 749df08ef27SAndres Salomon } 750df08ef27SAndres Salomon 751df08ef27SAndres Salomon static ssize_t hgpk_show_powered(struct psmouse *psmouse, void *data, char *buf) 752df08ef27SAndres Salomon { 753df08ef27SAndres Salomon struct hgpk_data *priv = psmouse->private; 754df08ef27SAndres Salomon 755df08ef27SAndres Salomon return sprintf(buf, "%d\n", priv->powered); 756df08ef27SAndres Salomon } 757df08ef27SAndres Salomon 758df08ef27SAndres Salomon static ssize_t hgpk_set_powered(struct psmouse *psmouse, void *data, 759df08ef27SAndres Salomon const char *buf, size_t count) 760df08ef27SAndres Salomon { 761df08ef27SAndres Salomon struct hgpk_data *priv = psmouse->private; 762df08ef27SAndres Salomon unsigned long value; 763df08ef27SAndres Salomon int err; 764df08ef27SAndres Salomon 765df08ef27SAndres Salomon err = strict_strtoul(buf, 10, &value); 766df08ef27SAndres Salomon if (err || value > 1) 767df08ef27SAndres Salomon return -EINVAL; 768df08ef27SAndres Salomon 769df08ef27SAndres Salomon if (value != priv->powered) { 770df08ef27SAndres Salomon /* 771df08ef27SAndres Salomon * hgpk_toggle_power will deal w/ state so 772df08ef27SAndres Salomon * we're not racing w/ irq 773df08ef27SAndres Salomon */ 774df08ef27SAndres Salomon err = hgpk_toggle_power(psmouse, value); 775df08ef27SAndres Salomon if (!err) 776df08ef27SAndres Salomon priv->powered = value; 777df08ef27SAndres Salomon } 778df08ef27SAndres Salomon 779df08ef27SAndres Salomon return err ? err : count; 780df08ef27SAndres Salomon } 781df08ef27SAndres Salomon 782df08ef27SAndres Salomon __PSMOUSE_DEFINE_ATTR(powered, S_IWUSR | S_IRUGO, NULL, 783b7802c5cSDmitry Torokhov hgpk_show_powered, hgpk_set_powered, false); 784df08ef27SAndres Salomon 785ca94ec43SDaniel Drake static ssize_t attr_show_mode(struct psmouse *psmouse, void *data, char *buf) 786ca94ec43SDaniel Drake { 787ca94ec43SDaniel Drake struct hgpk_data *priv = psmouse->private; 788ca94ec43SDaniel Drake 789ca94ec43SDaniel Drake return sprintf(buf, "%s\n", hgpk_mode_names[priv->mode]); 790ca94ec43SDaniel Drake } 791ca94ec43SDaniel Drake 792ca94ec43SDaniel Drake static ssize_t attr_set_mode(struct psmouse *psmouse, void *data, 793ca94ec43SDaniel Drake const char *buf, size_t len) 794ca94ec43SDaniel Drake { 795ca94ec43SDaniel Drake struct hgpk_data *priv = psmouse->private; 796ca94ec43SDaniel Drake enum hgpk_mode old_mode = priv->mode; 797ca94ec43SDaniel Drake enum hgpk_mode new_mode = hgpk_mode_from_name(buf, len); 798ca94ec43SDaniel Drake struct input_dev *old_dev = psmouse->dev; 799ca94ec43SDaniel Drake struct input_dev *new_dev; 800ca94ec43SDaniel Drake int err; 801ca94ec43SDaniel Drake 802ca94ec43SDaniel Drake if (new_mode == HGPK_MODE_INVALID) 803ca94ec43SDaniel Drake return -EINVAL; 804ca94ec43SDaniel Drake 805ca94ec43SDaniel Drake if (old_mode == new_mode) 806ca94ec43SDaniel Drake return len; 807ca94ec43SDaniel Drake 808ca94ec43SDaniel Drake new_dev = input_allocate_device(); 809ca94ec43SDaniel Drake if (!new_dev) 810ca94ec43SDaniel Drake return -ENOMEM; 811ca94ec43SDaniel Drake 812ca94ec43SDaniel Drake psmouse_set_state(psmouse, PSMOUSE_INITIALIZING); 813ca94ec43SDaniel Drake 814ca94ec43SDaniel Drake /* Switch device into the new mode */ 815ca94ec43SDaniel Drake priv->mode = new_mode; 816ca94ec43SDaniel Drake err = hgpk_reset_device(psmouse, false); 817ca94ec43SDaniel Drake if (err) 818ca94ec43SDaniel Drake goto err_try_restore; 819ca94ec43SDaniel Drake 820ca94ec43SDaniel Drake hgpk_setup_input_device(new_dev, old_dev, new_mode); 821ca94ec43SDaniel Drake 822ca94ec43SDaniel Drake psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); 823ca94ec43SDaniel Drake 824ca94ec43SDaniel Drake err = input_register_device(new_dev); 825ca94ec43SDaniel Drake if (err) 826ca94ec43SDaniel Drake goto err_try_restore; 827ca94ec43SDaniel Drake 828ca94ec43SDaniel Drake psmouse->dev = new_dev; 829ca94ec43SDaniel Drake input_unregister_device(old_dev); 830ca94ec43SDaniel Drake 831ca94ec43SDaniel Drake return len; 832ca94ec43SDaniel Drake 833ca94ec43SDaniel Drake err_try_restore: 834ca94ec43SDaniel Drake input_free_device(new_dev); 835ca94ec43SDaniel Drake priv->mode = old_mode; 836ca94ec43SDaniel Drake hgpk_reset_device(psmouse, false); 837ca94ec43SDaniel Drake 838ca94ec43SDaniel Drake return err; 839ca94ec43SDaniel Drake } 840ca94ec43SDaniel Drake 841ca94ec43SDaniel Drake PSMOUSE_DEFINE_ATTR(hgpk_mode, S_IWUSR | S_IRUGO, NULL, 842ca94ec43SDaniel Drake attr_show_mode, attr_set_mode); 843ca94ec43SDaniel Drake 844c46dd1ebSPaul Fox static ssize_t hgpk_trigger_recal_show(struct psmouse *psmouse, 845c46dd1ebSPaul Fox void *data, char *buf) 846c46dd1ebSPaul Fox { 847c46dd1ebSPaul Fox return -EINVAL; 848c46dd1ebSPaul Fox } 849c46dd1ebSPaul Fox 850c46dd1ebSPaul Fox static ssize_t hgpk_trigger_recal(struct psmouse *psmouse, void *data, 851c46dd1ebSPaul Fox const char *buf, size_t count) 852c46dd1ebSPaul Fox { 853c46dd1ebSPaul Fox struct hgpk_data *priv = psmouse->private; 854c46dd1ebSPaul Fox unsigned long value; 855c46dd1ebSPaul Fox int err; 856c46dd1ebSPaul Fox 857c46dd1ebSPaul Fox err = strict_strtoul(buf, 10, &value); 858c46dd1ebSPaul Fox if (err || value != 1) 859c46dd1ebSPaul Fox return -EINVAL; 860c46dd1ebSPaul Fox 861c46dd1ebSPaul Fox /* 862c46dd1ebSPaul Fox * We queue work instead of doing recalibration right here 863c46dd1ebSPaul Fox * to avoid adding locking to to hgpk_force_recalibrate() 864c46dd1ebSPaul Fox * since workqueue provides serialization. 865c46dd1ebSPaul Fox */ 866c46dd1ebSPaul Fox psmouse_queue_work(psmouse, &priv->recalib_wq, 0); 867c46dd1ebSPaul Fox return count; 868c46dd1ebSPaul Fox } 869c46dd1ebSPaul Fox 870c46dd1ebSPaul Fox __PSMOUSE_DEFINE_ATTR(recalibrate, S_IWUSR | S_IRUGO, NULL, 871b7802c5cSDmitry Torokhov hgpk_trigger_recal_show, hgpk_trigger_recal, false); 872c46dd1ebSPaul Fox 873df08ef27SAndres Salomon static void hgpk_disconnect(struct psmouse *psmouse) 874df08ef27SAndres Salomon { 875df08ef27SAndres Salomon struct hgpk_data *priv = psmouse->private; 876df08ef27SAndres Salomon 877df08ef27SAndres Salomon device_remove_file(&psmouse->ps2dev.serio->dev, 878df08ef27SAndres Salomon &psmouse_attr_powered.dattr); 879ca94ec43SDaniel Drake device_remove_file(&psmouse->ps2dev.serio->dev, 880ca94ec43SDaniel Drake &psmouse_attr_hgpk_mode.dattr); 881c46dd1ebSPaul Fox 882c46dd1ebSPaul Fox if (psmouse->model >= HGPK_MODEL_C) 883c46dd1ebSPaul Fox device_remove_file(&psmouse->ps2dev.serio->dev, 884c46dd1ebSPaul Fox &psmouse_attr_recalibrate.dattr); 885c46dd1ebSPaul Fox 886df08ef27SAndres Salomon psmouse_reset(psmouse); 887df08ef27SAndres Salomon kfree(priv); 888df08ef27SAndres Salomon } 889df08ef27SAndres Salomon 890df08ef27SAndres Salomon static void hgpk_recalib_work(struct work_struct *work) 891df08ef27SAndres Salomon { 892bf6aede7SJean Delvare struct delayed_work *w = to_delayed_work(work); 893df08ef27SAndres Salomon struct hgpk_data *priv = container_of(w, struct hgpk_data, recalib_wq); 894df08ef27SAndres Salomon struct psmouse *psmouse = priv->psmouse; 895df08ef27SAndres Salomon 896df08ef27SAndres Salomon hgpk_dbg(psmouse, "recalibrating touchpad..\n"); 897df08ef27SAndres Salomon 898df08ef27SAndres Salomon if (hgpk_force_recalibrate(psmouse)) 899df08ef27SAndres Salomon hgpk_err(psmouse, "recalibration failed!\n"); 900df08ef27SAndres Salomon } 901df08ef27SAndres Salomon 902df08ef27SAndres Salomon static int hgpk_register(struct psmouse *psmouse) 903df08ef27SAndres Salomon { 904ca94ec43SDaniel Drake struct hgpk_data *priv = psmouse->private; 905df08ef27SAndres Salomon int err; 906df08ef27SAndres Salomon 907df08ef27SAndres Salomon /* register handlers */ 908df08ef27SAndres Salomon psmouse->protocol_handler = hgpk_process_byte; 909df08ef27SAndres Salomon psmouse->poll = hgpk_poll; 910df08ef27SAndres Salomon psmouse->disconnect = hgpk_disconnect; 911df08ef27SAndres Salomon psmouse->reconnect = hgpk_reconnect; 912df08ef27SAndres Salomon 913df08ef27SAndres Salomon /* Disable the idle resync. */ 914df08ef27SAndres Salomon psmouse->resync_time = 0; 915df08ef27SAndres Salomon /* Reset after a lot of bad bytes. */ 916df08ef27SAndres Salomon psmouse->resetafter = 1024; 917df08ef27SAndres Salomon 918ca94ec43SDaniel Drake hgpk_setup_input_device(psmouse->dev, NULL, priv->mode); 919ca94ec43SDaniel Drake 920df08ef27SAndres Salomon err = device_create_file(&psmouse->ps2dev.serio->dev, 921df08ef27SAndres Salomon &psmouse_attr_powered.dattr); 922c46dd1ebSPaul Fox if (err) { 923c46dd1ebSPaul Fox hgpk_err(psmouse, "Failed creating 'powered' sysfs node\n"); 924df08ef27SAndres Salomon return err; 925df08ef27SAndres Salomon } 926df08ef27SAndres Salomon 927ca94ec43SDaniel Drake err = device_create_file(&psmouse->ps2dev.serio->dev, 928ca94ec43SDaniel Drake &psmouse_attr_hgpk_mode.dattr); 929ca94ec43SDaniel Drake if (err) { 930ca94ec43SDaniel Drake hgpk_err(psmouse, "Failed creating 'hgpk_mode' sysfs node\n"); 931ca94ec43SDaniel Drake goto err_remove_powered; 932ca94ec43SDaniel Drake } 933ca94ec43SDaniel Drake 934c46dd1ebSPaul Fox /* C-series touchpads added the recalibrate command */ 935c46dd1ebSPaul Fox if (psmouse->model >= HGPK_MODEL_C) { 936c46dd1ebSPaul Fox err = device_create_file(&psmouse->ps2dev.serio->dev, 937c46dd1ebSPaul Fox &psmouse_attr_recalibrate.dattr); 938c46dd1ebSPaul Fox if (err) { 939c46dd1ebSPaul Fox hgpk_err(psmouse, 940c46dd1ebSPaul Fox "Failed creating 'recalibrate' sysfs node\n"); 941ca94ec43SDaniel Drake goto err_remove_mode; 942c46dd1ebSPaul Fox } 943c46dd1ebSPaul Fox } 944c46dd1ebSPaul Fox 945c46dd1ebSPaul Fox return 0; 946ca94ec43SDaniel Drake 947ca94ec43SDaniel Drake err_remove_mode: 948ca94ec43SDaniel Drake device_remove_file(&psmouse->ps2dev.serio->dev, 949ca94ec43SDaniel Drake &psmouse_attr_hgpk_mode.dattr); 950ca94ec43SDaniel Drake err_remove_powered: 951ca94ec43SDaniel Drake device_remove_file(&psmouse->ps2dev.serio->dev, 952ca94ec43SDaniel Drake &psmouse_attr_powered.dattr); 953ca94ec43SDaniel Drake return err; 954c46dd1ebSPaul Fox } 955c46dd1ebSPaul Fox 956df08ef27SAndres Salomon int hgpk_init(struct psmouse *psmouse) 957df08ef27SAndres Salomon { 958df08ef27SAndres Salomon struct hgpk_data *priv; 959ca94ec43SDaniel Drake int err; 960df08ef27SAndres Salomon 961df08ef27SAndres Salomon priv = kzalloc(sizeof(struct hgpk_data), GFP_KERNEL); 962ca94ec43SDaniel Drake if (!priv) { 963ca94ec43SDaniel Drake err = -ENOMEM; 964df08ef27SAndres Salomon goto alloc_fail; 965ca94ec43SDaniel Drake } 966df08ef27SAndres Salomon 967df08ef27SAndres Salomon psmouse->private = priv; 968ca94ec43SDaniel Drake 969df08ef27SAndres Salomon priv->psmouse = psmouse; 970b7802c5cSDmitry Torokhov priv->powered = true; 971ca94ec43SDaniel Drake priv->mode = hgpk_default_mode; 972df08ef27SAndres Salomon INIT_DELAYED_WORK(&priv->recalib_wq, hgpk_recalib_work); 973df08ef27SAndres Salomon 974ca94ec43SDaniel Drake err = hgpk_reset_device(psmouse, false); 975df08ef27SAndres Salomon if (err) 976df08ef27SAndres Salomon goto init_fail; 977df08ef27SAndres Salomon 978df08ef27SAndres Salomon err = hgpk_register(psmouse); 979df08ef27SAndres Salomon if (err) 980df08ef27SAndres Salomon goto init_fail; 981df08ef27SAndres Salomon 982df08ef27SAndres Salomon return 0; 983df08ef27SAndres Salomon 984df08ef27SAndres Salomon init_fail: 985df08ef27SAndres Salomon kfree(priv); 986df08ef27SAndres Salomon alloc_fail: 987df08ef27SAndres Salomon return err; 988df08ef27SAndres Salomon } 989df08ef27SAndres Salomon 990df08ef27SAndres Salomon static enum hgpk_model_t hgpk_get_model(struct psmouse *psmouse) 991df08ef27SAndres Salomon { 992df08ef27SAndres Salomon struct ps2dev *ps2dev = &psmouse->ps2dev; 993df08ef27SAndres Salomon unsigned char param[3]; 994df08ef27SAndres Salomon 995df08ef27SAndres Salomon /* E7, E7, E7, E9 gets us a 3 byte identifier */ 996df08ef27SAndres Salomon if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || 997df08ef27SAndres Salomon ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || 998df08ef27SAndres Salomon ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || 999df08ef27SAndres Salomon ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) { 1000df08ef27SAndres Salomon return -EIO; 1001df08ef27SAndres Salomon } 1002df08ef27SAndres Salomon 10030f495481SAndy Whitcroft hgpk_dbg(psmouse, "ID: %02x %02x %02x\n", param[0], param[1], param[2]); 1004df08ef27SAndres Salomon 1005df08ef27SAndres Salomon /* HGPK signature: 0x67, 0x00, 0x<model> */ 1006df08ef27SAndres Salomon if (param[0] != 0x67 || param[1] != 0x00) 1007df08ef27SAndres Salomon return -ENODEV; 1008df08ef27SAndres Salomon 1009df08ef27SAndres Salomon hgpk_info(psmouse, "OLPC touchpad revision 0x%x\n", param[2]); 1010df08ef27SAndres Salomon 1011df08ef27SAndres Salomon return param[2]; 1012df08ef27SAndres Salomon } 1013df08ef27SAndres Salomon 1014b7802c5cSDmitry Torokhov int hgpk_detect(struct psmouse *psmouse, bool set_properties) 1015df08ef27SAndres Salomon { 1016df08ef27SAndres Salomon int version; 1017df08ef27SAndres Salomon 1018df08ef27SAndres Salomon version = hgpk_get_model(psmouse); 1019df08ef27SAndres Salomon if (version < 0) 1020df08ef27SAndres Salomon return version; 1021df08ef27SAndres Salomon 1022df08ef27SAndres Salomon if (set_properties) { 1023df08ef27SAndres Salomon psmouse->vendor = "ALPS"; 1024df08ef27SAndres Salomon psmouse->name = "HGPK"; 1025df08ef27SAndres Salomon psmouse->model = version; 1026df08ef27SAndres Salomon } 1027df08ef27SAndres Salomon 1028df08ef27SAndres Salomon return 0; 1029df08ef27SAndres Salomon } 1030ca94ec43SDaniel Drake 1031ca94ec43SDaniel Drake void hgpk_module_init(void) 1032ca94ec43SDaniel Drake { 1033ca94ec43SDaniel Drake hgpk_default_mode = hgpk_mode_from_name(hgpk_mode_name, 1034ca94ec43SDaniel Drake strlen(hgpk_mode_name)); 1035ca94ec43SDaniel Drake if (hgpk_default_mode == HGPK_MODE_INVALID) { 1036ca94ec43SDaniel Drake hgpk_default_mode = HGPK_MODE_MOUSE; 1037ca94ec43SDaniel Drake strlcpy(hgpk_mode_name, hgpk_mode_names[HGPK_MODE_MOUSE], 1038ca94ec43SDaniel Drake sizeof(hgpk_mode_name)); 1039ca94ec43SDaniel Drake } 1040ca94ec43SDaniel Drake } 1041