alps.c 69 KB

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