0001-surface-acpi.patch 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350
  1. From fe620408edbe977514da914279f549e7d8fb036a Mon Sep 17 00:00:00 2001
  2. From: Maximilian Luz <luzmaximilian@gmail.com>
  3. Date: Wed, 2 Oct 2019 22:36:03 +0200
  4. Subject: [PATCH 01/12] surface-acpi
  5. ---
  6. drivers/acpi/acpica/dsopcode.c | 2 +-
  7. drivers/acpi/acpica/exfield.c | 12 +-
  8. drivers/platform/x86/Kconfig | 97 +
  9. drivers/platform/x86/Makefile | 1 +
  10. drivers/platform/x86/surface_acpi.c | 4010 +++++++++++++++++++++++++++
  11. drivers/tty/serdev/core.c | 111 +-
  12. 6 files changed, 4217 insertions(+), 16 deletions(-)
  13. create mode 100644 drivers/platform/x86/surface_acpi.c
  14. diff --git a/drivers/acpi/acpica/dsopcode.c b/drivers/acpi/acpica/dsopcode.c
  15. index 10f32b62608e..7b2a4987f050 100644
  16. --- a/drivers/acpi/acpica/dsopcode.c
  17. +++ b/drivers/acpi/acpica/dsopcode.c
  18. @@ -123,7 +123,7 @@ acpi_ds_init_buffer_field(u16 aml_opcode,
  19. /* Offset is in bits, count is in bits */
  20. - field_flags = AML_FIELD_ACCESS_BYTE;
  21. + field_flags = AML_FIELD_ACCESS_BUFFER;
  22. bit_offset = offset;
  23. bit_count = (u32) length_desc->integer.value;
  24. diff --git a/drivers/acpi/acpica/exfield.c b/drivers/acpi/acpica/exfield.c
  25. index d3d2dbfba680..0b7f617a6e9b 100644
  26. --- a/drivers/acpi/acpica/exfield.c
  27. +++ b/drivers/acpi/acpica/exfield.c
  28. @@ -109,6 +109,7 @@ acpi_ex_read_data_from_field(struct acpi_walk_state *walk_state,
  29. union acpi_operand_object *buffer_desc;
  30. void *buffer;
  31. u32 buffer_length;
  32. + u8 field_flags;
  33. ACPI_FUNCTION_TRACE_PTR(ex_read_data_from_field, obj_desc);
  34. @@ -157,11 +158,16 @@ acpi_ex_read_data_from_field(struct acpi_walk_state *walk_state,
  35. * Note: Field.length is in bits.
  36. */
  37. buffer_length =
  38. - (acpi_size)ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->field.bit_length);
  39. + (acpi_size)ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length);
  40. + field_flags = obj_desc->common_field.field_flags;
  41. - if (buffer_length > acpi_gbl_integer_byte_width) {
  42. + if (buffer_length > acpi_gbl_integer_byte_width ||
  43. + (field_flags & AML_FIELD_ACCESS_TYPE_MASK) == AML_FIELD_ACCESS_BUFFER) {
  44. - /* Field is too large for an Integer, create a Buffer instead */
  45. + /*
  46. + * Field is either too large for an Integer, or a actually of type
  47. + * buffer, so create a Buffer.
  48. + */
  49. buffer_desc = acpi_ut_create_buffer_object(buffer_length);
  50. if (!buffer_desc) {
  51. diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
  52. index 7c2fd1d72e18..c00cb830914a 100644
  53. --- a/drivers/platform/x86/Kconfig
  54. +++ b/drivers/platform/x86/Kconfig
  55. @@ -623,6 +623,103 @@ config THINKPAD_ACPI_HOTKEY_POLL
  56. If you are not sure, say Y here. The driver enables polling only if
  57. it is strictly necessary to do so.
  58. +config SURFACE_ACPI
  59. + depends on ACPI
  60. + tristate "Microsoft Surface ACPI/Platform Drivers"
  61. + ---help---
  62. + ACPI and platform drivers for Microsoft Surface devices.
  63. +
  64. +config SURFACE_ACPI_SSH
  65. + bool "Surface Serial Hub Driver"
  66. + depends on SURFACE_ACPI
  67. + depends on X86_INTEL_LPSS
  68. + depends on SERIAL_8250_DW
  69. + depends on SERIAL_8250_DMA
  70. + depends on SERIAL_DEV_CTRL_TTYPORT
  71. + select CRC_CCITT
  72. + default y
  73. + ---help---
  74. + Surface Serial Hub driver for 5th generation (or later) Microsoft
  75. + Surface devices.
  76. +
  77. + This is the base driver for the embedded serial controller found on
  78. + 5th generation (and later) Microsoft Surface devices (e.g. Book 2,
  79. + Laptop, Laptop 2, Pro 2017, Pro 6, ...). This driver itself only
  80. + provides access to the embedded controller and subsequent drivers are
  81. + required for the respective functionalities.
  82. +
  83. + If you have a 5th generation (or later) Microsoft Surface device, say
  84. + Y or M here.
  85. +
  86. +config SURFACE_ACPI_SSH_DEBUG_DEVICE
  87. + bool "Surface Serial Hub Debug Device"
  88. + depends on SURFACE_ACPI_SSH
  89. + default n
  90. + ---help---
  91. + Debug device for direct communication with the embedded controller
  92. + found on 5th generation (and later) Microsoft Surface devices (e.g.
  93. + Book 2, Laptop, Laptop 2, Pro 2017, Pro 6, ...) via sysfs.
  94. +
  95. + If you are not sure, say N here.
  96. +
  97. +config SURFACE_ACPI_SAN
  98. + bool "Surface ACPI Notify Driver"
  99. + depends on SURFACE_ACPI_SSH
  100. + default y
  101. + ---help---
  102. + Surface ACPI Notify driver for 5th generation (or later) Microsoft
  103. + Surface devices.
  104. +
  105. + This driver enables basic ACPI events and requests, such as battery
  106. + status requests/events, thermal events, lid status, and possibly more,
  107. + which would otherwise not work on these devices.
  108. +
  109. + If you are not sure, say Y here.
  110. +
  111. +config SURFACE_ACPI_VHF
  112. + bool "Surface Virtual HID Framework Driver"
  113. + depends on SURFACE_ACPI_SSH
  114. + depends on HID
  115. + default y
  116. + ---help---
  117. + Surface Virtual HID Framework driver for 5th generation (or later)
  118. + Microsoft Surface devices.
  119. +
  120. + This driver provides support for the Microsoft Virtual HID framework,
  121. + which is required for the Surface Laptop (1 and newer) keyboard.
  122. +
  123. + If you are not sure, say Y here.
  124. +
  125. +config SURFACE_ACPI_DTX
  126. + bool "Surface Detachment System (DTX) Driver"
  127. + depends on SURFACE_ACPI_SSH
  128. + depends on INPUT
  129. + default y
  130. + ---help---
  131. + Surface Detachment System (DTX) driver for the Microsoft Surface Book
  132. + 2. This driver provides support for proper detachment handling in
  133. + user-space, status-events relating to the base and support for
  134. + the safe-guard keeping the base attached when the discrete GPU
  135. + contained in it is running via the special /dev/surface-dtx device.
  136. +
  137. + Also provides a standard input device to provide SW_TABLET_MODE events
  138. + upon device mode change.
  139. +
  140. + If you are not sure, say Y here.
  141. +
  142. +config SURFACE_ACPI_SID
  143. + bool "Surface Platform Integration Driver"
  144. + depends on SURFACE_ACPI_SSH
  145. + default y
  146. + ---help---
  147. + Surface Platform Integration Driver for the Microsoft Surface Devices.
  148. + Currently only supports the Surface Book 2. This driver provides suport
  149. + for setting performance-modes via the perf_mode sysfs attribute.
  150. + Performance-modes directly influence the fan-profile of the device,
  151. + allowing to choose between higher performance or quieter operation.
  152. +
  153. + If you are not sure, say Y here.
  154. +
  155. config SENSORS_HDAPS
  156. tristate "Thinkpad Hard Drive Active Protection System (hdaps)"
  157. depends on INPUT
  158. diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
  159. index 87b0069bd781..8b12c19dc165 100644
  160. --- a/drivers/platform/x86/Makefile
  161. +++ b/drivers/platform/x86/Makefile
  162. @@ -39,6 +39,7 @@ obj-$(CONFIG_TC1100_WMI) += tc1100-wmi.o
  163. obj-$(CONFIG_SONY_LAPTOP) += sony-laptop.o
  164. obj-$(CONFIG_IDEAPAD_LAPTOP) += ideapad-laptop.o
  165. obj-$(CONFIG_THINKPAD_ACPI) += thinkpad_acpi.o
  166. +obj-$(CONFIG_SURFACE_ACPI) += surface_acpi.o
  167. obj-$(CONFIG_SENSORS_HDAPS) += hdaps.o
  168. obj-$(CONFIG_FUJITSU_LAPTOP) += fujitsu-laptop.o
  169. obj-$(CONFIG_FUJITSU_TABLET) += fujitsu-tablet.o
  170. diff --git a/drivers/platform/x86/surface_acpi.c b/drivers/platform/x86/surface_acpi.c
  171. new file mode 100644
  172. index 000000000000..5dbf48a3d9b3
  173. --- /dev/null
  174. +++ b/drivers/platform/x86/surface_acpi.c
  175. @@ -0,0 +1,4010 @@
  176. +#include <asm/unaligned.h>
  177. +#include <linux/acpi.h>
  178. +#include <linux/completion.h>
  179. +#include <linux/crc-ccitt.h>
  180. +#include <linux/delay.h>
  181. +#include <linux/device.h>
  182. +#include <linux/dmaengine.h>
  183. +#include <linux/dmi.h>
  184. +#include <linux/fs.h>
  185. +#include <linux/hid.h>
  186. +#include <linux/input.h>
  187. +#include <linux/ioctl.h>
  188. +#include <linux/jiffies.h>
  189. +#include <linux/kernel.h>
  190. +#include <linux/kfifo.h>
  191. +#include <linux/miscdevice.h>
  192. +#include <linux/module.h>
  193. +#include <linux/moduleparam.h>
  194. +#include <linux/mutex.h>
  195. +#include <linux/platform_device.h>
  196. +#include <linux/pm.h>
  197. +#include <linux/poll.h>
  198. +#include <linux/rculist.h>
  199. +#include <linux/refcount.h>
  200. +#include <linux/serdev.h>
  201. +#include <linux/spinlock.h>
  202. +#include <linux/sysfs.h>
  203. +#include <linux/types.h>
  204. +#include <linux/workqueue.h>
  205. +
  206. +
  207. +#define USB_VENDOR_ID_MICROSOFT 0x045e
  208. +#define USB_DEVICE_ID_MS_VHF 0xf001
  209. +#define USB_DEVICE_ID_MS_SURFACE_BASE_2_INTEGRATION 0x0922
  210. +
  211. +#define SG5_PARAM_PERM (S_IRUGO | S_IWUSR)
  212. +
  213. +
  214. +/*************************************************************************
  215. + * Surface Serial Hub driver (cross-driver interface)
  216. + */
  217. +
  218. +#ifdef CONFIG_SURFACE_ACPI_SSH
  219. +
  220. +/*
  221. + * Maximum request payload size in bytes.
  222. + * Value based on ACPI (255 bytes minus header/status bytes).
  223. + */
  224. +#define SURFACEGEN5_MAX_RQST_PAYLOAD (255 - 10)
  225. +
  226. +/*
  227. + * Maximum response payload size in bytes.
  228. + * Value based on ACPI (255 bytes minus header/status bytes).
  229. + */
  230. +#define SURFACEGEN5_MAX_RQST_RESPONSE (255 - 4)
  231. +
  232. +#define SURFACEGEN5_RQID_EVENT_BITS 5
  233. +
  234. +#define SURFACEGEN5_EVENT_IMMEDIATE ((unsigned long) -1)
  235. +
  236. +
  237. +struct surfacegen5_buf {
  238. + u8 cap;
  239. + u8 len;
  240. + u8 *data;
  241. +};
  242. +
  243. +struct surfacegen5_rqst {
  244. + u8 tc;
  245. + u8 iid;
  246. + u8 cid;
  247. + u8 snc;
  248. + u8 cdl;
  249. + u8 *pld;
  250. +};
  251. +
  252. +struct surfacegen5_event {
  253. + u16 rqid;
  254. + u8 tc;
  255. + u8 iid;
  256. + u8 cid;
  257. + u8 len;
  258. + u8 *pld;
  259. +};
  260. +
  261. +
  262. +typedef int (*surfacegen5_ec_event_handler_fn)(struct surfacegen5_event *event, void *data);
  263. +typedef unsigned long (*surfacegen5_ec_event_handler_delay)(struct surfacegen5_event *event, void *data);
  264. +
  265. +int surfacegen5_ec_consumer_register(struct device *consumer);
  266. +
  267. +int surfacegen5_ec_rqst(const struct surfacegen5_rqst *rqst, struct surfacegen5_buf *result);
  268. +
  269. +int surfacegen5_ec_enable_event_source(u8 tc, u8 unknown, u16 rqid);
  270. +int surfacegen5_ec_disable_event_source(u8 tc, u8 unknown, u16 rqid);
  271. +int surfacegen5_ec_remove_event_handler(u16 rqid);
  272. +int surfacegen5_ec_set_event_handler(u16 rqid, surfacegen5_ec_event_handler_fn fn, void *data);
  273. +int surfacegen5_ec_set_delayed_event_handler(u16 rqid,
  274. + surfacegen5_ec_event_handler_fn fn,
  275. + surfacegen5_ec_event_handler_delay delay, void *data);
  276. +
  277. +#endif /* CONFIG_SURFACE_ACPI_SSH */
  278. +
  279. +
  280. +/*************************************************************************
  281. + * Surface Serial Hub Debug Device (cross-driver interface)
  282. + */
  283. +
  284. +#ifdef CONFIG_SURFACE_ACPI_SSH
  285. +
  286. +int surfacegen5_ssh_sysfs_register(struct device *dev);
  287. +void surfacegen5_ssh_sysfs_unregister(struct device *dev);
  288. +
  289. +#endif /* CONFIG_SURFACE_ACPI_SSH */
  290. +
  291. +
  292. +/*************************************************************************
  293. + * Surface Serial Hub driver (private implementation)
  294. + */
  295. +
  296. +#ifdef CONFIG_SURFACE_ACPI_SSH
  297. +
  298. +#define SG5_RQST_TAG_FULL "surfacegen5_ec_rqst: "
  299. +#define SG5_RQST_TAG "rqst: "
  300. +#define SG5_EVENT_TAG "event: "
  301. +#define SG5_RECV_TAG "recv: "
  302. +
  303. +#define SG5_SUPPORTED_FLOW_CONTROL_MASK (~((u8) ACPI_UART_FLOW_CONTROL_HW))
  304. +
  305. +#define SG5_BYTELEN_SYNC 2
  306. +#define SG5_BYTELEN_TERM 2
  307. +#define SG5_BYTELEN_CRC 2
  308. +#define SG5_BYTELEN_CTRL 4 // command-header, ACK, or RETRY
  309. +#define SG5_BYTELEN_CMDFRAME 8 // without payload
  310. +
  311. +#define SG5_MAX_WRITE ( \
  312. + SG5_BYTELEN_SYNC \
  313. + + SG5_BYTELEN_CTRL \
  314. + + SG5_BYTELEN_CRC \
  315. + + SG5_BYTELEN_CMDFRAME \
  316. + + SURFACEGEN5_MAX_RQST_PAYLOAD \
  317. + + SG5_BYTELEN_CRC \
  318. +)
  319. +
  320. +#define SG5_MSG_LEN_CTRL ( \
  321. + SG5_BYTELEN_SYNC \
  322. + + SG5_BYTELEN_CTRL \
  323. + + SG5_BYTELEN_CRC \
  324. + + SG5_BYTELEN_TERM \
  325. +)
  326. +
  327. +#define SG5_MSG_LEN_CMD_BASE ( \
  328. + SG5_BYTELEN_SYNC \
  329. + + SG5_BYTELEN_CTRL \
  330. + + SG5_BYTELEN_CRC \
  331. + + SG5_BYTELEN_CRC \
  332. +) // without payload and command-frame
  333. +
  334. +#define SG5_WRITE_TIMEOUT msecs_to_jiffies(1000)
  335. +#define SG5_READ_TIMEOUT msecs_to_jiffies(1000)
  336. +#define SG5_NUM_RETRY 3
  337. +
  338. +#define SG5_WRITE_BUF_LEN SG5_MAX_WRITE
  339. +#define SG5_READ_BUF_LEN 512 // must be power of 2
  340. +#define SG5_EVAL_BUF_LEN SG5_MAX_WRITE // also works for reading
  341. +
  342. +#define SG5_FRAME_TYPE_CMD 0x80
  343. +#define SG5_FRAME_TYPE_ACK 0x40
  344. +#define SG5_FRAME_TYPE_RETRY 0x04
  345. +
  346. +#define SG5_FRAME_OFFS_CTRL SG5_BYTELEN_SYNC
  347. +#define SG5_FRAME_OFFS_CTRL_CRC (SG5_FRAME_OFFS_CTRL + SG5_BYTELEN_CTRL)
  348. +#define SG5_FRAME_OFFS_TERM (SG5_FRAME_OFFS_CTRL_CRC + SG5_BYTELEN_CRC)
  349. +#define SG5_FRAME_OFFS_CMD SG5_FRAME_OFFS_TERM // either TERM or CMD
  350. +#define SG5_FRAME_OFFS_CMD_PLD (SG5_FRAME_OFFS_CMD + SG5_BYTELEN_CMDFRAME)
  351. +
  352. +/*
  353. + * A note on Request IDs (RQIDs):
  354. + * 0x0000 is not a valid RQID
  355. + * 0x0001 is valid, but reserved for Surface Laptop keyboard events
  356. + */
  357. +#define SG5_NUM_EVENT_TYPES ((1 << SURFACEGEN5_RQID_EVENT_BITS) - 1)
  358. +
  359. +/*
  360. + * Sync: aa 55
  361. + * Terminate: ff ff
  362. + *
  363. + * Request Message: sync cmd-hdr crc(cmd-hdr) cmd-rqst-frame crc(cmd-rqst-frame)
  364. + * Ack Message: sync ack crc(ack) terminate
  365. + * Retry Message: sync retry crc(retry) terminate
  366. + * Response Message: sync cmd-hdr crc(cmd-hdr) cmd-resp-frame crc(cmd-resp-frame)
  367. + *
  368. + * Command Header: 80 LEN 00 SEQ
  369. + * Ack: 40 00 00 SEQ
  370. + * Retry: 04 00 00 00
  371. + * Command Request Frame: 80 RTC 01 00 RIID RQID RCID PLD
  372. + * Command Response Frame: 80 RTC 00 01 RIID RQID RCID PLD
  373. + */
  374. +
  375. +struct surfacegen5_frame_ctrl {
  376. + u8 type;
  377. + u8 len; // without crc
  378. + u8 pad;
  379. + u8 seq;
  380. +} __packed;
  381. +
  382. +struct surfacegen5_frame_cmd {
  383. + u8 type;
  384. + u8 tc;
  385. + u8 unknown1;
  386. + u8 unknown2;
  387. + u8 iid;
  388. + u8 rqid_lo; // id for request/response matching (low byte)
  389. + u8 rqid_hi; // id for request/response matching (high byte)
  390. + u8 cid;
  391. +} __packed;
  392. +
  393. +
  394. +enum surfacegen5_ec_state {
  395. + SG5_EC_UNINITIALIZED,
  396. + SG5_EC_INITIALIZED,
  397. + SG5_EC_SUSPENDED,
  398. +};
  399. +
  400. +struct surfacegen5_ec_counters {
  401. + u8 seq; // control sequence id
  402. + u16 rqid; // id for request/response matching
  403. +};
  404. +
  405. +struct surfacegen5_ec_writer {
  406. + u8 *data;
  407. + u8 *ptr;
  408. +} __packed;
  409. +
  410. +enum surfacegen5_ec_receiver_state {
  411. + SG5_RCV_DISCARD,
  412. + SG5_RCV_CONTROL,
  413. + SG5_RCV_COMMAND,
  414. +};
  415. +
  416. +struct surfacegen5_ec_receiver {
  417. + spinlock_t lock;
  418. + enum surfacegen5_ec_receiver_state state;
  419. + struct completion signal;
  420. + struct kfifo fifo;
  421. + struct {
  422. + bool pld;
  423. + u8 seq;
  424. + u16 rqid;
  425. + } expect;
  426. + struct {
  427. + u16 cap;
  428. + u16 len;
  429. + u8 *ptr;
  430. + } eval_buf;
  431. +};
  432. +
  433. +struct surfacegen5_ec_event_handler {
  434. + surfacegen5_ec_event_handler_fn handler;
  435. + surfacegen5_ec_event_handler_delay delay;
  436. + void *data;
  437. +};
  438. +
  439. +struct surfacegen5_ec_events {
  440. + spinlock_t lock;
  441. + struct workqueue_struct *queue_ack;
  442. + struct workqueue_struct *queue_evt;
  443. + struct surfacegen5_ec_event_handler handler[SG5_NUM_EVENT_TYPES];
  444. +};
  445. +
  446. +struct surfacegen5_ec {
  447. + struct mutex lock;
  448. + enum surfacegen5_ec_state state;
  449. + struct serdev_device *serdev;
  450. + struct surfacegen5_ec_counters counter;
  451. + struct surfacegen5_ec_writer writer;
  452. + struct surfacegen5_ec_receiver receiver;
  453. + struct surfacegen5_ec_events events;
  454. +};
  455. +
  456. +struct surfacegen5_fifo_packet {
  457. + u8 type; // packet type (ACK/RETRY/CMD)
  458. + u8 seq;
  459. + u8 len;
  460. +};
  461. +
  462. +struct surfacegen5_event_work {
  463. + refcount_t refcount;
  464. + struct surfacegen5_ec *ec;
  465. + struct work_struct work_ack;
  466. + struct delayed_work work_evt;
  467. + struct surfacegen5_event event;
  468. + u8 seq;
  469. +};
  470. +
  471. +
  472. +static struct surfacegen5_ec surfacegen5_ec = {
  473. + .lock = __MUTEX_INITIALIZER(surfacegen5_ec.lock),
  474. + .state = SG5_EC_UNINITIALIZED,
  475. + .serdev = NULL,
  476. + .counter = {
  477. + .seq = 0,
  478. + .rqid = 0,
  479. + },
  480. + .writer = {
  481. + .data = NULL,
  482. + .ptr = NULL,
  483. + },
  484. + .receiver = {
  485. + .lock = __SPIN_LOCK_UNLOCKED(),
  486. + .state = SG5_RCV_DISCARD,
  487. + .expect = {},
  488. + },
  489. + .events = {
  490. + .lock = __SPIN_LOCK_UNLOCKED(),
  491. + .handler = {},
  492. + }
  493. +};
  494. +
  495. +
  496. +static int surfacegen5_ec_rqst_unlocked(struct surfacegen5_ec *ec,
  497. + const struct surfacegen5_rqst *rqst,
  498. + struct surfacegen5_buf *result);
  499. +
  500. +
  501. +inline static struct surfacegen5_ec *surfacegen5_ec_acquire(void)
  502. +{
  503. + struct surfacegen5_ec *ec = &surfacegen5_ec;
  504. +
  505. + mutex_lock(&ec->lock);
  506. + return ec;
  507. +}
  508. +
  509. +inline static void surfacegen5_ec_release(struct surfacegen5_ec *ec)
  510. +{
  511. + mutex_unlock(&ec->lock);
  512. +}
  513. +
  514. +inline static struct surfacegen5_ec *surfacegen5_ec_acquire_init(void)
  515. +{
  516. + struct surfacegen5_ec *ec = surfacegen5_ec_acquire();
  517. +
  518. + if (ec->state == SG5_EC_UNINITIALIZED) {
  519. + surfacegen5_ec_release(ec);
  520. + return NULL;
  521. + }
  522. +
  523. + return ec;
  524. +}
  525. +
  526. +int surfacegen5_ec_consumer_register(struct device *consumer)
  527. +{
  528. + u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;
  529. + struct surfacegen5_ec *ec;
  530. + struct device_link *link;
  531. +
  532. + ec = surfacegen5_ec_acquire_init();
  533. + if (!ec) {
  534. + return -ENXIO;
  535. + }
  536. +
  537. + link = device_link_add(consumer, &ec->serdev->dev, flags);
  538. + if (!link) {
  539. + return -EFAULT;
  540. + }
  541. +
  542. + surfacegen5_ec_release(ec);
  543. + return 0;
  544. +}
  545. +
  546. +
  547. +inline static u16 surfacegen5_rqid_to_rqst(u16 rqid) {
  548. + return rqid << SURFACEGEN5_RQID_EVENT_BITS;
  549. +}
  550. +
  551. +inline static bool surfacegen5_rqid_is_event(u16 rqid) {
  552. + const u16 mask = (1 << SURFACEGEN5_RQID_EVENT_BITS) - 1;
  553. + return rqid != 0 && (rqid | mask) == mask;
  554. +}
  555. +
  556. +int surfacegen5_ec_enable_event_source(u8 tc, u8 unknown, u16 rqid)
  557. +{
  558. + struct surfacegen5_ec *ec;
  559. +
  560. + u8 pld[4] = { tc, unknown, rqid & 0xff, rqid >> 8 };
  561. + u8 buf[1] = { 0x00 };
  562. +
  563. + struct surfacegen5_rqst rqst = {
  564. + .tc = 0x01,
  565. + .iid = 0x00,
  566. + .cid = 0x0b,
  567. + .snc = 0x01,
  568. + .cdl = 0x04,
  569. + .pld = pld,
  570. + };
  571. +
  572. + struct surfacegen5_buf result = {
  573. + result.cap = ARRAY_SIZE(buf),
  574. + result.len = 0,
  575. + result.data = buf,
  576. + };
  577. +
  578. + int status;
  579. +
  580. + // only allow RQIDs that lie within event spectrum
  581. + if (!surfacegen5_rqid_is_event(rqid)) {
  582. + return -EINVAL;
  583. + }
  584. +
  585. + ec = surfacegen5_ec_acquire_init();
  586. + if (!ec) {
  587. + printk(KERN_WARNING SG5_RQST_TAG_FULL "embedded controller is uninitialized\n");
  588. + return -ENXIO;
  589. + }
  590. +
  591. + if (ec->state == SG5_EC_SUSPENDED) {
  592. + dev_warn(&ec->serdev->dev, SG5_RQST_TAG "embedded controller is suspended\n");
  593. +
  594. + surfacegen5_ec_release(ec);
  595. + return -EPERM;
  596. + }
  597. +
  598. + status = surfacegen5_ec_rqst_unlocked(ec, &rqst, &result);
  599. +
  600. + if (buf[0] != 0x00) {
  601. + dev_warn(&ec->serdev->dev,
  602. + "unexpected result while enabling event source: 0x%02x\n",
  603. + buf[0]);
  604. + }
  605. +
  606. + surfacegen5_ec_release(ec);
  607. + return status;
  608. +
  609. +}
  610. +
  611. +int surfacegen5_ec_disable_event_source(u8 tc, u8 unknown, u16 rqid)
  612. +{
  613. + struct surfacegen5_ec *ec;
  614. +
  615. + u8 pld[4] = { tc, unknown, rqid & 0xff, rqid >> 8 };
  616. + u8 buf[1] = { 0x00 };
  617. +
  618. + struct surfacegen5_rqst rqst = {
  619. + .tc = 0x01,
  620. + .iid = 0x00,
  621. + .cid = 0x0c,
  622. + .snc = 0x01,
  623. + .cdl = 0x04,
  624. + .pld = pld,
  625. + };
  626. +
  627. + struct surfacegen5_buf result = {
  628. + result.cap = ARRAY_SIZE(buf),
  629. + result.len = 0,
  630. + result.data = buf,
  631. + };
  632. +
  633. + int status;
  634. +
  635. + // only allow RQIDs that lie within event spectrum
  636. + if (!surfacegen5_rqid_is_event(rqid)) {
  637. + return -EINVAL;
  638. + }
  639. +
  640. + ec = surfacegen5_ec_acquire_init();
  641. + if (!ec) {
  642. + printk(KERN_WARNING SG5_RQST_TAG_FULL "embedded controller is uninitialized\n");
  643. + return -ENXIO;
  644. + }
  645. +
  646. + if (ec->state == SG5_EC_SUSPENDED) {
  647. + dev_warn(&ec->serdev->dev, SG5_RQST_TAG "embedded controller is suspended\n");
  648. +
  649. + surfacegen5_ec_release(ec);
  650. + return -EPERM;
  651. + }
  652. +
  653. + status = surfacegen5_ec_rqst_unlocked(ec, &rqst, &result);
  654. +
  655. + if (buf[0] != 0x00) {
  656. + dev_warn(&ec->serdev->dev,
  657. + "unexpected result while disabling event source: 0x%02x\n",
  658. + buf[0]);
  659. + }
  660. +
  661. + surfacegen5_ec_release(ec);
  662. + return status;
  663. +}
  664. +
  665. +int surfacegen5_ec_set_delayed_event_handler(
  666. + u16 rqid, surfacegen5_ec_event_handler_fn fn,
  667. + surfacegen5_ec_event_handler_delay delay,
  668. + void *data)
  669. +{
  670. + struct surfacegen5_ec *ec;
  671. + unsigned long flags;
  672. +
  673. + if (!surfacegen5_rqid_is_event(rqid)) {
  674. + return -EINVAL;
  675. + }
  676. +
  677. + ec = surfacegen5_ec_acquire_init();
  678. + if (!ec) {
  679. + return -ENXIO;
  680. + }
  681. +
  682. + spin_lock_irqsave(&ec->events.lock, flags);
  683. +
  684. + // 0 is not a valid event RQID
  685. + ec->events.handler[rqid - 1].handler = fn;
  686. + ec->events.handler[rqid - 1].delay = delay;
  687. + ec->events.handler[rqid - 1].data = data;
  688. +
  689. + spin_unlock_irqrestore(&ec->events.lock, flags);
  690. + surfacegen5_ec_release(ec);
  691. +
  692. + return 0;
  693. +}
  694. +
  695. +int surfacegen5_ec_set_event_handler(
  696. + u16 rqid, surfacegen5_ec_event_handler_fn fn, void *data)
  697. +{
  698. + return surfacegen5_ec_set_delayed_event_handler(rqid, fn, NULL, data);
  699. +}
  700. +
  701. +int surfacegen5_ec_remove_event_handler(u16 rqid)
  702. +{
  703. + struct surfacegen5_ec *ec;
  704. + unsigned long flags;
  705. +
  706. + if (!surfacegen5_rqid_is_event(rqid)) {
  707. + return -EINVAL;
  708. + }
  709. +
  710. + ec = surfacegen5_ec_acquire_init();
  711. + if (!ec) {
  712. + return -ENXIO;
  713. + }
  714. +
  715. + spin_lock_irqsave(&ec->events.lock, flags);
  716. +
  717. + // 0 is not a valid event RQID
  718. + ec->events.handler[rqid - 1].handler = NULL;
  719. + ec->events.handler[rqid - 1].delay = NULL;
  720. + ec->events.handler[rqid - 1].data = NULL;
  721. +
  722. + spin_unlock_irqrestore(&ec->events.lock, flags);
  723. + surfacegen5_ec_release(ec);
  724. +
  725. + /*
  726. + * Make sure that the handler is not in use any more after we've
  727. + * removed it.
  728. + */
  729. + flush_workqueue(ec->events.queue_evt);
  730. +
  731. + return 0;
  732. +}
  733. +
  734. +
  735. +inline static u16 surfacegen5_ssh_crc(const u8 *buf, size_t size)
  736. +{
  737. + return crc_ccitt_false(0xffff, buf, size);
  738. +}
  739. +
  740. +inline static void surfacegen5_ssh_write_u16(struct surfacegen5_ec_writer *writer, u16 in)
  741. +{
  742. + put_unaligned_le16(in, writer->ptr);
  743. + writer->ptr += 2;
  744. +}
  745. +
  746. +inline static void surfacegen5_ssh_write_crc(struct surfacegen5_ec_writer *writer,
  747. + const u8 *buf, size_t size)
  748. +{
  749. + surfacegen5_ssh_write_u16(writer, surfacegen5_ssh_crc(buf, size));
  750. +}
  751. +
  752. +inline static void surfacegen5_ssh_write_syn(struct surfacegen5_ec_writer *writer)
  753. +{
  754. + u8 *w = writer->ptr;
  755. +
  756. + *w++ = 0xaa;
  757. + *w++ = 0x55;
  758. +
  759. + writer->ptr = w;
  760. +}
  761. +
  762. +inline static void surfacegen5_ssh_write_ter(struct surfacegen5_ec_writer *writer)
  763. +{
  764. + u8 *w = writer->ptr;
  765. +
  766. + *w++ = 0xff;
  767. + *w++ = 0xff;
  768. +
  769. + writer->ptr = w;
  770. +}
  771. +
  772. +inline static void surfacegen5_ssh_write_buf(struct surfacegen5_ec_writer *writer,
  773. + u8 *in, size_t len)
  774. +{
  775. + writer->ptr = memcpy(writer->ptr, in, len) + len;
  776. +}
  777. +
  778. +inline static void surfacegen5_ssh_write_hdr(struct surfacegen5_ec_writer *writer,
  779. + const struct surfacegen5_rqst *rqst,
  780. + struct surfacegen5_ec *ec)
  781. +{
  782. + struct surfacegen5_frame_ctrl *hdr = (struct surfacegen5_frame_ctrl *)writer->ptr;
  783. + u8 *begin = writer->ptr;
  784. +
  785. + hdr->type = SG5_FRAME_TYPE_CMD;
  786. + hdr->len = SG5_BYTELEN_CMDFRAME + rqst->cdl; // without CRC
  787. + hdr->pad = 0x00;
  788. + hdr->seq = ec->counter.seq;
  789. +
  790. + writer->ptr += sizeof(*hdr);
  791. +
  792. + surfacegen5_ssh_write_crc(writer, begin, writer->ptr - begin);
  793. +}
  794. +
  795. +inline static void surfacegen5_ssh_write_cmd(struct surfacegen5_ec_writer *writer,
  796. + const struct surfacegen5_rqst *rqst,
  797. + struct surfacegen5_ec *ec)
  798. +{
  799. + struct surfacegen5_frame_cmd *cmd = (struct surfacegen5_frame_cmd *)writer->ptr;
  800. + u8 *begin = writer->ptr;
  801. +
  802. + u16 rqid = surfacegen5_rqid_to_rqst(ec->counter.rqid);
  803. + u8 rqid_lo = rqid & 0xFF;
  804. + u8 rqid_hi = rqid >> 8;
  805. +
  806. + cmd->type = SG5_FRAME_TYPE_CMD;
  807. + cmd->tc = rqst->tc;
  808. + cmd->unknown1 = 0x01;
  809. + cmd->unknown2 = 0x00;
  810. + cmd->iid = rqst->iid;
  811. + cmd->rqid_lo = rqid_lo;
  812. + cmd->rqid_hi = rqid_hi;
  813. + cmd->cid = rqst->cid;
  814. +
  815. + writer->ptr += sizeof(*cmd);
  816. +
  817. + surfacegen5_ssh_write_buf(writer, rqst->pld, rqst->cdl);
  818. + surfacegen5_ssh_write_crc(writer, begin, writer->ptr - begin);
  819. +}
  820. +
  821. +inline static void surfacegen5_ssh_write_ack(struct surfacegen5_ec_writer *writer, u8 seq)
  822. +{
  823. + struct surfacegen5_frame_ctrl *ack = (struct surfacegen5_frame_ctrl *)writer->ptr;
  824. + u8 *begin = writer->ptr;
  825. +
  826. + ack->type = SG5_FRAME_TYPE_ACK;
  827. + ack->len = 0x00;
  828. + ack->pad = 0x00;
  829. + ack->seq = seq;
  830. +
  831. + writer->ptr += sizeof(*ack);
  832. +
  833. + surfacegen5_ssh_write_crc(writer, begin, writer->ptr - begin);
  834. +}
  835. +
  836. +inline static void surfacegen5_ssh_writer_reset(struct surfacegen5_ec_writer *writer)
  837. +{
  838. + writer->ptr = writer->data;
  839. +}
  840. +
  841. +inline static int surfacegen5_ssh_writer_flush(struct surfacegen5_ec *ec)
  842. +{
  843. + struct surfacegen5_ec_writer *writer = &ec->writer;
  844. + struct serdev_device *serdev = ec->serdev;
  845. + int status;
  846. +
  847. + size_t len = writer->ptr - writer->data;
  848. +
  849. + dev_dbg(&ec->serdev->dev, "sending message\n");
  850. + print_hex_dump_debug("send: ", DUMP_PREFIX_OFFSET, 16, 1,
  851. + writer->data, writer->ptr - writer->data, false);
  852. +
  853. + status = serdev_device_write(serdev, writer->data, len, SG5_WRITE_TIMEOUT);
  854. + return status >= 0 ? 0 : status;
  855. +}
  856. +
  857. +inline static void surfacegen5_ssh_write_msg_cmd(struct surfacegen5_ec *ec,
  858. + const struct surfacegen5_rqst *rqst)
  859. +{
  860. + surfacegen5_ssh_writer_reset(&ec->writer);
  861. + surfacegen5_ssh_write_syn(&ec->writer);
  862. + surfacegen5_ssh_write_hdr(&ec->writer, rqst, ec);
  863. + surfacegen5_ssh_write_cmd(&ec->writer, rqst, ec);
  864. +}
  865. +
  866. +inline static void surfacegen5_ssh_write_msg_ack(struct surfacegen5_ec *ec, u8 seq)
  867. +{
  868. + surfacegen5_ssh_writer_reset(&ec->writer);
  869. + surfacegen5_ssh_write_syn(&ec->writer);
  870. + surfacegen5_ssh_write_ack(&ec->writer, seq);
  871. + surfacegen5_ssh_write_ter(&ec->writer);
  872. +}
  873. +
  874. +inline static void surfacegen5_ssh_receiver_restart(struct surfacegen5_ec *ec,
  875. + const struct surfacegen5_rqst *rqst)
  876. +{
  877. + unsigned long flags;
  878. +
  879. + spin_lock_irqsave(&ec->receiver.lock, flags);
  880. + reinit_completion(&ec->receiver.signal);
  881. + ec->receiver.state = SG5_RCV_CONTROL;
  882. + ec->receiver.expect.pld = rqst->snc;
  883. + ec->receiver.expect.seq = ec->counter.seq;
  884. + ec->receiver.expect.rqid = surfacegen5_rqid_to_rqst(ec->counter.rqid);
  885. + ec->receiver.eval_buf.len = 0;
  886. + spin_unlock_irqrestore(&ec->receiver.lock, flags);
  887. +}
  888. +
  889. +inline static void surfacegen5_ssh_receiver_discard(struct surfacegen5_ec *ec)
  890. +{
  891. + unsigned long flags;
  892. +
  893. + spin_lock_irqsave(&ec->receiver.lock, flags);
  894. + ec->receiver.state = SG5_RCV_DISCARD;
  895. + ec->receiver.eval_buf.len = 0;
  896. + kfifo_reset(&ec->receiver.fifo);
  897. + spin_unlock_irqrestore(&ec->receiver.lock, flags);
  898. +}
  899. +
  900. +static int surfacegen5_ec_rqst_unlocked(struct surfacegen5_ec *ec,
  901. + const struct surfacegen5_rqst *rqst,
  902. + struct surfacegen5_buf *result)
  903. +{
  904. + struct device *dev = &ec->serdev->dev;
  905. + struct surfacegen5_fifo_packet packet = {};
  906. + int status;
  907. + int try;
  908. + unsigned int rem;
  909. +
  910. + if (rqst->cdl > SURFACEGEN5_MAX_RQST_PAYLOAD) {
  911. + dev_err(dev, SG5_RQST_TAG "request payload too large\n");
  912. + return -EINVAL;
  913. + }
  914. +
  915. + // write command in buffer, we may need it multiple times
  916. + surfacegen5_ssh_write_msg_cmd(ec, rqst);
  917. + surfacegen5_ssh_receiver_restart(ec, rqst);
  918. +
  919. + // send command, try to get an ack response
  920. + for (try = 0; try < SG5_NUM_RETRY; try++) {
  921. + status = surfacegen5_ssh_writer_flush(ec);
  922. + if (status) {
  923. + goto ec_rqst_out;
  924. + }
  925. +
  926. + rem = wait_for_completion_timeout(&ec->receiver.signal, SG5_READ_TIMEOUT);
  927. + if (rem) {
  928. + // completion assures valid packet, thus ignore returned length
  929. + (void) !kfifo_out(&ec->receiver.fifo, &packet, sizeof(packet));
  930. +
  931. + if (packet.type == SG5_FRAME_TYPE_ACK) {
  932. + break;
  933. + }
  934. + }
  935. + }
  936. +
  937. + // check if we ran out of tries?
  938. + if (try >= SG5_NUM_RETRY) {
  939. + dev_err(dev, SG5_RQST_TAG "communication failed %d times, giving up\n", try);
  940. + status = -EIO;
  941. + goto ec_rqst_out;
  942. + }
  943. +
  944. + ec->counter.seq += 1;
  945. + ec->counter.rqid += 1;
  946. +
  947. + // get command response/payload
  948. + if (rqst->snc && result) {
  949. + rem = wait_for_completion_timeout(&ec->receiver.signal, SG5_READ_TIMEOUT);
  950. + if (rem) {
  951. + // completion assures valid packet, thus ignore returned length
  952. + (void) !kfifo_out(&ec->receiver.fifo, &packet, sizeof(packet));
  953. +
  954. + if (result->cap < packet.len) {
  955. + status = -EINVAL;
  956. + goto ec_rqst_out;
  957. + }
  958. +
  959. + // completion assures valid packet, thus ignore returned length
  960. + (void) !kfifo_out(&ec->receiver.fifo, result->data, packet.len);
  961. + result->len = packet.len;
  962. + } else {
  963. + dev_err(dev, SG5_RQST_TAG "communication timed out\n");
  964. + status = -EIO;
  965. + goto ec_rqst_out;
  966. + }
  967. +
  968. + // send ACK
  969. + surfacegen5_ssh_write_msg_ack(ec, packet.seq);
  970. + status = surfacegen5_ssh_writer_flush(ec);
  971. + if (status) {
  972. + goto ec_rqst_out;
  973. + }
  974. + }
  975. +
  976. +ec_rqst_out:
  977. + surfacegen5_ssh_receiver_discard(ec);
  978. + return status;
  979. +}
  980. +
  981. +int surfacegen5_ec_rqst(const struct surfacegen5_rqst *rqst, struct surfacegen5_buf *result)
  982. +{
  983. + struct surfacegen5_ec *ec;
  984. + int status;
  985. +
  986. + ec = surfacegen5_ec_acquire_init();
  987. + if (!ec) {
  988. + printk(KERN_WARNING SG5_RQST_TAG_FULL "embedded controller is uninitialized\n");
  989. + return -ENXIO;
  990. + }
  991. +
  992. + if (ec->state == SG5_EC_SUSPENDED) {
  993. + dev_warn(&ec->serdev->dev, SG5_RQST_TAG "embedded controller is suspended\n");
  994. +
  995. + surfacegen5_ec_release(ec);
  996. + return -EPERM;
  997. + }
  998. +
  999. + status = surfacegen5_ec_rqst_unlocked(ec, rqst, result);
  1000. +
  1001. + surfacegen5_ec_release(ec);
  1002. + return status;
  1003. +}
  1004. +
  1005. +
  1006. +static int surfacegen5_ssh_ec_resume(struct surfacegen5_ec *ec)
  1007. +{
  1008. + u8 buf[1] = { 0x00 };
  1009. +
  1010. + struct surfacegen5_rqst rqst = {
  1011. + .tc = 0x01,
  1012. + .iid = 0x00,
  1013. + .cid = 0x16,
  1014. + .snc = 0x01,
  1015. + .cdl = 0x00,
  1016. + .pld = NULL,
  1017. + };
  1018. +
  1019. + struct surfacegen5_buf result = {
  1020. + result.cap = ARRAY_SIZE(buf),
  1021. + result.len = 0,
  1022. + result.data = buf,
  1023. + };
  1024. +
  1025. + int status = surfacegen5_ec_rqst_unlocked(ec, &rqst, &result);
  1026. + if (status) {
  1027. + return status;
  1028. + }
  1029. +
  1030. + if (buf[0] != 0x00) {
  1031. + dev_warn(&ec->serdev->dev,
  1032. + "unexpected result while trying to resume EC: 0x%02x\n",
  1033. + buf[0]);
  1034. + }
  1035. +
  1036. + return 0;
  1037. +}
  1038. +
  1039. +static int surfacegen5_ssh_ec_suspend(struct surfacegen5_ec *ec)
  1040. +{
  1041. + u8 buf[1] = { 0x00 };
  1042. +
  1043. + struct surfacegen5_rqst rqst = {
  1044. + .tc = 0x01,
  1045. + .iid = 0x00,
  1046. + .cid = 0x15,
  1047. + .snc = 0x01,
  1048. + .cdl = 0x00,
  1049. + .pld = NULL,
  1050. + };
  1051. +
  1052. + struct surfacegen5_buf result = {
  1053. + result.cap = ARRAY_SIZE(buf),
  1054. + result.len = 0,
  1055. + result.data = buf,
  1056. + };
  1057. +
  1058. + int status = surfacegen5_ec_rqst_unlocked(ec, &rqst, &result);
  1059. + if (status) {
  1060. + return status;
  1061. + }
  1062. +
  1063. + if (buf[0] != 0x00) {
  1064. + dev_warn(&ec->serdev->dev,
  1065. + "unexpected result while trying to suspend EC: 0x%02x\n",
  1066. + buf[0]);
  1067. + }
  1068. +
  1069. + return 0;
  1070. +}
  1071. +
  1072. +
  1073. +inline static bool surfacegen5_ssh_is_valid_syn(const u8 *ptr)
  1074. +{
  1075. + return ptr[0] == 0xaa && ptr[1] == 0x55;
  1076. +}
  1077. +
  1078. +inline static bool surfacegen5_ssh_is_valid_ter(const u8 *ptr)
  1079. +{
  1080. + return ptr[0] == 0xff && ptr[1] == 0xff;
  1081. +}
  1082. +
  1083. +inline static bool surfacegen5_ssh_is_valid_crc(const u8 *begin, const u8 *end)
  1084. +{
  1085. + u16 crc = surfacegen5_ssh_crc(begin, end - begin);
  1086. + return (end[0] == (crc & 0xff)) && (end[1] == (crc >> 8));
  1087. +}
  1088. +
  1089. +
  1090. +static int surfacegen5_ssh_send_ack(struct surfacegen5_ec *ec, u8 seq)
  1091. +{
  1092. + int status;
  1093. + u8 buf[SG5_MSG_LEN_CTRL];
  1094. + u16 crc;
  1095. +
  1096. + buf[0] = 0xaa;
  1097. + buf[1] = 0x55;
  1098. + buf[2] = 0x40;
  1099. + buf[3] = 0x00;
  1100. + buf[4] = 0x00;
  1101. + buf[5] = seq;
  1102. +
  1103. + crc = surfacegen5_ssh_crc(buf + SG5_FRAME_OFFS_CTRL, SG5_BYTELEN_CTRL);
  1104. + buf[6] = crc & 0xff;
  1105. + buf[7] = crc >> 8;
  1106. +
  1107. + buf[8] = 0xff;
  1108. + buf[9] = 0xff;
  1109. +
  1110. + dev_dbg(&ec->serdev->dev, "sending message\n");
  1111. + print_hex_dump_debug("send: ", DUMP_PREFIX_OFFSET, 16, 1,
  1112. + buf, SG5_MSG_LEN_CTRL, false);
  1113. +
  1114. + status = serdev_device_write(ec->serdev, buf, SG5_MSG_LEN_CTRL, SG5_WRITE_TIMEOUT);
  1115. + return status >= 0 ? 0 : status;
  1116. +}
  1117. +
  1118. +static void surfacegen5_event_work_ack_handler(struct work_struct *_work)
  1119. +{
  1120. + struct surfacegen5_event_work *work;
  1121. + struct surfacegen5_event *event;
  1122. + struct surfacegen5_ec *ec;
  1123. + struct device *dev;
  1124. + int status;
  1125. +
  1126. + work = container_of(_work, struct surfacegen5_event_work, work_ack);
  1127. + event = &work->event;
  1128. + ec = work->ec;
  1129. + dev = &ec->serdev->dev;
  1130. +
  1131. + // make sure we load a fresh ec state
  1132. + smp_mb();
  1133. +
  1134. + if (ec->state == SG5_EC_INITIALIZED) {
  1135. + status = surfacegen5_ssh_send_ack(ec, work->seq);
  1136. + if (status) {
  1137. + dev_err(dev, SG5_EVENT_TAG "failed to send ACK: %d\n", status);
  1138. + }
  1139. + }
  1140. +
  1141. + if (refcount_dec_and_test(&work->refcount)) {
  1142. + kfree(work);
  1143. + }
  1144. +}
  1145. +
  1146. +static void surfacegen5_event_work_evt_handler(struct work_struct *_work)
  1147. +{
  1148. + struct delayed_work *dwork = (struct delayed_work *)_work;
  1149. + struct surfacegen5_event_work *work;
  1150. + struct surfacegen5_event *event;
  1151. + struct surfacegen5_ec *ec;
  1152. + struct device *dev;
  1153. + unsigned long flags;
  1154. +
  1155. + surfacegen5_ec_event_handler_fn handler;
  1156. + void *handler_data;
  1157. +
  1158. + int status = 0;
  1159. +
  1160. + work = container_of(dwork, struct surfacegen5_event_work, work_evt);
  1161. + event = &work->event;
  1162. + ec = work->ec;
  1163. + dev = &ec->serdev->dev;
  1164. +
  1165. + spin_lock_irqsave(&ec->events.lock, flags);
  1166. + handler = ec->events.handler[event->rqid - 1].handler;
  1167. + handler_data = ec->events.handler[event->rqid - 1].data;
  1168. + spin_unlock_irqrestore(&ec->events.lock, flags);
  1169. +
  1170. + /*
  1171. + * During handler removal or driver release, we ensure every event gets
  1172. + * handled before return of that function. Thus a handler obtained here is
  1173. + * guaranteed to be valid at least until this function returns.
  1174. + */
  1175. +
  1176. + if (handler) {
  1177. + status = handler(event, handler_data);
  1178. + } else {
  1179. + dev_warn(dev, SG5_EVENT_TAG "unhandled event (rqid: %04x)\n", event->rqid);
  1180. + }
  1181. +
  1182. + if (status) {
  1183. + dev_err(dev, SG5_EVENT_TAG "error handling event: %d\n", status);
  1184. + }
  1185. +
  1186. + if (refcount_dec_and_test(&work->refcount)) {
  1187. + kfree(work);
  1188. + }
  1189. +}
  1190. +
  1191. +static void surfacegen5_ssh_handle_event(struct surfacegen5_ec *ec, const u8 *buf)
  1192. +{
  1193. + struct device *dev = &ec->serdev->dev;
  1194. + const struct surfacegen5_frame_ctrl *ctrl;
  1195. + const struct surfacegen5_frame_cmd *cmd;
  1196. + struct surfacegen5_event_work *work;
  1197. + unsigned long flags;
  1198. + u16 pld_len;
  1199. +
  1200. + surfacegen5_ec_event_handler_delay delay_fn;
  1201. + void *handler_data;
  1202. + unsigned long delay = 0;
  1203. +
  1204. + ctrl = (const struct surfacegen5_frame_ctrl *)(buf + SG5_FRAME_OFFS_CTRL);
  1205. + cmd = (const struct surfacegen5_frame_cmd *)(buf + SG5_FRAME_OFFS_CMD);
  1206. +
  1207. + pld_len = ctrl->len - SG5_BYTELEN_CMDFRAME;
  1208. +
  1209. + work = kzalloc(sizeof(struct surfacegen5_event_work) + pld_len, GFP_ATOMIC);
  1210. + if (!work) {
  1211. + dev_warn(dev, SG5_EVENT_TAG "failed to allocate memory, dropping event\n");
  1212. + return;
  1213. + }
  1214. +
  1215. + refcount_set(&work->refcount, 2);
  1216. + work->ec = ec;
  1217. + work->seq = ctrl->seq;
  1218. + work->event.rqid = (cmd->rqid_hi << 8) | cmd->rqid_lo;
  1219. + work->event.tc = cmd->tc;
  1220. + work->event.iid = cmd->iid;
  1221. + work->event.cid = cmd->cid;
  1222. + work->event.len = pld_len;
  1223. + work->event.pld = ((u8*) work) + sizeof(struct surfacegen5_event_work);
  1224. +
  1225. + memcpy(work->event.pld, buf + SG5_FRAME_OFFS_CMD_PLD, pld_len);
  1226. +
  1227. + INIT_WORK(&work->work_ack, surfacegen5_event_work_ack_handler);
  1228. + queue_work(ec->events.queue_ack, &work->work_ack);
  1229. +
  1230. + spin_lock_irqsave(&ec->events.lock, flags);
  1231. + handler_data = ec->events.handler[work->event.rqid - 1].data;
  1232. + delay_fn = ec->events.handler[work->event.rqid - 1].delay;
  1233. + if (delay_fn) {
  1234. + delay = delay_fn(&work->event, handler_data);
  1235. + }
  1236. + spin_unlock_irqrestore(&ec->events.lock, flags);
  1237. +
  1238. + // immediate execution for high priority events (e.g. keyboard)
  1239. + if (delay == SURFACEGEN5_EVENT_IMMEDIATE) {
  1240. + surfacegen5_event_work_evt_handler(&work->work_evt.work);
  1241. + } else {
  1242. + INIT_DELAYED_WORK(&work->work_evt, surfacegen5_event_work_evt_handler);
  1243. + queue_delayed_work(ec->events.queue_evt, &work->work_evt, delay);
  1244. + }
  1245. +}
  1246. +
  1247. +static int surfacegen5_ssh_receive_msg_ctrl(struct surfacegen5_ec *ec,
  1248. + const u8 *buf, size_t size)
  1249. +{
  1250. + struct device *dev = &ec->serdev->dev;
  1251. + struct surfacegen5_ec_receiver *rcv = &ec->receiver;
  1252. + const struct surfacegen5_frame_ctrl *ctrl;
  1253. + struct surfacegen5_fifo_packet packet;
  1254. +
  1255. + const u8 *ctrl_begin = buf + SG5_FRAME_OFFS_CTRL;
  1256. + const u8 *ctrl_end = buf + SG5_FRAME_OFFS_CTRL_CRC;
  1257. +
  1258. + ctrl = (const struct surfacegen5_frame_ctrl *)(ctrl_begin);
  1259. +
  1260. + // actual length check
  1261. + if (size < SG5_MSG_LEN_CTRL) {
  1262. + return 0; // need more bytes
  1263. + }
  1264. +
  1265. + // validate TERM
  1266. + if (!surfacegen5_ssh_is_valid_ter(buf + SG5_FRAME_OFFS_TERM)) {
  1267. + dev_err(dev, SG5_RECV_TAG "invalid end of message\n");
  1268. + return size; // discard everything
  1269. + }
  1270. +
  1271. + // validate CRC
  1272. + if (!surfacegen5_ssh_is_valid_crc(ctrl_begin, ctrl_end)) {
  1273. + dev_err(dev, SG5_RECV_TAG "invalid checksum (ctrl)\n");
  1274. + return SG5_MSG_LEN_CTRL; // only discard message
  1275. + }
  1276. +
  1277. + // check if we expect the message
  1278. + if (rcv->state != SG5_RCV_CONTROL) {
  1279. + dev_err(dev, SG5_RECV_TAG "discarding message: ctrl not expected\n");
  1280. + return SG5_MSG_LEN_CTRL; // discard message
  1281. + }
  1282. +
  1283. + // check if it is for our request
  1284. + if (ctrl->type == SG5_FRAME_TYPE_ACK && ctrl->seq != rcv->expect.seq) {
  1285. + dev_err(dev, SG5_RECV_TAG "discarding message: ack does not match\n");
  1286. + return SG5_MSG_LEN_CTRL; // discard message
  1287. + }
  1288. +
  1289. + // we now have a valid & expected ACK/RETRY message
  1290. + dev_dbg(dev, SG5_RECV_TAG "valid control message received (type: 0x%02x)\n", ctrl->type);
  1291. +
  1292. + packet.type = ctrl->type;
  1293. + packet.seq = ctrl->seq;
  1294. + packet.len = 0;
  1295. +
  1296. + if (kfifo_avail(&rcv->fifo) >= sizeof(packet)) {
  1297. + kfifo_in(&rcv->fifo, (u8 *) &packet, sizeof(packet));
  1298. +
  1299. + } else {
  1300. + dev_warn(dev, SG5_RECV_TAG
  1301. + "dropping frame: not enough space in fifo (type = %d)\n",
  1302. + SG5_FRAME_TYPE_CMD);
  1303. +
  1304. + return SG5_MSG_LEN_CTRL; // discard message
  1305. + }
  1306. +
  1307. + // update decoder state
  1308. + if (ctrl->type == SG5_FRAME_TYPE_ACK) {
  1309. + rcv->state = rcv->expect.pld
  1310. + ? SG5_RCV_COMMAND
  1311. + : SG5_RCV_DISCARD;
  1312. + }
  1313. +
  1314. + complete(&rcv->signal);
  1315. + return SG5_MSG_LEN_CTRL; // handled message
  1316. +}
  1317. +
  1318. +static int surfacegen5_ssh_receive_msg_cmd(struct surfacegen5_ec *ec,
  1319. + const u8 *buf, size_t size)
  1320. +{
  1321. + struct device *dev = &ec->serdev->dev;
  1322. + struct surfacegen5_ec_receiver *rcv = &ec->receiver;
  1323. + const struct surfacegen5_frame_ctrl *ctrl;
  1324. + const struct surfacegen5_frame_cmd *cmd;
  1325. + struct surfacegen5_fifo_packet packet;
  1326. +
  1327. + const u8 *ctrl_begin = buf + SG5_FRAME_OFFS_CTRL;
  1328. + const u8 *ctrl_end = buf + SG5_FRAME_OFFS_CTRL_CRC;
  1329. + const u8 *cmd_begin = buf + SG5_FRAME_OFFS_CMD;
  1330. + const u8 *cmd_begin_pld = buf + SG5_FRAME_OFFS_CMD_PLD;
  1331. + const u8 *cmd_end;
  1332. +
  1333. + size_t msg_len;
  1334. +
  1335. + ctrl = (const struct surfacegen5_frame_ctrl *)(ctrl_begin);
  1336. + cmd = (const struct surfacegen5_frame_cmd *)(cmd_begin);
  1337. +
  1338. + // we need at least a full control frame
  1339. + if (size < (SG5_BYTELEN_SYNC + SG5_BYTELEN_CTRL + SG5_BYTELEN_CRC)) {
  1340. + return 0; // need more bytes
  1341. + }
  1342. +
  1343. + // validate control-frame CRC
  1344. + if (!surfacegen5_ssh_is_valid_crc(ctrl_begin, ctrl_end)) {
  1345. + dev_err(dev, SG5_RECV_TAG "invalid checksum (cmd-ctrl)\n");
  1346. + /*
  1347. + * We can't be sure here if length is valid, thus
  1348. + * discard everything.
  1349. + */
  1350. + return size;
  1351. + }
  1352. +
  1353. + // actual length check (ctrl->len contains command-frame but not crc)
  1354. + msg_len = SG5_MSG_LEN_CMD_BASE + ctrl->len;
  1355. + if (size < msg_len) {
  1356. + return 0; // need more bytes
  1357. + }
  1358. +
  1359. + cmd_end = cmd_begin + ctrl->len;
  1360. +
  1361. + // validate command-frame type
  1362. + if (cmd->type != SG5_FRAME_TYPE_CMD) {
  1363. + dev_err(dev, SG5_RECV_TAG "expected command frame type but got 0x%02x\n", cmd->type);
  1364. + return size; // discard everything
  1365. + }
  1366. +
  1367. + // validate command-frame CRC
  1368. + if (!surfacegen5_ssh_is_valid_crc(cmd_begin, cmd_end)) {
  1369. + dev_err(dev, SG5_RECV_TAG "invalid checksum (cmd-pld)\n");
  1370. +
  1371. + /*
  1372. + * The message length is provided in the control frame. As we
  1373. + * already validated that, we can be sure here that it's
  1374. + * correct, so we only need to discard the message.
  1375. + */
  1376. + return msg_len;
  1377. + }
  1378. +
  1379. + // check if we received an event notification
  1380. + if (surfacegen5_rqid_is_event((cmd->rqid_hi << 8) | cmd->rqid_lo)) {
  1381. + surfacegen5_ssh_handle_event(ec, buf);
  1382. + return msg_len; // handled message
  1383. + }
  1384. +
  1385. + // check if we expect the message
  1386. + if (rcv->state != SG5_RCV_COMMAND) {
  1387. + dev_dbg(dev, SG5_RECV_TAG "discarding message: command not expected\n");
  1388. + return msg_len; // discard message
  1389. + }
  1390. +
  1391. + // check if response is for our request
  1392. + if (rcv->expect.rqid != (cmd->rqid_lo | (cmd->rqid_hi << 8))) {
  1393. + dev_dbg(dev, SG5_RECV_TAG "discarding message: command not a match\n");
  1394. + return msg_len; // discard message
  1395. + }
  1396. +
  1397. + // we now have a valid & expected command message
  1398. + dev_dbg(dev, SG5_RECV_TAG "valid command message received\n");
  1399. +
  1400. + packet.type = ctrl->type;
  1401. + packet.seq = ctrl->seq;
  1402. + packet.len = cmd_end - cmd_begin_pld;
  1403. +
  1404. + if (kfifo_avail(&rcv->fifo) >= sizeof(packet) + packet.len) {
  1405. + kfifo_in(&rcv->fifo, &packet, sizeof(packet));
  1406. + kfifo_in(&rcv->fifo, cmd_begin_pld, packet.len);
  1407. +
  1408. + } else {
  1409. + dev_warn(dev, SG5_RECV_TAG
  1410. + "dropping frame: not enough space in fifo (type = %d)\n",
  1411. + SG5_FRAME_TYPE_CMD);
  1412. +
  1413. + return SG5_MSG_LEN_CTRL; // discard message
  1414. + }
  1415. +
  1416. + rcv->state = SG5_RCV_DISCARD;
  1417. +
  1418. + complete(&rcv->signal);
  1419. + return msg_len; // handled message
  1420. +}
  1421. +
  1422. +static int surfacegen5_ssh_eval_buf(struct surfacegen5_ec *ec,
  1423. + const u8 *buf, size_t size)
  1424. +{
  1425. + struct device *dev = &ec->serdev->dev;
  1426. + struct surfacegen5_frame_ctrl *ctrl;
  1427. +
  1428. + // we need at least a control frame to check what to do
  1429. + if (size < (SG5_BYTELEN_SYNC + SG5_BYTELEN_CTRL)) {
  1430. + return 0; // need more bytes
  1431. + }
  1432. +
  1433. + // make sure we're actually at the start of a new message
  1434. + if (!surfacegen5_ssh_is_valid_syn(buf)) {
  1435. + dev_err(dev, SG5_RECV_TAG "invalid start of message\n");
  1436. + return size; // discard everything
  1437. + }
  1438. +
  1439. + // handle individual message types seperately
  1440. + ctrl = (struct surfacegen5_frame_ctrl *)(buf + SG5_FRAME_OFFS_CTRL);
  1441. +
  1442. + switch (ctrl->type) {
  1443. + case SG5_FRAME_TYPE_ACK:
  1444. + case SG5_FRAME_TYPE_RETRY:
  1445. + return surfacegen5_ssh_receive_msg_ctrl(ec, buf, size);
  1446. +
  1447. + case SG5_FRAME_TYPE_CMD:
  1448. + return surfacegen5_ssh_receive_msg_cmd(ec, buf, size);
  1449. +
  1450. + default:
  1451. + dev_err(dev, SG5_RECV_TAG "unknown frame type 0x%02x\n", ctrl->type);
  1452. + return size; // discard everything
  1453. + }
  1454. +}
  1455. +
  1456. +static int surfacegen5_ssh_receive_buf(struct serdev_device *serdev,
  1457. + const unsigned char *buf, size_t size)
  1458. +{
  1459. + struct surfacegen5_ec *ec = serdev_device_get_drvdata(serdev);
  1460. + struct surfacegen5_ec_receiver *rcv = &ec->receiver;
  1461. + unsigned long flags;
  1462. + int offs = 0;
  1463. + int used, n;
  1464. +
  1465. + dev_dbg(&serdev->dev, SG5_RECV_TAG "received buffer (size: %zu)\n", size);
  1466. + print_hex_dump_debug(SG5_RECV_TAG, DUMP_PREFIX_OFFSET, 16, 1, buf, size, false);
  1467. +
  1468. + /*
  1469. + * The battery _BIX message gets a bit long, thus we have to add some
  1470. + * additional buffering here.
  1471. + */
  1472. +
  1473. + spin_lock_irqsave(&rcv->lock, flags);
  1474. +
  1475. + // copy to eval-buffer
  1476. + used = min(size, (size_t)(rcv->eval_buf.cap - rcv->eval_buf.len));
  1477. + memcpy(rcv->eval_buf.ptr + rcv->eval_buf.len, buf, used);
  1478. + rcv->eval_buf.len += used;
  1479. +
  1480. + // evaluate buffer until we need more bytes or eval-buf is empty
  1481. + while (offs < rcv->eval_buf.len) {
  1482. + n = rcv->eval_buf.len - offs;
  1483. + n = surfacegen5_ssh_eval_buf(ec, rcv->eval_buf.ptr + offs, n);
  1484. + if (n <= 0) break; // need more bytes
  1485. +
  1486. + offs += n;
  1487. + }
  1488. +
  1489. + // throw away the evaluated parts
  1490. + rcv->eval_buf.len -= offs;
  1491. + memmove(rcv->eval_buf.ptr, rcv->eval_buf.ptr + offs, rcv->eval_buf.len);
  1492. +
  1493. + spin_unlock_irqrestore(&rcv->lock, flags);
  1494. +
  1495. + return used;
  1496. +}
  1497. +
  1498. +
  1499. +static acpi_status
  1500. +surfacegen5_ssh_setup_from_resource(struct acpi_resource *resource, void *context)
  1501. +{
  1502. + struct serdev_device *serdev = context;
  1503. + struct acpi_resource_common_serialbus *serial;
  1504. + struct acpi_resource_uart_serialbus *uart;
  1505. + int status = 0;
  1506. +
  1507. + if (resource->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  1508. + return AE_OK;
  1509. + }
  1510. +
  1511. + serial = &resource->data.common_serial_bus;
  1512. + if (serial->type != ACPI_RESOURCE_SERIAL_TYPE_UART) {
  1513. + return AE_OK;
  1514. + }
  1515. +
  1516. + uart = &resource->data.uart_serial_bus;
  1517. +
  1518. + // set up serdev device
  1519. + serdev_device_set_baudrate(serdev, uart->default_baud_rate);
  1520. +
  1521. + // serdev currently only supports RTSCTS flow control
  1522. + if (uart->flow_control & SG5_SUPPORTED_FLOW_CONTROL_MASK) {
  1523. + dev_warn(&serdev->dev, "unsupported flow control (value: 0x%02x)\n", uart->flow_control);
  1524. + }
  1525. +
  1526. + // set RTSCTS flow control
  1527. + serdev_device_set_flow_control(serdev, uart->flow_control & ACPI_UART_FLOW_CONTROL_HW);
  1528. +
  1529. + // serdev currently only supports EVEN/ODD parity
  1530. + switch (uart->parity) {
  1531. + case ACPI_UART_PARITY_NONE:
  1532. + status = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
  1533. + break;
  1534. + case ACPI_UART_PARITY_EVEN:
  1535. + status = serdev_device_set_parity(serdev, SERDEV_PARITY_EVEN);
  1536. + break;
  1537. + case ACPI_UART_PARITY_ODD:
  1538. + status = serdev_device_set_parity(serdev, SERDEV_PARITY_ODD);
  1539. + break;
  1540. + default:
  1541. + dev_warn(&serdev->dev, "unsupported parity (value: 0x%02x)\n", uart->parity);
  1542. + break;
  1543. + }
  1544. +
  1545. + if (status) {
  1546. + dev_err(&serdev->dev, "failed to set parity (value: 0x%02x)\n", uart->parity);
  1547. + return status;
  1548. + }
  1549. +
  1550. + return AE_CTRL_TERMINATE; // we've found the resource and are done
  1551. +}
  1552. +
  1553. +
  1554. +static bool surfacegen5_idma_filter(struct dma_chan *chan, void *param)
  1555. +{
  1556. + // see dw8250_idma_filter
  1557. + return param == chan->device->dev;
  1558. +}
  1559. +
  1560. +static int surfacegen5_ssh_check_dma(struct serdev_device *serdev)
  1561. +{
  1562. + struct device *dev = serdev->ctrl->dev.parent;
  1563. + struct dma_chan *rx, *tx;
  1564. + dma_cap_mask_t mask;
  1565. + int status = 0;
  1566. +
  1567. + /*
  1568. + * The EC UART requires DMA for proper communication. If we don't use DMA,
  1569. + * we'll drop bytes when the system has high load, e.g. during boot. This
  1570. + * causes some ugly behaviour, i.e. battery information (_BIX) messages
  1571. + * failing frequently. We're making sure the required DMA channels are
  1572. + * available here so serial8250_do_startup is able to grab them later
  1573. + * instead of silently falling back to a non-DMA approach.
  1574. + */
  1575. +
  1576. + dma_cap_zero(mask);
  1577. + dma_cap_set(DMA_SLAVE, mask);
  1578. +
  1579. + rx = dma_request_slave_channel_compat(mask, surfacegen5_idma_filter, dev->parent, dev, "rx");
  1580. + if (IS_ERR_OR_NULL(rx)) {
  1581. + status = rx ? PTR_ERR(rx) : -EPROBE_DEFER;
  1582. + if (status != -EPROBE_DEFER) {
  1583. + dev_err(&serdev->dev, "sg5_dma: error requesting rx channel: %d\n", status);
  1584. + } else {
  1585. + dev_dbg(&serdev->dev, "sg5_dma: rx channel not found, deferring probe\n");
  1586. + }
  1587. + goto check_dma_out;
  1588. + }
  1589. +
  1590. + tx = dma_request_slave_channel_compat(mask, surfacegen5_idma_filter, dev->parent, dev, "tx");
  1591. + if (IS_ERR_OR_NULL(tx)) {
  1592. + status = tx ? PTR_ERR(tx) : -EPROBE_DEFER;
  1593. + if (status != -EPROBE_DEFER) {
  1594. + dev_err(&serdev->dev, "sg5_dma: error requesting tx channel: %d\n", status);
  1595. + } else {
  1596. + dev_dbg(&serdev->dev, "sg5_dma: tx channel not found, deferring probe\n");
  1597. + }
  1598. + goto check_dma_release_rx;
  1599. + }
  1600. +
  1601. + dma_release_channel(tx);
  1602. +check_dma_release_rx:
  1603. + dma_release_channel(rx);
  1604. +check_dma_out:
  1605. + return status;
  1606. +}
  1607. +
  1608. +
  1609. +static int surfacegen5_ssh_suspend(struct device *dev)
  1610. +{
  1611. + struct surfacegen5_ec *ec;
  1612. + int status = 0;
  1613. +
  1614. + dev_dbg(dev, "suspending\n");
  1615. +
  1616. + ec = surfacegen5_ec_acquire_init();
  1617. + if (ec) {
  1618. + status = surfacegen5_ssh_ec_suspend(ec);
  1619. + if (status) {
  1620. + dev_err(dev, "failed to suspend EC: %d\n", status);
  1621. + }
  1622. +
  1623. + ec->state = SG5_EC_SUSPENDED;
  1624. + surfacegen5_ec_release(ec);
  1625. + }
  1626. +
  1627. + return status;
  1628. +}
  1629. +
  1630. +static int surfacegen5_ssh_resume(struct device *dev)
  1631. +{
  1632. + struct surfacegen5_ec *ec;
  1633. + int status = 0;
  1634. +
  1635. + dev_dbg(dev, "resuming\n");
  1636. +
  1637. + ec = surfacegen5_ec_acquire_init();
  1638. + if (ec) {
  1639. + ec->state = SG5_EC_INITIALIZED;
  1640. +
  1641. + status = surfacegen5_ssh_ec_resume(ec);
  1642. + if (status) {
  1643. + dev_err(dev, "failed to resume EC: %d\n", status);
  1644. + }
  1645. +
  1646. + surfacegen5_ec_release(ec);
  1647. + }
  1648. +
  1649. + return status;
  1650. +}
  1651. +
  1652. +static SIMPLE_DEV_PM_OPS(surfacegen5_ssh_pm_ops, surfacegen5_ssh_suspend, surfacegen5_ssh_resume);
  1653. +
  1654. +
  1655. +static const struct serdev_device_ops surfacegen5_ssh_device_ops = {
  1656. + .receive_buf = surfacegen5_ssh_receive_buf,
  1657. + .write_wakeup = serdev_device_write_wakeup,
  1658. +};
  1659. +
  1660. +static int surfacegen5_acpi_ssh_probe(struct serdev_device *serdev)
  1661. +{
  1662. + struct surfacegen5_ec *ec;
  1663. + struct workqueue_struct *event_queue_ack;
  1664. + struct workqueue_struct *event_queue_evt;
  1665. + u8 *write_buf;
  1666. + u8 *read_buf;
  1667. + u8 *eval_buf;
  1668. + acpi_handle *ssh = ACPI_HANDLE(&serdev->dev);
  1669. + acpi_status status;
  1670. +
  1671. + dev_dbg(&serdev->dev, "probing\n");
  1672. +
  1673. + // ensure DMA is ready before we set up the device
  1674. + status = surfacegen5_ssh_check_dma(serdev);
  1675. + if (status) {
  1676. + return status;
  1677. + }
  1678. +
  1679. + // allocate buffers
  1680. + write_buf = kzalloc(SG5_WRITE_BUF_LEN, GFP_KERNEL);
  1681. + if (!write_buf) {
  1682. + status = -ENOMEM;
  1683. + goto err_probe_write_buf;
  1684. + }
  1685. +
  1686. + read_buf = kzalloc(SG5_READ_BUF_LEN, GFP_KERNEL);
  1687. + if (!read_buf) {
  1688. + status = -ENOMEM;
  1689. + goto err_probe_read_buf;
  1690. + }
  1691. +
  1692. + eval_buf = kzalloc(SG5_EVAL_BUF_LEN, GFP_KERNEL);
  1693. + if (!eval_buf) {
  1694. + status = -ENOMEM;
  1695. + goto err_probe_eval_buf;
  1696. + }
  1697. +
  1698. + event_queue_ack = create_singlethread_workqueue("sg5_ackq");
  1699. + if (!event_queue_ack) {
  1700. + status = -ENOMEM;
  1701. + goto err_probe_ackq;
  1702. + }
  1703. +
  1704. + event_queue_evt = create_workqueue("sg5_evtq");
  1705. + if (!event_queue_evt) {
  1706. + status = -ENOMEM;
  1707. + goto err_probe_evtq;
  1708. + }
  1709. +
  1710. + // set up EC
  1711. + ec = surfacegen5_ec_acquire();
  1712. + if (ec->state != SG5_EC_UNINITIALIZED) {
  1713. + dev_err(&serdev->dev, "embedded controller already initialized\n");
  1714. + surfacegen5_ec_release(ec);
  1715. +
  1716. + status = -EBUSY;
  1717. + goto err_probe_busy;
  1718. + }
  1719. +
  1720. + ec->serdev = serdev;
  1721. + ec->writer.data = write_buf;
  1722. + ec->writer.ptr = write_buf;
  1723. +
  1724. + // initialize receiver
  1725. + init_completion(&ec->receiver.signal);
  1726. + kfifo_init(&ec->receiver.fifo, read_buf, SG5_READ_BUF_LEN);
  1727. + ec->receiver.eval_buf.ptr = eval_buf;
  1728. + ec->receiver.eval_buf.cap = SG5_EVAL_BUF_LEN;
  1729. + ec->receiver.eval_buf.len = 0;
  1730. +
  1731. + // initialize event handling
  1732. + ec->events.queue_ack = event_queue_ack;
  1733. + ec->events.queue_evt = event_queue_evt;
  1734. +
  1735. + ec->state = SG5_EC_INITIALIZED;
  1736. +
  1737. + serdev_device_set_drvdata(serdev, ec);
  1738. +
  1739. + // ensure everything is properly set-up before we open the device
  1740. + smp_mb();
  1741. +
  1742. + serdev_device_set_client_ops(serdev, &surfacegen5_ssh_device_ops);
  1743. + status = serdev_device_open(serdev);
  1744. + if (status) {
  1745. + goto err_probe_open;
  1746. + }
  1747. +
  1748. + status = acpi_walk_resources(ssh, METHOD_NAME__CRS,
  1749. + surfacegen5_ssh_setup_from_resource, serdev);
  1750. + if (ACPI_FAILURE(status)) {
  1751. + goto err_probe_devinit;
  1752. + }
  1753. +
  1754. + status = surfacegen5_ssh_ec_resume(ec);
  1755. + if (status) {
  1756. + goto err_probe_devinit;
  1757. + }
  1758. +
  1759. + status = surfacegen5_ssh_sysfs_register(&serdev->dev);
  1760. + if (status) {
  1761. + goto err_probe_devinit;
  1762. + }
  1763. +
  1764. + surfacegen5_ec_release(ec);
  1765. +
  1766. + acpi_walk_dep_device_list(ssh);
  1767. +
  1768. + return 0;
  1769. +
  1770. +err_probe_devinit:
  1771. + serdev_device_close(serdev);
  1772. +err_probe_open:
  1773. + ec->state = SG5_EC_UNINITIALIZED;
  1774. + serdev_device_set_drvdata(serdev, NULL);
  1775. + surfacegen5_ec_release(ec);
  1776. +err_probe_busy:
  1777. + destroy_workqueue(event_queue_evt);
  1778. +err_probe_evtq:
  1779. + destroy_workqueue(event_queue_ack);
  1780. +err_probe_ackq:
  1781. + kfree(eval_buf);
  1782. +err_probe_eval_buf:
  1783. + kfree(read_buf);
  1784. +err_probe_read_buf:
  1785. + kfree(write_buf);
  1786. +err_probe_write_buf:
  1787. + return status;
  1788. +}
  1789. +
  1790. +static void surfacegen5_acpi_ssh_remove(struct serdev_device *serdev)
  1791. +{
  1792. + struct surfacegen5_ec *ec;
  1793. + unsigned long flags;
  1794. + int status;
  1795. +
  1796. + ec = surfacegen5_ec_acquire_init();
  1797. + if (!ec) {
  1798. + return;
  1799. + }
  1800. +
  1801. + surfacegen5_ssh_sysfs_unregister(&serdev->dev);
  1802. +
  1803. + // suspend EC and disable events
  1804. + status = surfacegen5_ssh_ec_suspend(ec);
  1805. + if (status) {
  1806. + dev_err(&serdev->dev, "failed to suspend EC: %d\n", status);
  1807. + }
  1808. +
  1809. + // make sure all events (received up to now) have been properly handled
  1810. + flush_workqueue(ec->events.queue_ack);
  1811. + flush_workqueue(ec->events.queue_evt);
  1812. +
  1813. + // remove event handlers
  1814. + spin_lock_irqsave(&ec->events.lock, flags);
  1815. + memset(ec->events.handler, 0,
  1816. + sizeof(struct surfacegen5_ec_event_handler)
  1817. + * SG5_NUM_EVENT_TYPES);
  1818. + spin_unlock_irqrestore(&ec->events.lock, flags);
  1819. +
  1820. + // set device to deinitialized state
  1821. + ec->state = SG5_EC_UNINITIALIZED;
  1822. + ec->serdev = NULL;
  1823. +
  1824. + // ensure state and serdev get set before continuing
  1825. + smp_mb();
  1826. +
  1827. + /*
  1828. + * Flush any event that has not been processed yet to ensure we're not going to
  1829. + * use the serial device any more (e.g. for ACKing).
  1830. + */
  1831. + flush_workqueue(ec->events.queue_ack);
  1832. + flush_workqueue(ec->events.queue_evt);
  1833. +
  1834. + serdev_device_close(serdev);
  1835. +
  1836. + /*
  1837. + * Only at this point, no new events can be received. Destroying the
  1838. + * workqueue here flushes all remaining events. Those events will be
  1839. + * silently ignored and neither ACKed nor any handler gets called.
  1840. + */
  1841. + destroy_workqueue(ec->events.queue_ack);
  1842. + destroy_workqueue(ec->events.queue_evt);
  1843. +
  1844. + // free writer
  1845. + kfree(ec->writer.data);
  1846. + ec->writer.data = NULL;
  1847. + ec->writer.ptr = NULL;
  1848. +
  1849. + // free receiver
  1850. + spin_lock_irqsave(&ec->receiver.lock, flags);
  1851. + ec->receiver.state = SG5_RCV_DISCARD;
  1852. + kfifo_free(&ec->receiver.fifo);
  1853. +
  1854. + kfree(ec->receiver.eval_buf.ptr);
  1855. + ec->receiver.eval_buf.ptr = NULL;
  1856. + ec->receiver.eval_buf.cap = 0;
  1857. + ec->receiver.eval_buf.len = 0;
  1858. + spin_unlock_irqrestore(&ec->receiver.lock, flags);
  1859. +
  1860. + serdev_device_set_drvdata(serdev, NULL);
  1861. + surfacegen5_ec_release(ec);
  1862. +}
  1863. +
  1864. +
  1865. +static const struct acpi_device_id surfacegen5_acpi_ssh_match[] = {
  1866. + { "MSHW0084", 0 },
  1867. + { },
  1868. +};
  1869. +MODULE_DEVICE_TABLE(acpi, surfacegen5_acpi_ssh_match);
  1870. +
  1871. +struct serdev_device_driver surfacegen5_acpi_ssh = {
  1872. + .probe = surfacegen5_acpi_ssh_probe,
  1873. + .remove = surfacegen5_acpi_ssh_remove,
  1874. + .driver = {
  1875. + .name = "surfacegen5_acpi_ssh",
  1876. + .acpi_match_table = ACPI_PTR(surfacegen5_acpi_ssh_match),
  1877. + .pm = &surfacegen5_ssh_pm_ops,
  1878. + },
  1879. +};
  1880. +
  1881. +inline int surfacegen5_acpi_ssh_register(void)
  1882. +{
  1883. + return serdev_device_driver_register(&surfacegen5_acpi_ssh);
  1884. +}
  1885. +
  1886. +inline void surfacegen5_acpi_ssh_unregister(void)
  1887. +{
  1888. + serdev_device_driver_unregister(&surfacegen5_acpi_ssh);
  1889. +}
  1890. +
  1891. +#else /* CONFIG_SURFACE_ACPI_SSH */
  1892. +
  1893. +inline int surfacegen5_acpi_ssh_register(void)
  1894. +{
  1895. + return 0;
  1896. +}
  1897. +
  1898. +inline void surfacegen5_acpi_ssh_unregister(void)
  1899. +{
  1900. +}
  1901. +
  1902. +
  1903. +#endif /* CONFIG_SURFACE_ACPI_SSH */
  1904. +
  1905. +
  1906. +/*************************************************************************
  1907. + * Surface Serial Hub Debug Device (private implementation)
  1908. + */
  1909. +
  1910. +#ifdef CONFIG_SURFACE_ACPI_SSH_DEBUG_DEVICE
  1911. +
  1912. +static char sg5_ssh_debug_rqst_buf_sysfs[SURFACEGEN5_MAX_RQST_RESPONSE + 1] = { 0 };
  1913. +static char sg5_ssh_debug_rqst_buf_pld[SURFACEGEN5_MAX_RQST_PAYLOAD] = { 0 };
  1914. +static char sg5_ssh_debug_rqst_buf_res[SURFACEGEN5_MAX_RQST_RESPONSE] = { 0 };
  1915. +
  1916. +static ssize_t rqst_read(struct file *f, struct kobject *kobj, struct bin_attribute *attr,
  1917. + char *buf, loff_t offs, size_t count)
  1918. +{
  1919. + if (offs < 0 || count + offs > SURFACEGEN5_MAX_RQST_RESPONSE) {
  1920. + return -EINVAL;
  1921. + }
  1922. +
  1923. + memcpy(buf, sg5_ssh_debug_rqst_buf_sysfs + offs, count);
  1924. + return count;
  1925. +}
  1926. +
  1927. +static ssize_t rqst_write(struct file *f, struct kobject *kobj, struct bin_attribute *attr,
  1928. + char *buf, loff_t offs, size_t count)
  1929. +{
  1930. + struct surfacegen5_rqst rqst = {};
  1931. + struct surfacegen5_buf result = {};
  1932. + int status;
  1933. +
  1934. + // check basic write constriants
  1935. + if (offs != 0 || count > SURFACEGEN5_MAX_RQST_PAYLOAD + 5) {
  1936. + return -EINVAL;
  1937. + }
  1938. +
  1939. + // payload length should be consistent with data provided
  1940. + if (buf[4] + 5 != count) {
  1941. + return -EINVAL;
  1942. + }
  1943. +
  1944. + rqst.tc = buf[0];
  1945. + rqst.iid = buf[1];
  1946. + rqst.cid = buf[2];
  1947. + rqst.snc = buf[3];
  1948. + rqst.cdl = buf[4];
  1949. + rqst.pld = sg5_ssh_debug_rqst_buf_pld;
  1950. + memcpy(sg5_ssh_debug_rqst_buf_pld, buf + 5, count - 5);
  1951. +
  1952. + result.cap = SURFACEGEN5_MAX_RQST_RESPONSE;
  1953. + result.len = 0;
  1954. + result.data = sg5_ssh_debug_rqst_buf_res;
  1955. +
  1956. + status = surfacegen5_ec_rqst(&rqst, &result);
  1957. + if (status) {
  1958. + return status;
  1959. + }
  1960. +
  1961. + sg5_ssh_debug_rqst_buf_sysfs[0] = result.len;
  1962. + memcpy(sg5_ssh_debug_rqst_buf_sysfs + 1, result.data, result.len);
  1963. + memset(sg5_ssh_debug_rqst_buf_sysfs + result.len + 1, 0,
  1964. + SURFACEGEN5_MAX_RQST_RESPONSE + 1 - result.len);
  1965. +
  1966. + return count;
  1967. +}
  1968. +
  1969. +static const BIN_ATTR_RW(rqst, SURFACEGEN5_MAX_RQST_RESPONSE + 1);
  1970. +
  1971. +
  1972. +inline int surfacegen5_ssh_sysfs_register(struct device *dev)
  1973. +{
  1974. + return sysfs_create_bin_file(&dev->kobj, &bin_attr_rqst);
  1975. +}
  1976. +
  1977. +inline void surfacegen5_ssh_sysfs_unregister(struct device *dev)
  1978. +{
  1979. + sysfs_remove_bin_file(&dev->kobj, &bin_attr_rqst);
  1980. +}
  1981. +
  1982. +#elif defined(CONFIG_SURFACE_ACPI_SSH)
  1983. +
  1984. +inline int surfacegen5_ssh_sysfs_register(struct device *dev)
  1985. +{
  1986. + return 0;
  1987. +}
  1988. +
  1989. +inline void surfacegen5_ssh_sysfs_unregister(struct device *dev)
  1990. +{
  1991. +}
  1992. +
  1993. +#endif /* CONFIG_SURFACE_ACPI_SSH_DEBUG_DEVICE*/
  1994. +
  1995. +
  1996. +/*************************************************************************
  1997. + * Surface ACPI Notify driver
  1998. + */
  1999. +
  2000. +#ifdef CONFIG_SURFACE_ACPI_SAN
  2001. +
  2002. +#define SG5_SAN_RQST_RETRY 5
  2003. +
  2004. +#define SG5_SAN_DSM_REVISION 0
  2005. +#define SG5_SAN_DSM_FN_NOTIFY_SENSOR_TRIP_POINT 0x09
  2006. +
  2007. +static const guid_t SG5_SAN_DSM_UUID =
  2008. + GUID_INIT(0x93b666c5, 0x70c6, 0x469f, 0xa2, 0x15, 0x3d,
  2009. + 0x48, 0x7c, 0x91, 0xab, 0x3c);
  2010. +
  2011. +#define SG5_EVENT_DELAY_POWER msecs_to_jiffies(5000)
  2012. +
  2013. +#define SG5_EVENT_PWR_TC 0x02
  2014. +#define SG5_EVENT_PWR_RQID 0x0002
  2015. +#define SG5_EVENT_PWR_CID_HWCHANGE 0x15
  2016. +#define SG5_EVENT_PWR_CID_CHARGING 0x16
  2017. +#define SG5_EVENT_PWR_CID_ADAPTER 0x17
  2018. +#define SG5_EVENT_PWR_CID_STATE 0x4f
  2019. +
  2020. +#define SG5_EVENT_TEMP_TC 0x03
  2021. +#define SG5_EVENT_TEMP_RQID 0x0003
  2022. +#define SG5_EVENT_TEMP_CID_NOTIFY_SENSOR_TRIP_POINT 0x0b
  2023. +
  2024. +#define SG5_SAN_RQST_TAG "surfacegen5_ec_rqst: "
  2025. +
  2026. +#define SG5_QUIRK_BASE_STATE_DELAY 1000
  2027. +
  2028. +
  2029. +struct surfacegen5_san_acpi_consumer {
  2030. + char *path;
  2031. + bool required;
  2032. + u32 flags;
  2033. +};
  2034. +
  2035. +struct surfacegen5_san_opreg_context {
  2036. + struct acpi_connection_info connection;
  2037. + struct device *dev;
  2038. +};
  2039. +
  2040. +struct surfacegen5_san_consumer_link {
  2041. + const struct surfacegen5_san_acpi_consumer *properties;
  2042. + struct device_link *link;
  2043. +};
  2044. +
  2045. +struct surfacegen5_san_consumers {
  2046. + u32 num;
  2047. + struct surfacegen5_san_consumer_link *links;
  2048. +};
  2049. +
  2050. +struct surfacegen5_san_drvdata {
  2051. + struct surfacegen5_san_opreg_context opreg_ctx;
  2052. + struct surfacegen5_san_consumers consumers;
  2053. +};
  2054. +
  2055. +struct gsb_data_in {
  2056. + u8 cv;
  2057. +} __packed;
  2058. +
  2059. +struct gsb_data_rqsx {
  2060. + u8 cv; // command value (should be 0x01 or 0x03)
  2061. + u8 tc; // target controller
  2062. + u8 tid; // expected to be 0x01, could be revision
  2063. + u8 iid; // target sub-controller (e.g. primary vs. secondary battery)
  2064. + u8 snc; // expect-response-flag
  2065. + u8 cid; // command ID
  2066. + u8 cdl; // payload length
  2067. + u8 _pad; // padding
  2068. + u8 pld[0]; // payload
  2069. +} __packed;
  2070. +
  2071. +struct gsb_data_etwl {
  2072. + u8 cv; // command value (should be 0x02)
  2073. + u8 etw3; // ?
  2074. + u8 etw4; // ?
  2075. + u8 msg[0]; // error message (ASCIIZ)
  2076. +} __packed;
  2077. +
  2078. +struct gsb_data_out {
  2079. + u8 status; // _SSH communication status
  2080. + u8 len; // _SSH payload length
  2081. + u8 pld[0]; // _SSH payload
  2082. +} __packed;
  2083. +
  2084. +union gsb_buffer_data {
  2085. + struct gsb_data_in in; // common input
  2086. + struct gsb_data_rqsx rqsx; // RQSX input
  2087. + struct gsb_data_etwl etwl; // ETWL input
  2088. + struct gsb_data_out out; // output
  2089. +};
  2090. +
  2091. +struct gsb_buffer {
  2092. + u8 status; // GSB AttribRawProcess status
  2093. + u8 len; // GSB AttribRawProcess length
  2094. + union gsb_buffer_data data;
  2095. +} __packed;
  2096. +
  2097. +
  2098. +enum surfacegen5_pwr_event {
  2099. + SURFACEGEN5_PWR_EVENT_BAT1_STAT = 0x03,
  2100. + SURFACEGEN5_PWR_EVENT_BAT1_INFO = 0x04,
  2101. + SURFACEGEN5_PWR_EVENT_ADP1_STAT = 0x05,
  2102. + SURFACEGEN5_PWR_EVENT_ADP1_INFO = 0x06,
  2103. + SURFACEGEN5_PWR_EVENT_BAT2_STAT = 0x07,
  2104. + SURFACEGEN5_PWR_EVENT_BAT2_INFO = 0x08,
  2105. +};
  2106. +
  2107. +
  2108. +static int surfacegen5_acpi_notify_power_event(struct device *dev, enum surfacegen5_pwr_event event)
  2109. +{
  2110. + acpi_handle san = ACPI_HANDLE(dev);
  2111. + union acpi_object *obj;
  2112. +
  2113. + obj = acpi_evaluate_dsm_typed(san, &SG5_SAN_DSM_UUID, SG5_SAN_DSM_REVISION,
  2114. + (u8) event, NULL, ACPI_TYPE_BUFFER);
  2115. +
  2116. + if (IS_ERR_OR_NULL(obj)) {
  2117. + return obj ? PTR_ERR(obj) : -ENXIO;
  2118. + }
  2119. +
  2120. + if (obj->buffer.length != 1 || obj->buffer.pointer[0] != 0) {
  2121. + dev_err(dev, "got unexpected result from _DSM\n");
  2122. + return -EFAULT;
  2123. + }
  2124. +
  2125. + ACPI_FREE(obj);
  2126. + return 0;
  2127. +}
  2128. +
  2129. +static int surfacegen5_acpi_notify_sensor_trip_point(struct device *dev, u8 iid)
  2130. +{
  2131. + acpi_handle san = ACPI_HANDLE(dev);
  2132. + union acpi_object *obj;
  2133. + union acpi_object param;
  2134. +
  2135. + param.type = ACPI_TYPE_INTEGER;
  2136. + param.integer.value = iid;
  2137. +
  2138. + obj = acpi_evaluate_dsm_typed(san, &SG5_SAN_DSM_UUID, SG5_SAN_DSM_REVISION,
  2139. + SG5_SAN_DSM_FN_NOTIFY_SENSOR_TRIP_POINT,
  2140. + &param, ACPI_TYPE_BUFFER);
  2141. +
  2142. + if (IS_ERR_OR_NULL(obj)) {
  2143. + return obj ? PTR_ERR(obj) : -ENXIO;
  2144. + }
  2145. +
  2146. + if (obj->buffer.length != 1 || obj->buffer.pointer[0] != 0) {
  2147. + dev_err(dev, "got unexpected result from _DSM\n");
  2148. + return -EFAULT;
  2149. + }
  2150. +
  2151. + ACPI_FREE(obj);
  2152. + return 0;
  2153. +}
  2154. +
  2155. +
  2156. +inline static int surfacegen5_evt_power_adapter(struct device *dev, struct surfacegen5_event *event)
  2157. +{
  2158. + int status;
  2159. +
  2160. + status = surfacegen5_acpi_notify_power_event(dev, SURFACEGEN5_PWR_EVENT_ADP1_STAT);
  2161. + if (status) {
  2162. + dev_err(dev, "error handling power event (cid = %x)\n", event->cid);
  2163. + return status;
  2164. + }
  2165. +
  2166. + return 0;
  2167. +}
  2168. +
  2169. +inline static int surfacegen5_evt_power_hwchange(struct device *dev, struct surfacegen5_event *event)
  2170. +{
  2171. + enum surfacegen5_pwr_event evcode;
  2172. + int status;
  2173. +
  2174. + if (event->iid == 0x02) {
  2175. + evcode = SURFACEGEN5_PWR_EVENT_BAT2_INFO;
  2176. + } else {
  2177. + evcode = SURFACEGEN5_PWR_EVENT_BAT1_INFO;
  2178. + }
  2179. +
  2180. + status = surfacegen5_acpi_notify_power_event(dev, evcode);
  2181. + if (status) {
  2182. + dev_err(dev, "error handling power event (cid = %x)\n", event->cid);
  2183. + return status;
  2184. + }
  2185. +
  2186. + return 0;
  2187. +}
  2188. +
  2189. +inline static int surfacegen5_evt_power_state(struct device *dev, struct surfacegen5_event *event)
  2190. +{
  2191. + int status;
  2192. +
  2193. + status = surfacegen5_acpi_notify_power_event(dev, SURFACEGEN5_PWR_EVENT_BAT1_STAT);
  2194. + if (status) {
  2195. + dev_err(dev, "error handling power event (cid = %x)\n", event->cid);
  2196. + return status;
  2197. + }
  2198. +
  2199. + status = surfacegen5_acpi_notify_power_event(dev, SURFACEGEN5_PWR_EVENT_BAT2_STAT);
  2200. + if (status) {
  2201. + dev_err(dev, "error handling power event (cid = %x)\n", event->cid);
  2202. + return status;
  2203. + }
  2204. +
  2205. + return 0;
  2206. +}
  2207. +
  2208. +static unsigned long surfacegen5_evt_power_delay(struct surfacegen5_event *event, void *data)
  2209. +{
  2210. + switch (event->cid) {
  2211. + case SG5_EVENT_PWR_CID_CHARGING:
  2212. + case SG5_EVENT_PWR_CID_STATE:
  2213. + return SG5_EVENT_DELAY_POWER;
  2214. +
  2215. + case SG5_EVENT_PWR_CID_ADAPTER:
  2216. + case SG5_EVENT_PWR_CID_HWCHANGE:
  2217. + default:
  2218. + return 0;
  2219. + }
  2220. +}
  2221. +
  2222. +static int surfacegen5_evt_power(struct surfacegen5_event *event, void *data)
  2223. +{
  2224. + struct device *dev = (struct device *)data;
  2225. +
  2226. + switch (event->cid) {
  2227. + case SG5_EVENT_PWR_CID_HWCHANGE:
  2228. + return surfacegen5_evt_power_hwchange(dev, event);
  2229. +
  2230. + case SG5_EVENT_PWR_CID_ADAPTER:
  2231. + return surfacegen5_evt_power_adapter(dev, event);
  2232. +
  2233. + case SG5_EVENT_PWR_CID_CHARGING:
  2234. + case SG5_EVENT_PWR_CID_STATE:
  2235. + return surfacegen5_evt_power_state(dev, event);
  2236. +
  2237. + default:
  2238. + dev_warn(dev, "unhandled power event (cid = %x)\n", event->cid);
  2239. + }
  2240. +
  2241. + return 0;
  2242. +}
  2243. +
  2244. +
  2245. +inline static int surfacegen5_evt_thermal_notify(struct device *dev, struct surfacegen5_event *event)
  2246. +{
  2247. + int status;
  2248. +
  2249. + status = surfacegen5_acpi_notify_sensor_trip_point(dev, event->iid);
  2250. + if (status) {
  2251. + dev_err(dev, "error handling thermal event (cid = %x)\n", event->cid);
  2252. + return status;
  2253. + }
  2254. +
  2255. + return 0;
  2256. +}
  2257. +
  2258. +static int surfacegen5_evt_thermal(struct surfacegen5_event *event, void *data)
  2259. +{
  2260. + struct device *dev = (struct device *)data;
  2261. +
  2262. + switch (event->cid) {
  2263. + case SG5_EVENT_TEMP_CID_NOTIFY_SENSOR_TRIP_POINT:
  2264. + return surfacegen5_evt_thermal_notify(dev, event);
  2265. +
  2266. + default:
  2267. + dev_warn(dev, "unhandled thermal event (cid = %x)\n", event->cid);
  2268. + }
  2269. +
  2270. + return 0;
  2271. +}
  2272. +
  2273. +
  2274. +static struct gsb_data_rqsx *surfacegen5_san_validate_rqsx(
  2275. + struct device *dev, const char *type, struct gsb_buffer *buffer)
  2276. +{
  2277. + struct gsb_data_rqsx *rqsx = &buffer->data.rqsx;
  2278. +
  2279. + if (buffer->len < 8) {
  2280. + dev_err(dev, "invalid %s package (len = %d)\n",
  2281. + type, buffer->len);
  2282. + return NULL;
  2283. + }
  2284. +
  2285. + if (rqsx->cdl != buffer->len - 8) {
  2286. + dev_err(dev, "bogus %s package (len = %d, cdl = %d)\n",
  2287. + type, buffer->len, rqsx->cdl);
  2288. + return NULL;
  2289. + }
  2290. +
  2291. + if (rqsx->tid != 0x01) {
  2292. + dev_warn(dev, "unsupported %s package (tid = 0x%02x)\n",
  2293. + type, rqsx->tid);
  2294. + return NULL;
  2295. + }
  2296. +
  2297. + return rqsx;
  2298. +}
  2299. +
  2300. +static acpi_status
  2301. +surfacegen5_san_etwl(struct surfacegen5_san_opreg_context *ctx, struct gsb_buffer *buffer)
  2302. +{
  2303. + struct gsb_data_etwl *etwl = &buffer->data.etwl;
  2304. +
  2305. + if (buffer->len < 3) {
  2306. + dev_err(ctx->dev, "invalid ETWL package (len = %d)\n", buffer->len);
  2307. + return AE_OK;
  2308. + }
  2309. +
  2310. + dev_err(ctx->dev, "ETWL(0x%02x, 0x%02x): %.*s\n",
  2311. + etwl->etw3, etwl->etw4,
  2312. + buffer->len - 3, (char *)etwl->msg);
  2313. +
  2314. + // indicate success
  2315. + buffer->status = 0x00;
  2316. + buffer->len = 0x00;
  2317. +
  2318. + return AE_OK;
  2319. +}
  2320. +
  2321. +static acpi_status
  2322. +surfacegen5_san_rqst(struct surfacegen5_san_opreg_context *ctx, struct gsb_buffer *buffer)
  2323. +{
  2324. + struct gsb_data_rqsx *gsb_rqst = surfacegen5_san_validate_rqsx(ctx->dev, "RQST", buffer);
  2325. + struct surfacegen5_rqst rqst = {};
  2326. + struct surfacegen5_buf result = {};
  2327. + int status = 0;
  2328. + int try;
  2329. +
  2330. + if (!gsb_rqst) {
  2331. + return AE_OK;
  2332. + }
  2333. +
  2334. + rqst.tc = gsb_rqst->tc;
  2335. + rqst.iid = gsb_rqst->iid;
  2336. + rqst.cid = gsb_rqst->cid;
  2337. + rqst.snc = gsb_rqst->snc;
  2338. + rqst.cdl = gsb_rqst->cdl;
  2339. + rqst.pld = &gsb_rqst->pld[0];
  2340. +
  2341. + result.cap = SURFACEGEN5_MAX_RQST_RESPONSE;
  2342. + result.len = 0;
  2343. + result.data = kzalloc(result.cap, GFP_KERNEL);
  2344. +
  2345. + if (!result.data) {
  2346. + return AE_NO_MEMORY;
  2347. + }
  2348. +
  2349. + for (try = 0; try < SG5_SAN_RQST_RETRY; try++) {
  2350. + if (try) {
  2351. + dev_warn(ctx->dev, SG5_SAN_RQST_TAG "IO error occured, trying again\n");
  2352. + }
  2353. +
  2354. + status = surfacegen5_ec_rqst(&rqst, &result);
  2355. + if (status != -EIO) break;
  2356. + }
  2357. +
  2358. + if (rqst.tc == 0x11 && rqst.cid == 0x0D && status == -EPERM) {
  2359. + /* Base state quirk:
  2360. + * The base state may be queried from ACPI when the EC is still
  2361. + * suspended. In this case it will return '-EPERM'. This query
  2362. + * will only be triggered from the ACPI lid GPE interrupt, thus
  2363. + * we are either in laptop or studio mode (base status 0x01 or
  2364. + * 0x02). Furthermore, we will only get here if the device (and
  2365. + * EC) have been suspended.
  2366. + *
  2367. + * We now assume that the device is in laptop mode (0x01). This
  2368. + * has the drawback that it will wake the device when unfolding
  2369. + * it in studio mode, but it also allows us to avoid actively
  2370. + * waiting for the EC to wake up, which may incur a notable
  2371. + * delay.
  2372. + */
  2373. +
  2374. + buffer->status = 0x00;
  2375. + buffer->len = 0x03;
  2376. + buffer->data.out.status = 0x00;
  2377. + buffer->data.out.len = 0x01;
  2378. + buffer->data.out.pld[0] = 0x01;
  2379. +
  2380. + } else if (!status) { // success
  2381. + buffer->status = 0x00;
  2382. + buffer->len = result.len + 2;
  2383. + buffer->data.out.status = 0x00;
  2384. + buffer->data.out.len = result.len;
  2385. + memcpy(&buffer->data.out.pld[0], result.data, result.len);
  2386. +
  2387. + } else { // failure
  2388. + dev_err(ctx->dev, SG5_SAN_RQST_TAG "failed with error %d\n", status);
  2389. + buffer->status = 0x00;
  2390. + buffer->len = 0x02;
  2391. + buffer->data.out.status = 0x01; // indicate _SSH error
  2392. + buffer->data.out.len = 0x00;
  2393. + }
  2394. +
  2395. + kfree(result.data);
  2396. +
  2397. + return AE_OK;
  2398. +}
  2399. +
  2400. +static acpi_status
  2401. +surfacegen5_san_rqsg(struct surfacegen5_san_opreg_context *ctx, struct gsb_buffer *buffer)
  2402. +{
  2403. + struct gsb_data_rqsx *rqsg = surfacegen5_san_validate_rqsx(ctx->dev, "RQSG", buffer);
  2404. +
  2405. + if (!rqsg) {
  2406. + return AE_OK;
  2407. + }
  2408. +
  2409. + // TODO: RQSG handler
  2410. +
  2411. + dev_warn(ctx->dev, "unsupported request: RQSG(0x%02x, 0x%02x, 0x%02x)\n",
  2412. + rqsg->tc, rqsg->cid, rqsg->iid);
  2413. +
  2414. + return AE_OK;
  2415. +}
  2416. +
  2417. +
  2418. +static acpi_status
  2419. +surfacegen5_san_opreg_handler(u32 function, acpi_physical_address command,
  2420. + u32 bits, u64 *value64,
  2421. + void *opreg_context, void *region_context)
  2422. +{
  2423. + struct surfacegen5_san_opreg_context *context = opreg_context;
  2424. + struct gsb_buffer *buffer = (struct gsb_buffer *)value64;
  2425. + int accessor_type = (0xFFFF0000 & function) >> 16;
  2426. +
  2427. + if (command != 0) {
  2428. + dev_warn(context->dev, "unsupported command: 0x%02llx\n", command);
  2429. + return AE_OK;
  2430. + }
  2431. +
  2432. + if (accessor_type != ACPI_GSB_ACCESS_ATTRIB_RAW_PROCESS) {
  2433. + dev_err(context->dev, "invalid access type: 0x%02x\n", accessor_type);
  2434. + return AE_OK;
  2435. + }
  2436. +
  2437. + // buffer must have at least contain the command-value
  2438. + if (buffer->len == 0) {
  2439. + dev_err(context->dev, "request-package too small\n");
  2440. + return AE_OK;
  2441. + }
  2442. +
  2443. + switch (buffer->data.in.cv) {
  2444. + case 0x01: return surfacegen5_san_rqst(context, buffer);
  2445. + case 0x02: return surfacegen5_san_etwl(context, buffer);
  2446. + case 0x03: return surfacegen5_san_rqsg(context, buffer);
  2447. + }
  2448. +
  2449. + dev_warn(context->dev, "unsupported SAN0 request (cv: 0x%02x)\n", buffer->data.in.cv);
  2450. + return AE_OK;
  2451. +}
  2452. +
  2453. +static int surfacegen5_san_enable_events(struct device *dev)
  2454. +{
  2455. + int status;
  2456. +
  2457. + status = surfacegen5_ec_set_delayed_event_handler(
  2458. + SG5_EVENT_PWR_RQID, surfacegen5_evt_power,
  2459. + surfacegen5_evt_power_delay, dev);
  2460. + if (status) {
  2461. + goto err_event_handler_power;
  2462. + }
  2463. +
  2464. + status = surfacegen5_ec_set_event_handler(
  2465. + SG5_EVENT_TEMP_RQID, surfacegen5_evt_thermal,
  2466. + dev);
  2467. + if (status) {
  2468. + goto err_event_handler_thermal;
  2469. + }
  2470. +
  2471. + status = surfacegen5_ec_enable_event_source(SG5_EVENT_PWR_TC, 0x01, SG5_EVENT_PWR_RQID);
  2472. + if (status) {
  2473. + goto err_event_source_power;
  2474. + }
  2475. +
  2476. + status = surfacegen5_ec_enable_event_source(SG5_EVENT_TEMP_TC, 0x01, SG5_EVENT_TEMP_RQID);
  2477. + if (status) {
  2478. + goto err_event_source_thermal;
  2479. + }
  2480. +
  2481. + return 0;
  2482. +
  2483. +err_event_source_thermal:
  2484. + surfacegen5_ec_disable_event_source(SG5_EVENT_PWR_TC, 0x01, SG5_EVENT_PWR_RQID);
  2485. +err_event_source_power:
  2486. + surfacegen5_ec_remove_event_handler(SG5_EVENT_TEMP_RQID);
  2487. +err_event_handler_thermal:
  2488. + surfacegen5_ec_remove_event_handler(SG5_EVENT_PWR_RQID);
  2489. +err_event_handler_power:
  2490. + return status;
  2491. +}
  2492. +
  2493. +static void surfacegen5_san_disable_events(void)
  2494. +{
  2495. + surfacegen5_ec_disable_event_source(SG5_EVENT_TEMP_TC, 0x01, SG5_EVENT_TEMP_RQID);
  2496. + surfacegen5_ec_disable_event_source(SG5_EVENT_PWR_TC, 0x01, SG5_EVENT_PWR_RQID);
  2497. + surfacegen5_ec_remove_event_handler(SG5_EVENT_TEMP_RQID);
  2498. + surfacegen5_ec_remove_event_handler(SG5_EVENT_PWR_RQID);
  2499. +}
  2500. +
  2501. +
  2502. +static int surfacegen5_san_consumers_link(struct platform_device *pdev,
  2503. + const struct surfacegen5_san_acpi_consumer *cons,
  2504. + struct surfacegen5_san_consumers *out)
  2505. +{
  2506. + const struct surfacegen5_san_acpi_consumer *con;
  2507. + struct surfacegen5_san_consumer_link *links, *link;
  2508. + struct acpi_device *adev;
  2509. + acpi_handle handle;
  2510. + u32 max_links = 0;
  2511. + int status;
  2512. +
  2513. + if (!cons) {
  2514. + return 0;
  2515. + }
  2516. +
  2517. + // count links
  2518. + for (con = cons; con->path; ++con) {
  2519. + max_links += 1;
  2520. + }
  2521. +
  2522. + // allocate
  2523. + links = kzalloc(max_links * sizeof(struct surfacegen5_san_consumer_link), GFP_KERNEL);
  2524. + link = &links[0];
  2525. +
  2526. + if (!links) {
  2527. + return -ENOMEM;
  2528. + }
  2529. +
  2530. + // create links
  2531. + for (con = cons; con->path; ++con) {
  2532. + status = acpi_get_handle(NULL, con->path, &handle);
  2533. + if (status) {
  2534. + if (con->required || status != AE_NOT_FOUND) {
  2535. + status = -ENXIO;
  2536. + goto consumers_link_cleanup;
  2537. + } else {
  2538. + continue;
  2539. + }
  2540. + }
  2541. +
  2542. + status = acpi_bus_get_device(handle, &adev);
  2543. + if (status) {
  2544. + goto consumers_link_cleanup;
  2545. + }
  2546. +
  2547. + link->link = device_link_add(&adev->dev, &pdev->dev, con->flags);
  2548. + if (!(link->link)) {
  2549. + status = -EFAULT;
  2550. + goto consumers_link_cleanup;
  2551. + }
  2552. + link->properties = con;
  2553. +
  2554. + link += 1;
  2555. + }
  2556. +
  2557. + out->num = link - links;
  2558. + out->links = links;
  2559. +
  2560. + return 0;
  2561. +
  2562. +consumers_link_cleanup:
  2563. + for (link = link - 1; link >= links; --link) {
  2564. + if (link->properties->flags & DL_FLAG_STATELESS) {
  2565. + device_link_del(link->link);
  2566. + }
  2567. + }
  2568. +
  2569. + return status;
  2570. +}
  2571. +
  2572. +static void surfacegen5_san_consumers_unlink(struct surfacegen5_san_consumers *consumers) {
  2573. + u32 i;
  2574. +
  2575. + if (!consumers) {
  2576. + return;
  2577. + }
  2578. +
  2579. + for (i = 0; i < consumers->num; ++i) {
  2580. + if (consumers->links[i].properties->flags & DL_FLAG_STATELESS) {
  2581. + device_link_del(consumers->links[i].link);
  2582. + }
  2583. + }
  2584. +
  2585. + kfree(consumers->links);
  2586. +
  2587. + consumers->num = 0;
  2588. + consumers->links = NULL;
  2589. +}
  2590. +
  2591. +static int surfacegen5_acpi_san_probe(struct platform_device *pdev)
  2592. +{
  2593. + const struct surfacegen5_san_acpi_consumer *cons;
  2594. + struct surfacegen5_san_drvdata *drvdata;
  2595. + acpi_handle san = ACPI_HANDLE(&pdev->dev); // _SAN device node
  2596. + int status;
  2597. +
  2598. + /*
  2599. + * Defer probe if the _SSH driver has not set up the controller yet. This
  2600. + * makes sure we do not fail any initial requests (e.g. _STA request without
  2601. + * which the battery does not get set up correctly). Otherwise register as
  2602. + * consumer to set up a device_link.
  2603. + */
  2604. + status = surfacegen5_ec_consumer_register(&pdev->dev);
  2605. + if (status) {
  2606. + return status == -ENXIO ? -EPROBE_DEFER : status;
  2607. + }
  2608. +
  2609. + drvdata = kzalloc(sizeof(struct surfacegen5_san_drvdata), GFP_KERNEL);
  2610. + if (!drvdata) {
  2611. + return -ENOMEM;
  2612. + }
  2613. +
  2614. + drvdata->opreg_ctx.dev = &pdev->dev;
  2615. +
  2616. + cons = acpi_device_get_match_data(&pdev->dev);
  2617. + status = surfacegen5_san_consumers_link(pdev, cons, &drvdata->consumers);
  2618. + if (status) {
  2619. + goto err_probe_consumers;
  2620. + }
  2621. +
  2622. + platform_set_drvdata(pdev, drvdata);
  2623. +
  2624. + status = acpi_install_address_space_handler(san,
  2625. + ACPI_ADR_SPACE_GSBUS,
  2626. + &surfacegen5_san_opreg_handler,
  2627. + NULL, &drvdata->opreg_ctx);
  2628. +
  2629. + if (ACPI_FAILURE(status)) {
  2630. + status = -ENODEV;
  2631. + goto err_probe_install_handler;
  2632. + }
  2633. +
  2634. + status = surfacegen5_san_enable_events(&pdev->dev);
  2635. + if (status) {
  2636. + goto err_probe_enable_events;
  2637. + }
  2638. +
  2639. + acpi_walk_dep_device_list(san);
  2640. + return 0;
  2641. +
  2642. +err_probe_enable_events:
  2643. + acpi_remove_address_space_handler(san, ACPI_ADR_SPACE_GSBUS, &surfacegen5_san_opreg_handler);
  2644. +err_probe_install_handler:
  2645. + platform_set_drvdata(san, NULL);
  2646. + surfacegen5_san_consumers_unlink(&drvdata->consumers);
  2647. +err_probe_consumers:
  2648. + kfree(drvdata);
  2649. + return status;
  2650. +}
  2651. +
  2652. +static int surfacegen5_acpi_san_remove(struct platform_device *pdev)
  2653. +{
  2654. + struct surfacegen5_san_drvdata *drvdata = platform_get_drvdata(pdev);
  2655. + acpi_handle san = ACPI_HANDLE(&pdev->dev); // _SAN device node
  2656. + acpi_status status = AE_OK;
  2657. +
  2658. + acpi_remove_address_space_handler(san, ACPI_ADR_SPACE_GSBUS, &surfacegen5_san_opreg_handler);
  2659. + surfacegen5_san_disable_events();
  2660. +
  2661. + surfacegen5_san_consumers_unlink(&drvdata->consumers);
  2662. + kfree(drvdata);
  2663. +
  2664. + platform_set_drvdata(pdev, NULL);
  2665. + return status;
  2666. +}
  2667. +
  2668. +
  2669. +static const struct surfacegen5_san_acpi_consumer surfacegen5_mshw0091_consumers[] = {
  2670. + { "\\_SB.SRTC", true, DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS },
  2671. + { "\\ADP1", true, DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS },
  2672. + { "\\_SB.BAT1", true, DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS },
  2673. + { "\\_SB.BAT2", false, DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS },
  2674. + { },
  2675. +};
  2676. +
  2677. +static const struct acpi_device_id surfacegen5_acpi_san_match[] = {
  2678. + { "MSHW0091", (long unsigned int) surfacegen5_mshw0091_consumers },
  2679. + { },
  2680. +};
  2681. +MODULE_DEVICE_TABLE(acpi, surfacegen5_acpi_san_match);
  2682. +
  2683. +struct platform_driver surfacegen5_acpi_san = {
  2684. + .probe = surfacegen5_acpi_san_probe,
  2685. + .remove = surfacegen5_acpi_san_remove,
  2686. + .driver = {
  2687. + .name = "surfacegen5_acpi_san",
  2688. + .acpi_match_table = ACPI_PTR(surfacegen5_acpi_san_match),
  2689. + },
  2690. +};
  2691. +
  2692. +
  2693. +inline int surfacegen5_acpi_san_register(void)
  2694. +{
  2695. + return platform_driver_register(&surfacegen5_acpi_san);
  2696. +}
  2697. +
  2698. +inline void surfacegen5_acpi_san_unregister(void)
  2699. +{
  2700. + platform_driver_unregister(&surfacegen5_acpi_san);
  2701. +}
  2702. +
  2703. +#else /* CONFIG_SURFACE_ACPI_SAN */
  2704. +
  2705. +inline int surfacegen5_acpi_san_register(void)
  2706. +{
  2707. + return 0;
  2708. +}
  2709. +
  2710. +inline void surfacegen5_acpi_san_unregister(void)
  2711. +{
  2712. +}
  2713. +
  2714. +#endif /* CONFIG_SURFACE_ACPI_SAN */
  2715. +
  2716. +
  2717. +/*************************************************************************
  2718. + * Virtual HID Framework driver
  2719. + */
  2720. +
  2721. +#ifdef CONFIG_SURFACE_ACPI_VHF
  2722. +
  2723. +#define SG5_VHF_INPUT_NAME "Microsoft Virtual HID Framework Device"
  2724. +
  2725. +/*
  2726. + * Request ID for VHF events. This value is based on the output of the Surface
  2727. + * EC and should not be changed.
  2728. + */
  2729. +#define SG5_EVENT_VHF_RQID 0x0001
  2730. +#define SG5_EVENT_VHF_TC 0x08
  2731. +
  2732. +
  2733. +struct surfacegen5_vhf_evtctx {
  2734. + struct device *dev;
  2735. + struct hid_device *hid;
  2736. +};
  2737. +
  2738. +struct surfacegen5_vhf_drvdata {
  2739. + struct surfacegen5_vhf_evtctx event_ctx;
  2740. +};
  2741. +
  2742. +
  2743. +/*
  2744. + * These report descriptors have been extracted from a Surface Book 2.
  2745. + * They seems to be similar enough to be usable on the Surface Laptop.
  2746. + */
  2747. +static const u8 vhf_hid_desc[] = {
  2748. + // keyboard descriptor (event command ID 0x03)
  2749. + 0x05, 0x01, /* Usage Page (Desktop), */
  2750. + 0x09, 0x06, /* Usage (Keyboard), */
  2751. + 0xA1, 0x01, /* Collection (Application), */
  2752. + 0x85, 0x01, /* Report ID (1), */
  2753. + 0x15, 0x00, /* Logical Minimum (0), */
  2754. + 0x25, 0x01, /* Logical Maximum (1), */
  2755. + 0x75, 0x01, /* Report Size (1), */
  2756. + 0x95, 0x08, /* Report Count (8), */
  2757. + 0x05, 0x07, /* Usage Page (Keyboard), */
  2758. + 0x19, 0xE0, /* Usage Minimum (KB Leftcontrol), */
  2759. + 0x29, 0xE7, /* Usage Maximum (KB Right GUI), */
  2760. + 0x81, 0x02, /* Input (Variable), */
  2761. + 0x75, 0x08, /* Report Size (8), */
  2762. + 0x95, 0x0A, /* Report Count (10), */
  2763. + 0x19, 0x00, /* Usage Minimum (None), */
  2764. + 0x29, 0x91, /* Usage Maximum (KB LANG2), */
  2765. + 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  2766. + 0x81, 0x00, /* Input, */
  2767. + 0x05, 0x0C, /* Usage Page (Consumer), */
  2768. + 0x0A, 0xC0, 0x02, /* Usage (02C0h), */
  2769. + 0xA1, 0x02, /* Collection (Logical), */
  2770. + 0x1A, 0xC1, 0x02, /* Usage Minimum (02C1h), */
  2771. + 0x2A, 0xC6, 0x02, /* Usage Maximum (02C6h), */
  2772. + 0x95, 0x06, /* Report Count (6), */
  2773. + 0xB1, 0x03, /* Feature (Constant, Variable), */
  2774. + 0xC0, /* End Collection, */
  2775. + 0x05, 0x08, /* Usage Page (LED), */
  2776. + 0x19, 0x01, /* Usage Minimum (01h), */
  2777. + 0x29, 0x03, /* Usage Maximum (03h), */
  2778. + 0x75, 0x01, /* Report Size (1), */
  2779. + 0x95, 0x03, /* Report Count (3), */
  2780. + 0x25, 0x01, /* Logical Maximum (1), */
  2781. + 0x91, 0x02, /* Output (Variable), */
  2782. + 0x95, 0x05, /* Report Count (5), */
  2783. + 0x91, 0x01, /* Output (Constant), */
  2784. + 0xC0, /* End Collection, */
  2785. +
  2786. + // media key descriptor (event command ID 0x04)
  2787. + 0x05, 0x0C, /* Usage Page (Consumer), */
  2788. + 0x09, 0x01, /* Usage (Consumer Control), */
  2789. + 0xA1, 0x01, /* Collection (Application), */
  2790. + 0x85, 0x03, /* Report ID (3), */
  2791. + 0x75, 0x10, /* Report Size (16), */
  2792. + 0x15, 0x00, /* Logical Minimum (0), */
  2793. + 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
  2794. + 0x19, 0x00, /* Usage Minimum (00h), */
  2795. + 0x2A, 0xFF, 0x03, /* Usage Maximum (03FFh), */
  2796. + 0x81, 0x00, /* Input, */
  2797. + 0xC0, /* End Collection, */
  2798. +};
  2799. +
  2800. +
  2801. +static int vhf_hid_start(struct hid_device *hid)
  2802. +{
  2803. + hid_dbg(hid, "%s\n", __func__);
  2804. + return 0;
  2805. +}
  2806. +
  2807. +static void vhf_hid_stop(struct hid_device *hid)
  2808. +{
  2809. + hid_dbg(hid, "%s\n", __func__);
  2810. +}
  2811. +
  2812. +static int vhf_hid_open(struct hid_device *hid)
  2813. +{
  2814. + hid_dbg(hid, "%s\n", __func__);
  2815. + return 0;
  2816. +}
  2817. +
  2818. +static void vhf_hid_close(struct hid_device *hid)
  2819. +{
  2820. + hid_dbg(hid, "%s\n", __func__);
  2821. +}
  2822. +
  2823. +static int vhf_hid_parse(struct hid_device *hid)
  2824. +{
  2825. + return hid_parse_report(hid, (u8 *)vhf_hid_desc, ARRAY_SIZE(vhf_hid_desc));
  2826. +}
  2827. +
  2828. +static int vhf_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
  2829. + u8 *buf, size_t len, unsigned char rtype,
  2830. + int reqtype)
  2831. +{
  2832. + hid_dbg(hid, "%s\n", __func__);
  2833. + return 0;
  2834. +}
  2835. +
  2836. +static int vhf_hid_output_report(struct hid_device *hid, u8 *buf, size_t len)
  2837. +{
  2838. + hid_dbg(hid, "%s\n", __func__);
  2839. + print_hex_dump_debug("report:", DUMP_PREFIX_OFFSET, 16, 1, buf, len, false);
  2840. +
  2841. + return len;
  2842. +}
  2843. +
  2844. +static struct hid_ll_driver vhf_hid_ll_driver = {
  2845. + .start = vhf_hid_start,
  2846. + .stop = vhf_hid_stop,
  2847. + .open = vhf_hid_open,
  2848. + .close = vhf_hid_close,
  2849. + .parse = vhf_hid_parse,
  2850. + .raw_request = vhf_hid_raw_request,
  2851. + .output_report = vhf_hid_output_report,
  2852. +};
  2853. +
  2854. +
  2855. +static struct hid_device *surfacegen5_vhf_create_hid_device(struct platform_device *pdev)
  2856. +{
  2857. + struct hid_device *hid;
  2858. +
  2859. + hid = hid_allocate_device();
  2860. + if (IS_ERR(hid)) {
  2861. + return hid;
  2862. + }
  2863. +
  2864. + hid->dev.parent = &pdev->dev;
  2865. +
  2866. + hid->bus = BUS_VIRTUAL;
  2867. + hid->vendor = USB_VENDOR_ID_MICROSOFT;
  2868. + hid->product = USB_DEVICE_ID_MS_VHF;
  2869. +
  2870. + hid->ll_driver = &vhf_hid_ll_driver;
  2871. +
  2872. + sprintf(hid->name, "%s", SG5_VHF_INPUT_NAME);
  2873. +
  2874. + return hid;
  2875. +}
  2876. +
  2877. +static int surfacegen5_vhf_event_handler(struct surfacegen5_event *event, void *data)
  2878. +{
  2879. + struct surfacegen5_vhf_evtctx *ctx = (struct surfacegen5_vhf_evtctx *)data;
  2880. +
  2881. + if (event->tc == 0x08 && (event->cid == 0x03 || event->cid == 0x04)) {
  2882. + return hid_input_report(ctx->hid, HID_INPUT_REPORT, event->pld, event->len, 1);
  2883. + }
  2884. +
  2885. + dev_warn(ctx->dev, "unsupported event (tc = %d, cid = %d)\n", event->tc, event->cid);
  2886. + return 0;
  2887. +}
  2888. +
  2889. +static unsigned long surfacegen5_vhf_event_delay(struct surfacegen5_event *event, void *data)
  2890. +{
  2891. + // high priority immediate execution for keyboard events
  2892. + if (event->tc == 0x08 && (event->cid == 0x03 || event->cid == 0x04)) {
  2893. + return SURFACEGEN5_EVENT_IMMEDIATE;
  2894. + }
  2895. +
  2896. + return 0;
  2897. +}
  2898. +
  2899. +static int surfacegen5_acpi_vhf_probe(struct platform_device *pdev)
  2900. +{
  2901. + struct surfacegen5_vhf_drvdata *drvdata;
  2902. + struct hid_device *hid;
  2903. + int status;
  2904. +
  2905. + // add device link to EC
  2906. + status = surfacegen5_ec_consumer_register(&pdev->dev);
  2907. + if (status) {
  2908. + return status == -ENXIO ? -EPROBE_DEFER : status;
  2909. + }
  2910. +
  2911. + drvdata = kzalloc(sizeof(struct surfacegen5_vhf_drvdata), GFP_KERNEL);
  2912. + if (!drvdata) {
  2913. + return -ENOMEM;
  2914. + }
  2915. +
  2916. + hid = surfacegen5_vhf_create_hid_device(pdev);
  2917. + if (IS_ERR(hid)) {
  2918. + status = PTR_ERR(hid);
  2919. + goto err_probe_hid;
  2920. + }
  2921. +
  2922. + status = hid_add_device(hid);
  2923. + if (status) {
  2924. + goto err_add_hid;
  2925. + }
  2926. +
  2927. + drvdata->event_ctx.dev = &pdev->dev;
  2928. + drvdata->event_ctx.hid = hid;
  2929. +
  2930. + platform_set_drvdata(pdev, drvdata);
  2931. +
  2932. + /*
  2933. + * Set event hanlder for VHF events. They seem to be enabled by
  2934. + * default, thus there should be no need to explicitly enable them.
  2935. + */
  2936. + status = surfacegen5_ec_set_delayed_event_handler(
  2937. + SG5_EVENT_VHF_RQID,
  2938. + surfacegen5_vhf_event_handler,
  2939. + surfacegen5_vhf_event_delay,
  2940. + &drvdata->event_ctx);
  2941. + if (status) {
  2942. + goto err_add_hid;
  2943. + }
  2944. +
  2945. + status = surfacegen5_ec_enable_event_source(SG5_EVENT_VHF_TC, 0x01, SG5_EVENT_VHF_RQID);
  2946. + if (status) {
  2947. + goto err_event_source;
  2948. + }
  2949. +
  2950. + return 0;
  2951. +
  2952. +err_event_source:
  2953. + surfacegen5_ec_remove_event_handler(SG5_EVENT_VHF_RQID);
  2954. +err_add_hid:
  2955. + hid_destroy_device(hid);
  2956. + platform_set_drvdata(pdev, NULL);
  2957. +err_probe_hid:
  2958. + kfree(drvdata);
  2959. + return status;
  2960. +}
  2961. +
  2962. +static int surfacegen5_acpi_vhf_remove(struct platform_device *pdev)
  2963. +{
  2964. + struct surfacegen5_vhf_drvdata *drvdata = platform_get_drvdata(pdev);
  2965. +
  2966. + surfacegen5_ec_disable_event_source(SG5_EVENT_VHF_TC, 0x01, SG5_EVENT_VHF_RQID);
  2967. + surfacegen5_ec_remove_event_handler(SG5_EVENT_VHF_RQID);
  2968. +
  2969. + hid_destroy_device(drvdata->event_ctx.hid);
  2970. + kfree(drvdata);
  2971. +
  2972. + platform_set_drvdata(pdev, NULL);
  2973. + return 0;
  2974. +}
  2975. +
  2976. +
  2977. +static const struct acpi_device_id surfacegen5_acpi_vhf_match[] = {
  2978. + { "MSHW0096" },
  2979. + { },
  2980. +};
  2981. +MODULE_DEVICE_TABLE(acpi, surfacegen5_acpi_vhf_match);
  2982. +
  2983. +struct platform_driver surfacegen5_acpi_vhf = {
  2984. + .probe = surfacegen5_acpi_vhf_probe,
  2985. + .remove = surfacegen5_acpi_vhf_remove,
  2986. + .driver = {
  2987. + .name = "surfacegen5_acpi_vhf",
  2988. + .acpi_match_table = ACPI_PTR(surfacegen5_acpi_vhf_match),
  2989. + },
  2990. +};
  2991. +
  2992. +
  2993. +inline int surfacegen5_acpi_vhf_register(void)
  2994. +{
  2995. + return platform_driver_register(&surfacegen5_acpi_vhf);
  2996. +}
  2997. +
  2998. +inline void surfacegen5_acpi_vhf_unregister(void)
  2999. +{
  3000. + platform_driver_unregister(&surfacegen5_acpi_vhf);
  3001. +}
  3002. +
  3003. +#else /* CONFIG_SURFACE_ACPI_VHF */
  3004. +
  3005. +inline int surfacegen5_acpi_vhf_register(void)
  3006. +{
  3007. + return 0;
  3008. +}
  3009. +
  3010. +inline void surfacegen5_acpi_vhf_unregister(void)
  3011. +{
  3012. +}
  3013. +
  3014. +#endif /* CONFIG_SURFACE_ACPI_VHF */
  3015. +
  3016. +
  3017. +/*************************************************************************
  3018. + * Detachment System Driver (DTX)
  3019. + */
  3020. +
  3021. +#ifdef CONFIG_SURFACE_ACPI_DTX
  3022. +
  3023. +#define SG5_DTX_INPUT_NAME "Microsoft Surface Base 2 Integration Device"
  3024. +
  3025. +#define DTX_CMD_LATCH_LOCK _IO(0x11, 0x01)
  3026. +#define DTX_CMD_LATCH_UNLOCK _IO(0x11, 0x02)
  3027. +#define DTX_CMD_LATCH_REQUEST _IO(0x11, 0x03)
  3028. +#define DTX_CMD_LATCH_OPEN _IO(0x11, 0x04)
  3029. +#define DTX_CMD_GET_OPMODE _IOR(0x11, 0x05, int)
  3030. +
  3031. +#define SG5_RQST_DTX_TC 0x11
  3032. +#define SG5_RQST_DTX_CID_LATCH_LOCK 0x06
  3033. +#define SG5_RQST_DTX_CID_LATCH_UNLOCK 0x07
  3034. +#define SG5_RQST_DTX_CID_LATCH_REQUEST 0x08
  3035. +#define SG5_RQST_DTX_CID_LATCH_OPEN 0x09
  3036. +#define SG5_RQST_DTX_CID_GET_OPMODE 0x0D
  3037. +
  3038. +#define SG5_EVENT_DTX_TC 0x11
  3039. +#define SG5_EVENT_DTX_RQID 0x0011
  3040. +#define SG5_EVENT_DTX_CID_CONNECTION 0x0c
  3041. +#define SG5_EVENT_DTX_CID_BUTTON 0x0e
  3042. +#define SG5_EVENT_DTX_CID_ERROR 0x0f
  3043. +#define SG5_EVENT_DTX_CID_LATCH_STATUS 0x11
  3044. +
  3045. +#define DTX_OPMODE_TABLET 0x00
  3046. +#define DTX_OPMODE_LAPTOP 0x01
  3047. +#define DTX_OPMODE_STUDIO 0x02
  3048. +
  3049. +#define DTX_LATCH_CLOSED 0x00
  3050. +#define DTX_LATCH_OPENED 0x01
  3051. +
  3052. +// Warning: This must always be a power of 2!
  3053. +#define SURFACE_DTX_CLIENT_BUF_SIZE 16
  3054. +
  3055. +#define SG5_DTX_CONNECT_OPMODE_DELAY 1000
  3056. +
  3057. +#define DTX_ERR KERN_ERR "surfacegen5_acpi_dtx: "
  3058. +#define DTX_WARN KERN_WARNING "surfacegen5_acpi_dtx: "
  3059. +
  3060. +
  3061. +struct surface_dtx_event {
  3062. + u8 type;
  3063. + u8 code;
  3064. + u8 arg0;
  3065. + u8 arg1;
  3066. +} __packed;
  3067. +
  3068. +struct surface_dtx_dev {
  3069. + wait_queue_head_t waitq;
  3070. + struct miscdevice mdev;
  3071. + spinlock_t client_lock;
  3072. + struct list_head client_list;
  3073. + struct mutex mutex;
  3074. + bool active;
  3075. + spinlock_t input_lock;
  3076. + struct input_dev *input_dev;
  3077. +};
  3078. +
  3079. +struct surface_dtx_client {
  3080. + struct list_head node;
  3081. + struct surface_dtx_dev *ddev;
  3082. + struct fasync_struct *fasync;
  3083. + spinlock_t buffer_lock;
  3084. + unsigned int buffer_head;
  3085. + unsigned int buffer_tail;
  3086. + struct surface_dtx_event buffer[SURFACE_DTX_CLIENT_BUF_SIZE];
  3087. +};
  3088. +
  3089. +
  3090. +static struct surface_dtx_dev surface_dtx_dev;
  3091. +
  3092. +
  3093. +static int sg5_ec_query_opmpde(void)
  3094. +{
  3095. + u8 result_buf[1];
  3096. + int status;
  3097. +
  3098. + struct surfacegen5_rqst rqst = {
  3099. + .tc = SG5_RQST_DTX_TC,
  3100. + .iid = 0,
  3101. + .cid = SG5_RQST_DTX_CID_GET_OPMODE,
  3102. + .snc = 1,
  3103. + .cdl = 0,
  3104. + .pld = NULL,
  3105. + };
  3106. +
  3107. + struct surfacegen5_buf result = {
  3108. + .cap = 1,
  3109. + .len = 0,
  3110. + .data = result_buf,
  3111. + };
  3112. +
  3113. + status = surfacegen5_ec_rqst(&rqst, &result);
  3114. + if (status) {
  3115. + return status;
  3116. + }
  3117. +
  3118. + if (result.len != 1) {
  3119. + return -EFAULT;
  3120. + }
  3121. +
  3122. + return result.data[0];
  3123. +}
  3124. +
  3125. +
  3126. +static int dtx_cmd_simple(u8 cid)
  3127. +{
  3128. + struct surfacegen5_rqst rqst = {
  3129. + .tc = SG5_RQST_DTX_TC,
  3130. + .iid = 0,
  3131. + .cid = cid,
  3132. + .snc = 0,
  3133. + .cdl = 0,
  3134. + .pld = NULL,
  3135. + };
  3136. +
  3137. + return surfacegen5_ec_rqst(&rqst, NULL);
  3138. +}
  3139. +
  3140. +static int dtx_cmd_get_opmode(int __user *buf)
  3141. +{
  3142. + int opmode = sg5_ec_query_opmpde();
  3143. + if (opmode < 0) {
  3144. + return opmode;
  3145. + }
  3146. +
  3147. + if (put_user(opmode, buf)) {
  3148. + return -EACCES;
  3149. + }
  3150. +
  3151. + return 0;
  3152. +}
  3153. +
  3154. +
  3155. +static int surface_dtx_open(struct inode *inode, struct file *file)
  3156. +{
  3157. + struct surface_dtx_dev *ddev = container_of(file->private_data, struct surface_dtx_dev, mdev);
  3158. + struct surface_dtx_client *client;
  3159. +
  3160. + // initialize client
  3161. + client = kzalloc(sizeof(struct surface_dtx_client), GFP_KERNEL);
  3162. + if (!client) {
  3163. + return -ENOMEM;
  3164. + }
  3165. +
  3166. + spin_lock_init(&client->buffer_lock);
  3167. + client->buffer_head = 0;
  3168. + client->buffer_tail = 0;
  3169. + client->ddev = ddev;
  3170. +
  3171. + // attach client
  3172. + spin_lock(&ddev->client_lock);
  3173. + list_add_tail_rcu(&client->node, &ddev->client_list);
  3174. + spin_unlock(&ddev->client_lock);
  3175. +
  3176. + file->private_data = client;
  3177. + nonseekable_open(inode, file);
  3178. +
  3179. + return 0;
  3180. +}
  3181. +
  3182. +static int surface_dtx_release(struct inode *inode, struct file *file)
  3183. +{
  3184. + struct surface_dtx_client *client = file->private_data;
  3185. +
  3186. + // detach client
  3187. + spin_lock(&client->ddev->client_lock);
  3188. + list_del_rcu(&client->node);
  3189. + spin_unlock(&client->ddev->client_lock);
  3190. + synchronize_rcu();
  3191. +
  3192. + kfree(client);
  3193. + file->private_data = NULL;
  3194. +
  3195. + return 0;
  3196. +}
  3197. +
  3198. +static ssize_t surface_dtx_read(struct file *file, char __user *buf, size_t count, loff_t *offs)
  3199. +{
  3200. + struct surface_dtx_client *client = file->private_data;
  3201. + struct surface_dtx_dev *ddev = client->ddev;
  3202. + struct surface_dtx_event event;
  3203. + size_t read = 0;
  3204. + int status = 0;
  3205. +
  3206. + if (count != 0 && count < sizeof(struct surface_dtx_event)) {
  3207. + return -EINVAL;
  3208. + }
  3209. +
  3210. + if (!ddev->active) {
  3211. + return -ENODEV;
  3212. + }
  3213. +
  3214. + // check availability
  3215. + if (client->buffer_head == client->buffer_tail){
  3216. + if (file->f_flags & O_NONBLOCK) {
  3217. + return -EAGAIN;
  3218. + }
  3219. +
  3220. + status = wait_event_interruptible(ddev->waitq,
  3221. + client->buffer_head != client->buffer_tail ||
  3222. + !ddev->active);
  3223. + if (status) {
  3224. + return status;
  3225. + }
  3226. +
  3227. + if (!ddev->active) {
  3228. + return -ENODEV;
  3229. + }
  3230. + }
  3231. +
  3232. + // copy events one by one
  3233. + while (read + sizeof(struct surface_dtx_event) <= count) {
  3234. + spin_lock_irq(&client->buffer_lock);
  3235. +
  3236. + if(client->buffer_head == client->buffer_tail) {
  3237. + spin_unlock_irq(&client->buffer_lock);
  3238. + break;
  3239. + }
  3240. +
  3241. + // get one event
  3242. + event = client->buffer[client->buffer_tail];
  3243. + client->buffer_tail = (client->buffer_tail + 1) & (SURFACE_DTX_CLIENT_BUF_SIZE - 1);
  3244. + spin_unlock_irq(&client->buffer_lock);
  3245. +
  3246. + // copy to userspace
  3247. + if(copy_to_user(buf, &event, sizeof(struct surface_dtx_event))) {
  3248. + return -EFAULT;
  3249. + }
  3250. +
  3251. + read += sizeof(struct surface_dtx_event);
  3252. + }
  3253. +
  3254. + return read;
  3255. +}
  3256. +
  3257. +static __poll_t surface_dtx_poll(struct file *file, struct poll_table_struct *pt)
  3258. +{
  3259. + struct surface_dtx_client *client = file->private_data;
  3260. + int mask;
  3261. +
  3262. + poll_wait(file, &client->ddev->waitq, pt);
  3263. +
  3264. + if (client->ddev->active) {
  3265. + mask = EPOLLOUT | EPOLLWRNORM;
  3266. + } else {
  3267. + mask = EPOLLHUP | EPOLLERR;
  3268. + }
  3269. +
  3270. + if (client->buffer_head != client->buffer_tail) {
  3271. + mask |= EPOLLIN | EPOLLRDNORM;
  3272. + }
  3273. +
  3274. + return mask;
  3275. +}
  3276. +
  3277. +static int surface_dtx_fasync(int fd, struct file *file, int on)
  3278. +{
  3279. + struct surface_dtx_client *client = file->private_data;
  3280. +
  3281. + return fasync_helper(fd, file, on, &client->fasync);
  3282. +}
  3283. +
  3284. +static long surface_dtx_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  3285. +{
  3286. + struct surface_dtx_client *client = file->private_data;
  3287. + struct surface_dtx_dev *ddev = client->ddev;
  3288. + int status;
  3289. +
  3290. + status = mutex_lock_interruptible(&ddev->mutex);
  3291. + if (status) {
  3292. + return status;
  3293. + }
  3294. +
  3295. + if (!ddev->active) {
  3296. + mutex_unlock(&ddev->mutex);
  3297. + return -ENODEV;
  3298. + }
  3299. +
  3300. + switch (cmd) {
  3301. + case DTX_CMD_LATCH_LOCK:
  3302. + status = dtx_cmd_simple(SG5_RQST_DTX_CID_LATCH_LOCK);
  3303. + break;
  3304. +
  3305. + case DTX_CMD_LATCH_UNLOCK:
  3306. + status = dtx_cmd_simple(SG5_RQST_DTX_CID_LATCH_UNLOCK);
  3307. + break;
  3308. +
  3309. + case DTX_CMD_LATCH_REQUEST:
  3310. + status = dtx_cmd_simple(SG5_RQST_DTX_CID_LATCH_REQUEST);
  3311. + break;
  3312. +
  3313. + case DTX_CMD_LATCH_OPEN:
  3314. + status = dtx_cmd_simple(SG5_RQST_DTX_CID_LATCH_OPEN);
  3315. + break;
  3316. +
  3317. + case DTX_CMD_GET_OPMODE:
  3318. + status = dtx_cmd_get_opmode((int __user *)arg);
  3319. + break;
  3320. +
  3321. + default:
  3322. + status = -EINVAL;
  3323. + break;
  3324. + }
  3325. +
  3326. + mutex_unlock(&ddev->mutex);
  3327. + return status;
  3328. +}
  3329. +
  3330. +static const struct file_operations surface_dtx_fops = {
  3331. + .owner = THIS_MODULE,
  3332. + .open = surface_dtx_open,
  3333. + .release = surface_dtx_release,
  3334. + .read = surface_dtx_read,
  3335. + .poll = surface_dtx_poll,
  3336. + .fasync = surface_dtx_fasync,
  3337. + .unlocked_ioctl = surface_dtx_ioctl,
  3338. + .llseek = no_llseek,
  3339. +};
  3340. +
  3341. +static struct surface_dtx_dev surface_dtx_dev = {
  3342. + .mdev = {
  3343. + .minor = MISC_DYNAMIC_MINOR,
  3344. + .name = "surface_dtx",
  3345. + .fops = &surface_dtx_fops,
  3346. + },
  3347. + .client_lock = __SPIN_LOCK_UNLOCKED(),
  3348. + .input_lock = __SPIN_LOCK_UNLOCKED(),
  3349. + .mutex = __MUTEX_INITIALIZER(surface_dtx_dev.mutex),
  3350. + .active = false,
  3351. +};
  3352. +
  3353. +
  3354. +static void surface_dtx_push_event(struct surface_dtx_dev *ddev, struct surface_dtx_event *event)
  3355. +{
  3356. + struct surface_dtx_client *client;
  3357. +
  3358. + rcu_read_lock();
  3359. + list_for_each_entry_rcu(client, &ddev->client_list, node) {
  3360. + spin_lock(&client->buffer_lock);
  3361. +
  3362. + client->buffer[client->buffer_head++] = *event;
  3363. + client->buffer_head &= SURFACE_DTX_CLIENT_BUF_SIZE - 1;
  3364. +
  3365. + if (unlikely(client->buffer_head == client->buffer_tail)) {
  3366. + printk(DTX_WARN "event buffer overrun\n");
  3367. + client->buffer_tail = (client->buffer_tail + 1) & (SURFACE_DTX_CLIENT_BUF_SIZE - 1);
  3368. + }
  3369. +
  3370. + spin_unlock(&client->buffer_lock);
  3371. +
  3372. + kill_fasync(&client->fasync, SIGIO, POLL_IN);
  3373. + }
  3374. + rcu_read_unlock();
  3375. +
  3376. + wake_up_interruptible(&ddev->waitq);
  3377. +}
  3378. +
  3379. +
  3380. +static void surface_dtx_update_opmpde(struct surface_dtx_dev *ddev)
  3381. +{
  3382. + struct surface_dtx_event event;
  3383. + int opmode;
  3384. +
  3385. + // get operation mode
  3386. + opmode = sg5_ec_query_opmpde();
  3387. + if (opmode < 0) {
  3388. + printk(DTX_ERR "EC request failed with error %d\n", opmode);
  3389. + }
  3390. +
  3391. + // send DTX event
  3392. + event.type = 0x11;
  3393. + event.code = 0x0D;
  3394. + event.arg0 = opmode;
  3395. + event.arg1 = 0x00;
  3396. +
  3397. + surface_dtx_push_event(ddev, &event);
  3398. +
  3399. + // send SW_TABLET_MODE event
  3400. + spin_lock(&ddev->input_lock);
  3401. + input_report_switch(ddev->input_dev, SW_TABLET_MODE, opmode == 0x00);
  3402. + input_sync(ddev->input_dev);
  3403. + spin_unlock(&ddev->input_lock);
  3404. +}
  3405. +
  3406. +static int surface_dtx_evt_dtx(struct surfacegen5_event *in_event, void *data)
  3407. +{
  3408. + struct surface_dtx_dev *ddev = data;
  3409. + struct surface_dtx_event event;
  3410. +
  3411. + switch (in_event->cid) {
  3412. + case SG5_EVENT_DTX_CID_CONNECTION:
  3413. + case SG5_EVENT_DTX_CID_BUTTON:
  3414. + case SG5_EVENT_DTX_CID_ERROR:
  3415. + case SG5_EVENT_DTX_CID_LATCH_STATUS:
  3416. + if (in_event->len > 2) {
  3417. + printk(DTX_ERR "unexpected payload size (cid: %x, len: %u)\n",
  3418. + in_event->cid, in_event->len);
  3419. + return 0;
  3420. + }
  3421. +
  3422. + event.type = in_event->tc;
  3423. + event.code = in_event->cid;
  3424. + event.arg0 = in_event->len >= 1 ? in_event->pld[0] : 0x00;
  3425. + event.arg1 = in_event->len >= 2 ? in_event->pld[1] : 0x00;
  3426. + surface_dtx_push_event(ddev, &event);
  3427. + break;
  3428. +
  3429. + default:
  3430. + printk(DTX_WARN "unhandled dtx event (cid: %x)\n", in_event->cid);
  3431. + }
  3432. +
  3433. + // update device mode
  3434. + if (in_event->cid == SG5_EVENT_DTX_CID_CONNECTION) {
  3435. + if (in_event->pld[0]) {
  3436. + // Note: we're already in a workqueue task
  3437. + msleep(SG5_DTX_CONNECT_OPMODE_DELAY);
  3438. + }
  3439. +
  3440. + surface_dtx_update_opmpde(ddev);
  3441. + }
  3442. +
  3443. + return 0;
  3444. +}
  3445. +
  3446. +static int surface_dtx_events_setup(struct surface_dtx_dev *ddev)
  3447. +{
  3448. + int status;
  3449. +
  3450. + status = surfacegen5_ec_set_event_handler(SG5_EVENT_DTX_RQID, surface_dtx_evt_dtx, ddev);
  3451. + if (status) {
  3452. + goto err_event_handler;
  3453. + }
  3454. +
  3455. + status = surfacegen5_ec_enable_event_source(SG5_EVENT_DTX_TC, 0x01, SG5_EVENT_DTX_RQID);
  3456. + if (status) {
  3457. + goto err_event_source;
  3458. + }
  3459. +
  3460. + return 0;
  3461. +
  3462. +err_event_source:
  3463. + surfacegen5_ec_remove_event_handler(SG5_EVENT_DTX_RQID);
  3464. +err_event_handler:
  3465. + return status;
  3466. +}
  3467. +
  3468. +static void surface_dtx_events_disable(void)
  3469. +{
  3470. + surfacegen5_ec_disable_event_source(SG5_EVENT_DTX_TC, 0x01, SG5_EVENT_DTX_RQID);
  3471. + surfacegen5_ec_remove_event_handler(SG5_EVENT_DTX_RQID);
  3472. +}
  3473. +
  3474. +
  3475. +static struct input_dev *surface_dtx_register_inputdev(struct platform_device *pdev)
  3476. +{
  3477. + struct input_dev *input_dev;
  3478. + int status;
  3479. +
  3480. + input_dev = input_allocate_device();
  3481. + if (!input_dev) {
  3482. + return ERR_PTR(-ENOMEM);
  3483. + }
  3484. +
  3485. + input_dev->name = SG5_DTX_INPUT_NAME;
  3486. + input_dev->dev.parent = &pdev->dev;
  3487. + input_dev->id.bustype = BUS_VIRTUAL;
  3488. + input_dev->id.vendor = USB_VENDOR_ID_MICROSOFT;
  3489. + input_dev->id.product = USB_DEVICE_ID_MS_SURFACE_BASE_2_INTEGRATION;
  3490. +
  3491. + input_set_capability(input_dev, EV_SW, SW_TABLET_MODE);
  3492. +
  3493. + status = sg5_ec_query_opmpde();
  3494. + if (status < 0) {
  3495. + input_free_device(input_dev);
  3496. + return ERR_PTR(status);
  3497. + }
  3498. +
  3499. + input_report_switch(input_dev, SW_TABLET_MODE, status == 0x00);
  3500. +
  3501. + status = input_register_device(input_dev);
  3502. + if (status) {
  3503. + input_unregister_device(input_dev);
  3504. + return ERR_PTR(status);
  3505. + }
  3506. +
  3507. + return input_dev;
  3508. +}
  3509. +
  3510. +
  3511. +static int surfacegen5_acpi_dtx_probe(struct platform_device *pdev)
  3512. +{
  3513. + struct surface_dtx_dev *ddev = &surface_dtx_dev;
  3514. + struct input_dev *input_dev;
  3515. + int status;
  3516. +
  3517. + // link to ec
  3518. + status = surfacegen5_ec_consumer_register(&pdev->dev);
  3519. + if (status) {
  3520. + return status == -ENXIO ? -EPROBE_DEFER : status;
  3521. + }
  3522. +
  3523. + input_dev = surface_dtx_register_inputdev(pdev);
  3524. + if (IS_ERR(input_dev)) {
  3525. + return PTR_ERR(input_dev);
  3526. + }
  3527. +
  3528. + // initialize device
  3529. + mutex_lock(&ddev->mutex);
  3530. + if (ddev->active) {
  3531. + mutex_unlock(&ddev->mutex);
  3532. + status = -ENODEV;
  3533. + goto err_register;
  3534. + }
  3535. +
  3536. + INIT_LIST_HEAD(&ddev->client_list);
  3537. + init_waitqueue_head(&ddev->waitq);
  3538. + ddev->active = true;
  3539. + ddev->input_dev = input_dev;
  3540. + mutex_unlock(&ddev->mutex);
  3541. +
  3542. + status = misc_register(&ddev->mdev);
  3543. + if (status) {
  3544. + goto err_register;
  3545. + }
  3546. +
  3547. + // enable events
  3548. + status = surface_dtx_events_setup(ddev);
  3549. + if (status) {
  3550. + goto err_events_setup;
  3551. + }
  3552. +
  3553. + return 0;
  3554. +
  3555. +err_events_setup:
  3556. + misc_deregister(&ddev->mdev);
  3557. +err_register:
  3558. + input_unregister_device(ddev->input_dev);
  3559. + return status;
  3560. +}
  3561. +
  3562. +static int surfacegen5_acpi_dtx_remove(struct platform_device *pdev)
  3563. +{
  3564. + struct surface_dtx_dev *ddev = &surface_dtx_dev;
  3565. + struct surface_dtx_client *client;
  3566. +
  3567. + mutex_lock(&ddev->mutex);
  3568. + if (!ddev->active) {
  3569. + mutex_unlock(&ddev->mutex);
  3570. + return 0;
  3571. + }
  3572. +
  3573. + // mark as inactive
  3574. + ddev->active = false;
  3575. + mutex_unlock(&ddev->mutex);
  3576. +
  3577. + // After this call we're guaranteed that no more input events will arive
  3578. + surface_dtx_events_disable();
  3579. +
  3580. + // wake up clients
  3581. + spin_lock(&ddev->client_lock);
  3582. + list_for_each_entry(client, &ddev->client_list, node) {
  3583. + kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  3584. + }
  3585. + spin_unlock(&ddev->client_lock);
  3586. +
  3587. + wake_up_interruptible(&ddev->waitq);
  3588. +
  3589. + // unregister user-space devices
  3590. + input_unregister_device(ddev->input_dev);
  3591. + misc_deregister(&ddev->mdev);
  3592. +
  3593. + return 0;
  3594. +}
  3595. +
  3596. +
  3597. +static const struct acpi_device_id surfacegen5_acpi_dtx_match[] = {
  3598. + { "MSHW0133", 0 },
  3599. + { },
  3600. +};
  3601. +MODULE_DEVICE_TABLE(acpi, surfacegen5_acpi_dtx_match);
  3602. +
  3603. +struct platform_driver surfacegen5_acpi_dtx = {
  3604. + .probe = surfacegen5_acpi_dtx_probe,
  3605. + .remove = surfacegen5_acpi_dtx_remove,
  3606. + .driver = {
  3607. + .name = "surfacegen5_acpi_dtx",
  3608. + .acpi_match_table = ACPI_PTR(surfacegen5_acpi_dtx_match),
  3609. + },
  3610. +};
  3611. +
  3612. +
  3613. +inline int surfacegen5_acpi_dtx_register(void)
  3614. +{
  3615. + return platform_driver_register(&surfacegen5_acpi_dtx);
  3616. +}
  3617. +
  3618. +inline void surfacegen5_acpi_dtx_unregister(void)
  3619. +{
  3620. + platform_driver_unregister(&surfacegen5_acpi_dtx);
  3621. +}
  3622. +
  3623. +#else /* CONFIG_SURFACE_ACPI_DTX */
  3624. +
  3625. +inline int surfacegen5_acpi_dtx_register(void)
  3626. +{
  3627. + return 0;
  3628. +}
  3629. +
  3630. +inline void surfacegen5_acpi_dtx_unregister(void)
  3631. +{
  3632. +}
  3633. +
  3634. +#endif /* CONFIG_SURFACE_ACPI_DTX */
  3635. +
  3636. +
  3637. +/*************************************************************************
  3638. + * Surface Platform Integration Driver
  3639. + */
  3640. +
  3641. +#ifdef CONFIG_SURFACE_ACPI_SID
  3642. +
  3643. +struct si_lid_device {
  3644. + const char *acpi_path;
  3645. + const u32 gpe_number;
  3646. +};
  3647. +
  3648. +struct si_device_info {
  3649. + const bool has_perf_mode;
  3650. + const struct si_lid_device *lid_device;
  3651. +};
  3652. +
  3653. +
  3654. +static const struct si_lid_device lid_device_l17 = {
  3655. + .acpi_path = "\\_SB.LID0",
  3656. + .gpe_number = 0x17,
  3657. +};
  3658. +
  3659. +static const struct si_lid_device lid_device_l4F = {
  3660. + .acpi_path = "\\_SB.LID0",
  3661. + .gpe_number = 0x4F,
  3662. +};
  3663. +
  3664. +static const struct si_lid_device lid_device_l57 = {
  3665. + .acpi_path = "\\_SB.LID0",
  3666. + .gpe_number = 0x57,
  3667. +};
  3668. +
  3669. +
  3670. +static const struct si_device_info si_device_pro_4 = {
  3671. + .has_perf_mode = false,
  3672. + .lid_device = &lid_device_l17,
  3673. +};
  3674. +
  3675. +static const struct si_device_info si_device_pro_5 = {
  3676. + .has_perf_mode = false,
  3677. + .lid_device = &lid_device_l4F,
  3678. +};
  3679. +
  3680. +static const struct si_device_info si_device_pro_6 = {
  3681. + .has_perf_mode = false,
  3682. + .lid_device = &lid_device_l4F,
  3683. +};
  3684. +
  3685. +static const struct si_device_info si_device_book_1 = {
  3686. + .has_perf_mode = false,
  3687. + .lid_device = &lid_device_l17,
  3688. +};
  3689. +
  3690. +static const struct si_device_info si_device_book_2 = {
  3691. + .has_perf_mode = true,
  3692. + .lid_device = &lid_device_l17,
  3693. +};
  3694. +
  3695. +static const struct si_device_info si_device_laptop_1 = {
  3696. + .has_perf_mode = false,
  3697. + .lid_device = &lid_device_l57,
  3698. +};
  3699. +
  3700. +static const struct si_device_info si_device_laptop_2 = {
  3701. + .has_perf_mode = false,
  3702. + .lid_device = &lid_device_l57,
  3703. +};
  3704. +
  3705. +
  3706. +static const struct dmi_system_id dmi_lid_device_table[] = {
  3707. + {
  3708. + .ident = "Surface Pro 4",
  3709. + .matches = {
  3710. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3711. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 4"),
  3712. + },
  3713. + .driver_data = (void *)&si_device_pro_4,
  3714. + },
  3715. + {
  3716. + .ident = "Surface Pro 5",
  3717. + .matches = {
  3718. + /* match for SKU here due to generic product name "Surface Pro" */
  3719. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3720. + DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Pro_1796"),
  3721. + },
  3722. + .driver_data = (void *)&si_device_pro_5,
  3723. + },
  3724. + {
  3725. + .ident = "Surface Pro 5 (LTE)",
  3726. + .matches = {
  3727. + /* match for SKU here due to generic product name "Surface Pro" */
  3728. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3729. + DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Pro_1807"),
  3730. + },
  3731. + .driver_data = (void *)&si_device_pro_5,
  3732. + },
  3733. + {
  3734. + .ident = "Surface Pro 6",
  3735. + .matches = {
  3736. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3737. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 6"),
  3738. + },
  3739. + .driver_data = (void *)&si_device_pro_6,
  3740. + },
  3741. + {
  3742. + .ident = "Surface Book 1",
  3743. + .matches = {
  3744. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3745. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book"),
  3746. + },
  3747. + .driver_data = (void *)&si_device_book_1,
  3748. + },
  3749. + {
  3750. + .ident = "Surface Book 2",
  3751. + .matches = {
  3752. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3753. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 2"),
  3754. + },
  3755. + .driver_data = (void *)&si_device_book_2,
  3756. + },
  3757. + {
  3758. + .ident = "Surface Laptop 1",
  3759. + .matches = {
  3760. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3761. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop"),
  3762. + },
  3763. + .driver_data = (void *)&si_device_laptop_1,
  3764. + },
  3765. + {
  3766. + .ident = "Surface Laptop 2",
  3767. + .matches = {
  3768. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3769. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop 2"),
  3770. + },
  3771. + .driver_data = (void *)&si_device_laptop_2,
  3772. + },
  3773. + { }
  3774. +};
  3775. +
  3776. +#define SG5_PARAM_PERM (S_IRUGO | S_IWUSR)
  3777. +
  3778. +enum sg5_perf_mode {
  3779. + SG5_PERF_MODE_NORMAL = 1,
  3780. + SG5_PERF_MODE_BATTERY = 2,
  3781. + SG5_PERF_MODE_PERF1 = 3,
  3782. + SG5_PERF_MODE_PERF2 = 4,
  3783. +
  3784. + __SG5_PERF_MODE__START = 1,
  3785. + __SG5_PERF_MODE__END = 4,
  3786. +};
  3787. +
  3788. +enum sg5_param_perf_mode {
  3789. + SG5_PARAM_PERF_MODE_AS_IS = 0,
  3790. + SG5_PARAM_PERF_MODE_NORMAL = SG5_PERF_MODE_NORMAL,
  3791. + SG5_PARAM_PERF_MODE_BATTERY = SG5_PERF_MODE_BATTERY,
  3792. + SG5_PARAM_PERF_MODE_PERF1 = SG5_PERF_MODE_PERF1,
  3793. + SG5_PARAM_PERF_MODE_PERF2 = SG5_PERF_MODE_PERF2,
  3794. +
  3795. + __SG5_PARAM_PERF_MODE__START = 0,
  3796. + __SG5_PARAM_PERF_MODE__END = 4,
  3797. +};
  3798. +
  3799. +
  3800. +static int sg5_ec_perf_mode_get(void)
  3801. +{
  3802. + u8 result_buf[8] = { 0 };
  3803. + int status;
  3804. +
  3805. + struct surfacegen5_rqst rqst = {
  3806. + .tc = 0x03,
  3807. + .iid = 0x00,
  3808. + .cid = 0x02,
  3809. + .snc = 0x01,
  3810. + .cdl = 0x00,
  3811. + .pld = NULL,
  3812. + };
  3813. +
  3814. + struct surfacegen5_buf result = {
  3815. + .cap = ARRAY_SIZE(result_buf),
  3816. + .len = 0,
  3817. + .data = result_buf,
  3818. + };
  3819. +
  3820. + status = surfacegen5_ec_rqst(&rqst, &result);
  3821. + if (status) {
  3822. + return status;
  3823. + }
  3824. +
  3825. + if (result.len != 8) {
  3826. + return -EFAULT;
  3827. + }
  3828. +
  3829. + return get_unaligned_le32(&result.data[0]);
  3830. +}
  3831. +
  3832. +static int sg5_ec_perf_mode_set(int perf_mode)
  3833. +{
  3834. + u8 payload[4] = { 0 };
  3835. +
  3836. + struct surfacegen5_rqst rqst = {
  3837. + .tc = 0x03,
  3838. + .iid = 0x00,
  3839. + .cid = 0x03,
  3840. + .snc = 0x00,
  3841. + .cdl = ARRAY_SIZE(payload),
  3842. + .pld = payload,
  3843. + };
  3844. +
  3845. + if (perf_mode < __SG5_PERF_MODE__START || perf_mode > __SG5_PERF_MODE__END) {
  3846. + return -EINVAL;
  3847. + }
  3848. +
  3849. + put_unaligned_le32(perf_mode, &rqst.pld[0]);
  3850. + return surfacegen5_ec_rqst(&rqst, NULL);
  3851. +}
  3852. +
  3853. +
  3854. +static int param_perf_mode_set(const char *val, const struct kernel_param *kp)
  3855. +{
  3856. + int perf_mode;
  3857. + int status;
  3858. +
  3859. + status = kstrtoint(val, 0, &perf_mode);
  3860. + if (status) {
  3861. + return status;
  3862. + }
  3863. +
  3864. + if (perf_mode < __SG5_PARAM_PERF_MODE__START || perf_mode > __SG5_PARAM_PERF_MODE__END) {
  3865. + return -EINVAL;
  3866. + }
  3867. +
  3868. + return param_set_int(val, kp);
  3869. +}
  3870. +
  3871. +static const struct kernel_param_ops param_perf_mode_ops = {
  3872. + .set = param_perf_mode_set,
  3873. + .get = param_get_int,
  3874. +};
  3875. +
  3876. +static int param_perf_mode_init = SG5_PARAM_PERF_MODE_AS_IS;
  3877. +static int param_perf_mode_exit = SG5_PARAM_PERF_MODE_AS_IS;
  3878. +
  3879. +module_param_cb(perf_mode_init, &param_perf_mode_ops, &param_perf_mode_init, SG5_PARAM_PERM);
  3880. +module_param_cb(perf_mode_exit, &param_perf_mode_ops, &param_perf_mode_exit, SG5_PARAM_PERM);
  3881. +
  3882. +MODULE_PARM_DESC(perf_mode_init, "Performance-mode to be set on module initialization");
  3883. +MODULE_PARM_DESC(perf_mode_exit, "Performance-mode to be set on module exit");
  3884. +
  3885. +static ssize_t perf_mode_show(struct device *dev, struct device_attribute *attr, char *data)
  3886. +{
  3887. + int perf_mode;
  3888. +
  3889. + perf_mode = sg5_ec_perf_mode_get();
  3890. + if (perf_mode < 0) {
  3891. + dev_err(dev, "failed to get current performance mode: %d", perf_mode);
  3892. + return -EIO;
  3893. + }
  3894. +
  3895. + return sprintf(data, "%d\n", perf_mode);
  3896. +}
  3897. +
  3898. +static ssize_t perf_mode_store(struct device *dev, struct device_attribute *attr,
  3899. + const char *data, size_t count)
  3900. +{
  3901. + int perf_mode;
  3902. + int status;
  3903. +
  3904. + status = kstrtoint(data, 0, &perf_mode);
  3905. + if (status) {
  3906. + return status;
  3907. + }
  3908. +
  3909. + status = sg5_ec_perf_mode_set(perf_mode);
  3910. + if (status) {
  3911. + return status;
  3912. + }
  3913. +
  3914. + // TODO: Should we notify ACPI here?
  3915. + //
  3916. + // There is a _DSM call described as
  3917. + // WSID._DSM: Notify DPTF on Slider State change
  3918. + // which calls
  3919. + // ODV3 = ToInteger (Arg3)
  3920. + // Notify(IETM, 0x88)
  3921. + // IETM is an INT3400 Intel Dynamic Power Performance Management
  3922. + // device, part of the DPTF framework. From the corresponding
  3923. + // kernel driver, it looks like event 0x88 is being ignored. Also
  3924. + // it is currently unknown what the consequecnes of setting ODV3
  3925. + // are.
  3926. +
  3927. + return count;
  3928. +}
  3929. +
  3930. +const static DEVICE_ATTR_RW(perf_mode);
  3931. +
  3932. +static int sid_perf_mode_setup(struct platform_device *pdev, const struct si_device_info *info)
  3933. +{
  3934. + int status;
  3935. +
  3936. + if (!info->has_perf_mode)
  3937. + return 0;
  3938. +
  3939. + // link to ec
  3940. + status = surfacegen5_ec_consumer_register(&pdev->dev);
  3941. + if (status) {
  3942. + return status == -ENXIO ? -EPROBE_DEFER : status;
  3943. + }
  3944. +
  3945. + // set initial perf_mode
  3946. + if (param_perf_mode_init != SG5_PARAM_PERF_MODE_AS_IS) {
  3947. + status = sg5_ec_perf_mode_set(param_perf_mode_init);
  3948. + if (status) {
  3949. + return status;
  3950. + }
  3951. + }
  3952. +
  3953. + // register perf_mode attribute
  3954. + status = sysfs_create_file(&pdev->dev.kobj, &dev_attr_perf_mode.attr);
  3955. + if (status) {
  3956. + goto err_sysfs;
  3957. + }
  3958. +
  3959. + return 0;
  3960. +
  3961. +err_sysfs:
  3962. + sg5_ec_perf_mode_set(param_perf_mode_exit);
  3963. + return status;
  3964. +}
  3965. +
  3966. +static void sid_perf_mode_remove(struct platform_device *pdev, const struct si_device_info *info)
  3967. +{
  3968. + if (!info->has_perf_mode)
  3969. + return;
  3970. +
  3971. + // remove perf_mode attribute
  3972. + sysfs_remove_file(&pdev->dev.kobj, &dev_attr_perf_mode.attr);
  3973. +
  3974. + // set exit perf_mode
  3975. + sg5_ec_perf_mode_set(param_perf_mode_exit);
  3976. +}
  3977. +
  3978. +
  3979. +static int sid_lid_enable_wakeup(const struct si_device_info *info, bool enable)
  3980. +{
  3981. + int action = enable ? ACPI_GPE_ENABLE : ACPI_GPE_DISABLE;
  3982. + int status;
  3983. +
  3984. + if (!info->lid_device)
  3985. + return 0;
  3986. +
  3987. + status = acpi_set_gpe_wake_mask(NULL, info->lid_device->gpe_number, action);
  3988. + if (status)
  3989. + return -EFAULT;
  3990. +
  3991. + return 0;
  3992. +}
  3993. +
  3994. +static int sid_lid_device_setup(const struct si_device_info *info)
  3995. +{
  3996. + acpi_handle lid_handle;
  3997. + int status;
  3998. +
  3999. + if (!info->lid_device)
  4000. + return 0;
  4001. +
  4002. + status = acpi_get_handle(NULL, (acpi_string)info->lid_device->acpi_path, &lid_handle);
  4003. + if (status)
  4004. + return -EFAULT;
  4005. +
  4006. + status = acpi_setup_gpe_for_wake(lid_handle, NULL, info->lid_device->gpe_number);
  4007. + if (status)
  4008. + return -EFAULT;
  4009. +
  4010. + status = acpi_enable_gpe(NULL, info->lid_device->gpe_number);
  4011. + if (status)
  4012. + return -EFAULT;
  4013. +
  4014. + return sid_lid_enable_wakeup(info, false);
  4015. +}
  4016. +
  4017. +static void sid_lid_device_remove(const struct si_device_info *info)
  4018. +{
  4019. + /* restore default behavior without this module */
  4020. + sid_lid_enable_wakeup(info, false);
  4021. +}
  4022. +
  4023. +
  4024. +static int surfacegen5_acpi_sid_suspend(struct device *dev)
  4025. +{
  4026. + const struct si_device_info *info = dev_get_drvdata(dev);
  4027. + return sid_lid_enable_wakeup(info, true);
  4028. +}
  4029. +
  4030. +static int surfacegen5_acpi_sid_resume(struct device *dev)
  4031. +{
  4032. + const struct si_device_info *info = dev_get_drvdata(dev);
  4033. + return sid_lid_enable_wakeup(info, false);
  4034. +}
  4035. +
  4036. +static SIMPLE_DEV_PM_OPS(surfacegen5_acpi_sid_pm, surfacegen5_acpi_sid_suspend, surfacegen5_acpi_sid_resume);
  4037. +
  4038. +
  4039. +static int surfacegen5_acpi_sid_probe(struct platform_device *pdev)
  4040. +{
  4041. + const struct dmi_system_id *dmi_match;
  4042. + struct si_device_info *info;
  4043. + int status;
  4044. +
  4045. + dmi_match = dmi_first_match(dmi_lid_device_table);
  4046. + if (!dmi_match)
  4047. + return -ENODEV;
  4048. +
  4049. + info = dmi_match->driver_data;
  4050. +
  4051. + platform_set_drvdata(pdev, info);
  4052. +
  4053. + status = sid_perf_mode_setup(pdev, info);
  4054. + if (status)
  4055. + goto err_perf_mode;
  4056. +
  4057. + status = sid_lid_device_setup(info);
  4058. + if (status)
  4059. + goto err_lid;
  4060. +
  4061. + return 0;
  4062. +
  4063. +err_lid:
  4064. + sid_perf_mode_remove(pdev, info);
  4065. +err_perf_mode:
  4066. + return status;
  4067. +}
  4068. +
  4069. +static int surfacegen5_acpi_sid_remove(struct platform_device *pdev)
  4070. +{
  4071. + const struct si_device_info *info = platform_get_drvdata(pdev);
  4072. +
  4073. + sid_perf_mode_remove(pdev, info);
  4074. + sid_lid_device_remove(info);
  4075. +
  4076. + platform_set_drvdata(pdev, NULL);
  4077. + return 0;
  4078. +}
  4079. +
  4080. +static const struct acpi_device_id surfacegen5_acpi_sid_match[] = {
  4081. + { "MSHW0081", }, /* Surface Pro 4, 5, and 6 */
  4082. + { "MSHW0080", }, /* Surface Book 1 */
  4083. + { "MSHW0107", }, /* Surface Book 2 */
  4084. + { "MSHW0086", }, /* Surface Laptop 1 */
  4085. + { "MSHW0112", }, /* Surface Laptop 2 */
  4086. + { },
  4087. +};
  4088. +MODULE_DEVICE_TABLE(acpi, surfacegen5_acpi_sid_match);
  4089. +
  4090. +struct platform_driver surfacegen5_acpi_sid = {
  4091. + .probe = surfacegen5_acpi_sid_probe,
  4092. + .remove = surfacegen5_acpi_sid_remove,
  4093. + .driver = {
  4094. + .name = "surfacegen5_acpi_sid",
  4095. + .acpi_match_table = ACPI_PTR(surfacegen5_acpi_sid_match),
  4096. + .pm = &surfacegen5_acpi_sid_pm,
  4097. + },
  4098. +};
  4099. +
  4100. +inline int surfacegen5_acpi_sid_register(void)
  4101. +{
  4102. + return platform_driver_register(&surfacegen5_acpi_sid);
  4103. +}
  4104. +
  4105. +inline void surfacegen5_acpi_sid_unregister(void)
  4106. +{
  4107. + platform_driver_unregister(&surfacegen5_acpi_sid);
  4108. +}
  4109. +
  4110. +#else /* CONFIG_SURFACE_ACPI_SID */
  4111. +
  4112. +inline int surfacegen5_acpi_sid_register(void)
  4113. +{
  4114. + return 0;
  4115. +}
  4116. +
  4117. +inline void surfacegen5_acpi_sid_unregister(void)
  4118. +{
  4119. +}
  4120. +
  4121. +#endif /* CONFIG_SURFACE_ACPI_SID */
  4122. +
  4123. +
  4124. +/*************************************************************************
  4125. + * Module initialization
  4126. + */
  4127. +
  4128. +int __init surface_acpi_init(void)
  4129. +{
  4130. + int status;
  4131. +
  4132. + status = surfacegen5_acpi_ssh_register();
  4133. + if (status) {
  4134. + goto err_ssh;
  4135. + }
  4136. +
  4137. + status = surfacegen5_acpi_san_register();
  4138. + if (status) {
  4139. + goto err_san;
  4140. + }
  4141. +
  4142. + status = surfacegen5_acpi_vhf_register();
  4143. + if (status) {
  4144. + goto err_vhf;
  4145. + }
  4146. +
  4147. + status = surfacegen5_acpi_dtx_register();
  4148. + if (status) {
  4149. + goto err_dtx;
  4150. + }
  4151. +
  4152. + status = surfacegen5_acpi_sid_register();
  4153. + if (status) {
  4154. + goto err_sid;
  4155. + }
  4156. +
  4157. + return 0;
  4158. +
  4159. +err_sid:
  4160. + surfacegen5_acpi_sid_unregister();
  4161. +err_dtx:
  4162. + surfacegen5_acpi_vhf_unregister();
  4163. +err_vhf:
  4164. + surfacegen5_acpi_san_unregister();
  4165. +err_san:
  4166. + surfacegen5_acpi_ssh_unregister();
  4167. +err_ssh:
  4168. + return status;
  4169. +}
  4170. +
  4171. +void __exit surface_acpi_exit(void)
  4172. +{
  4173. + surfacegen5_acpi_sid_unregister();
  4174. + surfacegen5_acpi_dtx_unregister();
  4175. + surfacegen5_acpi_vhf_unregister();
  4176. + surfacegen5_acpi_san_unregister();
  4177. + surfacegen5_acpi_ssh_unregister();
  4178. +}
  4179. +
  4180. +module_init(surface_acpi_init)
  4181. +module_exit(surface_acpi_exit)
  4182. +
  4183. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  4184. +MODULE_DESCRIPTION("ACPI/Platform Drivers for Microsoft Surface Devices");
  4185. +MODULE_LICENSE("GPL v2");
  4186. diff --git a/drivers/tty/serdev/core.c b/drivers/tty/serdev/core.c
  4187. index a0ac16ee6575..226adeec2aed 100644
  4188. --- a/drivers/tty/serdev/core.c
  4189. +++ b/drivers/tty/serdev/core.c
  4190. @@ -552,16 +552,97 @@ static int of_serdev_register_devices(struct serdev_controller *ctrl)
  4191. }
  4192. #ifdef CONFIG_ACPI
  4193. +
  4194. +#define SERDEV_ACPI_MAX_SCAN_DEPTH 32
  4195. +
  4196. +struct acpi_serdev_lookup {
  4197. + acpi_handle device_handle;
  4198. + acpi_handle controller_handle;
  4199. + int n;
  4200. + int index;
  4201. +};
  4202. +
  4203. +static int acpi_serdev_parse_resource(struct acpi_resource *ares, void *data)
  4204. +{
  4205. + struct acpi_serdev_lookup *lookup = data;
  4206. + struct acpi_resource_uart_serialbus *sb;
  4207. + acpi_status status;
  4208. +
  4209. + if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
  4210. + return 1;
  4211. +
  4212. + if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
  4213. + return 1;
  4214. +
  4215. + if (lookup->index != -1 && lookup->n++ != lookup->index)
  4216. + return 1;
  4217. +
  4218. + sb = &ares->data.uart_serial_bus;
  4219. +
  4220. + status = acpi_get_handle(lookup->device_handle,
  4221. + sb->resource_source.string_ptr,
  4222. + &lookup->controller_handle);
  4223. + if (ACPI_FAILURE(status))
  4224. + return 1;
  4225. +
  4226. + /*
  4227. + * NOTE: Ideally, we would also want to retreive other properties here,
  4228. + * once setting them before opening the device is supported by serdev.
  4229. + */
  4230. +
  4231. + return 1;
  4232. +}
  4233. +
  4234. +static int acpi_serdev_do_lookup(struct acpi_device *adev,
  4235. + struct acpi_serdev_lookup *lookup)
  4236. +{
  4237. + struct list_head resource_list;
  4238. + int ret;
  4239. +
  4240. + lookup->device_handle = acpi_device_handle(adev);
  4241. + lookup->controller_handle = NULL;
  4242. + lookup->n = 0;
  4243. +
  4244. + INIT_LIST_HEAD(&resource_list);
  4245. + ret = acpi_dev_get_resources(adev, &resource_list,
  4246. + acpi_serdev_parse_resource, lookup);
  4247. + acpi_dev_free_resource_list(&resource_list);
  4248. +
  4249. + if (ret < 0)
  4250. + return -EINVAL;
  4251. +
  4252. + return 0;
  4253. +}
  4254. +
  4255. +static int acpi_serdev_check_resources(struct serdev_controller *ctrl,
  4256. + struct acpi_device *adev)
  4257. +{
  4258. + struct acpi_serdev_lookup lookup;
  4259. + int ret;
  4260. +
  4261. + if (acpi_bus_get_status(adev) || !adev->status.present)
  4262. + return -EINVAL;
  4263. +
  4264. + /* Look for UARTSerialBusV2 resource */
  4265. + lookup.index = -1; // we only care for the last device
  4266. +
  4267. + ret = acpi_serdev_do_lookup(adev, &lookup);
  4268. + if (ret)
  4269. + return ret;
  4270. +
  4271. + /* Make sure controller and ResourceSource handle match */
  4272. + if (ACPI_HANDLE(ctrl->dev.parent) != lookup.controller_handle)
  4273. + return -ENODEV;
  4274. +
  4275. + return 0;
  4276. +}
  4277. +
  4278. static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
  4279. - struct acpi_device *adev)
  4280. + struct acpi_device *adev)
  4281. {
  4282. - struct serdev_device *serdev = NULL;
  4283. + struct serdev_device *serdev;
  4284. int err;
  4285. - if (acpi_bus_get_status(adev) || !adev->status.present ||
  4286. - acpi_device_enumerated(adev))
  4287. - return AE_OK;
  4288. -
  4289. serdev = serdev_device_alloc(ctrl);
  4290. if (!serdev) {
  4291. dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
  4292. @@ -583,7 +664,7 @@ static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
  4293. }
  4294. static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
  4295. - void *data, void **return_value)
  4296. + void *data, void **return_value)
  4297. {
  4298. struct serdev_controller *ctrl = data;
  4299. struct acpi_device *adev;
  4300. @@ -591,22 +672,28 @@ static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
  4301. if (acpi_bus_get_device(handle, &adev))
  4302. return AE_OK;
  4303. + if (acpi_device_enumerated(adev))
  4304. + return AE_OK;
  4305. +
  4306. + if (acpi_serdev_check_resources(ctrl, adev))
  4307. + return AE_OK;
  4308. +
  4309. return acpi_serdev_register_device(ctrl, adev);
  4310. }
  4311. +
  4312. static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
  4313. {
  4314. acpi_status status;
  4315. - acpi_handle handle;
  4316. - handle = ACPI_HANDLE(ctrl->dev.parent);
  4317. - if (!handle)
  4318. + if (!has_acpi_companion(ctrl->dev.parent))
  4319. return -ENODEV;
  4320. - status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  4321. + status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  4322. + SERDEV_ACPI_MAX_SCAN_DEPTH,
  4323. acpi_serdev_add_device, NULL, ctrl, NULL);
  4324. if (ACPI_FAILURE(status))
  4325. - dev_dbg(&ctrl->dev, "failed to enumerate serdev slaves\n");
  4326. + dev_warn(&ctrl->dev, "failed to enumerate serdev slaves\n");
  4327. if (!ctrl->serdev)
  4328. return -ENODEV;
  4329. --
  4330. 2.23.0