alps.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417
  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;
  780. for (i = 0; i < MAX_TOUCHES && mt[i].x != 0 && mt[i].y != 0; i++)
  781. /* empty */;
  782. return i;
  783. }
  784. static int alps_decode_packet_v7(struct alps_fields *f,
  785. unsigned char *p,
  786. struct psmouse *psmouse)
  787. {
  788. unsigned char pkt_id;
  789. pkt_id = alps_get_packet_id_v7(p);
  790. if (pkt_id == V7_PACKET_ID_IDLE)
  791. return 0;
  792. if (pkt_id == V7_PACKET_ID_UNKNOWN)
  793. return -1;
  794. alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
  795. if (pkt_id == V7_PACKET_ID_TWO || pkt_id == V7_PACKET_ID_MULTI) {
  796. f->left = (p[0] & 0x80) >> 7;
  797. f->right = (p[0] & 0x20) >> 5;
  798. f->middle = (p[0] & 0x10) >> 4;
  799. }
  800. if (pkt_id == V7_PACKET_ID_TWO)
  801. f->fingers = alps_get_mt_count(f->mt);
  802. else if (pkt_id == V7_PACKET_ID_MULTI)
  803. f->fingers = 3 + (p[5] & 0x03);
  804. return 0;
  805. }
  806. static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
  807. {
  808. struct alps_data *priv = psmouse->private;
  809. unsigned char *packet = psmouse->packet;
  810. struct input_dev *dev2 = priv->dev2;
  811. int x, y, z, left, right, middle;
  812. /*
  813. * b7 b6 b5 b4 b3 b2 b1 b0
  814. * Byte0 0 1 0 0 1 0 0 0
  815. * Byte1 1 1 * * 1 M R L
  816. * Byte2 X7 1 X5 X4 X3 X2 X1 X0
  817. * Byte3 Z6 1 Y6 X6 1 Y2 Y1 Y0
  818. * Byte4 Y7 0 Y5 Y4 Y3 1 1 0
  819. * Byte5 T&P 0 Z5 Z4 Z3 Z2 Z1 Z0
  820. * M / R / L: Middle / Right / Left button
  821. */
  822. x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
  823. y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
  824. ((packet[3] & 0x20) << 1);
  825. z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
  826. left = (packet[1] & 0x01);
  827. right = (packet[1] & 0x02) >> 1;
  828. middle = (packet[1] & 0x04) >> 2;
  829. /* Divide 2 since trackpoint's speed is too fast */
  830. input_report_rel(dev2, REL_X, (char)x / 2);
  831. input_report_rel(dev2, REL_Y, -((char)y / 2));
  832. input_report_key(dev2, BTN_LEFT, left);
  833. input_report_key(dev2, BTN_RIGHT, right);
  834. input_report_key(dev2, BTN_MIDDLE, middle);
  835. input_sync(dev2);
  836. }
  837. static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
  838. {
  839. struct alps_data *priv = psmouse->private;
  840. struct input_dev *dev = psmouse->dev;
  841. struct alps_fields *f = &priv->f;
  842. memset(f, 0, sizeof(*f));
  843. if (priv->decode_fields(f, psmouse->packet, psmouse))
  844. return;
  845. alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
  846. input_mt_report_finger_count(dev, f->fingers);
  847. input_report_key(dev, BTN_LEFT, f->left);
  848. input_report_key(dev, BTN_RIGHT, f->right);
  849. input_report_key(dev, BTN_MIDDLE, f->middle);
  850. input_sync(dev);
  851. }
  852. static void alps_process_packet_v7(struct psmouse *psmouse)
  853. {
  854. unsigned char *packet = psmouse->packet;
  855. if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
  856. alps_process_trackstick_packet_v7(psmouse);
  857. else
  858. alps_process_touchpad_packet_v7(psmouse);
  859. }
  860. static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
  861. unsigned char packet[],
  862. bool report_buttons)
  863. {
  864. struct alps_data *priv = psmouse->private;
  865. struct input_dev *dev2 = priv->dev2;
  866. if (report_buttons)
  867. alps_report_buttons(psmouse, dev2, psmouse->dev,
  868. packet[0] & 1, packet[0] & 2, packet[0] & 4);
  869. input_report_rel(dev2, REL_X,
  870. packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
  871. input_report_rel(dev2, REL_Y,
  872. packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
  873. input_sync(dev2);
  874. }
  875. static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
  876. {
  877. struct alps_data *priv = psmouse->private;
  878. if (psmouse->pktcnt < 6)
  879. return PSMOUSE_GOOD_DATA;
  880. if (psmouse->pktcnt == 6) {
  881. /*
  882. * Start a timer to flush the packet if it ends up last
  883. * 6-byte packet in the stream. Timer needs to fire
  884. * psmouse core times out itself. 20 ms should be enough
  885. * to decide if we are getting more data or not.
  886. */
  887. mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
  888. return PSMOUSE_GOOD_DATA;
  889. }
  890. del_timer(&priv->timer);
  891. if (psmouse->packet[6] & 0x80) {
  892. /*
  893. * Highest bit is set - that means we either had
  894. * complete ALPS packet and this is start of the
  895. * next packet or we got garbage.
  896. */
  897. if (((psmouse->packet[3] |
  898. psmouse->packet[4] |
  899. psmouse->packet[5]) & 0x80) ||
  900. (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
  901. psmouse_dbg(psmouse,
  902. "refusing packet %4ph (suspected interleaved ps/2)\n",
  903. psmouse->packet + 3);
  904. return PSMOUSE_BAD_DATA;
  905. }
  906. priv->process_packet(psmouse);
  907. /* Continue with the next packet */
  908. psmouse->packet[0] = psmouse->packet[6];
  909. psmouse->pktcnt = 1;
  910. } else {
  911. /*
  912. * High bit is 0 - that means that we indeed got a PS/2
  913. * packet in the middle of ALPS packet.
  914. *
  915. * There is also possibility that we got 6-byte ALPS
  916. * packet followed by 3-byte packet from trackpoint. We
  917. * can not distinguish between these 2 scenarios but
  918. * because the latter is unlikely to happen in course of
  919. * normal operation (user would need to press all
  920. * buttons on the pad and start moving trackpoint
  921. * without touching the pad surface) we assume former.
  922. * Even if we are wrong the wost thing that would happen
  923. * the cursor would jump but we should not get protocol
  924. * de-synchronization.
  925. */
  926. alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
  927. false);
  928. /*
  929. * Continue with the standard ALPS protocol handling,
  930. * but make sure we won't process it as an interleaved
  931. * packet again, which may happen if all buttons are
  932. * pressed. To avoid this let's reset the 4th bit which
  933. * is normally 1.
  934. */
  935. psmouse->packet[3] = psmouse->packet[6] & 0xf7;
  936. psmouse->pktcnt = 4;
  937. }
  938. return PSMOUSE_GOOD_DATA;
  939. }
  940. static void alps_flush_packet(unsigned long data)
  941. {
  942. struct psmouse *psmouse = (struct psmouse *)data;
  943. struct alps_data *priv = psmouse->private;
  944. serio_pause_rx(psmouse->ps2dev.serio);
  945. if (psmouse->pktcnt == psmouse->pktsize) {
  946. /*
  947. * We did not any more data in reasonable amount of time.
  948. * Validate the last 3 bytes and process as a standard
  949. * ALPS packet.
  950. */
  951. if ((psmouse->packet[3] |
  952. psmouse->packet[4] |
  953. psmouse->packet[5]) & 0x80) {
  954. psmouse_dbg(psmouse,
  955. "refusing packet %3ph (suspected interleaved ps/2)\n",
  956. psmouse->packet + 3);
  957. } else {
  958. priv->process_packet(psmouse);
  959. }
  960. psmouse->pktcnt = 0;
  961. }
  962. serio_continue_rx(psmouse->ps2dev.serio);
  963. }
  964. static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
  965. {
  966. struct alps_data *priv = psmouse->private;
  967. if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
  968. if (psmouse->pktcnt == 3) {
  969. alps_report_bare_ps2_packet(psmouse, psmouse->packet,
  970. true);
  971. return PSMOUSE_FULL_PACKET;
  972. }
  973. return PSMOUSE_GOOD_DATA;
  974. }
  975. /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
  976. if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
  977. psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
  978. return alps_handle_interleaved_ps2(psmouse);
  979. }
  980. if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
  981. psmouse_dbg(psmouse,
  982. "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
  983. psmouse->packet[0], priv->mask0, priv->byte0);
  984. return PSMOUSE_BAD_DATA;
  985. }
  986. /* Bytes 2 - pktsize should have 0 in the highest bit */
  987. if ((priv->proto_version < ALPS_PROTO_V5) &&
  988. psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
  989. (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
  990. psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
  991. psmouse->pktcnt - 1,
  992. psmouse->packet[psmouse->pktcnt - 1]);
  993. return PSMOUSE_BAD_DATA;
  994. }
  995. if (priv->proto_version == ALPS_PROTO_V7 &&
  996. !alps_is_valid_package_v7(psmouse)) {
  997. psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
  998. psmouse->pktcnt - 1,
  999. psmouse->packet[psmouse->pktcnt - 1]);
  1000. return PSMOUSE_BAD_DATA;
  1001. }
  1002. if (psmouse->pktcnt == psmouse->pktsize) {
  1003. priv->process_packet(psmouse);
  1004. return PSMOUSE_FULL_PACKET;
  1005. }
  1006. return PSMOUSE_GOOD_DATA;
  1007. }
  1008. static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
  1009. {
  1010. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1011. struct alps_data *priv = psmouse->private;
  1012. int command;
  1013. unsigned char *param;
  1014. unsigned char dummy[4];
  1015. BUG_ON(nibble > 0xf);
  1016. command = priv->nibble_commands[nibble].command;
  1017. param = (command & 0x0f00) ?
  1018. dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
  1019. if (ps2_command(ps2dev, param, command))
  1020. return -1;
  1021. return 0;
  1022. }
  1023. static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
  1024. {
  1025. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1026. struct alps_data *priv = psmouse->private;
  1027. int i, nibble;
  1028. if (ps2_command(ps2dev, NULL, priv->addr_command))
  1029. return -1;
  1030. for (i = 12; i >= 0; i -= 4) {
  1031. nibble = (addr >> i) & 0xf;
  1032. if (alps_command_mode_send_nibble(psmouse, nibble))
  1033. return -1;
  1034. }
  1035. return 0;
  1036. }
  1037. static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
  1038. {
  1039. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1040. unsigned char param[4];
  1041. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  1042. return -1;
  1043. /*
  1044. * The address being read is returned in the first two bytes
  1045. * of the result. Check that this address matches the expected
  1046. * address.
  1047. */
  1048. if (addr != ((param[0] << 8) | param[1]))
  1049. return -1;
  1050. return param[2];
  1051. }
  1052. static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
  1053. {
  1054. if (alps_command_mode_set_addr(psmouse, addr))
  1055. return -1;
  1056. return __alps_command_mode_read_reg(psmouse, addr);
  1057. }
  1058. static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
  1059. {
  1060. if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
  1061. return -1;
  1062. if (alps_command_mode_send_nibble(psmouse, value & 0xf))
  1063. return -1;
  1064. return 0;
  1065. }
  1066. static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
  1067. u8 value)
  1068. {
  1069. if (alps_command_mode_set_addr(psmouse, addr))
  1070. return -1;
  1071. return __alps_command_mode_write_reg(psmouse, value);
  1072. }
  1073. static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
  1074. int repeated_command, unsigned char *param)
  1075. {
  1076. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1077. param[0] = 0;
  1078. if (init_command && ps2_command(ps2dev, param, init_command))
  1079. return -EIO;
  1080. if (ps2_command(ps2dev, NULL, repeated_command) ||
  1081. ps2_command(ps2dev, NULL, repeated_command) ||
  1082. ps2_command(ps2dev, NULL, repeated_command))
  1083. return -EIO;
  1084. param[0] = param[1] = param[2] = 0xff;
  1085. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  1086. return -EIO;
  1087. psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
  1088. repeated_command, param);
  1089. return 0;
  1090. }
  1091. static bool alps_check_valid_firmware_id(unsigned char id[])
  1092. {
  1093. if (id[0] == 0x73)
  1094. return true;
  1095. if (id[0] == 0x88 &&
  1096. (id[1] == 0x07 ||
  1097. id[1] == 0x08 ||
  1098. (id[1] & 0xf0) == 0xb0 ||
  1099. (id[1] & 0xf0) == 0xc0)) {
  1100. return true;
  1101. }
  1102. return false;
  1103. }
  1104. static int alps_enter_command_mode(struct psmouse *psmouse)
  1105. {
  1106. unsigned char param[4];
  1107. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
  1108. psmouse_err(psmouse, "failed to enter command mode\n");
  1109. return -1;
  1110. }
  1111. if (!alps_check_valid_firmware_id(param)) {
  1112. psmouse_dbg(psmouse,
  1113. "unknown response while entering command mode\n");
  1114. return -1;
  1115. }
  1116. return 0;
  1117. }
  1118. static inline int alps_exit_command_mode(struct psmouse *psmouse)
  1119. {
  1120. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1121. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
  1122. return -1;
  1123. return 0;
  1124. }
  1125. /*
  1126. * For DualPoint devices select the device that should respond to
  1127. * subsequent commands. It looks like glidepad is behind stickpointer,
  1128. * I'd thought it would be other way around...
  1129. */
  1130. static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
  1131. {
  1132. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1133. int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
  1134. if (ps2_command(ps2dev, NULL, cmd) ||
  1135. ps2_command(ps2dev, NULL, cmd) ||
  1136. ps2_command(ps2dev, NULL, cmd) ||
  1137. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  1138. return -1;
  1139. /* we may get 3 more bytes, just ignore them */
  1140. ps2_drain(ps2dev, 3, 100);
  1141. return 0;
  1142. }
  1143. static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
  1144. {
  1145. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1146. /* Try ALPS magic knock - 4 disable before enable */
  1147. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1148. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1149. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1150. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1151. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
  1152. return -1;
  1153. /*
  1154. * Switch mouse to poll (remote) mode so motion data will not
  1155. * get in our way
  1156. */
  1157. return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
  1158. }
  1159. static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
  1160. {
  1161. int i, nibble;
  1162. /*
  1163. * b0-b11 are valid bits, send sequence is inverse.
  1164. * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
  1165. */
  1166. for (i = 0; i <= 8; i += 4) {
  1167. nibble = (word >> i) & 0xf;
  1168. if (alps_command_mode_send_nibble(psmouse, nibble))
  1169. return -1;
  1170. }
  1171. return 0;
  1172. }
  1173. static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
  1174. u16 addr, u16 value)
  1175. {
  1176. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1177. /* 0x0A0 is the command to write the word */
  1178. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
  1179. alps_monitor_mode_send_word(psmouse, 0x0A0) ||
  1180. alps_monitor_mode_send_word(psmouse, addr) ||
  1181. alps_monitor_mode_send_word(psmouse, value) ||
  1182. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  1183. return -1;
  1184. return 0;
  1185. }
  1186. static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
  1187. {
  1188. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1189. if (enable) {
  1190. /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
  1191. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
  1192. ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
  1193. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1194. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1195. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  1196. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1197. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  1198. ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
  1199. return -1;
  1200. } else {
  1201. /* EC to exit monitor mode */
  1202. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
  1203. return -1;
  1204. }
  1205. return 0;
  1206. }
  1207. static int alps_absolute_mode_v6(struct psmouse *psmouse)
  1208. {
  1209. u16 reg_val = 0x181;
  1210. int ret = -1;
  1211. /* enter monitor mode, to write the register */
  1212. if (alps_monitor_mode(psmouse, true))
  1213. return -1;
  1214. ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
  1215. if (alps_monitor_mode(psmouse, false))
  1216. ret = -1;
  1217. return ret;
  1218. }
  1219. static int alps_get_status(struct psmouse *psmouse, char *param)
  1220. {
  1221. /* Get status: 0xF5 0xF5 0xF5 0xE9 */
  1222. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
  1223. return -1;
  1224. return 0;
  1225. }
  1226. /*
  1227. * Turn touchpad tapping on or off. The sequences are:
  1228. * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
  1229. * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
  1230. * My guess that 0xE9 (GetInfo) is here as a sync point.
  1231. * For models that also have stickpointer (DualPoints) its tapping
  1232. * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
  1233. * we don't fiddle with it.
  1234. */
  1235. static int alps_tap_mode(struct psmouse *psmouse, int enable)
  1236. {
  1237. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1238. int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
  1239. unsigned char tap_arg = enable ? 0x0A : 0x00;
  1240. unsigned char param[4];
  1241. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
  1242. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1243. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1244. ps2_command(ps2dev, &tap_arg, cmd))
  1245. return -1;
  1246. if (alps_get_status(psmouse, param))
  1247. return -1;
  1248. return 0;
  1249. }
  1250. /*
  1251. * alps_poll() - poll the touchpad for current motion packet.
  1252. * Used in resync.
  1253. */
  1254. static int alps_poll(struct psmouse *psmouse)
  1255. {
  1256. struct alps_data *priv = psmouse->private;
  1257. unsigned char buf[sizeof(psmouse->packet)];
  1258. bool poll_failed;
  1259. if (priv->flags & ALPS_PASS)
  1260. alps_passthrough_mode_v2(psmouse, true);
  1261. poll_failed = ps2_command(&psmouse->ps2dev, buf,
  1262. PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
  1263. if (priv->flags & ALPS_PASS)
  1264. alps_passthrough_mode_v2(psmouse, false);
  1265. if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
  1266. return -1;
  1267. if ((psmouse->badbyte & 0xc8) == 0x08) {
  1268. /*
  1269. * Poll the track stick ...
  1270. */
  1271. if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
  1272. return -1;
  1273. }
  1274. memcpy(psmouse->packet, buf, sizeof(buf));
  1275. return 0;
  1276. }
  1277. static int alps_hw_init_v1_v2(struct psmouse *psmouse)
  1278. {
  1279. struct alps_data *priv = psmouse->private;
  1280. if ((priv->flags & ALPS_PASS) &&
  1281. alps_passthrough_mode_v2(psmouse, true)) {
  1282. return -1;
  1283. }
  1284. if (alps_tap_mode(psmouse, true)) {
  1285. psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
  1286. return -1;
  1287. }
  1288. if (alps_absolute_mode_v1_v2(psmouse)) {
  1289. psmouse_err(psmouse, "Failed to enable absolute mode\n");
  1290. return -1;
  1291. }
  1292. if ((priv->flags & ALPS_PASS) &&
  1293. alps_passthrough_mode_v2(psmouse, false)) {
  1294. return -1;
  1295. }
  1296. /* ALPS needs stream mode, otherwise it won't report any data */
  1297. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
  1298. psmouse_err(psmouse, "Failed to enable stream mode\n");
  1299. return -1;
  1300. }
  1301. return 0;
  1302. }
  1303. static int alps_hw_init_v6(struct psmouse *psmouse)
  1304. {
  1305. unsigned char param[2] = {0xC8, 0x14};
  1306. /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
  1307. if (alps_passthrough_mode_v2(psmouse, true))
  1308. return -1;
  1309. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1310. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1311. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1312. ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1313. ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
  1314. return -1;
  1315. if (alps_passthrough_mode_v2(psmouse, false))
  1316. return -1;
  1317. if (alps_absolute_mode_v6(psmouse)) {
  1318. psmouse_err(psmouse, "Failed to enable absolute mode\n");
  1319. return -1;
  1320. }
  1321. return 0;
  1322. }
  1323. /*
  1324. * Enable or disable passthrough mode to the trackstick.
  1325. */
  1326. static int alps_passthrough_mode_v3(struct psmouse *psmouse,
  1327. int reg_base, bool enable)
  1328. {
  1329. int reg_val, ret = -1;
  1330. if (alps_enter_command_mode(psmouse))
  1331. return -1;
  1332. reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
  1333. if (reg_val == -1)
  1334. goto error;
  1335. if (enable)
  1336. reg_val |= 0x01;
  1337. else
  1338. reg_val &= ~0x01;
  1339. ret = __alps_command_mode_write_reg(psmouse, reg_val);
  1340. error:
  1341. if (alps_exit_command_mode(psmouse))
  1342. ret = -1;
  1343. return ret;
  1344. }
  1345. /* Must be in command mode when calling this function */
  1346. static int alps_absolute_mode_v3(struct psmouse *psmouse)
  1347. {
  1348. int reg_val;
  1349. reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
  1350. if (reg_val == -1)
  1351. return -1;
  1352. reg_val |= 0x06;
  1353. if (__alps_command_mode_write_reg(psmouse, reg_val))
  1354. return -1;
  1355. return 0;
  1356. }
  1357. static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
  1358. {
  1359. int ret = -EIO, reg_val;
  1360. if (alps_enter_command_mode(psmouse))
  1361. goto error;
  1362. reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
  1363. if (reg_val == -1)
  1364. goto error;
  1365. /* bit 7: trackstick is present */
  1366. ret = reg_val & 0x80 ? 0 : -ENODEV;
  1367. error:
  1368. alps_exit_command_mode(psmouse);
  1369. return ret;
  1370. }
  1371. static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
  1372. {
  1373. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1374. int ret = 0;
  1375. unsigned char param[4];
  1376. if (alps_passthrough_mode_v3(psmouse, reg_base, true))
  1377. return -EIO;
  1378. /*
  1379. * E7 report for the trackstick
  1380. *
  1381. * There have been reports of failures to seem to trace back
  1382. * to the above trackstick check failing. When these occur
  1383. * this E7 report fails, so when that happens we continue
  1384. * with the assumption that there isn't a trackstick after
  1385. * all.
  1386. */
  1387. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
  1388. psmouse_warn(psmouse, "trackstick E7 report failed\n");
  1389. ret = -ENODEV;
  1390. } else {
  1391. psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
  1392. /*
  1393. * Not sure what this does, but it is absolutely
  1394. * essential. Without it, the touchpad does not
  1395. * work at all and the trackstick just emits normal
  1396. * PS/2 packets.
  1397. */
  1398. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1399. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1400. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1401. alps_command_mode_send_nibble(psmouse, 0x9) ||
  1402. alps_command_mode_send_nibble(psmouse, 0x4)) {
  1403. psmouse_err(psmouse,
  1404. "Error sending magic E6 sequence\n");
  1405. ret = -EIO;
  1406. goto error;
  1407. }
  1408. /*
  1409. * This ensures the trackstick packets are in the format
  1410. * supported by this driver. If bit 1 isn't set the packet
  1411. * format is different.
  1412. */
  1413. if (alps_enter_command_mode(psmouse) ||
  1414. alps_command_mode_write_reg(psmouse,
  1415. reg_base + 0x08, 0x82) ||
  1416. alps_exit_command_mode(psmouse))
  1417. ret = -EIO;
  1418. }
  1419. error:
  1420. if (alps_passthrough_mode_v3(psmouse, reg_base, false))
  1421. ret = -EIO;
  1422. return ret;
  1423. }
  1424. static int alps_hw_init_v3(struct psmouse *psmouse)
  1425. {
  1426. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1427. int reg_val;
  1428. unsigned char param[4];
  1429. reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
  1430. if (reg_val == -EIO)
  1431. goto error;
  1432. if (reg_val == 0 &&
  1433. alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
  1434. goto error;
  1435. if (alps_enter_command_mode(psmouse) ||
  1436. alps_absolute_mode_v3(psmouse)) {
  1437. psmouse_err(psmouse, "Failed to enter absolute mode\n");
  1438. goto error;
  1439. }
  1440. reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
  1441. if (reg_val == -1)
  1442. goto error;
  1443. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
  1444. goto error;
  1445. reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
  1446. if (reg_val == -1)
  1447. goto error;
  1448. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
  1449. goto error;
  1450. if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
  1451. goto error;
  1452. if (__alps_command_mode_write_reg(psmouse, 0x04))
  1453. goto error;
  1454. if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
  1455. goto error;
  1456. if (__alps_command_mode_write_reg(psmouse, 0x03))
  1457. goto error;
  1458. if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
  1459. goto error;
  1460. if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
  1461. goto error;
  1462. if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
  1463. goto error;
  1464. if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
  1465. goto error;
  1466. alps_exit_command_mode(psmouse);
  1467. /* Set rate and enable data reporting */
  1468. param[0] = 0x64;
  1469. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
  1470. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  1471. psmouse_err(psmouse, "Failed to enable data reporting\n");
  1472. return -1;
  1473. }
  1474. return 0;
  1475. error:
  1476. /*
  1477. * Leaving the touchpad in command mode will essentially render
  1478. * it unusable until the machine reboots, so exit it here just
  1479. * to be safe
  1480. */
  1481. alps_exit_command_mode(psmouse);
  1482. return -1;
  1483. }
  1484. static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
  1485. {
  1486. int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
  1487. struct alps_data *priv = psmouse->private;
  1488. reg = alps_command_mode_read_reg(psmouse, reg_pitch);
  1489. if (reg < 0)
  1490. return reg;
  1491. x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
  1492. x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
  1493. y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
  1494. y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
  1495. reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
  1496. if (reg < 0)
  1497. return reg;
  1498. x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
  1499. x_electrode = 17 + x_electrode;
  1500. y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
  1501. y_electrode = 13 + y_electrode;
  1502. x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
  1503. y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
  1504. priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
  1505. priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
  1506. psmouse_dbg(psmouse,
  1507. "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
  1508. x_pitch, y_pitch, x_electrode, y_electrode,
  1509. x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
  1510. return 0;
  1511. }
  1512. static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
  1513. {
  1514. struct alps_data *priv = psmouse->private;
  1515. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1516. int reg_val, ret = -1;
  1517. if (priv->flags & ALPS_DUALPOINT) {
  1518. reg_val = alps_setup_trackstick_v3(psmouse,
  1519. ALPS_REG_BASE_RUSHMORE);
  1520. if (reg_val == -EIO)
  1521. goto error;
  1522. if (reg_val == -ENODEV)
  1523. priv->flags &= ~ALPS_DUALPOINT;
  1524. }
  1525. if (alps_enter_command_mode(psmouse) ||
  1526. alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
  1527. alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
  1528. goto error;
  1529. if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
  1530. goto error;
  1531. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
  1532. if (reg_val == -1)
  1533. goto error;
  1534. if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
  1535. goto error;
  1536. if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
  1537. goto error;
  1538. /* enter absolute mode */
  1539. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
  1540. if (reg_val == -1)
  1541. goto error;
  1542. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
  1543. goto error;
  1544. alps_exit_command_mode(psmouse);
  1545. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  1546. error:
  1547. alps_exit_command_mode(psmouse);
  1548. return ret;
  1549. }
  1550. /* Must be in command mode when calling this function */
  1551. static int alps_absolute_mode_v4(struct psmouse *psmouse)
  1552. {
  1553. int reg_val;
  1554. reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
  1555. if (reg_val == -1)
  1556. return -1;
  1557. reg_val |= 0x02;
  1558. if (__alps_command_mode_write_reg(psmouse, reg_val))
  1559. return -1;
  1560. return 0;
  1561. }
  1562. static int alps_hw_init_v4(struct psmouse *psmouse)
  1563. {
  1564. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1565. unsigned char param[4];
  1566. if (alps_enter_command_mode(psmouse))
  1567. goto error;
  1568. if (alps_absolute_mode_v4(psmouse)) {
  1569. psmouse_err(psmouse, "Failed to enter absolute mode\n");
  1570. goto error;
  1571. }
  1572. if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
  1573. goto error;
  1574. if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
  1575. goto error;
  1576. if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
  1577. goto error;
  1578. if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
  1579. goto error;
  1580. if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
  1581. goto error;
  1582. if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
  1583. goto error;
  1584. if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
  1585. goto error;
  1586. if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
  1587. goto error;
  1588. alps_exit_command_mode(psmouse);
  1589. /*
  1590. * This sequence changes the output from a 9-byte to an
  1591. * 8-byte format. All the same data seems to be present,
  1592. * just in a more compact format.
  1593. */
  1594. param[0] = 0xc8;
  1595. param[1] = 0x64;
  1596. param[2] = 0x50;
  1597. if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1598. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
  1599. ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
  1600. ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
  1601. return -1;
  1602. /* Set rate and enable data reporting */
  1603. param[0] = 0x64;
  1604. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
  1605. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  1606. psmouse_err(psmouse, "Failed to enable data reporting\n");
  1607. return -1;
  1608. }
  1609. return 0;
  1610. error:
  1611. /*
  1612. * Leaving the touchpad in command mode will essentially render
  1613. * it unusable until the machine reboots, so exit it here just
  1614. * to be safe
  1615. */
  1616. alps_exit_command_mode(psmouse);
  1617. return -1;
  1618. }
  1619. static int alps_dolphin_get_device_area(struct psmouse *psmouse,
  1620. struct alps_data *priv)
  1621. {
  1622. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1623. unsigned char param[4] = {0};
  1624. int num_x_electrode, num_y_electrode;
  1625. if (alps_enter_command_mode(psmouse))
  1626. return -1;
  1627. param[0] = 0x0a;
  1628. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
  1629. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  1630. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  1631. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1632. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
  1633. return -1;
  1634. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  1635. return -1;
  1636. /*
  1637. * Dolphin's sensor line number is not fixed. It can be calculated
  1638. * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
  1639. * Further more, we can get device's x_max and y_max by multiplying
  1640. * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
  1641. *
  1642. * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
  1643. * real sensor line number X = 11 + 8 = 19, and
  1644. * real sensor line number Y = 8 + 1 = 9.
  1645. * So, x_max = (19 - 1) * 64 = 1152, and
  1646. * y_max = (9 - 1) * 64 = 512.
  1647. */
  1648. num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
  1649. num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
  1650. priv->x_bits = num_x_electrode;
  1651. priv->y_bits = num_y_electrode;
  1652. priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
  1653. priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
  1654. if (alps_exit_command_mode(psmouse))
  1655. return -1;
  1656. return 0;
  1657. }
  1658. static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
  1659. {
  1660. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1661. unsigned char param[2];
  1662. /* This is dolphin "v1" as empirically defined by florin9doi */
  1663. param[0] = 0x64;
  1664. param[1] = 0x28;
  1665. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  1666. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1667. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
  1668. return -1;
  1669. return 0;
  1670. }
  1671. static int alps_hw_init_v7(struct psmouse *psmouse)
  1672. {
  1673. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1674. int reg_val, ret = -1;
  1675. if (alps_enter_command_mode(psmouse) ||
  1676. alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
  1677. goto error;
  1678. if (alps_get_v3_v7_resolution(psmouse, 0xc397))
  1679. goto error;
  1680. if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
  1681. goto error;
  1682. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
  1683. if (reg_val == -1)
  1684. goto error;
  1685. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
  1686. goto error;
  1687. alps_exit_command_mode(psmouse);
  1688. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  1689. error:
  1690. alps_exit_command_mode(psmouse);
  1691. return ret;
  1692. }
  1693. static void alps_set_defaults(struct alps_data *priv)
  1694. {
  1695. priv->byte0 = 0x8f;
  1696. priv->mask0 = 0x8f;
  1697. priv->flags = ALPS_DUALPOINT;
  1698. priv->x_max = 2000;
  1699. priv->y_max = 1400;
  1700. priv->x_bits = 15;
  1701. priv->y_bits = 11;
  1702. switch (priv->proto_version) {
  1703. case ALPS_PROTO_V1:
  1704. case ALPS_PROTO_V2:
  1705. priv->hw_init = alps_hw_init_v1_v2;
  1706. priv->process_packet = alps_process_packet_v1_v2;
  1707. priv->set_abs_params = alps_set_abs_params_st;
  1708. priv->x_max = 1023;
  1709. priv->y_max = 767;
  1710. break;
  1711. case ALPS_PROTO_V3:
  1712. priv->hw_init = alps_hw_init_v3;
  1713. priv->process_packet = alps_process_packet_v3;
  1714. priv->set_abs_params = alps_set_abs_params_mt;
  1715. priv->decode_fields = alps_decode_pinnacle;
  1716. priv->nibble_commands = alps_v3_nibble_commands;
  1717. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  1718. break;
  1719. case ALPS_PROTO_V4:
  1720. priv->hw_init = alps_hw_init_v4;
  1721. priv->process_packet = alps_process_packet_v4;
  1722. priv->set_abs_params = alps_set_abs_params_mt;
  1723. priv->nibble_commands = alps_v4_nibble_commands;
  1724. priv->addr_command = PSMOUSE_CMD_DISABLE;
  1725. break;
  1726. case ALPS_PROTO_V5:
  1727. priv->hw_init = alps_hw_init_dolphin_v1;
  1728. priv->process_packet = alps_process_touchpad_packet_v3_v5;
  1729. priv->decode_fields = alps_decode_dolphin;
  1730. priv->set_abs_params = alps_set_abs_params_mt;
  1731. priv->nibble_commands = alps_v3_nibble_commands;
  1732. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  1733. priv->byte0 = 0xc8;
  1734. priv->mask0 = 0xd8;
  1735. priv->flags = 0;
  1736. priv->x_max = 1360;
  1737. priv->y_max = 660;
  1738. priv->x_bits = 23;
  1739. priv->y_bits = 12;
  1740. break;
  1741. case ALPS_PROTO_V6:
  1742. priv->hw_init = alps_hw_init_v6;
  1743. priv->process_packet = alps_process_packet_v6;
  1744. priv->set_abs_params = alps_set_abs_params_st;
  1745. priv->nibble_commands = alps_v6_nibble_commands;
  1746. priv->x_max = 2047;
  1747. priv->y_max = 1535;
  1748. break;
  1749. case ALPS_PROTO_V7:
  1750. priv->hw_init = alps_hw_init_v7;
  1751. priv->process_packet = alps_process_packet_v7;
  1752. priv->decode_fields = alps_decode_packet_v7;
  1753. priv->set_abs_params = alps_set_abs_params_mt;
  1754. priv->nibble_commands = alps_v3_nibble_commands;
  1755. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  1756. priv->x_max = 0xfff;
  1757. priv->y_max = 0x7ff;
  1758. priv->byte0 = 0x48;
  1759. priv->mask0 = 0x48;
  1760. if (priv->fw_ver[1] != 0xba)
  1761. priv->flags |= ALPS_BUTTONPAD;
  1762. break;
  1763. }
  1764. }
  1765. static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
  1766. unsigned char *e7, unsigned char *ec)
  1767. {
  1768. const struct alps_model_info *model;
  1769. int i;
  1770. for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
  1771. model = &alps_model_data[i];
  1772. if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
  1773. (!model->command_mode_resp ||
  1774. model->command_mode_resp == ec[2])) {
  1775. priv->proto_version = model->proto_version;
  1776. alps_set_defaults(priv);
  1777. priv->flags = model->flags;
  1778. priv->byte0 = model->byte0;
  1779. priv->mask0 = model->mask0;
  1780. return 0;
  1781. }
  1782. }
  1783. return -EINVAL;
  1784. }
  1785. static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
  1786. {
  1787. unsigned char e6[4], e7[4], ec[4];
  1788. /*
  1789. * First try "E6 report".
  1790. * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
  1791. * The bits 0-2 of the first byte will be 1s if some buttons are
  1792. * pressed.
  1793. */
  1794. if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  1795. PSMOUSE_CMD_SETSCALE11, e6))
  1796. return -EIO;
  1797. if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
  1798. return -EINVAL;
  1799. /*
  1800. * Now get the "E7" and "EC" reports. These will uniquely identify
  1801. * most ALPS touchpads.
  1802. */
  1803. if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  1804. PSMOUSE_CMD_SETSCALE21, e7) ||
  1805. alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  1806. PSMOUSE_CMD_RESET_WRAP, ec) ||
  1807. alps_exit_command_mode(psmouse))
  1808. return -EIO;
  1809. /* Save the Firmware version */
  1810. memcpy(priv->fw_ver, ec, 3);
  1811. if (alps_match_table(psmouse, priv, e7, ec) == 0) {
  1812. return 0;
  1813. } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
  1814. ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
  1815. priv->proto_version = ALPS_PROTO_V5;
  1816. alps_set_defaults(priv);
  1817. if (alps_dolphin_get_device_area(psmouse, priv))
  1818. return -EIO;
  1819. else
  1820. return 0;
  1821. } else if (ec[0] == 0x88 &&
  1822. ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
  1823. priv->proto_version = ALPS_PROTO_V7;
  1824. alps_set_defaults(priv);
  1825. return 0;
  1826. } else if (ec[0] == 0x88 && ec[1] == 0x08) {
  1827. priv->proto_version = ALPS_PROTO_V3;
  1828. alps_set_defaults(priv);
  1829. priv->hw_init = alps_hw_init_rushmore_v3;
  1830. priv->decode_fields = alps_decode_rushmore;
  1831. priv->x_bits = 16;
  1832. priv->y_bits = 12;
  1833. priv->flags |= ALPS_IS_RUSHMORE;
  1834. /* hack to make addr_command, nibble_command available */
  1835. psmouse->private = priv;
  1836. if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
  1837. priv->flags &= ~ALPS_DUALPOINT;
  1838. return 0;
  1839. } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
  1840. ec[2] >= 0x90 && ec[2] <= 0x9d) {
  1841. priv->proto_version = ALPS_PROTO_V3;
  1842. alps_set_defaults(priv);
  1843. return 0;
  1844. }
  1845. psmouse_dbg(psmouse,
  1846. "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
  1847. return -EINVAL;
  1848. }
  1849. static int alps_reconnect(struct psmouse *psmouse)
  1850. {
  1851. struct alps_data *priv = psmouse->private;
  1852. psmouse_reset(psmouse);
  1853. if (alps_identify(psmouse, priv) < 0)
  1854. return -1;
  1855. return priv->hw_init(psmouse);
  1856. }
  1857. static void alps_disconnect(struct psmouse *psmouse)
  1858. {
  1859. struct alps_data *priv = psmouse->private;
  1860. psmouse_reset(psmouse);
  1861. del_timer_sync(&priv->timer);
  1862. input_unregister_device(priv->dev2);
  1863. kfree(priv);
  1864. }
  1865. static void alps_set_abs_params_st(struct alps_data *priv,
  1866. struct input_dev *dev1)
  1867. {
  1868. input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
  1869. input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
  1870. }
  1871. static void alps_set_abs_params_mt(struct alps_data *priv,
  1872. struct input_dev *dev1)
  1873. {
  1874. input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
  1875. input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
  1876. input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
  1877. input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
  1878. input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
  1879. INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
  1880. set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
  1881. set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
  1882. /* V7 is real multi-touch */
  1883. if (priv->proto_version == ALPS_PROTO_V7)
  1884. clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
  1885. }
  1886. int alps_init(struct psmouse *psmouse)
  1887. {
  1888. struct alps_data *priv;
  1889. struct input_dev *dev1 = psmouse->dev, *dev2;
  1890. priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
  1891. dev2 = input_allocate_device();
  1892. if (!priv || !dev2)
  1893. goto init_fail;
  1894. priv->dev2 = dev2;
  1895. setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
  1896. psmouse->private = priv;
  1897. psmouse_reset(psmouse);
  1898. if (alps_identify(psmouse, priv) < 0)
  1899. goto init_fail;
  1900. if (priv->hw_init(psmouse))
  1901. goto init_fail;
  1902. /*
  1903. * Undo part of setup done for us by psmouse core since touchpad
  1904. * is not a relative device.
  1905. */
  1906. __clear_bit(EV_REL, dev1->evbit);
  1907. __clear_bit(REL_X, dev1->relbit);
  1908. __clear_bit(REL_Y, dev1->relbit);
  1909. /*
  1910. * Now set up our capabilities.
  1911. */
  1912. dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
  1913. dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
  1914. dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
  1915. dev1->keybit[BIT_WORD(BTN_LEFT)] |=
  1916. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
  1917. dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
  1918. priv->set_abs_params(priv, dev1);
  1919. /* No pressure on V7 */
  1920. if (priv->proto_version != ALPS_PROTO_V7)
  1921. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  1922. if (priv->flags & ALPS_WHEEL) {
  1923. dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
  1924. dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
  1925. }
  1926. if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  1927. dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
  1928. dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
  1929. }
  1930. if (priv->flags & ALPS_FOUR_BUTTONS) {
  1931. dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
  1932. dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
  1933. dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
  1934. dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
  1935. } else if (priv->flags & ALPS_BUTTONPAD) {
  1936. set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
  1937. clear_bit(BTN_RIGHT, dev1->keybit);
  1938. } else {
  1939. dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
  1940. }
  1941. snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
  1942. dev2->phys = priv->phys;
  1943. dev2->name = (priv->flags & ALPS_DUALPOINT) ?
  1944. "DualPoint Stick" : "ALPS PS/2 Device";
  1945. dev2->id.bustype = BUS_I8042;
  1946. dev2->id.vendor = 0x0002;
  1947. dev2->id.product = PSMOUSE_ALPS;
  1948. dev2->id.version = 0x0000;
  1949. dev2->dev.parent = &psmouse->ps2dev.serio->dev;
  1950. dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  1951. dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  1952. dev2->keybit[BIT_WORD(BTN_LEFT)] =
  1953. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
  1954. __set_bit(INPUT_PROP_POINTER, dev2->propbit);
  1955. if (priv->flags & ALPS_DUALPOINT)
  1956. __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
  1957. if (input_register_device(priv->dev2))
  1958. goto init_fail;
  1959. psmouse->protocol_handler = alps_process_byte;
  1960. psmouse->poll = alps_poll;
  1961. psmouse->disconnect = alps_disconnect;
  1962. psmouse->reconnect = alps_reconnect;
  1963. psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
  1964. /* We are having trouble resyncing ALPS touchpads so disable it for now */
  1965. psmouse->resync_time = 0;
  1966. return 0;
  1967. init_fail:
  1968. psmouse_reset(psmouse);
  1969. input_free_device(dev2);
  1970. kfree(priv);
  1971. psmouse->private = NULL;
  1972. return -1;
  1973. }
  1974. int alps_detect(struct psmouse *psmouse, bool set_properties)
  1975. {
  1976. struct alps_data dummy;
  1977. if (alps_identify(psmouse, &dummy) < 0)
  1978. return -1;
  1979. if (set_properties) {
  1980. psmouse->vendor = "ALPS";
  1981. psmouse->name = dummy.flags & ALPS_DUALPOINT ?
  1982. "DualPoint TouchPad" : "GlidePoint";
  1983. psmouse->model = dummy.proto_version << 8;
  1984. }
  1985. return 0;
  1986. }