alps.c 49 KB

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