alps.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  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. #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
  67. #define ALPS_PASS 0x04 /* device has a pass-through port */
  68. #define ALPS_WHEEL 0x08 /* hardware wheel present */
  69. #define ALPS_FW_BK_1 0x10 /* front & back buttons present */
  70. #define ALPS_FW_BK_2 0x20 /* front & back buttons present */
  71. #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
  72. #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
  73. 6-byte ALPS packet */
  74. static const struct alps_model_info alps_model_data[] = {
  75. { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
  76. { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */
  77. { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
  78. { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
  79. { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */
  80. { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
  81. { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
  82. { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
  83. { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
  84. { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
  85. { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
  86. { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
  87. { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
  88. { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
  89. { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
  90. { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
  91. { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
  92. { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
  93. /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
  94. { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
  95. ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
  96. { { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_DUALPOINT }, /* Dell XT2 */
  97. { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
  98. { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
  99. ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
  100. { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
  101. };
  102. static void alps_set_abs_params_st(struct alps_data *priv,
  103. struct input_dev *dev1);
  104. static void alps_set_abs_params_mt(struct alps_data *priv,
  105. struct input_dev *dev1);
  106. /*
  107. * XXX - this entry is suspicious. First byte has zero lower nibble,
  108. * which is what a normal mouse would report. Also, the value 0x0e
  109. * isn't valid per PS/2 spec.
  110. */
  111. /* Packet formats are described in Documentation/input/alps.txt */
  112. static bool alps_is_valid_first_byte(struct alps_data *priv,
  113. unsigned char data)
  114. {
  115. return (data & priv->mask0) == priv->byte0;
  116. }
  117. static void alps_report_buttons(struct psmouse *psmouse,
  118. struct input_dev *dev1, struct input_dev *dev2,
  119. int left, int right, int middle)
  120. {
  121. struct input_dev *dev;
  122. /*
  123. * If shared button has already been reported on the
  124. * other device (dev2) then this event should be also
  125. * sent through that device.
  126. */
  127. dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
  128. input_report_key(dev, BTN_LEFT, left);
  129. dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
  130. input_report_key(dev, BTN_RIGHT, right);
  131. dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
  132. input_report_key(dev, BTN_MIDDLE, middle);
  133. /*
  134. * Sync the _other_ device now, we'll do the first
  135. * device later once we report the rest of the events.
  136. */
  137. input_sync(dev2);
  138. }
  139. static void alps_process_packet_v1_v2(struct psmouse *psmouse)
  140. {
  141. struct alps_data *priv = psmouse->private;
  142. unsigned char *packet = psmouse->packet;
  143. struct input_dev *dev = psmouse->dev;
  144. struct input_dev *dev2 = priv->dev2;
  145. int x, y, z, ges, fin, left, right, middle;
  146. int back = 0, forward = 0;
  147. if (priv->proto_version == ALPS_PROTO_V1) {
  148. left = packet[2] & 0x10;
  149. right = packet[2] & 0x08;
  150. middle = 0;
  151. x = packet[1] | ((packet[0] & 0x07) << 7);
  152. y = packet[4] | ((packet[3] & 0x07) << 7);
  153. z = packet[5];
  154. } else {
  155. left = packet[3] & 1;
  156. right = packet[3] & 2;
  157. middle = packet[3] & 4;
  158. x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
  159. y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
  160. z = packet[5];
  161. }
  162. if (priv->flags & ALPS_FW_BK_1) {
  163. back = packet[0] & 0x10;
  164. forward = packet[2] & 4;
  165. }
  166. if (priv->flags & ALPS_FW_BK_2) {
  167. back = packet[3] & 4;
  168. forward = packet[2] & 4;
  169. if ((middle = forward && back))
  170. forward = back = 0;
  171. }
  172. ges = packet[2] & 1;
  173. fin = packet[2] & 2;
  174. if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
  175. input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
  176. input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
  177. alps_report_buttons(psmouse, dev2, dev, left, right, middle);
  178. input_sync(dev2);
  179. return;
  180. }
  181. alps_report_buttons(psmouse, dev, dev2, left, right, middle);
  182. /* Convert hardware tap to a reasonable Z value */
  183. if (ges && !fin)
  184. z = 40;
  185. /*
  186. * A "tap and drag" operation is reported by the hardware as a transition
  187. * from (!fin && ges) to (fin && ges). This should be translated to the
  188. * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
  189. */
  190. if (ges && fin && !priv->prev_fin) {
  191. input_report_abs(dev, ABS_X, x);
  192. input_report_abs(dev, ABS_Y, y);
  193. input_report_abs(dev, ABS_PRESSURE, 0);
  194. input_report_key(dev, BTN_TOOL_FINGER, 0);
  195. input_sync(dev);
  196. }
  197. priv->prev_fin = fin;
  198. if (z > 30)
  199. input_report_key(dev, BTN_TOUCH, 1);
  200. if (z < 25)
  201. input_report_key(dev, BTN_TOUCH, 0);
  202. if (z > 0) {
  203. input_report_abs(dev, ABS_X, x);
  204. input_report_abs(dev, ABS_Y, y);
  205. }
  206. input_report_abs(dev, ABS_PRESSURE, z);
  207. input_report_key(dev, BTN_TOOL_FINGER, z > 0);
  208. if (priv->flags & ALPS_WHEEL)
  209. input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
  210. if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  211. input_report_key(dev, BTN_FORWARD, forward);
  212. input_report_key(dev, BTN_BACK, back);
  213. }
  214. if (priv->flags & ALPS_FOUR_BUTTONS) {
  215. input_report_key(dev, BTN_0, packet[2] & 4);
  216. input_report_key(dev, BTN_1, packet[0] & 0x10);
  217. input_report_key(dev, BTN_2, packet[3] & 4);
  218. input_report_key(dev, BTN_3, packet[0] & 0x20);
  219. }
  220. input_sync(dev);
  221. }
  222. /*
  223. * Process bitmap data from v3 and v4 protocols. Returns the number of
  224. * fingers detected. A return value of 0 means at least one of the
  225. * bitmaps was empty.
  226. *
  227. * The bitmaps don't have enough data to track fingers, so this function
  228. * only generates points representing a bounding box of all contacts.
  229. * These points are returned in x1, y1, x2, and y2 when the return value
  230. * is greater than 0.
  231. */
  232. static int alps_process_bitmap(struct alps_data *priv,
  233. unsigned int x_map, unsigned int y_map,
  234. int *x1, int *y1, int *x2, int *y2)
  235. {
  236. struct alps_bitmap_point {
  237. int start_bit;
  238. int num_bits;
  239. };
  240. int fingers_x = 0, fingers_y = 0, fingers;
  241. int i, bit, prev_bit;
  242. struct alps_bitmap_point x_low = {0,}, x_high = {0,};
  243. struct alps_bitmap_point y_low = {0,}, y_high = {0,};
  244. struct alps_bitmap_point *point;
  245. if (!x_map || !y_map)
  246. return 0;
  247. *x1 = *y1 = *x2 = *y2 = 0;
  248. prev_bit = 0;
  249. point = &x_low;
  250. for (i = 0; x_map != 0; i++, x_map >>= 1) {
  251. bit = x_map & 1;
  252. if (bit) {
  253. if (!prev_bit) {
  254. point->start_bit = i;
  255. fingers_x++;
  256. }
  257. point->num_bits++;
  258. } else {
  259. if (prev_bit)
  260. point = &x_high;
  261. else
  262. point->num_bits = 0;
  263. }
  264. prev_bit = bit;
  265. }
  266. /*
  267. * y bitmap is reversed for what we need (lower positions are in
  268. * higher bits), so we process from the top end.
  269. */
  270. y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - priv->y_bits);
  271. prev_bit = 0;
  272. point = &y_low;
  273. for (i = 0; y_map != 0; i++, y_map <<= 1) {
  274. bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1));
  275. if (bit) {
  276. if (!prev_bit) {
  277. point->start_bit = i;
  278. fingers_y++;
  279. }
  280. point->num_bits++;
  281. } else {
  282. if (prev_bit)
  283. point = &y_high;
  284. else
  285. point->num_bits = 0;
  286. }
  287. prev_bit = bit;
  288. }
  289. /*
  290. * Fingers can overlap, so we use the maximum count of fingers
  291. * on either axis as the finger count.
  292. */
  293. fingers = max(fingers_x, fingers_y);
  294. /*
  295. * If total fingers is > 1 but either axis reports only a single
  296. * contact, we have overlapping or adjacent fingers. For the
  297. * purposes of creating a bounding box, divide the single contact
  298. * (roughly) equally between the two points.
  299. */
  300. if (fingers > 1) {
  301. if (fingers_x == 1) {
  302. i = x_low.num_bits / 2;
  303. x_low.num_bits = x_low.num_bits - i;
  304. x_high.start_bit = x_low.start_bit + i;
  305. x_high.num_bits = max(i, 1);
  306. } else if (fingers_y == 1) {
  307. i = y_low.num_bits / 2;
  308. y_low.num_bits = y_low.num_bits - i;
  309. y_high.start_bit = y_low.start_bit + i;
  310. y_high.num_bits = max(i, 1);
  311. }
  312. }
  313. *x1 = (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
  314. (2 * (priv->x_bits - 1));
  315. *y1 = (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
  316. (2 * (priv->y_bits - 1));
  317. if (fingers > 1) {
  318. *x2 = (priv->x_max *
  319. (2 * x_high.start_bit + x_high.num_bits - 1)) /
  320. (2 * (priv->x_bits - 1));
  321. *y2 = (priv->y_max *
  322. (2 * y_high.start_bit + y_high.num_bits - 1)) /
  323. (2 * (priv->y_bits - 1));
  324. }
  325. return fingers;
  326. }
  327. static void alps_set_slot(struct input_dev *dev, int slot, bool active,
  328. int x, int y)
  329. {
  330. input_mt_slot(dev, slot);
  331. input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
  332. if (active) {
  333. input_report_abs(dev, ABS_MT_POSITION_X, x);
  334. input_report_abs(dev, ABS_MT_POSITION_Y, y);
  335. }
  336. }
  337. static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
  338. int x1, int y1, int x2, int y2)
  339. {
  340. alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
  341. alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
  342. }
  343. static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
  344. {
  345. struct alps_data *priv = psmouse->private;
  346. unsigned char *packet = psmouse->packet;
  347. struct input_dev *dev = priv->dev2;
  348. int x, y, z, left, right, middle;
  349. /* Sanity check packet */
  350. if (!(packet[0] & 0x40)) {
  351. psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
  352. return;
  353. }
  354. /*
  355. * There's a special packet that seems to indicate the end
  356. * of a stream of trackstick data. Filter these out.
  357. */
  358. if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
  359. return;
  360. x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
  361. y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
  362. z = (packet[4] & 0x7c) >> 2;
  363. /*
  364. * The x and y values tend to be quite large, and when used
  365. * alone the trackstick is difficult to use. Scale them down
  366. * to compensate.
  367. */
  368. x /= 8;
  369. y /= 8;
  370. input_report_rel(dev, REL_X, x);
  371. input_report_rel(dev, REL_Y, -y);
  372. /*
  373. * Most ALPS models report the trackstick buttons in the touchpad
  374. * packets, but a few report them here. No reliable way has been
  375. * found to differentiate between the models upfront, so we enable
  376. * the quirk in response to seeing a button press in the trackstick
  377. * packet.
  378. */
  379. left = packet[3] & 0x01;
  380. right = packet[3] & 0x02;
  381. middle = packet[3] & 0x04;
  382. if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
  383. (left || right || middle))
  384. priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
  385. if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
  386. input_report_key(dev, BTN_LEFT, left);
  387. input_report_key(dev, BTN_RIGHT, right);
  388. input_report_key(dev, BTN_MIDDLE, middle);
  389. }
  390. input_sync(dev);
  391. return;
  392. }
  393. static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
  394. {
  395. f->left = !!(p[3] & 0x01);
  396. f->right = !!(p[3] & 0x02);
  397. f->middle = !!(p[3] & 0x04);
  398. f->ts_left = !!(p[3] & 0x10);
  399. f->ts_right = !!(p[3] & 0x20);
  400. f->ts_middle = !!(p[3] & 0x40);
  401. }
  402. static void alps_decode_pinnacle(struct alps_fields *f, unsigned char *p)
  403. {
  404. f->first_mp = !!(p[4] & 0x40);
  405. f->is_mp = !!(p[0] & 0x40);
  406. f->fingers = (p[5] & 0x3) + 1;
  407. f->x_map = ((p[4] & 0x7e) << 8) |
  408. ((p[1] & 0x7f) << 2) |
  409. ((p[0] & 0x30) >> 4);
  410. f->y_map = ((p[3] & 0x70) << 4) |
  411. ((p[2] & 0x7f) << 1) |
  412. (p[4] & 0x01);
  413. f->x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
  414. ((p[0] & 0x30) >> 4);
  415. f->y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
  416. f->z = p[5] & 0x7f;
  417. alps_decode_buttons_v3(f, p);
  418. }
  419. static void alps_decode_rushmore(struct alps_fields *f, unsigned char *p)
  420. {
  421. alps_decode_pinnacle(f, p);
  422. f->x_map |= (p[5] & 0x10) << 11;
  423. f->y_map |= (p[5] & 0x20) << 6;
  424. }
  425. static void alps_decode_dolphin(struct alps_fields *f, unsigned char *p)
  426. {
  427. f->first_mp = !!(p[0] & 0x02);
  428. f->is_mp = !!(p[0] & 0x20);
  429. f->fingers = ((p[0] & 0x6) >> 1 |
  430. (p[0] & 0x10) >> 2);
  431. f->x_map = ((p[2] & 0x60) >> 5) |
  432. ((p[4] & 0x7f) << 2) |
  433. ((p[5] & 0x7f) << 9) |
  434. ((p[3] & 0x07) << 16) |
  435. ((p[3] & 0x70) << 15) |
  436. ((p[0] & 0x01) << 22);
  437. f->y_map = (p[1] & 0x7f) |
  438. ((p[2] & 0x1f) << 7);
  439. f->x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
  440. f->y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
  441. f->z = (p[0] & 4) ? 0 : p[5] & 0x7f;
  442. alps_decode_buttons_v3(f, p);
  443. }
  444. static void alps_process_touchpad_packet_v3(struct psmouse *psmouse)
  445. {
  446. struct alps_data *priv = psmouse->private;
  447. unsigned char *packet = psmouse->packet;
  448. struct input_dev *dev = psmouse->dev;
  449. struct input_dev *dev2 = priv->dev2;
  450. int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  451. int fingers = 0, bmap_fingers;
  452. struct alps_fields f;
  453. priv->decode_fields(&f, packet);
  454. /*
  455. * There's no single feature of touchpad position and bitmap packets
  456. * that can be used to distinguish between them. We rely on the fact
  457. * that a bitmap packet should always follow a position packet with
  458. * bit 6 of packet[4] set.
  459. */
  460. if (priv->multi_packet) {
  461. /*
  462. * Sometimes a position packet will indicate a multi-packet
  463. * sequence, but then what follows is another position
  464. * packet. Check for this, and when it happens process the
  465. * position packet as usual.
  466. */
  467. if (f.is_mp) {
  468. fingers = f.fingers;
  469. bmap_fingers = alps_process_bitmap(priv,
  470. f.x_map, f.y_map,
  471. &x1, &y1, &x2, &y2);
  472. /*
  473. * We shouldn't report more than one finger if
  474. * we don't have two coordinates.
  475. */
  476. if (fingers > 1 && bmap_fingers < 2)
  477. fingers = bmap_fingers;
  478. /* Now process position packet */
  479. priv->decode_fields(&f, priv->multi_data);
  480. } else {
  481. priv->multi_packet = 0;
  482. }
  483. }
  484. /*
  485. * Bit 6 of byte 0 is not usually set in position packets. The only
  486. * times it seems to be set is in situations where the data is
  487. * suspect anyway, e.g. a palm resting flat on the touchpad. Given
  488. * this combined with the fact that this bit is useful for filtering
  489. * out misidentified bitmap packets, we reject anything with this
  490. * bit set.
  491. */
  492. if (f.is_mp)
  493. return;
  494. if (!priv->multi_packet && f.first_mp) {
  495. priv->multi_packet = 1;
  496. memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
  497. return;
  498. }
  499. priv->multi_packet = 0;
  500. /*
  501. * Sometimes the hardware sends a single packet with z = 0
  502. * in the middle of a stream. Real releases generate packets
  503. * with x, y, and z all zero, so these seem to be flukes.
  504. * Ignore them.
  505. */
  506. if (f.x && f.y && !f.z)
  507. return;
  508. /*
  509. * If we don't have MT data or the bitmaps were empty, we have
  510. * to rely on ST data.
  511. */
  512. if (!fingers) {
  513. x1 = f.x;
  514. y1 = f.y;
  515. fingers = f.z > 0 ? 1 : 0;
  516. }
  517. if (f.z >= 64)
  518. input_report_key(dev, BTN_TOUCH, 1);
  519. else
  520. input_report_key(dev, BTN_TOUCH, 0);
  521. alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
  522. input_mt_report_finger_count(dev, fingers);
  523. input_report_key(dev, BTN_LEFT, f.left);
  524. input_report_key(dev, BTN_RIGHT, f.right);
  525. input_report_key(dev, BTN_MIDDLE, f.middle);
  526. if (f.z > 0) {
  527. input_report_abs(dev, ABS_X, f.x);
  528. input_report_abs(dev, ABS_Y, f.y);
  529. }
  530. input_report_abs(dev, ABS_PRESSURE, f.z);
  531. input_sync(dev);
  532. if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
  533. input_report_key(dev2, BTN_LEFT, f.ts_left);
  534. input_report_key(dev2, BTN_RIGHT, f.ts_right);
  535. input_report_key(dev2, BTN_MIDDLE, f.ts_middle);
  536. input_sync(dev2);
  537. }
  538. }
  539. static void alps_process_packet_v3(struct psmouse *psmouse)
  540. {
  541. unsigned char *packet = psmouse->packet;
  542. /*
  543. * v3 protocol packets come in three types, two representing
  544. * touchpad data and one representing trackstick data.
  545. * Trackstick packets seem to be distinguished by always
  546. * having 0x3f in the last byte. This value has never been
  547. * observed in the last byte of either of the other types
  548. * of packets.
  549. */
  550. if (packet[5] == 0x3f) {
  551. alps_process_trackstick_packet_v3(psmouse);
  552. return;
  553. }
  554. alps_process_touchpad_packet_v3(psmouse);
  555. }
  556. static void alps_process_packet_v4(struct psmouse *psmouse)
  557. {
  558. struct alps_data *priv = psmouse->private;
  559. unsigned char *packet = psmouse->packet;
  560. struct input_dev *dev = psmouse->dev;
  561. int offset;
  562. int x, y, z;
  563. int left, right;
  564. int x1, y1, x2, y2;
  565. int fingers = 0;
  566. unsigned int x_bitmap, y_bitmap;
  567. /*
  568. * v4 has a 6-byte encoding for bitmap data, but this data is
  569. * broken up between 3 normal packets. Use priv->multi_packet to
  570. * track our position in the bitmap packet.
  571. */
  572. if (packet[6] & 0x40) {
  573. /* sync, reset position */
  574. priv->multi_packet = 0;
  575. }
  576. if (WARN_ON_ONCE(priv->multi_packet > 2))
  577. return;
  578. offset = 2 * priv->multi_packet;
  579. priv->multi_data[offset] = packet[6];
  580. priv->multi_data[offset + 1] = packet[7];
  581. if (++priv->multi_packet > 2) {
  582. priv->multi_packet = 0;
  583. x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
  584. ((priv->multi_data[3] & 0x60) << 3) |
  585. ((priv->multi_data[0] & 0x3f) << 2) |
  586. ((priv->multi_data[1] & 0x60) >> 5);
  587. y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
  588. ((priv->multi_data[3] & 0x1f) << 5) |
  589. (priv->multi_data[1] & 0x1f);
  590. fingers = alps_process_bitmap(priv, x_bitmap, y_bitmap,
  591. &x1, &y1, &x2, &y2);
  592. /* Store MT data.*/
  593. priv->fingers = fingers;
  594. priv->x1 = x1;
  595. priv->x2 = x2;
  596. priv->y1 = y1;
  597. priv->y2 = y2;
  598. }
  599. left = packet[4] & 0x01;
  600. right = packet[4] & 0x02;
  601. x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
  602. ((packet[0] & 0x30) >> 4);
  603. y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
  604. z = packet[5] & 0x7f;
  605. /*
  606. * If there were no contacts in the bitmap, use ST
  607. * points in MT reports.
  608. * If there were two contacts or more, report MT data.
  609. */
  610. if (priv->fingers < 2) {
  611. x1 = x;
  612. y1 = y;
  613. fingers = z > 0 ? 1 : 0;
  614. } else {
  615. fingers = priv->fingers;
  616. x1 = priv->x1;
  617. x2 = priv->x2;
  618. y1 = priv->y1;
  619. y2 = priv->y2;
  620. }
  621. if (z >= 64)
  622. input_report_key(dev, BTN_TOUCH, 1);
  623. else
  624. input_report_key(dev, BTN_TOUCH, 0);
  625. alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
  626. input_mt_report_finger_count(dev, fingers);
  627. input_report_key(dev, BTN_LEFT, left);
  628. input_report_key(dev, BTN_RIGHT, right);
  629. if (z > 0) {
  630. input_report_abs(dev, ABS_X, x);
  631. input_report_abs(dev, ABS_Y, y);
  632. }
  633. input_report_abs(dev, ABS_PRESSURE, z);
  634. input_sync(dev);
  635. }
  636. static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
  637. unsigned char packet[],
  638. bool report_buttons)
  639. {
  640. struct alps_data *priv = psmouse->private;
  641. struct input_dev *dev2 = priv->dev2;
  642. if (report_buttons)
  643. alps_report_buttons(psmouse, dev2, psmouse->dev,
  644. packet[0] & 1, packet[0] & 2, packet[0] & 4);
  645. input_report_rel(dev2, REL_X,
  646. packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
  647. input_report_rel(dev2, REL_Y,
  648. packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
  649. input_sync(dev2);
  650. }
  651. static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
  652. {
  653. struct alps_data *priv = psmouse->private;
  654. if (psmouse->pktcnt < 6)
  655. return PSMOUSE_GOOD_DATA;
  656. if (psmouse->pktcnt == 6) {
  657. /*
  658. * Start a timer to flush the packet if it ends up last
  659. * 6-byte packet in the stream. Timer needs to fire
  660. * psmouse core times out itself. 20 ms should be enough
  661. * to decide if we are getting more data or not.
  662. */
  663. mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
  664. return PSMOUSE_GOOD_DATA;
  665. }
  666. del_timer(&priv->timer);
  667. if (psmouse->packet[6] & 0x80) {
  668. /*
  669. * Highest bit is set - that means we either had
  670. * complete ALPS packet and this is start of the
  671. * next packet or we got garbage.
  672. */
  673. if (((psmouse->packet[3] |
  674. psmouse->packet[4] |
  675. psmouse->packet[5]) & 0x80) ||
  676. (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
  677. psmouse_dbg(psmouse,
  678. "refusing packet %4ph (suspected interleaved ps/2)\n",
  679. psmouse->packet + 3);
  680. return PSMOUSE_BAD_DATA;
  681. }
  682. priv->process_packet(psmouse);
  683. /* Continue with the next packet */
  684. psmouse->packet[0] = psmouse->packet[6];
  685. psmouse->pktcnt = 1;
  686. } else {
  687. /*
  688. * High bit is 0 - that means that we indeed got a PS/2
  689. * packet in the middle of ALPS packet.
  690. *
  691. * There is also possibility that we got 6-byte ALPS
  692. * packet followed by 3-byte packet from trackpoint. We
  693. * can not distinguish between these 2 scenarios but
  694. * because the latter is unlikely to happen in course of
  695. * normal operation (user would need to press all
  696. * buttons on the pad and start moving trackpoint
  697. * without touching the pad surface) we assume former.
  698. * Even if we are wrong the wost thing that would happen
  699. * the cursor would jump but we should not get protocol
  700. * de-synchronization.
  701. */
  702. alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
  703. false);
  704. /*
  705. * Continue with the standard ALPS protocol handling,
  706. * but make sure we won't process it as an interleaved
  707. * packet again, which may happen if all buttons are
  708. * pressed. To avoid this let's reset the 4th bit which
  709. * is normally 1.
  710. */
  711. psmouse->packet[3] = psmouse->packet[6] & 0xf7;
  712. psmouse->pktcnt = 4;
  713. }
  714. return PSMOUSE_GOOD_DATA;
  715. }
  716. static void alps_flush_packet(unsigned long data)
  717. {
  718. struct psmouse *psmouse = (struct psmouse *)data;
  719. struct alps_data *priv = psmouse->private;
  720. serio_pause_rx(psmouse->ps2dev.serio);
  721. if (psmouse->pktcnt == psmouse->pktsize) {
  722. /*
  723. * We did not any more data in reasonable amount of time.
  724. * Validate the last 3 bytes and process as a standard
  725. * ALPS packet.
  726. */
  727. if ((psmouse->packet[3] |
  728. psmouse->packet[4] |
  729. psmouse->packet[5]) & 0x80) {
  730. psmouse_dbg(psmouse,
  731. "refusing packet %3ph (suspected interleaved ps/2)\n",
  732. psmouse->packet + 3);
  733. } else {
  734. priv->process_packet(psmouse);
  735. }
  736. psmouse->pktcnt = 0;
  737. }
  738. serio_continue_rx(psmouse->ps2dev.serio);
  739. }
  740. static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
  741. {
  742. struct alps_data *priv = psmouse->private;
  743. if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
  744. if (psmouse->pktcnt == 3) {
  745. alps_report_bare_ps2_packet(psmouse, psmouse->packet,
  746. true);
  747. return PSMOUSE_FULL_PACKET;
  748. }
  749. return PSMOUSE_GOOD_DATA;
  750. }
  751. /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
  752. if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
  753. psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
  754. return alps_handle_interleaved_ps2(psmouse);
  755. }
  756. if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
  757. psmouse_dbg(psmouse,
  758. "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
  759. psmouse->packet[0], priv->mask0, priv->byte0);
  760. return PSMOUSE_BAD_DATA;
  761. }
  762. /* Bytes 2 - pktsize should have 0 in the highest bit */
  763. if (priv->proto_version != ALPS_PROTO_V5 &&
  764. psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
  765. (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
  766. psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
  767. psmouse->pktcnt - 1,
  768. psmouse->packet[psmouse->pktcnt - 1]);
  769. return PSMOUSE_BAD_DATA;
  770. }
  771. if (psmouse->pktcnt == psmouse->pktsize) {
  772. priv->process_packet(psmouse);
  773. return PSMOUSE_FULL_PACKET;
  774. }
  775. return PSMOUSE_GOOD_DATA;
  776. }
  777. static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
  778. {
  779. struct ps2dev *ps2dev = &psmouse->ps2dev;
  780. struct alps_data *priv = psmouse->private;
  781. int command;
  782. unsigned char *param;
  783. unsigned char dummy[4];
  784. BUG_ON(nibble > 0xf);
  785. command = priv->nibble_commands[nibble].command;
  786. param = (command & 0x0f00) ?
  787. dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
  788. if (ps2_command(ps2dev, param, command))
  789. return -1;
  790. return 0;
  791. }
  792. static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
  793. {
  794. struct ps2dev *ps2dev = &psmouse->ps2dev;
  795. struct alps_data *priv = psmouse->private;
  796. int i, nibble;
  797. if (ps2_command(ps2dev, NULL, priv->addr_command))
  798. return -1;
  799. for (i = 12; i >= 0; i -= 4) {
  800. nibble = (addr >> i) & 0xf;
  801. if (alps_command_mode_send_nibble(psmouse, nibble))
  802. return -1;
  803. }
  804. return 0;
  805. }
  806. static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
  807. {
  808. struct ps2dev *ps2dev = &psmouse->ps2dev;
  809. unsigned char param[4];
  810. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  811. return -1;
  812. /*
  813. * The address being read is returned in the first two bytes
  814. * of the result. Check that this address matches the expected
  815. * address.
  816. */
  817. if (addr != ((param[0] << 8) | param[1]))
  818. return -1;
  819. return param[2];
  820. }
  821. static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
  822. {
  823. if (alps_command_mode_set_addr(psmouse, addr))
  824. return -1;
  825. return __alps_command_mode_read_reg(psmouse, addr);
  826. }
  827. static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
  828. {
  829. if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
  830. return -1;
  831. if (alps_command_mode_send_nibble(psmouse, value & 0xf))
  832. return -1;
  833. return 0;
  834. }
  835. static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
  836. u8 value)
  837. {
  838. if (alps_command_mode_set_addr(psmouse, addr))
  839. return -1;
  840. return __alps_command_mode_write_reg(psmouse, value);
  841. }
  842. static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
  843. int repeated_command, unsigned char *param)
  844. {
  845. struct ps2dev *ps2dev = &psmouse->ps2dev;
  846. param[0] = 0;
  847. if (init_command && ps2_command(ps2dev, param, init_command))
  848. return -EIO;
  849. if (ps2_command(ps2dev, NULL, repeated_command) ||
  850. ps2_command(ps2dev, NULL, repeated_command) ||
  851. ps2_command(ps2dev, NULL, repeated_command))
  852. return -EIO;
  853. param[0] = param[1] = param[2] = 0xff;
  854. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  855. return -EIO;
  856. psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
  857. repeated_command, param);
  858. return 0;
  859. }
  860. static int alps_enter_command_mode(struct psmouse *psmouse)
  861. {
  862. unsigned char param[4];
  863. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
  864. psmouse_err(psmouse, "failed to enter command mode\n");
  865. return -1;
  866. }
  867. if ((param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) &&
  868. param[0] != 0x73) {
  869. psmouse_dbg(psmouse,
  870. "unknown response while entering command mode\n");
  871. return -1;
  872. }
  873. return 0;
  874. }
  875. static inline int alps_exit_command_mode(struct psmouse *psmouse)
  876. {
  877. struct ps2dev *ps2dev = &psmouse->ps2dev;
  878. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
  879. return -1;
  880. return 0;
  881. }
  882. /*
  883. * For DualPoint devices select the device that should respond to
  884. * subsequent commands. It looks like glidepad is behind stickpointer,
  885. * I'd thought it would be other way around...
  886. */
  887. static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
  888. {
  889. struct ps2dev *ps2dev = &psmouse->ps2dev;
  890. int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
  891. if (ps2_command(ps2dev, NULL, cmd) ||
  892. ps2_command(ps2dev, NULL, cmd) ||
  893. ps2_command(ps2dev, NULL, cmd) ||
  894. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  895. return -1;
  896. /* we may get 3 more bytes, just ignore them */
  897. ps2_drain(ps2dev, 3, 100);
  898. return 0;
  899. }
  900. static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
  901. {
  902. struct ps2dev *ps2dev = &psmouse->ps2dev;
  903. /* Try ALPS magic knock - 4 disable before enable */
  904. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  905. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  906. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  907. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  908. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
  909. return -1;
  910. /*
  911. * Switch mouse to poll (remote) mode so motion data will not
  912. * get in our way
  913. */
  914. return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
  915. }
  916. static int alps_get_status(struct psmouse *psmouse, char *param)
  917. {
  918. /* Get status: 0xF5 0xF5 0xF5 0xE9 */
  919. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
  920. return -1;
  921. return 0;
  922. }
  923. /*
  924. * Turn touchpad tapping on or off. The sequences are:
  925. * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
  926. * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
  927. * My guess that 0xE9 (GetInfo) is here as a sync point.
  928. * For models that also have stickpointer (DualPoints) its tapping
  929. * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
  930. * we don't fiddle with it.
  931. */
  932. static int alps_tap_mode(struct psmouse *psmouse, int enable)
  933. {
  934. struct ps2dev *ps2dev = &psmouse->ps2dev;
  935. int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
  936. unsigned char tap_arg = enable ? 0x0A : 0x00;
  937. unsigned char param[4];
  938. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
  939. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  940. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  941. ps2_command(ps2dev, &tap_arg, cmd))
  942. return -1;
  943. if (alps_get_status(psmouse, param))
  944. return -1;
  945. return 0;
  946. }
  947. /*
  948. * alps_poll() - poll the touchpad for current motion packet.
  949. * Used in resync.
  950. */
  951. static int alps_poll(struct psmouse *psmouse)
  952. {
  953. struct alps_data *priv = psmouse->private;
  954. unsigned char buf[sizeof(psmouse->packet)];
  955. bool poll_failed;
  956. if (priv->flags & ALPS_PASS)
  957. alps_passthrough_mode_v2(psmouse, true);
  958. poll_failed = ps2_command(&psmouse->ps2dev, buf,
  959. PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
  960. if (priv->flags & ALPS_PASS)
  961. alps_passthrough_mode_v2(psmouse, false);
  962. if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
  963. return -1;
  964. if ((psmouse->badbyte & 0xc8) == 0x08) {
  965. /*
  966. * Poll the track stick ...
  967. */
  968. if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
  969. return -1;
  970. }
  971. memcpy(psmouse->packet, buf, sizeof(buf));
  972. return 0;
  973. }
  974. static int alps_hw_init_v1_v2(struct psmouse *psmouse)
  975. {
  976. struct alps_data *priv = psmouse->private;
  977. if ((priv->flags & ALPS_PASS) &&
  978. alps_passthrough_mode_v2(psmouse, true)) {
  979. return -1;
  980. }
  981. if (alps_tap_mode(psmouse, true)) {
  982. psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
  983. return -1;
  984. }
  985. if (alps_absolute_mode_v1_v2(psmouse)) {
  986. psmouse_err(psmouse, "Failed to enable absolute mode\n");
  987. return -1;
  988. }
  989. if ((priv->flags & ALPS_PASS) &&
  990. alps_passthrough_mode_v2(psmouse, false)) {
  991. return -1;
  992. }
  993. /* ALPS needs stream mode, otherwise it won't report any data */
  994. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
  995. psmouse_err(psmouse, "Failed to enable stream mode\n");
  996. return -1;
  997. }
  998. return 0;
  999. }
  1000. /*
  1001. * Enable or disable passthrough mode to the trackstick.
  1002. */
  1003. static int alps_passthrough_mode_v3(struct psmouse *psmouse,
  1004. int reg_base, bool enable)
  1005. {
  1006. int reg_val, ret = -1;
  1007. if (alps_enter_command_mode(psmouse))
  1008. return -1;
  1009. reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
  1010. if (reg_val == -1)
  1011. goto error;
  1012. if (enable)
  1013. reg_val |= 0x01;
  1014. else
  1015. reg_val &= ~0x01;
  1016. ret = __alps_command_mode_write_reg(psmouse, reg_val);
  1017. error:
  1018. if (alps_exit_command_mode(psmouse))
  1019. ret = -1;
  1020. return ret;
  1021. }
  1022. /* Must be in command mode when calling this function */
  1023. static int alps_absolute_mode_v3(struct psmouse *psmouse)
  1024. {
  1025. int reg_val;
  1026. reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
  1027. if (reg_val == -1)
  1028. return -1;
  1029. reg_val |= 0x06;
  1030. if (__alps_command_mode_write_reg(psmouse, reg_val))
  1031. return -1;
  1032. return 0;
  1033. }
  1034. static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
  1035. {
  1036. int ret = -EIO, reg_val;
  1037. if (alps_enter_command_mode(psmouse))
  1038. goto error;
  1039. reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
  1040. if (reg_val == -1)
  1041. goto error;
  1042. /* bit 7: trackstick is present */
  1043. ret = reg_val & 0x80 ? 0 : -ENODEV;
  1044. error:
  1045. alps_exit_command_mode(psmouse);
  1046. return ret;
  1047. }
  1048. static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
  1049. {
  1050. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1051. int ret = 0;
  1052. unsigned char param[4];
  1053. if (alps_passthrough_mode_v3(psmouse, reg_base, true))
  1054. return -EIO;
  1055. /*
  1056. * E7 report for the trackstick
  1057. *
  1058. * There have been reports of failures to seem to trace back
  1059. * to the above trackstick check failing. When these occur
  1060. * this E7 report fails, so when that happens we continue
  1061. * with the assumption that there isn't a trackstick after
  1062. * all.
  1063. */
  1064. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
  1065. psmouse_warn(psmouse, "trackstick E7 report failed\n");
  1066. ret = -ENODEV;
  1067. } else {
  1068. psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
  1069. /*
  1070. * Not sure what this does, but it is absolutely
  1071. * essential. Without it, the touchpad does not
  1072. * work at all and the trackstick just emits normal
  1073. * PS/2 packets.
  1074. */
  1075. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1076. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1077. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1078. alps_command_mode_send_nibble(psmouse, 0x9) ||
  1079. alps_command_mode_send_nibble(psmouse, 0x4)) {
  1080. psmouse_err(psmouse,
  1081. "Error sending magic E6 sequence\n");
  1082. ret = -EIO;
  1083. goto error;
  1084. }
  1085. /*
  1086. * This ensures the trackstick packets are in the format
  1087. * supported by this driver. If bit 1 isn't set the packet
  1088. * format is different.
  1089. */
  1090. if (alps_enter_command_mode(psmouse) ||
  1091. alps_command_mode_write_reg(psmouse,
  1092. reg_base + 0x08, 0x82) ||
  1093. alps_exit_command_mode(psmouse))
  1094. ret = -EIO;
  1095. }
  1096. error:
  1097. if (alps_passthrough_mode_v3(psmouse, reg_base, false))
  1098. ret = -EIO;
  1099. return ret;
  1100. }
  1101. static int alps_hw_init_v3(struct psmouse *psmouse)
  1102. {
  1103. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1104. int reg_val;
  1105. unsigned char param[4];
  1106. reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
  1107. if (reg_val == -EIO)
  1108. goto error;
  1109. if (reg_val == 0 &&
  1110. alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
  1111. goto error;
  1112. if (alps_enter_command_mode(psmouse) ||
  1113. alps_absolute_mode_v3(psmouse)) {
  1114. psmouse_err(psmouse, "Failed to enter absolute mode\n");
  1115. goto error;
  1116. }
  1117. reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
  1118. if (reg_val == -1)
  1119. goto error;
  1120. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
  1121. goto error;
  1122. reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
  1123. if (reg_val == -1)
  1124. goto error;
  1125. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
  1126. goto error;
  1127. if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
  1128. goto error;
  1129. if (__alps_command_mode_write_reg(psmouse, 0x04))
  1130. goto error;
  1131. if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
  1132. goto error;
  1133. if (__alps_command_mode_write_reg(psmouse, 0x03))
  1134. goto error;
  1135. if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
  1136. goto error;
  1137. if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
  1138. goto error;
  1139. if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
  1140. goto error;
  1141. if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
  1142. goto error;
  1143. alps_exit_command_mode(psmouse);
  1144. /* Set rate and enable data reporting */
  1145. param[0] = 0x64;
  1146. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
  1147. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  1148. psmouse_err(psmouse, "Failed to enable data reporting\n");
  1149. return -1;
  1150. }
  1151. return 0;
  1152. error:
  1153. /*
  1154. * Leaving the touchpad in command mode will essentially render
  1155. * it unusable until the machine reboots, so exit it here just
  1156. * to be safe
  1157. */
  1158. alps_exit_command_mode(psmouse);
  1159. return -1;
  1160. }
  1161. static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
  1162. {
  1163. struct alps_data *priv = psmouse->private;
  1164. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1165. int reg_val, ret = -1;
  1166. if (priv->flags & ALPS_DUALPOINT) {
  1167. reg_val = alps_setup_trackstick_v3(psmouse,
  1168. ALPS_REG_BASE_RUSHMORE);
  1169. if (reg_val == -EIO)
  1170. goto error;
  1171. if (reg_val == -ENODEV)
  1172. priv->flags &= ~ALPS_DUALPOINT;
  1173. }
  1174. if (alps_enter_command_mode(psmouse) ||
  1175. alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
  1176. alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
  1177. goto error;
  1178. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
  1179. if (reg_val == -1)
  1180. goto error;
  1181. if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
  1182. goto error;
  1183. if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
  1184. goto error;
  1185. /* enter absolute mode */
  1186. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
  1187. if (reg_val == -1)
  1188. goto error;
  1189. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
  1190. goto error;
  1191. alps_exit_command_mode(psmouse);
  1192. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  1193. error:
  1194. alps_exit_command_mode(psmouse);
  1195. return ret;
  1196. }
  1197. /* Must be in command mode when calling this function */
  1198. static int alps_absolute_mode_v4(struct psmouse *psmouse)
  1199. {
  1200. int reg_val;
  1201. reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
  1202. if (reg_val == -1)
  1203. return -1;
  1204. reg_val |= 0x02;
  1205. if (__alps_command_mode_write_reg(psmouse, reg_val))
  1206. return -1;
  1207. return 0;
  1208. }
  1209. static int alps_hw_init_v4(struct psmouse *psmouse)
  1210. {
  1211. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1212. unsigned char param[4];
  1213. if (alps_enter_command_mode(psmouse))
  1214. goto error;
  1215. if (alps_absolute_mode_v4(psmouse)) {
  1216. psmouse_err(psmouse, "Failed to enter absolute mode\n");
  1217. goto error;
  1218. }
  1219. if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
  1220. goto error;
  1221. if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
  1222. goto error;
  1223. if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
  1224. goto error;
  1225. if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
  1226. goto error;
  1227. if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
  1228. goto error;
  1229. if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
  1230. goto error;
  1231. if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
  1232. goto error;
  1233. if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
  1234. goto error;
  1235. alps_exit_command_mode(psmouse);
  1236. /*
  1237. * This sequence changes the output from a 9-byte to an
  1238. * 8-byte format. All the same data seems to be present,
  1239. * just in a more compact format.
  1240. */
  1241. param[0] = 0xc8;
  1242. param[1] = 0x64;
  1243. param[2] = 0x50;
  1244. if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1245. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
  1246. ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
  1247. ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
  1248. return -1;
  1249. /* Set rate and enable data reporting */
  1250. param[0] = 0x64;
  1251. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
  1252. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  1253. psmouse_err(psmouse, "Failed to enable data reporting\n");
  1254. return -1;
  1255. }
  1256. return 0;
  1257. error:
  1258. /*
  1259. * Leaving the touchpad in command mode will essentially render
  1260. * it unusable until the machine reboots, so exit it here just
  1261. * to be safe
  1262. */
  1263. alps_exit_command_mode(psmouse);
  1264. return -1;
  1265. }
  1266. static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
  1267. {
  1268. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1269. unsigned char param[2];
  1270. /* This is dolphin "v1" as empirically defined by florin9doi */
  1271. param[0] = 0x64;
  1272. param[1] = 0x28;
  1273. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  1274. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1275. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
  1276. return -1;
  1277. return 0;
  1278. }
  1279. static void alps_set_defaults(struct alps_data *priv)
  1280. {
  1281. priv->byte0 = 0x8f;
  1282. priv->mask0 = 0x8f;
  1283. priv->flags = ALPS_DUALPOINT;
  1284. priv->x_max = 2000;
  1285. priv->y_max = 1400;
  1286. priv->x_bits = 15;
  1287. priv->y_bits = 11;
  1288. switch (priv->proto_version) {
  1289. case ALPS_PROTO_V1:
  1290. case ALPS_PROTO_V2:
  1291. priv->hw_init = alps_hw_init_v1_v2;
  1292. priv->process_packet = alps_process_packet_v1_v2;
  1293. priv->set_abs_params = alps_set_abs_params_st;
  1294. break;
  1295. case ALPS_PROTO_V3:
  1296. priv->hw_init = alps_hw_init_v3;
  1297. priv->process_packet = alps_process_packet_v3;
  1298. priv->set_abs_params = alps_set_abs_params_mt;
  1299. priv->decode_fields = alps_decode_pinnacle;
  1300. priv->nibble_commands = alps_v3_nibble_commands;
  1301. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  1302. break;
  1303. case ALPS_PROTO_V4:
  1304. priv->hw_init = alps_hw_init_v4;
  1305. priv->process_packet = alps_process_packet_v4;
  1306. priv->set_abs_params = alps_set_abs_params_mt;
  1307. priv->nibble_commands = alps_v4_nibble_commands;
  1308. priv->addr_command = PSMOUSE_CMD_DISABLE;
  1309. break;
  1310. case ALPS_PROTO_V5:
  1311. priv->hw_init = alps_hw_init_dolphin_v1;
  1312. priv->process_packet = alps_process_packet_v3;
  1313. priv->decode_fields = alps_decode_dolphin;
  1314. priv->set_abs_params = alps_set_abs_params_mt;
  1315. priv->nibble_commands = alps_v3_nibble_commands;
  1316. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  1317. priv->byte0 = 0xc8;
  1318. priv->mask0 = 0xc8;
  1319. priv->flags = 0;
  1320. priv->x_max = 1360;
  1321. priv->y_max = 660;
  1322. priv->x_bits = 23;
  1323. priv->y_bits = 12;
  1324. break;
  1325. }
  1326. }
  1327. static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
  1328. unsigned char *e7, unsigned char *ec)
  1329. {
  1330. const struct alps_model_info *model;
  1331. int i;
  1332. for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
  1333. model = &alps_model_data[i];
  1334. if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
  1335. (!model->command_mode_resp ||
  1336. model->command_mode_resp == ec[2])) {
  1337. priv->proto_version = model->proto_version;
  1338. alps_set_defaults(priv);
  1339. priv->flags = model->flags;
  1340. priv->byte0 = model->byte0;
  1341. priv->mask0 = model->mask0;
  1342. return 0;
  1343. }
  1344. }
  1345. return -EINVAL;
  1346. }
  1347. static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
  1348. {
  1349. unsigned char e6[4], e7[4], ec[4];
  1350. /*
  1351. * First try "E6 report".
  1352. * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
  1353. * The bits 0-2 of the first byte will be 1s if some buttons are
  1354. * pressed.
  1355. */
  1356. if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  1357. PSMOUSE_CMD_SETSCALE11, e6))
  1358. return -EIO;
  1359. if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
  1360. return -EINVAL;
  1361. /*
  1362. * Now get the "E7" and "EC" reports. These will uniquely identify
  1363. * most ALPS touchpads.
  1364. */
  1365. if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  1366. PSMOUSE_CMD_SETSCALE21, e7) ||
  1367. alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  1368. PSMOUSE_CMD_RESET_WRAP, ec) ||
  1369. alps_exit_command_mode(psmouse))
  1370. return -EIO;
  1371. if (alps_match_table(psmouse, priv, e7, ec) == 0) {
  1372. return 0;
  1373. } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
  1374. ec[0] == 0x73 && ec[1] == 0x01) {
  1375. priv->proto_version = ALPS_PROTO_V5;
  1376. alps_set_defaults(priv);
  1377. return 0;
  1378. } else if (ec[0] == 0x88 && ec[1] == 0x08) {
  1379. priv->proto_version = ALPS_PROTO_V3;
  1380. alps_set_defaults(priv);
  1381. priv->hw_init = alps_hw_init_rushmore_v3;
  1382. priv->decode_fields = alps_decode_rushmore;
  1383. priv->x_bits = 16;
  1384. priv->y_bits = 12;
  1385. /* hack to make addr_command, nibble_command available */
  1386. psmouse->private = priv;
  1387. if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
  1388. priv->flags &= ~ALPS_DUALPOINT;
  1389. return 0;
  1390. } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
  1391. ec[2] >= 0x90 && ec[2] <= 0x9d) {
  1392. priv->proto_version = ALPS_PROTO_V3;
  1393. alps_set_defaults(priv);
  1394. return 0;
  1395. }
  1396. psmouse_info(psmouse,
  1397. "Unknown ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
  1398. return -EINVAL;
  1399. }
  1400. static int alps_reconnect(struct psmouse *psmouse)
  1401. {
  1402. struct alps_data *priv = psmouse->private;
  1403. psmouse_reset(psmouse);
  1404. if (alps_identify(psmouse, priv) < 0)
  1405. return -1;
  1406. return priv->hw_init(psmouse);
  1407. }
  1408. static void alps_disconnect(struct psmouse *psmouse)
  1409. {
  1410. struct alps_data *priv = psmouse->private;
  1411. psmouse_reset(psmouse);
  1412. del_timer_sync(&priv->timer);
  1413. input_unregister_device(priv->dev2);
  1414. kfree(priv);
  1415. }
  1416. static void alps_set_abs_params_st(struct alps_data *priv,
  1417. struct input_dev *dev1)
  1418. {
  1419. input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
  1420. input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
  1421. }
  1422. static void alps_set_abs_params_mt(struct alps_data *priv,
  1423. struct input_dev *dev1)
  1424. {
  1425. set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
  1426. input_mt_init_slots(dev1, 2, 0);
  1427. input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
  1428. input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
  1429. set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
  1430. set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
  1431. set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
  1432. input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
  1433. input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
  1434. }
  1435. int alps_init(struct psmouse *psmouse)
  1436. {
  1437. struct alps_data *priv;
  1438. struct input_dev *dev1 = psmouse->dev, *dev2;
  1439. priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
  1440. dev2 = input_allocate_device();
  1441. if (!priv || !dev2)
  1442. goto init_fail;
  1443. priv->dev2 = dev2;
  1444. setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
  1445. psmouse->private = priv;
  1446. psmouse_reset(psmouse);
  1447. if (alps_identify(psmouse, priv) < 0)
  1448. goto init_fail;
  1449. if (priv->hw_init(psmouse))
  1450. goto init_fail;
  1451. /*
  1452. * Undo part of setup done for us by psmouse core since touchpad
  1453. * is not a relative device.
  1454. */
  1455. __clear_bit(EV_REL, dev1->evbit);
  1456. __clear_bit(REL_X, dev1->relbit);
  1457. __clear_bit(REL_Y, dev1->relbit);
  1458. /*
  1459. * Now set up our capabilities.
  1460. */
  1461. dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
  1462. dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
  1463. dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
  1464. dev1->keybit[BIT_WORD(BTN_LEFT)] |=
  1465. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
  1466. dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
  1467. priv->set_abs_params(priv, dev1);
  1468. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  1469. if (priv->flags & ALPS_WHEEL) {
  1470. dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
  1471. dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
  1472. }
  1473. if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  1474. dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
  1475. dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
  1476. }
  1477. if (priv->flags & ALPS_FOUR_BUTTONS) {
  1478. dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
  1479. dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
  1480. dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
  1481. dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
  1482. } else {
  1483. dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
  1484. }
  1485. snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
  1486. dev2->phys = priv->phys;
  1487. dev2->name = (priv->flags & ALPS_DUALPOINT) ?
  1488. "DualPoint Stick" : "PS/2 Mouse";
  1489. dev2->id.bustype = BUS_I8042;
  1490. dev2->id.vendor = 0x0002;
  1491. dev2->id.product = PSMOUSE_ALPS;
  1492. dev2->id.version = 0x0000;
  1493. dev2->dev.parent = &psmouse->ps2dev.serio->dev;
  1494. dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  1495. dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  1496. dev2->keybit[BIT_WORD(BTN_LEFT)] =
  1497. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
  1498. if (input_register_device(priv->dev2))
  1499. goto init_fail;
  1500. psmouse->protocol_handler = alps_process_byte;
  1501. psmouse->poll = alps_poll;
  1502. psmouse->disconnect = alps_disconnect;
  1503. psmouse->reconnect = alps_reconnect;
  1504. psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
  1505. /* We are having trouble resyncing ALPS touchpads so disable it for now */
  1506. psmouse->resync_time = 0;
  1507. return 0;
  1508. init_fail:
  1509. psmouse_reset(psmouse);
  1510. input_free_device(dev2);
  1511. kfree(priv);
  1512. psmouse->private = NULL;
  1513. return -1;
  1514. }
  1515. int alps_detect(struct psmouse *psmouse, bool set_properties)
  1516. {
  1517. struct alps_data dummy;
  1518. if (alps_identify(psmouse, &dummy) < 0)
  1519. return -1;
  1520. if (set_properties) {
  1521. psmouse->vendor = "ALPS";
  1522. psmouse->name = dummy.flags & ALPS_DUALPOINT ?
  1523. "DualPoint TouchPad" : "GlidePoint";
  1524. psmouse->model = dummy.proto_version << 8;
  1525. }
  1526. return 0;
  1527. }