alps.c 86 KB

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