alps.c 81 KB

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