alps.c 59 KB

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