alps.c 50 KB

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