hgpk.c 27 KB

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