alps.c 87 KB

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