hid-logitech-hidpp.c 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HIDPP protocol for Logitech receivers
  4. *
  5. * Copyright (c) 2011 Logitech (c)
  6. * Copyright (c) 2012-2013 Google (c)
  7. * Copyright (c) 2013-2014 Red Hat Inc.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/device.h>
  11. #include <linux/input.h>
  12. #include <linux/usb.h>
  13. #include <linux/hid.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/sched.h>
  17. #include <linux/sched/clock.h>
  18. #include <linux/kfifo.h>
  19. #include <linux/input/mt.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/atomic.h>
  22. #include <linux/fixp-arith.h>
  23. #include <asm/unaligned.h>
  24. #include "usbhid/usbhid.h"
  25. #include "hid-ids.h"
  26. MODULE_LICENSE("GPL");
  27. MODULE_AUTHOR("Benjamin Tissoires <[email protected]>");
  28. MODULE_AUTHOR("Nestor Lopez Casado <[email protected]>");
  29. static bool disable_tap_to_click;
  30. module_param(disable_tap_to_click, bool, 0644);
  31. MODULE_PARM_DESC(disable_tap_to_click,
  32. "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
  33. /* Define a non-zero software ID to identify our own requests */
  34. #define LINUX_KERNEL_SW_ID 0x01
  35. #define REPORT_ID_HIDPP_SHORT 0x10
  36. #define REPORT_ID_HIDPP_LONG 0x11
  37. #define REPORT_ID_HIDPP_VERY_LONG 0x12
  38. #define HIDPP_REPORT_SHORT_LENGTH 7
  39. #define HIDPP_REPORT_LONG_LENGTH 20
  40. #define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
  41. #define HIDPP_REPORT_SHORT_SUPPORTED BIT(0)
  42. #define HIDPP_REPORT_LONG_SUPPORTED BIT(1)
  43. #define HIDPP_REPORT_VERY_LONG_SUPPORTED BIT(2)
  44. #define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
  45. #define HIDPP_SUB_ID_ROLLER 0x05
  46. #define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
  47. #define HIDPP_SUB_ID_USER_IFACE_EVENT 0x08
  48. #define HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST BIT(5)
  49. #define HIDPP_QUIRK_CLASS_WTP BIT(0)
  50. #define HIDPP_QUIRK_CLASS_M560 BIT(1)
  51. #define HIDPP_QUIRK_CLASS_K400 BIT(2)
  52. #define HIDPP_QUIRK_CLASS_G920 BIT(3)
  53. #define HIDPP_QUIRK_CLASS_K750 BIT(4)
  54. /* bits 2..20 are reserved for classes */
  55. /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
  56. #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
  57. #define HIDPP_QUIRK_DELAYED_INIT BIT(23)
  58. #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
  59. #define HIDPP_QUIRK_UNIFYING BIT(25)
  60. #define HIDPP_QUIRK_HIDPP_WHEELS BIT(26)
  61. #define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(27)
  62. #define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(28)
  63. #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(29)
  64. /* These are just aliases for now */
  65. #define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
  66. #define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
  67. /* Convenience constant to check for any high-res support. */
  68. #define HIDPP_CAPABILITY_HI_RES_SCROLL (HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL | \
  69. HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL | \
  70. HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL)
  71. #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
  72. #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
  73. #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
  74. #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
  75. #define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4)
  76. #define HIDPP_CAPABILITY_BATTERY_PERCENTAGE BIT(5)
  77. #define HIDPP_CAPABILITY_UNIFIED_BATTERY BIT(6)
  78. #define HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL BIT(7)
  79. #define HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL BIT(8)
  80. #define HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL BIT(9)
  81. #define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
  82. /*
  83. * There are two hidpp protocols in use, the first version hidpp10 is known
  84. * as register access protocol or RAP, the second version hidpp20 is known as
  85. * feature access protocol or FAP
  86. *
  87. * Most older devices (including the Unifying usb receiver) use the RAP protocol
  88. * where as most newer devices use the FAP protocol. Both protocols are
  89. * compatible with the underlying transport, which could be usb, Unifiying, or
  90. * bluetooth. The message lengths are defined by the hid vendor specific report
  91. * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
  92. * the HIDPP_LONG report type (total message length 20 bytes)
  93. *
  94. * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
  95. * messages. The Unifying receiver itself responds to RAP messages (device index
  96. * is 0xFF for the receiver), and all messages (short or long) with a device
  97. * index between 1 and 6 are passed untouched to the corresponding paired
  98. * Unifying device.
  99. *
  100. * The paired device can be RAP or FAP, it will receive the message untouched
  101. * from the Unifiying receiver.
  102. */
  103. struct fap {
  104. u8 feature_index;
  105. u8 funcindex_clientid;
  106. u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
  107. };
  108. struct rap {
  109. u8 sub_id;
  110. u8 reg_address;
  111. u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
  112. };
  113. struct hidpp_report {
  114. u8 report_id;
  115. u8 device_index;
  116. union {
  117. struct fap fap;
  118. struct rap rap;
  119. u8 rawbytes[sizeof(struct fap)];
  120. };
  121. } __packed;
  122. struct hidpp_battery {
  123. u8 feature_index;
  124. u8 solar_feature_index;
  125. u8 voltage_feature_index;
  126. struct power_supply_desc desc;
  127. struct power_supply *ps;
  128. char name[64];
  129. int status;
  130. int capacity;
  131. int level;
  132. int voltage;
  133. int charge_type;
  134. bool online;
  135. u8 supported_levels_1004;
  136. };
  137. /**
  138. * struct hidpp_scroll_counter - Utility class for processing high-resolution
  139. * scroll events.
  140. * @dev: the input device for which events should be reported.
  141. * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
  142. * @remainder: counts the number of high-resolution units moved since the last
  143. * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
  144. * only be used by class methods.
  145. * @direction: direction of last movement (1 or -1)
  146. * @last_time: last event time, used to reset remainder after inactivity
  147. */
  148. struct hidpp_scroll_counter {
  149. int wheel_multiplier;
  150. int remainder;
  151. int direction;
  152. unsigned long long last_time;
  153. };
  154. struct hidpp_device {
  155. struct hid_device *hid_dev;
  156. struct input_dev *input;
  157. struct mutex send_mutex;
  158. void *send_receive_buf;
  159. char *name; /* will never be NULL and should not be freed */
  160. wait_queue_head_t wait;
  161. int very_long_report_length;
  162. bool answer_available;
  163. u8 protocol_major;
  164. u8 protocol_minor;
  165. void *private_data;
  166. struct work_struct work;
  167. struct kfifo delayed_work_fifo;
  168. atomic_t connected;
  169. struct input_dev *delayed_input;
  170. unsigned long quirks;
  171. unsigned long capabilities;
  172. u8 supported_reports;
  173. struct hidpp_battery battery;
  174. struct hidpp_scroll_counter vertical_wheel_counter;
  175. u8 wireless_feature_index;
  176. };
  177. /* HID++ 1.0 error codes */
  178. #define HIDPP_ERROR 0x8f
  179. #define HIDPP_ERROR_SUCCESS 0x00
  180. #define HIDPP_ERROR_INVALID_SUBID 0x01
  181. #define HIDPP_ERROR_INVALID_ADRESS 0x02
  182. #define HIDPP_ERROR_INVALID_VALUE 0x03
  183. #define HIDPP_ERROR_CONNECT_FAIL 0x04
  184. #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
  185. #define HIDPP_ERROR_ALREADY_EXISTS 0x06
  186. #define HIDPP_ERROR_BUSY 0x07
  187. #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
  188. #define HIDPP_ERROR_RESOURCE_ERROR 0x09
  189. #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
  190. #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
  191. #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
  192. /* HID++ 2.0 error codes */
  193. #define HIDPP20_ERROR 0xff
  194. static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
  195. static int __hidpp_send_report(struct hid_device *hdev,
  196. struct hidpp_report *hidpp_report)
  197. {
  198. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  199. int fields_count, ret;
  200. switch (hidpp_report->report_id) {
  201. case REPORT_ID_HIDPP_SHORT:
  202. fields_count = HIDPP_REPORT_SHORT_LENGTH;
  203. break;
  204. case REPORT_ID_HIDPP_LONG:
  205. fields_count = HIDPP_REPORT_LONG_LENGTH;
  206. break;
  207. case REPORT_ID_HIDPP_VERY_LONG:
  208. fields_count = hidpp->very_long_report_length;
  209. break;
  210. default:
  211. return -ENODEV;
  212. }
  213. /*
  214. * set the device_index as the receiver, it will be overwritten by
  215. * hid_hw_request if needed
  216. */
  217. hidpp_report->device_index = 0xff;
  218. if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
  219. ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
  220. } else {
  221. ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
  222. (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
  223. HID_REQ_SET_REPORT);
  224. }
  225. return ret == fields_count ? 0 : -1;
  226. }
  227. /*
  228. * hidpp_send_message_sync() returns 0 in case of success, and something else
  229. * in case of a failure.
  230. * - If ' something else' is positive, that means that an error has been raised
  231. * by the protocol itself.
  232. * - If ' something else' is negative, that means that we had a classic error
  233. * (-ENOMEM, -EPIPE, etc...)
  234. */
  235. static int hidpp_send_message_sync(struct hidpp_device *hidpp,
  236. struct hidpp_report *message,
  237. struct hidpp_report *response)
  238. {
  239. int ret;
  240. mutex_lock(&hidpp->send_mutex);
  241. hidpp->send_receive_buf = response;
  242. hidpp->answer_available = false;
  243. /*
  244. * So that we can later validate the answer when it arrives
  245. * in hidpp_raw_event
  246. */
  247. *response = *message;
  248. ret = __hidpp_send_report(hidpp->hid_dev, message);
  249. if (ret) {
  250. dbg_hid("__hidpp_send_report returned err: %d\n", ret);
  251. memset(response, 0, sizeof(struct hidpp_report));
  252. goto exit;
  253. }
  254. if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
  255. 5*HZ)) {
  256. dbg_hid("%s:timeout waiting for response\n", __func__);
  257. memset(response, 0, sizeof(struct hidpp_report));
  258. ret = -ETIMEDOUT;
  259. }
  260. if (response->report_id == REPORT_ID_HIDPP_SHORT &&
  261. response->rap.sub_id == HIDPP_ERROR) {
  262. ret = response->rap.params[1];
  263. dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
  264. goto exit;
  265. }
  266. if ((response->report_id == REPORT_ID_HIDPP_LONG ||
  267. response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
  268. response->fap.feature_index == HIDPP20_ERROR) {
  269. ret = response->fap.params[1];
  270. dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
  271. goto exit;
  272. }
  273. exit:
  274. mutex_unlock(&hidpp->send_mutex);
  275. return ret;
  276. }
  277. static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
  278. u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
  279. struct hidpp_report *response)
  280. {
  281. struct hidpp_report *message;
  282. int ret;
  283. if (param_count > sizeof(message->fap.params))
  284. return -EINVAL;
  285. message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
  286. if (!message)
  287. return -ENOMEM;
  288. if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
  289. message->report_id = REPORT_ID_HIDPP_VERY_LONG;
  290. else
  291. message->report_id = REPORT_ID_HIDPP_LONG;
  292. message->fap.feature_index = feat_index;
  293. message->fap.funcindex_clientid = funcindex_clientid | LINUX_KERNEL_SW_ID;
  294. memcpy(&message->fap.params, params, param_count);
  295. ret = hidpp_send_message_sync(hidpp, message, response);
  296. kfree(message);
  297. return ret;
  298. }
  299. static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
  300. u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
  301. struct hidpp_report *response)
  302. {
  303. struct hidpp_report *message;
  304. int ret, max_count;
  305. /* Send as long report if short reports are not supported. */
  306. if (report_id == REPORT_ID_HIDPP_SHORT &&
  307. !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
  308. report_id = REPORT_ID_HIDPP_LONG;
  309. switch (report_id) {
  310. case REPORT_ID_HIDPP_SHORT:
  311. max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
  312. break;
  313. case REPORT_ID_HIDPP_LONG:
  314. max_count = HIDPP_REPORT_LONG_LENGTH - 4;
  315. break;
  316. case REPORT_ID_HIDPP_VERY_LONG:
  317. max_count = hidpp_dev->very_long_report_length - 4;
  318. break;
  319. default:
  320. return -EINVAL;
  321. }
  322. if (param_count > max_count)
  323. return -EINVAL;
  324. message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
  325. if (!message)
  326. return -ENOMEM;
  327. message->report_id = report_id;
  328. message->rap.sub_id = sub_id;
  329. message->rap.reg_address = reg_address;
  330. memcpy(&message->rap.params, params, param_count);
  331. ret = hidpp_send_message_sync(hidpp_dev, message, response);
  332. kfree(message);
  333. return ret;
  334. }
  335. static void delayed_work_cb(struct work_struct *work)
  336. {
  337. struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
  338. work);
  339. hidpp_connect_event(hidpp);
  340. }
  341. static inline bool hidpp_match_answer(struct hidpp_report *question,
  342. struct hidpp_report *answer)
  343. {
  344. return (answer->fap.feature_index == question->fap.feature_index) &&
  345. (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
  346. }
  347. static inline bool hidpp_match_error(struct hidpp_report *question,
  348. struct hidpp_report *answer)
  349. {
  350. return ((answer->rap.sub_id == HIDPP_ERROR) ||
  351. (answer->fap.feature_index == HIDPP20_ERROR)) &&
  352. (answer->fap.funcindex_clientid == question->fap.feature_index) &&
  353. (answer->fap.params[0] == question->fap.funcindex_clientid);
  354. }
  355. static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp,
  356. struct hidpp_report *report)
  357. {
  358. return (hidpp->wireless_feature_index &&
  359. (report->fap.feature_index == hidpp->wireless_feature_index)) ||
  360. ((report->report_id == REPORT_ID_HIDPP_SHORT) &&
  361. (report->rap.sub_id == 0x41));
  362. }
  363. /*
  364. * hidpp_prefix_name() prefixes the current given name with "Logitech ".
  365. */
  366. static void hidpp_prefix_name(char **name, int name_length)
  367. {
  368. #define PREFIX_LENGTH 9 /* "Logitech " */
  369. int new_length;
  370. char *new_name;
  371. if (name_length > PREFIX_LENGTH &&
  372. strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
  373. /* The prefix has is already in the name */
  374. return;
  375. new_length = PREFIX_LENGTH + name_length;
  376. new_name = kzalloc(new_length, GFP_KERNEL);
  377. if (!new_name)
  378. return;
  379. snprintf(new_name, new_length, "Logitech %s", *name);
  380. kfree(*name);
  381. *name = new_name;
  382. }
  383. /**
  384. * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
  385. * events given a high-resolution wheel
  386. * movement.
  387. * @input_dev: Pointer to the input device
  388. * @counter: a hid_scroll_counter struct describing the wheel.
  389. * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
  390. * units.
  391. *
  392. * Given a high-resolution movement, this function converts the movement into
  393. * fractions of 120 and emits high-resolution scroll events for the input
  394. * device. It also uses the multiplier from &struct hid_scroll_counter to
  395. * emit low-resolution scroll events when appropriate for
  396. * backwards-compatibility with userspace input libraries.
  397. */
  398. static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
  399. struct hidpp_scroll_counter *counter,
  400. int hi_res_value)
  401. {
  402. int low_res_value, remainder, direction;
  403. unsigned long long now, previous;
  404. hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
  405. input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
  406. remainder = counter->remainder;
  407. direction = hi_res_value > 0 ? 1 : -1;
  408. now = sched_clock();
  409. previous = counter->last_time;
  410. counter->last_time = now;
  411. /*
  412. * Reset the remainder after a period of inactivity or when the
  413. * direction changes. This prevents the REL_WHEEL emulation point
  414. * from sliding for devices that don't always provide the same
  415. * number of movements per detent.
  416. */
  417. if (now - previous > 1000000000 || direction != counter->direction)
  418. remainder = 0;
  419. counter->direction = direction;
  420. remainder += hi_res_value;
  421. /* Some wheels will rest 7/8ths of a detent from the previous detent
  422. * after slow movement, so we want the threshold for low-res events to
  423. * be in the middle between two detents (e.g. after 4/8ths) as
  424. * opposed to on the detents themselves (8/8ths).
  425. */
  426. if (abs(remainder) >= 60) {
  427. /* Add (or subtract) 1 because we want to trigger when the wheel
  428. * is half-way to the next detent (i.e. scroll 1 detent after a
  429. * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
  430. * etc.).
  431. */
  432. low_res_value = remainder / 120;
  433. if (low_res_value == 0)
  434. low_res_value = (hi_res_value > 0 ? 1 : -1);
  435. input_report_rel(input_dev, REL_WHEEL, low_res_value);
  436. remainder -= low_res_value * 120;
  437. }
  438. counter->remainder = remainder;
  439. }
  440. /* -------------------------------------------------------------------------- */
  441. /* HIDP++ 1.0 commands */
  442. /* -------------------------------------------------------------------------- */
  443. #define HIDPP_SET_REGISTER 0x80
  444. #define HIDPP_GET_REGISTER 0x81
  445. #define HIDPP_SET_LONG_REGISTER 0x82
  446. #define HIDPP_GET_LONG_REGISTER 0x83
  447. /**
  448. * hidpp10_set_register - Modify a HID++ 1.0 register.
  449. * @hidpp_dev: the device to set the register on.
  450. * @register_address: the address of the register to modify.
  451. * @byte: the byte of the register to modify. Should be less than 3.
  452. * @mask: mask of the bits to modify
  453. * @value: new values for the bits in mask
  454. * Return: 0 if successful, otherwise a negative error code.
  455. */
  456. static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
  457. u8 register_address, u8 byte, u8 mask, u8 value)
  458. {
  459. struct hidpp_report response;
  460. int ret;
  461. u8 params[3] = { 0 };
  462. ret = hidpp_send_rap_command_sync(hidpp_dev,
  463. REPORT_ID_HIDPP_SHORT,
  464. HIDPP_GET_REGISTER,
  465. register_address,
  466. NULL, 0, &response);
  467. if (ret)
  468. return ret;
  469. memcpy(params, response.rap.params, 3);
  470. params[byte] &= ~mask;
  471. params[byte] |= value & mask;
  472. return hidpp_send_rap_command_sync(hidpp_dev,
  473. REPORT_ID_HIDPP_SHORT,
  474. HIDPP_SET_REGISTER,
  475. register_address,
  476. params, 3, &response);
  477. }
  478. #define HIDPP_REG_ENABLE_REPORTS 0x00
  479. #define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
  480. #define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
  481. #define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
  482. #define HIDPP_ENABLE_BAT_REPORT BIT(4)
  483. #define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
  484. static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
  485. {
  486. return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
  487. HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
  488. }
  489. #define HIDPP_REG_FEATURES 0x01
  490. #define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
  491. #define HIDPP_ENABLE_FAST_SCROLL BIT(6)
  492. /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
  493. static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
  494. {
  495. return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
  496. HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
  497. }
  498. #define HIDPP_REG_BATTERY_STATUS 0x07
  499. static int hidpp10_battery_status_map_level(u8 param)
  500. {
  501. int level;
  502. switch (param) {
  503. case 1 ... 2:
  504. level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  505. break;
  506. case 3 ... 4:
  507. level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  508. break;
  509. case 5 ... 6:
  510. level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  511. break;
  512. case 7:
  513. level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
  514. break;
  515. default:
  516. level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
  517. }
  518. return level;
  519. }
  520. static int hidpp10_battery_status_map_status(u8 param)
  521. {
  522. int status;
  523. switch (param) {
  524. case 0x00:
  525. /* discharging (in use) */
  526. status = POWER_SUPPLY_STATUS_DISCHARGING;
  527. break;
  528. case 0x21: /* (standard) charging */
  529. case 0x24: /* fast charging */
  530. case 0x25: /* slow charging */
  531. status = POWER_SUPPLY_STATUS_CHARGING;
  532. break;
  533. case 0x26: /* topping charge */
  534. case 0x22: /* charge complete */
  535. status = POWER_SUPPLY_STATUS_FULL;
  536. break;
  537. case 0x20: /* unknown */
  538. status = POWER_SUPPLY_STATUS_UNKNOWN;
  539. break;
  540. /*
  541. * 0x01...0x1F = reserved (not charging)
  542. * 0x23 = charging error
  543. * 0x27..0xff = reserved
  544. */
  545. default:
  546. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  547. break;
  548. }
  549. return status;
  550. }
  551. static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
  552. {
  553. struct hidpp_report response;
  554. int ret, status;
  555. ret = hidpp_send_rap_command_sync(hidpp,
  556. REPORT_ID_HIDPP_SHORT,
  557. HIDPP_GET_REGISTER,
  558. HIDPP_REG_BATTERY_STATUS,
  559. NULL, 0, &response);
  560. if (ret)
  561. return ret;
  562. hidpp->battery.level =
  563. hidpp10_battery_status_map_level(response.rap.params[0]);
  564. status = hidpp10_battery_status_map_status(response.rap.params[1]);
  565. hidpp->battery.status = status;
  566. /* the capacity is only available when discharging or full */
  567. hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
  568. status == POWER_SUPPLY_STATUS_FULL;
  569. return 0;
  570. }
  571. #define HIDPP_REG_BATTERY_MILEAGE 0x0D
  572. static int hidpp10_battery_mileage_map_status(u8 param)
  573. {
  574. int status;
  575. switch (param >> 6) {
  576. case 0x00:
  577. /* discharging (in use) */
  578. status = POWER_SUPPLY_STATUS_DISCHARGING;
  579. break;
  580. case 0x01: /* charging */
  581. status = POWER_SUPPLY_STATUS_CHARGING;
  582. break;
  583. case 0x02: /* charge complete */
  584. status = POWER_SUPPLY_STATUS_FULL;
  585. break;
  586. /*
  587. * 0x03 = charging error
  588. */
  589. default:
  590. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  591. break;
  592. }
  593. return status;
  594. }
  595. static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
  596. {
  597. struct hidpp_report response;
  598. int ret, status;
  599. ret = hidpp_send_rap_command_sync(hidpp,
  600. REPORT_ID_HIDPP_SHORT,
  601. HIDPP_GET_REGISTER,
  602. HIDPP_REG_BATTERY_MILEAGE,
  603. NULL, 0, &response);
  604. if (ret)
  605. return ret;
  606. hidpp->battery.capacity = response.rap.params[0];
  607. status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
  608. hidpp->battery.status = status;
  609. /* the capacity is only available when discharging or full */
  610. hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
  611. status == POWER_SUPPLY_STATUS_FULL;
  612. return 0;
  613. }
  614. static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
  615. {
  616. struct hidpp_report *report = (struct hidpp_report *)data;
  617. int status, capacity, level;
  618. bool changed;
  619. if (report->report_id != REPORT_ID_HIDPP_SHORT)
  620. return 0;
  621. switch (report->rap.sub_id) {
  622. case HIDPP_REG_BATTERY_STATUS:
  623. capacity = hidpp->battery.capacity;
  624. level = hidpp10_battery_status_map_level(report->rawbytes[1]);
  625. status = hidpp10_battery_status_map_status(report->rawbytes[2]);
  626. break;
  627. case HIDPP_REG_BATTERY_MILEAGE:
  628. capacity = report->rap.params[0];
  629. level = hidpp->battery.level;
  630. status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
  631. break;
  632. default:
  633. return 0;
  634. }
  635. changed = capacity != hidpp->battery.capacity ||
  636. level != hidpp->battery.level ||
  637. status != hidpp->battery.status;
  638. /* the capacity is only available when discharging or full */
  639. hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
  640. status == POWER_SUPPLY_STATUS_FULL;
  641. if (changed) {
  642. hidpp->battery.level = level;
  643. hidpp->battery.status = status;
  644. if (hidpp->battery.ps)
  645. power_supply_changed(hidpp->battery.ps);
  646. }
  647. return 0;
  648. }
  649. #define HIDPP_REG_PAIRING_INFORMATION 0xB5
  650. #define HIDPP_EXTENDED_PAIRING 0x30
  651. #define HIDPP_DEVICE_NAME 0x40
  652. static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
  653. {
  654. struct hidpp_report response;
  655. int ret;
  656. u8 params[1] = { HIDPP_DEVICE_NAME };
  657. char *name;
  658. int len;
  659. ret = hidpp_send_rap_command_sync(hidpp_dev,
  660. REPORT_ID_HIDPP_SHORT,
  661. HIDPP_GET_LONG_REGISTER,
  662. HIDPP_REG_PAIRING_INFORMATION,
  663. params, 1, &response);
  664. if (ret)
  665. return NULL;
  666. len = response.rap.params[1];
  667. if (2 + len > sizeof(response.rap.params))
  668. return NULL;
  669. if (len < 4) /* logitech devices are usually at least Xddd */
  670. return NULL;
  671. name = kzalloc(len + 1, GFP_KERNEL);
  672. if (!name)
  673. return NULL;
  674. memcpy(name, &response.rap.params[2], len);
  675. /* include the terminating '\0' */
  676. hidpp_prefix_name(&name, len + 1);
  677. return name;
  678. }
  679. static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
  680. {
  681. struct hidpp_report response;
  682. int ret;
  683. u8 params[1] = { HIDPP_EXTENDED_PAIRING };
  684. ret = hidpp_send_rap_command_sync(hidpp,
  685. REPORT_ID_HIDPP_SHORT,
  686. HIDPP_GET_LONG_REGISTER,
  687. HIDPP_REG_PAIRING_INFORMATION,
  688. params, 1, &response);
  689. if (ret)
  690. return ret;
  691. /*
  692. * We don't care about LE or BE, we will output it as a string
  693. * with %4phD, so we need to keep the order.
  694. */
  695. *serial = *((u32 *)&response.rap.params[1]);
  696. return 0;
  697. }
  698. static int hidpp_unifying_init(struct hidpp_device *hidpp)
  699. {
  700. struct hid_device *hdev = hidpp->hid_dev;
  701. const char *name;
  702. u32 serial;
  703. int ret;
  704. ret = hidpp_unifying_get_serial(hidpp, &serial);
  705. if (ret)
  706. return ret;
  707. snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial);
  708. dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
  709. name = hidpp_unifying_get_name(hidpp);
  710. if (!name)
  711. return -EIO;
  712. snprintf(hdev->name, sizeof(hdev->name), "%s", name);
  713. dbg_hid("HID++ Unifying: Got name: %s\n", name);
  714. kfree(name);
  715. return 0;
  716. }
  717. /* -------------------------------------------------------------------------- */
  718. /* 0x0000: Root */
  719. /* -------------------------------------------------------------------------- */
  720. #define HIDPP_PAGE_ROOT 0x0000
  721. #define HIDPP_PAGE_ROOT_IDX 0x00
  722. #define CMD_ROOT_GET_FEATURE 0x00
  723. #define CMD_ROOT_GET_PROTOCOL_VERSION 0x10
  724. static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
  725. u8 *feature_index, u8 *feature_type)
  726. {
  727. struct hidpp_report response;
  728. int ret;
  729. u8 params[2] = { feature >> 8, feature & 0x00FF };
  730. ret = hidpp_send_fap_command_sync(hidpp,
  731. HIDPP_PAGE_ROOT_IDX,
  732. CMD_ROOT_GET_FEATURE,
  733. params, 2, &response);
  734. if (ret)
  735. return ret;
  736. if (response.fap.params[0] == 0)
  737. return -ENOENT;
  738. *feature_index = response.fap.params[0];
  739. *feature_type = response.fap.params[1];
  740. return ret;
  741. }
  742. static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
  743. {
  744. const u8 ping_byte = 0x5a;
  745. u8 ping_data[3] = { 0, 0, ping_byte };
  746. struct hidpp_report response;
  747. int ret;
  748. ret = hidpp_send_rap_command_sync(hidpp,
  749. REPORT_ID_HIDPP_SHORT,
  750. HIDPP_PAGE_ROOT_IDX,
  751. CMD_ROOT_GET_PROTOCOL_VERSION,
  752. ping_data, sizeof(ping_data), &response);
  753. if (ret == HIDPP_ERROR_INVALID_SUBID) {
  754. hidpp->protocol_major = 1;
  755. hidpp->protocol_minor = 0;
  756. goto print_version;
  757. }
  758. /* the device might not be connected */
  759. if (ret == HIDPP_ERROR_RESOURCE_ERROR)
  760. return -EIO;
  761. if (ret > 0) {
  762. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  763. __func__, ret);
  764. return -EPROTO;
  765. }
  766. if (ret)
  767. return ret;
  768. if (response.rap.params[2] != ping_byte) {
  769. hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
  770. __func__, response.rap.params[2], ping_byte);
  771. return -EPROTO;
  772. }
  773. hidpp->protocol_major = response.rap.params[0];
  774. hidpp->protocol_minor = response.rap.params[1];
  775. print_version:
  776. hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
  777. hidpp->protocol_major, hidpp->protocol_minor);
  778. return 0;
  779. }
  780. /* -------------------------------------------------------------------------- */
  781. /* 0x0003: Device Information */
  782. /* -------------------------------------------------------------------------- */
  783. #define HIDPP_PAGE_DEVICE_INFORMATION 0x0003
  784. #define CMD_GET_DEVICE_INFO 0x00
  785. static int hidpp_get_serial(struct hidpp_device *hidpp, u32 *serial)
  786. {
  787. struct hidpp_report response;
  788. u8 feature_type;
  789. u8 feature_index;
  790. int ret;
  791. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_DEVICE_INFORMATION,
  792. &feature_index,
  793. &feature_type);
  794. if (ret)
  795. return ret;
  796. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  797. CMD_GET_DEVICE_INFO,
  798. NULL, 0, &response);
  799. if (ret)
  800. return ret;
  801. /* See hidpp_unifying_get_serial() */
  802. *serial = *((u32 *)&response.rap.params[1]);
  803. return 0;
  804. }
  805. static int hidpp_serial_init(struct hidpp_device *hidpp)
  806. {
  807. struct hid_device *hdev = hidpp->hid_dev;
  808. u32 serial;
  809. int ret;
  810. ret = hidpp_get_serial(hidpp, &serial);
  811. if (ret)
  812. return ret;
  813. snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial);
  814. dbg_hid("HID++ DeviceInformation: Got serial: %s\n", hdev->uniq);
  815. return 0;
  816. }
  817. /* -------------------------------------------------------------------------- */
  818. /* 0x0005: GetDeviceNameType */
  819. /* -------------------------------------------------------------------------- */
  820. #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
  821. #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x00
  822. #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x10
  823. #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x20
  824. static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
  825. u8 feature_index, u8 *nameLength)
  826. {
  827. struct hidpp_report response;
  828. int ret;
  829. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  830. CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
  831. if (ret > 0) {
  832. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  833. __func__, ret);
  834. return -EPROTO;
  835. }
  836. if (ret)
  837. return ret;
  838. *nameLength = response.fap.params[0];
  839. return ret;
  840. }
  841. static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
  842. u8 feature_index, u8 char_index, char *device_name, int len_buf)
  843. {
  844. struct hidpp_report response;
  845. int ret, i;
  846. int count;
  847. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  848. CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
  849. &response);
  850. if (ret > 0) {
  851. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  852. __func__, ret);
  853. return -EPROTO;
  854. }
  855. if (ret)
  856. return ret;
  857. switch (response.report_id) {
  858. case REPORT_ID_HIDPP_VERY_LONG:
  859. count = hidpp->very_long_report_length - 4;
  860. break;
  861. case REPORT_ID_HIDPP_LONG:
  862. count = HIDPP_REPORT_LONG_LENGTH - 4;
  863. break;
  864. case REPORT_ID_HIDPP_SHORT:
  865. count = HIDPP_REPORT_SHORT_LENGTH - 4;
  866. break;
  867. default:
  868. return -EPROTO;
  869. }
  870. if (len_buf < count)
  871. count = len_buf;
  872. for (i = 0; i < count; i++)
  873. device_name[i] = response.fap.params[i];
  874. return count;
  875. }
  876. static char *hidpp_get_device_name(struct hidpp_device *hidpp)
  877. {
  878. u8 feature_type;
  879. u8 feature_index;
  880. u8 __name_length;
  881. char *name;
  882. unsigned index = 0;
  883. int ret;
  884. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
  885. &feature_index, &feature_type);
  886. if (ret)
  887. return NULL;
  888. ret = hidpp_devicenametype_get_count(hidpp, feature_index,
  889. &__name_length);
  890. if (ret)
  891. return NULL;
  892. name = kzalloc(__name_length + 1, GFP_KERNEL);
  893. if (!name)
  894. return NULL;
  895. while (index < __name_length) {
  896. ret = hidpp_devicenametype_get_device_name(hidpp,
  897. feature_index, index, name + index,
  898. __name_length - index);
  899. if (ret <= 0) {
  900. kfree(name);
  901. return NULL;
  902. }
  903. index += ret;
  904. }
  905. /* include the terminating '\0' */
  906. hidpp_prefix_name(&name, __name_length + 1);
  907. return name;
  908. }
  909. /* -------------------------------------------------------------------------- */
  910. /* 0x1000: Battery level status */
  911. /* -------------------------------------------------------------------------- */
  912. #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
  913. #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
  914. #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
  915. #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
  916. #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
  917. #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
  918. #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
  919. static int hidpp_map_battery_level(int capacity)
  920. {
  921. if (capacity < 11)
  922. return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  923. /*
  924. * The spec says this should be < 31 but some devices report 30
  925. * with brand new batteries and Windows reports 30 as "Good".
  926. */
  927. else if (capacity < 30)
  928. return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  929. else if (capacity < 81)
  930. return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  931. return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  932. }
  933. static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
  934. int *next_capacity,
  935. int *level)
  936. {
  937. int status;
  938. *capacity = data[0];
  939. *next_capacity = data[1];
  940. *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
  941. /* When discharging, we can rely on the device reported capacity.
  942. * For all other states the device reports 0 (unknown).
  943. */
  944. switch (data[2]) {
  945. case 0: /* discharging (in use) */
  946. status = POWER_SUPPLY_STATUS_DISCHARGING;
  947. *level = hidpp_map_battery_level(*capacity);
  948. break;
  949. case 1: /* recharging */
  950. status = POWER_SUPPLY_STATUS_CHARGING;
  951. break;
  952. case 2: /* charge in final stage */
  953. status = POWER_SUPPLY_STATUS_CHARGING;
  954. break;
  955. case 3: /* charge complete */
  956. status = POWER_SUPPLY_STATUS_FULL;
  957. *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  958. *capacity = 100;
  959. break;
  960. case 4: /* recharging below optimal speed */
  961. status = POWER_SUPPLY_STATUS_CHARGING;
  962. break;
  963. /* 5 = invalid battery type
  964. 6 = thermal error
  965. 7 = other charging error */
  966. default:
  967. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  968. break;
  969. }
  970. return status;
  971. }
  972. static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
  973. u8 feature_index,
  974. int *status,
  975. int *capacity,
  976. int *next_capacity,
  977. int *level)
  978. {
  979. struct hidpp_report response;
  980. int ret;
  981. u8 *params = (u8 *)response.fap.params;
  982. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  983. CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
  984. NULL, 0, &response);
  985. /* Ignore these intermittent errors */
  986. if (ret == HIDPP_ERROR_RESOURCE_ERROR)
  987. return -EIO;
  988. if (ret > 0) {
  989. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  990. __func__, ret);
  991. return -EPROTO;
  992. }
  993. if (ret)
  994. return ret;
  995. *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
  996. next_capacity,
  997. level);
  998. return 0;
  999. }
  1000. static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
  1001. u8 feature_index)
  1002. {
  1003. struct hidpp_report response;
  1004. int ret;
  1005. u8 *params = (u8 *)response.fap.params;
  1006. unsigned int level_count, flags;
  1007. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  1008. CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
  1009. NULL, 0, &response);
  1010. if (ret > 0) {
  1011. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  1012. __func__, ret);
  1013. return -EPROTO;
  1014. }
  1015. if (ret)
  1016. return ret;
  1017. level_count = params[0];
  1018. flags = params[1];
  1019. if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
  1020. hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
  1021. else
  1022. hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
  1023. return 0;
  1024. }
  1025. static int hidpp20_query_battery_info_1000(struct hidpp_device *hidpp)
  1026. {
  1027. u8 feature_type;
  1028. int ret;
  1029. int status, capacity, next_capacity, level;
  1030. if (hidpp->battery.feature_index == 0xff) {
  1031. ret = hidpp_root_get_feature(hidpp,
  1032. HIDPP_PAGE_BATTERY_LEVEL_STATUS,
  1033. &hidpp->battery.feature_index,
  1034. &feature_type);
  1035. if (ret)
  1036. return ret;
  1037. }
  1038. ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
  1039. hidpp->battery.feature_index,
  1040. &status, &capacity,
  1041. &next_capacity, &level);
  1042. if (ret)
  1043. return ret;
  1044. ret = hidpp20_batterylevel_get_battery_info(hidpp,
  1045. hidpp->battery.feature_index);
  1046. if (ret)
  1047. return ret;
  1048. hidpp->battery.status = status;
  1049. hidpp->battery.capacity = capacity;
  1050. hidpp->battery.level = level;
  1051. /* the capacity is only available when discharging or full */
  1052. hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
  1053. status == POWER_SUPPLY_STATUS_FULL;
  1054. return 0;
  1055. }
  1056. static int hidpp20_battery_event_1000(struct hidpp_device *hidpp,
  1057. u8 *data, int size)
  1058. {
  1059. struct hidpp_report *report = (struct hidpp_report *)data;
  1060. int status, capacity, next_capacity, level;
  1061. bool changed;
  1062. if (report->fap.feature_index != hidpp->battery.feature_index ||
  1063. report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
  1064. return 0;
  1065. status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
  1066. &capacity,
  1067. &next_capacity,
  1068. &level);
  1069. /* the capacity is only available when discharging or full */
  1070. hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
  1071. status == POWER_SUPPLY_STATUS_FULL;
  1072. changed = capacity != hidpp->battery.capacity ||
  1073. level != hidpp->battery.level ||
  1074. status != hidpp->battery.status;
  1075. if (changed) {
  1076. hidpp->battery.level = level;
  1077. hidpp->battery.capacity = capacity;
  1078. hidpp->battery.status = status;
  1079. if (hidpp->battery.ps)
  1080. power_supply_changed(hidpp->battery.ps);
  1081. }
  1082. return 0;
  1083. }
  1084. /* -------------------------------------------------------------------------- */
  1085. /* 0x1001: Battery voltage */
  1086. /* -------------------------------------------------------------------------- */
  1087. #define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
  1088. #define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
  1089. #define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
  1090. static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
  1091. int *level, int *charge_type)
  1092. {
  1093. int status;
  1094. long flags = (long) data[2];
  1095. *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
  1096. if (flags & 0x80)
  1097. switch (flags & 0x07) {
  1098. case 0:
  1099. status = POWER_SUPPLY_STATUS_CHARGING;
  1100. break;
  1101. case 1:
  1102. status = POWER_SUPPLY_STATUS_FULL;
  1103. *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  1104. break;
  1105. case 2:
  1106. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  1107. break;
  1108. default:
  1109. status = POWER_SUPPLY_STATUS_UNKNOWN;
  1110. break;
  1111. }
  1112. else
  1113. status = POWER_SUPPLY_STATUS_DISCHARGING;
  1114. *charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
  1115. if (test_bit(3, &flags)) {
  1116. *charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
  1117. }
  1118. if (test_bit(4, &flags)) {
  1119. *charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  1120. }
  1121. if (test_bit(5, &flags)) {
  1122. *level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  1123. }
  1124. *voltage = get_unaligned_be16(data);
  1125. return status;
  1126. }
  1127. static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
  1128. u8 feature_index,
  1129. int *status, int *voltage,
  1130. int *level, int *charge_type)
  1131. {
  1132. struct hidpp_report response;
  1133. int ret;
  1134. u8 *params = (u8 *)response.fap.params;
  1135. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  1136. CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE,
  1137. NULL, 0, &response);
  1138. if (ret > 0) {
  1139. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  1140. __func__, ret);
  1141. return -EPROTO;
  1142. }
  1143. if (ret)
  1144. return ret;
  1145. hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE;
  1146. *status = hidpp20_battery_map_status_voltage(params, voltage,
  1147. level, charge_type);
  1148. return 0;
  1149. }
  1150. static int hidpp20_map_battery_capacity(struct hid_device *hid_dev, int voltage)
  1151. {
  1152. /* NB: This voltage curve doesn't necessarily map perfectly to all
  1153. * devices that implement the BATTERY_VOLTAGE feature. This is because
  1154. * there are a few devices that use different battery technology.
  1155. */
  1156. static const int voltages[] = {
  1157. 4186, 4156, 4143, 4133, 4122, 4113, 4103, 4094, 4086, 4075,
  1158. 4067, 4059, 4051, 4043, 4035, 4027, 4019, 4011, 4003, 3997,
  1159. 3989, 3983, 3976, 3969, 3961, 3955, 3949, 3942, 3935, 3929,
  1160. 3922, 3916, 3909, 3902, 3896, 3890, 3883, 3877, 3870, 3865,
  1161. 3859, 3853, 3848, 3842, 3837, 3833, 3828, 3824, 3819, 3815,
  1162. 3811, 3808, 3804, 3800, 3797, 3793, 3790, 3787, 3784, 3781,
  1163. 3778, 3775, 3772, 3770, 3767, 3764, 3762, 3759, 3757, 3754,
  1164. 3751, 3748, 3744, 3741, 3737, 3734, 3730, 3726, 3724, 3720,
  1165. 3717, 3714, 3710, 3706, 3702, 3697, 3693, 3688, 3683, 3677,
  1166. 3671, 3666, 3662, 3658, 3654, 3646, 3633, 3612, 3579, 3537
  1167. };
  1168. int i;
  1169. BUILD_BUG_ON(ARRAY_SIZE(voltages) != 100);
  1170. if (unlikely(voltage < 3500 || voltage >= 5000))
  1171. hid_warn_once(hid_dev,
  1172. "%s: possibly using the wrong voltage curve\n",
  1173. __func__);
  1174. for (i = 0; i < ARRAY_SIZE(voltages); i++) {
  1175. if (voltage >= voltages[i])
  1176. return ARRAY_SIZE(voltages) - i;
  1177. }
  1178. return 0;
  1179. }
  1180. static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
  1181. {
  1182. u8 feature_type;
  1183. int ret;
  1184. int status, voltage, level, charge_type;
  1185. if (hidpp->battery.voltage_feature_index == 0xff) {
  1186. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE,
  1187. &hidpp->battery.voltage_feature_index,
  1188. &feature_type);
  1189. if (ret)
  1190. return ret;
  1191. }
  1192. ret = hidpp20_battery_get_battery_voltage(hidpp,
  1193. hidpp->battery.voltage_feature_index,
  1194. &status, &voltage, &level, &charge_type);
  1195. if (ret)
  1196. return ret;
  1197. hidpp->battery.status = status;
  1198. hidpp->battery.voltage = voltage;
  1199. hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
  1200. voltage);
  1201. hidpp->battery.level = level;
  1202. hidpp->battery.charge_type = charge_type;
  1203. hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
  1204. return 0;
  1205. }
  1206. static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,
  1207. u8 *data, int size)
  1208. {
  1209. struct hidpp_report *report = (struct hidpp_report *)data;
  1210. int status, voltage, level, charge_type;
  1211. if (report->fap.feature_index != hidpp->battery.voltage_feature_index ||
  1212. report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST)
  1213. return 0;
  1214. status = hidpp20_battery_map_status_voltage(report->fap.params, &voltage,
  1215. &level, &charge_type);
  1216. hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
  1217. if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
  1218. hidpp->battery.voltage = voltage;
  1219. hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
  1220. voltage);
  1221. hidpp->battery.status = status;
  1222. hidpp->battery.level = level;
  1223. hidpp->battery.charge_type = charge_type;
  1224. if (hidpp->battery.ps)
  1225. power_supply_changed(hidpp->battery.ps);
  1226. }
  1227. return 0;
  1228. }
  1229. /* -------------------------------------------------------------------------- */
  1230. /* 0x1004: Unified battery */
  1231. /* -------------------------------------------------------------------------- */
  1232. #define HIDPP_PAGE_UNIFIED_BATTERY 0x1004
  1233. #define CMD_UNIFIED_BATTERY_GET_CAPABILITIES 0x00
  1234. #define CMD_UNIFIED_BATTERY_GET_STATUS 0x10
  1235. #define EVENT_UNIFIED_BATTERY_STATUS_EVENT 0x00
  1236. #define FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL BIT(0)
  1237. #define FLAG_UNIFIED_BATTERY_LEVEL_LOW BIT(1)
  1238. #define FLAG_UNIFIED_BATTERY_LEVEL_GOOD BIT(2)
  1239. #define FLAG_UNIFIED_BATTERY_LEVEL_FULL BIT(3)
  1240. #define FLAG_UNIFIED_BATTERY_FLAGS_RECHARGEABLE BIT(0)
  1241. #define FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE BIT(1)
  1242. static int hidpp20_unifiedbattery_get_capabilities(struct hidpp_device *hidpp,
  1243. u8 feature_index)
  1244. {
  1245. struct hidpp_report response;
  1246. int ret;
  1247. u8 *params = (u8 *)response.fap.params;
  1248. if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS ||
  1249. hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) {
  1250. /* we have already set the device capabilities, so let's skip */
  1251. return 0;
  1252. }
  1253. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  1254. CMD_UNIFIED_BATTERY_GET_CAPABILITIES,
  1255. NULL, 0, &response);
  1256. /* Ignore these intermittent errors */
  1257. if (ret == HIDPP_ERROR_RESOURCE_ERROR)
  1258. return -EIO;
  1259. if (ret > 0) {
  1260. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  1261. __func__, ret);
  1262. return -EPROTO;
  1263. }
  1264. if (ret)
  1265. return ret;
  1266. /*
  1267. * If the device supports state of charge (battery percentage) we won't
  1268. * export the battery level information. there are 4 possible battery
  1269. * levels and they all are optional, this means that the device might
  1270. * not support any of them, we are just better off with the battery
  1271. * percentage.
  1272. */
  1273. if (params[1] & FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE) {
  1274. hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_PERCENTAGE;
  1275. hidpp->battery.supported_levels_1004 = 0;
  1276. } else {
  1277. hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
  1278. hidpp->battery.supported_levels_1004 = params[0];
  1279. }
  1280. return 0;
  1281. }
  1282. static int hidpp20_unifiedbattery_map_status(struct hidpp_device *hidpp,
  1283. u8 charging_status,
  1284. u8 external_power_status)
  1285. {
  1286. int status;
  1287. switch (charging_status) {
  1288. case 0: /* discharging */
  1289. status = POWER_SUPPLY_STATUS_DISCHARGING;
  1290. break;
  1291. case 1: /* charging */
  1292. case 2: /* charging slow */
  1293. status = POWER_SUPPLY_STATUS_CHARGING;
  1294. break;
  1295. case 3: /* complete */
  1296. status = POWER_SUPPLY_STATUS_FULL;
  1297. break;
  1298. case 4: /* error */
  1299. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  1300. hid_info(hidpp->hid_dev, "%s: charging error",
  1301. hidpp->name);
  1302. break;
  1303. default:
  1304. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  1305. break;
  1306. }
  1307. return status;
  1308. }
  1309. static int hidpp20_unifiedbattery_map_level(struct hidpp_device *hidpp,
  1310. u8 battery_level)
  1311. {
  1312. /* cler unsupported level bits */
  1313. battery_level &= hidpp->battery.supported_levels_1004;
  1314. if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_FULL)
  1315. return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  1316. else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_GOOD)
  1317. return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  1318. else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_LOW)
  1319. return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  1320. else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL)
  1321. return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  1322. return POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
  1323. }
  1324. static int hidpp20_unifiedbattery_get_status(struct hidpp_device *hidpp,
  1325. u8 feature_index,
  1326. u8 *state_of_charge,
  1327. int *status,
  1328. int *level)
  1329. {
  1330. struct hidpp_report response;
  1331. int ret;
  1332. u8 *params = (u8 *)response.fap.params;
  1333. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  1334. CMD_UNIFIED_BATTERY_GET_STATUS,
  1335. NULL, 0, &response);
  1336. /* Ignore these intermittent errors */
  1337. if (ret == HIDPP_ERROR_RESOURCE_ERROR)
  1338. return -EIO;
  1339. if (ret > 0) {
  1340. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  1341. __func__, ret);
  1342. return -EPROTO;
  1343. }
  1344. if (ret)
  1345. return ret;
  1346. *state_of_charge = params[0];
  1347. *status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
  1348. *level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
  1349. return 0;
  1350. }
  1351. static int hidpp20_query_battery_info_1004(struct hidpp_device *hidpp)
  1352. {
  1353. u8 feature_type;
  1354. int ret;
  1355. u8 state_of_charge;
  1356. int status, level;
  1357. if (hidpp->battery.feature_index == 0xff) {
  1358. ret = hidpp_root_get_feature(hidpp,
  1359. HIDPP_PAGE_UNIFIED_BATTERY,
  1360. &hidpp->battery.feature_index,
  1361. &feature_type);
  1362. if (ret)
  1363. return ret;
  1364. }
  1365. ret = hidpp20_unifiedbattery_get_capabilities(hidpp,
  1366. hidpp->battery.feature_index);
  1367. if (ret)
  1368. return ret;
  1369. ret = hidpp20_unifiedbattery_get_status(hidpp,
  1370. hidpp->battery.feature_index,
  1371. &state_of_charge,
  1372. &status,
  1373. &level);
  1374. if (ret)
  1375. return ret;
  1376. hidpp->capabilities |= HIDPP_CAPABILITY_UNIFIED_BATTERY;
  1377. hidpp->battery.capacity = state_of_charge;
  1378. hidpp->battery.status = status;
  1379. hidpp->battery.level = level;
  1380. hidpp->battery.online = true;
  1381. return 0;
  1382. }
  1383. static int hidpp20_battery_event_1004(struct hidpp_device *hidpp,
  1384. u8 *data, int size)
  1385. {
  1386. struct hidpp_report *report = (struct hidpp_report *)data;
  1387. u8 *params = (u8 *)report->fap.params;
  1388. int state_of_charge, status, level;
  1389. bool changed;
  1390. if (report->fap.feature_index != hidpp->battery.feature_index ||
  1391. report->fap.funcindex_clientid != EVENT_UNIFIED_BATTERY_STATUS_EVENT)
  1392. return 0;
  1393. state_of_charge = params[0];
  1394. status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
  1395. level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
  1396. changed = status != hidpp->battery.status ||
  1397. (state_of_charge != hidpp->battery.capacity &&
  1398. hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) ||
  1399. (level != hidpp->battery.level &&
  1400. hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS);
  1401. if (changed) {
  1402. hidpp->battery.capacity = state_of_charge;
  1403. hidpp->battery.status = status;
  1404. hidpp->battery.level = level;
  1405. if (hidpp->battery.ps)
  1406. power_supply_changed(hidpp->battery.ps);
  1407. }
  1408. return 0;
  1409. }
  1410. /* -------------------------------------------------------------------------- */
  1411. /* Battery feature helpers */
  1412. /* -------------------------------------------------------------------------- */
  1413. static enum power_supply_property hidpp_battery_props[] = {
  1414. POWER_SUPPLY_PROP_ONLINE,
  1415. POWER_SUPPLY_PROP_STATUS,
  1416. POWER_SUPPLY_PROP_SCOPE,
  1417. POWER_SUPPLY_PROP_MODEL_NAME,
  1418. POWER_SUPPLY_PROP_MANUFACTURER,
  1419. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  1420. 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
  1421. 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
  1422. 0, /* placeholder for POWER_SUPPLY_PROP_VOLTAGE_NOW, */
  1423. };
  1424. static int hidpp_battery_get_property(struct power_supply *psy,
  1425. enum power_supply_property psp,
  1426. union power_supply_propval *val)
  1427. {
  1428. struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
  1429. int ret = 0;
  1430. switch(psp) {
  1431. case POWER_SUPPLY_PROP_STATUS:
  1432. val->intval = hidpp->battery.status;
  1433. break;
  1434. case POWER_SUPPLY_PROP_CAPACITY:
  1435. val->intval = hidpp->battery.capacity;
  1436. break;
  1437. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  1438. val->intval = hidpp->battery.level;
  1439. break;
  1440. case POWER_SUPPLY_PROP_SCOPE:
  1441. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  1442. break;
  1443. case POWER_SUPPLY_PROP_ONLINE:
  1444. val->intval = hidpp->battery.online;
  1445. break;
  1446. case POWER_SUPPLY_PROP_MODEL_NAME:
  1447. if (!strncmp(hidpp->name, "Logitech ", 9))
  1448. val->strval = hidpp->name + 9;
  1449. else
  1450. val->strval = hidpp->name;
  1451. break;
  1452. case POWER_SUPPLY_PROP_MANUFACTURER:
  1453. val->strval = "Logitech";
  1454. break;
  1455. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  1456. val->strval = hidpp->hid_dev->uniq;
  1457. break;
  1458. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  1459. /* hardware reports voltage in mV. sysfs expects uV */
  1460. val->intval = hidpp->battery.voltage * 1000;
  1461. break;
  1462. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  1463. val->intval = hidpp->battery.charge_type;
  1464. break;
  1465. default:
  1466. ret = -EINVAL;
  1467. break;
  1468. }
  1469. return ret;
  1470. }
  1471. /* -------------------------------------------------------------------------- */
  1472. /* 0x1d4b: Wireless device status */
  1473. /* -------------------------------------------------------------------------- */
  1474. #define HIDPP_PAGE_WIRELESS_DEVICE_STATUS 0x1d4b
  1475. static int hidpp_get_wireless_feature_index(struct hidpp_device *hidpp, u8 *feature_index)
  1476. {
  1477. u8 feature_type;
  1478. int ret;
  1479. ret = hidpp_root_get_feature(hidpp,
  1480. HIDPP_PAGE_WIRELESS_DEVICE_STATUS,
  1481. feature_index, &feature_type);
  1482. return ret;
  1483. }
  1484. /* -------------------------------------------------------------------------- */
  1485. /* 0x2120: Hi-resolution scrolling */
  1486. /* -------------------------------------------------------------------------- */
  1487. #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
  1488. #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
  1489. static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
  1490. bool enabled, u8 *multiplier)
  1491. {
  1492. u8 feature_index;
  1493. u8 feature_type;
  1494. int ret;
  1495. u8 params[1];
  1496. struct hidpp_report response;
  1497. ret = hidpp_root_get_feature(hidpp,
  1498. HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
  1499. &feature_index,
  1500. &feature_type);
  1501. if (ret)
  1502. return ret;
  1503. params[0] = enabled ? BIT(0) : 0;
  1504. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  1505. CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
  1506. params, sizeof(params), &response);
  1507. if (ret)
  1508. return ret;
  1509. *multiplier = response.fap.params[1];
  1510. return 0;
  1511. }
  1512. /* -------------------------------------------------------------------------- */
  1513. /* 0x2121: HiRes Wheel */
  1514. /* -------------------------------------------------------------------------- */
  1515. #define HIDPP_PAGE_HIRES_WHEEL 0x2121
  1516. #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
  1517. #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
  1518. static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
  1519. u8 *multiplier)
  1520. {
  1521. u8 feature_index;
  1522. u8 feature_type;
  1523. int ret;
  1524. struct hidpp_report response;
  1525. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
  1526. &feature_index, &feature_type);
  1527. if (ret)
  1528. goto return_default;
  1529. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  1530. CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
  1531. NULL, 0, &response);
  1532. if (ret)
  1533. goto return_default;
  1534. *multiplier = response.fap.params[0];
  1535. return 0;
  1536. return_default:
  1537. hid_warn(hidpp->hid_dev,
  1538. "Couldn't get wheel multiplier (error %d)\n", ret);
  1539. return ret;
  1540. }
  1541. static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
  1542. bool high_resolution, bool use_hidpp)
  1543. {
  1544. u8 feature_index;
  1545. u8 feature_type;
  1546. int ret;
  1547. u8 params[1];
  1548. struct hidpp_report response;
  1549. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
  1550. &feature_index, &feature_type);
  1551. if (ret)
  1552. return ret;
  1553. params[0] = (invert ? BIT(2) : 0) |
  1554. (high_resolution ? BIT(1) : 0) |
  1555. (use_hidpp ? BIT(0) : 0);
  1556. return hidpp_send_fap_command_sync(hidpp, feature_index,
  1557. CMD_HIRES_WHEEL_SET_WHEEL_MODE,
  1558. params, sizeof(params), &response);
  1559. }
  1560. /* -------------------------------------------------------------------------- */
  1561. /* 0x4301: Solar Keyboard */
  1562. /* -------------------------------------------------------------------------- */
  1563. #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
  1564. #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
  1565. #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
  1566. #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
  1567. #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
  1568. static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
  1569. {
  1570. struct hidpp_report response;
  1571. u8 params[2] = { 1, 1 };
  1572. u8 feature_type;
  1573. int ret;
  1574. if (hidpp->battery.feature_index == 0xff) {
  1575. ret = hidpp_root_get_feature(hidpp,
  1576. HIDPP_PAGE_SOLAR_KEYBOARD,
  1577. &hidpp->battery.solar_feature_index,
  1578. &feature_type);
  1579. if (ret)
  1580. return ret;
  1581. }
  1582. ret = hidpp_send_fap_command_sync(hidpp,
  1583. hidpp->battery.solar_feature_index,
  1584. CMD_SOLAR_SET_LIGHT_MEASURE,
  1585. params, 2, &response);
  1586. if (ret > 0) {
  1587. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  1588. __func__, ret);
  1589. return -EPROTO;
  1590. }
  1591. if (ret)
  1592. return ret;
  1593. hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
  1594. return 0;
  1595. }
  1596. static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
  1597. u8 *data, int size)
  1598. {
  1599. struct hidpp_report *report = (struct hidpp_report *)data;
  1600. int capacity, lux, status;
  1601. u8 function;
  1602. function = report->fap.funcindex_clientid;
  1603. if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
  1604. !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
  1605. function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
  1606. function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
  1607. return 0;
  1608. capacity = report->fap.params[0];
  1609. switch (function) {
  1610. case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
  1611. lux = (report->fap.params[1] << 8) | report->fap.params[2];
  1612. if (lux > 200)
  1613. status = POWER_SUPPLY_STATUS_CHARGING;
  1614. else
  1615. status = POWER_SUPPLY_STATUS_DISCHARGING;
  1616. break;
  1617. case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
  1618. default:
  1619. if (capacity < hidpp->battery.capacity)
  1620. status = POWER_SUPPLY_STATUS_DISCHARGING;
  1621. else
  1622. status = POWER_SUPPLY_STATUS_CHARGING;
  1623. }
  1624. if (capacity == 100)
  1625. status = POWER_SUPPLY_STATUS_FULL;
  1626. hidpp->battery.online = true;
  1627. if (capacity != hidpp->battery.capacity ||
  1628. status != hidpp->battery.status) {
  1629. hidpp->battery.capacity = capacity;
  1630. hidpp->battery.status = status;
  1631. if (hidpp->battery.ps)
  1632. power_supply_changed(hidpp->battery.ps);
  1633. }
  1634. return 0;
  1635. }
  1636. /* -------------------------------------------------------------------------- */
  1637. /* 0x6010: Touchpad FW items */
  1638. /* -------------------------------------------------------------------------- */
  1639. #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
  1640. #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
  1641. struct hidpp_touchpad_fw_items {
  1642. uint8_t presence;
  1643. uint8_t desired_state;
  1644. uint8_t state;
  1645. uint8_t persistent;
  1646. };
  1647. /*
  1648. * send a set state command to the device by reading the current items->state
  1649. * field. items is then filled with the current state.
  1650. */
  1651. static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
  1652. u8 feature_index,
  1653. struct hidpp_touchpad_fw_items *items)
  1654. {
  1655. struct hidpp_report response;
  1656. int ret;
  1657. u8 *params = (u8 *)response.fap.params;
  1658. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  1659. CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
  1660. if (ret > 0) {
  1661. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  1662. __func__, ret);
  1663. return -EPROTO;
  1664. }
  1665. if (ret)
  1666. return ret;
  1667. items->presence = params[0];
  1668. items->desired_state = params[1];
  1669. items->state = params[2];
  1670. items->persistent = params[3];
  1671. return 0;
  1672. }
  1673. /* -------------------------------------------------------------------------- */
  1674. /* 0x6100: TouchPadRawXY */
  1675. /* -------------------------------------------------------------------------- */
  1676. #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
  1677. #define CMD_TOUCHPAD_GET_RAW_INFO 0x00
  1678. #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x20
  1679. #define EVENT_TOUCHPAD_RAW_XY 0x00
  1680. #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
  1681. #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
  1682. struct hidpp_touchpad_raw_info {
  1683. u16 x_size;
  1684. u16 y_size;
  1685. u8 z_range;
  1686. u8 area_range;
  1687. u8 timestamp_unit;
  1688. u8 maxcontacts;
  1689. u8 origin;
  1690. u16 res;
  1691. };
  1692. struct hidpp_touchpad_raw_xy_finger {
  1693. u8 contact_type;
  1694. u8 contact_status;
  1695. u16 x;
  1696. u16 y;
  1697. u8 z;
  1698. u8 area;
  1699. u8 finger_id;
  1700. };
  1701. struct hidpp_touchpad_raw_xy {
  1702. u16 timestamp;
  1703. struct hidpp_touchpad_raw_xy_finger fingers[2];
  1704. u8 spurious_flag;
  1705. u8 end_of_frame;
  1706. u8 finger_count;
  1707. u8 button;
  1708. };
  1709. static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
  1710. u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
  1711. {
  1712. struct hidpp_report response;
  1713. int ret;
  1714. u8 *params = (u8 *)response.fap.params;
  1715. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  1716. CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
  1717. if (ret > 0) {
  1718. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  1719. __func__, ret);
  1720. return -EPROTO;
  1721. }
  1722. if (ret)
  1723. return ret;
  1724. raw_info->x_size = get_unaligned_be16(&params[0]);
  1725. raw_info->y_size = get_unaligned_be16(&params[2]);
  1726. raw_info->z_range = params[4];
  1727. raw_info->area_range = params[5];
  1728. raw_info->maxcontacts = params[7];
  1729. raw_info->origin = params[8];
  1730. /* res is given in unit per inch */
  1731. raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
  1732. return ret;
  1733. }
  1734. static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
  1735. u8 feature_index, bool send_raw_reports,
  1736. bool sensor_enhanced_settings)
  1737. {
  1738. struct hidpp_report response;
  1739. /*
  1740. * Params:
  1741. * bit 0 - enable raw
  1742. * bit 1 - 16bit Z, no area
  1743. * bit 2 - enhanced sensitivity
  1744. * bit 3 - width, height (4 bits each) instead of area
  1745. * bit 4 - send raw + gestures (degrades smoothness)
  1746. * remaining bits - reserved
  1747. */
  1748. u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
  1749. return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
  1750. CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
  1751. }
  1752. static void hidpp_touchpad_touch_event(u8 *data,
  1753. struct hidpp_touchpad_raw_xy_finger *finger)
  1754. {
  1755. u8 x_m = data[0] << 2;
  1756. u8 y_m = data[2] << 2;
  1757. finger->x = x_m << 6 | data[1];
  1758. finger->y = y_m << 6 | data[3];
  1759. finger->contact_type = data[0] >> 6;
  1760. finger->contact_status = data[2] >> 6;
  1761. finger->z = data[4];
  1762. finger->area = data[5];
  1763. finger->finger_id = data[6] >> 4;
  1764. }
  1765. static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
  1766. u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
  1767. {
  1768. memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
  1769. raw_xy->end_of_frame = data[8] & 0x01;
  1770. raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
  1771. raw_xy->finger_count = data[15] & 0x0f;
  1772. raw_xy->button = (data[8] >> 2) & 0x01;
  1773. if (raw_xy->finger_count) {
  1774. hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
  1775. hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
  1776. }
  1777. }
  1778. /* -------------------------------------------------------------------------- */
  1779. /* 0x8123: Force feedback support */
  1780. /* -------------------------------------------------------------------------- */
  1781. #define HIDPP_FF_GET_INFO 0x01
  1782. #define HIDPP_FF_RESET_ALL 0x11
  1783. #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
  1784. #define HIDPP_FF_SET_EFFECT_STATE 0x31
  1785. #define HIDPP_FF_DESTROY_EFFECT 0x41
  1786. #define HIDPP_FF_GET_APERTURE 0x51
  1787. #define HIDPP_FF_SET_APERTURE 0x61
  1788. #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
  1789. #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
  1790. #define HIDPP_FF_EFFECT_STATE_GET 0x00
  1791. #define HIDPP_FF_EFFECT_STATE_STOP 0x01
  1792. #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
  1793. #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
  1794. #define HIDPP_FF_EFFECT_CONSTANT 0x00
  1795. #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
  1796. #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
  1797. #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
  1798. #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
  1799. #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
  1800. #define HIDPP_FF_EFFECT_SPRING 0x06
  1801. #define HIDPP_FF_EFFECT_DAMPER 0x07
  1802. #define HIDPP_FF_EFFECT_FRICTION 0x08
  1803. #define HIDPP_FF_EFFECT_INERTIA 0x09
  1804. #define HIDPP_FF_EFFECT_RAMP 0x0A
  1805. #define HIDPP_FF_EFFECT_AUTOSTART 0x80
  1806. #define HIDPP_FF_EFFECTID_NONE -1
  1807. #define HIDPP_FF_EFFECTID_AUTOCENTER -2
  1808. #define HIDPP_AUTOCENTER_PARAMS_LENGTH 18
  1809. #define HIDPP_FF_MAX_PARAMS 20
  1810. #define HIDPP_FF_RESERVED_SLOTS 1
  1811. struct hidpp_ff_private_data {
  1812. struct hidpp_device *hidpp;
  1813. u8 feature_index;
  1814. u8 version;
  1815. u16 gain;
  1816. s16 range;
  1817. u8 slot_autocenter;
  1818. u8 num_effects;
  1819. int *effect_ids;
  1820. struct workqueue_struct *wq;
  1821. atomic_t workqueue_size;
  1822. };
  1823. struct hidpp_ff_work_data {
  1824. struct work_struct work;
  1825. struct hidpp_ff_private_data *data;
  1826. int effect_id;
  1827. u8 command;
  1828. u8 params[HIDPP_FF_MAX_PARAMS];
  1829. u8 size;
  1830. };
  1831. static const signed short hidpp_ff_effects[] = {
  1832. FF_CONSTANT,
  1833. FF_PERIODIC,
  1834. FF_SINE,
  1835. FF_SQUARE,
  1836. FF_SAW_UP,
  1837. FF_SAW_DOWN,
  1838. FF_TRIANGLE,
  1839. FF_SPRING,
  1840. FF_DAMPER,
  1841. FF_AUTOCENTER,
  1842. FF_GAIN,
  1843. -1
  1844. };
  1845. static const signed short hidpp_ff_effects_v2[] = {
  1846. FF_RAMP,
  1847. FF_FRICTION,
  1848. FF_INERTIA,
  1849. -1
  1850. };
  1851. static const u8 HIDPP_FF_CONDITION_CMDS[] = {
  1852. HIDPP_FF_EFFECT_SPRING,
  1853. HIDPP_FF_EFFECT_FRICTION,
  1854. HIDPP_FF_EFFECT_DAMPER,
  1855. HIDPP_FF_EFFECT_INERTIA
  1856. };
  1857. static const char *HIDPP_FF_CONDITION_NAMES[] = {
  1858. "spring",
  1859. "friction",
  1860. "damper",
  1861. "inertia"
  1862. };
  1863. static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
  1864. {
  1865. int i;
  1866. for (i = 0; i < data->num_effects; i++)
  1867. if (data->effect_ids[i] == effect_id)
  1868. return i+1;
  1869. return 0;
  1870. }
  1871. static void hidpp_ff_work_handler(struct work_struct *w)
  1872. {
  1873. struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
  1874. struct hidpp_ff_private_data *data = wd->data;
  1875. struct hidpp_report response;
  1876. u8 slot;
  1877. int ret;
  1878. /* add slot number if needed */
  1879. switch (wd->effect_id) {
  1880. case HIDPP_FF_EFFECTID_AUTOCENTER:
  1881. wd->params[0] = data->slot_autocenter;
  1882. break;
  1883. case HIDPP_FF_EFFECTID_NONE:
  1884. /* leave slot as zero */
  1885. break;
  1886. default:
  1887. /* find current slot for effect */
  1888. wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
  1889. break;
  1890. }
  1891. /* send command and wait for reply */
  1892. ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
  1893. wd->command, wd->params, wd->size, &response);
  1894. if (ret) {
  1895. hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
  1896. goto out;
  1897. }
  1898. /* parse return data */
  1899. switch (wd->command) {
  1900. case HIDPP_FF_DOWNLOAD_EFFECT:
  1901. slot = response.fap.params[0];
  1902. if (slot > 0 && slot <= data->num_effects) {
  1903. if (wd->effect_id >= 0)
  1904. /* regular effect uploaded */
  1905. data->effect_ids[slot-1] = wd->effect_id;
  1906. else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
  1907. /* autocenter spring uploaded */
  1908. data->slot_autocenter = slot;
  1909. }
  1910. break;
  1911. case HIDPP_FF_DESTROY_EFFECT:
  1912. if (wd->effect_id >= 0)
  1913. /* regular effect destroyed */
  1914. data->effect_ids[wd->params[0]-1] = -1;
  1915. else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
  1916. /* autocenter spring destoyed */
  1917. data->slot_autocenter = 0;
  1918. break;
  1919. case HIDPP_FF_SET_GLOBAL_GAINS:
  1920. data->gain = (wd->params[0] << 8) + wd->params[1];
  1921. break;
  1922. case HIDPP_FF_SET_APERTURE:
  1923. data->range = (wd->params[0] << 8) + wd->params[1];
  1924. break;
  1925. default:
  1926. /* no action needed */
  1927. break;
  1928. }
  1929. out:
  1930. atomic_dec(&data->workqueue_size);
  1931. kfree(wd);
  1932. }
  1933. static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
  1934. {
  1935. struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
  1936. int s;
  1937. if (!wd)
  1938. return -ENOMEM;
  1939. INIT_WORK(&wd->work, hidpp_ff_work_handler);
  1940. wd->data = data;
  1941. wd->effect_id = effect_id;
  1942. wd->command = command;
  1943. wd->size = size;
  1944. memcpy(wd->params, params, size);
  1945. s = atomic_inc_return(&data->workqueue_size);
  1946. queue_work(data->wq, &wd->work);
  1947. /* warn about excessive queue size */
  1948. if (s >= 20 && s % 20 == 0)
  1949. hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
  1950. return 0;
  1951. }
  1952. static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
  1953. {
  1954. struct hidpp_ff_private_data *data = dev->ff->private;
  1955. u8 params[20];
  1956. u8 size;
  1957. int force;
  1958. /* set common parameters */
  1959. params[2] = effect->replay.length >> 8;
  1960. params[3] = effect->replay.length & 255;
  1961. params[4] = effect->replay.delay >> 8;
  1962. params[5] = effect->replay.delay & 255;
  1963. switch (effect->type) {
  1964. case FF_CONSTANT:
  1965. force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
  1966. params[1] = HIDPP_FF_EFFECT_CONSTANT;
  1967. params[6] = force >> 8;
  1968. params[7] = force & 255;
  1969. params[8] = effect->u.constant.envelope.attack_level >> 7;
  1970. params[9] = effect->u.constant.envelope.attack_length >> 8;
  1971. params[10] = effect->u.constant.envelope.attack_length & 255;
  1972. params[11] = effect->u.constant.envelope.fade_level >> 7;
  1973. params[12] = effect->u.constant.envelope.fade_length >> 8;
  1974. params[13] = effect->u.constant.envelope.fade_length & 255;
  1975. size = 14;
  1976. dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
  1977. effect->u.constant.level,
  1978. effect->direction, force);
  1979. dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
  1980. effect->u.constant.envelope.attack_level,
  1981. effect->u.constant.envelope.attack_length,
  1982. effect->u.constant.envelope.fade_level,
  1983. effect->u.constant.envelope.fade_length);
  1984. break;
  1985. case FF_PERIODIC:
  1986. {
  1987. switch (effect->u.periodic.waveform) {
  1988. case FF_SINE:
  1989. params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
  1990. break;
  1991. case FF_SQUARE:
  1992. params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
  1993. break;
  1994. case FF_SAW_UP:
  1995. params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
  1996. break;
  1997. case FF_SAW_DOWN:
  1998. params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
  1999. break;
  2000. case FF_TRIANGLE:
  2001. params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
  2002. break;
  2003. default:
  2004. hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
  2005. return -EINVAL;
  2006. }
  2007. force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
  2008. params[6] = effect->u.periodic.magnitude >> 8;
  2009. params[7] = effect->u.periodic.magnitude & 255;
  2010. params[8] = effect->u.periodic.offset >> 8;
  2011. params[9] = effect->u.periodic.offset & 255;
  2012. params[10] = effect->u.periodic.period >> 8;
  2013. params[11] = effect->u.periodic.period & 255;
  2014. params[12] = effect->u.periodic.phase >> 8;
  2015. params[13] = effect->u.periodic.phase & 255;
  2016. params[14] = effect->u.periodic.envelope.attack_level >> 7;
  2017. params[15] = effect->u.periodic.envelope.attack_length >> 8;
  2018. params[16] = effect->u.periodic.envelope.attack_length & 255;
  2019. params[17] = effect->u.periodic.envelope.fade_level >> 7;
  2020. params[18] = effect->u.periodic.envelope.fade_length >> 8;
  2021. params[19] = effect->u.periodic.envelope.fade_length & 255;
  2022. size = 20;
  2023. dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
  2024. effect->u.periodic.magnitude, effect->direction,
  2025. effect->u.periodic.offset,
  2026. effect->u.periodic.period,
  2027. effect->u.periodic.phase);
  2028. dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
  2029. effect->u.periodic.envelope.attack_level,
  2030. effect->u.periodic.envelope.attack_length,
  2031. effect->u.periodic.envelope.fade_level,
  2032. effect->u.periodic.envelope.fade_length);
  2033. break;
  2034. }
  2035. case FF_RAMP:
  2036. params[1] = HIDPP_FF_EFFECT_RAMP;
  2037. force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
  2038. params[6] = force >> 8;
  2039. params[7] = force & 255;
  2040. force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
  2041. params[8] = force >> 8;
  2042. params[9] = force & 255;
  2043. params[10] = effect->u.ramp.envelope.attack_level >> 7;
  2044. params[11] = effect->u.ramp.envelope.attack_length >> 8;
  2045. params[12] = effect->u.ramp.envelope.attack_length & 255;
  2046. params[13] = effect->u.ramp.envelope.fade_level >> 7;
  2047. params[14] = effect->u.ramp.envelope.fade_length >> 8;
  2048. params[15] = effect->u.ramp.envelope.fade_length & 255;
  2049. size = 16;
  2050. dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
  2051. effect->u.ramp.start_level,
  2052. effect->u.ramp.end_level,
  2053. effect->direction, force);
  2054. dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
  2055. effect->u.ramp.envelope.attack_level,
  2056. effect->u.ramp.envelope.attack_length,
  2057. effect->u.ramp.envelope.fade_level,
  2058. effect->u.ramp.envelope.fade_length);
  2059. break;
  2060. case FF_FRICTION:
  2061. case FF_INERTIA:
  2062. case FF_SPRING:
  2063. case FF_DAMPER:
  2064. params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
  2065. params[6] = effect->u.condition[0].left_saturation >> 9;
  2066. params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
  2067. params[8] = effect->u.condition[0].left_coeff >> 8;
  2068. params[9] = effect->u.condition[0].left_coeff & 255;
  2069. params[10] = effect->u.condition[0].deadband >> 9;
  2070. params[11] = (effect->u.condition[0].deadband >> 1) & 255;
  2071. params[12] = effect->u.condition[0].center >> 8;
  2072. params[13] = effect->u.condition[0].center & 255;
  2073. params[14] = effect->u.condition[0].right_coeff >> 8;
  2074. params[15] = effect->u.condition[0].right_coeff & 255;
  2075. params[16] = effect->u.condition[0].right_saturation >> 9;
  2076. params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
  2077. size = 18;
  2078. dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
  2079. HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
  2080. effect->u.condition[0].left_coeff,
  2081. effect->u.condition[0].left_saturation,
  2082. effect->u.condition[0].right_coeff,
  2083. effect->u.condition[0].right_saturation);
  2084. dbg_hid(" deadband=%d, center=%d\n",
  2085. effect->u.condition[0].deadband,
  2086. effect->u.condition[0].center);
  2087. break;
  2088. default:
  2089. hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
  2090. return -EINVAL;
  2091. }
  2092. return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
  2093. }
  2094. static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
  2095. {
  2096. struct hidpp_ff_private_data *data = dev->ff->private;
  2097. u8 params[2];
  2098. params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
  2099. dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
  2100. return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
  2101. }
  2102. static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
  2103. {
  2104. struct hidpp_ff_private_data *data = dev->ff->private;
  2105. u8 slot = 0;
  2106. dbg_hid("Erasing effect %d.\n", effect_id);
  2107. return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
  2108. }
  2109. static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
  2110. {
  2111. struct hidpp_ff_private_data *data = dev->ff->private;
  2112. u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH];
  2113. dbg_hid("Setting autocenter to %d.\n", magnitude);
  2114. /* start a standard spring effect */
  2115. params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
  2116. /* zero delay and duration */
  2117. params[2] = params[3] = params[4] = params[5] = 0;
  2118. /* set coeff to 25% of saturation */
  2119. params[8] = params[14] = magnitude >> 11;
  2120. params[9] = params[15] = (magnitude >> 3) & 255;
  2121. params[6] = params[16] = magnitude >> 9;
  2122. params[7] = params[17] = (magnitude >> 1) & 255;
  2123. /* zero deadband and center */
  2124. params[10] = params[11] = params[12] = params[13] = 0;
  2125. hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
  2126. }
  2127. static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
  2128. {
  2129. struct hidpp_ff_private_data *data = dev->ff->private;
  2130. u8 params[4];
  2131. dbg_hid("Setting gain to %d.\n", gain);
  2132. params[0] = gain >> 8;
  2133. params[1] = gain & 255;
  2134. params[2] = 0; /* no boost */
  2135. params[3] = 0;
  2136. hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
  2137. }
  2138. static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
  2139. {
  2140. struct hid_device *hid = to_hid_device(dev);
  2141. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  2142. struct input_dev *idev = hidinput->input;
  2143. struct hidpp_ff_private_data *data = idev->ff->private;
  2144. return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
  2145. }
  2146. static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  2147. {
  2148. struct hid_device *hid = to_hid_device(dev);
  2149. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  2150. struct input_dev *idev = hidinput->input;
  2151. struct hidpp_ff_private_data *data = idev->ff->private;
  2152. u8 params[2];
  2153. int range = simple_strtoul(buf, NULL, 10);
  2154. range = clamp(range, 180, 900);
  2155. params[0] = range >> 8;
  2156. params[1] = range & 0x00FF;
  2157. hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
  2158. return count;
  2159. }
  2160. static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
  2161. static void hidpp_ff_destroy(struct ff_device *ff)
  2162. {
  2163. struct hidpp_ff_private_data *data = ff->private;
  2164. struct hid_device *hid = data->hidpp->hid_dev;
  2165. hid_info(hid, "Unloading HID++ force feedback.\n");
  2166. device_remove_file(&hid->dev, &dev_attr_range);
  2167. destroy_workqueue(data->wq);
  2168. kfree(data->effect_ids);
  2169. }
  2170. static int hidpp_ff_init(struct hidpp_device *hidpp,
  2171. struct hidpp_ff_private_data *data)
  2172. {
  2173. struct hid_device *hid = hidpp->hid_dev;
  2174. struct hid_input *hidinput;
  2175. struct input_dev *dev;
  2176. struct usb_device_descriptor *udesc;
  2177. u16 bcdDevice;
  2178. struct ff_device *ff;
  2179. int error, j, num_slots = data->num_effects;
  2180. u8 version;
  2181. if (!hid_is_usb(hid)) {
  2182. hid_err(hid, "device is not USB\n");
  2183. return -ENODEV;
  2184. }
  2185. if (list_empty(&hid->inputs)) {
  2186. hid_err(hid, "no inputs found\n");
  2187. return -ENODEV;
  2188. }
  2189. hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  2190. dev = hidinput->input;
  2191. if (!dev) {
  2192. hid_err(hid, "Struct input_dev not set!\n");
  2193. return -EINVAL;
  2194. }
  2195. /* Get firmware release */
  2196. udesc = &(hid_to_usb_dev(hid)->descriptor);
  2197. bcdDevice = le16_to_cpu(udesc->bcdDevice);
  2198. version = bcdDevice & 255;
  2199. /* Set supported force feedback capabilities */
  2200. for (j = 0; hidpp_ff_effects[j] >= 0; j++)
  2201. set_bit(hidpp_ff_effects[j], dev->ffbit);
  2202. if (version > 1)
  2203. for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
  2204. set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
  2205. error = input_ff_create(dev, num_slots);
  2206. if (error) {
  2207. hid_err(dev, "Failed to create FF device!\n");
  2208. return error;
  2209. }
  2210. /*
  2211. * Create a copy of passed data, so we can transfer memory
  2212. * ownership to FF core
  2213. */
  2214. data = kmemdup(data, sizeof(*data), GFP_KERNEL);
  2215. if (!data)
  2216. return -ENOMEM;
  2217. data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
  2218. if (!data->effect_ids) {
  2219. kfree(data);
  2220. return -ENOMEM;
  2221. }
  2222. data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
  2223. if (!data->wq) {
  2224. kfree(data->effect_ids);
  2225. kfree(data);
  2226. return -ENOMEM;
  2227. }
  2228. data->hidpp = hidpp;
  2229. data->version = version;
  2230. for (j = 0; j < num_slots; j++)
  2231. data->effect_ids[j] = -1;
  2232. ff = dev->ff;
  2233. ff->private = data;
  2234. ff->upload = hidpp_ff_upload_effect;
  2235. ff->erase = hidpp_ff_erase_effect;
  2236. ff->playback = hidpp_ff_playback;
  2237. ff->set_gain = hidpp_ff_set_gain;
  2238. ff->set_autocenter = hidpp_ff_set_autocenter;
  2239. ff->destroy = hidpp_ff_destroy;
  2240. /* Create sysfs interface */
  2241. error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
  2242. if (error)
  2243. hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
  2244. /* init the hardware command queue */
  2245. atomic_set(&data->workqueue_size, 0);
  2246. hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
  2247. version);
  2248. return 0;
  2249. }
  2250. /* ************************************************************************** */
  2251. /* */
  2252. /* Device Support */
  2253. /* */
  2254. /* ************************************************************************** */
  2255. /* -------------------------------------------------------------------------- */
  2256. /* Touchpad HID++ devices */
  2257. /* -------------------------------------------------------------------------- */
  2258. #define WTP_MANUAL_RESOLUTION 39
  2259. struct wtp_data {
  2260. u16 x_size, y_size;
  2261. u8 finger_count;
  2262. u8 mt_feature_index;
  2263. u8 button_feature_index;
  2264. u8 maxcontacts;
  2265. bool flip_y;
  2266. unsigned int resolution;
  2267. };
  2268. static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  2269. struct hid_field *field, struct hid_usage *usage,
  2270. unsigned long **bit, int *max)
  2271. {
  2272. return -1;
  2273. }
  2274. static void wtp_populate_input(struct hidpp_device *hidpp,
  2275. struct input_dev *input_dev)
  2276. {
  2277. struct wtp_data *wd = hidpp->private_data;
  2278. __set_bit(EV_ABS, input_dev->evbit);
  2279. __set_bit(EV_KEY, input_dev->evbit);
  2280. __clear_bit(EV_REL, input_dev->evbit);
  2281. __clear_bit(EV_LED, input_dev->evbit);
  2282. input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
  2283. input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
  2284. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
  2285. input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
  2286. /* Max pressure is not given by the devices, pick one */
  2287. input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
  2288. input_set_capability(input_dev, EV_KEY, BTN_LEFT);
  2289. if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
  2290. input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
  2291. else
  2292. __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
  2293. input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
  2294. INPUT_MT_DROP_UNUSED);
  2295. }
  2296. static void wtp_touch_event(struct hidpp_device *hidpp,
  2297. struct hidpp_touchpad_raw_xy_finger *touch_report)
  2298. {
  2299. struct wtp_data *wd = hidpp->private_data;
  2300. int slot;
  2301. if (!touch_report->finger_id || touch_report->contact_type)
  2302. /* no actual data */
  2303. return;
  2304. slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
  2305. input_mt_slot(hidpp->input, slot);
  2306. input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
  2307. touch_report->contact_status);
  2308. if (touch_report->contact_status) {
  2309. input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
  2310. touch_report->x);
  2311. input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
  2312. wd->flip_y ? wd->y_size - touch_report->y :
  2313. touch_report->y);
  2314. input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
  2315. touch_report->area);
  2316. }
  2317. }
  2318. static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
  2319. struct hidpp_touchpad_raw_xy *raw)
  2320. {
  2321. int i;
  2322. for (i = 0; i < 2; i++)
  2323. wtp_touch_event(hidpp, &(raw->fingers[i]));
  2324. if (raw->end_of_frame &&
  2325. !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
  2326. input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
  2327. if (raw->end_of_frame || raw->finger_count <= 2) {
  2328. input_mt_sync_frame(hidpp->input);
  2329. input_sync(hidpp->input);
  2330. }
  2331. }
  2332. static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
  2333. {
  2334. struct wtp_data *wd = hidpp->private_data;
  2335. u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
  2336. (data[7] >> 4) * (data[7] >> 4)) / 2;
  2337. u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
  2338. (data[13] >> 4) * (data[13] >> 4)) / 2;
  2339. struct hidpp_touchpad_raw_xy raw = {
  2340. .timestamp = data[1],
  2341. .fingers = {
  2342. {
  2343. .contact_type = 0,
  2344. .contact_status = !!data[7],
  2345. .x = get_unaligned_le16(&data[3]),
  2346. .y = get_unaligned_le16(&data[5]),
  2347. .z = c1_area,
  2348. .area = c1_area,
  2349. .finger_id = data[2],
  2350. }, {
  2351. .contact_type = 0,
  2352. .contact_status = !!data[13],
  2353. .x = get_unaligned_le16(&data[9]),
  2354. .y = get_unaligned_le16(&data[11]),
  2355. .z = c2_area,
  2356. .area = c2_area,
  2357. .finger_id = data[8],
  2358. }
  2359. },
  2360. .finger_count = wd->maxcontacts,
  2361. .spurious_flag = 0,
  2362. .end_of_frame = (data[0] >> 7) == 0,
  2363. .button = data[0] & 0x01,
  2364. };
  2365. wtp_send_raw_xy_event(hidpp, &raw);
  2366. return 1;
  2367. }
  2368. static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
  2369. {
  2370. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2371. struct wtp_data *wd = hidpp->private_data;
  2372. struct hidpp_report *report = (struct hidpp_report *)data;
  2373. struct hidpp_touchpad_raw_xy raw;
  2374. if (!wd || !hidpp->input)
  2375. return 1;
  2376. switch (data[0]) {
  2377. case 0x02:
  2378. if (size < 2) {
  2379. hid_err(hdev, "Received HID report of bad size (%d)",
  2380. size);
  2381. return 1;
  2382. }
  2383. if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
  2384. input_event(hidpp->input, EV_KEY, BTN_LEFT,
  2385. !!(data[1] & 0x01));
  2386. input_event(hidpp->input, EV_KEY, BTN_RIGHT,
  2387. !!(data[1] & 0x02));
  2388. input_sync(hidpp->input);
  2389. return 0;
  2390. } else {
  2391. if (size < 21)
  2392. return 1;
  2393. return wtp_mouse_raw_xy_event(hidpp, &data[7]);
  2394. }
  2395. case REPORT_ID_HIDPP_LONG:
  2396. /* size is already checked in hidpp_raw_event. */
  2397. if ((report->fap.feature_index != wd->mt_feature_index) ||
  2398. (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
  2399. return 1;
  2400. hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
  2401. wtp_send_raw_xy_event(hidpp, &raw);
  2402. return 0;
  2403. }
  2404. return 0;
  2405. }
  2406. static int wtp_get_config(struct hidpp_device *hidpp)
  2407. {
  2408. struct wtp_data *wd = hidpp->private_data;
  2409. struct hidpp_touchpad_raw_info raw_info = {0};
  2410. u8 feature_type;
  2411. int ret;
  2412. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
  2413. &wd->mt_feature_index, &feature_type);
  2414. if (ret)
  2415. /* means that the device is not powered up */
  2416. return ret;
  2417. ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
  2418. &raw_info);
  2419. if (ret)
  2420. return ret;
  2421. wd->x_size = raw_info.x_size;
  2422. wd->y_size = raw_info.y_size;
  2423. wd->maxcontacts = raw_info.maxcontacts;
  2424. wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
  2425. wd->resolution = raw_info.res;
  2426. if (!wd->resolution)
  2427. wd->resolution = WTP_MANUAL_RESOLUTION;
  2428. return 0;
  2429. }
  2430. static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
  2431. {
  2432. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2433. struct wtp_data *wd;
  2434. wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
  2435. GFP_KERNEL);
  2436. if (!wd)
  2437. return -ENOMEM;
  2438. hidpp->private_data = wd;
  2439. return 0;
  2440. };
  2441. static int wtp_connect(struct hid_device *hdev, bool connected)
  2442. {
  2443. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2444. struct wtp_data *wd = hidpp->private_data;
  2445. int ret;
  2446. if (!wd->x_size) {
  2447. ret = wtp_get_config(hidpp);
  2448. if (ret) {
  2449. hid_err(hdev, "Can not get wtp config: %d\n", ret);
  2450. return ret;
  2451. }
  2452. }
  2453. return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
  2454. true, true);
  2455. }
  2456. /* ------------------------------------------------------------------------- */
  2457. /* Logitech M560 devices */
  2458. /* ------------------------------------------------------------------------- */
  2459. /*
  2460. * Logitech M560 protocol overview
  2461. *
  2462. * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
  2463. * the sides buttons are pressed, it sends some keyboard keys events
  2464. * instead of buttons ones.
  2465. * To complicate things further, the middle button keys sequence
  2466. * is different from the odd press and the even press.
  2467. *
  2468. * forward button -> Super_R
  2469. * backward button -> Super_L+'d' (press only)
  2470. * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
  2471. * 2nd time: left-click (press only)
  2472. * NB: press-only means that when the button is pressed, the
  2473. * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
  2474. * together sequentially; instead when the button is released, no event is
  2475. * generated !
  2476. *
  2477. * With the command
  2478. * 10<xx>0a 3500af03 (where <xx> is the mouse id),
  2479. * the mouse reacts differently:
  2480. * - it never sends a keyboard key event
  2481. * - for the three mouse button it sends:
  2482. * middle button press 11<xx>0a 3500af00...
  2483. * side 1 button (forward) press 11<xx>0a 3500b000...
  2484. * side 2 button (backward) press 11<xx>0a 3500ae00...
  2485. * middle/side1/side2 button release 11<xx>0a 35000000...
  2486. */
  2487. static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
  2488. /* how buttons are mapped in the report */
  2489. #define M560_MOUSE_BTN_LEFT 0x01
  2490. #define M560_MOUSE_BTN_RIGHT 0x02
  2491. #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
  2492. #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
  2493. #define M560_SUB_ID 0x0a
  2494. #define M560_BUTTON_MODE_REGISTER 0x35
  2495. static int m560_send_config_command(struct hid_device *hdev, bool connected)
  2496. {
  2497. struct hidpp_report response;
  2498. struct hidpp_device *hidpp_dev;
  2499. hidpp_dev = hid_get_drvdata(hdev);
  2500. return hidpp_send_rap_command_sync(
  2501. hidpp_dev,
  2502. REPORT_ID_HIDPP_SHORT,
  2503. M560_SUB_ID,
  2504. M560_BUTTON_MODE_REGISTER,
  2505. (u8 *)m560_config_parameter,
  2506. sizeof(m560_config_parameter),
  2507. &response
  2508. );
  2509. }
  2510. static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
  2511. {
  2512. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2513. /* sanity check */
  2514. if (!hidpp->input) {
  2515. hid_err(hdev, "error in parameter\n");
  2516. return -EINVAL;
  2517. }
  2518. if (size < 7) {
  2519. hid_err(hdev, "error in report\n");
  2520. return 0;
  2521. }
  2522. if (data[0] == REPORT_ID_HIDPP_LONG &&
  2523. data[2] == M560_SUB_ID && data[6] == 0x00) {
  2524. /*
  2525. * m560 mouse report for middle, forward and backward button
  2526. *
  2527. * data[0] = 0x11
  2528. * data[1] = device-id
  2529. * data[2] = 0x0a
  2530. * data[5] = 0xaf -> middle
  2531. * 0xb0 -> forward
  2532. * 0xae -> backward
  2533. * 0x00 -> release all
  2534. * data[6] = 0x00
  2535. */
  2536. switch (data[5]) {
  2537. case 0xaf:
  2538. input_report_key(hidpp->input, BTN_MIDDLE, 1);
  2539. break;
  2540. case 0xb0:
  2541. input_report_key(hidpp->input, BTN_FORWARD, 1);
  2542. break;
  2543. case 0xae:
  2544. input_report_key(hidpp->input, BTN_BACK, 1);
  2545. break;
  2546. case 0x00:
  2547. input_report_key(hidpp->input, BTN_BACK, 0);
  2548. input_report_key(hidpp->input, BTN_FORWARD, 0);
  2549. input_report_key(hidpp->input, BTN_MIDDLE, 0);
  2550. break;
  2551. default:
  2552. hid_err(hdev, "error in report\n");
  2553. return 0;
  2554. }
  2555. input_sync(hidpp->input);
  2556. } else if (data[0] == 0x02) {
  2557. /*
  2558. * Logitech M560 mouse report
  2559. *
  2560. * data[0] = type (0x02)
  2561. * data[1..2] = buttons
  2562. * data[3..5] = xy
  2563. * data[6] = wheel
  2564. */
  2565. int v;
  2566. input_report_key(hidpp->input, BTN_LEFT,
  2567. !!(data[1] & M560_MOUSE_BTN_LEFT));
  2568. input_report_key(hidpp->input, BTN_RIGHT,
  2569. !!(data[1] & M560_MOUSE_BTN_RIGHT));
  2570. if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
  2571. input_report_rel(hidpp->input, REL_HWHEEL, -1);
  2572. input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
  2573. -120);
  2574. } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
  2575. input_report_rel(hidpp->input, REL_HWHEEL, 1);
  2576. input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
  2577. 120);
  2578. }
  2579. v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
  2580. input_report_rel(hidpp->input, REL_X, v);
  2581. v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
  2582. input_report_rel(hidpp->input, REL_Y, v);
  2583. v = hid_snto32(data[6], 8);
  2584. if (v != 0)
  2585. hidpp_scroll_counter_handle_scroll(hidpp->input,
  2586. &hidpp->vertical_wheel_counter, v);
  2587. input_sync(hidpp->input);
  2588. }
  2589. return 1;
  2590. }
  2591. static void m560_populate_input(struct hidpp_device *hidpp,
  2592. struct input_dev *input_dev)
  2593. {
  2594. __set_bit(EV_KEY, input_dev->evbit);
  2595. __set_bit(BTN_MIDDLE, input_dev->keybit);
  2596. __set_bit(BTN_RIGHT, input_dev->keybit);
  2597. __set_bit(BTN_LEFT, input_dev->keybit);
  2598. __set_bit(BTN_BACK, input_dev->keybit);
  2599. __set_bit(BTN_FORWARD, input_dev->keybit);
  2600. __set_bit(EV_REL, input_dev->evbit);
  2601. __set_bit(REL_X, input_dev->relbit);
  2602. __set_bit(REL_Y, input_dev->relbit);
  2603. __set_bit(REL_WHEEL, input_dev->relbit);
  2604. __set_bit(REL_HWHEEL, input_dev->relbit);
  2605. __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
  2606. __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
  2607. }
  2608. static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  2609. struct hid_field *field, struct hid_usage *usage,
  2610. unsigned long **bit, int *max)
  2611. {
  2612. return -1;
  2613. }
  2614. /* ------------------------------------------------------------------------- */
  2615. /* Logitech K400 devices */
  2616. /* ------------------------------------------------------------------------- */
  2617. /*
  2618. * The Logitech K400 keyboard has an embedded touchpad which is seen
  2619. * as a mouse from the OS point of view. There is a hardware shortcut to disable
  2620. * tap-to-click but the setting is not remembered accross reset, annoying some
  2621. * users.
  2622. *
  2623. * We can toggle this feature from the host by using the feature 0x6010:
  2624. * Touchpad FW items
  2625. */
  2626. struct k400_private_data {
  2627. u8 feature_index;
  2628. };
  2629. static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
  2630. {
  2631. struct k400_private_data *k400 = hidpp->private_data;
  2632. struct hidpp_touchpad_fw_items items = {};
  2633. int ret;
  2634. u8 feature_type;
  2635. if (!k400->feature_index) {
  2636. ret = hidpp_root_get_feature(hidpp,
  2637. HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
  2638. &k400->feature_index, &feature_type);
  2639. if (ret)
  2640. /* means that the device is not powered up */
  2641. return ret;
  2642. }
  2643. ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
  2644. if (ret)
  2645. return ret;
  2646. return 0;
  2647. }
  2648. static int k400_allocate(struct hid_device *hdev)
  2649. {
  2650. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2651. struct k400_private_data *k400;
  2652. k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
  2653. GFP_KERNEL);
  2654. if (!k400)
  2655. return -ENOMEM;
  2656. hidpp->private_data = k400;
  2657. return 0;
  2658. };
  2659. static int k400_connect(struct hid_device *hdev, bool connected)
  2660. {
  2661. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2662. if (!disable_tap_to_click)
  2663. return 0;
  2664. return k400_disable_tap_to_click(hidpp);
  2665. }
  2666. /* ------------------------------------------------------------------------- */
  2667. /* Logitech G920 Driving Force Racing Wheel for Xbox One */
  2668. /* ------------------------------------------------------------------------- */
  2669. #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
  2670. static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
  2671. struct hidpp_ff_private_data *data)
  2672. {
  2673. struct hidpp_report response;
  2674. u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
  2675. [1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
  2676. };
  2677. int ret;
  2678. /* initialize with zero autocenter to get wheel in usable state */
  2679. dbg_hid("Setting autocenter to 0.\n");
  2680. ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
  2681. HIDPP_FF_DOWNLOAD_EFFECT,
  2682. params, ARRAY_SIZE(params),
  2683. &response);
  2684. if (ret)
  2685. hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
  2686. else
  2687. data->slot_autocenter = response.fap.params[0];
  2688. return ret;
  2689. }
  2690. static int g920_get_config(struct hidpp_device *hidpp,
  2691. struct hidpp_ff_private_data *data)
  2692. {
  2693. struct hidpp_report response;
  2694. u8 feature_type;
  2695. int ret;
  2696. memset(data, 0, sizeof(*data));
  2697. /* Find feature and store for later use */
  2698. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
  2699. &data->feature_index, &feature_type);
  2700. if (ret)
  2701. return ret;
  2702. /* Read number of slots available in device */
  2703. ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
  2704. HIDPP_FF_GET_INFO,
  2705. NULL, 0,
  2706. &response);
  2707. if (ret) {
  2708. if (ret < 0)
  2709. return ret;
  2710. hid_err(hidpp->hid_dev,
  2711. "%s: received protocol error 0x%02x\n", __func__, ret);
  2712. return -EPROTO;
  2713. }
  2714. data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
  2715. /* reset all forces */
  2716. ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
  2717. HIDPP_FF_RESET_ALL,
  2718. NULL, 0,
  2719. &response);
  2720. if (ret)
  2721. hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
  2722. ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
  2723. HIDPP_FF_GET_APERTURE,
  2724. NULL, 0,
  2725. &response);
  2726. if (ret) {
  2727. hid_warn(hidpp->hid_dev,
  2728. "Failed to read range from device!\n");
  2729. }
  2730. data->range = ret ?
  2731. 900 : get_unaligned_be16(&response.fap.params[0]);
  2732. /* Read the current gain values */
  2733. ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
  2734. HIDPP_FF_GET_GLOBAL_GAINS,
  2735. NULL, 0,
  2736. &response);
  2737. if (ret)
  2738. hid_warn(hidpp->hid_dev,
  2739. "Failed to read gain values from device!\n");
  2740. data->gain = ret ?
  2741. 0xffff : get_unaligned_be16(&response.fap.params[0]);
  2742. /* ignore boost value at response.fap.params[2] */
  2743. return g920_ff_set_autocenter(hidpp, data);
  2744. }
  2745. /* -------------------------------------------------------------------------- */
  2746. /* Logitech Dinovo Mini keyboard with builtin touchpad */
  2747. /* -------------------------------------------------------------------------- */
  2748. #define DINOVO_MINI_PRODUCT_ID 0xb30c
  2749. static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  2750. struct hid_field *field, struct hid_usage *usage,
  2751. unsigned long **bit, int *max)
  2752. {
  2753. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
  2754. return 0;
  2755. switch (usage->hid & HID_USAGE) {
  2756. case 0x00d: lg_map_key_clear(KEY_MEDIA); break;
  2757. default:
  2758. return 0;
  2759. }
  2760. return 1;
  2761. }
  2762. /* -------------------------------------------------------------------------- */
  2763. /* HID++1.0 devices which use HID++ reports for their wheels */
  2764. /* -------------------------------------------------------------------------- */
  2765. static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
  2766. {
  2767. return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
  2768. HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
  2769. HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
  2770. }
  2771. static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
  2772. u8 *data, int size)
  2773. {
  2774. s8 value, hvalue;
  2775. if (!hidpp->input)
  2776. return -EINVAL;
  2777. if (size < 7)
  2778. return 0;
  2779. if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
  2780. return 0;
  2781. value = data[3];
  2782. hvalue = data[4];
  2783. input_report_rel(hidpp->input, REL_WHEEL, value);
  2784. input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
  2785. input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
  2786. input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
  2787. input_sync(hidpp->input);
  2788. return 1;
  2789. }
  2790. static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
  2791. struct input_dev *input_dev)
  2792. {
  2793. __set_bit(EV_REL, input_dev->evbit);
  2794. __set_bit(REL_WHEEL, input_dev->relbit);
  2795. __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
  2796. __set_bit(REL_HWHEEL, input_dev->relbit);
  2797. __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
  2798. }
  2799. /* -------------------------------------------------------------------------- */
  2800. /* HID++1.0 mice which use HID++ reports for extra mouse buttons */
  2801. /* -------------------------------------------------------------------------- */
  2802. static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
  2803. {
  2804. return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
  2805. HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
  2806. HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
  2807. }
  2808. static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
  2809. u8 *data, int size)
  2810. {
  2811. int i;
  2812. if (!hidpp->input)
  2813. return -EINVAL;
  2814. if (size < 7)
  2815. return 0;
  2816. if (data[0] != REPORT_ID_HIDPP_SHORT ||
  2817. data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
  2818. return 0;
  2819. /*
  2820. * Buttons are either delivered through the regular mouse report *or*
  2821. * through the extra buttons report. At least for button 6 how it is
  2822. * delivered differs per receiver firmware version. Even receivers with
  2823. * the same usb-id show different behavior, so we handle both cases.
  2824. */
  2825. for (i = 0; i < 8; i++)
  2826. input_report_key(hidpp->input, BTN_MOUSE + i,
  2827. (data[3] & (1 << i)));
  2828. /* Some mice report events on button 9+, use BTN_MISC */
  2829. for (i = 0; i < 8; i++)
  2830. input_report_key(hidpp->input, BTN_MISC + i,
  2831. (data[4] & (1 << i)));
  2832. input_sync(hidpp->input);
  2833. return 1;
  2834. }
  2835. static void hidpp10_extra_mouse_buttons_populate_input(
  2836. struct hidpp_device *hidpp, struct input_dev *input_dev)
  2837. {
  2838. /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
  2839. __set_bit(BTN_0, input_dev->keybit);
  2840. __set_bit(BTN_1, input_dev->keybit);
  2841. __set_bit(BTN_2, input_dev->keybit);
  2842. __set_bit(BTN_3, input_dev->keybit);
  2843. __set_bit(BTN_4, input_dev->keybit);
  2844. __set_bit(BTN_5, input_dev->keybit);
  2845. __set_bit(BTN_6, input_dev->keybit);
  2846. __set_bit(BTN_7, input_dev->keybit);
  2847. }
  2848. /* -------------------------------------------------------------------------- */
  2849. /* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
  2850. /* -------------------------------------------------------------------------- */
  2851. /* Find the consumer-page input report desc and change Maximums to 0x107f */
  2852. static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
  2853. u8 *_rdesc, unsigned int *rsize)
  2854. {
  2855. /* Note 0 terminated so we can use strnstr to search for this. */
  2856. static const char consumer_rdesc_start[] = {
  2857. 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
  2858. 0x09, 0x01, /* USAGE (Consumer Control) */
  2859. 0xA1, 0x01, /* COLLECTION (Application) */
  2860. 0x85, 0x03, /* REPORT_ID = 3 */
  2861. 0x75, 0x10, /* REPORT_SIZE (16) */
  2862. 0x95, 0x02, /* REPORT_COUNT (2) */
  2863. 0x15, 0x01, /* LOGICAL_MIN (1) */
  2864. 0x26, 0x00 /* LOGICAL_MAX (... */
  2865. };
  2866. char *consumer_rdesc, *rdesc = (char *)_rdesc;
  2867. unsigned int size;
  2868. consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
  2869. size = *rsize - (consumer_rdesc - rdesc);
  2870. if (consumer_rdesc && size >= 25) {
  2871. consumer_rdesc[15] = 0x7f;
  2872. consumer_rdesc[16] = 0x10;
  2873. consumer_rdesc[20] = 0x7f;
  2874. consumer_rdesc[21] = 0x10;
  2875. }
  2876. return _rdesc;
  2877. }
  2878. static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
  2879. {
  2880. return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
  2881. HIDPP_ENABLE_CONSUMER_REPORT,
  2882. HIDPP_ENABLE_CONSUMER_REPORT);
  2883. }
  2884. static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
  2885. u8 *data, int size)
  2886. {
  2887. u8 consumer_report[5];
  2888. if (size < 7)
  2889. return 0;
  2890. if (data[0] != REPORT_ID_HIDPP_SHORT ||
  2891. data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
  2892. return 0;
  2893. /*
  2894. * Build a normal consumer report (3) out of the data, this detour
  2895. * is necessary to get some keyboards to report their 0x10xx usages.
  2896. */
  2897. consumer_report[0] = 0x03;
  2898. memcpy(&consumer_report[1], &data[3], 4);
  2899. /* We are called from atomic context */
  2900. hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
  2901. consumer_report, 5, 1);
  2902. return 1;
  2903. }
  2904. /* -------------------------------------------------------------------------- */
  2905. /* High-resolution scroll wheels */
  2906. /* -------------------------------------------------------------------------- */
  2907. static int hi_res_scroll_enable(struct hidpp_device *hidpp)
  2908. {
  2909. int ret;
  2910. u8 multiplier = 1;
  2911. if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL) {
  2912. ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
  2913. if (ret == 0)
  2914. ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
  2915. } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL) {
  2916. ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
  2917. &multiplier);
  2918. } else /* if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL) */ {
  2919. ret = hidpp10_enable_scrolling_acceleration(hidpp);
  2920. multiplier = 8;
  2921. }
  2922. if (ret)
  2923. return ret;
  2924. if (multiplier == 0)
  2925. multiplier = 1;
  2926. hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
  2927. hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier);
  2928. return 0;
  2929. }
  2930. static int hidpp_initialize_hires_scroll(struct hidpp_device *hidpp)
  2931. {
  2932. int ret;
  2933. unsigned long capabilities;
  2934. capabilities = hidpp->capabilities;
  2935. if (hidpp->protocol_major >= 2) {
  2936. u8 feature_index;
  2937. u8 feature_type;
  2938. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
  2939. &feature_index, &feature_type);
  2940. if (!ret) {
  2941. hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL;
  2942. hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scroll wheel\n");
  2943. return 0;
  2944. }
  2945. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
  2946. &feature_index, &feature_type);
  2947. if (!ret) {
  2948. hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL;
  2949. hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scrolling\n");
  2950. }
  2951. } else {
  2952. /* We cannot detect fast scrolling support on HID++ 1.0 devices */
  2953. if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) {
  2954. hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL;
  2955. hid_dbg(hidpp->hid_dev, "Detected HID++ 1.0 fast scroll\n");
  2956. }
  2957. }
  2958. if (hidpp->capabilities == capabilities)
  2959. hid_dbg(hidpp->hid_dev, "Did not detect HID++ hi-res scrolling hardware support\n");
  2960. return 0;
  2961. }
  2962. /* -------------------------------------------------------------------------- */
  2963. /* Generic HID++ devices */
  2964. /* -------------------------------------------------------------------------- */
  2965. static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
  2966. unsigned int *rsize)
  2967. {
  2968. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2969. if (!hidpp)
  2970. return rdesc;
  2971. /* For 27 MHz keyboards the quirk gets set after hid_parse. */
  2972. if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
  2973. (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
  2974. rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
  2975. return rdesc;
  2976. }
  2977. static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  2978. struct hid_field *field, struct hid_usage *usage,
  2979. unsigned long **bit, int *max)
  2980. {
  2981. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2982. if (!hidpp)
  2983. return 0;
  2984. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  2985. return wtp_input_mapping(hdev, hi, field, usage, bit, max);
  2986. else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
  2987. field->application != HID_GD_MOUSE)
  2988. return m560_input_mapping(hdev, hi, field, usage, bit, max);
  2989. if (hdev->product == DINOVO_MINI_PRODUCT_ID)
  2990. return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
  2991. return 0;
  2992. }
  2993. static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  2994. struct hid_field *field, struct hid_usage *usage,
  2995. unsigned long **bit, int *max)
  2996. {
  2997. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2998. if (!hidpp)
  2999. return 0;
  3000. /* Ensure that Logitech G920 is not given a default fuzz/flat value */
  3001. if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
  3002. if (usage->type == EV_ABS && (usage->code == ABS_X ||
  3003. usage->code == ABS_Y || usage->code == ABS_Z ||
  3004. usage->code == ABS_RZ)) {
  3005. field->application = HID_GD_MULTIAXIS;
  3006. }
  3007. }
  3008. return 0;
  3009. }
  3010. static void hidpp_populate_input(struct hidpp_device *hidpp,
  3011. struct input_dev *input)
  3012. {
  3013. hidpp->input = input;
  3014. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  3015. wtp_populate_input(hidpp, input);
  3016. else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
  3017. m560_populate_input(hidpp, input);
  3018. if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
  3019. hidpp10_wheel_populate_input(hidpp, input);
  3020. if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
  3021. hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
  3022. }
  3023. static int hidpp_input_configured(struct hid_device *hdev,
  3024. struct hid_input *hidinput)
  3025. {
  3026. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  3027. struct input_dev *input = hidinput->input;
  3028. if (!hidpp)
  3029. return 0;
  3030. hidpp_populate_input(hidpp, input);
  3031. return 0;
  3032. }
  3033. static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
  3034. int size)
  3035. {
  3036. struct hidpp_report *question = hidpp->send_receive_buf;
  3037. struct hidpp_report *answer = hidpp->send_receive_buf;
  3038. struct hidpp_report *report = (struct hidpp_report *)data;
  3039. int ret;
  3040. /*
  3041. * If the mutex is locked then we have a pending answer from a
  3042. * previously sent command.
  3043. */
  3044. if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
  3045. /*
  3046. * Check for a correct hidpp20 answer or the corresponding
  3047. * error
  3048. */
  3049. if (hidpp_match_answer(question, report) ||
  3050. hidpp_match_error(question, report)) {
  3051. *answer = *report;
  3052. hidpp->answer_available = true;
  3053. wake_up(&hidpp->wait);
  3054. /*
  3055. * This was an answer to a command that this driver sent
  3056. * We return 1 to hid-core to avoid forwarding the
  3057. * command upstream as it has been treated by the driver
  3058. */
  3059. return 1;
  3060. }
  3061. }
  3062. if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
  3063. atomic_set(&hidpp->connected,
  3064. !(report->rap.params[0] & (1 << 6)));
  3065. if (schedule_work(&hidpp->work) == 0)
  3066. dbg_hid("%s: connect event already queued\n", __func__);
  3067. return 1;
  3068. }
  3069. if (hidpp->hid_dev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
  3070. data[0] == REPORT_ID_HIDPP_SHORT &&
  3071. data[2] == HIDPP_SUB_ID_USER_IFACE_EVENT &&
  3072. (data[3] & HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST)) {
  3073. dev_err_ratelimited(&hidpp->hid_dev->dev,
  3074. "Error the keyboard's wireless encryption key has been lost, your keyboard will not work unless you re-configure encryption.\n");
  3075. dev_err_ratelimited(&hidpp->hid_dev->dev,
  3076. "See: https://gitlab.freedesktop.org/jwrdegoede/logitech-27mhz-keyboard-encryption-setup/\n");
  3077. }
  3078. if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
  3079. ret = hidpp20_battery_event_1000(hidpp, data, size);
  3080. if (ret != 0)
  3081. return ret;
  3082. ret = hidpp20_battery_event_1004(hidpp, data, size);
  3083. if (ret != 0)
  3084. return ret;
  3085. ret = hidpp_solar_battery_event(hidpp, data, size);
  3086. if (ret != 0)
  3087. return ret;
  3088. ret = hidpp20_battery_voltage_event(hidpp, data, size);
  3089. if (ret != 0)
  3090. return ret;
  3091. }
  3092. if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
  3093. ret = hidpp10_battery_event(hidpp, data, size);
  3094. if (ret != 0)
  3095. return ret;
  3096. }
  3097. if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
  3098. ret = hidpp10_wheel_raw_event(hidpp, data, size);
  3099. if (ret != 0)
  3100. return ret;
  3101. }
  3102. if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
  3103. ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
  3104. if (ret != 0)
  3105. return ret;
  3106. }
  3107. if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
  3108. ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
  3109. if (ret != 0)
  3110. return ret;
  3111. }
  3112. return 0;
  3113. }
  3114. static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
  3115. u8 *data, int size)
  3116. {
  3117. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  3118. int ret = 0;
  3119. if (!hidpp)
  3120. return 0;
  3121. /* Generic HID++ processing. */
  3122. switch (data[0]) {
  3123. case REPORT_ID_HIDPP_VERY_LONG:
  3124. if (size != hidpp->very_long_report_length) {
  3125. hid_err(hdev, "received hid++ report of bad size (%d)",
  3126. size);
  3127. return 1;
  3128. }
  3129. ret = hidpp_raw_hidpp_event(hidpp, data, size);
  3130. break;
  3131. case REPORT_ID_HIDPP_LONG:
  3132. if (size != HIDPP_REPORT_LONG_LENGTH) {
  3133. hid_err(hdev, "received hid++ report of bad size (%d)",
  3134. size);
  3135. return 1;
  3136. }
  3137. ret = hidpp_raw_hidpp_event(hidpp, data, size);
  3138. break;
  3139. case REPORT_ID_HIDPP_SHORT:
  3140. if (size != HIDPP_REPORT_SHORT_LENGTH) {
  3141. hid_err(hdev, "received hid++ report of bad size (%d)",
  3142. size);
  3143. return 1;
  3144. }
  3145. ret = hidpp_raw_hidpp_event(hidpp, data, size);
  3146. break;
  3147. }
  3148. /* If no report is available for further processing, skip calling
  3149. * raw_event of subclasses. */
  3150. if (ret != 0)
  3151. return ret;
  3152. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  3153. return wtp_raw_event(hdev, data, size);
  3154. else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
  3155. return m560_raw_event(hdev, data, size);
  3156. return 0;
  3157. }
  3158. static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
  3159. struct hid_usage *usage, __s32 value)
  3160. {
  3161. /* This function will only be called for scroll events, due to the
  3162. * restriction imposed in hidpp_usages.
  3163. */
  3164. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  3165. struct hidpp_scroll_counter *counter;
  3166. if (!hidpp)
  3167. return 0;
  3168. counter = &hidpp->vertical_wheel_counter;
  3169. /* A scroll event may occur before the multiplier has been retrieved or
  3170. * the input device set, or high-res scroll enabling may fail. In such
  3171. * cases we must return early (falling back to default behaviour) to
  3172. * avoid a crash in hidpp_scroll_counter_handle_scroll.
  3173. */
  3174. if (!(hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
  3175. || value == 0 || hidpp->input == NULL
  3176. || counter->wheel_multiplier == 0)
  3177. return 0;
  3178. hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
  3179. return 1;
  3180. }
  3181. static int hidpp_initialize_battery(struct hidpp_device *hidpp)
  3182. {
  3183. static atomic_t battery_no = ATOMIC_INIT(0);
  3184. struct power_supply_config cfg = { .drv_data = hidpp };
  3185. struct power_supply_desc *desc = &hidpp->battery.desc;
  3186. enum power_supply_property *battery_props;
  3187. struct hidpp_battery *battery;
  3188. unsigned int num_battery_props;
  3189. unsigned long n;
  3190. int ret;
  3191. if (hidpp->battery.ps)
  3192. return 0;
  3193. hidpp->battery.feature_index = 0xff;
  3194. hidpp->battery.solar_feature_index = 0xff;
  3195. hidpp->battery.voltage_feature_index = 0xff;
  3196. if (hidpp->protocol_major >= 2) {
  3197. if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
  3198. ret = hidpp_solar_request_battery_event(hidpp);
  3199. else {
  3200. /* we only support one battery feature right now, so let's
  3201. first check the ones that support battery level first
  3202. and leave voltage for last */
  3203. ret = hidpp20_query_battery_info_1000(hidpp);
  3204. if (ret)
  3205. ret = hidpp20_query_battery_info_1004(hidpp);
  3206. if (ret)
  3207. ret = hidpp20_query_battery_voltage_info(hidpp);
  3208. }
  3209. if (ret)
  3210. return ret;
  3211. hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
  3212. } else {
  3213. ret = hidpp10_query_battery_status(hidpp);
  3214. if (ret) {
  3215. ret = hidpp10_query_battery_mileage(hidpp);
  3216. if (ret)
  3217. return -ENOENT;
  3218. hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
  3219. } else {
  3220. hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
  3221. }
  3222. hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
  3223. }
  3224. battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
  3225. hidpp_battery_props,
  3226. sizeof(hidpp_battery_props),
  3227. GFP_KERNEL);
  3228. if (!battery_props)
  3229. return -ENOMEM;
  3230. num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
  3231. if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE ||
  3232. hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE ||
  3233. hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
  3234. battery_props[num_battery_props++] =
  3235. POWER_SUPPLY_PROP_CAPACITY;
  3236. if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
  3237. battery_props[num_battery_props++] =
  3238. POWER_SUPPLY_PROP_CAPACITY_LEVEL;
  3239. if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
  3240. battery_props[num_battery_props++] =
  3241. POWER_SUPPLY_PROP_VOLTAGE_NOW;
  3242. battery = &hidpp->battery;
  3243. n = atomic_inc_return(&battery_no) - 1;
  3244. desc->properties = battery_props;
  3245. desc->num_properties = num_battery_props;
  3246. desc->get_property = hidpp_battery_get_property;
  3247. sprintf(battery->name, "hidpp_battery_%ld", n);
  3248. desc->name = battery->name;
  3249. desc->type = POWER_SUPPLY_TYPE_BATTERY;
  3250. desc->use_for_apm = 0;
  3251. battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
  3252. &battery->desc,
  3253. &cfg);
  3254. if (IS_ERR(battery->ps))
  3255. return PTR_ERR(battery->ps);
  3256. power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
  3257. return ret;
  3258. }
  3259. static void hidpp_overwrite_name(struct hid_device *hdev)
  3260. {
  3261. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  3262. char *name;
  3263. if (hidpp->protocol_major < 2)
  3264. return;
  3265. name = hidpp_get_device_name(hidpp);
  3266. if (!name) {
  3267. hid_err(hdev, "unable to retrieve the name of the device");
  3268. } else {
  3269. dbg_hid("HID++: Got name: %s\n", name);
  3270. snprintf(hdev->name, sizeof(hdev->name), "%s", name);
  3271. }
  3272. kfree(name);
  3273. }
  3274. static int hidpp_input_open(struct input_dev *dev)
  3275. {
  3276. struct hid_device *hid = input_get_drvdata(dev);
  3277. return hid_hw_open(hid);
  3278. }
  3279. static void hidpp_input_close(struct input_dev *dev)
  3280. {
  3281. struct hid_device *hid = input_get_drvdata(dev);
  3282. hid_hw_close(hid);
  3283. }
  3284. static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
  3285. {
  3286. struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
  3287. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  3288. if (!input_dev)
  3289. return NULL;
  3290. input_set_drvdata(input_dev, hdev);
  3291. input_dev->open = hidpp_input_open;
  3292. input_dev->close = hidpp_input_close;
  3293. input_dev->name = hidpp->name;
  3294. input_dev->phys = hdev->phys;
  3295. input_dev->uniq = hdev->uniq;
  3296. input_dev->id.bustype = hdev->bus;
  3297. input_dev->id.vendor = hdev->vendor;
  3298. input_dev->id.product = hdev->product;
  3299. input_dev->id.version = hdev->version;
  3300. input_dev->dev.parent = &hdev->dev;
  3301. return input_dev;
  3302. }
  3303. static void hidpp_connect_event(struct hidpp_device *hidpp)
  3304. {
  3305. struct hid_device *hdev = hidpp->hid_dev;
  3306. int ret = 0;
  3307. bool connected = atomic_read(&hidpp->connected);
  3308. struct input_dev *input;
  3309. char *name, *devm_name;
  3310. if (!connected) {
  3311. if (hidpp->battery.ps) {
  3312. hidpp->battery.online = false;
  3313. hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
  3314. hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
  3315. power_supply_changed(hidpp->battery.ps);
  3316. }
  3317. return;
  3318. }
  3319. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
  3320. ret = wtp_connect(hdev, connected);
  3321. if (ret)
  3322. return;
  3323. } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
  3324. ret = m560_send_config_command(hdev, connected);
  3325. if (ret)
  3326. return;
  3327. } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
  3328. ret = k400_connect(hdev, connected);
  3329. if (ret)
  3330. return;
  3331. }
  3332. if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
  3333. ret = hidpp10_wheel_connect(hidpp);
  3334. if (ret)
  3335. return;
  3336. }
  3337. if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
  3338. ret = hidpp10_extra_mouse_buttons_connect(hidpp);
  3339. if (ret)
  3340. return;
  3341. }
  3342. if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
  3343. ret = hidpp10_consumer_keys_connect(hidpp);
  3344. if (ret)
  3345. return;
  3346. }
  3347. /* the device is already connected, we can ask for its name and
  3348. * protocol */
  3349. if (!hidpp->protocol_major) {
  3350. ret = hidpp_root_get_protocol_version(hidpp);
  3351. if (ret) {
  3352. hid_err(hdev, "Can not get the protocol version.\n");
  3353. return;
  3354. }
  3355. }
  3356. if (hidpp->protocol_major >= 2) {
  3357. u8 feature_index;
  3358. if (!hidpp_get_wireless_feature_index(hidpp, &feature_index))
  3359. hidpp->wireless_feature_index = feature_index;
  3360. }
  3361. if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
  3362. name = hidpp_get_device_name(hidpp);
  3363. if (name) {
  3364. devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
  3365. "%s", name);
  3366. kfree(name);
  3367. if (!devm_name)
  3368. return;
  3369. hidpp->name = devm_name;
  3370. }
  3371. }
  3372. hidpp_initialize_battery(hidpp);
  3373. if (!hid_is_usb(hidpp->hid_dev))
  3374. hidpp_initialize_hires_scroll(hidpp);
  3375. /* forward current battery state */
  3376. if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
  3377. hidpp10_enable_battery_reporting(hidpp);
  3378. if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
  3379. hidpp10_query_battery_mileage(hidpp);
  3380. else
  3381. hidpp10_query_battery_status(hidpp);
  3382. } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
  3383. if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
  3384. hidpp20_query_battery_voltage_info(hidpp);
  3385. else if (hidpp->capabilities & HIDPP_CAPABILITY_UNIFIED_BATTERY)
  3386. hidpp20_query_battery_info_1004(hidpp);
  3387. else
  3388. hidpp20_query_battery_info_1000(hidpp);
  3389. }
  3390. if (hidpp->battery.ps)
  3391. power_supply_changed(hidpp->battery.ps);
  3392. if (hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
  3393. hi_res_scroll_enable(hidpp);
  3394. if (!(hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) || hidpp->delayed_input)
  3395. /* if the input nodes are already created, we can stop now */
  3396. return;
  3397. input = hidpp_allocate_input(hdev);
  3398. if (!input) {
  3399. hid_err(hdev, "cannot allocate new input device: %d\n", ret);
  3400. return;
  3401. }
  3402. hidpp_populate_input(hidpp, input);
  3403. ret = input_register_device(input);
  3404. if (ret) {
  3405. input_free_device(input);
  3406. return;
  3407. }
  3408. hidpp->delayed_input = input;
  3409. }
  3410. static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
  3411. static struct attribute *sysfs_attrs[] = {
  3412. &dev_attr_builtin_power_supply.attr,
  3413. NULL
  3414. };
  3415. static const struct attribute_group ps_attribute_group = {
  3416. .attrs = sysfs_attrs
  3417. };
  3418. static int hidpp_get_report_length(struct hid_device *hdev, int id)
  3419. {
  3420. struct hid_report_enum *re;
  3421. struct hid_report *report;
  3422. re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
  3423. report = re->report_id_hash[id];
  3424. if (!report)
  3425. return 0;
  3426. return report->field[0]->report_count + 1;
  3427. }
  3428. static u8 hidpp_validate_device(struct hid_device *hdev)
  3429. {
  3430. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  3431. int id, report_length;
  3432. u8 supported_reports = 0;
  3433. id = REPORT_ID_HIDPP_SHORT;
  3434. report_length = hidpp_get_report_length(hdev, id);
  3435. if (report_length) {
  3436. if (report_length < HIDPP_REPORT_SHORT_LENGTH)
  3437. goto bad_device;
  3438. supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
  3439. }
  3440. id = REPORT_ID_HIDPP_LONG;
  3441. report_length = hidpp_get_report_length(hdev, id);
  3442. if (report_length) {
  3443. if (report_length < HIDPP_REPORT_LONG_LENGTH)
  3444. goto bad_device;
  3445. supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
  3446. }
  3447. id = REPORT_ID_HIDPP_VERY_LONG;
  3448. report_length = hidpp_get_report_length(hdev, id);
  3449. if (report_length) {
  3450. if (report_length < HIDPP_REPORT_LONG_LENGTH ||
  3451. report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
  3452. goto bad_device;
  3453. supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
  3454. hidpp->very_long_report_length = report_length;
  3455. }
  3456. return supported_reports;
  3457. bad_device:
  3458. hid_warn(hdev, "not enough values in hidpp report %d\n", id);
  3459. return false;
  3460. }
  3461. static bool hidpp_application_equals(struct hid_device *hdev,
  3462. unsigned int application)
  3463. {
  3464. struct list_head *report_list;
  3465. struct hid_report *report;
  3466. report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
  3467. report = list_first_entry_or_null(report_list, struct hid_report, list);
  3468. return report && report->application == application;
  3469. }
  3470. static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
  3471. {
  3472. struct hidpp_device *hidpp;
  3473. int ret;
  3474. bool connected;
  3475. unsigned int connect_mask = HID_CONNECT_DEFAULT;
  3476. struct hidpp_ff_private_data data;
  3477. /* report_fixup needs drvdata to be set before we call hid_parse */
  3478. hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
  3479. if (!hidpp)
  3480. return -ENOMEM;
  3481. hidpp->hid_dev = hdev;
  3482. hidpp->name = hdev->name;
  3483. hidpp->quirks = id->driver_data;
  3484. hid_set_drvdata(hdev, hidpp);
  3485. ret = hid_parse(hdev);
  3486. if (ret) {
  3487. hid_err(hdev, "%s:parse failed\n", __func__);
  3488. return ret;
  3489. }
  3490. /*
  3491. * Make sure the device is HID++ capable, otherwise treat as generic HID
  3492. */
  3493. hidpp->supported_reports = hidpp_validate_device(hdev);
  3494. if (!hidpp->supported_reports) {
  3495. hid_set_drvdata(hdev, NULL);
  3496. devm_kfree(&hdev->dev, hidpp);
  3497. return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  3498. }
  3499. if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
  3500. hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
  3501. if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
  3502. hidpp_application_equals(hdev, HID_GD_MOUSE))
  3503. hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
  3504. HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
  3505. if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
  3506. hidpp_application_equals(hdev, HID_GD_KEYBOARD))
  3507. hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
  3508. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
  3509. ret = wtp_allocate(hdev, id);
  3510. if (ret)
  3511. return ret;
  3512. } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
  3513. ret = k400_allocate(hdev);
  3514. if (ret)
  3515. return ret;
  3516. }
  3517. INIT_WORK(&hidpp->work, delayed_work_cb);
  3518. mutex_init(&hidpp->send_mutex);
  3519. init_waitqueue_head(&hidpp->wait);
  3520. /* indicates we are handling the battery properties in the kernel */
  3521. ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
  3522. if (ret)
  3523. hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
  3524. hdev->name);
  3525. /*
  3526. * First call hid_hw_start(hdev, 0) to allow IO without connecting any
  3527. * hid subdrivers (hid-input, hidraw). This allows retrieving the dev's
  3528. * name and serial number and store these in hdev->name and hdev->uniq,
  3529. * before the hid-input and hidraw drivers expose these to userspace.
  3530. */
  3531. ret = hid_hw_start(hdev, 0);
  3532. if (ret) {
  3533. hid_err(hdev, "hw start failed\n");
  3534. goto hid_hw_start_fail;
  3535. }
  3536. ret = hid_hw_open(hdev);
  3537. if (ret < 0) {
  3538. dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
  3539. __func__, ret);
  3540. goto hid_hw_open_fail;
  3541. }
  3542. /* Allow incoming packets */
  3543. hid_device_io_start(hdev);
  3544. if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
  3545. hidpp_unifying_init(hidpp);
  3546. else if (hid_is_usb(hidpp->hid_dev))
  3547. hidpp_serial_init(hidpp);
  3548. connected = hidpp_root_get_protocol_version(hidpp) == 0;
  3549. atomic_set(&hidpp->connected, connected);
  3550. if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
  3551. if (!connected) {
  3552. ret = -ENODEV;
  3553. hid_err(hdev, "Device not connected");
  3554. goto hid_hw_init_fail;
  3555. }
  3556. hidpp_overwrite_name(hdev);
  3557. }
  3558. if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
  3559. ret = wtp_get_config(hidpp);
  3560. if (ret)
  3561. goto hid_hw_init_fail;
  3562. } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
  3563. ret = g920_get_config(hidpp, &data);
  3564. if (ret)
  3565. goto hid_hw_init_fail;
  3566. }
  3567. schedule_work(&hidpp->work);
  3568. flush_work(&hidpp->work);
  3569. if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
  3570. connect_mask &= ~HID_CONNECT_HIDINPUT;
  3571. /* Now export the actual inputs and hidraw nodes to the world */
  3572. ret = hid_connect(hdev, connect_mask);
  3573. if (ret) {
  3574. hid_err(hdev, "%s:hid_connect returned error %d\n", __func__, ret);
  3575. goto hid_hw_init_fail;
  3576. }
  3577. if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
  3578. ret = hidpp_ff_init(hidpp, &data);
  3579. if (ret)
  3580. hid_warn(hidpp->hid_dev,
  3581. "Unable to initialize force feedback support, errno %d\n",
  3582. ret);
  3583. }
  3584. /*
  3585. * This relies on logi_dj_ll_close() being a no-op so that DJ connection
  3586. * events will still be received.
  3587. */
  3588. hid_hw_close(hdev);
  3589. return ret;
  3590. hid_hw_init_fail:
  3591. hid_hw_close(hdev);
  3592. hid_hw_open_fail:
  3593. hid_hw_stop(hdev);
  3594. hid_hw_start_fail:
  3595. sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
  3596. cancel_work_sync(&hidpp->work);
  3597. mutex_destroy(&hidpp->send_mutex);
  3598. return ret;
  3599. }
  3600. static void hidpp_remove(struct hid_device *hdev)
  3601. {
  3602. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  3603. if (!hidpp)
  3604. return hid_hw_stop(hdev);
  3605. sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
  3606. hid_hw_stop(hdev);
  3607. cancel_work_sync(&hidpp->work);
  3608. mutex_destroy(&hidpp->send_mutex);
  3609. }
  3610. #define LDJ_DEVICE(product) \
  3611. HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
  3612. USB_VENDOR_ID_LOGITECH, (product))
  3613. #define L27MHZ_DEVICE(product) \
  3614. HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
  3615. USB_VENDOR_ID_LOGITECH, (product))
  3616. static const struct hid_device_id hidpp_devices[] = {
  3617. { /* wireless touchpad */
  3618. LDJ_DEVICE(0x4011),
  3619. .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
  3620. HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
  3621. { /* wireless touchpad T650 */
  3622. LDJ_DEVICE(0x4101),
  3623. .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
  3624. { /* wireless touchpad T651 */
  3625. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
  3626. USB_DEVICE_ID_LOGITECH_T651),
  3627. .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
  3628. { /* Mouse Logitech Anywhere MX */
  3629. LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
  3630. { /* Mouse logitech M560 */
  3631. LDJ_DEVICE(0x402d),
  3632. .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
  3633. { /* Mouse Logitech M705 (firmware RQM17) */
  3634. LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
  3635. { /* Mouse Logitech Performance MX */
  3636. LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
  3637. { /* Keyboard logitech K400 */
  3638. LDJ_DEVICE(0x4024),
  3639. .driver_data = HIDPP_QUIRK_CLASS_K400 },
  3640. { /* Solar Keyboard Logitech K750 */
  3641. LDJ_DEVICE(0x4002),
  3642. .driver_data = HIDPP_QUIRK_CLASS_K750 },
  3643. { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
  3644. LDJ_DEVICE(0xb305),
  3645. .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
  3646. { /* Dinovo Edge (Bluetooth-receiver in HID proxy mode) */
  3647. LDJ_DEVICE(0xb309),
  3648. .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
  3649. { /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
  3650. LDJ_DEVICE(0xb30b),
  3651. .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
  3652. { LDJ_DEVICE(HID_ANY_ID) },
  3653. { /* Keyboard LX501 (Y-RR53) */
  3654. L27MHZ_DEVICE(0x0049),
  3655. .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
  3656. { /* Keyboard MX3000 (Y-RAM74) */
  3657. L27MHZ_DEVICE(0x0057),
  3658. .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
  3659. { /* Keyboard MX3200 (Y-RAV80) */
  3660. L27MHZ_DEVICE(0x005c),
  3661. .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
  3662. { /* S510 Media Remote */
  3663. L27MHZ_DEVICE(0x00fe),
  3664. .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
  3665. { L27MHZ_DEVICE(HID_ANY_ID) },
  3666. { /* Logitech G403 Wireless Gaming Mouse over USB */
  3667. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
  3668. { /* Logitech G703 Gaming Mouse over USB */
  3669. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
  3670. { /* Logitech G703 Hero Gaming Mouse over USB */
  3671. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
  3672. { /* Logitech G900 Gaming Mouse over USB */
  3673. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
  3674. { /* Logitech G903 Gaming Mouse over USB */
  3675. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
  3676. { /* Logitech G903 Hero Gaming Mouse over USB */
  3677. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
  3678. { /* Logitech G915 TKL Keyboard over USB */
  3679. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC343) },
  3680. { /* Logitech G920 Wheel over USB */
  3681. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
  3682. .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
  3683. { /* Logitech G Pro Gaming Mouse over USB */
  3684. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
  3685. { /* MX5000 keyboard over Bluetooth */
  3686. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
  3687. .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
  3688. { /* Dinovo Edge keyboard over Bluetooth */
  3689. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309),
  3690. .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
  3691. { /* MX5500 keyboard over Bluetooth */
  3692. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
  3693. .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
  3694. { /* Logitech G915 TKL keyboard over Bluetooth */
  3695. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb35f) },
  3696. { /* M-RCQ142 V470 Cordless Laser Mouse over Bluetooth */
  3697. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb008) },
  3698. { /* MX Master mouse over Bluetooth */
  3699. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012) },
  3700. { /* M720 Triathlon mouse over Bluetooth */
  3701. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb015) },
  3702. { /* MX Ergo trackball over Bluetooth */
  3703. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) },
  3704. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e) },
  3705. { /* MX Master 3 mouse over Bluetooth */
  3706. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023) },
  3707. { /* MX Master 3S mouse over Bluetooth */
  3708. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb034) },
  3709. {}
  3710. };
  3711. MODULE_DEVICE_TABLE(hid, hidpp_devices);
  3712. static const struct hid_usage_id hidpp_usages[] = {
  3713. { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
  3714. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  3715. };
  3716. static struct hid_driver hidpp_driver = {
  3717. .name = "logitech-hidpp-device",
  3718. .id_table = hidpp_devices,
  3719. .report_fixup = hidpp_report_fixup,
  3720. .probe = hidpp_probe,
  3721. .remove = hidpp_remove,
  3722. .raw_event = hidpp_raw_event,
  3723. .usage_table = hidpp_usages,
  3724. .event = hidpp_event,
  3725. .input_configured = hidpp_input_configured,
  3726. .input_mapping = hidpp_input_mapping,
  3727. .input_mapped = hidpp_input_mapped,
  3728. };
  3729. module_hid_driver(hidpp_driver);