alps.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477
  1. /*
  2. * ALPS touchpad PS/2 mouse driver
  3. *
  4. * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
  5. * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
  6. * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
  7. * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
  8. * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
  9. *
  10. * ALPS detection, tap switching and status querying info is taken from
  11. * tpconfig utility (by C. Scott Ananian and Bruce Kall).
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License version 2 as published by
  15. * the Free Software Foundation.
  16. */
  17. #include <linux/slab.h>
  18. #include <linux/input.h>
  19. #include <linux/input/mt.h>
  20. #include <linux/serio.h>
  21. #include <linux/libps2.h>
  22. #include "psmouse.h"
  23. #include "alps.h"
  24. /*
  25. * Definitions for ALPS version 3 and 4 command mode protocol
  26. */
  27. #define ALPS_CMD_NIBBLE_10 0x01f2
  28. #define ALPS_REG_BASE_RUSHMORE 0xc2c0
  29. #define ALPS_REG_BASE_PINNACLE 0x0000
  30. static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
  31. { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
  32. { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
  33. { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
  34. { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
  35. { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
  36. { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
  37. { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
  38. { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
  39. { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
  40. { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
  41. { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
  42. { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
  43. { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
  44. { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
  45. { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
  46. { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
  47. };
  48. static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
  49. { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
  50. { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
  51. { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
  52. { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
  53. { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
  54. { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
  55. { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
  56. { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
  57. { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
  58. { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
  59. { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
  60. { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
  61. { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
  62. { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
  63. { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
  64. { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
  65. };
  66. static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
  67. { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
  68. { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
  69. { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
  70. { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
  71. { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
  72. { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
  73. { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
  74. { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
  75. { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
  76. { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
  77. { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
  78. { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
  79. { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
  80. { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
  81. { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
  82. { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
  83. };
  84. #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
  85. #define ALPS_PASS 0x04 /* device has a pass-through port */
  86. #define ALPS_WHEEL 0x08 /* hardware wheel present */
  87. #define ALPS_FW_BK_1 0x10 /* front & back buttons present */
  88. #define ALPS_FW_BK_2 0x20 /* front & back buttons present */
  89. #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
  90. #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
  91. 6-byte ALPS packet */
  92. #define ALPS_IS_RUSHMORE 0x100 /* device is a rushmore */
  93. #define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
  94. static const struct alps_model_info alps_model_data[] = {
  95. { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
  96. { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */
  97. { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
  98. { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
  99. { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */
  100. { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
  101. { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
  102. { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
  103. { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
  104. { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
  105. { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
  106. { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
  107. { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
  108. { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
  109. { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
  110. { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
  111. { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
  112. { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
  113. /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
  114. { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
  115. ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
  116. { { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT }, /* Dell XT2 */
  117. { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
  118. { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
  119. ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
  120. { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
  121. };
  122. static void alps_set_abs_params_st(struct alps_data *priv,
  123. struct input_dev *dev1);
  124. static void alps_set_abs_params_mt(struct alps_data *priv,
  125. struct input_dev *dev1);
  126. /*
  127. * XXX - this entry is suspicious. First byte has zero lower nibble,
  128. * which is what a normal mouse would report. Also, the value 0x0e
  129. * isn't valid per PS/2 spec.
  130. */
  131. /* Packet formats are described in Documentation/input/alps.txt */
  132. static bool alps_is_valid_first_byte(struct alps_data *priv,
  133. unsigned char data)
  134. {
  135. return (data & priv->mask0) == priv->byte0;
  136. }
  137. static void alps_report_buttons(struct psmouse *psmouse,
  138. struct input_dev *dev1, struct input_dev *dev2,
  139. int left, int right, int middle)
  140. {
  141. struct input_dev *dev;
  142. /*
  143. * If shared button has already been reported on the
  144. * other device (dev2) then this event should be also
  145. * sent through that device.
  146. */
  147. dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
  148. input_report_key(dev, BTN_LEFT, left);
  149. dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
  150. input_report_key(dev, BTN_RIGHT, right);
  151. dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
  152. input_report_key(dev, BTN_MIDDLE, middle);
  153. /*
  154. * Sync the _other_ device now, we'll do the first
  155. * device later once we report the rest of the events.
  156. */
  157. input_sync(dev2);
  158. }
  159. static void alps_process_packet_v1_v2(struct psmouse *psmouse)
  160. {
  161. struct alps_data *priv = psmouse->private;
  162. unsigned char *packet = psmouse->packet;
  163. struct input_dev *dev = psmouse->dev;
  164. struct input_dev *dev2 = priv->dev2;
  165. int x, y, z, ges, fin, left, right, middle;
  166. int back = 0, forward = 0;
  167. if (priv->proto_version == ALPS_PROTO_V1) {
  168. left = packet[2] & 0x10;
  169. right = packet[2] & 0x08;
  170. middle = 0;
  171. x = packet[1] | ((packet[0] & 0x07) << 7);
  172. y = packet[4] | ((packet[3] & 0x07) << 7);
  173. z = packet[5];
  174. } else {
  175. left = packet[3] & 1;
  176. right = packet[3] & 2;
  177. middle = packet[3] & 4;
  178. x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
  179. y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
  180. z = packet[5];
  181. }
  182. if (priv->flags & ALPS_FW_BK_1) {
  183. back = packet[0] & 0x10;
  184. forward = packet[2] & 4;
  185. }
  186. if (priv->flags & ALPS_FW_BK_2) {
  187. back = packet[3] & 4;
  188. forward = packet[2] & 4;
  189. if ((middle = forward && back))
  190. forward = back = 0;
  191. }
  192. ges = packet[2] & 1;
  193. fin = packet[2] & 2;
  194. if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
  195. input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
  196. input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
  197. alps_report_buttons(psmouse, dev2, dev, left, right, middle);
  198. input_sync(dev2);
  199. return;
  200. }
  201. alps_report_buttons(psmouse, dev, dev2, left, right, middle);
  202. /* Convert hardware tap to a reasonable Z value */
  203. if (ges && !fin)
  204. z = 40;
  205. /*
  206. * A "tap and drag" operation is reported by the hardware as a transition
  207. * from (!fin && ges) to (fin && ges). This should be translated to the
  208. * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
  209. */
  210. if (ges && fin && !priv->prev_fin) {
  211. input_report_abs(dev, ABS_X, x);
  212. input_report_abs(dev, ABS_Y, y);
  213. input_report_abs(dev, ABS_PRESSURE, 0);
  214. input_report_key(dev, BTN_TOOL_FINGER, 0);
  215. input_sync(dev);
  216. }
  217. priv->prev_fin = fin;
  218. if (z > 30)
  219. input_report_key(dev, BTN_TOUCH, 1);
  220. if (z < 25)
  221. input_report_key(dev, BTN_TOUCH, 0);
  222. if (z > 0) {
  223. input_report_abs(dev, ABS_X, x);
  224. input_report_abs(dev, ABS_Y, y);
  225. }
  226. input_report_abs(dev, ABS_PRESSURE, z);
  227. input_report_key(dev, BTN_TOOL_FINGER, z > 0);
  228. if (priv->flags & ALPS_WHEEL)
  229. input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
  230. if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  231. input_report_key(dev, BTN_FORWARD, forward);
  232. input_report_key(dev, BTN_BACK, back);
  233. }
  234. if (priv->flags & ALPS_FOUR_BUTTONS) {
  235. input_report_key(dev, BTN_0, packet[2] & 4);
  236. input_report_key(dev, BTN_1, packet[0] & 0x10);
  237. input_report_key(dev, BTN_2, packet[3] & 4);
  238. input_report_key(dev, BTN_3, packet[0] & 0x20);
  239. }
  240. input_sync(dev);
  241. }
  242. /*
  243. * Process bitmap data for V5 protocols. Return value is null.
  244. *
  245. * The bitmaps don't have enough data to track fingers, so this function
  246. * only generates points representing a bounding box of at most two contacts.
  247. * These two points are returned in fields->mt.
  248. */
  249. static void alps_process_bitmap_dolphin(struct alps_data *priv,
  250. struct alps_fields *fields)
  251. {
  252. int box_middle_x, box_middle_y;
  253. unsigned int x_map, y_map;
  254. unsigned char start_bit, end_bit;
  255. unsigned char x_msb, x_lsb, y_msb, y_lsb;
  256. x_map = fields->x_map;
  257. y_map = fields->y_map;
  258. if (!x_map || !y_map)
  259. return;
  260. /* Get Most-significant and Least-significant bit */
  261. x_msb = fls(x_map);
  262. x_lsb = ffs(x_map);
  263. y_msb = fls(y_map);
  264. y_lsb = ffs(y_map);
  265. /* Most-significant bit should never exceed max sensor line number */
  266. if (x_msb > priv->x_bits || y_msb > priv->y_bits)
  267. return;
  268. if (fields->fingers > 1) {
  269. start_bit = priv->x_bits - x_msb;
  270. end_bit = priv->x_bits - x_lsb;
  271. box_middle_x = (priv->x_max * (start_bit + end_bit)) /
  272. (2 * (priv->x_bits - 1));
  273. start_bit = y_lsb - 1;
  274. end_bit = y_msb - 1;
  275. box_middle_y = (priv->y_max * (start_bit + end_bit)) /
  276. (2 * (priv->y_bits - 1));
  277. fields->mt[0] = fields->st;
  278. fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
  279. fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
  280. }
  281. }
  282. static void alps_get_bitmap_points(unsigned int map,
  283. struct alps_bitmap_point *low,
  284. struct alps_bitmap_point *high,
  285. int *fingers)
  286. {
  287. struct alps_bitmap_point *point;
  288. int i, bit, prev_bit = 0;
  289. point = low;
  290. for (i = 0; map != 0; i++, map >>= 1) {
  291. bit = map & 1;
  292. if (bit) {
  293. if (!prev_bit) {
  294. point->start_bit = i;
  295. point->num_bits = 0;
  296. (*fingers)++;
  297. }
  298. point->num_bits++;
  299. } else {
  300. if (prev_bit)
  301. point = high;
  302. }
  303. prev_bit = bit;
  304. }
  305. }
  306. /*
  307. * Process bitmap data from v3 and v4 protocols. Returns the number of
  308. * fingers detected. A return value of 0 means at least one of the
  309. * bitmaps was empty.
  310. *
  311. * The bitmaps don't have enough data to track fingers, so this function
  312. * only generates points representing a bounding box of all contacts.
  313. * These points are returned in fields->mt when the return value
  314. * is greater than 0.
  315. */
  316. static int alps_process_bitmap(struct alps_data *priv,
  317. struct alps_fields *fields)
  318. {
  319. int i, fingers_x = 0, fingers_y = 0, fingers;
  320. struct alps_bitmap_point x_low = {0,}, x_high = {0,};
  321. struct alps_bitmap_point y_low = {0,}, y_high = {0,};
  322. if (!fields->x_map || !fields->y_map)
  323. return 0;
  324. alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
  325. alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
  326. /*
  327. * Fingers can overlap, so we use the maximum count of fingers
  328. * on either axis as the finger count.
  329. */
  330. fingers = max(fingers_x, fingers_y);
  331. /*
  332. * If an axis reports only a single contact, we have overlapping or
  333. * adjacent fingers. Divide the single contact between the two points.
  334. */
  335. if (fingers_x == 1) {
  336. i = (x_low.num_bits - 1) / 2;
  337. x_low.num_bits = x_low.num_bits - i;
  338. x_high.start_bit = x_low.start_bit + i;
  339. x_high.num_bits = max(i, 1);
  340. }
  341. if (fingers_y == 1) {
  342. i = (y_low.num_bits - 1) / 2;
  343. y_low.num_bits = y_low.num_bits - i;
  344. y_high.start_bit = y_low.start_bit + i;
  345. y_high.num_bits = max(i, 1);
  346. }
  347. fields->mt[0].x =
  348. (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
  349. (2 * (priv->x_bits - 1));
  350. fields->mt[0].y =
  351. (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
  352. (2 * (priv->y_bits - 1));
  353. fields->mt[1].x =
  354. (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
  355. (2 * (priv->x_bits - 1));
  356. fields->mt[1].y =
  357. (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
  358. (2 * (priv->y_bits - 1));
  359. /* y-bitmap order is reversed, except on rushmore */
  360. if (!(priv->flags & ALPS_IS_RUSHMORE)) {
  361. fields->mt[0].y = priv->y_max - fields->mt[0].y;
  362. fields->mt[1].y = priv->y_max - fields->mt[1].y;
  363. }
  364. return fingers;
  365. }
  366. static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
  367. {
  368. input_mt_slot(dev, slot);
  369. input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
  370. input_report_abs(dev, ABS_MT_POSITION_X, x);
  371. input_report_abs(dev, ABS_MT_POSITION_Y, y);
  372. }
  373. static void alps_report_mt_data(struct psmouse *psmouse, int n)
  374. {
  375. struct alps_data *priv = psmouse->private;
  376. struct input_dev *dev = psmouse->dev;
  377. struct alps_fields *f = &priv->f;
  378. int i, slot[MAX_TOUCHES];
  379. input_mt_assign_slots(dev, slot, f->mt, n);
  380. for (i = 0; i < n; i++)
  381. alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
  382. input_mt_sync_frame(dev);
  383. }
  384. static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
  385. {
  386. struct alps_data *priv = psmouse->private;
  387. struct input_dev *dev = psmouse->dev;
  388. struct alps_fields *f = &priv->f;
  389. /* Use st data when we don't have mt data */
  390. if (fingers < 2) {
  391. f->mt[0].x = f->st.x;
  392. f->mt[0].y = f->st.y;
  393. fingers = f->pressure > 0 ? 1 : 0;
  394. }
  395. alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
  396. input_mt_report_finger_count(dev, fingers);
  397. input_report_key(dev, BTN_LEFT, f->left);
  398. input_report_key(dev, BTN_RIGHT, f->right);
  399. input_report_key(dev, BTN_MIDDLE, f->middle);
  400. input_report_abs(dev, ABS_PRESSURE, f->pressure);
  401. input_sync(dev);
  402. }
  403. static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
  404. {
  405. struct alps_data *priv = psmouse->private;
  406. unsigned char *packet = psmouse->packet;
  407. struct input_dev *dev = priv->dev2;
  408. int x, y, z, left, right, middle;
  409. /* Sanity check packet */
  410. if (!(packet[0] & 0x40)) {
  411. psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
  412. return;
  413. }
  414. /*
  415. * There's a special packet that seems to indicate the end
  416. * of a stream of trackstick data. Filter these out.
  417. */
  418. if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
  419. return;
  420. x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
  421. y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
  422. z = (packet[4] & 0x7c) >> 2;
  423. /*
  424. * The x and y values tend to be quite large, and when used
  425. * alone the trackstick is difficult to use. Scale them down
  426. * to compensate.
  427. */
  428. x /= 8;
  429. y /= 8;
  430. input_report_rel(dev, REL_X, x);
  431. input_report_rel(dev, REL_Y, -y);
  432. /*
  433. * Most ALPS models report the trackstick buttons in the touchpad
  434. * packets, but a few report them here. No reliable way has been
  435. * found to differentiate between the models upfront, so we enable
  436. * the quirk in response to seeing a button press in the trackstick
  437. * packet.
  438. */
  439. left = packet[3] & 0x01;
  440. right = packet[3] & 0x02;
  441. middle = packet[3] & 0x04;
  442. if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
  443. (left || right || middle))
  444. priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
  445. if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
  446. input_report_key(dev, BTN_LEFT, left);
  447. input_report_key(dev, BTN_RIGHT, right);
  448. input_report_key(dev, BTN_MIDDLE, middle);
  449. }
  450. input_sync(dev);
  451. return;
  452. }
  453. static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
  454. {
  455. f->left = !!(p[3] & 0x01);
  456. f->right = !!(p[3] & 0x02);
  457. f->middle = !!(p[3] & 0x04);
  458. f->ts_left = !!(p[3] & 0x10);
  459. f->ts_right = !!(p[3] & 0x20);
  460. f->ts_middle = !!(p[3] & 0x40);
  461. }
  462. static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
  463. struct psmouse *psmouse)
  464. {
  465. f->first_mp = !!(p[4] & 0x40);
  466. f->is_mp = !!(p[0] & 0x40);
  467. f->fingers = (p[5] & 0x3) + 1;
  468. f->x_map = ((p[4] & 0x7e) << 8) |
  469. ((p[1] & 0x7f) << 2) |
  470. ((p[0] & 0x30) >> 4);
  471. f->y_map = ((p[3] & 0x70) << 4) |
  472. ((p[2] & 0x7f) << 1) |
  473. (p[4] & 0x01);
  474. f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
  475. ((p[0] & 0x30) >> 4);
  476. f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
  477. f->pressure = p[5] & 0x7f;
  478. alps_decode_buttons_v3(f, p);
  479. return 0;
  480. }
  481. static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
  482. struct psmouse *psmouse)
  483. {
  484. alps_decode_pinnacle(f, p, psmouse);
  485. /* Rushmore's packet decode has a bit difference with Pinnacle's */
  486. f->is_mp = !!(p[5] & 0x40);
  487. f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
  488. f->x_map |= (p[5] & 0x10) << 11;
  489. f->y_map |= (p[5] & 0x20) << 6;
  490. return 0;
  491. }
  492. static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
  493. struct psmouse *psmouse)
  494. {
  495. u64 palm_data = 0;
  496. struct alps_data *priv = psmouse->private;
  497. f->first_mp = !!(p[0] & 0x02);
  498. f->is_mp = !!(p[0] & 0x20);
  499. if (!f->is_mp) {
  500. f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
  501. f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
  502. f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
  503. alps_decode_buttons_v3(f, p);
  504. } else {
  505. f->fingers = ((p[0] & 0x6) >> 1 |
  506. (p[0] & 0x10) >> 2);
  507. palm_data = (p[1] & 0x7f) |
  508. ((p[2] & 0x7f) << 7) |
  509. ((p[4] & 0x7f) << 14) |
  510. ((p[5] & 0x7f) << 21) |
  511. ((p[3] & 0x07) << 28) |
  512. (((u64)p[3] & 0x70) << 27) |
  513. (((u64)p[0] & 0x01) << 34);
  514. /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
  515. f->y_map = palm_data & (BIT(priv->y_bits) - 1);
  516. /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
  517. f->x_map = (palm_data >> priv->y_bits) &
  518. (BIT(priv->x_bits) - 1);
  519. }
  520. return 0;
  521. }
  522. static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
  523. {
  524. struct alps_data *priv = psmouse->private;
  525. unsigned char *packet = psmouse->packet;
  526. struct input_dev *dev2 = priv->dev2;
  527. struct alps_fields *f = &priv->f;
  528. int fingers = 0;
  529. memset(f, 0, sizeof(*f));
  530. priv->decode_fields(f, packet, psmouse);
  531. /*
  532. * There's no single feature of touchpad position and bitmap packets
  533. * that can be used to distinguish between them. We rely on the fact
  534. * that a bitmap packet should always follow a position packet with
  535. * bit 6 of packet[4] set.
  536. */
  537. if (priv->multi_packet) {
  538. /*
  539. * Sometimes a position packet will indicate a multi-packet
  540. * sequence, but then what follows is another position
  541. * packet. Check for this, and when it happens process the
  542. * position packet as usual.
  543. */
  544. if (f->is_mp) {
  545. fingers = f->fingers;
  546. if (priv->proto_version == ALPS_PROTO_V3) {
  547. if (alps_process_bitmap(priv, f) == 0)
  548. fingers = 0; /* Use st data */
  549. /* Now process position packet */
  550. priv->decode_fields(f, priv->multi_data,
  551. psmouse);
  552. } else {
  553. /*
  554. * Because Dolphin uses position packet's
  555. * coordinate data as Pt1 and uses it to
  556. * calculate Pt2, so we need to do position
  557. * packet decode first.
  558. */
  559. priv->decode_fields(f, priv->multi_data,
  560. psmouse);
  561. /*
  562. * Since Dolphin's finger number is reliable,
  563. * there is no need to compare with bmap_fn.
  564. */
  565. alps_process_bitmap_dolphin(priv, f);
  566. }
  567. } else {
  568. priv->multi_packet = 0;
  569. }
  570. }
  571. /*
  572. * Bit 6 of byte 0 is not usually set in position packets. The only
  573. * times it seems to be set is in situations where the data is
  574. * suspect anyway, e.g. a palm resting flat on the touchpad. Given
  575. * this combined with the fact that this bit is useful for filtering
  576. * out misidentified bitmap packets, we reject anything with this
  577. * bit set.
  578. */
  579. if (f->is_mp)
  580. return;
  581. if (!priv->multi_packet && f->first_mp) {
  582. priv->multi_packet = 1;
  583. memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
  584. return;
  585. }
  586. priv->multi_packet = 0;
  587. /*
  588. * Sometimes the hardware sends a single packet with z = 0
  589. * in the middle of a stream. Real releases generate packets
  590. * with x, y, and z all zero, so these seem to be flukes.
  591. * Ignore them.
  592. */
  593. if (f->st.x && f->st.y && !f->pressure)
  594. return;
  595. alps_report_semi_mt_data(psmouse, fingers);
  596. if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
  597. input_report_key(dev2, BTN_LEFT, f->ts_left);
  598. input_report_key(dev2, BTN_RIGHT, f->ts_right);
  599. input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
  600. input_sync(dev2);
  601. }
  602. }
  603. static void alps_process_packet_v3(struct psmouse *psmouse)
  604. {
  605. unsigned char *packet = psmouse->packet;
  606. /*
  607. * v3 protocol packets come in three types, two representing
  608. * touchpad data and one representing trackstick data.
  609. * Trackstick packets seem to be distinguished by always
  610. * having 0x3f in the last byte. This value has never been
  611. * observed in the last byte of either of the other types
  612. * of packets.
  613. */
  614. if (packet[5] == 0x3f) {
  615. alps_process_trackstick_packet_v3(psmouse);
  616. return;
  617. }
  618. alps_process_touchpad_packet_v3_v5(psmouse);
  619. }
  620. static void alps_process_packet_v6(struct psmouse *psmouse)
  621. {
  622. struct alps_data *priv = psmouse->private;
  623. unsigned char *packet = psmouse->packet;
  624. struct input_dev *dev = psmouse->dev;
  625. struct input_dev *dev2 = priv->dev2;
  626. int x, y, z, left, right, middle;
  627. /*
  628. * We can use Byte5 to distinguish if the packet is from Touchpad
  629. * or Trackpoint.
  630. * Touchpad: 0 - 0x7E
  631. * Trackpoint: 0x7F
  632. */
  633. if (packet[5] == 0x7F) {
  634. /* It should be a DualPoint when received Trackpoint packet */
  635. if (!(priv->flags & ALPS_DUALPOINT))
  636. return;
  637. /* Trackpoint packet */
  638. x = packet[1] | ((packet[3] & 0x20) << 2);
  639. y = packet[2] | ((packet[3] & 0x40) << 1);
  640. z = packet[4];
  641. left = packet[3] & 0x01;
  642. right = packet[3] & 0x02;
  643. middle = packet[3] & 0x04;
  644. /* To prevent the cursor jump when finger lifted */
  645. if (x == 0x7F && y == 0x7F && z == 0x7F)
  646. x = y = z = 0;
  647. /* Divide 4 since trackpoint's speed is too fast */
  648. input_report_rel(dev2, REL_X, (char)x / 4);
  649. input_report_rel(dev2, REL_Y, -((char)y / 4));
  650. input_report_key(dev2, BTN_LEFT, left);
  651. input_report_key(dev2, BTN_RIGHT, right);
  652. input_report_key(dev2, BTN_MIDDLE, middle);
  653. input_sync(dev2);
  654. return;
  655. }
  656. /* Touchpad packet */
  657. x = packet[1] | ((packet[3] & 0x78) << 4);
  658. y = packet[2] | ((packet[4] & 0x78) << 4);
  659. z = packet[5];
  660. left = packet[3] & 0x01;
  661. right = packet[3] & 0x02;
  662. if (z > 30)
  663. input_report_key(dev, BTN_TOUCH, 1);
  664. if (z < 25)
  665. input_report_key(dev, BTN_TOUCH, 0);
  666. if (z > 0) {
  667. input_report_abs(dev, ABS_X, x);
  668. input_report_abs(dev, ABS_Y, y);
  669. }
  670. input_report_abs(dev, ABS_PRESSURE, z);
  671. input_report_key(dev, BTN_TOOL_FINGER, z > 0);
  672. /* v6 touchpad does not have middle button */
  673. input_report_key(dev, BTN_LEFT, left);
  674. input_report_key(dev, BTN_RIGHT, right);
  675. input_sync(dev);
  676. }
  677. static void alps_process_packet_v4(struct psmouse *psmouse)
  678. {
  679. struct alps_data *priv = psmouse->private;
  680. unsigned char *packet = psmouse->packet;
  681. struct alps_fields *f = &priv->f;
  682. int offset;
  683. /*
  684. * v4 has a 6-byte encoding for bitmap data, but this data is
  685. * broken up between 3 normal packets. Use priv->multi_packet to
  686. * track our position in the bitmap packet.
  687. */
  688. if (packet[6] & 0x40) {
  689. /* sync, reset position */
  690. priv->multi_packet = 0;
  691. }
  692. if (WARN_ON_ONCE(priv->multi_packet > 2))
  693. return;
  694. offset = 2 * priv->multi_packet;
  695. priv->multi_data[offset] = packet[6];
  696. priv->multi_data[offset + 1] = packet[7];
  697. if (++priv->multi_packet > 2) {
  698. priv->multi_packet = 0;
  699. f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
  700. ((priv->multi_data[3] & 0x60) << 3) |
  701. ((priv->multi_data[0] & 0x3f) << 2) |
  702. ((priv->multi_data[1] & 0x60) >> 5);
  703. f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
  704. ((priv->multi_data[3] & 0x1f) << 5) |
  705. (priv->multi_data[1] & 0x1f);
  706. f->fingers = alps_process_bitmap(priv, f);
  707. }
  708. f->left = !!(packet[4] & 0x01);
  709. f->right = !!(packet[4] & 0x02);
  710. f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
  711. ((packet[0] & 0x30) >> 4);
  712. f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
  713. f->pressure = packet[5] & 0x7f;
  714. alps_report_semi_mt_data(psmouse, f->fingers);
  715. }
  716. static bool alps_is_valid_package_v7(struct psmouse *psmouse)
  717. {
  718. switch (psmouse->pktcnt) {
  719. case 3:
  720. return (psmouse->packet[2] & 0x40) == 0x40;
  721. case 4:
  722. return (psmouse->packet[3] & 0x48) == 0x48;
  723. case 6:
  724. return (psmouse->packet[5] & 0x40) == 0x00;
  725. }
  726. return true;
  727. }
  728. static unsigned char alps_get_packet_id_v7(char *byte)
  729. {
  730. unsigned char packet_id;
  731. if (byte[4] & 0x40)
  732. packet_id = V7_PACKET_ID_TWO;
  733. else if (byte[4] & 0x01)
  734. packet_id = V7_PACKET_ID_MULTI;
  735. else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
  736. packet_id = V7_PACKET_ID_NEW;
  737. else if (byte[1] == 0x00 && byte[4] == 0x00)
  738. packet_id = V7_PACKET_ID_IDLE;
  739. else
  740. packet_id = V7_PACKET_ID_UNKNOWN;
  741. return packet_id;
  742. }
  743. static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
  744. unsigned char *pkt,
  745. unsigned char pkt_id)
  746. {
  747. mt[0].x = ((pkt[2] & 0x80) << 4);
  748. mt[0].x |= ((pkt[2] & 0x3F) << 5);
  749. mt[0].x |= ((pkt[3] & 0x30) >> 1);
  750. mt[0].x |= (pkt[3] & 0x07);
  751. mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
  752. mt[1].x = ((pkt[3] & 0x80) << 4);
  753. mt[1].x |= ((pkt[4] & 0x80) << 3);
  754. mt[1].x |= ((pkt[4] & 0x3F) << 4);
  755. mt[1].y = ((pkt[5] & 0x80) << 3);
  756. mt[1].y |= ((pkt[5] & 0x3F) << 4);
  757. switch (pkt_id) {
  758. case V7_PACKET_ID_TWO:
  759. mt[1].x &= ~0x000F;
  760. mt[1].y |= 0x000F;
  761. break;
  762. case V7_PACKET_ID_MULTI:
  763. mt[1].x &= ~0x003F;
  764. mt[1].y &= ~0x0020;
  765. mt[1].y |= ((pkt[4] & 0x02) << 4);
  766. mt[1].y |= 0x001F;
  767. break;
  768. case V7_PACKET_ID_NEW:
  769. mt[1].x &= ~0x003F;
  770. mt[1].x |= (pkt[0] & 0x20);
  771. mt[1].y |= 0x000F;
  772. break;
  773. }
  774. mt[0].y = 0x7FF - mt[0].y;
  775. mt[1].y = 0x7FF - mt[1].y;
  776. }
  777. static int alps_get_mt_count(struct input_mt_pos *mt)
  778. {
  779. int i, fingers = 0;
  780. for (i = 0; i < MAX_TOUCHES; i++) {
  781. if (mt[i].x != 0 || mt[i].y != 0)
  782. fingers++;
  783. }
  784. return fingers;
  785. }
  786. static int alps_decode_packet_v7(struct alps_fields *f,
  787. unsigned char *p,
  788. struct psmouse *psmouse)
  789. {
  790. struct alps_data *priv = psmouse->private;
  791. unsigned char pkt_id;
  792. pkt_id = alps_get_packet_id_v7(p);
  793. if (pkt_id == V7_PACKET_ID_IDLE)
  794. return 0;
  795. if (pkt_id == V7_PACKET_ID_UNKNOWN)
  796. return -1;
  797. /*
  798. * NEW packets are send to indicate a discontinuity in the finger
  799. * coordinate reporting. Specifically a finger may have moved from
  800. * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
  801. * us.
  802. *
  803. * NEW packets have 3 problems:
  804. * 1) They do not contain middle / right button info (on non clickpads)
  805. * this can be worked around by preserving the old button state
  806. * 2) They do not contain an accurate fingercount, and they are
  807. * typically send when the number of fingers changes. We cannot use
  808. * the old finger count as that may mismatch with the amount of
  809. * touch coordinates we've available in the NEW packet
  810. * 3) Their x data for the second touch is inaccurate leading to
  811. * a possible jump of the x coordinate by 16 units when the first
  812. * non NEW packet comes in
  813. * Since problems 2 & 3 cannot be worked around, just ignore them.
  814. */
  815. if (pkt_id == V7_PACKET_ID_NEW)
  816. return 1;
  817. alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
  818. if (pkt_id == V7_PACKET_ID_TWO)
  819. f->fingers = alps_get_mt_count(f->mt);
  820. else /* pkt_id == V7_PACKET_ID_MULTI */
  821. f->fingers = 3 + (p[5] & 0x03);
  822. f->left = (p[0] & 0x80) >> 7;
  823. if (priv->flags & ALPS_BUTTONPAD) {
  824. if (p[0] & 0x20)
  825. f->fingers++;
  826. if (p[0] & 0x10)
  827. f->fingers++;
  828. } else {
  829. f->right = (p[0] & 0x20) >> 5;
  830. f->middle = (p[0] & 0x10) >> 4;
  831. }
  832. /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
  833. if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
  834. f->mt[0].x = f->mt[1].x;
  835. f->mt[0].y = f->mt[1].y;
  836. f->mt[1].x = 0;
  837. f->mt[1].y = 0;
  838. }
  839. return 0;
  840. }
  841. static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
  842. {
  843. struct alps_data *priv = psmouse->private;
  844. unsigned char *packet = psmouse->packet;
  845. struct input_dev *dev2 = priv->dev2;
  846. int x, y, z, left, right, middle;
  847. /*
  848. * b7 b6 b5 b4 b3 b2 b1 b0
  849. * Byte0 0 1 0 0 1 0 0 0
  850. * Byte1 1 1 * * 1 M R L
  851. * Byte2 X7 1 X5 X4 X3 X2 X1 X0
  852. * Byte3 Z6 1 Y6 X6 1 Y2 Y1 Y0
  853. * Byte4 Y7 0 Y5 Y4 Y3 1 1 0
  854. * Byte5 T&P 0 Z5 Z4 Z3 Z2 Z1 Z0
  855. * M / R / L: Middle / Right / Left button
  856. */
  857. x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
  858. y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
  859. ((packet[3] & 0x20) << 1);
  860. z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
  861. left = (packet[1] & 0x01);
  862. right = (packet[1] & 0x02) >> 1;
  863. middle = (packet[1] & 0x04) >> 2;
  864. /* Divide 2 since trackpoint's speed is too fast */
  865. input_report_rel(dev2, REL_X, (char)x / 2);
  866. input_report_rel(dev2, REL_Y, -((char)y / 2));
  867. input_report_key(dev2, BTN_LEFT, left);
  868. input_report_key(dev2, BTN_RIGHT, right);
  869. input_report_key(dev2, BTN_MIDDLE, middle);
  870. input_sync(dev2);
  871. }
  872. static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
  873. {
  874. struct alps_data *priv = psmouse->private;
  875. struct input_dev *dev = psmouse->dev;
  876. struct alps_fields *f = &priv->f;
  877. memset(f, 0, sizeof(*f));
  878. if (priv->decode_fields(f, psmouse->packet, psmouse))
  879. return;
  880. alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
  881. input_mt_report_finger_count(dev, f->fingers);
  882. input_report_key(dev, BTN_LEFT, f->left);
  883. input_report_key(dev, BTN_RIGHT, f->right);
  884. input_report_key(dev, BTN_MIDDLE, f->middle);
  885. input_sync(dev);
  886. }
  887. static void alps_process_packet_v7(struct psmouse *psmouse)
  888. {
  889. unsigned char *packet = psmouse->packet;
  890. if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
  891. alps_process_trackstick_packet_v7(psmouse);
  892. else
  893. alps_process_touchpad_packet_v7(psmouse);
  894. }
  895. static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
  896. unsigned char packet[],
  897. bool report_buttons)
  898. {
  899. struct alps_data *priv = psmouse->private;
  900. struct input_dev *dev2 = priv->dev2;
  901. if (report_buttons)
  902. alps_report_buttons(psmouse, dev2, psmouse->dev,
  903. packet[0] & 1, packet[0] & 2, packet[0] & 4);
  904. input_report_rel(dev2, REL_X,
  905. packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
  906. input_report_rel(dev2, REL_Y,
  907. packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
  908. input_sync(dev2);
  909. }
  910. static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
  911. {
  912. struct alps_data *priv = psmouse->private;
  913. if (psmouse->pktcnt < 6)
  914. return PSMOUSE_GOOD_DATA;
  915. if (psmouse->pktcnt == 6) {
  916. /*
  917. * Start a timer to flush the packet if it ends up last
  918. * 6-byte packet in the stream. Timer needs to fire
  919. * psmouse core times out itself. 20 ms should be enough
  920. * to decide if we are getting more data or not.
  921. */
  922. mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
  923. return PSMOUSE_GOOD_DATA;
  924. }
  925. del_timer(&priv->timer);
  926. if (psmouse->packet[6] & 0x80) {
  927. /*
  928. * Highest bit is set - that means we either had
  929. * complete ALPS packet and this is start of the
  930. * next packet or we got garbage.
  931. */
  932. if (((psmouse->packet[3] |
  933. psmouse->packet[4] |
  934. psmouse->packet[5]) & 0x80) ||
  935. (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
  936. psmouse_dbg(psmouse,
  937. "refusing packet %4ph (suspected interleaved ps/2)\n",
  938. psmouse->packet + 3);
  939. return PSMOUSE_BAD_DATA;
  940. }
  941. priv->process_packet(psmouse);
  942. /* Continue with the next packet */
  943. psmouse->packet[0] = psmouse->packet[6];
  944. psmouse->pktcnt = 1;
  945. } else {
  946. /*
  947. * High bit is 0 - that means that we indeed got a PS/2
  948. * packet in the middle of ALPS packet.
  949. *
  950. * There is also possibility that we got 6-byte ALPS
  951. * packet followed by 3-byte packet from trackpoint. We
  952. * can not distinguish between these 2 scenarios but
  953. * because the latter is unlikely to happen in course of
  954. * normal operation (user would need to press all
  955. * buttons on the pad and start moving trackpoint
  956. * without touching the pad surface) we assume former.
  957. * Even if we are wrong the wost thing that would happen
  958. * the cursor would jump but we should not get protocol
  959. * de-synchronization.
  960. */
  961. alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
  962. false);
  963. /*
  964. * Continue with the standard ALPS protocol handling,
  965. * but make sure we won't process it as an interleaved
  966. * packet again, which may happen if all buttons are
  967. * pressed. To avoid this let's reset the 4th bit which
  968. * is normally 1.
  969. */
  970. psmouse->packet[3] = psmouse->packet[6] & 0xf7;
  971. psmouse->pktcnt = 4;
  972. }
  973. return PSMOUSE_GOOD_DATA;
  974. }
  975. static void alps_flush_packet(unsigned long data)
  976. {
  977. struct psmouse *psmouse = (struct psmouse *)data;
  978. struct alps_data *priv = psmouse->private;
  979. serio_pause_rx(psmouse->ps2dev.serio);
  980. if (psmouse->pktcnt == psmouse->pktsize) {
  981. /*
  982. * We did not any more data in reasonable amount of time.
  983. * Validate the last 3 bytes and process as a standard
  984. * ALPS packet.
  985. */
  986. if ((psmouse->packet[3] |
  987. psmouse->packet[4] |
  988. psmouse->packet[5]) & 0x80) {
  989. psmouse_dbg(psmouse,
  990. "refusing packet %3ph (suspected interleaved ps/2)\n",
  991. psmouse->packet + 3);
  992. } else {
  993. priv->process_packet(psmouse);
  994. }
  995. psmouse->pktcnt = 0;
  996. }
  997. serio_continue_rx(psmouse->ps2dev.serio);
  998. }
  999. static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
  1000. {
  1001. struct alps_data *priv = psmouse->private;
  1002. /*
  1003. * Check if we are dealing with a bare PS/2 packet, presumably from
  1004. * a device connected to the external PS/2 port. Because bare PS/2
  1005. * protocol does not have enough constant bits to self-synchronize
  1006. * properly we only do this if the device is fully synchronized.
  1007. */
  1008. if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
  1009. if (psmouse->pktcnt == 3) {
  1010. alps_report_bare_ps2_packet(psmouse, psmouse->packet,
  1011. true);
  1012. return PSMOUSE_FULL_PACKET;
  1013. }
  1014. return PSMOUSE_GOOD_DATA;
  1015. }
  1016. /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
  1017. if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
  1018. psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
  1019. return alps_handle_interleaved_ps2(psmouse);
  1020. }
  1021. if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
  1022. psmouse_dbg(psmouse,
  1023. "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
  1024. psmouse->packet[0], priv->mask0, priv->byte0);
  1025. return PSMOUSE_BAD_DATA;
  1026. }
  1027. /* Bytes 2 - pktsize should have 0 in the highest bit */
  1028. if (priv->proto_version < ALPS_PROTO_V5 &&
  1029. psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
  1030. (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
  1031. psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
  1032. psmouse->pktcnt - 1,
  1033. psmouse->packet[psmouse->pktcnt - 1]);
  1034. if (priv->proto_version == ALPS_PROTO_V3 &&
  1035. psmouse->pktcnt == psmouse->pktsize) {
  1036. /*
  1037. * Some Dell boxes, such as Latitude E6440 or E7440
  1038. * with closed lid, quite often smash last byte of
  1039. * otherwise valid packet with 0xff. Given that the
  1040. * next packet is very likely to be valid let's
  1041. * report PSMOUSE_FULL_PACKET but not process data,
  1042. * rather than reporting PSMOUSE_BAD_DATA and
  1043. * filling the logs.
  1044. */
  1045. return PSMOUSE_FULL_PACKET;
  1046. }
  1047. return PSMOUSE_BAD_DATA;
  1048. }
  1049. if (priv->proto_version == ALPS_PROTO_V7 &&
  1050. !alps_is_valid_package_v7(psmouse)) {
  1051. psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
  1052. psmouse->pktcnt - 1,
  1053. psmouse->packet[psmouse->pktcnt - 1]);
  1054. return PSMOUSE_BAD_DATA;
  1055. }
  1056. if (psmouse->pktcnt == psmouse->pktsize) {
  1057. priv->process_packet(psmouse);
  1058. return PSMOUSE_FULL_PACKET;
  1059. }
  1060. return PSMOUSE_GOOD_DATA;
  1061. }
  1062. static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
  1063. {
  1064. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1065. struct alps_data *priv = psmouse->private;
  1066. int command;
  1067. unsigned char *param;
  1068. unsigned char dummy[4];
  1069. BUG_ON(nibble > 0xf);
  1070. command = priv->nibble_commands[nibble].command;
  1071. param = (command & 0x0f00) ?
  1072. dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
  1073. if (ps2_command(ps2dev, param, command))
  1074. return -1;
  1075. return 0;
  1076. }
  1077. static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
  1078. {
  1079. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1080. struct alps_data *priv = psmouse->private;
  1081. int i, nibble;
  1082. if (ps2_command(ps2dev, NULL, priv->addr_command))
  1083. return -1;
  1084. for (i = 12; i >= 0; i -= 4) {
  1085. nibble = (addr >> i) & 0xf;
  1086. if (alps_command_mode_send_nibble(psmouse, nibble))
  1087. return -1;
  1088. }
  1089. return 0;
  1090. }
  1091. static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
  1092. {
  1093. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1094. unsigned char param[4];
  1095. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  1096. return -1;
  1097. /*
  1098. * The address being read is returned in the first two bytes
  1099. * of the result. Check that this address matches the expected
  1100. * address.
  1101. */
  1102. if (addr != ((param[0] << 8) | param[1]))
  1103. return -1;
  1104. return param[2];
  1105. }
  1106. static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
  1107. {
  1108. if (alps_command_mode_set_addr(psmouse, addr))
  1109. return -1;
  1110. return __alps_command_mode_read_reg(psmouse, addr);
  1111. }
  1112. static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
  1113. {
  1114. if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
  1115. return -1;
  1116. if (alps_command_mode_send_nibble(psmouse, value & 0xf))
  1117. return -1;
  1118. return 0;
  1119. }
  1120. static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
  1121. u8 value)
  1122. {
  1123. if (alps_command_mode_set_addr(psmouse, addr))
  1124. return -1;
  1125. return __alps_command_mode_write_reg(psmouse, value);
  1126. }
  1127. static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
  1128. int repeated_command, unsigned char *param)
  1129. {
  1130. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1131. param[0] = 0;
  1132. if (init_command && ps2_command(ps2dev, param, init_command))
  1133. return -EIO;
  1134. if (ps2_command(ps2dev, NULL, repeated_command) ||
  1135. ps2_command(ps2dev, NULL, repeated_command) ||
  1136. ps2_command(ps2dev, NULL, repeated_command))
  1137. return -EIO;
  1138. param[0] = param[1] = param[2] = 0xff;
  1139. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  1140. return -EIO;
  1141. psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
  1142. repeated_command, param);
  1143. return 0;
  1144. }
  1145. static bool alps_check_valid_firmware_id(unsigned char id[])
  1146. {
  1147. if (id[0] == 0x73)
  1148. return true;
  1149. if (id[0] == 0x88 &&
  1150. (id[1] == 0x07 ||
  1151. id[1] == 0x08 ||
  1152. (id[1] & 0xf0) == 0xb0 ||
  1153. (id[1] & 0xf0) == 0xc0)) {
  1154. return true;
  1155. }
  1156. return false;
  1157. }
  1158. static int alps_enter_command_mode(struct psmouse *psmouse)
  1159. {
  1160. unsigned char param[4];
  1161. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
  1162. psmouse_err(psmouse, "failed to enter command mode\n");
  1163. return -1;
  1164. }
  1165. if (!alps_check_valid_firmware_id(param)) {
  1166. psmouse_dbg(psmouse,
  1167. "unknown response while entering command mode\n");
  1168. return -1;
  1169. }
  1170. return 0;
  1171. }
  1172. static inline int alps_exit_command_mode(struct psmouse *psmouse)
  1173. {
  1174. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1175. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
  1176. return -1;
  1177. return 0;
  1178. }
  1179. /*
  1180. * For DualPoint devices select the device that should respond to
  1181. * subsequent commands. It looks like glidepad is behind stickpointer,
  1182. * I'd thought it would be other way around...
  1183. */
  1184. static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
  1185. {
  1186. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1187. int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
  1188. if (ps2_command(ps2dev, NULL, cmd) ||
  1189. ps2_command(ps2dev, NULL, cmd) ||
  1190. ps2_command(ps2dev, NULL, cmd) ||
  1191. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  1192. return -1;
  1193. /* we may get 3 more bytes, just ignore them */
  1194. ps2_drain(ps2dev, 3, 100);
  1195. return 0;
  1196. }
  1197. static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
  1198. {
  1199. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1200. /* Try ALPS magic knock - 4 disable before enable */
  1201. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1202. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1203. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1204. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1205. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
  1206. return -1;
  1207. /*
  1208. * Switch mouse to poll (remote) mode so motion data will not
  1209. * get in our way
  1210. */
  1211. return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
  1212. }
  1213. static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
  1214. {
  1215. int i, nibble;
  1216. /*
  1217. * b0-b11 are valid bits, send sequence is inverse.
  1218. * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
  1219. */
  1220. for (i = 0; i <= 8; i += 4) {
  1221. nibble = (word >> i) & 0xf;
  1222. if (alps_command_mode_send_nibble(psmouse, nibble))
  1223. return -1;
  1224. }
  1225. return 0;
  1226. }
  1227. static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
  1228. u16 addr, u16 value)
  1229. {
  1230. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1231. /* 0x0A0 is the command to write the word */
  1232. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
  1233. alps_monitor_mode_send_word(psmouse, 0x0A0) ||
  1234. alps_monitor_mode_send_word(psmouse, addr) ||
  1235. alps_monitor_mode_send_word(psmouse, value) ||
  1236. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  1237. return -1;
  1238. return 0;
  1239. }
  1240. static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
  1241. {
  1242. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1243. if (enable) {
  1244. /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
  1245. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
  1246. ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
  1247. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1248. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1249. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  1250. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1251. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  1252. ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
  1253. return -1;
  1254. } else {
  1255. /* EC to exit monitor mode */
  1256. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
  1257. return -1;
  1258. }
  1259. return 0;
  1260. }
  1261. static int alps_absolute_mode_v6(struct psmouse *psmouse)
  1262. {
  1263. u16 reg_val = 0x181;
  1264. int ret = -1;
  1265. /* enter monitor mode, to write the register */
  1266. if (alps_monitor_mode(psmouse, true))
  1267. return -1;
  1268. ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
  1269. if (alps_monitor_mode(psmouse, false))
  1270. ret = -1;
  1271. return ret;
  1272. }
  1273. static int alps_get_status(struct psmouse *psmouse, char *param)
  1274. {
  1275. /* Get status: 0xF5 0xF5 0xF5 0xE9 */
  1276. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
  1277. return -1;
  1278. return 0;
  1279. }
  1280. /*
  1281. * Turn touchpad tapping on or off. The sequences are:
  1282. * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
  1283. * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
  1284. * My guess that 0xE9 (GetInfo) is here as a sync point.
  1285. * For models that also have stickpointer (DualPoints) its tapping
  1286. * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
  1287. * we don't fiddle with it.
  1288. */
  1289. static int alps_tap_mode(struct psmouse *psmouse, int enable)
  1290. {
  1291. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1292. int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
  1293. unsigned char tap_arg = enable ? 0x0A : 0x00;
  1294. unsigned char param[4];
  1295. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
  1296. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1297. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1298. ps2_command(ps2dev, &tap_arg, cmd))
  1299. return -1;
  1300. if (alps_get_status(psmouse, param))
  1301. return -1;
  1302. return 0;
  1303. }
  1304. /*
  1305. * alps_poll() - poll the touchpad for current motion packet.
  1306. * Used in resync.
  1307. */
  1308. static int alps_poll(struct psmouse *psmouse)
  1309. {
  1310. struct alps_data *priv = psmouse->private;
  1311. unsigned char buf[sizeof(psmouse->packet)];
  1312. bool poll_failed;
  1313. if (priv->flags & ALPS_PASS)
  1314. alps_passthrough_mode_v2(psmouse, true);
  1315. poll_failed = ps2_command(&psmouse->ps2dev, buf,
  1316. PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
  1317. if (priv->flags & ALPS_PASS)
  1318. alps_passthrough_mode_v2(psmouse, false);
  1319. if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
  1320. return -1;
  1321. if ((psmouse->badbyte & 0xc8) == 0x08) {
  1322. /*
  1323. * Poll the track stick ...
  1324. */
  1325. if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
  1326. return -1;
  1327. }
  1328. memcpy(psmouse->packet, buf, sizeof(buf));
  1329. return 0;
  1330. }
  1331. static int alps_hw_init_v1_v2(struct psmouse *psmouse)
  1332. {
  1333. struct alps_data *priv = psmouse->private;
  1334. if ((priv->flags & ALPS_PASS) &&
  1335. alps_passthrough_mode_v2(psmouse, true)) {
  1336. return -1;
  1337. }
  1338. if (alps_tap_mode(psmouse, true)) {
  1339. psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
  1340. return -1;
  1341. }
  1342. if (alps_absolute_mode_v1_v2(psmouse)) {
  1343. psmouse_err(psmouse, "Failed to enable absolute mode\n");
  1344. return -1;
  1345. }
  1346. if ((priv->flags & ALPS_PASS) &&
  1347. alps_passthrough_mode_v2(psmouse, false)) {
  1348. return -1;
  1349. }
  1350. /* ALPS needs stream mode, otherwise it won't report any data */
  1351. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
  1352. psmouse_err(psmouse, "Failed to enable stream mode\n");
  1353. return -1;
  1354. }
  1355. return 0;
  1356. }
  1357. static int alps_hw_init_v6(struct psmouse *psmouse)
  1358. {
  1359. unsigned char param[2] = {0xC8, 0x14};
  1360. /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
  1361. if (alps_passthrough_mode_v2(psmouse, true))
  1362. return -1;
  1363. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1364. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1365. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1366. ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1367. ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
  1368. return -1;
  1369. if (alps_passthrough_mode_v2(psmouse, false))
  1370. return -1;
  1371. if (alps_absolute_mode_v6(psmouse)) {
  1372. psmouse_err(psmouse, "Failed to enable absolute mode\n");
  1373. return -1;
  1374. }
  1375. return 0;
  1376. }
  1377. /*
  1378. * Enable or disable passthrough mode to the trackstick.
  1379. */
  1380. static int alps_passthrough_mode_v3(struct psmouse *psmouse,
  1381. int reg_base, bool enable)
  1382. {
  1383. int reg_val, ret = -1;
  1384. if (alps_enter_command_mode(psmouse))
  1385. return -1;
  1386. reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
  1387. if (reg_val == -1)
  1388. goto error;
  1389. if (enable)
  1390. reg_val |= 0x01;
  1391. else
  1392. reg_val &= ~0x01;
  1393. ret = __alps_command_mode_write_reg(psmouse, reg_val);
  1394. error:
  1395. if (alps_exit_command_mode(psmouse))
  1396. ret = -1;
  1397. return ret;
  1398. }
  1399. /* Must be in command mode when calling this function */
  1400. static int alps_absolute_mode_v3(struct psmouse *psmouse)
  1401. {
  1402. int reg_val;
  1403. reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
  1404. if (reg_val == -1)
  1405. return -1;
  1406. reg_val |= 0x06;
  1407. if (__alps_command_mode_write_reg(psmouse, reg_val))
  1408. return -1;
  1409. return 0;
  1410. }
  1411. static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
  1412. {
  1413. int ret = -EIO, reg_val;
  1414. if (alps_enter_command_mode(psmouse))
  1415. goto error;
  1416. reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
  1417. if (reg_val == -1)
  1418. goto error;
  1419. /* bit 7: trackstick is present */
  1420. ret = reg_val & 0x80 ? 0 : -ENODEV;
  1421. error:
  1422. alps_exit_command_mode(psmouse);
  1423. return ret;
  1424. }
  1425. static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
  1426. {
  1427. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1428. int ret = 0;
  1429. unsigned char param[4];
  1430. if (alps_passthrough_mode_v3(psmouse, reg_base, true))
  1431. return -EIO;
  1432. /*
  1433. * E7 report for the trackstick
  1434. *
  1435. * There have been reports of failures to seem to trace back
  1436. * to the above trackstick check failing. When these occur
  1437. * this E7 report fails, so when that happens we continue
  1438. * with the assumption that there isn't a trackstick after
  1439. * all.
  1440. */
  1441. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
  1442. psmouse_warn(psmouse, "trackstick E7 report failed\n");
  1443. ret = -ENODEV;
  1444. } else {
  1445. psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
  1446. /*
  1447. * Not sure what this does, but it is absolutely
  1448. * essential. Without it, the touchpad does not
  1449. * work at all and the trackstick just emits normal
  1450. * PS/2 packets.
  1451. */
  1452. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1453. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1454. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1455. alps_command_mode_send_nibble(psmouse, 0x9) ||
  1456. alps_command_mode_send_nibble(psmouse, 0x4)) {
  1457. psmouse_err(psmouse,
  1458. "Error sending magic E6 sequence\n");
  1459. ret = -EIO;
  1460. goto error;
  1461. }
  1462. /*
  1463. * This ensures the trackstick packets are in the format
  1464. * supported by this driver. If bit 1 isn't set the packet
  1465. * format is different.
  1466. */
  1467. if (alps_enter_command_mode(psmouse) ||
  1468. alps_command_mode_write_reg(psmouse,
  1469. reg_base + 0x08, 0x82) ||
  1470. alps_exit_command_mode(psmouse))
  1471. ret = -EIO;
  1472. }
  1473. error:
  1474. if (alps_passthrough_mode_v3(psmouse, reg_base, false))
  1475. ret = -EIO;
  1476. return ret;
  1477. }
  1478. static int alps_hw_init_v3(struct psmouse *psmouse)
  1479. {
  1480. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1481. int reg_val;
  1482. unsigned char param[4];
  1483. reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
  1484. if (reg_val == -EIO)
  1485. goto error;
  1486. if (reg_val == 0 &&
  1487. alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
  1488. goto error;
  1489. if (alps_enter_command_mode(psmouse) ||
  1490. alps_absolute_mode_v3(psmouse)) {
  1491. psmouse_err(psmouse, "Failed to enter absolute mode\n");
  1492. goto error;
  1493. }
  1494. reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
  1495. if (reg_val == -1)
  1496. goto error;
  1497. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
  1498. goto error;
  1499. reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
  1500. if (reg_val == -1)
  1501. goto error;
  1502. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
  1503. goto error;
  1504. if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
  1505. goto error;
  1506. if (__alps_command_mode_write_reg(psmouse, 0x04))
  1507. goto error;
  1508. if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
  1509. goto error;
  1510. if (__alps_command_mode_write_reg(psmouse, 0x03))
  1511. goto error;
  1512. if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
  1513. goto error;
  1514. if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
  1515. goto error;
  1516. if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
  1517. goto error;
  1518. if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
  1519. goto error;
  1520. alps_exit_command_mode(psmouse);
  1521. /* Set rate and enable data reporting */
  1522. param[0] = 0x64;
  1523. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
  1524. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  1525. psmouse_err(psmouse, "Failed to enable data reporting\n");
  1526. return -1;
  1527. }
  1528. return 0;
  1529. error:
  1530. /*
  1531. * Leaving the touchpad in command mode will essentially render
  1532. * it unusable until the machine reboots, so exit it here just
  1533. * to be safe
  1534. */
  1535. alps_exit_command_mode(psmouse);
  1536. return -1;
  1537. }
  1538. static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
  1539. {
  1540. int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
  1541. struct alps_data *priv = psmouse->private;
  1542. reg = alps_command_mode_read_reg(psmouse, reg_pitch);
  1543. if (reg < 0)
  1544. return reg;
  1545. x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
  1546. x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
  1547. y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
  1548. y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
  1549. reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
  1550. if (reg < 0)
  1551. return reg;
  1552. x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
  1553. x_electrode = 17 + x_electrode;
  1554. y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
  1555. y_electrode = 13 + y_electrode;
  1556. x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
  1557. y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
  1558. priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
  1559. priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
  1560. psmouse_dbg(psmouse,
  1561. "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
  1562. x_pitch, y_pitch, x_electrode, y_electrode,
  1563. x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
  1564. return 0;
  1565. }
  1566. static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
  1567. {
  1568. struct alps_data *priv = psmouse->private;
  1569. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1570. int reg_val, ret = -1;
  1571. if (priv->flags & ALPS_DUALPOINT) {
  1572. reg_val = alps_setup_trackstick_v3(psmouse,
  1573. ALPS_REG_BASE_RUSHMORE);
  1574. if (reg_val == -EIO)
  1575. goto error;
  1576. if (reg_val == -ENODEV)
  1577. priv->flags &= ~ALPS_DUALPOINT;
  1578. }
  1579. if (alps_enter_command_mode(psmouse) ||
  1580. alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
  1581. alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
  1582. goto error;
  1583. if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
  1584. goto error;
  1585. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
  1586. if (reg_val == -1)
  1587. goto error;
  1588. if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
  1589. goto error;
  1590. if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
  1591. goto error;
  1592. /* enter absolute mode */
  1593. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
  1594. if (reg_val == -1)
  1595. goto error;
  1596. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
  1597. goto error;
  1598. alps_exit_command_mode(psmouse);
  1599. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  1600. error:
  1601. alps_exit_command_mode(psmouse);
  1602. return ret;
  1603. }
  1604. /* Must be in command mode when calling this function */
  1605. static int alps_absolute_mode_v4(struct psmouse *psmouse)
  1606. {
  1607. int reg_val;
  1608. reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
  1609. if (reg_val == -1)
  1610. return -1;
  1611. reg_val |= 0x02;
  1612. if (__alps_command_mode_write_reg(psmouse, reg_val))
  1613. return -1;
  1614. return 0;
  1615. }
  1616. static int alps_hw_init_v4(struct psmouse *psmouse)
  1617. {
  1618. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1619. unsigned char param[4];
  1620. if (alps_enter_command_mode(psmouse))
  1621. goto error;
  1622. if (alps_absolute_mode_v4(psmouse)) {
  1623. psmouse_err(psmouse, "Failed to enter absolute mode\n");
  1624. goto error;
  1625. }
  1626. if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
  1627. goto error;
  1628. if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
  1629. goto error;
  1630. if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
  1631. goto error;
  1632. if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
  1633. goto error;
  1634. if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
  1635. goto error;
  1636. if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
  1637. goto error;
  1638. if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
  1639. goto error;
  1640. if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
  1641. goto error;
  1642. alps_exit_command_mode(psmouse);
  1643. /*
  1644. * This sequence changes the output from a 9-byte to an
  1645. * 8-byte format. All the same data seems to be present,
  1646. * just in a more compact format.
  1647. */
  1648. param[0] = 0xc8;
  1649. param[1] = 0x64;
  1650. param[2] = 0x50;
  1651. if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1652. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
  1653. ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
  1654. ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
  1655. return -1;
  1656. /* Set rate and enable data reporting */
  1657. param[0] = 0x64;
  1658. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
  1659. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  1660. psmouse_err(psmouse, "Failed to enable data reporting\n");
  1661. return -1;
  1662. }
  1663. return 0;
  1664. error:
  1665. /*
  1666. * Leaving the touchpad in command mode will essentially render
  1667. * it unusable until the machine reboots, so exit it here just
  1668. * to be safe
  1669. */
  1670. alps_exit_command_mode(psmouse);
  1671. return -1;
  1672. }
  1673. static int alps_dolphin_get_device_area(struct psmouse *psmouse,
  1674. struct alps_data *priv)
  1675. {
  1676. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1677. unsigned char param[4] = {0};
  1678. int num_x_electrode, num_y_electrode;
  1679. if (alps_enter_command_mode(psmouse))
  1680. return -1;
  1681. param[0] = 0x0a;
  1682. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
  1683. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  1684. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  1685. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1686. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
  1687. return -1;
  1688. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  1689. return -1;
  1690. /*
  1691. * Dolphin's sensor line number is not fixed. It can be calculated
  1692. * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
  1693. * Further more, we can get device's x_max and y_max by multiplying
  1694. * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
  1695. *
  1696. * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
  1697. * real sensor line number X = 11 + 8 = 19, and
  1698. * real sensor line number Y = 8 + 1 = 9.
  1699. * So, x_max = (19 - 1) * 64 = 1152, and
  1700. * y_max = (9 - 1) * 64 = 512.
  1701. */
  1702. num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
  1703. num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
  1704. priv->x_bits = num_x_electrode;
  1705. priv->y_bits = num_y_electrode;
  1706. priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
  1707. priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
  1708. if (alps_exit_command_mode(psmouse))
  1709. return -1;
  1710. return 0;
  1711. }
  1712. static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
  1713. {
  1714. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1715. unsigned char param[2];
  1716. /* This is dolphin "v1" as empirically defined by florin9doi */
  1717. param[0] = 0x64;
  1718. param[1] = 0x28;
  1719. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  1720. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1721. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
  1722. return -1;
  1723. return 0;
  1724. }
  1725. static int alps_hw_init_v7(struct psmouse *psmouse)
  1726. {
  1727. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1728. int reg_val, ret = -1;
  1729. if (alps_enter_command_mode(psmouse) ||
  1730. alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
  1731. goto error;
  1732. if (alps_get_v3_v7_resolution(psmouse, 0xc397))
  1733. goto error;
  1734. if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
  1735. goto error;
  1736. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
  1737. if (reg_val == -1)
  1738. goto error;
  1739. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
  1740. goto error;
  1741. alps_exit_command_mode(psmouse);
  1742. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  1743. error:
  1744. alps_exit_command_mode(psmouse);
  1745. return ret;
  1746. }
  1747. static void alps_set_defaults(struct alps_data *priv)
  1748. {
  1749. priv->byte0 = 0x8f;
  1750. priv->mask0 = 0x8f;
  1751. priv->flags = ALPS_DUALPOINT;
  1752. priv->x_max = 2000;
  1753. priv->y_max = 1400;
  1754. priv->x_bits = 15;
  1755. priv->y_bits = 11;
  1756. switch (priv->proto_version) {
  1757. case ALPS_PROTO_V1:
  1758. case ALPS_PROTO_V2:
  1759. priv->hw_init = alps_hw_init_v1_v2;
  1760. priv->process_packet = alps_process_packet_v1_v2;
  1761. priv->set_abs_params = alps_set_abs_params_st;
  1762. priv->x_max = 1023;
  1763. priv->y_max = 767;
  1764. break;
  1765. case ALPS_PROTO_V3:
  1766. priv->hw_init = alps_hw_init_v3;
  1767. priv->process_packet = alps_process_packet_v3;
  1768. priv->set_abs_params = alps_set_abs_params_mt;
  1769. priv->decode_fields = alps_decode_pinnacle;
  1770. priv->nibble_commands = alps_v3_nibble_commands;
  1771. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  1772. break;
  1773. case ALPS_PROTO_V4:
  1774. priv->hw_init = alps_hw_init_v4;
  1775. priv->process_packet = alps_process_packet_v4;
  1776. priv->set_abs_params = alps_set_abs_params_mt;
  1777. priv->nibble_commands = alps_v4_nibble_commands;
  1778. priv->addr_command = PSMOUSE_CMD_DISABLE;
  1779. break;
  1780. case ALPS_PROTO_V5:
  1781. priv->hw_init = alps_hw_init_dolphin_v1;
  1782. priv->process_packet = alps_process_touchpad_packet_v3_v5;
  1783. priv->decode_fields = alps_decode_dolphin;
  1784. priv->set_abs_params = alps_set_abs_params_mt;
  1785. priv->nibble_commands = alps_v3_nibble_commands;
  1786. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  1787. priv->byte0 = 0xc8;
  1788. priv->mask0 = 0xd8;
  1789. priv->flags = 0;
  1790. priv->x_max = 1360;
  1791. priv->y_max = 660;
  1792. priv->x_bits = 23;
  1793. priv->y_bits = 12;
  1794. break;
  1795. case ALPS_PROTO_V6:
  1796. priv->hw_init = alps_hw_init_v6;
  1797. priv->process_packet = alps_process_packet_v6;
  1798. priv->set_abs_params = alps_set_abs_params_st;
  1799. priv->nibble_commands = alps_v6_nibble_commands;
  1800. priv->x_max = 2047;
  1801. priv->y_max = 1535;
  1802. break;
  1803. case ALPS_PROTO_V7:
  1804. priv->hw_init = alps_hw_init_v7;
  1805. priv->process_packet = alps_process_packet_v7;
  1806. priv->decode_fields = alps_decode_packet_v7;
  1807. priv->set_abs_params = alps_set_abs_params_mt;
  1808. priv->nibble_commands = alps_v3_nibble_commands;
  1809. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  1810. priv->x_max = 0xfff;
  1811. priv->y_max = 0x7ff;
  1812. priv->byte0 = 0x48;
  1813. priv->mask0 = 0x48;
  1814. if (priv->fw_ver[1] != 0xba)
  1815. priv->flags |= ALPS_BUTTONPAD;
  1816. break;
  1817. }
  1818. }
  1819. static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
  1820. unsigned char *e7, unsigned char *ec)
  1821. {
  1822. const struct alps_model_info *model;
  1823. int i;
  1824. for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
  1825. model = &alps_model_data[i];
  1826. if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
  1827. (!model->command_mode_resp ||
  1828. model->command_mode_resp == ec[2])) {
  1829. priv->proto_version = model->proto_version;
  1830. alps_set_defaults(priv);
  1831. priv->flags = model->flags;
  1832. priv->byte0 = model->byte0;
  1833. priv->mask0 = model->mask0;
  1834. return 0;
  1835. }
  1836. }
  1837. return -EINVAL;
  1838. }
  1839. static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
  1840. {
  1841. unsigned char e6[4], e7[4], ec[4];
  1842. /*
  1843. * First try "E6 report".
  1844. * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
  1845. * The bits 0-2 of the first byte will be 1s if some buttons are
  1846. * pressed.
  1847. */
  1848. if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  1849. PSMOUSE_CMD_SETSCALE11, e6))
  1850. return -EIO;
  1851. if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
  1852. return -EINVAL;
  1853. /*
  1854. * Now get the "E7" and "EC" reports. These will uniquely identify
  1855. * most ALPS touchpads.
  1856. */
  1857. if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  1858. PSMOUSE_CMD_SETSCALE21, e7) ||
  1859. alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  1860. PSMOUSE_CMD_RESET_WRAP, ec) ||
  1861. alps_exit_command_mode(psmouse))
  1862. return -EIO;
  1863. /* Save the Firmware version */
  1864. memcpy(priv->fw_ver, ec, 3);
  1865. if (alps_match_table(psmouse, priv, e7, ec) == 0) {
  1866. return 0;
  1867. } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
  1868. ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
  1869. priv->proto_version = ALPS_PROTO_V5;
  1870. alps_set_defaults(priv);
  1871. if (alps_dolphin_get_device_area(psmouse, priv))
  1872. return -EIO;
  1873. else
  1874. return 0;
  1875. } else if (ec[0] == 0x88 &&
  1876. ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
  1877. priv->proto_version = ALPS_PROTO_V7;
  1878. alps_set_defaults(priv);
  1879. return 0;
  1880. } else if (ec[0] == 0x88 && ec[1] == 0x08) {
  1881. priv->proto_version = ALPS_PROTO_V3;
  1882. alps_set_defaults(priv);
  1883. priv->hw_init = alps_hw_init_rushmore_v3;
  1884. priv->decode_fields = alps_decode_rushmore;
  1885. priv->x_bits = 16;
  1886. priv->y_bits = 12;
  1887. priv->flags |= ALPS_IS_RUSHMORE;
  1888. /* hack to make addr_command, nibble_command available */
  1889. psmouse->private = priv;
  1890. if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
  1891. priv->flags &= ~ALPS_DUALPOINT;
  1892. return 0;
  1893. } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
  1894. ec[2] >= 0x90 && ec[2] <= 0x9d) {
  1895. priv->proto_version = ALPS_PROTO_V3;
  1896. alps_set_defaults(priv);
  1897. return 0;
  1898. }
  1899. psmouse_dbg(psmouse,
  1900. "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
  1901. return -EINVAL;
  1902. }
  1903. static int alps_reconnect(struct psmouse *psmouse)
  1904. {
  1905. struct alps_data *priv = psmouse->private;
  1906. psmouse_reset(psmouse);
  1907. if (alps_identify(psmouse, priv) < 0)
  1908. return -1;
  1909. return priv->hw_init(psmouse);
  1910. }
  1911. static void alps_disconnect(struct psmouse *psmouse)
  1912. {
  1913. struct alps_data *priv = psmouse->private;
  1914. psmouse_reset(psmouse);
  1915. del_timer_sync(&priv->timer);
  1916. input_unregister_device(priv->dev2);
  1917. kfree(priv);
  1918. }
  1919. static void alps_set_abs_params_st(struct alps_data *priv,
  1920. struct input_dev *dev1)
  1921. {
  1922. input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
  1923. input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
  1924. }
  1925. static void alps_set_abs_params_mt(struct alps_data *priv,
  1926. struct input_dev *dev1)
  1927. {
  1928. input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
  1929. input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
  1930. input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
  1931. input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
  1932. input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
  1933. INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
  1934. set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
  1935. set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
  1936. /* V7 is real multi-touch */
  1937. if (priv->proto_version == ALPS_PROTO_V7)
  1938. clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
  1939. }
  1940. int alps_init(struct psmouse *psmouse)
  1941. {
  1942. struct alps_data *priv;
  1943. struct input_dev *dev1 = psmouse->dev, *dev2;
  1944. priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
  1945. dev2 = input_allocate_device();
  1946. if (!priv || !dev2)
  1947. goto init_fail;
  1948. priv->dev2 = dev2;
  1949. setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
  1950. psmouse->private = priv;
  1951. psmouse_reset(psmouse);
  1952. if (alps_identify(psmouse, priv) < 0)
  1953. goto init_fail;
  1954. if (priv->hw_init(psmouse))
  1955. goto init_fail;
  1956. /*
  1957. * Undo part of setup done for us by psmouse core since touchpad
  1958. * is not a relative device.
  1959. */
  1960. __clear_bit(EV_REL, dev1->evbit);
  1961. __clear_bit(REL_X, dev1->relbit);
  1962. __clear_bit(REL_Y, dev1->relbit);
  1963. /*
  1964. * Now set up our capabilities.
  1965. */
  1966. dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
  1967. dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
  1968. dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
  1969. dev1->keybit[BIT_WORD(BTN_LEFT)] |=
  1970. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
  1971. dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
  1972. priv->set_abs_params(priv, dev1);
  1973. /* No pressure on V7 */
  1974. if (priv->proto_version != ALPS_PROTO_V7)
  1975. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  1976. if (priv->flags & ALPS_WHEEL) {
  1977. dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
  1978. dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
  1979. }
  1980. if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  1981. dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
  1982. dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
  1983. }
  1984. if (priv->flags & ALPS_FOUR_BUTTONS) {
  1985. dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
  1986. dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
  1987. dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
  1988. dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
  1989. } else if (priv->flags & ALPS_BUTTONPAD) {
  1990. set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
  1991. clear_bit(BTN_RIGHT, dev1->keybit);
  1992. } else {
  1993. dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
  1994. }
  1995. snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
  1996. dev2->phys = priv->phys;
  1997. dev2->name = (priv->flags & ALPS_DUALPOINT) ?
  1998. "DualPoint Stick" : "ALPS PS/2 Device";
  1999. dev2->id.bustype = BUS_I8042;
  2000. dev2->id.vendor = 0x0002;
  2001. dev2->id.product = PSMOUSE_ALPS;
  2002. dev2->id.version = 0x0000;
  2003. dev2->dev.parent = &psmouse->ps2dev.serio->dev;
  2004. dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  2005. dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  2006. dev2->keybit[BIT_WORD(BTN_LEFT)] =
  2007. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
  2008. __set_bit(INPUT_PROP_POINTER, dev2->propbit);
  2009. if (priv->flags & ALPS_DUALPOINT)
  2010. __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
  2011. if (input_register_device(priv->dev2))
  2012. goto init_fail;
  2013. psmouse->protocol_handler = alps_process_byte;
  2014. psmouse->poll = alps_poll;
  2015. psmouse->disconnect = alps_disconnect;
  2016. psmouse->reconnect = alps_reconnect;
  2017. psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
  2018. /* We are having trouble resyncing ALPS touchpads so disable it for now */
  2019. psmouse->resync_time = 0;
  2020. /* Allow 2 invalid packets without resetting device */
  2021. psmouse->resetafter = psmouse->pktsize * 2;
  2022. return 0;
  2023. init_fail:
  2024. psmouse_reset(psmouse);
  2025. input_free_device(dev2);
  2026. kfree(priv);
  2027. psmouse->private = NULL;
  2028. return -1;
  2029. }
  2030. int alps_detect(struct psmouse *psmouse, bool set_properties)
  2031. {
  2032. struct alps_data dummy;
  2033. if (alps_identify(psmouse, &dummy) < 0)
  2034. return -1;
  2035. if (set_properties) {
  2036. psmouse->vendor = "ALPS";
  2037. psmouse->name = dummy.flags & ALPS_DUALPOINT ?
  2038. "DualPoint TouchPad" : "GlidePoint";
  2039. psmouse->model = dummy.proto_version << 8;
  2040. }
  2041. return 0;
  2042. }