alps.c 87 KB

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