alps.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  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_process_touchpad_packet_v3(struct psmouse *psmouse)
  425. {
  426. struct alps_data *priv = psmouse->private;
  427. unsigned char *packet = psmouse->packet;
  428. struct input_dev *dev = psmouse->dev;
  429. struct input_dev *dev2 = priv->dev2;
  430. int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  431. int fingers = 0, bmap_fingers;
  432. struct alps_fields f;
  433. priv->decode_fields(&f, packet);
  434. /*
  435. * There's no single feature of touchpad position and bitmap packets
  436. * that can be used to distinguish between them. We rely on the fact
  437. * that a bitmap packet should always follow a position packet with
  438. * bit 6 of packet[4] set.
  439. */
  440. if (priv->multi_packet) {
  441. /*
  442. * Sometimes a position packet will indicate a multi-packet
  443. * sequence, but then what follows is another position
  444. * packet. Check for this, and when it happens process the
  445. * position packet as usual.
  446. */
  447. if (f.is_mp) {
  448. fingers = f.fingers;
  449. bmap_fingers = alps_process_bitmap(priv,
  450. f.x_map, f.y_map,
  451. &x1, &y1, &x2, &y2);
  452. /*
  453. * We shouldn't report more than one finger if
  454. * we don't have two coordinates.
  455. */
  456. if (fingers > 1 && bmap_fingers < 2)
  457. fingers = bmap_fingers;
  458. /* Now process position packet */
  459. priv->decode_fields(&f, priv->multi_data);
  460. } else {
  461. priv->multi_packet = 0;
  462. }
  463. }
  464. /*
  465. * Bit 6 of byte 0 is not usually set in position packets. The only
  466. * times it seems to be set is in situations where the data is
  467. * suspect anyway, e.g. a palm resting flat on the touchpad. Given
  468. * this combined with the fact that this bit is useful for filtering
  469. * out misidentified bitmap packets, we reject anything with this
  470. * bit set.
  471. */
  472. if (f.is_mp)
  473. return;
  474. if (!priv->multi_packet && f.first_mp) {
  475. priv->multi_packet = 1;
  476. memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
  477. return;
  478. }
  479. priv->multi_packet = 0;
  480. /*
  481. * Sometimes the hardware sends a single packet with z = 0
  482. * in the middle of a stream. Real releases generate packets
  483. * with x, y, and z all zero, so these seem to be flukes.
  484. * Ignore them.
  485. */
  486. if (f.x && f.y && !f.z)
  487. return;
  488. /*
  489. * If we don't have MT data or the bitmaps were empty, we have
  490. * to rely on ST data.
  491. */
  492. if (!fingers) {
  493. x1 = f.x;
  494. y1 = f.y;
  495. fingers = f.z > 0 ? 1 : 0;
  496. }
  497. if (f.z >= 64)
  498. input_report_key(dev, BTN_TOUCH, 1);
  499. else
  500. input_report_key(dev, BTN_TOUCH, 0);
  501. alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
  502. input_mt_report_finger_count(dev, fingers);
  503. input_report_key(dev, BTN_LEFT, f.left);
  504. input_report_key(dev, BTN_RIGHT, f.right);
  505. input_report_key(dev, BTN_MIDDLE, f.middle);
  506. if (f.z > 0) {
  507. input_report_abs(dev, ABS_X, f.x);
  508. input_report_abs(dev, ABS_Y, f.y);
  509. }
  510. input_report_abs(dev, ABS_PRESSURE, f.z);
  511. input_sync(dev);
  512. if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
  513. input_report_key(dev2, BTN_LEFT, f.ts_left);
  514. input_report_key(dev2, BTN_RIGHT, f.ts_right);
  515. input_report_key(dev2, BTN_MIDDLE, f.ts_middle);
  516. input_sync(dev2);
  517. }
  518. }
  519. static void alps_process_packet_v3(struct psmouse *psmouse)
  520. {
  521. unsigned char *packet = psmouse->packet;
  522. /*
  523. * v3 protocol packets come in three types, two representing
  524. * touchpad data and one representing trackstick data.
  525. * Trackstick packets seem to be distinguished by always
  526. * having 0x3f in the last byte. This value has never been
  527. * observed in the last byte of either of the other types
  528. * of packets.
  529. */
  530. if (packet[5] == 0x3f) {
  531. alps_process_trackstick_packet_v3(psmouse);
  532. return;
  533. }
  534. alps_process_touchpad_packet_v3(psmouse);
  535. }
  536. static void alps_process_packet_v4(struct psmouse *psmouse)
  537. {
  538. struct alps_data *priv = psmouse->private;
  539. unsigned char *packet = psmouse->packet;
  540. struct input_dev *dev = psmouse->dev;
  541. int offset;
  542. int x, y, z;
  543. int left, right;
  544. int x1, y1, x2, y2;
  545. int fingers = 0;
  546. unsigned int x_bitmap, y_bitmap;
  547. /*
  548. * v4 has a 6-byte encoding for bitmap data, but this data is
  549. * broken up between 3 normal packets. Use priv->multi_packet to
  550. * track our position in the bitmap packet.
  551. */
  552. if (packet[6] & 0x40) {
  553. /* sync, reset position */
  554. priv->multi_packet = 0;
  555. }
  556. if (WARN_ON_ONCE(priv->multi_packet > 2))
  557. return;
  558. offset = 2 * priv->multi_packet;
  559. priv->multi_data[offset] = packet[6];
  560. priv->multi_data[offset + 1] = packet[7];
  561. if (++priv->multi_packet > 2) {
  562. priv->multi_packet = 0;
  563. x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
  564. ((priv->multi_data[3] & 0x60) << 3) |
  565. ((priv->multi_data[0] & 0x3f) << 2) |
  566. ((priv->multi_data[1] & 0x60) >> 5);
  567. y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
  568. ((priv->multi_data[3] & 0x1f) << 5) |
  569. (priv->multi_data[1] & 0x1f);
  570. fingers = alps_process_bitmap(priv, x_bitmap, y_bitmap,
  571. &x1, &y1, &x2, &y2);
  572. /* Store MT data.*/
  573. priv->fingers = fingers;
  574. priv->x1 = x1;
  575. priv->x2 = x2;
  576. priv->y1 = y1;
  577. priv->y2 = y2;
  578. }
  579. left = packet[4] & 0x01;
  580. right = packet[4] & 0x02;
  581. x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
  582. ((packet[0] & 0x30) >> 4);
  583. y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
  584. z = packet[5] & 0x7f;
  585. /*
  586. * If there were no contacts in the bitmap, use ST
  587. * points in MT reports.
  588. * If there were two contacts or more, report MT data.
  589. */
  590. if (priv->fingers < 2) {
  591. x1 = x;
  592. y1 = y;
  593. fingers = z > 0 ? 1 : 0;
  594. } else {
  595. fingers = priv->fingers;
  596. x1 = priv->x1;
  597. x2 = priv->x2;
  598. y1 = priv->y1;
  599. y2 = priv->y2;
  600. }
  601. if (z >= 64)
  602. input_report_key(dev, BTN_TOUCH, 1);
  603. else
  604. input_report_key(dev, BTN_TOUCH, 0);
  605. alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
  606. input_mt_report_finger_count(dev, fingers);
  607. input_report_key(dev, BTN_LEFT, left);
  608. input_report_key(dev, BTN_RIGHT, right);
  609. if (z > 0) {
  610. input_report_abs(dev, ABS_X, x);
  611. input_report_abs(dev, ABS_Y, y);
  612. }
  613. input_report_abs(dev, ABS_PRESSURE, z);
  614. input_sync(dev);
  615. }
  616. static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
  617. unsigned char packet[],
  618. bool report_buttons)
  619. {
  620. struct alps_data *priv = psmouse->private;
  621. struct input_dev *dev2 = priv->dev2;
  622. if (report_buttons)
  623. alps_report_buttons(psmouse, dev2, psmouse->dev,
  624. packet[0] & 1, packet[0] & 2, packet[0] & 4);
  625. input_report_rel(dev2, REL_X,
  626. packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
  627. input_report_rel(dev2, REL_Y,
  628. packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
  629. input_sync(dev2);
  630. }
  631. static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
  632. {
  633. struct alps_data *priv = psmouse->private;
  634. if (psmouse->pktcnt < 6)
  635. return PSMOUSE_GOOD_DATA;
  636. if (psmouse->pktcnt == 6) {
  637. /*
  638. * Start a timer to flush the packet if it ends up last
  639. * 6-byte packet in the stream. Timer needs to fire
  640. * psmouse core times out itself. 20 ms should be enough
  641. * to decide if we are getting more data or not.
  642. */
  643. mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
  644. return PSMOUSE_GOOD_DATA;
  645. }
  646. del_timer(&priv->timer);
  647. if (psmouse->packet[6] & 0x80) {
  648. /*
  649. * Highest bit is set - that means we either had
  650. * complete ALPS packet and this is start of the
  651. * next packet or we got garbage.
  652. */
  653. if (((psmouse->packet[3] |
  654. psmouse->packet[4] |
  655. psmouse->packet[5]) & 0x80) ||
  656. (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
  657. psmouse_dbg(psmouse,
  658. "refusing packet %4ph (suspected interleaved ps/2)\n",
  659. psmouse->packet + 3);
  660. return PSMOUSE_BAD_DATA;
  661. }
  662. priv->process_packet(psmouse);
  663. /* Continue with the next packet */
  664. psmouse->packet[0] = psmouse->packet[6];
  665. psmouse->pktcnt = 1;
  666. } else {
  667. /*
  668. * High bit is 0 - that means that we indeed got a PS/2
  669. * packet in the middle of ALPS packet.
  670. *
  671. * There is also possibility that we got 6-byte ALPS
  672. * packet followed by 3-byte packet from trackpoint. We
  673. * can not distinguish between these 2 scenarios but
  674. * because the latter is unlikely to happen in course of
  675. * normal operation (user would need to press all
  676. * buttons on the pad and start moving trackpoint
  677. * without touching the pad surface) we assume former.
  678. * Even if we are wrong the wost thing that would happen
  679. * the cursor would jump but we should not get protocol
  680. * de-synchronization.
  681. */
  682. alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
  683. false);
  684. /*
  685. * Continue with the standard ALPS protocol handling,
  686. * but make sure we won't process it as an interleaved
  687. * packet again, which may happen if all buttons are
  688. * pressed. To avoid this let's reset the 4th bit which
  689. * is normally 1.
  690. */
  691. psmouse->packet[3] = psmouse->packet[6] & 0xf7;
  692. psmouse->pktcnt = 4;
  693. }
  694. return PSMOUSE_GOOD_DATA;
  695. }
  696. static void alps_flush_packet(unsigned long data)
  697. {
  698. struct psmouse *psmouse = (struct psmouse *)data;
  699. struct alps_data *priv = psmouse->private;
  700. serio_pause_rx(psmouse->ps2dev.serio);
  701. if (psmouse->pktcnt == psmouse->pktsize) {
  702. /*
  703. * We did not any more data in reasonable amount of time.
  704. * Validate the last 3 bytes and process as a standard
  705. * ALPS packet.
  706. */
  707. if ((psmouse->packet[3] |
  708. psmouse->packet[4] |
  709. psmouse->packet[5]) & 0x80) {
  710. psmouse_dbg(psmouse,
  711. "refusing packet %3ph (suspected interleaved ps/2)\n",
  712. psmouse->packet + 3);
  713. } else {
  714. priv->process_packet(psmouse);
  715. }
  716. psmouse->pktcnt = 0;
  717. }
  718. serio_continue_rx(psmouse->ps2dev.serio);
  719. }
  720. static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
  721. {
  722. struct alps_data *priv = psmouse->private;
  723. if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
  724. if (psmouse->pktcnt == 3) {
  725. alps_report_bare_ps2_packet(psmouse, psmouse->packet,
  726. true);
  727. return PSMOUSE_FULL_PACKET;
  728. }
  729. return PSMOUSE_GOOD_DATA;
  730. }
  731. /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
  732. if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
  733. psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
  734. return alps_handle_interleaved_ps2(psmouse);
  735. }
  736. if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
  737. psmouse_dbg(psmouse,
  738. "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
  739. psmouse->packet[0], priv->mask0, priv->byte0);
  740. return PSMOUSE_BAD_DATA;
  741. }
  742. /* Bytes 2 - pktsize should have 0 in the highest bit */
  743. if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
  744. (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
  745. psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
  746. psmouse->pktcnt - 1,
  747. psmouse->packet[psmouse->pktcnt - 1]);
  748. return PSMOUSE_BAD_DATA;
  749. }
  750. if (psmouse->pktcnt == psmouse->pktsize) {
  751. priv->process_packet(psmouse);
  752. return PSMOUSE_FULL_PACKET;
  753. }
  754. return PSMOUSE_GOOD_DATA;
  755. }
  756. static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
  757. {
  758. struct ps2dev *ps2dev = &psmouse->ps2dev;
  759. struct alps_data *priv = psmouse->private;
  760. int command;
  761. unsigned char *param;
  762. unsigned char dummy[4];
  763. BUG_ON(nibble > 0xf);
  764. command = priv->nibble_commands[nibble].command;
  765. param = (command & 0x0f00) ?
  766. dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
  767. if (ps2_command(ps2dev, param, command))
  768. return -1;
  769. return 0;
  770. }
  771. static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
  772. {
  773. struct ps2dev *ps2dev = &psmouse->ps2dev;
  774. struct alps_data *priv = psmouse->private;
  775. int i, nibble;
  776. if (ps2_command(ps2dev, NULL, priv->addr_command))
  777. return -1;
  778. for (i = 12; i >= 0; i -= 4) {
  779. nibble = (addr >> i) & 0xf;
  780. if (alps_command_mode_send_nibble(psmouse, nibble))
  781. return -1;
  782. }
  783. return 0;
  784. }
  785. static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
  786. {
  787. struct ps2dev *ps2dev = &psmouse->ps2dev;
  788. unsigned char param[4];
  789. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  790. return -1;
  791. /*
  792. * The address being read is returned in the first two bytes
  793. * of the result. Check that this address matches the expected
  794. * address.
  795. */
  796. if (addr != ((param[0] << 8) | param[1]))
  797. return -1;
  798. return param[2];
  799. }
  800. static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
  801. {
  802. if (alps_command_mode_set_addr(psmouse, addr))
  803. return -1;
  804. return __alps_command_mode_read_reg(psmouse, addr);
  805. }
  806. static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
  807. {
  808. if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
  809. return -1;
  810. if (alps_command_mode_send_nibble(psmouse, value & 0xf))
  811. return -1;
  812. return 0;
  813. }
  814. static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
  815. u8 value)
  816. {
  817. if (alps_command_mode_set_addr(psmouse, addr))
  818. return -1;
  819. return __alps_command_mode_write_reg(psmouse, value);
  820. }
  821. static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
  822. int repeated_command, unsigned char *param)
  823. {
  824. struct ps2dev *ps2dev = &psmouse->ps2dev;
  825. param[0] = 0;
  826. if (init_command && ps2_command(ps2dev, param, init_command))
  827. return -EIO;
  828. if (ps2_command(ps2dev, NULL, repeated_command) ||
  829. ps2_command(ps2dev, NULL, repeated_command) ||
  830. ps2_command(ps2dev, NULL, repeated_command))
  831. return -EIO;
  832. param[0] = param[1] = param[2] = 0xff;
  833. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  834. return -EIO;
  835. psmouse_dbg(psmouse, "%2.2X report: %2.2x %2.2x %2.2x\n",
  836. repeated_command, param[0], param[1], param[2]);
  837. return 0;
  838. }
  839. static int alps_enter_command_mode(struct psmouse *psmouse)
  840. {
  841. unsigned char param[4];
  842. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
  843. psmouse_err(psmouse, "failed to enter command mode\n");
  844. return -1;
  845. }
  846. if (param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) {
  847. psmouse_dbg(psmouse,
  848. "unknown response while entering command mode\n");
  849. return -1;
  850. }
  851. return 0;
  852. }
  853. static inline int alps_exit_command_mode(struct psmouse *psmouse)
  854. {
  855. struct ps2dev *ps2dev = &psmouse->ps2dev;
  856. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
  857. return -1;
  858. return 0;
  859. }
  860. /*
  861. * For DualPoint devices select the device that should respond to
  862. * subsequent commands. It looks like glidepad is behind stickpointer,
  863. * I'd thought it would be other way around...
  864. */
  865. static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
  866. {
  867. struct ps2dev *ps2dev = &psmouse->ps2dev;
  868. int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
  869. if (ps2_command(ps2dev, NULL, cmd) ||
  870. ps2_command(ps2dev, NULL, cmd) ||
  871. ps2_command(ps2dev, NULL, cmd) ||
  872. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  873. return -1;
  874. /* we may get 3 more bytes, just ignore them */
  875. ps2_drain(ps2dev, 3, 100);
  876. return 0;
  877. }
  878. static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
  879. {
  880. struct ps2dev *ps2dev = &psmouse->ps2dev;
  881. /* Try ALPS magic knock - 4 disable before enable */
  882. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  883. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  884. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  885. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  886. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
  887. return -1;
  888. /*
  889. * Switch mouse to poll (remote) mode so motion data will not
  890. * get in our way
  891. */
  892. return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
  893. }
  894. static int alps_get_status(struct psmouse *psmouse, char *param)
  895. {
  896. /* Get status: 0xF5 0xF5 0xF5 0xE9 */
  897. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
  898. return -1;
  899. return 0;
  900. }
  901. /*
  902. * Turn touchpad tapping on or off. The sequences are:
  903. * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
  904. * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
  905. * My guess that 0xE9 (GetInfo) is here as a sync point.
  906. * For models that also have stickpointer (DualPoints) its tapping
  907. * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
  908. * we don't fiddle with it.
  909. */
  910. static int alps_tap_mode(struct psmouse *psmouse, int enable)
  911. {
  912. struct ps2dev *ps2dev = &psmouse->ps2dev;
  913. int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
  914. unsigned char tap_arg = enable ? 0x0A : 0x00;
  915. unsigned char param[4];
  916. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
  917. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  918. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  919. ps2_command(ps2dev, &tap_arg, cmd))
  920. return -1;
  921. if (alps_get_status(psmouse, param))
  922. return -1;
  923. return 0;
  924. }
  925. /*
  926. * alps_poll() - poll the touchpad for current motion packet.
  927. * Used in resync.
  928. */
  929. static int alps_poll(struct psmouse *psmouse)
  930. {
  931. struct alps_data *priv = psmouse->private;
  932. unsigned char buf[sizeof(psmouse->packet)];
  933. bool poll_failed;
  934. if (priv->flags & ALPS_PASS)
  935. alps_passthrough_mode_v2(psmouse, true);
  936. poll_failed = ps2_command(&psmouse->ps2dev, buf,
  937. PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
  938. if (priv->flags & ALPS_PASS)
  939. alps_passthrough_mode_v2(psmouse, false);
  940. if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
  941. return -1;
  942. if ((psmouse->badbyte & 0xc8) == 0x08) {
  943. /*
  944. * Poll the track stick ...
  945. */
  946. if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
  947. return -1;
  948. }
  949. memcpy(psmouse->packet, buf, sizeof(buf));
  950. return 0;
  951. }
  952. static int alps_hw_init_v1_v2(struct psmouse *psmouse)
  953. {
  954. struct alps_data *priv = psmouse->private;
  955. if ((priv->flags & ALPS_PASS) &&
  956. alps_passthrough_mode_v2(psmouse, true)) {
  957. return -1;
  958. }
  959. if (alps_tap_mode(psmouse, true)) {
  960. psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
  961. return -1;
  962. }
  963. if (alps_absolute_mode_v1_v2(psmouse)) {
  964. psmouse_err(psmouse, "Failed to enable absolute mode\n");
  965. return -1;
  966. }
  967. if ((priv->flags & ALPS_PASS) &&
  968. alps_passthrough_mode_v2(psmouse, false)) {
  969. return -1;
  970. }
  971. /* ALPS needs stream mode, otherwise it won't report any data */
  972. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
  973. psmouse_err(psmouse, "Failed to enable stream mode\n");
  974. return -1;
  975. }
  976. return 0;
  977. }
  978. /*
  979. * Enable or disable passthrough mode to the trackstick.
  980. */
  981. static int alps_passthrough_mode_v3(struct psmouse *psmouse,
  982. int reg_base, bool enable)
  983. {
  984. int reg_val, ret = -1;
  985. if (alps_enter_command_mode(psmouse))
  986. return -1;
  987. reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
  988. if (reg_val == -1)
  989. goto error;
  990. if (enable)
  991. reg_val |= 0x01;
  992. else
  993. reg_val &= ~0x01;
  994. ret = __alps_command_mode_write_reg(psmouse, reg_val);
  995. error:
  996. if (alps_exit_command_mode(psmouse))
  997. ret = -1;
  998. return ret;
  999. }
  1000. /* Must be in command mode when calling this function */
  1001. static int alps_absolute_mode_v3(struct psmouse *psmouse)
  1002. {
  1003. int reg_val;
  1004. reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
  1005. if (reg_val == -1)
  1006. return -1;
  1007. reg_val |= 0x06;
  1008. if (__alps_command_mode_write_reg(psmouse, reg_val))
  1009. return -1;
  1010. return 0;
  1011. }
  1012. static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
  1013. {
  1014. int ret = -EIO, reg_val;
  1015. if (alps_enter_command_mode(psmouse))
  1016. goto error;
  1017. reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
  1018. if (reg_val == -1)
  1019. goto error;
  1020. /* bit 7: trackstick is present */
  1021. ret = reg_val & 0x80 ? 0 : -ENODEV;
  1022. error:
  1023. alps_exit_command_mode(psmouse);
  1024. return ret;
  1025. }
  1026. static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
  1027. {
  1028. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1029. int ret = 0;
  1030. unsigned char param[4];
  1031. if (alps_passthrough_mode_v3(psmouse, reg_base, true))
  1032. return -EIO;
  1033. /*
  1034. * E7 report for the trackstick
  1035. *
  1036. * There have been reports of failures to seem to trace back
  1037. * to the above trackstick check failing. When these occur
  1038. * this E7 report fails, so when that happens we continue
  1039. * with the assumption that there isn't a trackstick after
  1040. * all.
  1041. */
  1042. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
  1043. psmouse_warn(psmouse, "trackstick E7 report failed\n");
  1044. ret = -ENODEV;
  1045. } else {
  1046. psmouse_dbg(psmouse,
  1047. "trackstick E7 report: %2.2x %2.2x %2.2x\n",
  1048. param[0], param[1], param[2]);
  1049. /*
  1050. * Not sure what this does, but it is absolutely
  1051. * essential. Without it, the touchpad does not
  1052. * work at all and the trackstick just emits normal
  1053. * PS/2 packets.
  1054. */
  1055. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1056. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1057. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1058. alps_command_mode_send_nibble(psmouse, 0x9) ||
  1059. alps_command_mode_send_nibble(psmouse, 0x4)) {
  1060. psmouse_err(psmouse,
  1061. "Error sending magic E6 sequence\n");
  1062. ret = -EIO;
  1063. goto error;
  1064. }
  1065. /*
  1066. * This ensures the trackstick packets are in the format
  1067. * supported by this driver. If bit 1 isn't set the packet
  1068. * format is different.
  1069. */
  1070. if (alps_enter_command_mode(psmouse) ||
  1071. alps_command_mode_write_reg(psmouse,
  1072. reg_base + 0x08, 0x82) ||
  1073. alps_exit_command_mode(psmouse))
  1074. ret = -EIO;
  1075. }
  1076. error:
  1077. if (alps_passthrough_mode_v3(psmouse, reg_base, false))
  1078. ret = -EIO;
  1079. return ret;
  1080. }
  1081. static int alps_hw_init_v3(struct psmouse *psmouse)
  1082. {
  1083. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1084. int reg_val;
  1085. unsigned char param[4];
  1086. reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
  1087. if (reg_val == -EIO)
  1088. goto error;
  1089. if (reg_val == 0 &&
  1090. alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
  1091. goto error;
  1092. if (alps_enter_command_mode(psmouse) ||
  1093. alps_absolute_mode_v3(psmouse)) {
  1094. psmouse_err(psmouse, "Failed to enter absolute mode\n");
  1095. goto error;
  1096. }
  1097. reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
  1098. if (reg_val == -1)
  1099. goto error;
  1100. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
  1101. goto error;
  1102. reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
  1103. if (reg_val == -1)
  1104. goto error;
  1105. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
  1106. goto error;
  1107. if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
  1108. goto error;
  1109. if (__alps_command_mode_write_reg(psmouse, 0x04))
  1110. goto error;
  1111. if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
  1112. goto error;
  1113. if (__alps_command_mode_write_reg(psmouse, 0x03))
  1114. goto error;
  1115. if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
  1116. goto error;
  1117. if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
  1118. goto error;
  1119. if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
  1120. goto error;
  1121. if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
  1122. goto error;
  1123. alps_exit_command_mode(psmouse);
  1124. /* Set rate and enable data reporting */
  1125. param[0] = 0x64;
  1126. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
  1127. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  1128. psmouse_err(psmouse, "Failed to enable data reporting\n");
  1129. return -1;
  1130. }
  1131. return 0;
  1132. error:
  1133. /*
  1134. * Leaving the touchpad in command mode will essentially render
  1135. * it unusable until the machine reboots, so exit it here just
  1136. * to be safe
  1137. */
  1138. alps_exit_command_mode(psmouse);
  1139. return -1;
  1140. }
  1141. static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
  1142. {
  1143. struct alps_data *priv = psmouse->private;
  1144. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1145. int reg_val, ret = -1;
  1146. if (priv->flags & ALPS_DUALPOINT) {
  1147. reg_val = alps_setup_trackstick_v3(psmouse,
  1148. ALPS_REG_BASE_RUSHMORE);
  1149. if (reg_val == -EIO)
  1150. goto error;
  1151. if (reg_val == -ENODEV)
  1152. priv->flags &= ~ALPS_DUALPOINT;
  1153. }
  1154. if (alps_enter_command_mode(psmouse) ||
  1155. alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
  1156. alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
  1157. goto error;
  1158. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
  1159. if (reg_val == -1)
  1160. goto error;
  1161. if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
  1162. goto error;
  1163. if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
  1164. goto error;
  1165. /* enter absolute mode */
  1166. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
  1167. if (reg_val == -1)
  1168. goto error;
  1169. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
  1170. goto error;
  1171. alps_exit_command_mode(psmouse);
  1172. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  1173. error:
  1174. alps_exit_command_mode(psmouse);
  1175. return ret;
  1176. }
  1177. /* Must be in command mode when calling this function */
  1178. static int alps_absolute_mode_v4(struct psmouse *psmouse)
  1179. {
  1180. int reg_val;
  1181. reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
  1182. if (reg_val == -1)
  1183. return -1;
  1184. reg_val |= 0x02;
  1185. if (__alps_command_mode_write_reg(psmouse, reg_val))
  1186. return -1;
  1187. return 0;
  1188. }
  1189. static int alps_hw_init_v4(struct psmouse *psmouse)
  1190. {
  1191. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1192. unsigned char param[4];
  1193. if (alps_enter_command_mode(psmouse))
  1194. goto error;
  1195. if (alps_absolute_mode_v4(psmouse)) {
  1196. psmouse_err(psmouse, "Failed to enter absolute mode\n");
  1197. goto error;
  1198. }
  1199. if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
  1200. goto error;
  1201. if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
  1202. goto error;
  1203. if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
  1204. goto error;
  1205. if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
  1206. goto error;
  1207. if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
  1208. goto error;
  1209. if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
  1210. goto error;
  1211. if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
  1212. goto error;
  1213. if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
  1214. goto error;
  1215. alps_exit_command_mode(psmouse);
  1216. /*
  1217. * This sequence changes the output from a 9-byte to an
  1218. * 8-byte format. All the same data seems to be present,
  1219. * just in a more compact format.
  1220. */
  1221. param[0] = 0xc8;
  1222. param[1] = 0x64;
  1223. param[2] = 0x50;
  1224. if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1225. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
  1226. ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
  1227. ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
  1228. return -1;
  1229. /* Set rate and enable data reporting */
  1230. param[0] = 0x64;
  1231. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
  1232. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  1233. psmouse_err(psmouse, "Failed to enable data reporting\n");
  1234. return -1;
  1235. }
  1236. return 0;
  1237. error:
  1238. /*
  1239. * Leaving the touchpad in command mode will essentially render
  1240. * it unusable until the machine reboots, so exit it here just
  1241. * to be safe
  1242. */
  1243. alps_exit_command_mode(psmouse);
  1244. return -1;
  1245. }
  1246. static void alps_set_defaults(struct alps_data *priv)
  1247. {
  1248. priv->byte0 = 0x8f;
  1249. priv->mask0 = 0x8f;
  1250. priv->flags = ALPS_DUALPOINT;
  1251. priv->x_max = 2000;
  1252. priv->y_max = 1400;
  1253. priv->x_bits = 15;
  1254. priv->y_bits = 11;
  1255. switch (priv->proto_version) {
  1256. case ALPS_PROTO_V1:
  1257. case ALPS_PROTO_V2:
  1258. priv->hw_init = alps_hw_init_v1_v2;
  1259. priv->process_packet = alps_process_packet_v1_v2;
  1260. priv->set_abs_params = alps_set_abs_params_st;
  1261. break;
  1262. case ALPS_PROTO_V3:
  1263. priv->hw_init = alps_hw_init_v3;
  1264. priv->process_packet = alps_process_packet_v3;
  1265. priv->set_abs_params = alps_set_abs_params_mt;
  1266. priv->decode_fields = alps_decode_pinnacle;
  1267. priv->nibble_commands = alps_v3_nibble_commands;
  1268. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  1269. break;
  1270. case ALPS_PROTO_V4:
  1271. priv->hw_init = alps_hw_init_v4;
  1272. priv->process_packet = alps_process_packet_v4;
  1273. priv->set_abs_params = alps_set_abs_params_mt;
  1274. priv->nibble_commands = alps_v4_nibble_commands;
  1275. priv->addr_command = PSMOUSE_CMD_DISABLE;
  1276. break;
  1277. }
  1278. }
  1279. static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
  1280. unsigned char *e7, unsigned char *ec)
  1281. {
  1282. const struct alps_model_info *model;
  1283. int i;
  1284. for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
  1285. model = &alps_model_data[i];
  1286. if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
  1287. (!model->command_mode_resp ||
  1288. model->command_mode_resp == ec[2])) {
  1289. priv->proto_version = model->proto_version;
  1290. alps_set_defaults(priv);
  1291. priv->flags = model->flags;
  1292. priv->byte0 = model->byte0;
  1293. priv->mask0 = model->mask0;
  1294. return 0;
  1295. }
  1296. }
  1297. return -EINVAL;
  1298. }
  1299. static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
  1300. {
  1301. unsigned char e6[4], e7[4], ec[4];
  1302. /*
  1303. * First try "E6 report".
  1304. * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
  1305. * The bits 0-2 of the first byte will be 1s if some buttons are
  1306. * pressed.
  1307. */
  1308. if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  1309. PSMOUSE_CMD_SETSCALE11, e6))
  1310. return -EIO;
  1311. if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
  1312. return -EINVAL;
  1313. /*
  1314. * Now get the "E7" and "EC" reports. These will uniquely identify
  1315. * most ALPS touchpads.
  1316. */
  1317. if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  1318. PSMOUSE_CMD_SETSCALE21, e7) ||
  1319. alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  1320. PSMOUSE_CMD_RESET_WRAP, ec) ||
  1321. alps_exit_command_mode(psmouse))
  1322. return -EIO;
  1323. if (alps_match_table(psmouse, priv, e7, ec) == 0) {
  1324. return 0;
  1325. } else if (ec[0] == 0x88 && ec[1] == 0x08) {
  1326. priv->proto_version = ALPS_PROTO_V3;
  1327. alps_set_defaults(priv);
  1328. priv->hw_init = alps_hw_init_rushmore_v3;
  1329. priv->decode_fields = alps_decode_rushmore;
  1330. priv->x_bits = 16;
  1331. priv->y_bits = 12;
  1332. /* hack to make addr_command, nibble_command available */
  1333. psmouse->private = priv;
  1334. if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
  1335. priv->flags &= ~ALPS_DUALPOINT;
  1336. return 0;
  1337. } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
  1338. ec[2] >= 0x90 && ec[2] <= 0x9d) {
  1339. priv->proto_version = ALPS_PROTO_V3;
  1340. alps_set_defaults(priv);
  1341. return 0;
  1342. }
  1343. psmouse_info(psmouse,
  1344. "Unknown ALPS touchpad: E7=%2.2x %2.2x %2.2x, EC=%2.2x %2.2x %2.2x\n",
  1345. e7[0], e7[1], e7[2], ec[0], ec[1], ec[2]);
  1346. return -EINVAL;
  1347. }
  1348. static int alps_reconnect(struct psmouse *psmouse)
  1349. {
  1350. struct alps_data *priv = psmouse->private;
  1351. psmouse_reset(psmouse);
  1352. if (alps_identify(psmouse, priv) < 0)
  1353. return -1;
  1354. return priv->hw_init(psmouse);
  1355. }
  1356. static void alps_disconnect(struct psmouse *psmouse)
  1357. {
  1358. struct alps_data *priv = psmouse->private;
  1359. psmouse_reset(psmouse);
  1360. del_timer_sync(&priv->timer);
  1361. input_unregister_device(priv->dev2);
  1362. kfree(priv);
  1363. }
  1364. static void alps_set_abs_params_st(struct alps_data *priv,
  1365. struct input_dev *dev1)
  1366. {
  1367. input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
  1368. input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
  1369. }
  1370. static void alps_set_abs_params_mt(struct alps_data *priv,
  1371. struct input_dev *dev1)
  1372. {
  1373. set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
  1374. input_mt_init_slots(dev1, 2, 0);
  1375. input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
  1376. input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
  1377. set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
  1378. set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
  1379. set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
  1380. input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
  1381. input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
  1382. }
  1383. int alps_init(struct psmouse *psmouse)
  1384. {
  1385. struct alps_data *priv;
  1386. struct input_dev *dev1 = psmouse->dev, *dev2;
  1387. priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
  1388. dev2 = input_allocate_device();
  1389. if (!priv || !dev2)
  1390. goto init_fail;
  1391. priv->dev2 = dev2;
  1392. setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
  1393. psmouse->private = priv;
  1394. psmouse_reset(psmouse);
  1395. if (alps_identify(psmouse, priv) < 0)
  1396. goto init_fail;
  1397. if (priv->hw_init(psmouse))
  1398. goto init_fail;
  1399. /*
  1400. * Undo part of setup done for us by psmouse core since touchpad
  1401. * is not a relative device.
  1402. */
  1403. __clear_bit(EV_REL, dev1->evbit);
  1404. __clear_bit(REL_X, dev1->relbit);
  1405. __clear_bit(REL_Y, dev1->relbit);
  1406. /*
  1407. * Now set up our capabilities.
  1408. */
  1409. dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
  1410. dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
  1411. dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
  1412. dev1->keybit[BIT_WORD(BTN_LEFT)] |=
  1413. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
  1414. dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
  1415. priv->set_abs_params(priv, dev1);
  1416. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  1417. if (priv->flags & ALPS_WHEEL) {
  1418. dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
  1419. dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
  1420. }
  1421. if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  1422. dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
  1423. dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
  1424. }
  1425. if (priv->flags & ALPS_FOUR_BUTTONS) {
  1426. dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
  1427. dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
  1428. dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
  1429. dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
  1430. } else {
  1431. dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
  1432. }
  1433. snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
  1434. dev2->phys = priv->phys;
  1435. dev2->name = (priv->flags & ALPS_DUALPOINT) ?
  1436. "DualPoint Stick" : "PS/2 Mouse";
  1437. dev2->id.bustype = BUS_I8042;
  1438. dev2->id.vendor = 0x0002;
  1439. dev2->id.product = PSMOUSE_ALPS;
  1440. dev2->id.version = 0x0000;
  1441. dev2->dev.parent = &psmouse->ps2dev.serio->dev;
  1442. dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  1443. dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  1444. dev2->keybit[BIT_WORD(BTN_LEFT)] =
  1445. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
  1446. if (input_register_device(priv->dev2))
  1447. goto init_fail;
  1448. psmouse->protocol_handler = alps_process_byte;
  1449. psmouse->poll = alps_poll;
  1450. psmouse->disconnect = alps_disconnect;
  1451. psmouse->reconnect = alps_reconnect;
  1452. psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
  1453. /* We are having trouble resyncing ALPS touchpads so disable it for now */
  1454. psmouse->resync_time = 0;
  1455. return 0;
  1456. init_fail:
  1457. psmouse_reset(psmouse);
  1458. input_free_device(dev2);
  1459. kfree(priv);
  1460. psmouse->private = NULL;
  1461. return -1;
  1462. }
  1463. int alps_detect(struct psmouse *psmouse, bool set_properties)
  1464. {
  1465. struct alps_data dummy;
  1466. if (alps_identify(psmouse, &dummy) < 0)
  1467. return -1;
  1468. if (set_properties) {
  1469. psmouse->vendor = "ALPS";
  1470. psmouse->name = dummy.flags & ALPS_DUALPOINT ?
  1471. "DualPoint TouchPad" : "GlidePoint";
  1472. psmouse->model = dummy.proto_version << 8;
  1473. }
  1474. return 0;
  1475. }