alps.c 85 KB

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