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