alps.c 81 KB

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