alps.c 47 KB

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