hid-logitech-hidpp.c 133 KB

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