alps.c 45 KB

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