alps.c 82 KB

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