alps.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167
  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 <linux/dmi.h>
  23. #include "psmouse.h"
  24. #include "alps.h"
  25. /*
  26. * Definitions for ALPS version 3 and 4 command mode protocol
  27. */
  28. #define ALPS_CMD_NIBBLE_10 0x01f2
  29. #define ALPS_REG_BASE_RUSHMORE 0xc2c0
  30. #define ALPS_REG_BASE_V7 0xc2c0
  31. #define ALPS_REG_BASE_PINNACLE 0x0000
  32. static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
  33. { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
  34. { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
  35. { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
  36. { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
  37. { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
  38. { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
  39. { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
  40. { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
  41. { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
  42. { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
  43. { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
  44. { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
  45. { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
  46. { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
  47. { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
  48. { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
  49. };
  50. static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
  51. { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
  52. { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
  53. { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
  54. { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
  55. { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
  56. { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
  57. { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
  58. { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
  59. { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
  60. { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
  61. { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
  62. { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
  63. { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
  64. { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
  65. { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
  66. { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
  67. };
  68. static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
  69. { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
  70. { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
  71. { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
  72. { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
  73. { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
  74. { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
  75. { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
  76. { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
  77. { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
  78. { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
  79. { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
  80. { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
  81. { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
  82. { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
  83. { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
  84. { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
  85. };
  86. #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
  87. #define ALPS_PASS 0x04 /* device has a pass-through port */
  88. #define ALPS_WHEEL 0x08 /* hardware wheel present */
  89. #define ALPS_FW_BK_1 0x10 /* front & back buttons present */
  90. #define ALPS_FW_BK_2 0x20 /* front & back buttons present */
  91. #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
  92. #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
  93. 6-byte ALPS packet */
  94. #define ALPS_STICK_BITS 0x100 /* separate stick button bits */
  95. #define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
  96. #define ALPS_DUALPOINT_WITH_PRESSURE 0x400 /* device can report trackpoint pressure */
  97. static const struct alps_model_info alps_model_data[] = {
  98. { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
  99. { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
  100. { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
  101. { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
  102. { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
  103. { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
  104. { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
  105. { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
  106. { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
  107. { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
  108. { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
  109. { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
  110. { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
  111. { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
  112. { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
  113. /*
  114. * XXX This entry is suspicious. First byte has zero lower nibble,
  115. * which is what a normal mouse would report. Also, the value 0x0e
  116. * isn't valid per PS/2 spec.
  117. */
  118. { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
  119. { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
  120. { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
  121. /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
  122. { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
  123. ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
  124. { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
  125. { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
  126. { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
  127. ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
  128. { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
  129. };
  130. static const struct alps_protocol_info alps_v3_protocol_data = {
  131. ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
  132. };
  133. static const struct alps_protocol_info alps_v3_rushmore_data = {
  134. ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
  135. };
  136. static const struct alps_protocol_info alps_v5_protocol_data = {
  137. ALPS_PROTO_V5, 0xc8, 0xd8, 0
  138. };
  139. static const struct alps_protocol_info alps_v7_protocol_data = {
  140. ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
  141. };
  142. static const struct alps_protocol_info alps_v8_protocol_data = {
  143. ALPS_PROTO_V8, 0x18, 0x18, 0
  144. };
  145. /*
  146. * Some v2 models report the stick buttons in separate bits
  147. */
  148. static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
  149. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  150. {
  151. /* Extrapolated from other entries */
  152. .matches = {
  153. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  154. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
  155. },
  156. },
  157. {
  158. /* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
  159. .matches = {
  160. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  161. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
  162. },
  163. },
  164. {
  165. /* Reported-by: Hans de Goede <hdegoede@redhat.com> */
  166. .matches = {
  167. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  168. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
  169. },
  170. },
  171. {
  172. /* Extrapolated from other entries */
  173. .matches = {
  174. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  175. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
  176. },
  177. },
  178. #endif
  179. { }
  180. };
  181. static void alps_set_abs_params_st(struct alps_data *priv,
  182. struct input_dev *dev1);
  183. static void alps_set_abs_params_semi_mt(struct alps_data *priv,
  184. struct input_dev *dev1);
  185. static void alps_set_abs_params_v7(struct alps_data *priv,
  186. struct input_dev *dev1);
  187. static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
  188. struct input_dev *dev1);
  189. /* Packet formats are described in Documentation/input/alps.txt */
  190. static bool alps_is_valid_first_byte(struct alps_data *priv,
  191. unsigned char data)
  192. {
  193. return (data & priv->mask0) == priv->byte0;
  194. }
  195. static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
  196. int left, int right, int middle)
  197. {
  198. struct input_dev *dev;
  199. /*
  200. * If shared button has already been reported on the
  201. * other device (dev2) then this event should be also
  202. * sent through that device.
  203. */
  204. dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
  205. input_report_key(dev, BTN_LEFT, left);
  206. dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
  207. input_report_key(dev, BTN_RIGHT, right);
  208. dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
  209. input_report_key(dev, BTN_MIDDLE, middle);
  210. /*
  211. * Sync the _other_ device now, we'll do the first
  212. * device later once we report the rest of the events.
  213. */
  214. if (dev2)
  215. input_sync(dev2);
  216. }
  217. static void alps_process_packet_v1_v2(struct psmouse *psmouse)
  218. {
  219. struct alps_data *priv = psmouse->private;
  220. unsigned char *packet = psmouse->packet;
  221. struct input_dev *dev = psmouse->dev;
  222. struct input_dev *dev2 = priv->dev2;
  223. int x, y, z, ges, fin, left, right, middle;
  224. int back = 0, forward = 0;
  225. if (priv->proto_version == ALPS_PROTO_V1) {
  226. left = packet[2] & 0x10;
  227. right = packet[2] & 0x08;
  228. middle = 0;
  229. x = packet[1] | ((packet[0] & 0x07) << 7);
  230. y = packet[4] | ((packet[3] & 0x07) << 7);
  231. z = packet[5];
  232. } else {
  233. left = packet[3] & 1;
  234. right = packet[3] & 2;
  235. middle = packet[3] & 4;
  236. x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
  237. y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
  238. z = packet[5];
  239. }
  240. if (priv->flags & ALPS_FW_BK_1) {
  241. back = packet[0] & 0x10;
  242. forward = packet[2] & 4;
  243. }
  244. if (priv->flags & ALPS_FW_BK_2) {
  245. back = packet[3] & 4;
  246. forward = packet[2] & 4;
  247. if ((middle = forward && back))
  248. forward = back = 0;
  249. }
  250. ges = packet[2] & 1;
  251. fin = packet[2] & 2;
  252. if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
  253. input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
  254. input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
  255. alps_report_buttons(dev2, dev, left, right, middle);
  256. input_sync(dev2);
  257. return;
  258. }
  259. /* Some models have separate stick button bits */
  260. if (priv->flags & ALPS_STICK_BITS) {
  261. left |= packet[0] & 1;
  262. right |= packet[0] & 2;
  263. middle |= packet[0] & 4;
  264. }
  265. alps_report_buttons(dev, dev2, left, right, middle);
  266. /* Convert hardware tap to a reasonable Z value */
  267. if (ges && !fin)
  268. z = 40;
  269. /*
  270. * A "tap and drag" operation is reported by the hardware as a transition
  271. * from (!fin && ges) to (fin && ges). This should be translated to the
  272. * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
  273. */
  274. if (ges && fin && !priv->prev_fin) {
  275. input_report_abs(dev, ABS_X, x);
  276. input_report_abs(dev, ABS_Y, y);
  277. input_report_abs(dev, ABS_PRESSURE, 0);
  278. input_report_key(dev, BTN_TOOL_FINGER, 0);
  279. input_sync(dev);
  280. }
  281. priv->prev_fin = fin;
  282. if (z > 30)
  283. input_report_key(dev, BTN_TOUCH, 1);
  284. if (z < 25)
  285. input_report_key(dev, BTN_TOUCH, 0);
  286. if (z > 0) {
  287. input_report_abs(dev, ABS_X, x);
  288. input_report_abs(dev, ABS_Y, y);
  289. }
  290. input_report_abs(dev, ABS_PRESSURE, z);
  291. input_report_key(dev, BTN_TOOL_FINGER, z > 0);
  292. if (priv->flags & ALPS_WHEEL)
  293. input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
  294. if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  295. input_report_key(dev, BTN_FORWARD, forward);
  296. input_report_key(dev, BTN_BACK, back);
  297. }
  298. if (priv->flags & ALPS_FOUR_BUTTONS) {
  299. input_report_key(dev, BTN_0, packet[2] & 4);
  300. input_report_key(dev, BTN_1, packet[0] & 0x10);
  301. input_report_key(dev, BTN_2, packet[3] & 4);
  302. input_report_key(dev, BTN_3, packet[0] & 0x20);
  303. }
  304. input_sync(dev);
  305. }
  306. static void alps_get_bitmap_points(unsigned int map,
  307. struct alps_bitmap_point *low,
  308. struct alps_bitmap_point *high,
  309. int *fingers)
  310. {
  311. struct alps_bitmap_point *point;
  312. int i, bit, prev_bit = 0;
  313. point = low;
  314. for (i = 0; map != 0; i++, map >>= 1) {
  315. bit = map & 1;
  316. if (bit) {
  317. if (!prev_bit) {
  318. point->start_bit = i;
  319. point->num_bits = 0;
  320. (*fingers)++;
  321. }
  322. point->num_bits++;
  323. } else {
  324. if (prev_bit)
  325. point = high;
  326. }
  327. prev_bit = bit;
  328. }
  329. }
  330. /*
  331. * Process bitmap data from semi-mt protocols. Returns the number of
  332. * fingers detected. A return value of 0 means at least one of the
  333. * bitmaps was empty.
  334. *
  335. * The bitmaps don't have enough data to track fingers, so this function
  336. * only generates points representing a bounding box of all contacts.
  337. * These points are returned in fields->mt when the return value
  338. * is greater than 0.
  339. */
  340. static int alps_process_bitmap(struct alps_data *priv,
  341. struct alps_fields *fields)
  342. {
  343. int i, fingers_x = 0, fingers_y = 0, fingers, closest;
  344. struct alps_bitmap_point x_low = {0,}, x_high = {0,};
  345. struct alps_bitmap_point y_low = {0,}, y_high = {0,};
  346. struct input_mt_pos corner[4];
  347. if (!fields->x_map || !fields->y_map)
  348. return 0;
  349. alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
  350. alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
  351. /*
  352. * Fingers can overlap, so we use the maximum count of fingers
  353. * on either axis as the finger count.
  354. */
  355. fingers = max(fingers_x, fingers_y);
  356. /*
  357. * If an axis reports only a single contact, we have overlapping or
  358. * adjacent fingers. Divide the single contact between the two points.
  359. */
  360. if (fingers_x == 1) {
  361. i = (x_low.num_bits - 1) / 2;
  362. x_low.num_bits = x_low.num_bits - i;
  363. x_high.start_bit = x_low.start_bit + i;
  364. x_high.num_bits = max(i, 1);
  365. }
  366. if (fingers_y == 1) {
  367. i = (y_low.num_bits - 1) / 2;
  368. y_low.num_bits = y_low.num_bits - i;
  369. y_high.start_bit = y_low.start_bit + i;
  370. y_high.num_bits = max(i, 1);
  371. }
  372. /* top-left corner */
  373. corner[0].x =
  374. (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
  375. (2 * (priv->x_bits - 1));
  376. corner[0].y =
  377. (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
  378. (2 * (priv->y_bits - 1));
  379. /* top-right corner */
  380. corner[1].x =
  381. (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
  382. (2 * (priv->x_bits - 1));
  383. corner[1].y =
  384. (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
  385. (2 * (priv->y_bits - 1));
  386. /* bottom-right corner */
  387. corner[2].x =
  388. (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
  389. (2 * (priv->x_bits - 1));
  390. corner[2].y =
  391. (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
  392. (2 * (priv->y_bits - 1));
  393. /* bottom-left corner */
  394. corner[3].x =
  395. (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
  396. (2 * (priv->x_bits - 1));
  397. corner[3].y =
  398. (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
  399. (2 * (priv->y_bits - 1));
  400. /* x-bitmap order is reversed on v5 touchpads */
  401. if (priv->proto_version == ALPS_PROTO_V5) {
  402. for (i = 0; i < 4; i++)
  403. corner[i].x = priv->x_max - corner[i].x;
  404. }
  405. /* y-bitmap order is reversed on v3 and v4 touchpads */
  406. if (priv->proto_version == ALPS_PROTO_V3 ||
  407. priv->proto_version == ALPS_PROTO_V4) {
  408. for (i = 0; i < 4; i++)
  409. corner[i].y = priv->y_max - corner[i].y;
  410. }
  411. /*
  412. * We only select a corner for the second touch once per 2 finger
  413. * touch sequence to avoid the chosen corner (and thus the coordinates)
  414. * jumping around when the first touch is in the middle.
  415. */
  416. if (priv->second_touch == -1) {
  417. /* Find corner closest to our st coordinates */
  418. closest = 0x7fffffff;
  419. for (i = 0; i < 4; i++) {
  420. int dx = fields->st.x - corner[i].x;
  421. int dy = fields->st.y - corner[i].y;
  422. int distance = dx * dx + dy * dy;
  423. if (distance < closest) {
  424. priv->second_touch = i;
  425. closest = distance;
  426. }
  427. }
  428. /* And select the opposite corner to use for the 2nd touch */
  429. priv->second_touch = (priv->second_touch + 2) % 4;
  430. }
  431. fields->mt[0] = fields->st;
  432. fields->mt[1] = corner[priv->second_touch];
  433. return fingers;
  434. }
  435. static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
  436. {
  437. input_mt_slot(dev, slot);
  438. input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
  439. input_report_abs(dev, ABS_MT_POSITION_X, x);
  440. input_report_abs(dev, ABS_MT_POSITION_Y, y);
  441. }
  442. static void alps_report_mt_data(struct psmouse *psmouse, int n)
  443. {
  444. struct alps_data *priv = psmouse->private;
  445. struct input_dev *dev = psmouse->dev;
  446. struct alps_fields *f = &priv->f;
  447. int i, slot[MAX_TOUCHES];
  448. input_mt_assign_slots(dev, slot, f->mt, n, 0);
  449. for (i = 0; i < n; i++)
  450. alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
  451. input_mt_sync_frame(dev);
  452. }
  453. static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
  454. {
  455. struct alps_data *priv = psmouse->private;
  456. struct input_dev *dev = psmouse->dev;
  457. struct alps_fields *f = &priv->f;
  458. /* Use st data when we don't have mt data */
  459. if (fingers < 2) {
  460. f->mt[0].x = f->st.x;
  461. f->mt[0].y = f->st.y;
  462. fingers = f->pressure > 0 ? 1 : 0;
  463. priv->second_touch = -1;
  464. }
  465. if (fingers >= 1)
  466. alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
  467. if (fingers >= 2)
  468. alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
  469. input_mt_sync_frame(dev);
  470. input_mt_report_finger_count(dev, fingers);
  471. input_report_key(dev, BTN_LEFT, f->left);
  472. input_report_key(dev, BTN_RIGHT, f->right);
  473. input_report_key(dev, BTN_MIDDLE, f->middle);
  474. input_report_abs(dev, ABS_PRESSURE, f->pressure);
  475. input_sync(dev);
  476. }
  477. static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
  478. {
  479. struct alps_data *priv = psmouse->private;
  480. unsigned char *packet = psmouse->packet;
  481. struct input_dev *dev = priv->dev2;
  482. int x, y, z, left, right, middle;
  483. /* It should be a DualPoint when received trackstick packet */
  484. if (!(priv->flags & ALPS_DUALPOINT)) {
  485. psmouse_warn(psmouse,
  486. "Rejected trackstick packet from non DualPoint device");
  487. return;
  488. }
  489. /* Sanity check packet */
  490. if (!(packet[0] & 0x40)) {
  491. psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
  492. return;
  493. }
  494. /*
  495. * There's a special packet that seems to indicate the end
  496. * of a stream of trackstick data. Filter these out.
  497. */
  498. if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
  499. return;
  500. x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
  501. y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
  502. z = (packet[4] & 0x7c) >> 2;
  503. /*
  504. * The x and y values tend to be quite large, and when used
  505. * alone the trackstick is difficult to use. Scale them down
  506. * to compensate.
  507. */
  508. x /= 8;
  509. y /= 8;
  510. input_report_rel(dev, REL_X, x);
  511. input_report_rel(dev, REL_Y, -y);
  512. /*
  513. * Most ALPS models report the trackstick buttons in the touchpad
  514. * packets, but a few report them here. No reliable way has been
  515. * found to differentiate between the models upfront, so we enable
  516. * the quirk in response to seeing a button press in the trackstick
  517. * packet.
  518. */
  519. left = packet[3] & 0x01;
  520. right = packet[3] & 0x02;
  521. middle = packet[3] & 0x04;
  522. if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
  523. (left || right || middle))
  524. priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
  525. if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
  526. input_report_key(dev, BTN_LEFT, left);
  527. input_report_key(dev, BTN_RIGHT, right);
  528. input_report_key(dev, BTN_MIDDLE, middle);
  529. }
  530. input_sync(dev);
  531. return;
  532. }
  533. static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
  534. {
  535. f->left = !!(p[3] & 0x01);
  536. f->right = !!(p[3] & 0x02);
  537. f->middle = !!(p[3] & 0x04);
  538. f->ts_left = !!(p[3] & 0x10);
  539. f->ts_right = !!(p[3] & 0x20);
  540. f->ts_middle = !!(p[3] & 0x40);
  541. }
  542. static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
  543. struct psmouse *psmouse)
  544. {
  545. f->first_mp = !!(p[4] & 0x40);
  546. f->is_mp = !!(p[0] & 0x40);
  547. if (f->is_mp) {
  548. f->fingers = (p[5] & 0x3) + 1;
  549. f->x_map = ((p[4] & 0x7e) << 8) |
  550. ((p[1] & 0x7f) << 2) |
  551. ((p[0] & 0x30) >> 4);
  552. f->y_map = ((p[3] & 0x70) << 4) |
  553. ((p[2] & 0x7f) << 1) |
  554. (p[4] & 0x01);
  555. } else {
  556. f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
  557. ((p[0] & 0x30) >> 4);
  558. f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
  559. f->pressure = p[5] & 0x7f;
  560. alps_decode_buttons_v3(f, p);
  561. }
  562. return 0;
  563. }
  564. static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
  565. struct psmouse *psmouse)
  566. {
  567. f->first_mp = !!(p[4] & 0x40);
  568. f->is_mp = !!(p[5] & 0x40);
  569. if (f->is_mp) {
  570. f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
  571. f->x_map = ((p[5] & 0x10) << 11) |
  572. ((p[4] & 0x7e) << 8) |
  573. ((p[1] & 0x7f) << 2) |
  574. ((p[0] & 0x30) >> 4);
  575. f->y_map = ((p[5] & 0x20) << 6) |
  576. ((p[3] & 0x70) << 4) |
  577. ((p[2] & 0x7f) << 1) |
  578. (p[4] & 0x01);
  579. } else {
  580. f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
  581. ((p[0] & 0x30) >> 4);
  582. f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
  583. f->pressure = p[5] & 0x7f;
  584. alps_decode_buttons_v3(f, p);
  585. }
  586. return 0;
  587. }
  588. static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
  589. struct psmouse *psmouse)
  590. {
  591. u64 palm_data = 0;
  592. struct alps_data *priv = psmouse->private;
  593. f->first_mp = !!(p[0] & 0x02);
  594. f->is_mp = !!(p[0] & 0x20);
  595. if (!f->is_mp) {
  596. f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
  597. f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
  598. f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
  599. alps_decode_buttons_v3(f, p);
  600. } else {
  601. f->fingers = ((p[0] & 0x6) >> 1 |
  602. (p[0] & 0x10) >> 2);
  603. palm_data = (p[1] & 0x7f) |
  604. ((p[2] & 0x7f) << 7) |
  605. ((p[4] & 0x7f) << 14) |
  606. ((p[5] & 0x7f) << 21) |
  607. ((p[3] & 0x07) << 28) |
  608. (((u64)p[3] & 0x70) << 27) |
  609. (((u64)p[0] & 0x01) << 34);
  610. /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
  611. f->y_map = palm_data & (BIT(priv->y_bits) - 1);
  612. /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
  613. f->x_map = (palm_data >> priv->y_bits) &
  614. (BIT(priv->x_bits) - 1);
  615. }
  616. return 0;
  617. }
  618. static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
  619. {
  620. struct alps_data *priv = psmouse->private;
  621. unsigned char *packet = psmouse->packet;
  622. struct input_dev *dev2 = priv->dev2;
  623. struct alps_fields *f = &priv->f;
  624. int fingers = 0;
  625. memset(f, 0, sizeof(*f));
  626. priv->decode_fields(f, packet, psmouse);
  627. /*
  628. * There's no single feature of touchpad position and bitmap packets
  629. * that can be used to distinguish between them. We rely on the fact
  630. * that a bitmap packet should always follow a position packet with
  631. * bit 6 of packet[4] set.
  632. */
  633. if (priv->multi_packet) {
  634. /*
  635. * Sometimes a position packet will indicate a multi-packet
  636. * sequence, but then what follows is another position
  637. * packet. Check for this, and when it happens process the
  638. * position packet as usual.
  639. */
  640. if (f->is_mp) {
  641. fingers = f->fingers;
  642. /*
  643. * Bitmap processing uses position packet's coordinate
  644. * data, so we need to do decode it first.
  645. */
  646. priv->decode_fields(f, priv->multi_data, psmouse);
  647. if (alps_process_bitmap(priv, f) == 0)
  648. fingers = 0; /* Use st data */
  649. } else {
  650. priv->multi_packet = 0;
  651. }
  652. }
  653. /*
  654. * Bit 6 of byte 0 is not usually set in position packets. The only
  655. * times it seems to be set is in situations where the data is
  656. * suspect anyway, e.g. a palm resting flat on the touchpad. Given
  657. * this combined with the fact that this bit is useful for filtering
  658. * out misidentified bitmap packets, we reject anything with this
  659. * bit set.
  660. */
  661. if (f->is_mp)
  662. return;
  663. if (!priv->multi_packet && f->first_mp) {
  664. priv->multi_packet = 1;
  665. memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
  666. return;
  667. }
  668. priv->multi_packet = 0;
  669. /*
  670. * Sometimes the hardware sends a single packet with z = 0
  671. * in the middle of a stream. Real releases generate packets
  672. * with x, y, and z all zero, so these seem to be flukes.
  673. * Ignore them.
  674. */
  675. if (f->st.x && f->st.y && !f->pressure)
  676. return;
  677. alps_report_semi_mt_data(psmouse, fingers);
  678. if ((priv->flags & ALPS_DUALPOINT) &&
  679. !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
  680. input_report_key(dev2, BTN_LEFT, f->ts_left);
  681. input_report_key(dev2, BTN_RIGHT, f->ts_right);
  682. input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
  683. input_sync(dev2);
  684. }
  685. }
  686. static void alps_process_packet_v3(struct psmouse *psmouse)
  687. {
  688. unsigned char *packet = psmouse->packet;
  689. /*
  690. * v3 protocol packets come in three types, two representing
  691. * touchpad data and one representing trackstick data.
  692. * Trackstick packets seem to be distinguished by always
  693. * having 0x3f in the last byte. This value has never been
  694. * observed in the last byte of either of the other types
  695. * of packets.
  696. */
  697. if (packet[5] == 0x3f) {
  698. alps_process_trackstick_packet_v3(psmouse);
  699. return;
  700. }
  701. alps_process_touchpad_packet_v3_v5(psmouse);
  702. }
  703. static void alps_process_packet_v6(struct psmouse *psmouse)
  704. {
  705. struct alps_data *priv = psmouse->private;
  706. unsigned char *packet = psmouse->packet;
  707. struct input_dev *dev = psmouse->dev;
  708. struct input_dev *dev2 = priv->dev2;
  709. int x, y, z, left, right, middle;
  710. /*
  711. * We can use Byte5 to distinguish if the packet is from Touchpad
  712. * or Trackpoint.
  713. * Touchpad: 0 - 0x7E
  714. * Trackpoint: 0x7F
  715. */
  716. if (packet[5] == 0x7F) {
  717. /* It should be a DualPoint when received Trackpoint packet */
  718. if (!(priv->flags & ALPS_DUALPOINT)) {
  719. psmouse_warn(psmouse,
  720. "Rejected trackstick packet from non DualPoint device");
  721. return;
  722. }
  723. /* Trackpoint packet */
  724. x = packet[1] | ((packet[3] & 0x20) << 2);
  725. y = packet[2] | ((packet[3] & 0x40) << 1);
  726. z = packet[4];
  727. left = packet[3] & 0x01;
  728. right = packet[3] & 0x02;
  729. middle = packet[3] & 0x04;
  730. /* To prevent the cursor jump when finger lifted */
  731. if (x == 0x7F && y == 0x7F && z == 0x7F)
  732. x = y = z = 0;
  733. /* Divide 4 since trackpoint's speed is too fast */
  734. input_report_rel(dev2, REL_X, (char)x / 4);
  735. input_report_rel(dev2, REL_Y, -((char)y / 4));
  736. input_report_key(dev2, BTN_LEFT, left);
  737. input_report_key(dev2, BTN_RIGHT, right);
  738. input_report_key(dev2, BTN_MIDDLE, middle);
  739. input_sync(dev2);
  740. return;
  741. }
  742. /* Touchpad packet */
  743. x = packet[1] | ((packet[3] & 0x78) << 4);
  744. y = packet[2] | ((packet[4] & 0x78) << 4);
  745. z = packet[5];
  746. left = packet[3] & 0x01;
  747. right = packet[3] & 0x02;
  748. if (z > 30)
  749. input_report_key(dev, BTN_TOUCH, 1);
  750. if (z < 25)
  751. input_report_key(dev, BTN_TOUCH, 0);
  752. if (z > 0) {
  753. input_report_abs(dev, ABS_X, x);
  754. input_report_abs(dev, ABS_Y, y);
  755. }
  756. input_report_abs(dev, ABS_PRESSURE, z);
  757. input_report_key(dev, BTN_TOOL_FINGER, z > 0);
  758. /* v6 touchpad does not have middle button */
  759. input_report_key(dev, BTN_LEFT, left);
  760. input_report_key(dev, BTN_RIGHT, right);
  761. input_sync(dev);
  762. }
  763. static void alps_process_packet_v4(struct psmouse *psmouse)
  764. {
  765. struct alps_data *priv = psmouse->private;
  766. unsigned char *packet = psmouse->packet;
  767. struct alps_fields *f = &priv->f;
  768. int offset;
  769. /*
  770. * v4 has a 6-byte encoding for bitmap data, but this data is
  771. * broken up between 3 normal packets. Use priv->multi_packet to
  772. * track our position in the bitmap packet.
  773. */
  774. if (packet[6] & 0x40) {
  775. /* sync, reset position */
  776. priv->multi_packet = 0;
  777. }
  778. if (WARN_ON_ONCE(priv->multi_packet > 2))
  779. return;
  780. offset = 2 * priv->multi_packet;
  781. priv->multi_data[offset] = packet[6];
  782. priv->multi_data[offset + 1] = packet[7];
  783. f->left = !!(packet[4] & 0x01);
  784. f->right = !!(packet[4] & 0x02);
  785. f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
  786. ((packet[0] & 0x30) >> 4);
  787. f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
  788. f->pressure = packet[5] & 0x7f;
  789. if (++priv->multi_packet > 2) {
  790. priv->multi_packet = 0;
  791. f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
  792. ((priv->multi_data[3] & 0x60) << 3) |
  793. ((priv->multi_data[0] & 0x3f) << 2) |
  794. ((priv->multi_data[1] & 0x60) >> 5);
  795. f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
  796. ((priv->multi_data[3] & 0x1f) << 5) |
  797. (priv->multi_data[1] & 0x1f);
  798. f->fingers = alps_process_bitmap(priv, f);
  799. }
  800. alps_report_semi_mt_data(psmouse, f->fingers);
  801. }
  802. static bool alps_is_valid_package_v7(struct psmouse *psmouse)
  803. {
  804. switch (psmouse->pktcnt) {
  805. case 3:
  806. return (psmouse->packet[2] & 0x40) == 0x40;
  807. case 4:
  808. return (psmouse->packet[3] & 0x48) == 0x48;
  809. case 6:
  810. return (psmouse->packet[5] & 0x40) == 0x00;
  811. }
  812. return true;
  813. }
  814. static unsigned char alps_get_packet_id_v7(char *byte)
  815. {
  816. unsigned char packet_id;
  817. if (byte[4] & 0x40)
  818. packet_id = V7_PACKET_ID_TWO;
  819. else if (byte[4] & 0x01)
  820. packet_id = V7_PACKET_ID_MULTI;
  821. else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
  822. packet_id = V7_PACKET_ID_NEW;
  823. else if (byte[1] == 0x00 && byte[4] == 0x00)
  824. packet_id = V7_PACKET_ID_IDLE;
  825. else
  826. packet_id = V7_PACKET_ID_UNKNOWN;
  827. return packet_id;
  828. }
  829. static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
  830. unsigned char *pkt,
  831. unsigned char pkt_id)
  832. {
  833. mt[0].x = ((pkt[2] & 0x80) << 4);
  834. mt[0].x |= ((pkt[2] & 0x3F) << 5);
  835. mt[0].x |= ((pkt[3] & 0x30) >> 1);
  836. mt[0].x |= (pkt[3] & 0x07);
  837. mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
  838. mt[1].x = ((pkt[3] & 0x80) << 4);
  839. mt[1].x |= ((pkt[4] & 0x80) << 3);
  840. mt[1].x |= ((pkt[4] & 0x3F) << 4);
  841. mt[1].y = ((pkt[5] & 0x80) << 3);
  842. mt[1].y |= ((pkt[5] & 0x3F) << 4);
  843. switch (pkt_id) {
  844. case V7_PACKET_ID_TWO:
  845. mt[1].x &= ~0x000F;
  846. mt[1].y |= 0x000F;
  847. /* Detect false-postive touches where x & y report max value */
  848. if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
  849. mt[1].x = 0;
  850. /* y gets set to 0 at the end of this function */
  851. }
  852. break;
  853. case V7_PACKET_ID_MULTI:
  854. mt[1].x &= ~0x003F;
  855. mt[1].y &= ~0x0020;
  856. mt[1].y |= ((pkt[4] & 0x02) << 4);
  857. mt[1].y |= 0x001F;
  858. break;
  859. case V7_PACKET_ID_NEW:
  860. mt[1].x &= ~0x003F;
  861. mt[1].x |= (pkt[0] & 0x20);
  862. mt[1].y |= 0x000F;
  863. break;
  864. }
  865. mt[0].y = 0x7FF - mt[0].y;
  866. mt[1].y = 0x7FF - mt[1].y;
  867. }
  868. static int alps_get_mt_count(struct input_mt_pos *mt)
  869. {
  870. int i, fingers = 0;
  871. for (i = 0; i < MAX_TOUCHES; i++) {
  872. if (mt[i].x != 0 || mt[i].y != 0)
  873. fingers++;
  874. }
  875. return fingers;
  876. }
  877. static int alps_decode_packet_v7(struct alps_fields *f,
  878. unsigned char *p,
  879. struct psmouse *psmouse)
  880. {
  881. struct alps_data *priv = psmouse->private;
  882. unsigned char pkt_id;
  883. pkt_id = alps_get_packet_id_v7(p);
  884. if (pkt_id == V7_PACKET_ID_IDLE)
  885. return 0;
  886. if (pkt_id == V7_PACKET_ID_UNKNOWN)
  887. return -1;
  888. /*
  889. * NEW packets are send to indicate a discontinuity in the finger
  890. * coordinate reporting. Specifically a finger may have moved from
  891. * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
  892. * us.
  893. *
  894. * NEW packets have 3 problems:
  895. * 1) They do not contain middle / right button info (on non clickpads)
  896. * this can be worked around by preserving the old button state
  897. * 2) They do not contain an accurate fingercount, and they are
  898. * typically send when the number of fingers changes. We cannot use
  899. * the old finger count as that may mismatch with the amount of
  900. * touch coordinates we've available in the NEW packet
  901. * 3) Their x data for the second touch is inaccurate leading to
  902. * a possible jump of the x coordinate by 16 units when the first
  903. * non NEW packet comes in
  904. * Since problems 2 & 3 cannot be worked around, just ignore them.
  905. */
  906. if (pkt_id == V7_PACKET_ID_NEW)
  907. return 1;
  908. alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
  909. if (pkt_id == V7_PACKET_ID_TWO)
  910. f->fingers = alps_get_mt_count(f->mt);
  911. else /* pkt_id == V7_PACKET_ID_MULTI */
  912. f->fingers = 3 + (p[5] & 0x03);
  913. f->left = (p[0] & 0x80) >> 7;
  914. if (priv->flags & ALPS_BUTTONPAD) {
  915. if (p[0] & 0x20)
  916. f->fingers++;
  917. if (p[0] & 0x10)
  918. f->fingers++;
  919. } else {
  920. f->right = (p[0] & 0x20) >> 5;
  921. f->middle = (p[0] & 0x10) >> 4;
  922. }
  923. /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
  924. if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
  925. f->mt[0].x = f->mt[1].x;
  926. f->mt[0].y = f->mt[1].y;
  927. f->mt[1].x = 0;
  928. f->mt[1].y = 0;
  929. }
  930. return 0;
  931. }
  932. static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
  933. {
  934. struct alps_data *priv = psmouse->private;
  935. unsigned char *packet = psmouse->packet;
  936. struct input_dev *dev2 = priv->dev2;
  937. int x, y, z, left, right, middle;
  938. /* It should be a DualPoint when received trackstick packet */
  939. if (!(priv->flags & ALPS_DUALPOINT)) {
  940. psmouse_warn(psmouse,
  941. "Rejected trackstick packet from non DualPoint device");
  942. return;
  943. }
  944. x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
  945. y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
  946. ((packet[3] & 0x20) << 1);
  947. z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
  948. left = (packet[1] & 0x01);
  949. right = (packet[1] & 0x02) >> 1;
  950. middle = (packet[1] & 0x04) >> 2;
  951. input_report_rel(dev2, REL_X, (char)x);
  952. input_report_rel(dev2, REL_Y, -((char)y));
  953. input_report_key(dev2, BTN_LEFT, left);
  954. input_report_key(dev2, BTN_RIGHT, right);
  955. input_report_key(dev2, BTN_MIDDLE, middle);
  956. input_sync(dev2);
  957. }
  958. static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
  959. {
  960. struct alps_data *priv = psmouse->private;
  961. struct input_dev *dev = psmouse->dev;
  962. struct alps_fields *f = &priv->f;
  963. memset(f, 0, sizeof(*f));
  964. if (priv->decode_fields(f, psmouse->packet, psmouse))
  965. return;
  966. alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
  967. input_mt_report_finger_count(dev, f->fingers);
  968. input_report_key(dev, BTN_LEFT, f->left);
  969. input_report_key(dev, BTN_RIGHT, f->right);
  970. input_report_key(dev, BTN_MIDDLE, f->middle);
  971. input_sync(dev);
  972. }
  973. static void alps_process_packet_v7(struct psmouse *psmouse)
  974. {
  975. unsigned char *packet = psmouse->packet;
  976. if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
  977. alps_process_trackstick_packet_v7(psmouse);
  978. else
  979. alps_process_touchpad_packet_v7(psmouse);
  980. }
  981. static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
  982. {
  983. unsigned char pkt_id = SS4_PACKET_ID_IDLE;
  984. switch (byte[3] & 0x30) {
  985. case 0x00:
  986. if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
  987. (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 &&
  988. byte[5] == 0x00) {
  989. pkt_id = SS4_PACKET_ID_IDLE;
  990. } else {
  991. pkt_id = SS4_PACKET_ID_ONE;
  992. }
  993. break;
  994. case 0x10:
  995. /* two-finger finger positions */
  996. pkt_id = SS4_PACKET_ID_TWO;
  997. break;
  998. case 0x20:
  999. /* stick pointer */
  1000. pkt_id = SS4_PACKET_ID_STICK;
  1001. break;
  1002. case 0x30:
  1003. /* third and fourth finger positions */
  1004. pkt_id = SS4_PACKET_ID_MULTI;
  1005. break;
  1006. }
  1007. return pkt_id;
  1008. }
  1009. static int alps_decode_ss4_v2(struct alps_fields *f,
  1010. unsigned char *p, struct psmouse *psmouse)
  1011. {
  1012. struct alps_data *priv = psmouse->private;
  1013. unsigned char pkt_id;
  1014. unsigned int no_data_x, no_data_y;
  1015. pkt_id = alps_get_pkt_id_ss4_v2(p);
  1016. /* Current packet is 1Finger coordinate packet */
  1017. switch (pkt_id) {
  1018. case SS4_PACKET_ID_ONE:
  1019. f->mt[0].x = SS4_1F_X_V2(p);
  1020. f->mt[0].y = SS4_1F_Y_V2(p);
  1021. f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
  1022. /*
  1023. * When a button is held the device will give us events
  1024. * with x, y, and pressure of 0. This causes annoying jumps
  1025. * if a touch is released while the button is held.
  1026. * Handle this by claiming zero contacts.
  1027. */
  1028. f->fingers = f->pressure > 0 ? 1 : 0;
  1029. f->first_mp = 0;
  1030. f->is_mp = 0;
  1031. break;
  1032. case SS4_PACKET_ID_TWO:
  1033. if (priv->flags & ALPS_BUTTONPAD) {
  1034. if (IS_SS4PLUS_DEV(priv->dev_id)) {
  1035. f->mt[0].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
  1036. f->mt[1].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
  1037. } else {
  1038. f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
  1039. f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
  1040. }
  1041. f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
  1042. f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
  1043. } else {
  1044. if (IS_SS4PLUS_DEV(priv->dev_id)) {
  1045. f->mt[0].x = SS4_PLUS_STD_MF_X_V2(p, 0);
  1046. f->mt[1].x = SS4_PLUS_STD_MF_X_V2(p, 1);
  1047. } else {
  1048. f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
  1049. f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
  1050. }
  1051. f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
  1052. f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
  1053. }
  1054. f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
  1055. if (SS4_IS_MF_CONTINUE(p)) {
  1056. f->first_mp = 1;
  1057. } else {
  1058. f->fingers = 2;
  1059. f->first_mp = 0;
  1060. }
  1061. f->is_mp = 0;
  1062. break;
  1063. case SS4_PACKET_ID_MULTI:
  1064. if (priv->flags & ALPS_BUTTONPAD) {
  1065. if (IS_SS4PLUS_DEV(priv->dev_id)) {
  1066. f->mt[0].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
  1067. f->mt[1].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
  1068. } else {
  1069. f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
  1070. f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
  1071. }
  1072. f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
  1073. f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
  1074. no_data_x = SS4_MFPACKET_NO_AX_BL;
  1075. no_data_y = SS4_MFPACKET_NO_AY_BL;
  1076. } else {
  1077. if (IS_SS4PLUS_DEV(priv->dev_id)) {
  1078. f->mt[0].x = SS4_PLUS_STD_MF_X_V2(p, 0);
  1079. f->mt[1].x = SS4_PLUS_STD_MF_X_V2(p, 1);
  1080. } else {
  1081. f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
  1082. f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
  1083. }
  1084. f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
  1085. f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
  1086. no_data_x = SS4_MFPACKET_NO_AX;
  1087. no_data_y = SS4_MFPACKET_NO_AY;
  1088. }
  1089. f->first_mp = 0;
  1090. f->is_mp = 1;
  1091. if (SS4_IS_5F_DETECTED(p)) {
  1092. f->fingers = 5;
  1093. } else if (f->mt[3].x == no_data_x &&
  1094. f->mt[3].y == no_data_y) {
  1095. f->mt[3].x = 0;
  1096. f->mt[3].y = 0;
  1097. f->fingers = 3;
  1098. } else {
  1099. f->fingers = 4;
  1100. }
  1101. break;
  1102. case SS4_PACKET_ID_STICK:
  1103. if (!(priv->flags & ALPS_DUALPOINT)) {
  1104. psmouse_warn(psmouse,
  1105. "Rejected trackstick packet from non DualPoint device");
  1106. } else {
  1107. int x = (s8)(((p[0] & 1) << 7) | (p[1] & 0x7f));
  1108. int y = (s8)(((p[3] & 1) << 7) | (p[2] & 0x7f));
  1109. int pressure = (s8)(p[4] & 0x7f);
  1110. input_report_rel(priv->dev2, REL_X, x);
  1111. input_report_rel(priv->dev2, REL_Y, -y);
  1112. input_report_abs(priv->dev2, ABS_PRESSURE, pressure);
  1113. }
  1114. break;
  1115. case SS4_PACKET_ID_IDLE:
  1116. default:
  1117. memset(f, 0, sizeof(struct alps_fields));
  1118. break;
  1119. }
  1120. /* handle buttons */
  1121. if (pkt_id == SS4_PACKET_ID_STICK) {
  1122. f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
  1123. f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
  1124. f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
  1125. } else {
  1126. f->left = !!(SS4_BTN_V2(p) & 0x01);
  1127. if (!(priv->flags & ALPS_BUTTONPAD)) {
  1128. f->right = !!(SS4_BTN_V2(p) & 0x02);
  1129. f->middle = !!(SS4_BTN_V2(p) & 0x04);
  1130. }
  1131. }
  1132. return 0;
  1133. }
  1134. static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
  1135. {
  1136. struct alps_data *priv = psmouse->private;
  1137. unsigned char *packet = psmouse->packet;
  1138. struct input_dev *dev = psmouse->dev;
  1139. struct input_dev *dev2 = priv->dev2;
  1140. struct alps_fields *f = &priv->f;
  1141. memset(f, 0, sizeof(struct alps_fields));
  1142. priv->decode_fields(f, packet, psmouse);
  1143. if (priv->multi_packet) {
  1144. /*
  1145. * Sometimes the first packet will indicate a multi-packet
  1146. * sequence, but sometimes the next multi-packet would not
  1147. * come. Check for this, and when it happens process the
  1148. * position packet as usual.
  1149. */
  1150. if (f->is_mp) {
  1151. /* Now process the 1st packet */
  1152. priv->decode_fields(f, priv->multi_data, psmouse);
  1153. } else {
  1154. priv->multi_packet = 0;
  1155. }
  1156. }
  1157. /*
  1158. * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
  1159. * When it is set, it means 2nd packet comes without 1st packet come.
  1160. */
  1161. if (f->is_mp)
  1162. return;
  1163. /* Save the first packet */
  1164. if (!priv->multi_packet && f->first_mp) {
  1165. priv->multi_packet = 1;
  1166. memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
  1167. return;
  1168. }
  1169. priv->multi_packet = 0;
  1170. /* Report trackstick */
  1171. if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
  1172. if (priv->flags & ALPS_DUALPOINT) {
  1173. input_report_key(dev2, BTN_LEFT, f->ts_left);
  1174. input_report_key(dev2, BTN_RIGHT, f->ts_right);
  1175. input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
  1176. input_sync(dev2);
  1177. }
  1178. return;
  1179. }
  1180. /* Report touchpad */
  1181. alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
  1182. input_mt_report_finger_count(dev, f->fingers);
  1183. input_report_key(dev, BTN_LEFT, f->left);
  1184. input_report_key(dev, BTN_RIGHT, f->right);
  1185. input_report_key(dev, BTN_MIDDLE, f->middle);
  1186. input_report_abs(dev, ABS_PRESSURE, f->pressure);
  1187. input_sync(dev);
  1188. }
  1189. static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
  1190. {
  1191. if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
  1192. return false;
  1193. if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
  1194. return false;
  1195. return true;
  1196. }
  1197. static DEFINE_MUTEX(alps_mutex);
  1198. static void alps_register_bare_ps2_mouse(struct work_struct *work)
  1199. {
  1200. struct alps_data *priv =
  1201. container_of(work, struct alps_data, dev3_register_work.work);
  1202. struct psmouse *psmouse = priv->psmouse;
  1203. struct input_dev *dev3;
  1204. int error = 0;
  1205. mutex_lock(&alps_mutex);
  1206. if (priv->dev3)
  1207. goto out;
  1208. dev3 = input_allocate_device();
  1209. if (!dev3) {
  1210. psmouse_err(psmouse, "failed to allocate secondary device\n");
  1211. error = -ENOMEM;
  1212. goto out;
  1213. }
  1214. snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
  1215. psmouse->ps2dev.serio->phys,
  1216. (priv->dev2 ? "input2" : "input1"));
  1217. dev3->phys = priv->phys3;
  1218. /*
  1219. * format of input device name is: "protocol vendor name"
  1220. * see function psmouse_switch_protocol() in psmouse-base.c
  1221. */
  1222. dev3->name = "PS/2 ALPS Mouse";
  1223. dev3->id.bustype = BUS_I8042;
  1224. dev3->id.vendor = 0x0002;
  1225. dev3->id.product = PSMOUSE_PS2;
  1226. dev3->id.version = 0x0000;
  1227. dev3->dev.parent = &psmouse->ps2dev.serio->dev;
  1228. input_set_capability(dev3, EV_REL, REL_X);
  1229. input_set_capability(dev3, EV_REL, REL_Y);
  1230. input_set_capability(dev3, EV_KEY, BTN_LEFT);
  1231. input_set_capability(dev3, EV_KEY, BTN_RIGHT);
  1232. input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
  1233. __set_bit(INPUT_PROP_POINTER, dev3->propbit);
  1234. error = input_register_device(dev3);
  1235. if (error) {
  1236. psmouse_err(psmouse,
  1237. "failed to register secondary device: %d\n",
  1238. error);
  1239. input_free_device(dev3);
  1240. goto out;
  1241. }
  1242. priv->dev3 = dev3;
  1243. out:
  1244. /*
  1245. * Save the error code so that we can detect that we
  1246. * already tried to create the device.
  1247. */
  1248. if (error)
  1249. priv->dev3 = ERR_PTR(error);
  1250. mutex_unlock(&alps_mutex);
  1251. }
  1252. static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
  1253. unsigned char packet[],
  1254. bool report_buttons)
  1255. {
  1256. struct alps_data *priv = psmouse->private;
  1257. struct input_dev *dev, *dev2 = NULL;
  1258. /* Figure out which device to use to report the bare packet */
  1259. if (priv->proto_version == ALPS_PROTO_V2 &&
  1260. (priv->flags & ALPS_DUALPOINT)) {
  1261. /* On V2 devices the DualPoint Stick reports bare packets */
  1262. dev = priv->dev2;
  1263. dev2 = psmouse->dev;
  1264. } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
  1265. /* Register dev3 mouse if we received PS/2 packet first time */
  1266. if (!IS_ERR(priv->dev3))
  1267. psmouse_queue_work(psmouse, &priv->dev3_register_work,
  1268. 0);
  1269. return;
  1270. } else {
  1271. dev = priv->dev3;
  1272. }
  1273. if (report_buttons)
  1274. alps_report_buttons(dev, dev2,
  1275. packet[0] & 1, packet[0] & 2, packet[0] & 4);
  1276. input_report_rel(dev, REL_X,
  1277. packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
  1278. input_report_rel(dev, REL_Y,
  1279. packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
  1280. input_sync(dev);
  1281. }
  1282. static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
  1283. {
  1284. struct alps_data *priv = psmouse->private;
  1285. if (psmouse->pktcnt < 6)
  1286. return PSMOUSE_GOOD_DATA;
  1287. if (psmouse->pktcnt == 6) {
  1288. /*
  1289. * Start a timer to flush the packet if it ends up last
  1290. * 6-byte packet in the stream. Timer needs to fire
  1291. * psmouse core times out itself. 20 ms should be enough
  1292. * to decide if we are getting more data or not.
  1293. */
  1294. mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
  1295. return PSMOUSE_GOOD_DATA;
  1296. }
  1297. del_timer(&priv->timer);
  1298. if (psmouse->packet[6] & 0x80) {
  1299. /*
  1300. * Highest bit is set - that means we either had
  1301. * complete ALPS packet and this is start of the
  1302. * next packet or we got garbage.
  1303. */
  1304. if (((psmouse->packet[3] |
  1305. psmouse->packet[4] |
  1306. psmouse->packet[5]) & 0x80) ||
  1307. (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
  1308. psmouse_dbg(psmouse,
  1309. "refusing packet %4ph (suspected interleaved ps/2)\n",
  1310. psmouse->packet + 3);
  1311. return PSMOUSE_BAD_DATA;
  1312. }
  1313. priv->process_packet(psmouse);
  1314. /* Continue with the next packet */
  1315. psmouse->packet[0] = psmouse->packet[6];
  1316. psmouse->pktcnt = 1;
  1317. } else {
  1318. /*
  1319. * High bit is 0 - that means that we indeed got a PS/2
  1320. * packet in the middle of ALPS packet.
  1321. *
  1322. * There is also possibility that we got 6-byte ALPS
  1323. * packet followed by 3-byte packet from trackpoint. We
  1324. * can not distinguish between these 2 scenarios but
  1325. * because the latter is unlikely to happen in course of
  1326. * normal operation (user would need to press all
  1327. * buttons on the pad and start moving trackpoint
  1328. * without touching the pad surface) we assume former.
  1329. * Even if we are wrong the wost thing that would happen
  1330. * the cursor would jump but we should not get protocol
  1331. * de-synchronization.
  1332. */
  1333. alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
  1334. false);
  1335. /*
  1336. * Continue with the standard ALPS protocol handling,
  1337. * but make sure we won't process it as an interleaved
  1338. * packet again, which may happen if all buttons are
  1339. * pressed. To avoid this let's reset the 4th bit which
  1340. * is normally 1.
  1341. */
  1342. psmouse->packet[3] = psmouse->packet[6] & 0xf7;
  1343. psmouse->pktcnt = 4;
  1344. }
  1345. return PSMOUSE_GOOD_DATA;
  1346. }
  1347. static void alps_flush_packet(unsigned long data)
  1348. {
  1349. struct psmouse *psmouse = (struct psmouse *)data;
  1350. struct alps_data *priv = psmouse->private;
  1351. serio_pause_rx(psmouse->ps2dev.serio);
  1352. if (psmouse->pktcnt == psmouse->pktsize) {
  1353. /*
  1354. * We did not any more data in reasonable amount of time.
  1355. * Validate the last 3 bytes and process as a standard
  1356. * ALPS packet.
  1357. */
  1358. if ((psmouse->packet[3] |
  1359. psmouse->packet[4] |
  1360. psmouse->packet[5]) & 0x80) {
  1361. psmouse_dbg(psmouse,
  1362. "refusing packet %3ph (suspected interleaved ps/2)\n",
  1363. psmouse->packet + 3);
  1364. } else {
  1365. priv->process_packet(psmouse);
  1366. }
  1367. psmouse->pktcnt = 0;
  1368. }
  1369. serio_continue_rx(psmouse->ps2dev.serio);
  1370. }
  1371. static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
  1372. {
  1373. struct alps_data *priv = psmouse->private;
  1374. /*
  1375. * Check if we are dealing with a bare PS/2 packet, presumably from
  1376. * a device connected to the external PS/2 port. Because bare PS/2
  1377. * protocol does not have enough constant bits to self-synchronize
  1378. * properly we only do this if the device is fully synchronized.
  1379. * Can not distinguish V8's first byte from PS/2 packet's
  1380. */
  1381. if (priv->proto_version != ALPS_PROTO_V8 &&
  1382. !psmouse->out_of_sync_cnt &&
  1383. (psmouse->packet[0] & 0xc8) == 0x08) {
  1384. if (psmouse->pktcnt == 3) {
  1385. alps_report_bare_ps2_packet(psmouse, psmouse->packet,
  1386. true);
  1387. return PSMOUSE_FULL_PACKET;
  1388. }
  1389. return PSMOUSE_GOOD_DATA;
  1390. }
  1391. /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
  1392. if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
  1393. psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
  1394. return alps_handle_interleaved_ps2(psmouse);
  1395. }
  1396. if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
  1397. psmouse_dbg(psmouse,
  1398. "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
  1399. psmouse->packet[0], priv->mask0, priv->byte0);
  1400. return PSMOUSE_BAD_DATA;
  1401. }
  1402. /* Bytes 2 - pktsize should have 0 in the highest bit */
  1403. if (priv->proto_version < ALPS_PROTO_V5 &&
  1404. psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
  1405. (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
  1406. psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
  1407. psmouse->pktcnt - 1,
  1408. psmouse->packet[psmouse->pktcnt - 1]);
  1409. if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
  1410. psmouse->pktcnt == psmouse->pktsize) {
  1411. /*
  1412. * Some Dell boxes, such as Latitude E6440 or E7440
  1413. * with closed lid, quite often smash last byte of
  1414. * otherwise valid packet with 0xff. Given that the
  1415. * next packet is very likely to be valid let's
  1416. * report PSMOUSE_FULL_PACKET but not process data,
  1417. * rather than reporting PSMOUSE_BAD_DATA and
  1418. * filling the logs.
  1419. */
  1420. return PSMOUSE_FULL_PACKET;
  1421. }
  1422. return PSMOUSE_BAD_DATA;
  1423. }
  1424. if ((priv->proto_version == ALPS_PROTO_V7 &&
  1425. !alps_is_valid_package_v7(psmouse)) ||
  1426. (priv->proto_version == ALPS_PROTO_V8 &&
  1427. !alps_is_valid_package_ss4_v2(psmouse))) {
  1428. psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
  1429. psmouse->pktcnt - 1,
  1430. psmouse->packet[psmouse->pktcnt - 1]);
  1431. return PSMOUSE_BAD_DATA;
  1432. }
  1433. if (psmouse->pktcnt == psmouse->pktsize) {
  1434. priv->process_packet(psmouse);
  1435. return PSMOUSE_FULL_PACKET;
  1436. }
  1437. return PSMOUSE_GOOD_DATA;
  1438. }
  1439. static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
  1440. {
  1441. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1442. struct alps_data *priv = psmouse->private;
  1443. int command;
  1444. unsigned char *param;
  1445. unsigned char dummy[4];
  1446. BUG_ON(nibble > 0xf);
  1447. command = priv->nibble_commands[nibble].command;
  1448. param = (command & 0x0f00) ?
  1449. dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
  1450. if (ps2_command(ps2dev, param, command))
  1451. return -1;
  1452. return 0;
  1453. }
  1454. static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
  1455. {
  1456. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1457. struct alps_data *priv = psmouse->private;
  1458. int i, nibble;
  1459. if (ps2_command(ps2dev, NULL, priv->addr_command))
  1460. return -1;
  1461. for (i = 12; i >= 0; i -= 4) {
  1462. nibble = (addr >> i) & 0xf;
  1463. if (alps_command_mode_send_nibble(psmouse, nibble))
  1464. return -1;
  1465. }
  1466. return 0;
  1467. }
  1468. static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
  1469. {
  1470. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1471. unsigned char param[4];
  1472. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  1473. return -1;
  1474. /*
  1475. * The address being read is returned in the first two bytes
  1476. * of the result. Check that this address matches the expected
  1477. * address.
  1478. */
  1479. if (addr != ((param[0] << 8) | param[1]))
  1480. return -1;
  1481. return param[2];
  1482. }
  1483. static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
  1484. {
  1485. if (alps_command_mode_set_addr(psmouse, addr))
  1486. return -1;
  1487. return __alps_command_mode_read_reg(psmouse, addr);
  1488. }
  1489. static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
  1490. {
  1491. if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
  1492. return -1;
  1493. if (alps_command_mode_send_nibble(psmouse, value & 0xf))
  1494. return -1;
  1495. return 0;
  1496. }
  1497. static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
  1498. u8 value)
  1499. {
  1500. if (alps_command_mode_set_addr(psmouse, addr))
  1501. return -1;
  1502. return __alps_command_mode_write_reg(psmouse, value);
  1503. }
  1504. static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
  1505. int repeated_command, unsigned char *param)
  1506. {
  1507. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1508. param[0] = 0;
  1509. if (init_command && ps2_command(ps2dev, param, init_command))
  1510. return -EIO;
  1511. if (ps2_command(ps2dev, NULL, repeated_command) ||
  1512. ps2_command(ps2dev, NULL, repeated_command) ||
  1513. ps2_command(ps2dev, NULL, repeated_command))
  1514. return -EIO;
  1515. param[0] = param[1] = param[2] = 0xff;
  1516. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  1517. return -EIO;
  1518. psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
  1519. repeated_command, param);
  1520. return 0;
  1521. }
  1522. static bool alps_check_valid_firmware_id(unsigned char id[])
  1523. {
  1524. if (id[0] == 0x73)
  1525. return true;
  1526. if (id[0] == 0x88 &&
  1527. (id[1] == 0x07 ||
  1528. id[1] == 0x08 ||
  1529. (id[1] & 0xf0) == 0xb0 ||
  1530. (id[1] & 0xf0) == 0xc0)) {
  1531. return true;
  1532. }
  1533. return false;
  1534. }
  1535. static int alps_enter_command_mode(struct psmouse *psmouse)
  1536. {
  1537. unsigned char param[4];
  1538. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
  1539. psmouse_err(psmouse, "failed to enter command mode\n");
  1540. return -1;
  1541. }
  1542. if (!alps_check_valid_firmware_id(param)) {
  1543. psmouse_dbg(psmouse,
  1544. "unknown response while entering command mode\n");
  1545. return -1;
  1546. }
  1547. return 0;
  1548. }
  1549. static inline int alps_exit_command_mode(struct psmouse *psmouse)
  1550. {
  1551. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1552. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
  1553. return -1;
  1554. return 0;
  1555. }
  1556. /*
  1557. * For DualPoint devices select the device that should respond to
  1558. * subsequent commands. It looks like glidepad is behind stickpointer,
  1559. * I'd thought it would be other way around...
  1560. */
  1561. static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
  1562. {
  1563. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1564. int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
  1565. if (ps2_command(ps2dev, NULL, cmd) ||
  1566. ps2_command(ps2dev, NULL, cmd) ||
  1567. ps2_command(ps2dev, NULL, cmd) ||
  1568. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  1569. return -1;
  1570. /* we may get 3 more bytes, just ignore them */
  1571. ps2_drain(ps2dev, 3, 100);
  1572. return 0;
  1573. }
  1574. static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
  1575. {
  1576. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1577. /* Try ALPS magic knock - 4 disable before enable */
  1578. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1579. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1580. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1581. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1582. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
  1583. return -1;
  1584. /*
  1585. * Switch mouse to poll (remote) mode so motion data will not
  1586. * get in our way
  1587. */
  1588. return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
  1589. }
  1590. static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
  1591. {
  1592. int i, nibble;
  1593. /*
  1594. * b0-b11 are valid bits, send sequence is inverse.
  1595. * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
  1596. */
  1597. for (i = 0; i <= 8; i += 4) {
  1598. nibble = (word >> i) & 0xf;
  1599. if (alps_command_mode_send_nibble(psmouse, nibble))
  1600. return -1;
  1601. }
  1602. return 0;
  1603. }
  1604. static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
  1605. u16 addr, u16 value)
  1606. {
  1607. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1608. /* 0x0A0 is the command to write the word */
  1609. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
  1610. alps_monitor_mode_send_word(psmouse, 0x0A0) ||
  1611. alps_monitor_mode_send_word(psmouse, addr) ||
  1612. alps_monitor_mode_send_word(psmouse, value) ||
  1613. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  1614. return -1;
  1615. return 0;
  1616. }
  1617. static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
  1618. {
  1619. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1620. if (enable) {
  1621. /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
  1622. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
  1623. ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
  1624. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1625. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1626. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  1627. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1628. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  1629. ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
  1630. return -1;
  1631. } else {
  1632. /* EC to exit monitor mode */
  1633. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
  1634. return -1;
  1635. }
  1636. return 0;
  1637. }
  1638. static int alps_absolute_mode_v6(struct psmouse *psmouse)
  1639. {
  1640. u16 reg_val = 0x181;
  1641. int ret = -1;
  1642. /* enter monitor mode, to write the register */
  1643. if (alps_monitor_mode(psmouse, true))
  1644. return -1;
  1645. ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
  1646. if (alps_monitor_mode(psmouse, false))
  1647. ret = -1;
  1648. return ret;
  1649. }
  1650. static int alps_get_status(struct psmouse *psmouse, char *param)
  1651. {
  1652. /* Get status: 0xF5 0xF5 0xF5 0xE9 */
  1653. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
  1654. return -1;
  1655. return 0;
  1656. }
  1657. /*
  1658. * Turn touchpad tapping on or off. The sequences are:
  1659. * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
  1660. * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
  1661. * My guess that 0xE9 (GetInfo) is here as a sync point.
  1662. * For models that also have stickpointer (DualPoints) its tapping
  1663. * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
  1664. * we don't fiddle with it.
  1665. */
  1666. static int alps_tap_mode(struct psmouse *psmouse, int enable)
  1667. {
  1668. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1669. int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
  1670. unsigned char tap_arg = enable ? 0x0A : 0x00;
  1671. unsigned char param[4];
  1672. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
  1673. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1674. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1675. ps2_command(ps2dev, &tap_arg, cmd))
  1676. return -1;
  1677. if (alps_get_status(psmouse, param))
  1678. return -1;
  1679. return 0;
  1680. }
  1681. /*
  1682. * alps_poll() - poll the touchpad for current motion packet.
  1683. * Used in resync.
  1684. */
  1685. static int alps_poll(struct psmouse *psmouse)
  1686. {
  1687. struct alps_data *priv = psmouse->private;
  1688. unsigned char buf[sizeof(psmouse->packet)];
  1689. bool poll_failed;
  1690. if (priv->flags & ALPS_PASS)
  1691. alps_passthrough_mode_v2(psmouse, true);
  1692. poll_failed = ps2_command(&psmouse->ps2dev, buf,
  1693. PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
  1694. if (priv->flags & ALPS_PASS)
  1695. alps_passthrough_mode_v2(psmouse, false);
  1696. if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
  1697. return -1;
  1698. if ((psmouse->badbyte & 0xc8) == 0x08) {
  1699. /*
  1700. * Poll the track stick ...
  1701. */
  1702. if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
  1703. return -1;
  1704. }
  1705. memcpy(psmouse->packet, buf, sizeof(buf));
  1706. return 0;
  1707. }
  1708. static int alps_hw_init_v1_v2(struct psmouse *psmouse)
  1709. {
  1710. struct alps_data *priv = psmouse->private;
  1711. if ((priv->flags & ALPS_PASS) &&
  1712. alps_passthrough_mode_v2(psmouse, true)) {
  1713. return -1;
  1714. }
  1715. if (alps_tap_mode(psmouse, true)) {
  1716. psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
  1717. return -1;
  1718. }
  1719. if (alps_absolute_mode_v1_v2(psmouse)) {
  1720. psmouse_err(psmouse, "Failed to enable absolute mode\n");
  1721. return -1;
  1722. }
  1723. if ((priv->flags & ALPS_PASS) &&
  1724. alps_passthrough_mode_v2(psmouse, false)) {
  1725. return -1;
  1726. }
  1727. /* ALPS needs stream mode, otherwise it won't report any data */
  1728. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
  1729. psmouse_err(psmouse, "Failed to enable stream mode\n");
  1730. return -1;
  1731. }
  1732. return 0;
  1733. }
  1734. static int alps_hw_init_v6(struct psmouse *psmouse)
  1735. {
  1736. unsigned char param[2] = {0xC8, 0x14};
  1737. /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
  1738. if (alps_passthrough_mode_v2(psmouse, true))
  1739. return -1;
  1740. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1741. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1742. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1743. ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1744. ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
  1745. return -1;
  1746. if (alps_passthrough_mode_v2(psmouse, false))
  1747. return -1;
  1748. if (alps_absolute_mode_v6(psmouse)) {
  1749. psmouse_err(psmouse, "Failed to enable absolute mode\n");
  1750. return -1;
  1751. }
  1752. return 0;
  1753. }
  1754. /*
  1755. * Enable or disable passthrough mode to the trackstick.
  1756. */
  1757. static int alps_passthrough_mode_v3(struct psmouse *psmouse,
  1758. int reg_base, bool enable)
  1759. {
  1760. int reg_val, ret = -1;
  1761. if (alps_enter_command_mode(psmouse))
  1762. return -1;
  1763. reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
  1764. if (reg_val == -1)
  1765. goto error;
  1766. if (enable)
  1767. reg_val |= 0x01;
  1768. else
  1769. reg_val &= ~0x01;
  1770. ret = __alps_command_mode_write_reg(psmouse, reg_val);
  1771. error:
  1772. if (alps_exit_command_mode(psmouse))
  1773. ret = -1;
  1774. return ret;
  1775. }
  1776. /* Must be in command mode when calling this function */
  1777. static int alps_absolute_mode_v3(struct psmouse *psmouse)
  1778. {
  1779. int reg_val;
  1780. reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
  1781. if (reg_val == -1)
  1782. return -1;
  1783. reg_val |= 0x06;
  1784. if (__alps_command_mode_write_reg(psmouse, reg_val))
  1785. return -1;
  1786. return 0;
  1787. }
  1788. static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
  1789. {
  1790. int ret = -EIO, reg_val;
  1791. if (alps_enter_command_mode(psmouse))
  1792. goto error;
  1793. reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
  1794. if (reg_val == -1)
  1795. goto error;
  1796. /* bit 7: trackstick is present */
  1797. ret = reg_val & 0x80 ? 0 : -ENODEV;
  1798. error:
  1799. alps_exit_command_mode(psmouse);
  1800. return ret;
  1801. }
  1802. static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
  1803. {
  1804. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1805. int ret = 0;
  1806. unsigned char param[4];
  1807. if (alps_passthrough_mode_v3(psmouse, reg_base, true))
  1808. return -EIO;
  1809. /*
  1810. * E7 report for the trackstick
  1811. *
  1812. * There have been reports of failures to seem to trace back
  1813. * to the above trackstick check failing. When these occur
  1814. * this E7 report fails, so when that happens we continue
  1815. * with the assumption that there isn't a trackstick after
  1816. * all.
  1817. */
  1818. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
  1819. psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
  1820. ret = -ENODEV;
  1821. } else {
  1822. psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
  1823. /*
  1824. * Not sure what this does, but it is absolutely
  1825. * essential. Without it, the touchpad does not
  1826. * work at all and the trackstick just emits normal
  1827. * PS/2 packets.
  1828. */
  1829. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1830. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1831. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1832. alps_command_mode_send_nibble(psmouse, 0x9) ||
  1833. alps_command_mode_send_nibble(psmouse, 0x4)) {
  1834. psmouse_err(psmouse,
  1835. "Error sending magic E6 sequence\n");
  1836. ret = -EIO;
  1837. goto error;
  1838. }
  1839. /*
  1840. * This ensures the trackstick packets are in the format
  1841. * supported by this driver. If bit 1 isn't set the packet
  1842. * format is different.
  1843. */
  1844. if (alps_enter_command_mode(psmouse) ||
  1845. alps_command_mode_write_reg(psmouse,
  1846. reg_base + 0x08, 0x82) ||
  1847. alps_exit_command_mode(psmouse))
  1848. ret = -EIO;
  1849. }
  1850. error:
  1851. if (alps_passthrough_mode_v3(psmouse, reg_base, false))
  1852. ret = -EIO;
  1853. return ret;
  1854. }
  1855. static int alps_hw_init_v3(struct psmouse *psmouse)
  1856. {
  1857. struct alps_data *priv = psmouse->private;
  1858. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1859. int reg_val;
  1860. unsigned char param[4];
  1861. if ((priv->flags & ALPS_DUALPOINT) &&
  1862. alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
  1863. goto error;
  1864. if (alps_enter_command_mode(psmouse) ||
  1865. alps_absolute_mode_v3(psmouse)) {
  1866. psmouse_err(psmouse, "Failed to enter absolute mode\n");
  1867. goto error;
  1868. }
  1869. reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
  1870. if (reg_val == -1)
  1871. goto error;
  1872. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
  1873. goto error;
  1874. reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
  1875. if (reg_val == -1)
  1876. goto error;
  1877. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
  1878. goto error;
  1879. if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
  1880. goto error;
  1881. if (__alps_command_mode_write_reg(psmouse, 0x04))
  1882. goto error;
  1883. if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
  1884. goto error;
  1885. if (__alps_command_mode_write_reg(psmouse, 0x03))
  1886. goto error;
  1887. if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
  1888. goto error;
  1889. if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
  1890. goto error;
  1891. if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
  1892. goto error;
  1893. if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
  1894. goto error;
  1895. alps_exit_command_mode(psmouse);
  1896. /* Set rate and enable data reporting */
  1897. param[0] = 0x64;
  1898. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
  1899. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  1900. psmouse_err(psmouse, "Failed to enable data reporting\n");
  1901. return -1;
  1902. }
  1903. return 0;
  1904. error:
  1905. /*
  1906. * Leaving the touchpad in command mode will essentially render
  1907. * it unusable until the machine reboots, so exit it here just
  1908. * to be safe
  1909. */
  1910. alps_exit_command_mode(psmouse);
  1911. return -1;
  1912. }
  1913. static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
  1914. {
  1915. int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
  1916. struct alps_data *priv = psmouse->private;
  1917. reg = alps_command_mode_read_reg(psmouse, reg_pitch);
  1918. if (reg < 0)
  1919. return reg;
  1920. x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
  1921. x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
  1922. y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
  1923. y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
  1924. reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
  1925. if (reg < 0)
  1926. return reg;
  1927. x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
  1928. x_electrode = 17 + x_electrode;
  1929. y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
  1930. y_electrode = 13 + y_electrode;
  1931. x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
  1932. y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
  1933. priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
  1934. priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
  1935. psmouse_dbg(psmouse,
  1936. "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
  1937. x_pitch, y_pitch, x_electrode, y_electrode,
  1938. x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
  1939. return 0;
  1940. }
  1941. static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
  1942. {
  1943. struct alps_data *priv = psmouse->private;
  1944. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1945. int reg_val, ret = -1;
  1946. if (priv->flags & ALPS_DUALPOINT) {
  1947. reg_val = alps_setup_trackstick_v3(psmouse,
  1948. ALPS_REG_BASE_RUSHMORE);
  1949. if (reg_val == -EIO)
  1950. goto error;
  1951. }
  1952. if (alps_enter_command_mode(psmouse) ||
  1953. alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
  1954. alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
  1955. goto error;
  1956. if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
  1957. goto error;
  1958. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
  1959. if (reg_val == -1)
  1960. goto error;
  1961. if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
  1962. goto error;
  1963. if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
  1964. goto error;
  1965. /* enter absolute mode */
  1966. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
  1967. if (reg_val == -1)
  1968. goto error;
  1969. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
  1970. goto error;
  1971. alps_exit_command_mode(psmouse);
  1972. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  1973. error:
  1974. alps_exit_command_mode(psmouse);
  1975. return ret;
  1976. }
  1977. /* Must be in command mode when calling this function */
  1978. static int alps_absolute_mode_v4(struct psmouse *psmouse)
  1979. {
  1980. int reg_val;
  1981. reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
  1982. if (reg_val == -1)
  1983. return -1;
  1984. reg_val |= 0x02;
  1985. if (__alps_command_mode_write_reg(psmouse, reg_val))
  1986. return -1;
  1987. return 0;
  1988. }
  1989. static int alps_hw_init_v4(struct psmouse *psmouse)
  1990. {
  1991. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1992. unsigned char param[4];
  1993. if (alps_enter_command_mode(psmouse))
  1994. goto error;
  1995. if (alps_absolute_mode_v4(psmouse)) {
  1996. psmouse_err(psmouse, "Failed to enter absolute mode\n");
  1997. goto error;
  1998. }
  1999. if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
  2000. goto error;
  2001. if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
  2002. goto error;
  2003. if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
  2004. goto error;
  2005. if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
  2006. goto error;
  2007. if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
  2008. goto error;
  2009. if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
  2010. goto error;
  2011. if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
  2012. goto error;
  2013. if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
  2014. goto error;
  2015. alps_exit_command_mode(psmouse);
  2016. /*
  2017. * This sequence changes the output from a 9-byte to an
  2018. * 8-byte format. All the same data seems to be present,
  2019. * just in a more compact format.
  2020. */
  2021. param[0] = 0xc8;
  2022. param[1] = 0x64;
  2023. param[2] = 0x50;
  2024. if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  2025. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
  2026. ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
  2027. ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
  2028. return -1;
  2029. /* Set rate and enable data reporting */
  2030. param[0] = 0x64;
  2031. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
  2032. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  2033. psmouse_err(psmouse, "Failed to enable data reporting\n");
  2034. return -1;
  2035. }
  2036. return 0;
  2037. error:
  2038. /*
  2039. * Leaving the touchpad in command mode will essentially render
  2040. * it unusable until the machine reboots, so exit it here just
  2041. * to be safe
  2042. */
  2043. alps_exit_command_mode(psmouse);
  2044. return -1;
  2045. }
  2046. static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
  2047. unsigned char index, unsigned char otp[])
  2048. {
  2049. struct ps2dev *ps2dev = &psmouse->ps2dev;
  2050. switch (index) {
  2051. case 0:
  2052. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  2053. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  2054. ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
  2055. return -1;
  2056. break;
  2057. case 1:
  2058. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  2059. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  2060. ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
  2061. return -1;
  2062. break;
  2063. }
  2064. return 0;
  2065. }
  2066. static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
  2067. struct alps_data *priv)
  2068. {
  2069. int num_x_electrode;
  2070. int num_y_electrode;
  2071. int x_pitch, y_pitch, x_phys, y_phys;
  2072. if (IS_SS4PLUS_DEV(priv->dev_id)) {
  2073. num_x_electrode =
  2074. SS4PLUS_NUMSENSOR_XOFFSET + (otp[0][2] & 0x0F);
  2075. num_y_electrode =
  2076. SS4PLUS_NUMSENSOR_YOFFSET + ((otp[0][2] >> 4) & 0x0F);
  2077. priv->x_max =
  2078. (num_x_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
  2079. priv->y_max =
  2080. (num_y_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
  2081. x_pitch = (otp[0][1] & 0x0F) + SS4PLUS_MIN_PITCH_MM;
  2082. y_pitch = ((otp[0][1] >> 4) & 0x0F) + SS4PLUS_MIN_PITCH_MM;
  2083. } else {
  2084. num_x_electrode =
  2085. SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
  2086. num_y_electrode =
  2087. SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
  2088. priv->x_max =
  2089. (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
  2090. priv->y_max =
  2091. (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
  2092. x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
  2093. y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
  2094. }
  2095. x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
  2096. y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
  2097. priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
  2098. priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
  2099. return 0;
  2100. }
  2101. static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
  2102. struct alps_data *priv)
  2103. {
  2104. unsigned char is_btnless;
  2105. if (IS_SS4PLUS_DEV(priv->dev_id))
  2106. is_btnless = (otp[1][0] >> 1) & 0x01;
  2107. else
  2108. is_btnless = (otp[1][1] >> 3) & 0x01;
  2109. if (is_btnless)
  2110. priv->flags |= ALPS_BUTTONPAD;
  2111. return 0;
  2112. }
  2113. static int alps_update_dual_info_ss4_v2(unsigned char otp[][4],
  2114. struct alps_data *priv)
  2115. {
  2116. bool is_dual = false;
  2117. if (IS_SS4PLUS_DEV(priv->dev_id))
  2118. is_dual = (otp[0][0] >> 4) & 0x01;
  2119. if (is_dual)
  2120. priv->flags |= ALPS_DUALPOINT |
  2121. ALPS_DUALPOINT_WITH_PRESSURE;
  2122. return 0;
  2123. }
  2124. static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
  2125. struct alps_data *priv)
  2126. {
  2127. unsigned char otp[2][4];
  2128. memset(otp, 0, sizeof(otp));
  2129. if (alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]) ||
  2130. alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]))
  2131. return -1;
  2132. alps_update_device_area_ss4_v2(otp, priv);
  2133. alps_update_btn_info_ss4_v2(otp, priv);
  2134. alps_update_dual_info_ss4_v2(otp, priv);
  2135. return 0;
  2136. }
  2137. static int alps_dolphin_get_device_area(struct psmouse *psmouse,
  2138. struct alps_data *priv)
  2139. {
  2140. struct ps2dev *ps2dev = &psmouse->ps2dev;
  2141. unsigned char param[4] = {0};
  2142. int num_x_electrode, num_y_electrode;
  2143. if (alps_enter_command_mode(psmouse))
  2144. return -1;
  2145. param[0] = 0x0a;
  2146. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
  2147. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  2148. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  2149. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  2150. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
  2151. return -1;
  2152. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  2153. return -1;
  2154. /*
  2155. * Dolphin's sensor line number is not fixed. It can be calculated
  2156. * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
  2157. * Further more, we can get device's x_max and y_max by multiplying
  2158. * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
  2159. *
  2160. * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
  2161. * real sensor line number X = 11 + 8 = 19, and
  2162. * real sensor line number Y = 8 + 1 = 9.
  2163. * So, x_max = (19 - 1) * 64 = 1152, and
  2164. * y_max = (9 - 1) * 64 = 512.
  2165. */
  2166. num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
  2167. num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
  2168. priv->x_bits = num_x_electrode;
  2169. priv->y_bits = num_y_electrode;
  2170. priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
  2171. priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
  2172. if (alps_exit_command_mode(psmouse))
  2173. return -1;
  2174. return 0;
  2175. }
  2176. static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
  2177. {
  2178. struct ps2dev *ps2dev = &psmouse->ps2dev;
  2179. unsigned char param[2];
  2180. /* This is dolphin "v1" as empirically defined by florin9doi */
  2181. param[0] = 0x64;
  2182. param[1] = 0x28;
  2183. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  2184. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  2185. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
  2186. return -1;
  2187. return 0;
  2188. }
  2189. static int alps_hw_init_v7(struct psmouse *psmouse)
  2190. {
  2191. struct ps2dev *ps2dev = &psmouse->ps2dev;
  2192. int reg_val, ret = -1;
  2193. if (alps_enter_command_mode(psmouse) ||
  2194. alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
  2195. goto error;
  2196. if (alps_get_v3_v7_resolution(psmouse, 0xc397))
  2197. goto error;
  2198. if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
  2199. goto error;
  2200. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
  2201. if (reg_val == -1)
  2202. goto error;
  2203. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
  2204. goto error;
  2205. alps_exit_command_mode(psmouse);
  2206. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  2207. error:
  2208. alps_exit_command_mode(psmouse);
  2209. return ret;
  2210. }
  2211. static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
  2212. {
  2213. struct ps2dev *ps2dev = &psmouse->ps2dev;
  2214. char param[2] = {0x64, 0x28};
  2215. int ret = -1;
  2216. /* enter absolute mode */
  2217. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  2218. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  2219. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  2220. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
  2221. goto error;
  2222. }
  2223. /* T.B.D. Decread noise packet number, delete in the future */
  2224. if (alps_exit_command_mode(psmouse) ||
  2225. alps_enter_command_mode(psmouse) ||
  2226. alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
  2227. goto error;
  2228. }
  2229. alps_exit_command_mode(psmouse);
  2230. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  2231. error:
  2232. alps_exit_command_mode(psmouse);
  2233. return ret;
  2234. }
  2235. static int alps_set_protocol(struct psmouse *psmouse,
  2236. struct alps_data *priv,
  2237. const struct alps_protocol_info *protocol)
  2238. {
  2239. psmouse->private = priv;
  2240. setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
  2241. priv->proto_version = protocol->version;
  2242. priv->byte0 = protocol->byte0;
  2243. priv->mask0 = protocol->mask0;
  2244. priv->flags = protocol->flags;
  2245. priv->x_max = 2000;
  2246. priv->y_max = 1400;
  2247. priv->x_bits = 15;
  2248. priv->y_bits = 11;
  2249. switch (priv->proto_version) {
  2250. case ALPS_PROTO_V1:
  2251. case ALPS_PROTO_V2:
  2252. priv->hw_init = alps_hw_init_v1_v2;
  2253. priv->process_packet = alps_process_packet_v1_v2;
  2254. priv->set_abs_params = alps_set_abs_params_st;
  2255. priv->x_max = 1023;
  2256. priv->y_max = 767;
  2257. if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
  2258. priv->flags |= ALPS_STICK_BITS;
  2259. break;
  2260. case ALPS_PROTO_V3:
  2261. priv->hw_init = alps_hw_init_v3;
  2262. priv->process_packet = alps_process_packet_v3;
  2263. priv->set_abs_params = alps_set_abs_params_semi_mt;
  2264. priv->decode_fields = alps_decode_pinnacle;
  2265. priv->nibble_commands = alps_v3_nibble_commands;
  2266. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2267. if (alps_probe_trackstick_v3_v7(psmouse,
  2268. ALPS_REG_BASE_PINNACLE) < 0)
  2269. priv->flags &= ~ALPS_DUALPOINT;
  2270. break;
  2271. case ALPS_PROTO_V3_RUSHMORE:
  2272. priv->hw_init = alps_hw_init_rushmore_v3;
  2273. priv->process_packet = alps_process_packet_v3;
  2274. priv->set_abs_params = alps_set_abs_params_semi_mt;
  2275. priv->decode_fields = alps_decode_rushmore;
  2276. priv->nibble_commands = alps_v3_nibble_commands;
  2277. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2278. priv->x_bits = 16;
  2279. priv->y_bits = 12;
  2280. if (alps_probe_trackstick_v3_v7(psmouse,
  2281. ALPS_REG_BASE_RUSHMORE) < 0)
  2282. priv->flags &= ~ALPS_DUALPOINT;
  2283. break;
  2284. case ALPS_PROTO_V4:
  2285. priv->hw_init = alps_hw_init_v4;
  2286. priv->process_packet = alps_process_packet_v4;
  2287. priv->set_abs_params = alps_set_abs_params_semi_mt;
  2288. priv->nibble_commands = alps_v4_nibble_commands;
  2289. priv->addr_command = PSMOUSE_CMD_DISABLE;
  2290. break;
  2291. case ALPS_PROTO_V5:
  2292. priv->hw_init = alps_hw_init_dolphin_v1;
  2293. priv->process_packet = alps_process_touchpad_packet_v3_v5;
  2294. priv->decode_fields = alps_decode_dolphin;
  2295. priv->set_abs_params = alps_set_abs_params_semi_mt;
  2296. priv->nibble_commands = alps_v3_nibble_commands;
  2297. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2298. priv->x_bits = 23;
  2299. priv->y_bits = 12;
  2300. if (alps_dolphin_get_device_area(psmouse, priv))
  2301. return -EIO;
  2302. break;
  2303. case ALPS_PROTO_V6:
  2304. priv->hw_init = alps_hw_init_v6;
  2305. priv->process_packet = alps_process_packet_v6;
  2306. priv->set_abs_params = alps_set_abs_params_st;
  2307. priv->nibble_commands = alps_v6_nibble_commands;
  2308. priv->x_max = 2047;
  2309. priv->y_max = 1535;
  2310. break;
  2311. case ALPS_PROTO_V7:
  2312. priv->hw_init = alps_hw_init_v7;
  2313. priv->process_packet = alps_process_packet_v7;
  2314. priv->decode_fields = alps_decode_packet_v7;
  2315. priv->set_abs_params = alps_set_abs_params_v7;
  2316. priv->nibble_commands = alps_v3_nibble_commands;
  2317. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2318. priv->x_max = 0xfff;
  2319. priv->y_max = 0x7ff;
  2320. if (priv->fw_ver[1] != 0xba)
  2321. priv->flags |= ALPS_BUTTONPAD;
  2322. if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
  2323. priv->flags &= ~ALPS_DUALPOINT;
  2324. break;
  2325. case ALPS_PROTO_V8:
  2326. priv->hw_init = alps_hw_init_ss4_v2;
  2327. priv->process_packet = alps_process_packet_ss4_v2;
  2328. priv->decode_fields = alps_decode_ss4_v2;
  2329. priv->set_abs_params = alps_set_abs_params_ss4_v2;
  2330. priv->nibble_commands = alps_v3_nibble_commands;
  2331. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2332. if (alps_set_defaults_ss4_v2(psmouse, priv))
  2333. return -EIO;
  2334. break;
  2335. }
  2336. return 0;
  2337. }
  2338. static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
  2339. unsigned char *ec)
  2340. {
  2341. const struct alps_model_info *model;
  2342. int i;
  2343. for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
  2344. model = &alps_model_data[i];
  2345. if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
  2346. (!model->command_mode_resp ||
  2347. model->command_mode_resp == ec[2])) {
  2348. return &model->protocol_info;
  2349. }
  2350. }
  2351. return NULL;
  2352. }
  2353. static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
  2354. {
  2355. const struct alps_protocol_info *protocol;
  2356. unsigned char e6[4], e7[4], ec[4];
  2357. int error;
  2358. /*
  2359. * First try "E6 report".
  2360. * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
  2361. * The bits 0-2 of the first byte will be 1s if some buttons are
  2362. * pressed.
  2363. */
  2364. if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  2365. PSMOUSE_CMD_SETSCALE11, e6))
  2366. return -EIO;
  2367. if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
  2368. return -EINVAL;
  2369. /*
  2370. * Now get the "E7" and "EC" reports. These will uniquely identify
  2371. * most ALPS touchpads.
  2372. */
  2373. if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  2374. PSMOUSE_CMD_SETSCALE21, e7) ||
  2375. alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  2376. PSMOUSE_CMD_RESET_WRAP, ec) ||
  2377. alps_exit_command_mode(psmouse))
  2378. return -EIO;
  2379. protocol = alps_match_table(e7, ec);
  2380. if (!protocol) {
  2381. if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
  2382. ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
  2383. protocol = &alps_v5_protocol_data;
  2384. } else if (ec[0] == 0x88 &&
  2385. ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
  2386. protocol = &alps_v7_protocol_data;
  2387. } else if (ec[0] == 0x88 && ec[1] == 0x08) {
  2388. protocol = &alps_v3_rushmore_data;
  2389. } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
  2390. ec[2] >= 0x90 && ec[2] <= 0x9d) {
  2391. protocol = &alps_v3_protocol_data;
  2392. } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
  2393. (e7[2] == 0x14 || e7[2] == 0x28)) {
  2394. protocol = &alps_v8_protocol_data;
  2395. } else {
  2396. psmouse_dbg(psmouse,
  2397. "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
  2398. return -EINVAL;
  2399. }
  2400. }
  2401. if (priv) {
  2402. /* Save Device ID and Firmware version */
  2403. memcpy(priv->dev_id, e7, 3);
  2404. memcpy(priv->fw_ver, ec, 3);
  2405. error = alps_set_protocol(psmouse, priv, protocol);
  2406. if (error)
  2407. return error;
  2408. }
  2409. return 0;
  2410. }
  2411. static int alps_reconnect(struct psmouse *psmouse)
  2412. {
  2413. struct alps_data *priv = psmouse->private;
  2414. psmouse_reset(psmouse);
  2415. if (alps_identify(psmouse, priv) < 0)
  2416. return -1;
  2417. return priv->hw_init(psmouse);
  2418. }
  2419. static void alps_disconnect(struct psmouse *psmouse)
  2420. {
  2421. struct alps_data *priv = psmouse->private;
  2422. psmouse_reset(psmouse);
  2423. del_timer_sync(&priv->timer);
  2424. if (priv->dev2)
  2425. input_unregister_device(priv->dev2);
  2426. if (!IS_ERR_OR_NULL(priv->dev3))
  2427. input_unregister_device(priv->dev3);
  2428. kfree(priv);
  2429. }
  2430. static void alps_set_abs_params_st(struct alps_data *priv,
  2431. struct input_dev *dev1)
  2432. {
  2433. input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
  2434. input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
  2435. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  2436. }
  2437. static void alps_set_abs_params_mt_common(struct alps_data *priv,
  2438. struct input_dev *dev1)
  2439. {
  2440. input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
  2441. input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
  2442. input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
  2443. input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
  2444. set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
  2445. set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
  2446. }
  2447. static void alps_set_abs_params_semi_mt(struct alps_data *priv,
  2448. struct input_dev *dev1)
  2449. {
  2450. alps_set_abs_params_mt_common(priv, dev1);
  2451. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  2452. input_mt_init_slots(dev1, MAX_TOUCHES,
  2453. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
  2454. INPUT_MT_SEMI_MT);
  2455. }
  2456. static void alps_set_abs_params_v7(struct alps_data *priv,
  2457. struct input_dev *dev1)
  2458. {
  2459. alps_set_abs_params_mt_common(priv, dev1);
  2460. set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
  2461. input_mt_init_slots(dev1, MAX_TOUCHES,
  2462. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
  2463. INPUT_MT_TRACK);
  2464. set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
  2465. }
  2466. static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
  2467. struct input_dev *dev1)
  2468. {
  2469. alps_set_abs_params_mt_common(priv, dev1);
  2470. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  2471. set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
  2472. input_mt_init_slots(dev1, MAX_TOUCHES,
  2473. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
  2474. INPUT_MT_TRACK);
  2475. }
  2476. int alps_init(struct psmouse *psmouse)
  2477. {
  2478. struct alps_data *priv = psmouse->private;
  2479. struct input_dev *dev1 = psmouse->dev;
  2480. int error;
  2481. error = priv->hw_init(psmouse);
  2482. if (error)
  2483. goto init_fail;
  2484. /*
  2485. * Undo part of setup done for us by psmouse core since touchpad
  2486. * is not a relative device.
  2487. */
  2488. __clear_bit(EV_REL, dev1->evbit);
  2489. __clear_bit(REL_X, dev1->relbit);
  2490. __clear_bit(REL_Y, dev1->relbit);
  2491. /*
  2492. * Now set up our capabilities.
  2493. */
  2494. dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
  2495. dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
  2496. dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
  2497. dev1->keybit[BIT_WORD(BTN_LEFT)] |=
  2498. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
  2499. dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
  2500. priv->set_abs_params(priv, dev1);
  2501. if (priv->flags & ALPS_WHEEL) {
  2502. dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
  2503. dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
  2504. }
  2505. if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  2506. dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
  2507. dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
  2508. }
  2509. if (priv->flags & ALPS_FOUR_BUTTONS) {
  2510. dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
  2511. dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
  2512. dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
  2513. dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
  2514. } else if (priv->flags & ALPS_BUTTONPAD) {
  2515. set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
  2516. clear_bit(BTN_RIGHT, dev1->keybit);
  2517. } else {
  2518. dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
  2519. }
  2520. if (priv->flags & ALPS_DUALPOINT) {
  2521. struct input_dev *dev2;
  2522. dev2 = input_allocate_device();
  2523. if (!dev2) {
  2524. psmouse_err(psmouse,
  2525. "failed to allocate trackstick device\n");
  2526. error = -ENOMEM;
  2527. goto init_fail;
  2528. }
  2529. snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
  2530. psmouse->ps2dev.serio->phys);
  2531. dev2->phys = priv->phys2;
  2532. /*
  2533. * format of input device name is: "protocol vendor name"
  2534. * see function psmouse_switch_protocol() in psmouse-base.c
  2535. */
  2536. dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
  2537. dev2->id.bustype = BUS_I8042;
  2538. dev2->id.vendor = 0x0002;
  2539. dev2->id.product = PSMOUSE_ALPS;
  2540. dev2->id.version = priv->proto_version;
  2541. dev2->dev.parent = &psmouse->ps2dev.serio->dev;
  2542. input_set_capability(dev2, EV_REL, REL_X);
  2543. input_set_capability(dev2, EV_REL, REL_Y);
  2544. if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
  2545. input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
  2546. input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
  2547. }
  2548. input_set_capability(dev2, EV_KEY, BTN_LEFT);
  2549. input_set_capability(dev2, EV_KEY, BTN_RIGHT);
  2550. input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
  2551. __set_bit(INPUT_PROP_POINTER, dev2->propbit);
  2552. __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
  2553. error = input_register_device(dev2);
  2554. if (error) {
  2555. psmouse_err(psmouse,
  2556. "failed to register trackstick device: %d\n",
  2557. error);
  2558. input_free_device(dev2);
  2559. goto init_fail;
  2560. }
  2561. priv->dev2 = dev2;
  2562. }
  2563. priv->psmouse = psmouse;
  2564. INIT_DELAYED_WORK(&priv->dev3_register_work,
  2565. alps_register_bare_ps2_mouse);
  2566. psmouse->protocol_handler = alps_process_byte;
  2567. psmouse->poll = alps_poll;
  2568. psmouse->disconnect = alps_disconnect;
  2569. psmouse->reconnect = alps_reconnect;
  2570. psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
  2571. /* We are having trouble resyncing ALPS touchpads so disable it for now */
  2572. psmouse->resync_time = 0;
  2573. /* Allow 2 invalid packets without resetting device */
  2574. psmouse->resetafter = psmouse->pktsize * 2;
  2575. return 0;
  2576. init_fail:
  2577. psmouse_reset(psmouse);
  2578. /*
  2579. * Even though we did not allocate psmouse->private we do free
  2580. * it here.
  2581. */
  2582. kfree(psmouse->private);
  2583. psmouse->private = NULL;
  2584. return error;
  2585. }
  2586. int alps_detect(struct psmouse *psmouse, bool set_properties)
  2587. {
  2588. struct alps_data *priv;
  2589. int error;
  2590. error = alps_identify(psmouse, NULL);
  2591. if (error)
  2592. return error;
  2593. /*
  2594. * Reset the device to make sure it is fully operational:
  2595. * on some laptops, like certain Dell Latitudes, we may
  2596. * fail to properly detect presence of trackstick if device
  2597. * has not been reset.
  2598. */
  2599. psmouse_reset(psmouse);
  2600. priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
  2601. if (!priv)
  2602. return -ENOMEM;
  2603. error = alps_identify(psmouse, priv);
  2604. if (error) {
  2605. kfree(priv);
  2606. return error;
  2607. }
  2608. if (set_properties) {
  2609. psmouse->vendor = "ALPS";
  2610. psmouse->name = priv->flags & ALPS_DUALPOINT ?
  2611. "DualPoint TouchPad" : "GlidePoint";
  2612. psmouse->model = priv->proto_version;
  2613. } else {
  2614. /*
  2615. * Destroy alps_data structure we allocated earlier since
  2616. * this was just a "trial run". Otherwise we'll keep it
  2617. * to be used by alps_init() which has to be called if
  2618. * we succeed and set_properties is true.
  2619. */
  2620. kfree(priv);
  2621. psmouse->private = NULL;
  2622. }
  2623. return 0;
  2624. }