alps.c 87 KB

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