alps.c 71 KB

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