0001-surface-acpi.patch 108 KB

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