alps.c 82 KB

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