alps.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992
  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. static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
  67. { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
  68. { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
  69. { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
  70. { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
  71. { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
  72. { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
  73. { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
  74. { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
  75. { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
  76. { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
  77. { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
  78. { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
  79. { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
  80. { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
  81. { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
  82. { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
  83. };
  84. #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
  85. #define ALPS_PASS 0x04 /* device has a pass-through port */
  86. #define ALPS_WHEEL 0x08 /* hardware wheel present */
  87. #define ALPS_FW_BK_1 0x10 /* front & back buttons present */
  88. #define ALPS_FW_BK_2 0x20 /* front & back buttons present */
  89. #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
  90. #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
  91. 6-byte ALPS packet */
  92. #define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
  93. static const struct alps_model_info alps_model_data[] = {
  94. { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
  95. { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
  96. { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
  97. { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
  98. { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
  99. { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
  100. { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
  101. { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
  102. { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
  103. { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
  104. { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
  105. { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
  106. { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
  107. { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
  108. { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
  109. /*
  110. * XXX This entry is suspicious. First byte has zero lower nibble,
  111. * which is what a normal mouse would report. Also, the value 0x0e
  112. * isn't valid per PS/2 spec.
  113. */
  114. { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
  115. { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
  116. { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
  117. /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
  118. { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
  119. ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
  120. { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
  121. { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
  122. { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
  123. ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
  124. { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
  125. };
  126. static const struct alps_protocol_info alps_v3_protocol_data = {
  127. ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
  128. };
  129. static const struct alps_protocol_info alps_v3_rushmore_data = {
  130. ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
  131. };
  132. static const struct alps_protocol_info alps_v5_protocol_data = {
  133. ALPS_PROTO_V5, 0xc8, 0xd8, 0
  134. };
  135. static const struct alps_protocol_info alps_v7_protocol_data = {
  136. ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
  137. };
  138. static const struct alps_protocol_info alps_v8_protocol_data = {
  139. ALPS_PROTO_V8, 0x18, 0x18, 0
  140. };
  141. static void alps_set_abs_params_st(struct alps_data *priv,
  142. struct input_dev *dev1);
  143. static void alps_set_abs_params_mt(struct alps_data *priv,
  144. struct input_dev *dev1);
  145. static void alps_set_abs_params_v7(struct alps_data *priv,
  146. struct input_dev *dev1);
  147. static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
  148. struct input_dev *dev1);
  149. /* Packet formats are described in Documentation/input/alps.txt */
  150. static bool alps_is_valid_first_byte(struct alps_data *priv,
  151. unsigned char data)
  152. {
  153. return (data & priv->mask0) == priv->byte0;
  154. }
  155. static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
  156. int left, int right, int middle)
  157. {
  158. struct input_dev *dev;
  159. /*
  160. * If shared button has already been reported on the
  161. * other device (dev2) then this event should be also
  162. * sent through that device.
  163. */
  164. dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
  165. input_report_key(dev, BTN_LEFT, left);
  166. dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
  167. input_report_key(dev, BTN_RIGHT, right);
  168. dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
  169. input_report_key(dev, BTN_MIDDLE, middle);
  170. /*
  171. * Sync the _other_ device now, we'll do the first
  172. * device later once we report the rest of the events.
  173. */
  174. if (dev2)
  175. input_sync(dev2);
  176. }
  177. static void alps_process_packet_v1_v2(struct psmouse *psmouse)
  178. {
  179. struct alps_data *priv = psmouse->private;
  180. unsigned char *packet = psmouse->packet;
  181. struct input_dev *dev = psmouse->dev;
  182. struct input_dev *dev2 = priv->dev2;
  183. int x, y, z, ges, fin, left, right, middle;
  184. int back = 0, forward = 0;
  185. if (priv->proto_version == ALPS_PROTO_V1) {
  186. left = packet[2] & 0x10;
  187. right = packet[2] & 0x08;
  188. middle = 0;
  189. x = packet[1] | ((packet[0] & 0x07) << 7);
  190. y = packet[4] | ((packet[3] & 0x07) << 7);
  191. z = packet[5];
  192. } else {
  193. left = packet[3] & 1;
  194. right = packet[3] & 2;
  195. middle = packet[3] & 4;
  196. x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
  197. y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
  198. z = packet[5];
  199. }
  200. if (priv->flags & ALPS_FW_BK_1) {
  201. back = packet[0] & 0x10;
  202. forward = packet[2] & 4;
  203. }
  204. if (priv->flags & ALPS_FW_BK_2) {
  205. back = packet[3] & 4;
  206. forward = packet[2] & 4;
  207. if ((middle = forward && back))
  208. forward = back = 0;
  209. }
  210. ges = packet[2] & 1;
  211. fin = packet[2] & 2;
  212. if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
  213. input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
  214. input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
  215. alps_report_buttons(dev2, dev, left, right, middle);
  216. input_sync(dev2);
  217. return;
  218. }
  219. /* Non interleaved V2 dualpoint has separate stick button bits */
  220. if (priv->proto_version == ALPS_PROTO_V2 &&
  221. priv->flags == (ALPS_PASS | ALPS_DUALPOINT)) {
  222. left |= packet[0] & 1;
  223. right |= packet[0] & 2;
  224. middle |= packet[0] & 4;
  225. }
  226. alps_report_buttons(dev, dev2, left, right, middle);
  227. /* Convert hardware tap to a reasonable Z value */
  228. if (ges && !fin)
  229. z = 40;
  230. /*
  231. * A "tap and drag" operation is reported by the hardware as a transition
  232. * from (!fin && ges) to (fin && ges). This should be translated to the
  233. * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
  234. */
  235. if (ges && fin && !priv->prev_fin) {
  236. input_report_abs(dev, ABS_X, x);
  237. input_report_abs(dev, ABS_Y, y);
  238. input_report_abs(dev, ABS_PRESSURE, 0);
  239. input_report_key(dev, BTN_TOOL_FINGER, 0);
  240. input_sync(dev);
  241. }
  242. priv->prev_fin = fin;
  243. if (z > 30)
  244. input_report_key(dev, BTN_TOUCH, 1);
  245. if (z < 25)
  246. input_report_key(dev, BTN_TOUCH, 0);
  247. if (z > 0) {
  248. input_report_abs(dev, ABS_X, x);
  249. input_report_abs(dev, ABS_Y, y);
  250. }
  251. input_report_abs(dev, ABS_PRESSURE, z);
  252. input_report_key(dev, BTN_TOOL_FINGER, z > 0);
  253. if (priv->flags & ALPS_WHEEL)
  254. input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
  255. if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  256. input_report_key(dev, BTN_FORWARD, forward);
  257. input_report_key(dev, BTN_BACK, back);
  258. }
  259. if (priv->flags & ALPS_FOUR_BUTTONS) {
  260. input_report_key(dev, BTN_0, packet[2] & 4);
  261. input_report_key(dev, BTN_1, packet[0] & 0x10);
  262. input_report_key(dev, BTN_2, packet[3] & 4);
  263. input_report_key(dev, BTN_3, packet[0] & 0x20);
  264. }
  265. input_sync(dev);
  266. }
  267. /*
  268. * Process bitmap data for V5 protocols. Return value is null.
  269. *
  270. * The bitmaps don't have enough data to track fingers, so this function
  271. * only generates points representing a bounding box of at most two contacts.
  272. * These two points are returned in fields->mt.
  273. */
  274. static void alps_process_bitmap_dolphin(struct alps_data *priv,
  275. struct alps_fields *fields)
  276. {
  277. int box_middle_x, box_middle_y;
  278. unsigned int x_map, y_map;
  279. unsigned char start_bit, end_bit;
  280. unsigned char x_msb, x_lsb, y_msb, y_lsb;
  281. x_map = fields->x_map;
  282. y_map = fields->y_map;
  283. if (!x_map || !y_map)
  284. return;
  285. /* Get Most-significant and Least-significant bit */
  286. x_msb = fls(x_map);
  287. x_lsb = ffs(x_map);
  288. y_msb = fls(y_map);
  289. y_lsb = ffs(y_map);
  290. /* Most-significant bit should never exceed max sensor line number */
  291. if (x_msb > priv->x_bits || y_msb > priv->y_bits)
  292. return;
  293. if (fields->fingers > 1) {
  294. start_bit = priv->x_bits - x_msb;
  295. end_bit = priv->x_bits - x_lsb;
  296. box_middle_x = (priv->x_max * (start_bit + end_bit)) /
  297. (2 * (priv->x_bits - 1));
  298. start_bit = y_lsb - 1;
  299. end_bit = y_msb - 1;
  300. box_middle_y = (priv->y_max * (start_bit + end_bit)) /
  301. (2 * (priv->y_bits - 1));
  302. fields->mt[0] = fields->st;
  303. fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
  304. fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
  305. }
  306. }
  307. static void alps_get_bitmap_points(unsigned int map,
  308. struct alps_bitmap_point *low,
  309. struct alps_bitmap_point *high,
  310. int *fingers)
  311. {
  312. struct alps_bitmap_point *point;
  313. int i, bit, prev_bit = 0;
  314. point = low;
  315. for (i = 0; map != 0; i++, map >>= 1) {
  316. bit = map & 1;
  317. if (bit) {
  318. if (!prev_bit) {
  319. point->start_bit = i;
  320. point->num_bits = 0;
  321. (*fingers)++;
  322. }
  323. point->num_bits++;
  324. } else {
  325. if (prev_bit)
  326. point = high;
  327. }
  328. prev_bit = bit;
  329. }
  330. }
  331. /*
  332. * Process bitmap data from v3 and v4 protocols. Returns the number of
  333. * fingers detected. A return value of 0 means at least one of the
  334. * bitmaps was empty.
  335. *
  336. * The bitmaps don't have enough data to track fingers, so this function
  337. * only generates points representing a bounding box of all contacts.
  338. * These points are returned in fields->mt when the return value
  339. * is greater than 0.
  340. */
  341. static int alps_process_bitmap(struct alps_data *priv,
  342. struct alps_fields *fields)
  343. {
  344. int i, fingers_x = 0, fingers_y = 0, fingers;
  345. struct alps_bitmap_point x_low = {0,}, x_high = {0,};
  346. struct alps_bitmap_point y_low = {0,}, y_high = {0,};
  347. if (!fields->x_map || !fields->y_map)
  348. return 0;
  349. alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
  350. alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
  351. /*
  352. * Fingers can overlap, so we use the maximum count of fingers
  353. * on either axis as the finger count.
  354. */
  355. fingers = max(fingers_x, fingers_y);
  356. /*
  357. * If an axis reports only a single contact, we have overlapping or
  358. * adjacent fingers. Divide the single contact between the two points.
  359. */
  360. if (fingers_x == 1) {
  361. i = (x_low.num_bits - 1) / 2;
  362. x_low.num_bits = x_low.num_bits - i;
  363. x_high.start_bit = x_low.start_bit + i;
  364. x_high.num_bits = max(i, 1);
  365. }
  366. if (fingers_y == 1) {
  367. i = (y_low.num_bits - 1) / 2;
  368. y_low.num_bits = y_low.num_bits - i;
  369. y_high.start_bit = y_low.start_bit + i;
  370. y_high.num_bits = max(i, 1);
  371. }
  372. fields->mt[0].x =
  373. (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
  374. (2 * (priv->x_bits - 1));
  375. fields->mt[0].y =
  376. (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
  377. (2 * (priv->y_bits - 1));
  378. fields->mt[1].x =
  379. (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
  380. (2 * (priv->x_bits - 1));
  381. fields->mt[1].y =
  382. (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
  383. (2 * (priv->y_bits - 1));
  384. /* y-bitmap order is reversed, except on rushmore */
  385. if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
  386. fields->mt[0].y = priv->y_max - fields->mt[0].y;
  387. fields->mt[1].y = priv->y_max - fields->mt[1].y;
  388. }
  389. return fingers;
  390. }
  391. static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
  392. {
  393. input_mt_slot(dev, slot);
  394. input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
  395. input_report_abs(dev, ABS_MT_POSITION_X, x);
  396. input_report_abs(dev, ABS_MT_POSITION_Y, y);
  397. }
  398. static void alps_report_mt_data(struct psmouse *psmouse, int n)
  399. {
  400. struct alps_data *priv = psmouse->private;
  401. struct input_dev *dev = psmouse->dev;
  402. struct alps_fields *f = &priv->f;
  403. int i, slot[MAX_TOUCHES];
  404. input_mt_assign_slots(dev, slot, f->mt, n, 0);
  405. for (i = 0; i < n; i++)
  406. alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
  407. input_mt_sync_frame(dev);
  408. }
  409. static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
  410. {
  411. struct alps_data *priv = psmouse->private;
  412. struct input_dev *dev = psmouse->dev;
  413. struct alps_fields *f = &priv->f;
  414. /* Use st data when we don't have mt data */
  415. if (fingers < 2) {
  416. f->mt[0].x = f->st.x;
  417. f->mt[0].y = f->st.y;
  418. fingers = f->pressure > 0 ? 1 : 0;
  419. }
  420. alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
  421. input_mt_report_finger_count(dev, fingers);
  422. input_report_key(dev, BTN_LEFT, f->left);
  423. input_report_key(dev, BTN_RIGHT, f->right);
  424. input_report_key(dev, BTN_MIDDLE, f->middle);
  425. input_report_abs(dev, ABS_PRESSURE, f->pressure);
  426. input_sync(dev);
  427. }
  428. static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
  429. {
  430. struct alps_data *priv = psmouse->private;
  431. unsigned char *packet = psmouse->packet;
  432. struct input_dev *dev = priv->dev2;
  433. int x, y, z, left, right, middle;
  434. /* It should be a DualPoint when received trackstick packet */
  435. if (!(priv->flags & ALPS_DUALPOINT)) {
  436. psmouse_warn(psmouse,
  437. "Rejected trackstick packet from non DualPoint device");
  438. return;
  439. }
  440. /* Sanity check packet */
  441. if (!(packet[0] & 0x40)) {
  442. psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
  443. return;
  444. }
  445. /*
  446. * There's a special packet that seems to indicate the end
  447. * of a stream of trackstick data. Filter these out.
  448. */
  449. if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
  450. return;
  451. x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
  452. y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
  453. z = (packet[4] & 0x7c) >> 2;
  454. /*
  455. * The x and y values tend to be quite large, and when used
  456. * alone the trackstick is difficult to use. Scale them down
  457. * to compensate.
  458. */
  459. x /= 8;
  460. y /= 8;
  461. input_report_rel(dev, REL_X, x);
  462. input_report_rel(dev, REL_Y, -y);
  463. /*
  464. * Most ALPS models report the trackstick buttons in the touchpad
  465. * packets, but a few report them here. No reliable way has been
  466. * found to differentiate between the models upfront, so we enable
  467. * the quirk in response to seeing a button press in the trackstick
  468. * packet.
  469. */
  470. left = packet[3] & 0x01;
  471. right = packet[3] & 0x02;
  472. middle = packet[3] & 0x04;
  473. if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
  474. (left || right || middle))
  475. priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
  476. if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
  477. input_report_key(dev, BTN_LEFT, left);
  478. input_report_key(dev, BTN_RIGHT, right);
  479. input_report_key(dev, BTN_MIDDLE, middle);
  480. }
  481. input_sync(dev);
  482. return;
  483. }
  484. static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
  485. {
  486. f->left = !!(p[3] & 0x01);
  487. f->right = !!(p[3] & 0x02);
  488. f->middle = !!(p[3] & 0x04);
  489. f->ts_left = !!(p[3] & 0x10);
  490. f->ts_right = !!(p[3] & 0x20);
  491. f->ts_middle = !!(p[3] & 0x40);
  492. }
  493. static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
  494. struct psmouse *psmouse)
  495. {
  496. f->first_mp = !!(p[4] & 0x40);
  497. f->is_mp = !!(p[0] & 0x40);
  498. f->fingers = (p[5] & 0x3) + 1;
  499. f->x_map = ((p[4] & 0x7e) << 8) |
  500. ((p[1] & 0x7f) << 2) |
  501. ((p[0] & 0x30) >> 4);
  502. f->y_map = ((p[3] & 0x70) << 4) |
  503. ((p[2] & 0x7f) << 1) |
  504. (p[4] & 0x01);
  505. f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
  506. ((p[0] & 0x30) >> 4);
  507. f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
  508. f->pressure = p[5] & 0x7f;
  509. alps_decode_buttons_v3(f, p);
  510. return 0;
  511. }
  512. static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
  513. struct psmouse *psmouse)
  514. {
  515. alps_decode_pinnacle(f, p, psmouse);
  516. /* Rushmore's packet decode has a bit difference with Pinnacle's */
  517. f->is_mp = !!(p[5] & 0x40);
  518. f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
  519. f->x_map |= (p[5] & 0x10) << 11;
  520. f->y_map |= (p[5] & 0x20) << 6;
  521. return 0;
  522. }
  523. static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
  524. struct psmouse *psmouse)
  525. {
  526. u64 palm_data = 0;
  527. struct alps_data *priv = psmouse->private;
  528. f->first_mp = !!(p[0] & 0x02);
  529. f->is_mp = !!(p[0] & 0x20);
  530. if (!f->is_mp) {
  531. f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
  532. f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
  533. f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
  534. alps_decode_buttons_v3(f, p);
  535. } else {
  536. f->fingers = ((p[0] & 0x6) >> 1 |
  537. (p[0] & 0x10) >> 2);
  538. palm_data = (p[1] & 0x7f) |
  539. ((p[2] & 0x7f) << 7) |
  540. ((p[4] & 0x7f) << 14) |
  541. ((p[5] & 0x7f) << 21) |
  542. ((p[3] & 0x07) << 28) |
  543. (((u64)p[3] & 0x70) << 27) |
  544. (((u64)p[0] & 0x01) << 34);
  545. /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
  546. f->y_map = palm_data & (BIT(priv->y_bits) - 1);
  547. /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
  548. f->x_map = (palm_data >> priv->y_bits) &
  549. (BIT(priv->x_bits) - 1);
  550. }
  551. return 0;
  552. }
  553. static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
  554. {
  555. struct alps_data *priv = psmouse->private;
  556. unsigned char *packet = psmouse->packet;
  557. struct input_dev *dev2 = priv->dev2;
  558. struct alps_fields *f = &priv->f;
  559. int fingers = 0;
  560. memset(f, 0, sizeof(*f));
  561. priv->decode_fields(f, packet, psmouse);
  562. /*
  563. * There's no single feature of touchpad position and bitmap packets
  564. * that can be used to distinguish between them. We rely on the fact
  565. * that a bitmap packet should always follow a position packet with
  566. * bit 6 of packet[4] set.
  567. */
  568. if (priv->multi_packet) {
  569. /*
  570. * Sometimes a position packet will indicate a multi-packet
  571. * sequence, but then what follows is another position
  572. * packet. Check for this, and when it happens process the
  573. * position packet as usual.
  574. */
  575. if (f->is_mp) {
  576. fingers = f->fingers;
  577. if (priv->proto_version == ALPS_PROTO_V3 ||
  578. priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
  579. if (alps_process_bitmap(priv, f) == 0)
  580. fingers = 0; /* Use st data */
  581. /* Now process position packet */
  582. priv->decode_fields(f, priv->multi_data,
  583. psmouse);
  584. } else {
  585. /*
  586. * Because Dolphin uses position packet's
  587. * coordinate data as Pt1 and uses it to
  588. * calculate Pt2, so we need to do position
  589. * packet decode first.
  590. */
  591. priv->decode_fields(f, priv->multi_data,
  592. psmouse);
  593. /*
  594. * Since Dolphin's finger number is reliable,
  595. * there is no need to compare with bmap_fn.
  596. */
  597. alps_process_bitmap_dolphin(priv, f);
  598. }
  599. } else {
  600. priv->multi_packet = 0;
  601. }
  602. }
  603. /*
  604. * Bit 6 of byte 0 is not usually set in position packets. The only
  605. * times it seems to be set is in situations where the data is
  606. * suspect anyway, e.g. a palm resting flat on the touchpad. Given
  607. * this combined with the fact that this bit is useful for filtering
  608. * out misidentified bitmap packets, we reject anything with this
  609. * bit set.
  610. */
  611. if (f->is_mp)
  612. return;
  613. if (!priv->multi_packet && f->first_mp) {
  614. priv->multi_packet = 1;
  615. memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
  616. return;
  617. }
  618. priv->multi_packet = 0;
  619. /*
  620. * Sometimes the hardware sends a single packet with z = 0
  621. * in the middle of a stream. Real releases generate packets
  622. * with x, y, and z all zero, so these seem to be flukes.
  623. * Ignore them.
  624. */
  625. if (f->st.x && f->st.y && !f->pressure)
  626. return;
  627. alps_report_semi_mt_data(psmouse, fingers);
  628. if ((priv->flags & ALPS_DUALPOINT) &&
  629. !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
  630. input_report_key(dev2, BTN_LEFT, f->ts_left);
  631. input_report_key(dev2, BTN_RIGHT, f->ts_right);
  632. input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
  633. input_sync(dev2);
  634. }
  635. }
  636. static void alps_process_packet_v3(struct psmouse *psmouse)
  637. {
  638. unsigned char *packet = psmouse->packet;
  639. /*
  640. * v3 protocol packets come in three types, two representing
  641. * touchpad data and one representing trackstick data.
  642. * Trackstick packets seem to be distinguished by always
  643. * having 0x3f in the last byte. This value has never been
  644. * observed in the last byte of either of the other types
  645. * of packets.
  646. */
  647. if (packet[5] == 0x3f) {
  648. alps_process_trackstick_packet_v3(psmouse);
  649. return;
  650. }
  651. alps_process_touchpad_packet_v3_v5(psmouse);
  652. }
  653. static void alps_process_packet_v6(struct psmouse *psmouse)
  654. {
  655. struct alps_data *priv = psmouse->private;
  656. unsigned char *packet = psmouse->packet;
  657. struct input_dev *dev = psmouse->dev;
  658. struct input_dev *dev2 = priv->dev2;
  659. int x, y, z, left, right, middle;
  660. /*
  661. * We can use Byte5 to distinguish if the packet is from Touchpad
  662. * or Trackpoint.
  663. * Touchpad: 0 - 0x7E
  664. * Trackpoint: 0x7F
  665. */
  666. if (packet[5] == 0x7F) {
  667. /* It should be a DualPoint when received Trackpoint packet */
  668. if (!(priv->flags & ALPS_DUALPOINT)) {
  669. psmouse_warn(psmouse,
  670. "Rejected trackstick packet from non DualPoint device");
  671. return;
  672. }
  673. /* Trackpoint packet */
  674. x = packet[1] | ((packet[3] & 0x20) << 2);
  675. y = packet[2] | ((packet[3] & 0x40) << 1);
  676. z = packet[4];
  677. left = packet[3] & 0x01;
  678. right = packet[3] & 0x02;
  679. middle = packet[3] & 0x04;
  680. /* To prevent the cursor jump when finger lifted */
  681. if (x == 0x7F && y == 0x7F && z == 0x7F)
  682. x = y = z = 0;
  683. /* Divide 4 since trackpoint's speed is too fast */
  684. input_report_rel(dev2, REL_X, (char)x / 4);
  685. input_report_rel(dev2, REL_Y, -((char)y / 4));
  686. input_report_key(dev2, BTN_LEFT, left);
  687. input_report_key(dev2, BTN_RIGHT, right);
  688. input_report_key(dev2, BTN_MIDDLE, middle);
  689. input_sync(dev2);
  690. return;
  691. }
  692. /* Touchpad packet */
  693. x = packet[1] | ((packet[3] & 0x78) << 4);
  694. y = packet[2] | ((packet[4] & 0x78) << 4);
  695. z = packet[5];
  696. left = packet[3] & 0x01;
  697. right = packet[3] & 0x02;
  698. if (z > 30)
  699. input_report_key(dev, BTN_TOUCH, 1);
  700. if (z < 25)
  701. input_report_key(dev, BTN_TOUCH, 0);
  702. if (z > 0) {
  703. input_report_abs(dev, ABS_X, x);
  704. input_report_abs(dev, ABS_Y, y);
  705. }
  706. input_report_abs(dev, ABS_PRESSURE, z);
  707. input_report_key(dev, BTN_TOOL_FINGER, z > 0);
  708. /* v6 touchpad does not have middle button */
  709. input_report_key(dev, BTN_LEFT, left);
  710. input_report_key(dev, BTN_RIGHT, right);
  711. input_sync(dev);
  712. }
  713. static void alps_process_packet_v4(struct psmouse *psmouse)
  714. {
  715. struct alps_data *priv = psmouse->private;
  716. unsigned char *packet = psmouse->packet;
  717. struct alps_fields *f = &priv->f;
  718. int offset;
  719. /*
  720. * v4 has a 6-byte encoding for bitmap data, but this data is
  721. * broken up between 3 normal packets. Use priv->multi_packet to
  722. * track our position in the bitmap packet.
  723. */
  724. if (packet[6] & 0x40) {
  725. /* sync, reset position */
  726. priv->multi_packet = 0;
  727. }
  728. if (WARN_ON_ONCE(priv->multi_packet > 2))
  729. return;
  730. offset = 2 * priv->multi_packet;
  731. priv->multi_data[offset] = packet[6];
  732. priv->multi_data[offset + 1] = packet[7];
  733. if (++priv->multi_packet > 2) {
  734. priv->multi_packet = 0;
  735. f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
  736. ((priv->multi_data[3] & 0x60) << 3) |
  737. ((priv->multi_data[0] & 0x3f) << 2) |
  738. ((priv->multi_data[1] & 0x60) >> 5);
  739. f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
  740. ((priv->multi_data[3] & 0x1f) << 5) |
  741. (priv->multi_data[1] & 0x1f);
  742. f->fingers = alps_process_bitmap(priv, f);
  743. }
  744. f->left = !!(packet[4] & 0x01);
  745. f->right = !!(packet[4] & 0x02);
  746. f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
  747. ((packet[0] & 0x30) >> 4);
  748. f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
  749. f->pressure = packet[5] & 0x7f;
  750. alps_report_semi_mt_data(psmouse, f->fingers);
  751. }
  752. static bool alps_is_valid_package_v7(struct psmouse *psmouse)
  753. {
  754. switch (psmouse->pktcnt) {
  755. case 3:
  756. return (psmouse->packet[2] & 0x40) == 0x40;
  757. case 4:
  758. return (psmouse->packet[3] & 0x48) == 0x48;
  759. case 6:
  760. return (psmouse->packet[5] & 0x40) == 0x00;
  761. }
  762. return true;
  763. }
  764. static unsigned char alps_get_packet_id_v7(char *byte)
  765. {
  766. unsigned char packet_id;
  767. if (byte[4] & 0x40)
  768. packet_id = V7_PACKET_ID_TWO;
  769. else if (byte[4] & 0x01)
  770. packet_id = V7_PACKET_ID_MULTI;
  771. else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
  772. packet_id = V7_PACKET_ID_NEW;
  773. else if (byte[1] == 0x00 && byte[4] == 0x00)
  774. packet_id = V7_PACKET_ID_IDLE;
  775. else
  776. packet_id = V7_PACKET_ID_UNKNOWN;
  777. return packet_id;
  778. }
  779. static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
  780. unsigned char *pkt,
  781. unsigned char pkt_id)
  782. {
  783. mt[0].x = ((pkt[2] & 0x80) << 4);
  784. mt[0].x |= ((pkt[2] & 0x3F) << 5);
  785. mt[0].x |= ((pkt[3] & 0x30) >> 1);
  786. mt[0].x |= (pkt[3] & 0x07);
  787. mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
  788. mt[1].x = ((pkt[3] & 0x80) << 4);
  789. mt[1].x |= ((pkt[4] & 0x80) << 3);
  790. mt[1].x |= ((pkt[4] & 0x3F) << 4);
  791. mt[1].y = ((pkt[5] & 0x80) << 3);
  792. mt[1].y |= ((pkt[5] & 0x3F) << 4);
  793. switch (pkt_id) {
  794. case V7_PACKET_ID_TWO:
  795. mt[1].x &= ~0x000F;
  796. mt[1].y |= 0x000F;
  797. /* Detect false-postive touches where x & y report max value */
  798. if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
  799. mt[1].x = 0;
  800. /* y gets set to 0 at the end of this function */
  801. }
  802. break;
  803. case V7_PACKET_ID_MULTI:
  804. mt[1].x &= ~0x003F;
  805. mt[1].y &= ~0x0020;
  806. mt[1].y |= ((pkt[4] & 0x02) << 4);
  807. mt[1].y |= 0x001F;
  808. break;
  809. case V7_PACKET_ID_NEW:
  810. mt[1].x &= ~0x003F;
  811. mt[1].x |= (pkt[0] & 0x20);
  812. mt[1].y |= 0x000F;
  813. break;
  814. }
  815. mt[0].y = 0x7FF - mt[0].y;
  816. mt[1].y = 0x7FF - mt[1].y;
  817. }
  818. static int alps_get_mt_count(struct input_mt_pos *mt)
  819. {
  820. int i, fingers = 0;
  821. for (i = 0; i < MAX_TOUCHES; i++) {
  822. if (mt[i].x != 0 || mt[i].y != 0)
  823. fingers++;
  824. }
  825. return fingers;
  826. }
  827. static int alps_decode_packet_v7(struct alps_fields *f,
  828. unsigned char *p,
  829. struct psmouse *psmouse)
  830. {
  831. struct alps_data *priv = psmouse->private;
  832. unsigned char pkt_id;
  833. pkt_id = alps_get_packet_id_v7(p);
  834. if (pkt_id == V7_PACKET_ID_IDLE)
  835. return 0;
  836. if (pkt_id == V7_PACKET_ID_UNKNOWN)
  837. return -1;
  838. /*
  839. * NEW packets are send to indicate a discontinuity in the finger
  840. * coordinate reporting. Specifically a finger may have moved from
  841. * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
  842. * us.
  843. *
  844. * NEW packets have 3 problems:
  845. * 1) They do not contain middle / right button info (on non clickpads)
  846. * this can be worked around by preserving the old button state
  847. * 2) They do not contain an accurate fingercount, and they are
  848. * typically send when the number of fingers changes. We cannot use
  849. * the old finger count as that may mismatch with the amount of
  850. * touch coordinates we've available in the NEW packet
  851. * 3) Their x data for the second touch is inaccurate leading to
  852. * a possible jump of the x coordinate by 16 units when the first
  853. * non NEW packet comes in
  854. * Since problems 2 & 3 cannot be worked around, just ignore them.
  855. */
  856. if (pkt_id == V7_PACKET_ID_NEW)
  857. return 1;
  858. alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
  859. if (pkt_id == V7_PACKET_ID_TWO)
  860. f->fingers = alps_get_mt_count(f->mt);
  861. else /* pkt_id == V7_PACKET_ID_MULTI */
  862. f->fingers = 3 + (p[5] & 0x03);
  863. f->left = (p[0] & 0x80) >> 7;
  864. if (priv->flags & ALPS_BUTTONPAD) {
  865. if (p[0] & 0x20)
  866. f->fingers++;
  867. if (p[0] & 0x10)
  868. f->fingers++;
  869. } else {
  870. f->right = (p[0] & 0x20) >> 5;
  871. f->middle = (p[0] & 0x10) >> 4;
  872. }
  873. /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
  874. if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
  875. f->mt[0].x = f->mt[1].x;
  876. f->mt[0].y = f->mt[1].y;
  877. f->mt[1].x = 0;
  878. f->mt[1].y = 0;
  879. }
  880. return 0;
  881. }
  882. static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
  883. {
  884. struct alps_data *priv = psmouse->private;
  885. unsigned char *packet = psmouse->packet;
  886. struct input_dev *dev2 = priv->dev2;
  887. int x, y, z, left, right, middle;
  888. /* It should be a DualPoint when received trackstick packet */
  889. if (!(priv->flags & ALPS_DUALPOINT)) {
  890. psmouse_warn(psmouse,
  891. "Rejected trackstick packet from non DualPoint device");
  892. return;
  893. }
  894. x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
  895. y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
  896. ((packet[3] & 0x20) << 1);
  897. z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
  898. left = (packet[1] & 0x01);
  899. right = (packet[1] & 0x02) >> 1;
  900. middle = (packet[1] & 0x04) >> 2;
  901. input_report_rel(dev2, REL_X, (char)x);
  902. input_report_rel(dev2, REL_Y, -((char)y));
  903. input_report_key(dev2, BTN_LEFT, left);
  904. input_report_key(dev2, BTN_RIGHT, right);
  905. input_report_key(dev2, BTN_MIDDLE, middle);
  906. input_sync(dev2);
  907. }
  908. static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
  909. {
  910. struct alps_data *priv = psmouse->private;
  911. struct input_dev *dev = psmouse->dev;
  912. struct alps_fields *f = &priv->f;
  913. memset(f, 0, sizeof(*f));
  914. if (priv->decode_fields(f, psmouse->packet, psmouse))
  915. return;
  916. alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
  917. input_mt_report_finger_count(dev, f->fingers);
  918. input_report_key(dev, BTN_LEFT, f->left);
  919. input_report_key(dev, BTN_RIGHT, f->right);
  920. input_report_key(dev, BTN_MIDDLE, f->middle);
  921. input_sync(dev);
  922. }
  923. static void alps_process_packet_v7(struct psmouse *psmouse)
  924. {
  925. unsigned char *packet = psmouse->packet;
  926. if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
  927. alps_process_trackstick_packet_v7(psmouse);
  928. else
  929. alps_process_touchpad_packet_v7(psmouse);
  930. }
  931. static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
  932. {
  933. unsigned char pkt_id = SS4_PACKET_ID_IDLE;
  934. if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
  935. (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 && byte[5] == 0x00) {
  936. pkt_id = SS4_PACKET_ID_IDLE;
  937. } else if (!(byte[3] & 0x10)) {
  938. pkt_id = SS4_PACKET_ID_ONE;
  939. } else if (!(byte[3] & 0x20)) {
  940. pkt_id = SS4_PACKET_ID_TWO;
  941. } else {
  942. pkt_id = SS4_PACKET_ID_MULTI;
  943. }
  944. return pkt_id;
  945. }
  946. static int alps_decode_ss4_v2(struct alps_fields *f,
  947. unsigned char *p, struct psmouse *psmouse)
  948. {
  949. struct alps_data *priv = psmouse->private;
  950. unsigned char pkt_id;
  951. unsigned int no_data_x, no_data_y;
  952. pkt_id = alps_get_pkt_id_ss4_v2(p);
  953. /* Current packet is 1Finger coordinate packet */
  954. switch (pkt_id) {
  955. case SS4_PACKET_ID_ONE:
  956. f->mt[0].x = SS4_1F_X_V2(p);
  957. f->mt[0].y = SS4_1F_Y_V2(p);
  958. f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
  959. f->fingers = 1;
  960. f->first_mp = 0;
  961. f->is_mp = 0;
  962. break;
  963. case SS4_PACKET_ID_TWO:
  964. if (priv->flags & ALPS_BUTTONPAD) {
  965. f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
  966. f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
  967. f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
  968. f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
  969. } else {
  970. f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
  971. f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
  972. f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
  973. f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
  974. }
  975. f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
  976. if (SS4_IS_MF_CONTINUE(p)) {
  977. f->first_mp = 1;
  978. } else {
  979. f->fingers = 2;
  980. f->first_mp = 0;
  981. }
  982. f->is_mp = 0;
  983. break;
  984. case SS4_PACKET_ID_MULTI:
  985. if (priv->flags & ALPS_BUTTONPAD) {
  986. f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
  987. f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
  988. f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
  989. f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
  990. no_data_x = SS4_MFPACKET_NO_AX_BL;
  991. no_data_y = SS4_MFPACKET_NO_AY_BL;
  992. } else {
  993. f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
  994. f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
  995. f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
  996. f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
  997. no_data_x = SS4_MFPACKET_NO_AX;
  998. no_data_y = SS4_MFPACKET_NO_AY;
  999. }
  1000. f->first_mp = 0;
  1001. f->is_mp = 1;
  1002. if (SS4_IS_5F_DETECTED(p)) {
  1003. f->fingers = 5;
  1004. } else if (f->mt[3].x == no_data_x &&
  1005. f->mt[3].y == no_data_y) {
  1006. f->mt[3].x = 0;
  1007. f->mt[3].y = 0;
  1008. f->fingers = 3;
  1009. } else {
  1010. f->fingers = 4;
  1011. }
  1012. break;
  1013. case SS4_PACKET_ID_IDLE:
  1014. default:
  1015. memset(f, 0, sizeof(struct alps_fields));
  1016. break;
  1017. }
  1018. f->left = !!(SS4_BTN_V2(p) & 0x01);
  1019. if (!(priv->flags & ALPS_BUTTONPAD)) {
  1020. f->right = !!(SS4_BTN_V2(p) & 0x02);
  1021. f->middle = !!(SS4_BTN_V2(p) & 0x04);
  1022. }
  1023. return 0;
  1024. }
  1025. static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
  1026. {
  1027. struct alps_data *priv = psmouse->private;
  1028. unsigned char *packet = psmouse->packet;
  1029. struct input_dev *dev = psmouse->dev;
  1030. struct alps_fields *f = &priv->f;
  1031. memset(f, 0, sizeof(struct alps_fields));
  1032. priv->decode_fields(f, packet, psmouse);
  1033. if (priv->multi_packet) {
  1034. /*
  1035. * Sometimes the first packet will indicate a multi-packet
  1036. * sequence, but sometimes the next multi-packet would not
  1037. * come. Check for this, and when it happens process the
  1038. * position packet as usual.
  1039. */
  1040. if (f->is_mp) {
  1041. /* Now process the 1st packet */
  1042. priv->decode_fields(f, priv->multi_data, psmouse);
  1043. } else {
  1044. priv->multi_packet = 0;
  1045. }
  1046. }
  1047. /*
  1048. * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
  1049. * When it is set, it means 2nd packet comes without 1st packet come.
  1050. */
  1051. if (f->is_mp)
  1052. return;
  1053. /* Save the first packet */
  1054. if (!priv->multi_packet && f->first_mp) {
  1055. priv->multi_packet = 1;
  1056. memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
  1057. return;
  1058. }
  1059. priv->multi_packet = 0;
  1060. alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
  1061. input_mt_report_finger_count(dev, f->fingers);
  1062. input_report_key(dev, BTN_LEFT, f->left);
  1063. input_report_key(dev, BTN_RIGHT, f->right);
  1064. input_report_key(dev, BTN_MIDDLE, f->middle);
  1065. input_report_abs(dev, ABS_PRESSURE, f->pressure);
  1066. input_sync(dev);
  1067. }
  1068. static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
  1069. {
  1070. if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
  1071. return false;
  1072. if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
  1073. return false;
  1074. return true;
  1075. }
  1076. static DEFINE_MUTEX(alps_mutex);
  1077. static void alps_register_bare_ps2_mouse(struct work_struct *work)
  1078. {
  1079. struct alps_data *priv =
  1080. container_of(work, struct alps_data, dev3_register_work.work);
  1081. struct psmouse *psmouse = priv->psmouse;
  1082. struct input_dev *dev3;
  1083. int error = 0;
  1084. mutex_lock(&alps_mutex);
  1085. if (priv->dev3)
  1086. goto out;
  1087. dev3 = input_allocate_device();
  1088. if (!dev3) {
  1089. psmouse_err(psmouse, "failed to allocate secondary device\n");
  1090. error = -ENOMEM;
  1091. goto out;
  1092. }
  1093. snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
  1094. psmouse->ps2dev.serio->phys,
  1095. (priv->dev2 ? "input2" : "input1"));
  1096. dev3->phys = priv->phys3;
  1097. /*
  1098. * format of input device name is: "protocol vendor name"
  1099. * see function psmouse_switch_protocol() in psmouse-base.c
  1100. */
  1101. dev3->name = "PS/2 ALPS Mouse";
  1102. dev3->id.bustype = BUS_I8042;
  1103. dev3->id.vendor = 0x0002;
  1104. dev3->id.product = PSMOUSE_PS2;
  1105. dev3->id.version = 0x0000;
  1106. dev3->dev.parent = &psmouse->ps2dev.serio->dev;
  1107. input_set_capability(dev3, EV_REL, REL_X);
  1108. input_set_capability(dev3, EV_REL, REL_Y);
  1109. input_set_capability(dev3, EV_KEY, BTN_LEFT);
  1110. input_set_capability(dev3, EV_KEY, BTN_RIGHT);
  1111. input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
  1112. __set_bit(INPUT_PROP_POINTER, dev3->propbit);
  1113. error = input_register_device(dev3);
  1114. if (error) {
  1115. psmouse_err(psmouse,
  1116. "failed to register secondary device: %d\n",
  1117. error);
  1118. input_free_device(dev3);
  1119. goto out;
  1120. }
  1121. priv->dev3 = dev3;
  1122. out:
  1123. /*
  1124. * Save the error code so that we can detect that we
  1125. * already tried to create the device.
  1126. */
  1127. if (error)
  1128. priv->dev3 = ERR_PTR(error);
  1129. mutex_unlock(&alps_mutex);
  1130. }
  1131. static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
  1132. unsigned char packet[],
  1133. bool report_buttons)
  1134. {
  1135. struct alps_data *priv = psmouse->private;
  1136. struct input_dev *dev, *dev2 = NULL;
  1137. /* Figure out which device to use to report the bare packet */
  1138. if (priv->proto_version == ALPS_PROTO_V2 &&
  1139. (priv->flags & ALPS_DUALPOINT)) {
  1140. /* On V2 devices the DualPoint Stick reports bare packets */
  1141. dev = priv->dev2;
  1142. dev2 = psmouse->dev;
  1143. } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
  1144. /* Register dev3 mouse if we received PS/2 packet first time */
  1145. if (!IS_ERR(priv->dev3))
  1146. psmouse_queue_work(psmouse, &priv->dev3_register_work,
  1147. 0);
  1148. return;
  1149. } else {
  1150. dev = priv->dev3;
  1151. }
  1152. if (report_buttons)
  1153. alps_report_buttons(dev, dev2,
  1154. packet[0] & 1, packet[0] & 2, packet[0] & 4);
  1155. input_report_rel(dev, REL_X,
  1156. packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
  1157. input_report_rel(dev, REL_Y,
  1158. packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
  1159. input_sync(dev);
  1160. }
  1161. static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
  1162. {
  1163. struct alps_data *priv = psmouse->private;
  1164. if (psmouse->pktcnt < 6)
  1165. return PSMOUSE_GOOD_DATA;
  1166. if (psmouse->pktcnt == 6) {
  1167. /*
  1168. * Start a timer to flush the packet if it ends up last
  1169. * 6-byte packet in the stream. Timer needs to fire
  1170. * psmouse core times out itself. 20 ms should be enough
  1171. * to decide if we are getting more data or not.
  1172. */
  1173. mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
  1174. return PSMOUSE_GOOD_DATA;
  1175. }
  1176. del_timer(&priv->timer);
  1177. if (psmouse->packet[6] & 0x80) {
  1178. /*
  1179. * Highest bit is set - that means we either had
  1180. * complete ALPS packet and this is start of the
  1181. * next packet or we got garbage.
  1182. */
  1183. if (((psmouse->packet[3] |
  1184. psmouse->packet[4] |
  1185. psmouse->packet[5]) & 0x80) ||
  1186. (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
  1187. psmouse_dbg(psmouse,
  1188. "refusing packet %4ph (suspected interleaved ps/2)\n",
  1189. psmouse->packet + 3);
  1190. return PSMOUSE_BAD_DATA;
  1191. }
  1192. priv->process_packet(psmouse);
  1193. /* Continue with the next packet */
  1194. psmouse->packet[0] = psmouse->packet[6];
  1195. psmouse->pktcnt = 1;
  1196. } else {
  1197. /*
  1198. * High bit is 0 - that means that we indeed got a PS/2
  1199. * packet in the middle of ALPS packet.
  1200. *
  1201. * There is also possibility that we got 6-byte ALPS
  1202. * packet followed by 3-byte packet from trackpoint. We
  1203. * can not distinguish between these 2 scenarios but
  1204. * because the latter is unlikely to happen in course of
  1205. * normal operation (user would need to press all
  1206. * buttons on the pad and start moving trackpoint
  1207. * without touching the pad surface) we assume former.
  1208. * Even if we are wrong the wost thing that would happen
  1209. * the cursor would jump but we should not get protocol
  1210. * de-synchronization.
  1211. */
  1212. alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
  1213. false);
  1214. /*
  1215. * Continue with the standard ALPS protocol handling,
  1216. * but make sure we won't process it as an interleaved
  1217. * packet again, which may happen if all buttons are
  1218. * pressed. To avoid this let's reset the 4th bit which
  1219. * is normally 1.
  1220. */
  1221. psmouse->packet[3] = psmouse->packet[6] & 0xf7;
  1222. psmouse->pktcnt = 4;
  1223. }
  1224. return PSMOUSE_GOOD_DATA;
  1225. }
  1226. static void alps_flush_packet(unsigned long data)
  1227. {
  1228. struct psmouse *psmouse = (struct psmouse *)data;
  1229. struct alps_data *priv = psmouse->private;
  1230. serio_pause_rx(psmouse->ps2dev.serio);
  1231. if (psmouse->pktcnt == psmouse->pktsize) {
  1232. /*
  1233. * We did not any more data in reasonable amount of time.
  1234. * Validate the last 3 bytes and process as a standard
  1235. * ALPS packet.
  1236. */
  1237. if ((psmouse->packet[3] |
  1238. psmouse->packet[4] |
  1239. psmouse->packet[5]) & 0x80) {
  1240. psmouse_dbg(psmouse,
  1241. "refusing packet %3ph (suspected interleaved ps/2)\n",
  1242. psmouse->packet + 3);
  1243. } else {
  1244. priv->process_packet(psmouse);
  1245. }
  1246. psmouse->pktcnt = 0;
  1247. }
  1248. serio_continue_rx(psmouse->ps2dev.serio);
  1249. }
  1250. static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
  1251. {
  1252. struct alps_data *priv = psmouse->private;
  1253. /*
  1254. * Check if we are dealing with a bare PS/2 packet, presumably from
  1255. * a device connected to the external PS/2 port. Because bare PS/2
  1256. * protocol does not have enough constant bits to self-synchronize
  1257. * properly we only do this if the device is fully synchronized.
  1258. * Can not distinguish V8's first byte from PS/2 packet's
  1259. */
  1260. if (priv->proto_version != ALPS_PROTO_V8 &&
  1261. !psmouse->out_of_sync_cnt &&
  1262. (psmouse->packet[0] & 0xc8) == 0x08) {
  1263. if (psmouse->pktcnt == 3) {
  1264. alps_report_bare_ps2_packet(psmouse, psmouse->packet,
  1265. true);
  1266. return PSMOUSE_FULL_PACKET;
  1267. }
  1268. return PSMOUSE_GOOD_DATA;
  1269. }
  1270. /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
  1271. if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
  1272. psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
  1273. return alps_handle_interleaved_ps2(psmouse);
  1274. }
  1275. if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
  1276. psmouse_dbg(psmouse,
  1277. "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
  1278. psmouse->packet[0], priv->mask0, priv->byte0);
  1279. return PSMOUSE_BAD_DATA;
  1280. }
  1281. /* Bytes 2 - pktsize should have 0 in the highest bit */
  1282. if (priv->proto_version < ALPS_PROTO_V5 &&
  1283. psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
  1284. (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
  1285. psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
  1286. psmouse->pktcnt - 1,
  1287. psmouse->packet[psmouse->pktcnt - 1]);
  1288. if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
  1289. psmouse->pktcnt == psmouse->pktsize) {
  1290. /*
  1291. * Some Dell boxes, such as Latitude E6440 or E7440
  1292. * with closed lid, quite often smash last byte of
  1293. * otherwise valid packet with 0xff. Given that the
  1294. * next packet is very likely to be valid let's
  1295. * report PSMOUSE_FULL_PACKET but not process data,
  1296. * rather than reporting PSMOUSE_BAD_DATA and
  1297. * filling the logs.
  1298. */
  1299. return PSMOUSE_FULL_PACKET;
  1300. }
  1301. return PSMOUSE_BAD_DATA;
  1302. }
  1303. if ((priv->proto_version == ALPS_PROTO_V7 &&
  1304. !alps_is_valid_package_v7(psmouse)) ||
  1305. (priv->proto_version == ALPS_PROTO_V8 &&
  1306. !alps_is_valid_package_ss4_v2(psmouse))) {
  1307. psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
  1308. psmouse->pktcnt - 1,
  1309. psmouse->packet[psmouse->pktcnt - 1]);
  1310. return PSMOUSE_BAD_DATA;
  1311. }
  1312. if (psmouse->pktcnt == psmouse->pktsize) {
  1313. priv->process_packet(psmouse);
  1314. return PSMOUSE_FULL_PACKET;
  1315. }
  1316. return PSMOUSE_GOOD_DATA;
  1317. }
  1318. static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
  1319. {
  1320. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1321. struct alps_data *priv = psmouse->private;
  1322. int command;
  1323. unsigned char *param;
  1324. unsigned char dummy[4];
  1325. BUG_ON(nibble > 0xf);
  1326. command = priv->nibble_commands[nibble].command;
  1327. param = (command & 0x0f00) ?
  1328. dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
  1329. if (ps2_command(ps2dev, param, command))
  1330. return -1;
  1331. return 0;
  1332. }
  1333. static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
  1334. {
  1335. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1336. struct alps_data *priv = psmouse->private;
  1337. int i, nibble;
  1338. if (ps2_command(ps2dev, NULL, priv->addr_command))
  1339. return -1;
  1340. for (i = 12; i >= 0; i -= 4) {
  1341. nibble = (addr >> i) & 0xf;
  1342. if (alps_command_mode_send_nibble(psmouse, nibble))
  1343. return -1;
  1344. }
  1345. return 0;
  1346. }
  1347. static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
  1348. {
  1349. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1350. unsigned char param[4];
  1351. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  1352. return -1;
  1353. /*
  1354. * The address being read is returned in the first two bytes
  1355. * of the result. Check that this address matches the expected
  1356. * address.
  1357. */
  1358. if (addr != ((param[0] << 8) | param[1]))
  1359. return -1;
  1360. return param[2];
  1361. }
  1362. static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
  1363. {
  1364. if (alps_command_mode_set_addr(psmouse, addr))
  1365. return -1;
  1366. return __alps_command_mode_read_reg(psmouse, addr);
  1367. }
  1368. static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
  1369. {
  1370. if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
  1371. return -1;
  1372. if (alps_command_mode_send_nibble(psmouse, value & 0xf))
  1373. return -1;
  1374. return 0;
  1375. }
  1376. static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
  1377. u8 value)
  1378. {
  1379. if (alps_command_mode_set_addr(psmouse, addr))
  1380. return -1;
  1381. return __alps_command_mode_write_reg(psmouse, value);
  1382. }
  1383. static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
  1384. int repeated_command, unsigned char *param)
  1385. {
  1386. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1387. param[0] = 0;
  1388. if (init_command && ps2_command(ps2dev, param, init_command))
  1389. return -EIO;
  1390. if (ps2_command(ps2dev, NULL, repeated_command) ||
  1391. ps2_command(ps2dev, NULL, repeated_command) ||
  1392. ps2_command(ps2dev, NULL, repeated_command))
  1393. return -EIO;
  1394. param[0] = param[1] = param[2] = 0xff;
  1395. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  1396. return -EIO;
  1397. psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
  1398. repeated_command, param);
  1399. return 0;
  1400. }
  1401. static bool alps_check_valid_firmware_id(unsigned char id[])
  1402. {
  1403. if (id[0] == 0x73)
  1404. return true;
  1405. if (id[0] == 0x88 &&
  1406. (id[1] == 0x07 ||
  1407. id[1] == 0x08 ||
  1408. (id[1] & 0xf0) == 0xb0 ||
  1409. (id[1] & 0xf0) == 0xc0)) {
  1410. return true;
  1411. }
  1412. return false;
  1413. }
  1414. static int alps_enter_command_mode(struct psmouse *psmouse)
  1415. {
  1416. unsigned char param[4];
  1417. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
  1418. psmouse_err(psmouse, "failed to enter command mode\n");
  1419. return -1;
  1420. }
  1421. if (!alps_check_valid_firmware_id(param)) {
  1422. psmouse_dbg(psmouse,
  1423. "unknown response while entering command mode\n");
  1424. return -1;
  1425. }
  1426. return 0;
  1427. }
  1428. static inline int alps_exit_command_mode(struct psmouse *psmouse)
  1429. {
  1430. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1431. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
  1432. return -1;
  1433. return 0;
  1434. }
  1435. /*
  1436. * For DualPoint devices select the device that should respond to
  1437. * subsequent commands. It looks like glidepad is behind stickpointer,
  1438. * I'd thought it would be other way around...
  1439. */
  1440. static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
  1441. {
  1442. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1443. int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
  1444. if (ps2_command(ps2dev, NULL, cmd) ||
  1445. ps2_command(ps2dev, NULL, cmd) ||
  1446. ps2_command(ps2dev, NULL, cmd) ||
  1447. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  1448. return -1;
  1449. /* we may get 3 more bytes, just ignore them */
  1450. ps2_drain(ps2dev, 3, 100);
  1451. return 0;
  1452. }
  1453. static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
  1454. {
  1455. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1456. /* Try ALPS magic knock - 4 disable before enable */
  1457. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1458. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1459. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1460. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1461. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
  1462. return -1;
  1463. /*
  1464. * Switch mouse to poll (remote) mode so motion data will not
  1465. * get in our way
  1466. */
  1467. return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
  1468. }
  1469. static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
  1470. {
  1471. int i, nibble;
  1472. /*
  1473. * b0-b11 are valid bits, send sequence is inverse.
  1474. * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
  1475. */
  1476. for (i = 0; i <= 8; i += 4) {
  1477. nibble = (word >> i) & 0xf;
  1478. if (alps_command_mode_send_nibble(psmouse, nibble))
  1479. return -1;
  1480. }
  1481. return 0;
  1482. }
  1483. static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
  1484. u16 addr, u16 value)
  1485. {
  1486. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1487. /* 0x0A0 is the command to write the word */
  1488. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
  1489. alps_monitor_mode_send_word(psmouse, 0x0A0) ||
  1490. alps_monitor_mode_send_word(psmouse, addr) ||
  1491. alps_monitor_mode_send_word(psmouse, value) ||
  1492. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  1493. return -1;
  1494. return 0;
  1495. }
  1496. static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
  1497. {
  1498. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1499. if (enable) {
  1500. /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
  1501. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
  1502. ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
  1503. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1504. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1505. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  1506. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1507. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  1508. ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
  1509. return -1;
  1510. } else {
  1511. /* EC to exit monitor mode */
  1512. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
  1513. return -1;
  1514. }
  1515. return 0;
  1516. }
  1517. static int alps_absolute_mode_v6(struct psmouse *psmouse)
  1518. {
  1519. u16 reg_val = 0x181;
  1520. int ret = -1;
  1521. /* enter monitor mode, to write the register */
  1522. if (alps_monitor_mode(psmouse, true))
  1523. return -1;
  1524. ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
  1525. if (alps_monitor_mode(psmouse, false))
  1526. ret = -1;
  1527. return ret;
  1528. }
  1529. static int alps_get_status(struct psmouse *psmouse, char *param)
  1530. {
  1531. /* Get status: 0xF5 0xF5 0xF5 0xE9 */
  1532. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
  1533. return -1;
  1534. return 0;
  1535. }
  1536. /*
  1537. * Turn touchpad tapping on or off. The sequences are:
  1538. * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
  1539. * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
  1540. * My guess that 0xE9 (GetInfo) is here as a sync point.
  1541. * For models that also have stickpointer (DualPoints) its tapping
  1542. * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
  1543. * we don't fiddle with it.
  1544. */
  1545. static int alps_tap_mode(struct psmouse *psmouse, int enable)
  1546. {
  1547. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1548. int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
  1549. unsigned char tap_arg = enable ? 0x0A : 0x00;
  1550. unsigned char param[4];
  1551. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
  1552. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1553. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1554. ps2_command(ps2dev, &tap_arg, cmd))
  1555. return -1;
  1556. if (alps_get_status(psmouse, param))
  1557. return -1;
  1558. return 0;
  1559. }
  1560. /*
  1561. * alps_poll() - poll the touchpad for current motion packet.
  1562. * Used in resync.
  1563. */
  1564. static int alps_poll(struct psmouse *psmouse)
  1565. {
  1566. struct alps_data *priv = psmouse->private;
  1567. unsigned char buf[sizeof(psmouse->packet)];
  1568. bool poll_failed;
  1569. if (priv->flags & ALPS_PASS)
  1570. alps_passthrough_mode_v2(psmouse, true);
  1571. poll_failed = ps2_command(&psmouse->ps2dev, buf,
  1572. PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
  1573. if (priv->flags & ALPS_PASS)
  1574. alps_passthrough_mode_v2(psmouse, false);
  1575. if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
  1576. return -1;
  1577. if ((psmouse->badbyte & 0xc8) == 0x08) {
  1578. /*
  1579. * Poll the track stick ...
  1580. */
  1581. if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
  1582. return -1;
  1583. }
  1584. memcpy(psmouse->packet, buf, sizeof(buf));
  1585. return 0;
  1586. }
  1587. static int alps_hw_init_v1_v2(struct psmouse *psmouse)
  1588. {
  1589. struct alps_data *priv = psmouse->private;
  1590. if ((priv->flags & ALPS_PASS) &&
  1591. alps_passthrough_mode_v2(psmouse, true)) {
  1592. return -1;
  1593. }
  1594. if (alps_tap_mode(psmouse, true)) {
  1595. psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
  1596. return -1;
  1597. }
  1598. if (alps_absolute_mode_v1_v2(psmouse)) {
  1599. psmouse_err(psmouse, "Failed to enable absolute mode\n");
  1600. return -1;
  1601. }
  1602. if ((priv->flags & ALPS_PASS) &&
  1603. alps_passthrough_mode_v2(psmouse, false)) {
  1604. return -1;
  1605. }
  1606. /* ALPS needs stream mode, otherwise it won't report any data */
  1607. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
  1608. psmouse_err(psmouse, "Failed to enable stream mode\n");
  1609. return -1;
  1610. }
  1611. return 0;
  1612. }
  1613. static int alps_hw_init_v6(struct psmouse *psmouse)
  1614. {
  1615. unsigned char param[2] = {0xC8, 0x14};
  1616. /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
  1617. if (alps_passthrough_mode_v2(psmouse, true))
  1618. return -1;
  1619. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1620. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1621. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1622. ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1623. ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
  1624. return -1;
  1625. if (alps_passthrough_mode_v2(psmouse, false))
  1626. return -1;
  1627. if (alps_absolute_mode_v6(psmouse)) {
  1628. psmouse_err(psmouse, "Failed to enable absolute mode\n");
  1629. return -1;
  1630. }
  1631. return 0;
  1632. }
  1633. /*
  1634. * Enable or disable passthrough mode to the trackstick.
  1635. */
  1636. static int alps_passthrough_mode_v3(struct psmouse *psmouse,
  1637. int reg_base, bool enable)
  1638. {
  1639. int reg_val, ret = -1;
  1640. if (alps_enter_command_mode(psmouse))
  1641. return -1;
  1642. reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
  1643. if (reg_val == -1)
  1644. goto error;
  1645. if (enable)
  1646. reg_val |= 0x01;
  1647. else
  1648. reg_val &= ~0x01;
  1649. ret = __alps_command_mode_write_reg(psmouse, reg_val);
  1650. error:
  1651. if (alps_exit_command_mode(psmouse))
  1652. ret = -1;
  1653. return ret;
  1654. }
  1655. /* Must be in command mode when calling this function */
  1656. static int alps_absolute_mode_v3(struct psmouse *psmouse)
  1657. {
  1658. int reg_val;
  1659. reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
  1660. if (reg_val == -1)
  1661. return -1;
  1662. reg_val |= 0x06;
  1663. if (__alps_command_mode_write_reg(psmouse, reg_val))
  1664. return -1;
  1665. return 0;
  1666. }
  1667. static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
  1668. {
  1669. int ret = -EIO, reg_val;
  1670. if (alps_enter_command_mode(psmouse))
  1671. goto error;
  1672. reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
  1673. if (reg_val == -1)
  1674. goto error;
  1675. /* bit 7: trackstick is present */
  1676. ret = reg_val & 0x80 ? 0 : -ENODEV;
  1677. error:
  1678. alps_exit_command_mode(psmouse);
  1679. return ret;
  1680. }
  1681. static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
  1682. {
  1683. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1684. int ret = 0;
  1685. unsigned char param[4];
  1686. if (alps_passthrough_mode_v3(psmouse, reg_base, true))
  1687. return -EIO;
  1688. /*
  1689. * E7 report for the trackstick
  1690. *
  1691. * There have been reports of failures to seem to trace back
  1692. * to the above trackstick check failing. When these occur
  1693. * this E7 report fails, so when that happens we continue
  1694. * with the assumption that there isn't a trackstick after
  1695. * all.
  1696. */
  1697. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
  1698. psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
  1699. ret = -ENODEV;
  1700. } else {
  1701. psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
  1702. /*
  1703. * Not sure what this does, but it is absolutely
  1704. * essential. Without it, the touchpad does not
  1705. * work at all and the trackstick just emits normal
  1706. * PS/2 packets.
  1707. */
  1708. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1709. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1710. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1711. alps_command_mode_send_nibble(psmouse, 0x9) ||
  1712. alps_command_mode_send_nibble(psmouse, 0x4)) {
  1713. psmouse_err(psmouse,
  1714. "Error sending magic E6 sequence\n");
  1715. ret = -EIO;
  1716. goto error;
  1717. }
  1718. /*
  1719. * This ensures the trackstick packets are in the format
  1720. * supported by this driver. If bit 1 isn't set the packet
  1721. * format is different.
  1722. */
  1723. if (alps_enter_command_mode(psmouse) ||
  1724. alps_command_mode_write_reg(psmouse,
  1725. reg_base + 0x08, 0x82) ||
  1726. alps_exit_command_mode(psmouse))
  1727. ret = -EIO;
  1728. }
  1729. error:
  1730. if (alps_passthrough_mode_v3(psmouse, reg_base, false))
  1731. ret = -EIO;
  1732. return ret;
  1733. }
  1734. static int alps_hw_init_v3(struct psmouse *psmouse)
  1735. {
  1736. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1737. int reg_val;
  1738. unsigned char param[4];
  1739. reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
  1740. if (reg_val == -EIO)
  1741. goto error;
  1742. if (reg_val == 0 &&
  1743. alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
  1744. goto error;
  1745. if (alps_enter_command_mode(psmouse) ||
  1746. alps_absolute_mode_v3(psmouse)) {
  1747. psmouse_err(psmouse, "Failed to enter absolute mode\n");
  1748. goto error;
  1749. }
  1750. reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
  1751. if (reg_val == -1)
  1752. goto error;
  1753. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
  1754. goto error;
  1755. reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
  1756. if (reg_val == -1)
  1757. goto error;
  1758. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
  1759. goto error;
  1760. if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
  1761. goto error;
  1762. if (__alps_command_mode_write_reg(psmouse, 0x04))
  1763. goto error;
  1764. if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
  1765. goto error;
  1766. if (__alps_command_mode_write_reg(psmouse, 0x03))
  1767. goto error;
  1768. if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
  1769. goto error;
  1770. if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
  1771. goto error;
  1772. if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
  1773. goto error;
  1774. if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
  1775. goto error;
  1776. alps_exit_command_mode(psmouse);
  1777. /* Set rate and enable data reporting */
  1778. param[0] = 0x64;
  1779. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
  1780. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  1781. psmouse_err(psmouse, "Failed to enable data reporting\n");
  1782. return -1;
  1783. }
  1784. return 0;
  1785. error:
  1786. /*
  1787. * Leaving the touchpad in command mode will essentially render
  1788. * it unusable until the machine reboots, so exit it here just
  1789. * to be safe
  1790. */
  1791. alps_exit_command_mode(psmouse);
  1792. return -1;
  1793. }
  1794. static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
  1795. {
  1796. int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
  1797. struct alps_data *priv = psmouse->private;
  1798. reg = alps_command_mode_read_reg(psmouse, reg_pitch);
  1799. if (reg < 0)
  1800. return reg;
  1801. x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
  1802. x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
  1803. y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
  1804. y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
  1805. reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
  1806. if (reg < 0)
  1807. return reg;
  1808. x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
  1809. x_electrode = 17 + x_electrode;
  1810. y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
  1811. y_electrode = 13 + y_electrode;
  1812. x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
  1813. y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
  1814. priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
  1815. priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
  1816. psmouse_dbg(psmouse,
  1817. "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
  1818. x_pitch, y_pitch, x_electrode, y_electrode,
  1819. x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
  1820. return 0;
  1821. }
  1822. static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
  1823. {
  1824. struct alps_data *priv = psmouse->private;
  1825. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1826. int reg_val, ret = -1;
  1827. if (priv->flags & ALPS_DUALPOINT) {
  1828. reg_val = alps_setup_trackstick_v3(psmouse,
  1829. ALPS_REG_BASE_RUSHMORE);
  1830. if (reg_val == -EIO)
  1831. goto error;
  1832. }
  1833. if (alps_enter_command_mode(psmouse) ||
  1834. alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
  1835. alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
  1836. goto error;
  1837. if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
  1838. goto error;
  1839. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
  1840. if (reg_val == -1)
  1841. goto error;
  1842. if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
  1843. goto error;
  1844. if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
  1845. goto error;
  1846. /* enter absolute mode */
  1847. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
  1848. if (reg_val == -1)
  1849. goto error;
  1850. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
  1851. goto error;
  1852. alps_exit_command_mode(psmouse);
  1853. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  1854. error:
  1855. alps_exit_command_mode(psmouse);
  1856. return ret;
  1857. }
  1858. /* Must be in command mode when calling this function */
  1859. static int alps_absolute_mode_v4(struct psmouse *psmouse)
  1860. {
  1861. int reg_val;
  1862. reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
  1863. if (reg_val == -1)
  1864. return -1;
  1865. reg_val |= 0x02;
  1866. if (__alps_command_mode_write_reg(psmouse, reg_val))
  1867. return -1;
  1868. return 0;
  1869. }
  1870. static int alps_hw_init_v4(struct psmouse *psmouse)
  1871. {
  1872. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1873. unsigned char param[4];
  1874. if (alps_enter_command_mode(psmouse))
  1875. goto error;
  1876. if (alps_absolute_mode_v4(psmouse)) {
  1877. psmouse_err(psmouse, "Failed to enter absolute mode\n");
  1878. goto error;
  1879. }
  1880. if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
  1881. goto error;
  1882. if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
  1883. goto error;
  1884. if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
  1885. goto error;
  1886. if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
  1887. goto error;
  1888. if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
  1889. goto error;
  1890. if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
  1891. goto error;
  1892. if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
  1893. goto error;
  1894. if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
  1895. goto error;
  1896. alps_exit_command_mode(psmouse);
  1897. /*
  1898. * This sequence changes the output from a 9-byte to an
  1899. * 8-byte format. All the same data seems to be present,
  1900. * just in a more compact format.
  1901. */
  1902. param[0] = 0xc8;
  1903. param[1] = 0x64;
  1904. param[2] = 0x50;
  1905. if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1906. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
  1907. ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
  1908. ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
  1909. return -1;
  1910. /* Set rate and enable data reporting */
  1911. param[0] = 0x64;
  1912. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
  1913. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  1914. psmouse_err(psmouse, "Failed to enable data reporting\n");
  1915. return -1;
  1916. }
  1917. return 0;
  1918. error:
  1919. /*
  1920. * Leaving the touchpad in command mode will essentially render
  1921. * it unusable until the machine reboots, so exit it here just
  1922. * to be safe
  1923. */
  1924. alps_exit_command_mode(psmouse);
  1925. return -1;
  1926. }
  1927. static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
  1928. unsigned char index, unsigned char otp[])
  1929. {
  1930. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1931. switch (index) {
  1932. case 0:
  1933. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  1934. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  1935. ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
  1936. return -1;
  1937. break;
  1938. case 1:
  1939. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  1940. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  1941. ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
  1942. return -1;
  1943. break;
  1944. }
  1945. return 0;
  1946. }
  1947. static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
  1948. struct alps_data *priv)
  1949. {
  1950. int num_x_electrode;
  1951. int num_y_electrode;
  1952. int x_pitch, y_pitch, x_phys, y_phys;
  1953. num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
  1954. num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
  1955. priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
  1956. priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
  1957. x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
  1958. y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
  1959. x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
  1960. y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
  1961. priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
  1962. priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
  1963. return 0;
  1964. }
  1965. static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
  1966. struct alps_data *priv)
  1967. {
  1968. unsigned char is_btnless;
  1969. is_btnless = (otp[1][1] >> 3) & 0x01;
  1970. if (is_btnless)
  1971. priv->flags |= ALPS_BUTTONPAD;
  1972. return 0;
  1973. }
  1974. static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
  1975. struct alps_data *priv)
  1976. {
  1977. unsigned char otp[2][4];
  1978. memset(otp, 0, sizeof(otp));
  1979. if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
  1980. alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
  1981. return -1;
  1982. alps_update_device_area_ss4_v2(otp, priv);
  1983. alps_update_btn_info_ss4_v2(otp, priv);
  1984. return 0;
  1985. }
  1986. static int alps_dolphin_get_device_area(struct psmouse *psmouse,
  1987. struct alps_data *priv)
  1988. {
  1989. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1990. unsigned char param[4] = {0};
  1991. int num_x_electrode, num_y_electrode;
  1992. if (alps_enter_command_mode(psmouse))
  1993. return -1;
  1994. param[0] = 0x0a;
  1995. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
  1996. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  1997. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  1998. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1999. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
  2000. return -1;
  2001. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  2002. return -1;
  2003. /*
  2004. * Dolphin's sensor line number is not fixed. It can be calculated
  2005. * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
  2006. * Further more, we can get device's x_max and y_max by multiplying
  2007. * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
  2008. *
  2009. * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
  2010. * real sensor line number X = 11 + 8 = 19, and
  2011. * real sensor line number Y = 8 + 1 = 9.
  2012. * So, x_max = (19 - 1) * 64 = 1152, and
  2013. * y_max = (9 - 1) * 64 = 512.
  2014. */
  2015. num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
  2016. num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
  2017. priv->x_bits = num_x_electrode;
  2018. priv->y_bits = num_y_electrode;
  2019. priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
  2020. priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
  2021. if (alps_exit_command_mode(psmouse))
  2022. return -1;
  2023. return 0;
  2024. }
  2025. static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
  2026. {
  2027. struct ps2dev *ps2dev = &psmouse->ps2dev;
  2028. unsigned char param[2];
  2029. /* This is dolphin "v1" as empirically defined by florin9doi */
  2030. param[0] = 0x64;
  2031. param[1] = 0x28;
  2032. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  2033. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  2034. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
  2035. return -1;
  2036. return 0;
  2037. }
  2038. static int alps_hw_init_v7(struct psmouse *psmouse)
  2039. {
  2040. struct ps2dev *ps2dev = &psmouse->ps2dev;
  2041. int reg_val, ret = -1;
  2042. if (alps_enter_command_mode(psmouse) ||
  2043. alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
  2044. goto error;
  2045. if (alps_get_v3_v7_resolution(psmouse, 0xc397))
  2046. goto error;
  2047. if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
  2048. goto error;
  2049. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
  2050. if (reg_val == -1)
  2051. goto error;
  2052. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
  2053. goto error;
  2054. alps_exit_command_mode(psmouse);
  2055. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  2056. error:
  2057. alps_exit_command_mode(psmouse);
  2058. return ret;
  2059. }
  2060. static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
  2061. {
  2062. struct ps2dev *ps2dev = &psmouse->ps2dev;
  2063. char param[2] = {0x64, 0x28};
  2064. int ret = -1;
  2065. /* enter absolute mode */
  2066. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  2067. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  2068. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  2069. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
  2070. goto error;
  2071. }
  2072. /* T.B.D. Decread noise packet number, delete in the future */
  2073. if (alps_exit_command_mode(psmouse) ||
  2074. alps_enter_command_mode(psmouse) ||
  2075. alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
  2076. goto error;
  2077. }
  2078. alps_exit_command_mode(psmouse);
  2079. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  2080. error:
  2081. alps_exit_command_mode(psmouse);
  2082. return ret;
  2083. }
  2084. static int alps_set_protocol(struct psmouse *psmouse,
  2085. struct alps_data *priv,
  2086. const struct alps_protocol_info *protocol)
  2087. {
  2088. psmouse->private = priv;
  2089. setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
  2090. priv->proto_version = protocol->version;
  2091. priv->byte0 = protocol->byte0;
  2092. priv->mask0 = protocol->mask0;
  2093. priv->flags = protocol->flags;
  2094. priv->x_max = 2000;
  2095. priv->y_max = 1400;
  2096. priv->x_bits = 15;
  2097. priv->y_bits = 11;
  2098. switch (priv->proto_version) {
  2099. case ALPS_PROTO_V1:
  2100. case ALPS_PROTO_V2:
  2101. priv->hw_init = alps_hw_init_v1_v2;
  2102. priv->process_packet = alps_process_packet_v1_v2;
  2103. priv->set_abs_params = alps_set_abs_params_st;
  2104. priv->x_max = 1023;
  2105. priv->y_max = 767;
  2106. break;
  2107. case ALPS_PROTO_V3:
  2108. priv->hw_init = alps_hw_init_v3;
  2109. priv->process_packet = alps_process_packet_v3;
  2110. priv->set_abs_params = alps_set_abs_params_mt;
  2111. priv->decode_fields = alps_decode_pinnacle;
  2112. priv->nibble_commands = alps_v3_nibble_commands;
  2113. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2114. break;
  2115. case ALPS_PROTO_V3_RUSHMORE:
  2116. priv->hw_init = alps_hw_init_rushmore_v3;
  2117. priv->process_packet = alps_process_packet_v3;
  2118. priv->set_abs_params = alps_set_abs_params_mt;
  2119. priv->decode_fields = alps_decode_rushmore;
  2120. priv->nibble_commands = alps_v3_nibble_commands;
  2121. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2122. priv->x_bits = 16;
  2123. priv->y_bits = 12;
  2124. if (alps_probe_trackstick_v3(psmouse,
  2125. ALPS_REG_BASE_RUSHMORE) < 0)
  2126. priv->flags &= ~ALPS_DUALPOINT;
  2127. break;
  2128. case ALPS_PROTO_V4:
  2129. priv->hw_init = alps_hw_init_v4;
  2130. priv->process_packet = alps_process_packet_v4;
  2131. priv->set_abs_params = alps_set_abs_params_mt;
  2132. priv->nibble_commands = alps_v4_nibble_commands;
  2133. priv->addr_command = PSMOUSE_CMD_DISABLE;
  2134. break;
  2135. case ALPS_PROTO_V5:
  2136. priv->hw_init = alps_hw_init_dolphin_v1;
  2137. priv->process_packet = alps_process_touchpad_packet_v3_v5;
  2138. priv->decode_fields = alps_decode_dolphin;
  2139. priv->set_abs_params = alps_set_abs_params_mt;
  2140. priv->nibble_commands = alps_v3_nibble_commands;
  2141. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2142. priv->x_bits = 23;
  2143. priv->y_bits = 12;
  2144. if (alps_dolphin_get_device_area(psmouse, priv))
  2145. return -EIO;
  2146. break;
  2147. case ALPS_PROTO_V6:
  2148. priv->hw_init = alps_hw_init_v6;
  2149. priv->process_packet = alps_process_packet_v6;
  2150. priv->set_abs_params = alps_set_abs_params_st;
  2151. priv->nibble_commands = alps_v6_nibble_commands;
  2152. priv->x_max = 2047;
  2153. priv->y_max = 1535;
  2154. break;
  2155. case ALPS_PROTO_V7:
  2156. priv->hw_init = alps_hw_init_v7;
  2157. priv->process_packet = alps_process_packet_v7;
  2158. priv->decode_fields = alps_decode_packet_v7;
  2159. priv->set_abs_params = alps_set_abs_params_v7;
  2160. priv->nibble_commands = alps_v3_nibble_commands;
  2161. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2162. priv->x_max = 0xfff;
  2163. priv->y_max = 0x7ff;
  2164. if (priv->fw_ver[1] != 0xba)
  2165. priv->flags |= ALPS_BUTTONPAD;
  2166. break;
  2167. case ALPS_PROTO_V8:
  2168. priv->hw_init = alps_hw_init_ss4_v2;
  2169. priv->process_packet = alps_process_packet_ss4_v2;
  2170. priv->decode_fields = alps_decode_ss4_v2;
  2171. priv->set_abs_params = alps_set_abs_params_ss4_v2;
  2172. priv->nibble_commands = alps_v3_nibble_commands;
  2173. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2174. if (alps_set_defaults_ss4_v2(psmouse, priv))
  2175. return -EIO;
  2176. break;
  2177. }
  2178. return 0;
  2179. }
  2180. static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
  2181. unsigned char *ec)
  2182. {
  2183. const struct alps_model_info *model;
  2184. int i;
  2185. for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
  2186. model = &alps_model_data[i];
  2187. if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
  2188. (!model->command_mode_resp ||
  2189. model->command_mode_resp == ec[2])) {
  2190. return &model->protocol_info;
  2191. }
  2192. }
  2193. return NULL;
  2194. }
  2195. static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
  2196. {
  2197. const struct alps_protocol_info *protocol;
  2198. unsigned char e6[4], e7[4], ec[4];
  2199. int error;
  2200. /*
  2201. * First try "E6 report".
  2202. * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
  2203. * The bits 0-2 of the first byte will be 1s if some buttons are
  2204. * pressed.
  2205. */
  2206. if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  2207. PSMOUSE_CMD_SETSCALE11, e6))
  2208. return -EIO;
  2209. if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
  2210. return -EINVAL;
  2211. /*
  2212. * Now get the "E7" and "EC" reports. These will uniquely identify
  2213. * most ALPS touchpads.
  2214. */
  2215. if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  2216. PSMOUSE_CMD_SETSCALE21, e7) ||
  2217. alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  2218. PSMOUSE_CMD_RESET_WRAP, ec) ||
  2219. alps_exit_command_mode(psmouse))
  2220. return -EIO;
  2221. protocol = alps_match_table(e7, ec);
  2222. if (!protocol) {
  2223. if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
  2224. ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
  2225. protocol = &alps_v5_protocol_data;
  2226. } else if (ec[0] == 0x88 &&
  2227. ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
  2228. protocol = &alps_v7_protocol_data;
  2229. } else if (ec[0] == 0x88 && ec[1] == 0x08) {
  2230. protocol = &alps_v3_rushmore_data;
  2231. } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
  2232. ec[2] >= 0x90 && ec[2] <= 0x9d) {
  2233. protocol = &alps_v3_protocol_data;
  2234. } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
  2235. e7[2] == 0x14 && ec[1] == 0x02) {
  2236. protocol = &alps_v8_protocol_data;
  2237. } else {
  2238. psmouse_dbg(psmouse,
  2239. "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
  2240. return -EINVAL;
  2241. }
  2242. }
  2243. if (priv) {
  2244. /* Save the Firmware version */
  2245. memcpy(priv->fw_ver, ec, 3);
  2246. error = alps_set_protocol(psmouse, priv, protocol);
  2247. if (error)
  2248. return error;
  2249. }
  2250. return 0;
  2251. }
  2252. static int alps_reconnect(struct psmouse *psmouse)
  2253. {
  2254. struct alps_data *priv = psmouse->private;
  2255. psmouse_reset(psmouse);
  2256. if (alps_identify(psmouse, priv) < 0)
  2257. return -1;
  2258. return priv->hw_init(psmouse);
  2259. }
  2260. static void alps_disconnect(struct psmouse *psmouse)
  2261. {
  2262. struct alps_data *priv = psmouse->private;
  2263. psmouse_reset(psmouse);
  2264. del_timer_sync(&priv->timer);
  2265. if (priv->dev2)
  2266. input_unregister_device(priv->dev2);
  2267. if (!IS_ERR_OR_NULL(priv->dev3))
  2268. input_unregister_device(priv->dev3);
  2269. kfree(priv);
  2270. }
  2271. static void alps_set_abs_params_st(struct alps_data *priv,
  2272. struct input_dev *dev1)
  2273. {
  2274. input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
  2275. input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
  2276. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  2277. }
  2278. static void alps_set_abs_params_mt_common(struct alps_data *priv,
  2279. struct input_dev *dev1)
  2280. {
  2281. input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
  2282. input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
  2283. input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
  2284. input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
  2285. set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
  2286. set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
  2287. }
  2288. static void alps_set_abs_params_mt(struct alps_data *priv,
  2289. struct input_dev *dev1)
  2290. {
  2291. alps_set_abs_params_mt_common(priv, dev1);
  2292. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  2293. input_mt_init_slots(dev1, MAX_TOUCHES,
  2294. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
  2295. INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
  2296. }
  2297. static void alps_set_abs_params_v7(struct alps_data *priv,
  2298. struct input_dev *dev1)
  2299. {
  2300. alps_set_abs_params_mt_common(priv, dev1);
  2301. set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
  2302. input_mt_init_slots(dev1, MAX_TOUCHES,
  2303. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
  2304. INPUT_MT_TRACK);
  2305. set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
  2306. }
  2307. static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
  2308. struct input_dev *dev1)
  2309. {
  2310. alps_set_abs_params_mt_common(priv, dev1);
  2311. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  2312. set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
  2313. input_mt_init_slots(dev1, MAX_TOUCHES,
  2314. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
  2315. INPUT_MT_TRACK);
  2316. }
  2317. int alps_init(struct psmouse *psmouse)
  2318. {
  2319. struct alps_data *priv = psmouse->private;
  2320. struct input_dev *dev1 = psmouse->dev;
  2321. int error;
  2322. error = priv->hw_init(psmouse);
  2323. if (error)
  2324. goto init_fail;
  2325. /*
  2326. * Undo part of setup done for us by psmouse core since touchpad
  2327. * is not a relative device.
  2328. */
  2329. __clear_bit(EV_REL, dev1->evbit);
  2330. __clear_bit(REL_X, dev1->relbit);
  2331. __clear_bit(REL_Y, dev1->relbit);
  2332. /*
  2333. * Now set up our capabilities.
  2334. */
  2335. dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
  2336. dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
  2337. dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
  2338. dev1->keybit[BIT_WORD(BTN_LEFT)] |=
  2339. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
  2340. dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
  2341. priv->set_abs_params(priv, dev1);
  2342. if (priv->flags & ALPS_WHEEL) {
  2343. dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
  2344. dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
  2345. }
  2346. if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  2347. dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
  2348. dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
  2349. }
  2350. if (priv->flags & ALPS_FOUR_BUTTONS) {
  2351. dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
  2352. dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
  2353. dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
  2354. dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
  2355. } else if (priv->flags & ALPS_BUTTONPAD) {
  2356. set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
  2357. clear_bit(BTN_RIGHT, dev1->keybit);
  2358. } else {
  2359. dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
  2360. }
  2361. if (priv->flags & ALPS_DUALPOINT) {
  2362. struct input_dev *dev2;
  2363. dev2 = input_allocate_device();
  2364. if (!dev2) {
  2365. psmouse_err(psmouse,
  2366. "failed to allocate trackstick device\n");
  2367. error = -ENOMEM;
  2368. goto init_fail;
  2369. }
  2370. snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
  2371. psmouse->ps2dev.serio->phys);
  2372. dev2->phys = priv->phys2;
  2373. /*
  2374. * format of input device name is: "protocol vendor name"
  2375. * see function psmouse_switch_protocol() in psmouse-base.c
  2376. */
  2377. dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
  2378. dev2->id.bustype = BUS_I8042;
  2379. dev2->id.vendor = 0x0002;
  2380. dev2->id.product = PSMOUSE_ALPS;
  2381. dev2->id.version = priv->proto_version;
  2382. dev2->dev.parent = &psmouse->ps2dev.serio->dev;
  2383. input_set_capability(dev2, EV_REL, REL_X);
  2384. input_set_capability(dev2, EV_REL, REL_Y);
  2385. input_set_capability(dev2, EV_KEY, BTN_LEFT);
  2386. input_set_capability(dev2, EV_KEY, BTN_RIGHT);
  2387. input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
  2388. __set_bit(INPUT_PROP_POINTER, dev2->propbit);
  2389. __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
  2390. error = input_register_device(dev2);
  2391. if (error) {
  2392. psmouse_err(psmouse,
  2393. "failed to register trackstick device: %d\n",
  2394. error);
  2395. input_free_device(dev2);
  2396. goto init_fail;
  2397. }
  2398. priv->dev2 = dev2;
  2399. }
  2400. priv->psmouse = psmouse;
  2401. INIT_DELAYED_WORK(&priv->dev3_register_work,
  2402. alps_register_bare_ps2_mouse);
  2403. psmouse->protocol_handler = alps_process_byte;
  2404. psmouse->poll = alps_poll;
  2405. psmouse->disconnect = alps_disconnect;
  2406. psmouse->reconnect = alps_reconnect;
  2407. psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
  2408. /* We are having trouble resyncing ALPS touchpads so disable it for now */
  2409. psmouse->resync_time = 0;
  2410. /* Allow 2 invalid packets without resetting device */
  2411. psmouse->resetafter = psmouse->pktsize * 2;
  2412. return 0;
  2413. init_fail:
  2414. psmouse_reset(psmouse);
  2415. /*
  2416. * Even though we did not allocate psmouse->private we do free
  2417. * it here.
  2418. */
  2419. kfree(psmouse->private);
  2420. psmouse->private = NULL;
  2421. return error;
  2422. }
  2423. int alps_detect(struct psmouse *psmouse, bool set_properties)
  2424. {
  2425. struct alps_data *priv;
  2426. int error;
  2427. error = alps_identify(psmouse, NULL);
  2428. if (error)
  2429. return error;
  2430. /*
  2431. * Reset the device to make sure it is fully operational:
  2432. * on some laptops, like certain Dell Latitudes, we may
  2433. * fail to properly detect presence of trackstick if device
  2434. * has not been reset.
  2435. */
  2436. psmouse_reset(psmouse);
  2437. priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
  2438. if (!priv)
  2439. return -ENOMEM;
  2440. error = alps_identify(psmouse, priv);
  2441. if (error) {
  2442. kfree(priv);
  2443. return error;
  2444. }
  2445. if (set_properties) {
  2446. psmouse->vendor = "ALPS";
  2447. psmouse->name = priv->flags & ALPS_DUALPOINT ?
  2448. "DualPoint TouchPad" : "GlidePoint";
  2449. psmouse->model = priv->proto_version;
  2450. } else {
  2451. /*
  2452. * Destroy alps_data structure we allocated earlier since
  2453. * this was just a "trial run". Otherwise we'll keep it
  2454. * to be used by alps_init() which has to be called if
  2455. * we succeed and set_properties is true.
  2456. */
  2457. kfree(priv);
  2458. psmouse->private = NULL;
  2459. }
  2460. return 0;
  2461. }