alps.c 84 KB

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