alps.c 86 KB

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