alps.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157
  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. f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
  1037. f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
  1038. f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
  1039. f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
  1040. } else {
  1041. f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
  1042. f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
  1043. f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
  1044. f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
  1045. }
  1046. f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
  1047. if (SS4_IS_MF_CONTINUE(p)) {
  1048. f->first_mp = 1;
  1049. } else {
  1050. f->fingers = 2;
  1051. f->first_mp = 0;
  1052. }
  1053. f->is_mp = 0;
  1054. break;
  1055. case SS4_PACKET_ID_MULTI:
  1056. if (priv->flags & ALPS_BUTTONPAD) {
  1057. f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
  1058. f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
  1059. f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
  1060. f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
  1061. no_data_x = SS4_MFPACKET_NO_AX_BL;
  1062. no_data_y = SS4_MFPACKET_NO_AY_BL;
  1063. } else {
  1064. f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
  1065. f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
  1066. f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
  1067. f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
  1068. no_data_x = SS4_MFPACKET_NO_AX;
  1069. no_data_y = SS4_MFPACKET_NO_AY;
  1070. }
  1071. f->first_mp = 0;
  1072. f->is_mp = 1;
  1073. if (SS4_IS_5F_DETECTED(p)) {
  1074. f->fingers = 5;
  1075. } else if (f->mt[3].x == no_data_x &&
  1076. f->mt[3].y == no_data_y) {
  1077. f->mt[3].x = 0;
  1078. f->mt[3].y = 0;
  1079. f->fingers = 3;
  1080. } else {
  1081. f->fingers = 4;
  1082. }
  1083. break;
  1084. case SS4_PACKET_ID_STICK:
  1085. /*
  1086. * x, y, and pressure are decoded in
  1087. * alps_process_packet_ss4_v2()
  1088. */
  1089. f->first_mp = 0;
  1090. f->is_mp = 0;
  1091. break;
  1092. case SS4_PACKET_ID_IDLE:
  1093. default:
  1094. memset(f, 0, sizeof(struct alps_fields));
  1095. break;
  1096. }
  1097. /* handle buttons */
  1098. if (pkt_id == SS4_PACKET_ID_STICK) {
  1099. f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
  1100. f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
  1101. f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
  1102. } else {
  1103. f->left = !!(SS4_BTN_V2(p) & 0x01);
  1104. if (!(priv->flags & ALPS_BUTTONPAD)) {
  1105. f->right = !!(SS4_BTN_V2(p) & 0x02);
  1106. f->middle = !!(SS4_BTN_V2(p) & 0x04);
  1107. }
  1108. }
  1109. return 0;
  1110. }
  1111. static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
  1112. {
  1113. struct alps_data *priv = psmouse->private;
  1114. unsigned char *packet = psmouse->packet;
  1115. struct input_dev *dev = psmouse->dev;
  1116. struct input_dev *dev2 = priv->dev2;
  1117. struct alps_fields *f = &priv->f;
  1118. memset(f, 0, sizeof(struct alps_fields));
  1119. priv->decode_fields(f, packet, psmouse);
  1120. if (priv->multi_packet) {
  1121. /*
  1122. * Sometimes the first packet will indicate a multi-packet
  1123. * sequence, but sometimes the next multi-packet would not
  1124. * come. Check for this, and when it happens process the
  1125. * position packet as usual.
  1126. */
  1127. if (f->is_mp) {
  1128. /* Now process the 1st packet */
  1129. priv->decode_fields(f, priv->multi_data, psmouse);
  1130. } else {
  1131. priv->multi_packet = 0;
  1132. }
  1133. }
  1134. /*
  1135. * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
  1136. * When it is set, it means 2nd packet comes without 1st packet come.
  1137. */
  1138. if (f->is_mp)
  1139. return;
  1140. /* Save the first packet */
  1141. if (!priv->multi_packet && f->first_mp) {
  1142. priv->multi_packet = 1;
  1143. memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
  1144. return;
  1145. }
  1146. priv->multi_packet = 0;
  1147. /* Report trackstick */
  1148. if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
  1149. if (!(priv->flags & ALPS_DUALPOINT)) {
  1150. psmouse_warn(psmouse,
  1151. "Rejected trackstick packet from non DualPoint device");
  1152. return;
  1153. }
  1154. input_report_rel(dev2, REL_X, SS4_TS_X_V2(packet));
  1155. input_report_rel(dev2, REL_Y, SS4_TS_Y_V2(packet));
  1156. input_report_abs(dev2, ABS_PRESSURE, SS4_TS_Z_V2(packet));
  1157. input_report_key(dev2, BTN_LEFT, f->ts_left);
  1158. input_report_key(dev2, BTN_RIGHT, f->ts_right);
  1159. input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
  1160. input_sync(dev2);
  1161. return;
  1162. }
  1163. /* Report touchpad */
  1164. alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
  1165. input_mt_report_finger_count(dev, f->fingers);
  1166. input_report_key(dev, BTN_LEFT, f->left);
  1167. input_report_key(dev, BTN_RIGHT, f->right);
  1168. input_report_key(dev, BTN_MIDDLE, f->middle);
  1169. input_report_abs(dev, ABS_PRESSURE, f->pressure);
  1170. input_sync(dev);
  1171. }
  1172. static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
  1173. {
  1174. if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
  1175. return false;
  1176. if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
  1177. return false;
  1178. return true;
  1179. }
  1180. static DEFINE_MUTEX(alps_mutex);
  1181. static void alps_register_bare_ps2_mouse(struct work_struct *work)
  1182. {
  1183. struct alps_data *priv =
  1184. container_of(work, struct alps_data, dev3_register_work.work);
  1185. struct psmouse *psmouse = priv->psmouse;
  1186. struct input_dev *dev3;
  1187. int error = 0;
  1188. mutex_lock(&alps_mutex);
  1189. if (priv->dev3)
  1190. goto out;
  1191. dev3 = input_allocate_device();
  1192. if (!dev3) {
  1193. psmouse_err(psmouse, "failed to allocate secondary device\n");
  1194. error = -ENOMEM;
  1195. goto out;
  1196. }
  1197. snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
  1198. psmouse->ps2dev.serio->phys,
  1199. (priv->dev2 ? "input2" : "input1"));
  1200. dev3->phys = priv->phys3;
  1201. /*
  1202. * format of input device name is: "protocol vendor name"
  1203. * see function psmouse_switch_protocol() in psmouse-base.c
  1204. */
  1205. dev3->name = "PS/2 ALPS Mouse";
  1206. dev3->id.bustype = BUS_I8042;
  1207. dev3->id.vendor = 0x0002;
  1208. dev3->id.product = PSMOUSE_PS2;
  1209. dev3->id.version = 0x0000;
  1210. dev3->dev.parent = &psmouse->ps2dev.serio->dev;
  1211. input_set_capability(dev3, EV_REL, REL_X);
  1212. input_set_capability(dev3, EV_REL, REL_Y);
  1213. input_set_capability(dev3, EV_KEY, BTN_LEFT);
  1214. input_set_capability(dev3, EV_KEY, BTN_RIGHT);
  1215. input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
  1216. __set_bit(INPUT_PROP_POINTER, dev3->propbit);
  1217. error = input_register_device(dev3);
  1218. if (error) {
  1219. psmouse_err(psmouse,
  1220. "failed to register secondary device: %d\n",
  1221. error);
  1222. input_free_device(dev3);
  1223. goto out;
  1224. }
  1225. priv->dev3 = dev3;
  1226. out:
  1227. /*
  1228. * Save the error code so that we can detect that we
  1229. * already tried to create the device.
  1230. */
  1231. if (error)
  1232. priv->dev3 = ERR_PTR(error);
  1233. mutex_unlock(&alps_mutex);
  1234. }
  1235. static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
  1236. unsigned char packet[],
  1237. bool report_buttons)
  1238. {
  1239. struct alps_data *priv = psmouse->private;
  1240. struct input_dev *dev, *dev2 = NULL;
  1241. /* Figure out which device to use to report the bare packet */
  1242. if (priv->proto_version == ALPS_PROTO_V2 &&
  1243. (priv->flags & ALPS_DUALPOINT)) {
  1244. /* On V2 devices the DualPoint Stick reports bare packets */
  1245. dev = priv->dev2;
  1246. dev2 = psmouse->dev;
  1247. } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
  1248. /* Register dev3 mouse if we received PS/2 packet first time */
  1249. if (!IS_ERR(priv->dev3))
  1250. psmouse_queue_work(psmouse, &priv->dev3_register_work,
  1251. 0);
  1252. return;
  1253. } else {
  1254. dev = priv->dev3;
  1255. }
  1256. if (report_buttons)
  1257. alps_report_buttons(dev, dev2,
  1258. packet[0] & 1, packet[0] & 2, packet[0] & 4);
  1259. input_report_rel(dev, REL_X,
  1260. packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
  1261. input_report_rel(dev, REL_Y,
  1262. packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
  1263. input_sync(dev);
  1264. }
  1265. static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
  1266. {
  1267. struct alps_data *priv = psmouse->private;
  1268. if (psmouse->pktcnt < 6)
  1269. return PSMOUSE_GOOD_DATA;
  1270. if (psmouse->pktcnt == 6) {
  1271. /*
  1272. * Start a timer to flush the packet if it ends up last
  1273. * 6-byte packet in the stream. Timer needs to fire
  1274. * psmouse core times out itself. 20 ms should be enough
  1275. * to decide if we are getting more data or not.
  1276. */
  1277. mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
  1278. return PSMOUSE_GOOD_DATA;
  1279. }
  1280. del_timer(&priv->timer);
  1281. if (psmouse->packet[6] & 0x80) {
  1282. /*
  1283. * Highest bit is set - that means we either had
  1284. * complete ALPS packet and this is start of the
  1285. * next packet or we got garbage.
  1286. */
  1287. if (((psmouse->packet[3] |
  1288. psmouse->packet[4] |
  1289. psmouse->packet[5]) & 0x80) ||
  1290. (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
  1291. psmouse_dbg(psmouse,
  1292. "refusing packet %4ph (suspected interleaved ps/2)\n",
  1293. psmouse->packet + 3);
  1294. return PSMOUSE_BAD_DATA;
  1295. }
  1296. priv->process_packet(psmouse);
  1297. /* Continue with the next packet */
  1298. psmouse->packet[0] = psmouse->packet[6];
  1299. psmouse->pktcnt = 1;
  1300. } else {
  1301. /*
  1302. * High bit is 0 - that means that we indeed got a PS/2
  1303. * packet in the middle of ALPS packet.
  1304. *
  1305. * There is also possibility that we got 6-byte ALPS
  1306. * packet followed by 3-byte packet from trackpoint. We
  1307. * can not distinguish between these 2 scenarios but
  1308. * because the latter is unlikely to happen in course of
  1309. * normal operation (user would need to press all
  1310. * buttons on the pad and start moving trackpoint
  1311. * without touching the pad surface) we assume former.
  1312. * Even if we are wrong the wost thing that would happen
  1313. * the cursor would jump but we should not get protocol
  1314. * de-synchronization.
  1315. */
  1316. alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
  1317. false);
  1318. /*
  1319. * Continue with the standard ALPS protocol handling,
  1320. * but make sure we won't process it as an interleaved
  1321. * packet again, which may happen if all buttons are
  1322. * pressed. To avoid this let's reset the 4th bit which
  1323. * is normally 1.
  1324. */
  1325. psmouse->packet[3] = psmouse->packet[6] & 0xf7;
  1326. psmouse->pktcnt = 4;
  1327. }
  1328. return PSMOUSE_GOOD_DATA;
  1329. }
  1330. static void alps_flush_packet(unsigned long data)
  1331. {
  1332. struct psmouse *psmouse = (struct psmouse *)data;
  1333. struct alps_data *priv = psmouse->private;
  1334. serio_pause_rx(psmouse->ps2dev.serio);
  1335. if (psmouse->pktcnt == psmouse->pktsize) {
  1336. /*
  1337. * We did not any more data in reasonable amount of time.
  1338. * Validate the last 3 bytes and process as a standard
  1339. * ALPS packet.
  1340. */
  1341. if ((psmouse->packet[3] |
  1342. psmouse->packet[4] |
  1343. psmouse->packet[5]) & 0x80) {
  1344. psmouse_dbg(psmouse,
  1345. "refusing packet %3ph (suspected interleaved ps/2)\n",
  1346. psmouse->packet + 3);
  1347. } else {
  1348. priv->process_packet(psmouse);
  1349. }
  1350. psmouse->pktcnt = 0;
  1351. }
  1352. serio_continue_rx(psmouse->ps2dev.serio);
  1353. }
  1354. static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
  1355. {
  1356. struct alps_data *priv = psmouse->private;
  1357. /*
  1358. * Check if we are dealing with a bare PS/2 packet, presumably from
  1359. * a device connected to the external PS/2 port. Because bare PS/2
  1360. * protocol does not have enough constant bits to self-synchronize
  1361. * properly we only do this if the device is fully synchronized.
  1362. * Can not distinguish V8's first byte from PS/2 packet's
  1363. */
  1364. if (priv->proto_version != ALPS_PROTO_V8 &&
  1365. !psmouse->out_of_sync_cnt &&
  1366. (psmouse->packet[0] & 0xc8) == 0x08) {
  1367. if (psmouse->pktcnt == 3) {
  1368. alps_report_bare_ps2_packet(psmouse, psmouse->packet,
  1369. true);
  1370. return PSMOUSE_FULL_PACKET;
  1371. }
  1372. return PSMOUSE_GOOD_DATA;
  1373. }
  1374. /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
  1375. if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
  1376. psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
  1377. return alps_handle_interleaved_ps2(psmouse);
  1378. }
  1379. if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
  1380. psmouse_dbg(psmouse,
  1381. "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
  1382. psmouse->packet[0], priv->mask0, priv->byte0);
  1383. return PSMOUSE_BAD_DATA;
  1384. }
  1385. /* Bytes 2 - pktsize should have 0 in the highest bit */
  1386. if (priv->proto_version < ALPS_PROTO_V5 &&
  1387. psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
  1388. (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
  1389. psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
  1390. psmouse->pktcnt - 1,
  1391. psmouse->packet[psmouse->pktcnt - 1]);
  1392. if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
  1393. psmouse->pktcnt == psmouse->pktsize) {
  1394. /*
  1395. * Some Dell boxes, such as Latitude E6440 or E7440
  1396. * with closed lid, quite often smash last byte of
  1397. * otherwise valid packet with 0xff. Given that the
  1398. * next packet is very likely to be valid let's
  1399. * report PSMOUSE_FULL_PACKET but not process data,
  1400. * rather than reporting PSMOUSE_BAD_DATA and
  1401. * filling the logs.
  1402. */
  1403. return PSMOUSE_FULL_PACKET;
  1404. }
  1405. return PSMOUSE_BAD_DATA;
  1406. }
  1407. if ((priv->proto_version == ALPS_PROTO_V7 &&
  1408. !alps_is_valid_package_v7(psmouse)) ||
  1409. (priv->proto_version == ALPS_PROTO_V8 &&
  1410. !alps_is_valid_package_ss4_v2(psmouse))) {
  1411. psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
  1412. psmouse->pktcnt - 1,
  1413. psmouse->packet[psmouse->pktcnt - 1]);
  1414. return PSMOUSE_BAD_DATA;
  1415. }
  1416. if (psmouse->pktcnt == psmouse->pktsize) {
  1417. priv->process_packet(psmouse);
  1418. return PSMOUSE_FULL_PACKET;
  1419. }
  1420. return PSMOUSE_GOOD_DATA;
  1421. }
  1422. static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
  1423. {
  1424. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1425. struct alps_data *priv = psmouse->private;
  1426. int command;
  1427. unsigned char *param;
  1428. unsigned char dummy[4];
  1429. BUG_ON(nibble > 0xf);
  1430. command = priv->nibble_commands[nibble].command;
  1431. param = (command & 0x0f00) ?
  1432. dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
  1433. if (ps2_command(ps2dev, param, command))
  1434. return -1;
  1435. return 0;
  1436. }
  1437. static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
  1438. {
  1439. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1440. struct alps_data *priv = psmouse->private;
  1441. int i, nibble;
  1442. if (ps2_command(ps2dev, NULL, priv->addr_command))
  1443. return -1;
  1444. for (i = 12; i >= 0; i -= 4) {
  1445. nibble = (addr >> i) & 0xf;
  1446. if (alps_command_mode_send_nibble(psmouse, nibble))
  1447. return -1;
  1448. }
  1449. return 0;
  1450. }
  1451. static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
  1452. {
  1453. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1454. unsigned char param[4];
  1455. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  1456. return -1;
  1457. /*
  1458. * The address being read is returned in the first two bytes
  1459. * of the result. Check that this address matches the expected
  1460. * address.
  1461. */
  1462. if (addr != ((param[0] << 8) | param[1]))
  1463. return -1;
  1464. return param[2];
  1465. }
  1466. static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
  1467. {
  1468. if (alps_command_mode_set_addr(psmouse, addr))
  1469. return -1;
  1470. return __alps_command_mode_read_reg(psmouse, addr);
  1471. }
  1472. static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
  1473. {
  1474. if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
  1475. return -1;
  1476. if (alps_command_mode_send_nibble(psmouse, value & 0xf))
  1477. return -1;
  1478. return 0;
  1479. }
  1480. static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
  1481. u8 value)
  1482. {
  1483. if (alps_command_mode_set_addr(psmouse, addr))
  1484. return -1;
  1485. return __alps_command_mode_write_reg(psmouse, value);
  1486. }
  1487. static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
  1488. int repeated_command, unsigned char *param)
  1489. {
  1490. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1491. param[0] = 0;
  1492. if (init_command && ps2_command(ps2dev, param, init_command))
  1493. return -EIO;
  1494. if (ps2_command(ps2dev, NULL, repeated_command) ||
  1495. ps2_command(ps2dev, NULL, repeated_command) ||
  1496. ps2_command(ps2dev, NULL, repeated_command))
  1497. return -EIO;
  1498. param[0] = param[1] = param[2] = 0xff;
  1499. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  1500. return -EIO;
  1501. psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
  1502. repeated_command, param);
  1503. return 0;
  1504. }
  1505. static bool alps_check_valid_firmware_id(unsigned char id[])
  1506. {
  1507. if (id[0] == 0x73)
  1508. return true;
  1509. if (id[0] == 0x88 &&
  1510. (id[1] == 0x07 ||
  1511. id[1] == 0x08 ||
  1512. (id[1] & 0xf0) == 0xb0 ||
  1513. (id[1] & 0xf0) == 0xc0)) {
  1514. return true;
  1515. }
  1516. return false;
  1517. }
  1518. static int alps_enter_command_mode(struct psmouse *psmouse)
  1519. {
  1520. unsigned char param[4];
  1521. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
  1522. psmouse_err(psmouse, "failed to enter command mode\n");
  1523. return -1;
  1524. }
  1525. if (!alps_check_valid_firmware_id(param)) {
  1526. psmouse_dbg(psmouse,
  1527. "unknown response while entering command mode\n");
  1528. return -1;
  1529. }
  1530. return 0;
  1531. }
  1532. static inline int alps_exit_command_mode(struct psmouse *psmouse)
  1533. {
  1534. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1535. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
  1536. return -1;
  1537. return 0;
  1538. }
  1539. /*
  1540. * For DualPoint devices select the device that should respond to
  1541. * subsequent commands. It looks like glidepad is behind stickpointer,
  1542. * I'd thought it would be other way around...
  1543. */
  1544. static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
  1545. {
  1546. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1547. int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
  1548. if (ps2_command(ps2dev, NULL, cmd) ||
  1549. ps2_command(ps2dev, NULL, cmd) ||
  1550. ps2_command(ps2dev, NULL, cmd) ||
  1551. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  1552. return -1;
  1553. /* we may get 3 more bytes, just ignore them */
  1554. ps2_drain(ps2dev, 3, 100);
  1555. return 0;
  1556. }
  1557. static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
  1558. {
  1559. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1560. /* Try ALPS magic knock - 4 disable before enable */
  1561. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1562. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1563. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1564. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1565. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
  1566. return -1;
  1567. /*
  1568. * Switch mouse to poll (remote) mode so motion data will not
  1569. * get in our way
  1570. */
  1571. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
  1572. }
  1573. static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
  1574. {
  1575. int i, nibble;
  1576. /*
  1577. * b0-b11 are valid bits, send sequence is inverse.
  1578. * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
  1579. */
  1580. for (i = 0; i <= 8; i += 4) {
  1581. nibble = (word >> i) & 0xf;
  1582. if (alps_command_mode_send_nibble(psmouse, nibble))
  1583. return -1;
  1584. }
  1585. return 0;
  1586. }
  1587. static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
  1588. u16 addr, u16 value)
  1589. {
  1590. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1591. /* 0x0A0 is the command to write the word */
  1592. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
  1593. alps_monitor_mode_send_word(psmouse, 0x0A0) ||
  1594. alps_monitor_mode_send_word(psmouse, addr) ||
  1595. alps_monitor_mode_send_word(psmouse, value) ||
  1596. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  1597. return -1;
  1598. return 0;
  1599. }
  1600. static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
  1601. {
  1602. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1603. if (enable) {
  1604. /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
  1605. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
  1606. ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
  1607. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1608. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1609. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  1610. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1611. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  1612. ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
  1613. return -1;
  1614. } else {
  1615. /* EC to exit monitor mode */
  1616. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
  1617. return -1;
  1618. }
  1619. return 0;
  1620. }
  1621. static int alps_absolute_mode_v6(struct psmouse *psmouse)
  1622. {
  1623. u16 reg_val = 0x181;
  1624. int ret = -1;
  1625. /* enter monitor mode, to write the register */
  1626. if (alps_monitor_mode(psmouse, true))
  1627. return -1;
  1628. ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
  1629. if (alps_monitor_mode(psmouse, false))
  1630. ret = -1;
  1631. return ret;
  1632. }
  1633. static int alps_get_status(struct psmouse *psmouse, char *param)
  1634. {
  1635. /* Get status: 0xF5 0xF5 0xF5 0xE9 */
  1636. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
  1637. return -1;
  1638. return 0;
  1639. }
  1640. /*
  1641. * Turn touchpad tapping on or off. The sequences are:
  1642. * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
  1643. * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
  1644. * My guess that 0xE9 (GetInfo) is here as a sync point.
  1645. * For models that also have stickpointer (DualPoints) its tapping
  1646. * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
  1647. * we don't fiddle with it.
  1648. */
  1649. static int alps_tap_mode(struct psmouse *psmouse, int enable)
  1650. {
  1651. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1652. int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
  1653. unsigned char tap_arg = enable ? 0x0A : 0x00;
  1654. unsigned char param[4];
  1655. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
  1656. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1657. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1658. ps2_command(ps2dev, &tap_arg, cmd))
  1659. return -1;
  1660. if (alps_get_status(psmouse, param))
  1661. return -1;
  1662. return 0;
  1663. }
  1664. /*
  1665. * alps_poll() - poll the touchpad for current motion packet.
  1666. * Used in resync.
  1667. */
  1668. static int alps_poll(struct psmouse *psmouse)
  1669. {
  1670. struct alps_data *priv = psmouse->private;
  1671. unsigned char buf[sizeof(psmouse->packet)];
  1672. bool poll_failed;
  1673. if (priv->flags & ALPS_PASS)
  1674. alps_passthrough_mode_v2(psmouse, true);
  1675. poll_failed = ps2_command(&psmouse->ps2dev, buf,
  1676. PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
  1677. if (priv->flags & ALPS_PASS)
  1678. alps_passthrough_mode_v2(psmouse, false);
  1679. if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
  1680. return -1;
  1681. if ((psmouse->badbyte & 0xc8) == 0x08) {
  1682. /*
  1683. * Poll the track stick ...
  1684. */
  1685. if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
  1686. return -1;
  1687. }
  1688. memcpy(psmouse->packet, buf, sizeof(buf));
  1689. return 0;
  1690. }
  1691. static int alps_hw_init_v1_v2(struct psmouse *psmouse)
  1692. {
  1693. struct alps_data *priv = psmouse->private;
  1694. if ((priv->flags & ALPS_PASS) &&
  1695. alps_passthrough_mode_v2(psmouse, true)) {
  1696. return -1;
  1697. }
  1698. if (alps_tap_mode(psmouse, true)) {
  1699. psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
  1700. return -1;
  1701. }
  1702. if (alps_absolute_mode_v1_v2(psmouse)) {
  1703. psmouse_err(psmouse, "Failed to enable absolute mode\n");
  1704. return -1;
  1705. }
  1706. if ((priv->flags & ALPS_PASS) &&
  1707. alps_passthrough_mode_v2(psmouse, false)) {
  1708. return -1;
  1709. }
  1710. /* ALPS needs stream mode, otherwise it won't report any data */
  1711. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
  1712. psmouse_err(psmouse, "Failed to enable stream mode\n");
  1713. return -1;
  1714. }
  1715. return 0;
  1716. }
  1717. static int alps_hw_init_v6(struct psmouse *psmouse)
  1718. {
  1719. unsigned char param[2] = {0xC8, 0x14};
  1720. /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
  1721. if (alps_passthrough_mode_v2(psmouse, true))
  1722. return -1;
  1723. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1724. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1725. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1726. ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  1727. ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
  1728. return -1;
  1729. if (alps_passthrough_mode_v2(psmouse, false))
  1730. return -1;
  1731. if (alps_absolute_mode_v6(psmouse)) {
  1732. psmouse_err(psmouse, "Failed to enable absolute mode\n");
  1733. return -1;
  1734. }
  1735. return 0;
  1736. }
  1737. /*
  1738. * Enable or disable passthrough mode to the trackstick.
  1739. */
  1740. static int alps_passthrough_mode_v3(struct psmouse *psmouse,
  1741. int reg_base, bool enable)
  1742. {
  1743. int reg_val, ret = -1;
  1744. if (alps_enter_command_mode(psmouse))
  1745. return -1;
  1746. reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
  1747. if (reg_val == -1)
  1748. goto error;
  1749. if (enable)
  1750. reg_val |= 0x01;
  1751. else
  1752. reg_val &= ~0x01;
  1753. ret = __alps_command_mode_write_reg(psmouse, reg_val);
  1754. error:
  1755. if (alps_exit_command_mode(psmouse))
  1756. ret = -1;
  1757. return ret;
  1758. }
  1759. /* Must be in command mode when calling this function */
  1760. static int alps_absolute_mode_v3(struct psmouse *psmouse)
  1761. {
  1762. int reg_val;
  1763. reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
  1764. if (reg_val == -1)
  1765. return -1;
  1766. reg_val |= 0x06;
  1767. if (__alps_command_mode_write_reg(psmouse, reg_val))
  1768. return -1;
  1769. return 0;
  1770. }
  1771. static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
  1772. {
  1773. int ret = -EIO, reg_val;
  1774. if (alps_enter_command_mode(psmouse))
  1775. goto error;
  1776. reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
  1777. if (reg_val == -1)
  1778. goto error;
  1779. /* bit 7: trackstick is present */
  1780. ret = reg_val & 0x80 ? 0 : -ENODEV;
  1781. error:
  1782. alps_exit_command_mode(psmouse);
  1783. return ret;
  1784. }
  1785. static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
  1786. {
  1787. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1788. int ret = 0;
  1789. unsigned char param[4];
  1790. if (alps_passthrough_mode_v3(psmouse, reg_base, true))
  1791. return -EIO;
  1792. /*
  1793. * E7 report for the trackstick
  1794. *
  1795. * There have been reports of failures to seem to trace back
  1796. * to the above trackstick check failing. When these occur
  1797. * this E7 report fails, so when that happens we continue
  1798. * with the assumption that there isn't a trackstick after
  1799. * all.
  1800. */
  1801. if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
  1802. psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
  1803. ret = -ENODEV;
  1804. } else {
  1805. psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
  1806. /*
  1807. * Not sure what this does, but it is absolutely
  1808. * essential. Without it, the touchpad does not
  1809. * work at all and the trackstick just emits normal
  1810. * PS/2 packets.
  1811. */
  1812. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1813. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1814. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1815. alps_command_mode_send_nibble(psmouse, 0x9) ||
  1816. alps_command_mode_send_nibble(psmouse, 0x4)) {
  1817. psmouse_err(psmouse,
  1818. "Error sending magic E6 sequence\n");
  1819. ret = -EIO;
  1820. goto error;
  1821. }
  1822. /*
  1823. * This ensures the trackstick packets are in the format
  1824. * supported by this driver. If bit 1 isn't set the packet
  1825. * format is different.
  1826. */
  1827. if (alps_enter_command_mode(psmouse) ||
  1828. alps_command_mode_write_reg(psmouse,
  1829. reg_base + 0x08, 0x82) ||
  1830. alps_exit_command_mode(psmouse))
  1831. ret = -EIO;
  1832. }
  1833. error:
  1834. if (alps_passthrough_mode_v3(psmouse, reg_base, false))
  1835. ret = -EIO;
  1836. return ret;
  1837. }
  1838. static int alps_hw_init_v3(struct psmouse *psmouse)
  1839. {
  1840. struct alps_data *priv = psmouse->private;
  1841. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1842. int reg_val;
  1843. unsigned char param[4];
  1844. if ((priv->flags & ALPS_DUALPOINT) &&
  1845. alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
  1846. goto error;
  1847. if (alps_enter_command_mode(psmouse) ||
  1848. alps_absolute_mode_v3(psmouse)) {
  1849. psmouse_err(psmouse, "Failed to enter absolute mode\n");
  1850. goto error;
  1851. }
  1852. reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
  1853. if (reg_val == -1)
  1854. goto error;
  1855. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
  1856. goto error;
  1857. reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
  1858. if (reg_val == -1)
  1859. goto error;
  1860. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
  1861. goto error;
  1862. if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
  1863. goto error;
  1864. if (__alps_command_mode_write_reg(psmouse, 0x04))
  1865. goto error;
  1866. if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
  1867. goto error;
  1868. if (__alps_command_mode_write_reg(psmouse, 0x03))
  1869. goto error;
  1870. if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
  1871. goto error;
  1872. if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
  1873. goto error;
  1874. if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
  1875. goto error;
  1876. if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
  1877. goto error;
  1878. alps_exit_command_mode(psmouse);
  1879. /* Set rate and enable data reporting */
  1880. param[0] = 0x64;
  1881. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
  1882. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  1883. psmouse_err(psmouse, "Failed to enable data reporting\n");
  1884. return -1;
  1885. }
  1886. return 0;
  1887. error:
  1888. /*
  1889. * Leaving the touchpad in command mode will essentially render
  1890. * it unusable until the machine reboots, so exit it here just
  1891. * to be safe
  1892. */
  1893. alps_exit_command_mode(psmouse);
  1894. return -1;
  1895. }
  1896. static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
  1897. {
  1898. int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
  1899. struct alps_data *priv = psmouse->private;
  1900. reg = alps_command_mode_read_reg(psmouse, reg_pitch);
  1901. if (reg < 0)
  1902. return reg;
  1903. x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
  1904. x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
  1905. y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
  1906. y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
  1907. reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
  1908. if (reg < 0)
  1909. return reg;
  1910. x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
  1911. x_electrode = 17 + x_electrode;
  1912. y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
  1913. y_electrode = 13 + y_electrode;
  1914. x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
  1915. y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
  1916. priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
  1917. priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
  1918. psmouse_dbg(psmouse,
  1919. "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
  1920. x_pitch, y_pitch, x_electrode, y_electrode,
  1921. x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
  1922. return 0;
  1923. }
  1924. static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
  1925. {
  1926. struct alps_data *priv = psmouse->private;
  1927. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1928. int reg_val, ret = -1;
  1929. if (priv->flags & ALPS_DUALPOINT) {
  1930. reg_val = alps_setup_trackstick_v3(psmouse,
  1931. ALPS_REG_BASE_RUSHMORE);
  1932. if (reg_val == -EIO)
  1933. goto error;
  1934. }
  1935. if (alps_enter_command_mode(psmouse) ||
  1936. alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
  1937. alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
  1938. goto error;
  1939. if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
  1940. goto error;
  1941. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
  1942. if (reg_val == -1)
  1943. goto error;
  1944. if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
  1945. goto error;
  1946. if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
  1947. goto error;
  1948. /* enter absolute mode */
  1949. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
  1950. if (reg_val == -1)
  1951. goto error;
  1952. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
  1953. goto error;
  1954. alps_exit_command_mode(psmouse);
  1955. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  1956. error:
  1957. alps_exit_command_mode(psmouse);
  1958. return ret;
  1959. }
  1960. /* Must be in command mode when calling this function */
  1961. static int alps_absolute_mode_v4(struct psmouse *psmouse)
  1962. {
  1963. int reg_val;
  1964. reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
  1965. if (reg_val == -1)
  1966. return -1;
  1967. reg_val |= 0x02;
  1968. if (__alps_command_mode_write_reg(psmouse, reg_val))
  1969. return -1;
  1970. return 0;
  1971. }
  1972. static int alps_hw_init_v4(struct psmouse *psmouse)
  1973. {
  1974. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1975. unsigned char param[4];
  1976. if (alps_enter_command_mode(psmouse))
  1977. goto error;
  1978. if (alps_absolute_mode_v4(psmouse)) {
  1979. psmouse_err(psmouse, "Failed to enter absolute mode\n");
  1980. goto error;
  1981. }
  1982. if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
  1983. goto error;
  1984. if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
  1985. goto error;
  1986. if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
  1987. goto error;
  1988. if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
  1989. goto error;
  1990. if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
  1991. goto error;
  1992. if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
  1993. goto error;
  1994. if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
  1995. goto error;
  1996. if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
  1997. goto error;
  1998. alps_exit_command_mode(psmouse);
  1999. /*
  2000. * This sequence changes the output from a 9-byte to an
  2001. * 8-byte format. All the same data seems to be present,
  2002. * just in a more compact format.
  2003. */
  2004. param[0] = 0xc8;
  2005. param[1] = 0x64;
  2006. param[2] = 0x50;
  2007. if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  2008. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
  2009. ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
  2010. ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
  2011. return -1;
  2012. /* Set rate and enable data reporting */
  2013. param[0] = 0x64;
  2014. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
  2015. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  2016. psmouse_err(psmouse, "Failed to enable data reporting\n");
  2017. return -1;
  2018. }
  2019. return 0;
  2020. error:
  2021. /*
  2022. * Leaving the touchpad in command mode will essentially render
  2023. * it unusable until the machine reboots, so exit it here just
  2024. * to be safe
  2025. */
  2026. alps_exit_command_mode(psmouse);
  2027. return -1;
  2028. }
  2029. static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
  2030. unsigned char index, unsigned char otp[])
  2031. {
  2032. struct ps2dev *ps2dev = &psmouse->ps2dev;
  2033. switch (index) {
  2034. case 0:
  2035. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  2036. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  2037. ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
  2038. return -1;
  2039. break;
  2040. case 1:
  2041. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  2042. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  2043. ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
  2044. return -1;
  2045. break;
  2046. }
  2047. return 0;
  2048. }
  2049. static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
  2050. struct alps_data *priv)
  2051. {
  2052. int num_x_electrode;
  2053. int num_y_electrode;
  2054. int x_pitch, y_pitch, x_phys, y_phys;
  2055. if (IS_SS4PLUS_DEV(priv->dev_id)) {
  2056. num_x_electrode =
  2057. SS4PLUS_NUMSENSOR_XOFFSET + (otp[0][2] & 0x0F);
  2058. num_y_electrode =
  2059. SS4PLUS_NUMSENSOR_YOFFSET + ((otp[0][2] >> 4) & 0x0F);
  2060. priv->x_max =
  2061. (num_x_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
  2062. priv->y_max =
  2063. (num_y_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
  2064. x_pitch = (otp[0][1] & 0x0F) + SS4PLUS_MIN_PITCH_MM;
  2065. y_pitch = ((otp[0][1] >> 4) & 0x0F) + SS4PLUS_MIN_PITCH_MM;
  2066. } else {
  2067. num_x_electrode =
  2068. SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
  2069. num_y_electrode =
  2070. SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
  2071. priv->x_max =
  2072. (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
  2073. priv->y_max =
  2074. (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
  2075. x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
  2076. y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
  2077. }
  2078. x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
  2079. y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
  2080. priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
  2081. priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
  2082. return 0;
  2083. }
  2084. static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
  2085. struct alps_data *priv)
  2086. {
  2087. unsigned char is_btnless;
  2088. if (IS_SS4PLUS_DEV(priv->dev_id))
  2089. is_btnless = (otp[1][0] >> 1) & 0x01;
  2090. else
  2091. is_btnless = (otp[1][1] >> 3) & 0x01;
  2092. if (is_btnless)
  2093. priv->flags |= ALPS_BUTTONPAD;
  2094. return 0;
  2095. }
  2096. static int alps_update_dual_info_ss4_v2(unsigned char otp[][4],
  2097. struct alps_data *priv)
  2098. {
  2099. bool is_dual = false;
  2100. if (IS_SS4PLUS_DEV(priv->dev_id))
  2101. is_dual = (otp[0][0] >> 4) & 0x01;
  2102. if (is_dual)
  2103. priv->flags |= ALPS_DUALPOINT |
  2104. ALPS_DUALPOINT_WITH_PRESSURE;
  2105. return 0;
  2106. }
  2107. static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
  2108. struct alps_data *priv)
  2109. {
  2110. unsigned char otp[2][4];
  2111. memset(otp, 0, sizeof(otp));
  2112. if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
  2113. alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
  2114. return -1;
  2115. alps_update_device_area_ss4_v2(otp, priv);
  2116. alps_update_btn_info_ss4_v2(otp, priv);
  2117. alps_update_dual_info_ss4_v2(otp, priv);
  2118. return 0;
  2119. }
  2120. static int alps_dolphin_get_device_area(struct psmouse *psmouse,
  2121. struct alps_data *priv)
  2122. {
  2123. struct ps2dev *ps2dev = &psmouse->ps2dev;
  2124. unsigned char param[4] = {0};
  2125. int num_x_electrode, num_y_electrode;
  2126. if (alps_enter_command_mode(psmouse))
  2127. return -1;
  2128. param[0] = 0x0a;
  2129. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
  2130. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  2131. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
  2132. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  2133. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
  2134. return -1;
  2135. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  2136. return -1;
  2137. /*
  2138. * Dolphin's sensor line number is not fixed. It can be calculated
  2139. * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
  2140. * Further more, we can get device's x_max and y_max by multiplying
  2141. * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
  2142. *
  2143. * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
  2144. * real sensor line number X = 11 + 8 = 19, and
  2145. * real sensor line number Y = 8 + 1 = 9.
  2146. * So, x_max = (19 - 1) * 64 = 1152, and
  2147. * y_max = (9 - 1) * 64 = 512.
  2148. */
  2149. num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
  2150. num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
  2151. priv->x_bits = num_x_electrode;
  2152. priv->y_bits = num_y_electrode;
  2153. priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
  2154. priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
  2155. if (alps_exit_command_mode(psmouse))
  2156. return -1;
  2157. return 0;
  2158. }
  2159. static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
  2160. {
  2161. struct ps2dev *ps2dev = &psmouse->ps2dev;
  2162. unsigned char param[2];
  2163. /* This is dolphin "v1" as empirically defined by florin9doi */
  2164. param[0] = 0x64;
  2165. param[1] = 0x28;
  2166. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  2167. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  2168. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
  2169. return -1;
  2170. return 0;
  2171. }
  2172. static int alps_hw_init_v7(struct psmouse *psmouse)
  2173. {
  2174. struct ps2dev *ps2dev = &psmouse->ps2dev;
  2175. int reg_val, ret = -1;
  2176. if (alps_enter_command_mode(psmouse) ||
  2177. alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
  2178. goto error;
  2179. if (alps_get_v3_v7_resolution(psmouse, 0xc397))
  2180. goto error;
  2181. if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
  2182. goto error;
  2183. reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
  2184. if (reg_val == -1)
  2185. goto error;
  2186. if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
  2187. goto error;
  2188. alps_exit_command_mode(psmouse);
  2189. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  2190. error:
  2191. alps_exit_command_mode(psmouse);
  2192. return ret;
  2193. }
  2194. static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
  2195. {
  2196. struct ps2dev *ps2dev = &psmouse->ps2dev;
  2197. char param[2] = {0x64, 0x28};
  2198. int ret = -1;
  2199. /* enter absolute mode */
  2200. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  2201. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
  2202. ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
  2203. ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
  2204. goto error;
  2205. }
  2206. /* T.B.D. Decread noise packet number, delete in the future */
  2207. if (alps_exit_command_mode(psmouse) ||
  2208. alps_enter_command_mode(psmouse) ||
  2209. alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
  2210. goto error;
  2211. }
  2212. alps_exit_command_mode(psmouse);
  2213. return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  2214. error:
  2215. alps_exit_command_mode(psmouse);
  2216. return ret;
  2217. }
  2218. static int alps_set_protocol(struct psmouse *psmouse,
  2219. struct alps_data *priv,
  2220. const struct alps_protocol_info *protocol)
  2221. {
  2222. psmouse->private = priv;
  2223. setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
  2224. priv->proto_version = protocol->version;
  2225. priv->byte0 = protocol->byte0;
  2226. priv->mask0 = protocol->mask0;
  2227. priv->flags = protocol->flags;
  2228. priv->x_max = 2000;
  2229. priv->y_max = 1400;
  2230. priv->x_bits = 15;
  2231. priv->y_bits = 11;
  2232. switch (priv->proto_version) {
  2233. case ALPS_PROTO_V1:
  2234. case ALPS_PROTO_V2:
  2235. priv->hw_init = alps_hw_init_v1_v2;
  2236. priv->process_packet = alps_process_packet_v1_v2;
  2237. priv->set_abs_params = alps_set_abs_params_st;
  2238. priv->x_max = 1023;
  2239. priv->y_max = 767;
  2240. if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
  2241. priv->flags |= ALPS_STICK_BITS;
  2242. break;
  2243. case ALPS_PROTO_V3:
  2244. priv->hw_init = alps_hw_init_v3;
  2245. priv->process_packet = alps_process_packet_v3;
  2246. priv->set_abs_params = alps_set_abs_params_semi_mt;
  2247. priv->decode_fields = alps_decode_pinnacle;
  2248. priv->nibble_commands = alps_v3_nibble_commands;
  2249. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2250. if (alps_probe_trackstick_v3_v7(psmouse,
  2251. ALPS_REG_BASE_PINNACLE) < 0)
  2252. priv->flags &= ~ALPS_DUALPOINT;
  2253. break;
  2254. case ALPS_PROTO_V3_RUSHMORE:
  2255. priv->hw_init = alps_hw_init_rushmore_v3;
  2256. priv->process_packet = alps_process_packet_v3;
  2257. priv->set_abs_params = alps_set_abs_params_semi_mt;
  2258. priv->decode_fields = alps_decode_rushmore;
  2259. priv->nibble_commands = alps_v3_nibble_commands;
  2260. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2261. priv->x_bits = 16;
  2262. priv->y_bits = 12;
  2263. if (alps_probe_trackstick_v3_v7(psmouse,
  2264. ALPS_REG_BASE_RUSHMORE) < 0)
  2265. priv->flags &= ~ALPS_DUALPOINT;
  2266. break;
  2267. case ALPS_PROTO_V4:
  2268. priv->hw_init = alps_hw_init_v4;
  2269. priv->process_packet = alps_process_packet_v4;
  2270. priv->set_abs_params = alps_set_abs_params_semi_mt;
  2271. priv->nibble_commands = alps_v4_nibble_commands;
  2272. priv->addr_command = PSMOUSE_CMD_DISABLE;
  2273. break;
  2274. case ALPS_PROTO_V5:
  2275. priv->hw_init = alps_hw_init_dolphin_v1;
  2276. priv->process_packet = alps_process_touchpad_packet_v3_v5;
  2277. priv->decode_fields = alps_decode_dolphin;
  2278. priv->set_abs_params = alps_set_abs_params_semi_mt;
  2279. priv->nibble_commands = alps_v3_nibble_commands;
  2280. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2281. priv->x_bits = 23;
  2282. priv->y_bits = 12;
  2283. if (alps_dolphin_get_device_area(psmouse, priv))
  2284. return -EIO;
  2285. break;
  2286. case ALPS_PROTO_V6:
  2287. priv->hw_init = alps_hw_init_v6;
  2288. priv->process_packet = alps_process_packet_v6;
  2289. priv->set_abs_params = alps_set_abs_params_st;
  2290. priv->nibble_commands = alps_v6_nibble_commands;
  2291. priv->x_max = 2047;
  2292. priv->y_max = 1535;
  2293. break;
  2294. case ALPS_PROTO_V7:
  2295. priv->hw_init = alps_hw_init_v7;
  2296. priv->process_packet = alps_process_packet_v7;
  2297. priv->decode_fields = alps_decode_packet_v7;
  2298. priv->set_abs_params = alps_set_abs_params_v7;
  2299. priv->nibble_commands = alps_v3_nibble_commands;
  2300. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2301. priv->x_max = 0xfff;
  2302. priv->y_max = 0x7ff;
  2303. if (priv->fw_ver[1] != 0xba)
  2304. priv->flags |= ALPS_BUTTONPAD;
  2305. if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
  2306. priv->flags &= ~ALPS_DUALPOINT;
  2307. break;
  2308. case ALPS_PROTO_V8:
  2309. priv->hw_init = alps_hw_init_ss4_v2;
  2310. priv->process_packet = alps_process_packet_ss4_v2;
  2311. priv->decode_fields = alps_decode_ss4_v2;
  2312. priv->set_abs_params = alps_set_abs_params_ss4_v2;
  2313. priv->nibble_commands = alps_v3_nibble_commands;
  2314. priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
  2315. if (alps_set_defaults_ss4_v2(psmouse, priv))
  2316. return -EIO;
  2317. break;
  2318. }
  2319. return 0;
  2320. }
  2321. static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
  2322. unsigned char *ec)
  2323. {
  2324. const struct alps_model_info *model;
  2325. int i;
  2326. for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
  2327. model = &alps_model_data[i];
  2328. if (!memcmp(e7, model->signature, sizeof(model->signature)))
  2329. return &model->protocol_info;
  2330. }
  2331. return NULL;
  2332. }
  2333. static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
  2334. {
  2335. const struct alps_protocol_info *protocol;
  2336. unsigned char e6[4], e7[4], ec[4];
  2337. int error;
  2338. /*
  2339. * First try "E6 report".
  2340. * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
  2341. * The bits 0-2 of the first byte will be 1s if some buttons are
  2342. * pressed.
  2343. */
  2344. if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  2345. PSMOUSE_CMD_SETSCALE11, e6))
  2346. return -EIO;
  2347. if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
  2348. return -EINVAL;
  2349. /*
  2350. * Now get the "E7" and "EC" reports. These will uniquely identify
  2351. * most ALPS touchpads.
  2352. */
  2353. if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  2354. PSMOUSE_CMD_SETSCALE21, e7) ||
  2355. alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
  2356. PSMOUSE_CMD_RESET_WRAP, ec) ||
  2357. alps_exit_command_mode(psmouse))
  2358. return -EIO;
  2359. protocol = alps_match_table(e7, ec);
  2360. if (!protocol) {
  2361. if (e7[0] == 0x73 && e7[1] == 0x02 && e7[2] == 0x64 &&
  2362. ec[2] == 0x8a) {
  2363. protocol = &alps_v4_protocol_data;
  2364. } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
  2365. ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
  2366. protocol = &alps_v5_protocol_data;
  2367. } else if (ec[0] == 0x88 &&
  2368. ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
  2369. protocol = &alps_v7_protocol_data;
  2370. } else if (ec[0] == 0x88 && ec[1] == 0x08) {
  2371. protocol = &alps_v3_rushmore_data;
  2372. } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
  2373. ec[2] >= 0x90 && ec[2] <= 0x9d) {
  2374. protocol = &alps_v3_protocol_data;
  2375. } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
  2376. (e7[2] == 0x14 || e7[2] == 0x28)) {
  2377. protocol = &alps_v8_protocol_data;
  2378. } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0xc8) {
  2379. protocol = &alps_v9_protocol_data;
  2380. psmouse_warn(psmouse,
  2381. "Unsupported ALPS V9 touchpad: E7=%3ph, EC=%3ph\n",
  2382. e7, ec);
  2383. return -EINVAL;
  2384. } else {
  2385. psmouse_dbg(psmouse,
  2386. "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
  2387. return -EINVAL;
  2388. }
  2389. }
  2390. if (priv) {
  2391. /* Save Device ID and Firmware version */
  2392. memcpy(priv->dev_id, e7, 3);
  2393. memcpy(priv->fw_ver, ec, 3);
  2394. error = alps_set_protocol(psmouse, priv, protocol);
  2395. if (error)
  2396. return error;
  2397. }
  2398. return 0;
  2399. }
  2400. static int alps_reconnect(struct psmouse *psmouse)
  2401. {
  2402. struct alps_data *priv = psmouse->private;
  2403. psmouse_reset(psmouse);
  2404. if (alps_identify(psmouse, priv) < 0)
  2405. return -1;
  2406. return priv->hw_init(psmouse);
  2407. }
  2408. static void alps_disconnect(struct psmouse *psmouse)
  2409. {
  2410. struct alps_data *priv = psmouse->private;
  2411. psmouse_reset(psmouse);
  2412. del_timer_sync(&priv->timer);
  2413. if (priv->dev2)
  2414. input_unregister_device(priv->dev2);
  2415. if (!IS_ERR_OR_NULL(priv->dev3))
  2416. input_unregister_device(priv->dev3);
  2417. kfree(priv);
  2418. }
  2419. static void alps_set_abs_params_st(struct alps_data *priv,
  2420. struct input_dev *dev1)
  2421. {
  2422. input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
  2423. input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
  2424. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  2425. }
  2426. static void alps_set_abs_params_mt_common(struct alps_data *priv,
  2427. struct input_dev *dev1)
  2428. {
  2429. input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
  2430. input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
  2431. input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
  2432. input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
  2433. set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
  2434. set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
  2435. }
  2436. static void alps_set_abs_params_semi_mt(struct alps_data *priv,
  2437. struct input_dev *dev1)
  2438. {
  2439. alps_set_abs_params_mt_common(priv, dev1);
  2440. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  2441. input_mt_init_slots(dev1, MAX_TOUCHES,
  2442. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
  2443. INPUT_MT_SEMI_MT);
  2444. }
  2445. static void alps_set_abs_params_v7(struct alps_data *priv,
  2446. struct input_dev *dev1)
  2447. {
  2448. alps_set_abs_params_mt_common(priv, dev1);
  2449. set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
  2450. input_mt_init_slots(dev1, MAX_TOUCHES,
  2451. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
  2452. INPUT_MT_TRACK);
  2453. set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
  2454. }
  2455. static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
  2456. struct input_dev *dev1)
  2457. {
  2458. alps_set_abs_params_mt_common(priv, dev1);
  2459. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  2460. set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
  2461. input_mt_init_slots(dev1, MAX_TOUCHES,
  2462. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
  2463. INPUT_MT_TRACK);
  2464. }
  2465. int alps_init(struct psmouse *psmouse)
  2466. {
  2467. struct alps_data *priv = psmouse->private;
  2468. struct input_dev *dev1 = psmouse->dev;
  2469. int error;
  2470. error = priv->hw_init(psmouse);
  2471. if (error)
  2472. goto init_fail;
  2473. /*
  2474. * Undo part of setup done for us by psmouse core since touchpad
  2475. * is not a relative device.
  2476. */
  2477. __clear_bit(EV_REL, dev1->evbit);
  2478. __clear_bit(REL_X, dev1->relbit);
  2479. __clear_bit(REL_Y, dev1->relbit);
  2480. /*
  2481. * Now set up our capabilities.
  2482. */
  2483. dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
  2484. dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
  2485. dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
  2486. dev1->keybit[BIT_WORD(BTN_LEFT)] |=
  2487. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
  2488. dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
  2489. priv->set_abs_params(priv, dev1);
  2490. if (priv->flags & ALPS_WHEEL) {
  2491. dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
  2492. dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
  2493. }
  2494. if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  2495. dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
  2496. dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
  2497. }
  2498. if (priv->flags & ALPS_FOUR_BUTTONS) {
  2499. dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
  2500. dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
  2501. dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
  2502. dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
  2503. } else if (priv->flags & ALPS_BUTTONPAD) {
  2504. set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
  2505. clear_bit(BTN_RIGHT, dev1->keybit);
  2506. } else {
  2507. dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
  2508. }
  2509. if (priv->flags & ALPS_DUALPOINT) {
  2510. struct input_dev *dev2;
  2511. dev2 = input_allocate_device();
  2512. if (!dev2) {
  2513. psmouse_err(psmouse,
  2514. "failed to allocate trackstick device\n");
  2515. error = -ENOMEM;
  2516. goto init_fail;
  2517. }
  2518. snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
  2519. psmouse->ps2dev.serio->phys);
  2520. dev2->phys = priv->phys2;
  2521. /*
  2522. * format of input device name is: "protocol vendor name"
  2523. * see function psmouse_switch_protocol() in psmouse-base.c
  2524. */
  2525. dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
  2526. dev2->id.bustype = BUS_I8042;
  2527. dev2->id.vendor = 0x0002;
  2528. dev2->id.product = PSMOUSE_ALPS;
  2529. dev2->id.version = priv->proto_version;
  2530. dev2->dev.parent = &psmouse->ps2dev.serio->dev;
  2531. input_set_capability(dev2, EV_REL, REL_X);
  2532. input_set_capability(dev2, EV_REL, REL_Y);
  2533. if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
  2534. input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
  2535. input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
  2536. }
  2537. input_set_capability(dev2, EV_KEY, BTN_LEFT);
  2538. input_set_capability(dev2, EV_KEY, BTN_RIGHT);
  2539. input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
  2540. __set_bit(INPUT_PROP_POINTER, dev2->propbit);
  2541. __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
  2542. error = input_register_device(dev2);
  2543. if (error) {
  2544. psmouse_err(psmouse,
  2545. "failed to register trackstick device: %d\n",
  2546. error);
  2547. input_free_device(dev2);
  2548. goto init_fail;
  2549. }
  2550. priv->dev2 = dev2;
  2551. }
  2552. priv->psmouse = psmouse;
  2553. INIT_DELAYED_WORK(&priv->dev3_register_work,
  2554. alps_register_bare_ps2_mouse);
  2555. psmouse->protocol_handler = alps_process_byte;
  2556. psmouse->poll = alps_poll;
  2557. psmouse->disconnect = alps_disconnect;
  2558. psmouse->reconnect = alps_reconnect;
  2559. psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
  2560. /* We are having trouble resyncing ALPS touchpads so disable it for now */
  2561. psmouse->resync_time = 0;
  2562. /* Allow 2 invalid packets without resetting device */
  2563. psmouse->resetafter = psmouse->pktsize * 2;
  2564. return 0;
  2565. init_fail:
  2566. psmouse_reset(psmouse);
  2567. /*
  2568. * Even though we did not allocate psmouse->private we do free
  2569. * it here.
  2570. */
  2571. kfree(psmouse->private);
  2572. psmouse->private = NULL;
  2573. return error;
  2574. }
  2575. int alps_detect(struct psmouse *psmouse, bool set_properties)
  2576. {
  2577. struct alps_data *priv;
  2578. int error;
  2579. error = alps_identify(psmouse, NULL);
  2580. if (error)
  2581. return error;
  2582. /*
  2583. * Reset the device to make sure it is fully operational:
  2584. * on some laptops, like certain Dell Latitudes, we may
  2585. * fail to properly detect presence of trackstick if device
  2586. * has not been reset.
  2587. */
  2588. psmouse_reset(psmouse);
  2589. priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
  2590. if (!priv)
  2591. return -ENOMEM;
  2592. error = alps_identify(psmouse, priv);
  2593. if (error) {
  2594. kfree(priv);
  2595. return error;
  2596. }
  2597. if (set_properties) {
  2598. psmouse->vendor = "ALPS";
  2599. psmouse->name = priv->flags & ALPS_DUALPOINT ?
  2600. "DualPoint TouchPad" : "GlidePoint";
  2601. psmouse->model = priv->proto_version;
  2602. } else {
  2603. /*
  2604. * Destroy alps_data structure we allocated earlier since
  2605. * this was just a "trial run". Otherwise we'll keep it
  2606. * to be used by alps_init() which has to be called if
  2607. * we succeed and set_properties is true.
  2608. */
  2609. kfree(priv);
  2610. psmouse->private = NULL;
  2611. }
  2612. return 0;
  2613. }