0006-surface-sam.patch 653 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758
  1. From 4625c51cd75374e658046b43f26607bff6030a5d Mon Sep 17 00:00:00 2001
  2. From: Maximilian Luz <luzmaximilian@gmail.com>
  3. Date: Mon, 17 Aug 2020 01:23:20 +0200
  4. Subject: [PATCH] Add file2alias support for Surface Aggregator Module devices
  5. Implement file2alias support for Surface System Aggregator Module (SSAM)
  6. devices. This allows modules to be auto-loaded for specific devices via
  7. their respective module alias.
  8. Patchset: surface-sam
  9. ---
  10. include/linux/mod_devicetable.h | 17 +++++++++++++++++
  11. scripts/mod/devicetable-offsets.c | 7 +++++++
  12. scripts/mod/file2alias.c | 21 +++++++++++++++++++++
  13. 3 files changed, 45 insertions(+)
  14. diff --git a/include/linux/mod_devicetable.h b/include/linux/mod_devicetable.h
  15. index 5b08a473cdba..ef64063fac30 100644
  16. --- a/include/linux/mod_devicetable.h
  17. +++ b/include/linux/mod_devicetable.h
  18. @@ -838,4 +838,21 @@ struct mhi_device_id {
  19. kernel_ulong_t driver_data;
  20. };
  21. +/* Surface System Aggregator Module */
  22. +
  23. +#define SSAM_MATCH_CHANNEL 0x1
  24. +#define SSAM_MATCH_INSTANCE 0x2
  25. +#define SSAM_MATCH_FUNCTION 0x4
  26. +
  27. +struct ssam_device_id {
  28. + __u8 match_flags;
  29. +
  30. + __u8 category;
  31. + __u8 channel;
  32. + __u8 instance;
  33. + __u8 function;
  34. +
  35. + kernel_ulong_t driver_data;
  36. +};
  37. +
  38. #endif /* LINUX_MOD_DEVICETABLE_H */
  39. diff --git a/scripts/mod/devicetable-offsets.c b/scripts/mod/devicetable-offsets.c
  40. index 27007c18e754..bcff122d0dc8 100644
  41. --- a/scripts/mod/devicetable-offsets.c
  42. +++ b/scripts/mod/devicetable-offsets.c
  43. @@ -243,5 +243,12 @@ int main(void)
  44. DEVID(mhi_device_id);
  45. DEVID_FIELD(mhi_device_id, chan);
  46. + DEVID(ssam_device_id);
  47. + DEVID_FIELD(ssam_device_id, match_flags);
  48. + DEVID_FIELD(ssam_device_id, category);
  49. + DEVID_FIELD(ssam_device_id, channel);
  50. + DEVID_FIELD(ssam_device_id, instance);
  51. + DEVID_FIELD(ssam_device_id, function);
  52. +
  53. return 0;
  54. }
  55. diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
  56. index 2417dd1dee33..a6c583362b92 100644
  57. --- a/scripts/mod/file2alias.c
  58. +++ b/scripts/mod/file2alias.c
  59. @@ -1368,6 +1368,26 @@ static int do_mhi_entry(const char *filename, void *symval, char *alias)
  60. return 1;
  61. }
  62. +/* Looks like: ssam:cNtNiNfN
  63. + *
  64. + * N is exactly 2 digits, where each is an upper-case hex digit.
  65. + */
  66. +static int do_ssam_entry(const char *filename, void *symval, char *alias)
  67. +{
  68. + DEF_FIELD(symval, ssam_device_id, match_flags);
  69. + DEF_FIELD(symval, ssam_device_id, category);
  70. + DEF_FIELD(symval, ssam_device_id, channel);
  71. + DEF_FIELD(symval, ssam_device_id, instance);
  72. + DEF_FIELD(symval, ssam_device_id, function);
  73. +
  74. + sprintf(alias, "ssam:c%02X", category);
  75. + ADD(alias, "t", match_flags & SSAM_MATCH_CHANNEL, channel);
  76. + ADD(alias, "i", match_flags & SSAM_MATCH_INSTANCE, instance);
  77. + ADD(alias, "f", match_flags & SSAM_MATCH_FUNCTION, function);
  78. +
  79. + return 1;
  80. +}
  81. +
  82. /* Does namelen bytes of name exactly match the symbol? */
  83. static bool sym_is(const char *name, unsigned namelen, const char *symbol)
  84. {
  85. @@ -1442,6 +1462,7 @@ static const struct devtable devtable[] = {
  86. {"tee", SIZE_tee_client_device_id, do_tee_entry},
  87. {"wmi", SIZE_wmi_device_id, do_wmi_entry},
  88. {"mhi", SIZE_mhi_device_id, do_mhi_entry},
  89. + {"ssam", SIZE_ssam_device_id, do_ssam_entry},
  90. };
  91. /* Create MODULE_ALIAS() statements.
  92. --
  93. 2.33.0
  94. From e9a441abff000491b2839085ab2237849b070be2 Mon Sep 17 00:00:00 2001
  95. From: Maximilian Luz <luzmaximilian@gmail.com>
  96. Date: Mon, 17 Aug 2020 01:44:30 +0200
  97. Subject: [PATCH] platform/x86: Add support for Surface System Aggregator
  98. Module
  99. Add support for the Surface System Aggregator Module (SSAM), an embedded
  100. controller that can be found on 5th and later generation Microsoft
  101. Surface devices. The responsibilities of this EC vary from device to
  102. device. It provides battery information on all 5th and later generation
  103. devices, temperature sensor and cooling capability access, functionality
  104. for clipboard detaching on the Surface Books (2 and 3), as well as
  105. HID-over-SSAM input devices, including keyboard on the Surface Laptop 1
  106. and 2, and keyboard as well as touchpad input on the Surface Laptop 3
  107. and Surface Book 3.
  108. Patchset: surface-sam
  109. ---
  110. Documentation/driver-api/index.rst | 1 +
  111. .../surface_aggregator/client-api.rst | 38 +
  112. .../driver-api/surface_aggregator/client.rst | 393 +++
  113. .../surface_aggregator/clients/cdev.rst | 204 ++
  114. .../surface_aggregator/clients/dtx.rst | 712 +++++
  115. .../surface_aggregator/clients/index.rst | 22 +
  116. .../surface_aggregator/clients/san.rst | 44 +
  117. .../driver-api/surface_aggregator/index.rst | 21 +
  118. .../surface_aggregator/internal-api.rst | 67 +
  119. .../surface_aggregator/internal.rst | 577 ++++
  120. .../surface_aggregator/overview.rst | 77 +
  121. .../driver-api/surface_aggregator/ssh.rst | 344 ++
  122. arch/x86/kernel/acpi/boot.c | 24 +
  123. drivers/hid/Kconfig | 4 +-
  124. drivers/hid/Makefile | 2 +
  125. drivers/hid/surface-hid/Kconfig | 42 +
  126. drivers/hid/surface-hid/Makefile | 7 +
  127. drivers/hid/surface-hid/surface_hid.c | 253 ++
  128. drivers/hid/surface-hid/surface_hid_core.c | 272 ++
  129. drivers/hid/surface-hid/surface_hid_core.h | 77 +
  130. drivers/hid/surface-hid/surface_kbd.c | 300 ++
  131. drivers/platform/x86/Kconfig | 102 +
  132. drivers/platform/x86/Makefile | 7 +
  133. drivers/platform/x86/surface_acpi_notify.c | 886 ++++++
  134. .../platform/x86/surface_aggregator/Kconfig | 69 +
  135. .../platform/x86/surface_aggregator/Makefile | 17 +
  136. drivers/platform/x86/surface_aggregator/bus.c | 415 +++
  137. drivers/platform/x86/surface_aggregator/bus.h | 27 +
  138. .../x86/surface_aggregator/controller.c | 2789 +++++++++++++++++
  139. .../x86/surface_aggregator/controller.h | 285 ++
  140. .../platform/x86/surface_aggregator/core.c | 839 +++++
  141. .../x86/surface_aggregator/ssh_msgb.h | 205 ++
  142. .../x86/surface_aggregator/ssh_packet_layer.c | 2074 ++++++++++++
  143. .../x86/surface_aggregator/ssh_packet_layer.h | 190 ++
  144. .../x86/surface_aggregator/ssh_parser.c | 228 ++
  145. .../x86/surface_aggregator/ssh_parser.h | 154 +
  146. .../surface_aggregator/ssh_request_layer.c | 1263 ++++++++
  147. .../surface_aggregator/ssh_request_layer.h | 143 +
  148. .../platform/x86/surface_aggregator/trace.h | 632 ++++
  149. .../platform/x86/surface_aggregator_cdev.c | 810 +++++
  150. .../x86/surface_aggregator_registry.c | 606 ++++
  151. drivers/platform/x86/surface_dtx.c | 1283 ++++++++
  152. drivers/platform/x86/surface_perfmode.c | 122 +
  153. drivers/power/supply/Kconfig | 32 +
  154. drivers/power/supply/Makefile | 2 +
  155. drivers/power/supply/surface_battery.c | 875 ++++++
  156. drivers/power/supply/surface_charger.c | 282 ++
  157. include/linux/mod_devicetable.h | 5 +-
  158. include/linux/surface_acpi_notify.h | 39 +
  159. include/linux/surface_aggregator/controller.h | 849 +++++
  160. include/linux/surface_aggregator/device.h | 424 +++
  161. include/linux/surface_aggregator/serial_hub.h | 672 ++++
  162. include/uapi/linux/surface_aggregator/cdev.h | 147 +
  163. include/uapi/linux/surface_aggregator/dtx.h | 146 +
  164. scripts/mod/devicetable-offsets.c | 3 +-
  165. scripts/mod/file2alias.c | 10 +-
  166. 56 files changed, 20105 insertions(+), 8 deletions(-)
  167. create mode 100644 Documentation/driver-api/surface_aggregator/client-api.rst
  168. create mode 100644 Documentation/driver-api/surface_aggregator/client.rst
  169. create mode 100644 Documentation/driver-api/surface_aggregator/clients/cdev.rst
  170. create mode 100644 Documentation/driver-api/surface_aggregator/clients/dtx.rst
  171. create mode 100644 Documentation/driver-api/surface_aggregator/clients/index.rst
  172. create mode 100644 Documentation/driver-api/surface_aggregator/clients/san.rst
  173. create mode 100644 Documentation/driver-api/surface_aggregator/index.rst
  174. create mode 100644 Documentation/driver-api/surface_aggregator/internal-api.rst
  175. create mode 100644 Documentation/driver-api/surface_aggregator/internal.rst
  176. create mode 100644 Documentation/driver-api/surface_aggregator/overview.rst
  177. create mode 100644 Documentation/driver-api/surface_aggregator/ssh.rst
  178. create mode 100644 drivers/hid/surface-hid/Kconfig
  179. create mode 100644 drivers/hid/surface-hid/Makefile
  180. create mode 100644 drivers/hid/surface-hid/surface_hid.c
  181. create mode 100644 drivers/hid/surface-hid/surface_hid_core.c
  182. create mode 100644 drivers/hid/surface-hid/surface_hid_core.h
  183. create mode 100644 drivers/hid/surface-hid/surface_kbd.c
  184. create mode 100644 drivers/platform/x86/surface_acpi_notify.c
  185. create mode 100644 drivers/platform/x86/surface_aggregator/Kconfig
  186. create mode 100644 drivers/platform/x86/surface_aggregator/Makefile
  187. create mode 100644 drivers/platform/x86/surface_aggregator/bus.c
  188. create mode 100644 drivers/platform/x86/surface_aggregator/bus.h
  189. create mode 100644 drivers/platform/x86/surface_aggregator/controller.c
  190. create mode 100644 drivers/platform/x86/surface_aggregator/controller.h
  191. create mode 100644 drivers/platform/x86/surface_aggregator/core.c
  192. create mode 100644 drivers/platform/x86/surface_aggregator/ssh_msgb.h
  193. create mode 100644 drivers/platform/x86/surface_aggregator/ssh_packet_layer.c
  194. create mode 100644 drivers/platform/x86/surface_aggregator/ssh_packet_layer.h
  195. create mode 100644 drivers/platform/x86/surface_aggregator/ssh_parser.c
  196. create mode 100644 drivers/platform/x86/surface_aggregator/ssh_parser.h
  197. create mode 100644 drivers/platform/x86/surface_aggregator/ssh_request_layer.c
  198. create mode 100644 drivers/platform/x86/surface_aggregator/ssh_request_layer.h
  199. create mode 100644 drivers/platform/x86/surface_aggregator/trace.h
  200. create mode 100644 drivers/platform/x86/surface_aggregator_cdev.c
  201. create mode 100644 drivers/platform/x86/surface_aggregator_registry.c
  202. create mode 100644 drivers/platform/x86/surface_dtx.c
  203. create mode 100644 drivers/platform/x86/surface_perfmode.c
  204. create mode 100644 drivers/power/supply/surface_battery.c
  205. create mode 100644 drivers/power/supply/surface_charger.c
  206. create mode 100644 include/linux/surface_acpi_notify.h
  207. create mode 100644 include/linux/surface_aggregator/controller.h
  208. create mode 100644 include/linux/surface_aggregator/device.h
  209. create mode 100644 include/linux/surface_aggregator/serial_hub.h
  210. create mode 100644 include/uapi/linux/surface_aggregator/cdev.h
  211. create mode 100644 include/uapi/linux/surface_aggregator/dtx.h
  212. diff --git a/Documentation/driver-api/index.rst b/Documentation/driver-api/index.rst
  213. index f357f3eb400c..699dc7cac0fb 100644
  214. --- a/Documentation/driver-api/index.rst
  215. +++ b/Documentation/driver-api/index.rst
  216. @@ -97,6 +97,7 @@ available subsections can be seen below.
  217. rfkill
  218. serial/index
  219. sm501
  220. + surface_aggregator/index
  221. switchtec
  222. sync_file
  223. vfio-mediated-device
  224. diff --git a/Documentation/driver-api/surface_aggregator/client-api.rst b/Documentation/driver-api/surface_aggregator/client-api.rst
  225. new file mode 100644
  226. index 000000000000..a1117d57036a
  227. --- /dev/null
  228. +++ b/Documentation/driver-api/surface_aggregator/client-api.rst
  229. @@ -0,0 +1,38 @@
  230. +.. SPDX-License-Identifier: GPL-2.0+
  231. +
  232. +===============================
  233. +Client Driver API Documentation
  234. +===============================
  235. +
  236. +.. contents::
  237. + :depth: 2
  238. +
  239. +
  240. +Serial Hub Communication
  241. +========================
  242. +
  243. +.. kernel-doc:: include/linux/surface_aggregator/serial_hub.h
  244. +
  245. +.. kernel-doc:: drivers/misc/surface_aggregator/ssh_packet_layer.c
  246. + :export:
  247. +
  248. +
  249. +Controller and Core Interface
  250. +=============================
  251. +
  252. +.. kernel-doc:: include/linux/surface_aggregator/controller.h
  253. +
  254. +.. kernel-doc:: drivers/misc/surface_aggregator/controller.c
  255. + :export:
  256. +
  257. +.. kernel-doc:: drivers/misc/surface_aggregator/core.c
  258. + :export:
  259. +
  260. +
  261. +Client Bus and Client Device API
  262. +================================
  263. +
  264. +.. kernel-doc:: include/linux/surface_aggregator/device.h
  265. +
  266. +.. kernel-doc:: drivers/misc/surface_aggregator/bus.c
  267. + :export:
  268. diff --git a/Documentation/driver-api/surface_aggregator/client.rst b/Documentation/driver-api/surface_aggregator/client.rst
  269. new file mode 100644
  270. index 000000000000..e519d374c378
  271. --- /dev/null
  272. +++ b/Documentation/driver-api/surface_aggregator/client.rst
  273. @@ -0,0 +1,393 @@
  274. +.. SPDX-License-Identifier: GPL-2.0+
  275. +
  276. +.. |ssam_controller| replace:: :c:type:`struct ssam_controller <ssam_controller>`
  277. +.. |ssam_device| replace:: :c:type:`struct ssam_device <ssam_device>`
  278. +.. |ssam_device_driver| replace:: :c:type:`struct ssam_device_driver <ssam_device_driver>`
  279. +.. |ssam_client_bind| replace:: :c:func:`ssam_client_bind`
  280. +.. |ssam_client_link| replace:: :c:func:`ssam_client_link`
  281. +.. |ssam_get_controller| replace:: :c:func:`ssam_get_controller`
  282. +.. |ssam_controller_get| replace:: :c:func:`ssam_controller_get`
  283. +.. |ssam_controller_put| replace:: :c:func:`ssam_controller_put`
  284. +.. |ssam_device_alloc| replace:: :c:func:`ssam_device_alloc`
  285. +.. |ssam_device_add| replace:: :c:func:`ssam_device_add`
  286. +.. |ssam_device_remove| replace:: :c:func:`ssam_device_remove`
  287. +.. |ssam_device_driver_register| replace:: :c:func:`ssam_device_driver_register`
  288. +.. |ssam_device_driver_unregister| replace:: :c:func:`ssam_device_driver_unregister`
  289. +.. |module_ssam_device_driver| replace:: :c:func:`module_ssam_device_driver`
  290. +.. |SSAM_DEVICE| replace:: :c:func:`SSAM_DEVICE`
  291. +.. |ssam_notifier_register| replace:: :c:func:`ssam_notifier_register`
  292. +.. |ssam_notifier_unregister| replace:: :c:func:`ssam_notifier_unregister`
  293. +.. |ssam_request_sync| replace:: :c:func:`ssam_request_sync`
  294. +.. |ssam_event_mask| replace:: :c:type:`enum ssam_event_mask <ssam_event_mask>`
  295. +
  296. +
  297. +======================
  298. +Writing Client Drivers
  299. +======================
  300. +
  301. +For the API documentation, refer to:
  302. +
  303. +.. toctree::
  304. + :maxdepth: 2
  305. +
  306. + client-api
  307. +
  308. +
  309. +Overview
  310. +========
  311. +
  312. +Client drivers can be set up in two main ways, depending on how the
  313. +corresponding device is made available to the system. We specifically
  314. +differentiate between devices that are presented to the system via one of
  315. +the conventional ways, e.g. as platform devices via ACPI, and devices that
  316. +are non-discoverable and instead need to be explicitly provided by some
  317. +other mechanism, as discussed further below.
  318. +
  319. +
  320. +Non-SSAM Client Drivers
  321. +=======================
  322. +
  323. +All communication with the SAM EC is handled via the |ssam_controller|
  324. +representing that EC to the kernel. Drivers targeting a non-SSAM device (and
  325. +thus not being a |ssam_device_driver|) need to explicitly establish a
  326. +connection/relation to that controller. This can be done via the
  327. +|ssam_client_bind| function. Said function returns a reference to the SSAM
  328. +controller, but, more importantly, also establishes a device link between
  329. +client device and controller (this can also be done separate via
  330. +|ssam_client_link|). It is important to do this, as it, first, guarantees
  331. +that the returned controller is valid for use in the client driver for as
  332. +long as this driver is bound to its device, i.e. that the driver gets
  333. +unbound before the controller ever becomes invalid, and, second, as it
  334. +ensures correct suspend/resume ordering. This setup should be done in the
  335. +driver's probe function, and may be used to defer probing in case the SSAM
  336. +subsystem is not ready yet, for example:
  337. +
  338. +.. code-block:: c
  339. +
  340. + static int client_driver_probe(struct platform_device *pdev)
  341. + {
  342. + struct ssam_controller *ctrl;
  343. +
  344. + ctrl = ssam_client_bind(&pdev->dev);
  345. + if (IS_ERR(ctrl))
  346. + return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
  347. +
  348. + // ...
  349. +
  350. + return 0;
  351. + }
  352. +
  353. +The controller may be separately obtained via |ssam_get_controller| and its
  354. +lifetime be guaranteed via |ssam_controller_get| and |ssam_controller_put|.
  355. +Note that none of these functions, however, guarantee that the controller
  356. +will not be shut down or suspended. These functions essentially only operate
  357. +on the reference, i.e. only guarantee a bare minimum of accessibility
  358. +without any guarantees at all on practical operability.
  359. +
  360. +
  361. +Adding SSAM Devices
  362. +===================
  363. +
  364. +If a device does not already exist/is not already provided via conventional
  365. +means, it should be provided as |ssam_device| via the SSAM client device
  366. +hub. New devices can be added to this hub by entering their UID into the
  367. +corresponding registry. SSAM devices can also be manually allocated via
  368. +|ssam_device_alloc|, subsequently to which they have to be added via
  369. +|ssam_device_add| and eventually removed via |ssam_device_remove|. By
  370. +default, the parent of the device is set to the controller device provided
  371. +for allocation, however this may be changed before the device is added. Note
  372. +that, when changing the parent device, care must be taken to ensure that the
  373. +controller lifetime and suspend/resume ordering guarantees, in the default
  374. +setup provided through the parent-child relation, are preserved. If
  375. +necessary, by use of |ssam_client_link| as is done for non-SSAM client
  376. +drivers and described in more detail above.
  377. +
  378. +A client device must always be removed by the party which added the
  379. +respective device before the controller shuts down. Such removal can be
  380. +guaranteed by linking the driver providing the SSAM device to the controller
  381. +via |ssam_client_link|, causing it to unbind before the controller driver
  382. +unbinds. Client devices registered with the controller as parent are
  383. +automatically removed when the controller shuts down, but this should not be
  384. +relied upon, especially as this does not extend to client devices with a
  385. +different parent.
  386. +
  387. +
  388. +SSAM Client Drivers
  389. +===================
  390. +
  391. +SSAM client device drivers are, in essence, no different than other device
  392. +driver types. They are represented via |ssam_device_driver| and bind to a
  393. +|ssam_device| via its UID (:c:type:`struct ssam_device.uid <ssam_device>`)
  394. +member and the match table
  395. +(:c:type:`struct ssam_device_driver.match_table <ssam_device_driver>`),
  396. +which should be set when declaring the driver struct instance. Refer to the
  397. +|SSAM_DEVICE| macro documentation for more details on how to define members
  398. +of the driver's match table.
  399. +
  400. +The UID for SSAM client devices consists of a ``domain``, a ``category``,
  401. +a ``target``, an ``instance``, and a ``function``. The ``domain`` is used
  402. +differentiate between physical SAM devices
  403. +(:c:type:`SSAM_DOMAIN_SERIALHUB <ssam_device_domain>`), i.e. devices that can
  404. +be accessed via the Surface Serial Hub, and virtual ones
  405. +(:c:type:`SSAM_DOMAIN_VIRTUAL <ssam_device_domain>`), such as client-device
  406. +hubs, that have no real representation on the SAM EC and are solely used on
  407. +the kernel/driver-side. For physical devices, ``category`` represents the
  408. +target category, ``target`` the target ID, and ``instance`` the instance ID
  409. +used to access the physical SAM device. In addition, ``function`` references
  410. +a specific device functionality, but has no meaning to the SAM EC. The
  411. +(default) name of a client device is generated based on its UID.
  412. +
  413. +A driver instance can be registered via |ssam_device_driver_register| and
  414. +unregistered via |ssam_device_driver_unregister|. For convenience, the
  415. +|module_ssam_device_driver| macro may be used to define module init- and
  416. +exit-functions registering the driver.
  417. +
  418. +The controller associated with a SSAM client device can be found in its
  419. +:c:type:`struct ssam_device.ctrl <ssam_device>` member. This reference is
  420. +guaranteed to be valid for at least as long as the client driver is bound,
  421. +but should also be valid for as long as the client device exists. Note,
  422. +however, that access outside of the bound client driver must ensure that the
  423. +controller device is not suspended while making any requests or
  424. +(un-)registering event notifiers (and thus should generally be avoided). This
  425. +is guaranteed when the controller is accessed from inside the bound client
  426. +driver.
  427. +
  428. +
  429. +Making Synchronous Requests
  430. +===========================
  431. +
  432. +Synchronous requests are (currently) the main form of host-initiated
  433. +communication with the EC. There are a couple of ways to define and execute
  434. +such requests, however, most of them boil down to something similar as shown
  435. +in the example below. This example defines a write-read request, meaning
  436. +that the caller provides an argument to the SAM EC and receives a response.
  437. +The caller needs to know the (maximum) length of the response payload and
  438. +provide a buffer for it.
  439. +
  440. +Care must be taken to ensure that any command payload data passed to the SAM
  441. +EC is provided in little-endian format and, similarly, any response payload
  442. +data received from it is converted from little-endian to host endianness.
  443. +
  444. +.. code-block:: c
  445. +
  446. + int perform_request(struct ssam_controller *ctrl, u32 arg, u32 *ret)
  447. + {
  448. + struct ssam_request rqst;
  449. + struct ssam_response resp;
  450. + int status;
  451. +
  452. + /* Convert request argument to little-endian. */
  453. + __le32 arg_le = cpu_to_le32(arg);
  454. + __le32 ret_le = cpu_to_le32(0);
  455. +
  456. + /*
  457. + * Initialize request specification. Replace this with your values.
  458. + * The rqst.payload field may be NULL if rqst.length is zero,
  459. + * indicating that the request does not have any argument.
  460. + *
  461. + * Note: The request parameters used here are not valid, i.e.
  462. + * they do not correspond to an actual SAM/EC request.
  463. + */
  464. + rqst.target_category = SSAM_SSH_TC_SAM;
  465. + rqst.target_id = 0x01;
  466. + rqst.command_id = 0x02;
  467. + rqst.instance_id = 0x03;
  468. + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
  469. + rqst.length = sizeof(arg_le);
  470. + rqst.payload = (u8 *)&arg_le;
  471. +
  472. + /* Initialize request response. */
  473. + resp.capacity = sizeof(ret_le);
  474. + resp.length = 0;
  475. + resp.pointer = (u8 *)&ret_le;
  476. +
  477. + /*
  478. + * Perform actual request. The response pointer may be null in case
  479. + * the request does not have any response. This must be consistent
  480. + * with the SSAM_REQUEST_HAS_RESPONSE flag set in the specification
  481. + * above.
  482. + */
  483. + status = ssam_request_sync(ctrl, &rqst, &resp);
  484. +
  485. + /*
  486. + * Alternatively use
  487. + *
  488. + * ssam_request_sync_onstack(ctrl, &rqst, &resp, sizeof(arg_le));
  489. + *
  490. + * to perform the request, allocating the message buffer directly
  491. + * on the stack as opposed to allocation via kzalloc().
  492. + */
  493. +
  494. + /*
  495. + * Convert request response back to native format. Note that in the
  496. + * error case, this value is not touched by the SSAM core, i.e.
  497. + * 'ret_le' will be zero as specified in its initialization.
  498. + */
  499. + *ret = le32_to_cpu(ret_le);
  500. +
  501. + return status;
  502. + }
  503. +
  504. +Note that |ssam_request_sync| in its essence is a wrapper over lower-level
  505. +request primitives, which may also be used to perform requests. Refer to its
  506. +implementation and documentation for more details.
  507. +
  508. +An arguably more user-friendly way of defining such functions is by using
  509. +one of the generator macros, for example via:
  510. +
  511. +.. code-block:: c
  512. +
  513. + SSAM_DEFINE_SYNC_REQUEST_W(__ssam_tmp_perf_mode_set, __le32, {
  514. + .target_category = SSAM_SSH_TC_TMP,
  515. + .target_id = 0x01,
  516. + .command_id = 0x03,
  517. + .instance_id = 0x00,
  518. + });
  519. +
  520. +This example defines a function
  521. +
  522. +.. code-block:: c
  523. +
  524. + static int __ssam_tmp_perf_mode_set(struct ssam_controller *ctrl, const __le32 *arg);
  525. +
  526. +executing the specified request, with the controller passed in when calling
  527. +said function. In this example, the argument is provided via the ``arg``
  528. +pointer. Note that the generated function allocates the message buffer on
  529. +the stack. Thus, if the argument provided via the request is large, these
  530. +kinds of macros should be avoided. Also note that, in contrast to the
  531. +previous non-macro example, this function does not do any endianness
  532. +conversion, which has to be handled by the caller. Apart from those
  533. +differences the function generated by the macro is similar to the one
  534. +provided in the non-macro example above.
  535. +
  536. +The full list of such function-generating macros is
  537. +
  538. +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_N` for requests without return value and
  539. + without argument.
  540. +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_R` for requests with return value but no
  541. + argument.
  542. +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_W` for requests without return value but
  543. + with argument.
  544. +
  545. +Refer to their respective documentation for more details. For each one of
  546. +these macros, a special variant is provided, which targets request types
  547. +applicable to multiple instances of the same device type:
  548. +
  549. +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_MD_N`
  550. +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_MD_R`
  551. +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_MD_W`
  552. +
  553. +The difference of those macros to the previously mentioned versions is, that
  554. +the device target and instance IDs are not fixed for the generated function,
  555. +but instead have to be provided by the caller of said function.
  556. +
  557. +Additionally, variants for direct use with client devices, i.e.
  558. +|ssam_device|, are also provided. These can, for example, be used as
  559. +follows:
  560. +
  561. +.. code-block:: c
  562. +
  563. + SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_sta, __le32, {
  564. + .target_category = SSAM_SSH_TC_BAT,
  565. + .command_id = 0x01,
  566. + });
  567. +
  568. +This invocation of the macro defines a function
  569. +
  570. +.. code-block:: c
  571. +
  572. + static int ssam_bat_get_sta(struct ssam_device *sdev, __le32 *ret);
  573. +
  574. +executing the specified request, using the device IDs and controller given
  575. +in the client device. The full list of such macros for client devices is:
  576. +
  577. +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_CL_N`
  578. +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_CL_R`
  579. +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_CL_W`
  580. +
  581. +
  582. +Handling Events
  583. +===============
  584. +
  585. +To receive events from the SAM EC, an event notifier must be registered for
  586. +the desired event via |ssam_notifier_register|. The notifier must be
  587. +unregistered via |ssam_notifier_unregister| once it is not required any
  588. +more.
  589. +
  590. +Event notifiers are registered by providing (at minimum) a callback to call
  591. +in case an event has been received, the registry specifying how the event
  592. +should be enabled, an event ID specifying for which target category and,
  593. +optionally and depending on the registry used, for which instance ID events
  594. +should be enabled, and finally, flags describing how the EC will send these
  595. +events. If the specific registry does not enable events by instance ID, the
  596. +instance ID must be set to zero. Additionally, a priority for the respective
  597. +notifier may be specified, which determines its order in relation to any
  598. +other notifier registered for the same target category.
  599. +
  600. +By default, event notifiers will receive all events for the specific target
  601. +category, regardless of the instance ID specified when registering the
  602. +notifier. The core may be instructed to only call a notifier if the target
  603. +ID or instance ID (or both) of the event match the ones implied by the
  604. +notifier IDs (in case of target ID, the target ID of the registry), by
  605. +providing an event mask (see |ssam_event_mask|).
  606. +
  607. +In general, the target ID of the registry is also the target ID of the
  608. +enabled event (with the notable exception being keyboard input events on the
  609. +Surface Laptop 1 and 2, which are enabled via a registry with target ID 1,
  610. +but provide events with target ID 2).
  611. +
  612. +A full example for registering an event notifier and handling received
  613. +events is provided below:
  614. +
  615. +.. code-block:: c
  616. +
  617. + u32 notifier_callback(struct ssam_event_notifier *nf,
  618. + const struct ssam_event *event)
  619. + {
  620. + int status = ...
  621. +
  622. + /* Handle the event here ... */
  623. +
  624. + /* Convert return value and indicate that we handled the event. */
  625. + return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
  626. + }
  627. +
  628. + int setup_notifier(struct ssam_device *sdev,
  629. + struct ssam_event_notifier *nf)
  630. + {
  631. + /* Set priority wrt. other handlers of same target category. */
  632. + nf->base.priority = 1;
  633. +
  634. + /* Set event/notifier callback. */
  635. + nf->base.fn = notifier_callback;
  636. +
  637. + /* Specify event registry, i.e. how events get enabled/disabled. */
  638. + nf->event.reg = SSAM_EVENT_REGISTRY_KIP;
  639. +
  640. + /* Specify which event to enable/disable */
  641. + nf->event.id.target_category = sdev->uid.category;
  642. + nf->event.id.instance = sdev->uid.instance;
  643. +
  644. + /*
  645. + * Specify for which events the notifier callback gets executed.
  646. + * This essentially tells the core if it can skip notifiers that
  647. + * don't have target or instance IDs matching those of the event.
  648. + */
  649. + nf->event.mask = SSAM_EVENT_MASK_STRICT;
  650. +
  651. + /* Specify event flags. */
  652. + nf->event.flags = SSAM_EVENT_SEQUENCED;
  653. +
  654. + return ssam_notifier_register(sdev->ctrl, nf);
  655. + }
  656. +
  657. +Multiple event notifiers can be registered for the same event. The event
  658. +handler core takes care of enabling and disabling events when notifiers are
  659. +registered and unregistered, by keeping track of how many notifiers for a
  660. +specific event (combination of registry, event target category, and event
  661. +instance ID) are currently registered. This means that a specific event will
  662. +be enabled when the first notifier for it is being registered and disabled
  663. +when the last notifier for it is being unregistered. Note that the event
  664. +flags are therefore only used on the first registered notifier, however, one
  665. +should take care that notifiers for a specific event are always registered
  666. +with the same flag and it is considered a bug to do otherwise.
  667. diff --git a/Documentation/driver-api/surface_aggregator/clients/cdev.rst b/Documentation/driver-api/surface_aggregator/clients/cdev.rst
  668. new file mode 100644
  669. index 000000000000..0134a841a079
  670. --- /dev/null
  671. +++ b/Documentation/driver-api/surface_aggregator/clients/cdev.rst
  672. @@ -0,0 +1,204 @@
  673. +.. SPDX-License-Identifier: GPL-2.0+
  674. +
  675. +.. |ssam_cdev_request| replace:: :c:type:`struct ssam_cdev_request <ssam_cdev_request>`
  676. +.. |ssam_cdev_request_flags| replace:: :c:type:`enum ssam_cdev_request_flags <ssam_cdev_request_flags>`
  677. +.. |ssam_cdev_event| replace:: :c:type:`struct ssam_cdev_event <ssam_cdev_event>`
  678. +
  679. +==============================
  680. +User-Space EC Interface (cdev)
  681. +==============================
  682. +
  683. +The ``surface_aggregator_cdev`` module provides a misc-device for the SSAM
  684. +controller to allow for a (more or less) direct connection from user-space to
  685. +the SAM EC. It is intended to be used for development and debugging, and
  686. +therefore should not be used or relied upon in any other way. Note that this
  687. +module is not loaded automatically, but instead must be loaded manually.
  688. +
  689. +The provided interface is accessible through the ``/dev/surface/aggregator``
  690. +device-file. All functionality of this interface is provided via IOCTLs.
  691. +These IOCTLs and their respective input/output parameter structs are defined in
  692. +``include/uapi/linux/surface_aggregator/cdev.h``.
  693. +
  694. +A small python library and scripts for accessing this interface can be found
  695. +at https://github.com/linux-surface/surface-aggregator-module/tree/master/scripts/ssam.
  696. +
  697. +.. contents::
  698. +
  699. +
  700. +Receiving Events
  701. +================
  702. +
  703. +Events can be received by reading from the device-file. The are represented by
  704. +the |ssam_cdev_event| datatype.
  705. +
  706. +Before events are available to be read, however, the desired notifiers must be
  707. +registered via the ``SSAM_CDEV_NOTIF_REGISTER`` IOCTL. Notifiers are, in
  708. +essence, callbacks, called when the EC sends an event. They are, in this
  709. +interface, associated with a specific target category and device-file-instance.
  710. +They forward any event of this category to the buffer of the corresponding
  711. +instance, from which it can then be read.
  712. +
  713. +Notifiers themselves do not enable events on the EC. Thus, it may additionally
  714. +be necessary to enable events via the ``SSAM_CDEV_EVENT_ENABLE`` IOCTL. While
  715. +notifiers work per-client (i.e. per-device-file-instance), events are enabled
  716. +globally, for the EC and all of its clients (regardless of userspace or
  717. +non-userspace). The ``SSAM_CDEV_EVENT_ENABLE`` and ``SSAM_CDEV_EVENT_DISABLE``
  718. +IOCTLs take care of reference counting the events, such that an event is
  719. +enabled as long as there is a client that has requested it.
  720. +
  721. +Note that enabled events are not automatically disabled once the client
  722. +instance is closed. Therefore any client process (or group of processes) should
  723. +balance their event enable calls with the corresponding event disable calls. It
  724. +is, however, perfectly valid to enable and disable events on different client
  725. +instances. For example, it is valid to set up notifiers and read events on
  726. +client instance ``A``, enable those events on instance ``B`` (note that these
  727. +will also be received by A since events are enabled/disabled globally), and
  728. +after no more events are desired, disable the previously enabled events via
  729. +instance ``C``.
  730. +
  731. +
  732. +Controller IOCTLs
  733. +=================
  734. +
  735. +The following IOCTLs are provided:
  736. +
  737. +.. flat-table:: Controller IOCTLs
  738. + :widths: 1 1 1 1 4
  739. + :header-rows: 1
  740. +
  741. + * - Type
  742. + - Number
  743. + - Direction
  744. + - Name
  745. + - Description
  746. +
  747. + * - ``0xA5``
  748. + - ``1``
  749. + - ``WR``
  750. + - ``REQUEST``
  751. + - Perform synchronous SAM request.
  752. +
  753. + * - ``0xA5``
  754. + - ``2``
  755. + - ``W``
  756. + - ``NOTIF_REGISTER``
  757. + - Register event notifier.
  758. +
  759. + * - ``0xA5``
  760. + - ``3``
  761. + - ``W``
  762. + - ``NOTIF_UNREGISTER``
  763. + - Unregister event notifier.
  764. +
  765. + * - ``0xA5``
  766. + - ``4``
  767. + - ``W``
  768. + - ``EVENT_ENABLE``
  769. + - Enable event source.
  770. +
  771. + * - ``0xA5``
  772. + - ``5``
  773. + - ``W``
  774. + - ``EVENT_DISABLE``
  775. + - Disable event source.
  776. +
  777. +
  778. +``SSAM_CDEV_REQUEST``
  779. +---------------------
  780. +
  781. +Defined as ``_IOWR(0xA5, 1, struct ssam_cdev_request)``.
  782. +
  783. +Executes a synchronous SAM request. The request specification is passed in
  784. +as argument of type |ssam_cdev_request|, which is then written to/modified
  785. +by the IOCTL to return status and result of the request.
  786. +
  787. +Request payload data must be allocated separately and is passed in via the
  788. +``payload.data`` and ``payload.length`` members. If a response is required,
  789. +the response buffer must be allocated by the caller and passed in via the
  790. +``response.data`` member. The ``response.length`` member must be set to the
  791. +capacity of this buffer, or if no response is required, zero. Upon
  792. +completion of the request, the call will write the response to the response
  793. +buffer (if its capacity allows it) and overwrite the length field with the
  794. +actual size of the response, in bytes.
  795. +
  796. +Additionally, if the request has a response, this must be indicated via the
  797. +request flags, as is done with in-kernel requests. Request flags can be set
  798. +via the ``flags`` member and the values correspond to the values found in
  799. +|ssam_cdev_request_flags|.
  800. +
  801. +Finally, the status of the request itself is returned in the ``status``
  802. +member (a negative errno value indicating failure). Note that failure
  803. +indication of the IOCTL is separated from failure indication of the request:
  804. +The IOCTL returns a negative status code if anything failed during setup of
  805. +the request (``-EFAULT``) or if the provided argument or any of its fields
  806. +are invalid (``-EINVAL``). In this case, the status value of the request
  807. +argument may be set, providing more detail on what went wrong (e.g.
  808. +``-ENOMEM`` for out-of-memory), but this value may also be zero. The IOCTL
  809. +will return with a zero status code in case the request has been set up,
  810. +submitted, and completed (i.e. handed back to user-space) successfully from
  811. +inside the IOCTL, but the request ``status`` member may still be negative in
  812. +case the actual execution of the request failed after it has been submitted.
  813. +
  814. +A full definition of the argument struct is provided below.
  815. +
  816. +``SSAM_CDEV_NOTIF_REGISTER``
  817. +----------------------------
  818. +
  819. +Defined as ``_IOW(0xA5, 2, struct ssam_cdev_notifier_desc)``.
  820. +
  821. +Register a notifier for the event target category specified in the given
  822. +notifier description with the specified priority. Notifiers registration is
  823. +required to receive events, but does not enable events themselves. After a
  824. +notifier for a specific target category has been registered, all events of that
  825. +category will be forwarded to the userspace client and can then be read from
  826. +the device file instance. Note that events may have to be enabled, e.g. via the
  827. +``SSAM_CDEV_EVENT_ENABLE`` IOCTL, before the EC will send them.
  828. +
  829. +Only one notifier can be registered per target category and client instance. If
  830. +a notifier has already been registered, this IOCTL will fail with ``-EEXIST``.
  831. +
  832. +Notifiers will automatically be removed when the device file instance is
  833. +closed.
  834. +
  835. +``SSAM_CDEV_NOTIF_UNREGISTER``
  836. +------------------------------
  837. +
  838. +Defined as ``_IOW(0xA5, 3, struct ssam_cdev_notifier_desc)``.
  839. +
  840. +Unregisters the notifier associated with the specified target category. The
  841. +priority field will be ignored by this IOCTL. If no notifier has been
  842. +registered for this client instance and the given category, this IOCTL will
  843. +fail with ``-ENOENT``.
  844. +
  845. +``SSAM_CDEV_EVENT_ENABLE``
  846. +--------------------------
  847. +
  848. +Defined as ``_IOW(0xA5, 4, struct ssam_cdev_event_desc)``.
  849. +
  850. +Enable the event associated with the given event descriptor.
  851. +
  852. +Note that this call will not register a notifier itself, it will only enable
  853. +events on the controller. If you want to receive events by reading from the
  854. +device file, you will need to register the corresponding notifier(s) on that
  855. +instance.
  856. +
  857. +Events are not automatically disabled when the device file is closed. This must
  858. +be done manually, via a call to the ``SSAM_CDEV_EVENT_DISABLE`` IOCTL.
  859. +
  860. +``SSAM_CDEV_EVENT_DISABLE``
  861. +---------------------------
  862. +
  863. +Defined as ``_IOW(0xA5, 5, struct ssam_cdev_event_desc)``.
  864. +
  865. +Disable the event associated with the given event descriptor.
  866. +
  867. +Note that this will not unregister any notifiers. Events may still be received
  868. +and forwarded to user-space after this call. The only safe way of stopping
  869. +events from being received is unregistering all previously registered
  870. +notifiers.
  871. +
  872. +
  873. +Structures and Enums
  874. +====================
  875. +
  876. +.. kernel-doc:: include/uapi/linux/surface_aggregator/cdev.h
  877. diff --git a/Documentation/driver-api/surface_aggregator/clients/dtx.rst b/Documentation/driver-api/surface_aggregator/clients/dtx.rst
  878. new file mode 100644
  879. index 000000000000..e974c2b04e9f
  880. --- /dev/null
  881. +++ b/Documentation/driver-api/surface_aggregator/clients/dtx.rst
  882. @@ -0,0 +1,712 @@
  883. +.. SPDX-License-Identifier: GPL-2.0+
  884. +
  885. +.. |__u16| replace:: :c:type:`__u16 <__u16>`
  886. +.. |sdtx_event| replace:: :c:type:`struct sdtx_event <sdtx_event>`
  887. +.. |sdtx_event_code| replace:: :c:type:`enum sdtx_event_code <sdtx_event_code>`
  888. +.. |sdtx_base_info| replace:: :c:type:`struct sdtx_base_info <sdtx_base_info>`
  889. +.. |sdtx_device_mode| replace:: :c:type:`struct sdtx_device_mode <sdtx_device_mode>`
  890. +
  891. +======================================================
  892. +User-Space DTX (Clipboard Detachment System) Interface
  893. +======================================================
  894. +
  895. +The ``surface_dtx`` driver is responsible for proper clipboard detachment
  896. +and re-attachment handling. To this end, it provides the ``/dev/surface/dtx``
  897. +device file, through which it can interface with a user-space daemon. This
  898. +daemon is then ultimately responsible for determining and taking necessary
  899. +actions, such as unmounting devices attached to the base,
  900. +unloading/reloading the graphics-driver, user-notifications, etc.
  901. +
  902. +There are two basic communication principles used in this driver: Commands
  903. +(in other parts of the documentation also referred to as requests) and
  904. +events. Commands are sent to the EC and may have a different implications in
  905. +different contexts. Events are sent by the EC upon some internal state
  906. +change. Commands are always driver-initiated, whereas events are always
  907. +initiated by the EC.
  908. +
  909. +.. contents::
  910. +
  911. +Nomenclature
  912. +============
  913. +
  914. +* **Clipboard:**
  915. + The detachable upper part of the Surface Book, housing the screen and CPU.
  916. +
  917. +* **Base:**
  918. + The lower part of the Surface Book from which the clipboard can be
  919. + detached, optionally (model dependent) housing the discrete GPU (dGPU).
  920. +
  921. +* **Latch:**
  922. + The mechanism keeping the clipboard attached to the base in normal
  923. + operation and allowing it to be detached when requested.
  924. +
  925. +* **Silently ignored commands:**
  926. + The command is accepted by the EC as a valid command and acknowledged
  927. + (following the standard communication protocol), but the EC does not act
  928. + upon it, i.e. ignores it.e upper part of the
  929. +
  930. +
  931. +Detachment Process
  932. +==================
  933. +
  934. +Warning: This part of the documentation is based on reverse engineering and
  935. +testing and thus may contain errors or be incomplete.
  936. +
  937. +Latch States
  938. +------------
  939. +
  940. +The latch mechanism has two major states: *open* and *closed*. In the
  941. +*closed* state (default), the clipboard is secured to the base, whereas in
  942. +the *open* state, the clipboard can be removed by a user.
  943. +
  944. +The latch can additionally be locked and, correspondingly, unlocked, which
  945. +can influence the detachment procedure. Specifically, this locking mechanism
  946. +is intended to prevent the the dGPU, positioned in the base of the device,
  947. +from being hot-unplugged while in use. More details can be found in the
  948. +documentation for the detachment procedure below. By default, the latch is
  949. +unlocked.
  950. +
  951. +Detachment Procedure
  952. +--------------------
  953. +
  954. +Note that the detachment process is governed fully by the EC. The
  955. +``surface_dtx`` driver only relays events from the EC to user-space and
  956. +commands from user-space to the EC, i.e. it does not influence this process.
  957. +
  958. +The detachment process is started with the user pressing the *detach* button
  959. +on the base of the device or executing the ``SDTX_IOCTL_LATCH_REQUEST`` IOCTL.
  960. +Following that:
  961. +
  962. +1. The EC turns on the indicator led on the detach-button, sends a
  963. + *detach-request* event (``SDTX_EVENT_REQUEST``), and awaits further
  964. + instructions/commands. In case the latch is unlocked, the led will flash
  965. + green. If the latch has been locked, the led will be solid red
  966. +
  967. +2. The event is, via the ``surface_dtx`` driver, relayed to user-space, where
  968. + an appropriate user-space daemon can handle it and send instructions back
  969. + to the EC via IOCTLs provided by this driver.
  970. +
  971. +3. The EC waits for instructions from user-space and acts according to them.
  972. + If the EC does not receive any instructions in a given period, it will
  973. + time out and continue as follows:
  974. +
  975. + - If the latch is unlocked, the EC will open the latch and the clipboard
  976. + can be detached from the base. This is the exact behavior as without
  977. + this driver or any user-space daemon. See the ``SDTX_IOCTL_LATCH_CONFIRM``
  978. + description below for more details on the follow-up behavior of the EC.
  979. +
  980. + - If the latch is locked, the EC will *not* open the latch, meaning the
  981. + clipboard cannot be detached from the base. Furthermore, the EC sends
  982. + an cancel event (``SDTX_EVENT_CANCEL``) detailing this with the cancel
  983. + reason ``SDTX_DETACH_TIMEDOUT`` (see :ref:`events` for details).
  984. +
  985. +Valid responses by a user-space daemon to a detachment request event are:
  986. +
  987. +- Execute ``SDTX_IOCTL_LATCH_REQUEST``. This will immediately abort the
  988. + detachment process. Furthermore, the EC will send a detach-request event,
  989. + similar to the user pressing the detach-button to cancel said process (see
  990. + below).
  991. +
  992. +- Execute ``SDTX_IOCTL_LATCH_CONFIRM``. This will cause the EC to open the
  993. + latch, after which the user can separate clipboard and base.
  994. +
  995. + As this changes the latch state, a *latch-status* event
  996. + (``SDTX_EVENT_LATCH_STATUS``) will be sent once the latch has been opened
  997. + successfully. If the EC fails to open the latch, e.g. due to hardware
  998. + error or low battery, a latch-cancel event (``SDTX_EVENT_CANCEL``) will be
  999. + sent with the cancel reason indicating the specific failure.
  1000. +
  1001. + If the latch is currently locked, the latch will automatically be
  1002. + unlocked before it is opened.
  1003. +
  1004. +- Execute ``SDTX_IOCTL_LATCH_HEARTBEAT``. This will reset the internal timeout.
  1005. + No other actions will be performed, i.e. the detachment process will neither
  1006. + be completed nor canceled, and the EC will still be waiting for further
  1007. + responses.
  1008. +
  1009. +- Execute ``SDTX_IOCTL_LATCH_CANCEL``. This will abort the detachment process,
  1010. + similar to ``SDTX_IOCTL_LATCH_REQUEST``, described above, or the button
  1011. + press, described below. A *generic request* event (``SDTX_EVENT_REQUEST``)
  1012. + is send in response to this. In contrast to those, however, this command
  1013. + does not trigger a new detachment process if none is currently in
  1014. + progress.
  1015. +
  1016. +- Do nothing. The detachment process eventually times out as described in
  1017. + point 3.
  1018. +
  1019. +See :ref:`ioctls` for more details on these responses.
  1020. +
  1021. +It is important to note that, if the user presses the detach button at any
  1022. +point when a detachment operation is in progress (i.e. after the the EC has
  1023. +sent the initial *detach-request* event (``SDTX_EVENT_REQUEST``) and before
  1024. +it received the corresponding response concluding the process), the
  1025. +detachment process is canceled on the EC-level and an identical event is
  1026. +being sent. Thus a *detach-request* event, by itself, does not signal the
  1027. +start of the detachment process.
  1028. +
  1029. +The detachment process may further be canceled by the EC due to hardware
  1030. +failures or a low clipboard battery. This is done via a cancel event
  1031. +(``SDTX_EVENT_CANCEL``) with the corresponding cancel reason.
  1032. +
  1033. +
  1034. +User-Space Interface Documentation
  1035. +==================================
  1036. +
  1037. +Error Codes and Status Values
  1038. +-----------------------------
  1039. +
  1040. +Error and status codes are divided into different categories, which can be
  1041. +used to determine if the status code is an error, and, if it is, the
  1042. +severity and type of that error. The current categories are:
  1043. +
  1044. +.. flat-table:: Overview of Status/Error Categories.
  1045. + :widths: 2 1 3
  1046. + :header-rows: 1
  1047. +
  1048. + * - Name
  1049. + - Value
  1050. + - Short Description
  1051. +
  1052. + * - ``STATUS``
  1053. + - ``0x0000``
  1054. + - Non-error status codes.
  1055. +
  1056. + * - ``RUNTIME_ERROR``
  1057. + - ``0x1000``
  1058. + - Non-critical runtime errors.
  1059. +
  1060. + * - ``HARDWARE_ERROR``
  1061. + - ``0x2000``
  1062. + - Critical hardware failures.
  1063. +
  1064. + * - ``UNKNOWN``
  1065. + - ``0xF000``
  1066. + - Unknown error codes.
  1067. +
  1068. +Other categories are reserved for future use. The ``SDTX_CATEGORY()`` macro
  1069. +can be used to determine the category of any status value. The
  1070. +``SDTX_SUCCESS()`` macro can be used to check if the status value is a
  1071. +success value (``SDTX_CATEGORY_STATUS``) or if it indicates a failure.
  1072. +
  1073. +Unknown status or error codes sent by the EC are assigned to the ``UNKNOWN``
  1074. +category by the driver and may be implemented via their own code in the
  1075. +future.
  1076. +
  1077. +Currently used error codes are:
  1078. +
  1079. +.. flat-table:: Overview of Error Codes.
  1080. + :widths: 2 1 1 3
  1081. + :header-rows: 1
  1082. +
  1083. + * - Name
  1084. + - Category
  1085. + - Value
  1086. + - Short Description
  1087. +
  1088. + * - ``SDTX_DETACH_NOT_FEASIBLE``
  1089. + - ``RUNTIME``
  1090. + - ``0x1001``
  1091. + - Detachment not feasible due to low clipboard battery.
  1092. +
  1093. + * - ``SDTX_DETACH_TIMEDOUT``
  1094. + - ``RUNTIME``
  1095. + - ``0x1002``
  1096. + - Detachment process timed out while the latch was locked.
  1097. +
  1098. + * - ``SDTX_ERR_FAILED_TO_OPEN``
  1099. + - ``HARDWARE``
  1100. + - ``0x2001``
  1101. + - Failed to open latch.
  1102. +
  1103. + * - ``SDTX_ERR_FAILED_TO_REMAIN_OPEN``
  1104. + - ``HARDWARE``
  1105. + - ``0x2002``
  1106. + - Failed to keep latch open.
  1107. +
  1108. + * - ``SDTX_ERR_FAILED_TO_CLOSE``
  1109. + - ``HARDWARE``
  1110. + - ``0x2003``
  1111. + - Failed to close latch.
  1112. +
  1113. +Other error codes are reserved for future use. Non-error status codes may
  1114. +overlap and are generally only unique within their use-case:
  1115. +
  1116. +.. flat-table:: Latch Status Codes.
  1117. + :widths: 2 1 1 3
  1118. + :header-rows: 1
  1119. +
  1120. + * - Name
  1121. + - Category
  1122. + - Value
  1123. + - Short Description
  1124. +
  1125. + * - ``SDTX_LATCH_CLOSED``
  1126. + - ``STATUS``
  1127. + - ``0x0000``
  1128. + - Latch is closed/has been closed.
  1129. +
  1130. + * - ``SDTX_LATCH_OPENED``
  1131. + - ``STATUS``
  1132. + - ``0x0001``
  1133. + - Latch is open/has been opened.
  1134. +
  1135. +.. flat-table:: Base State Codes.
  1136. + :widths: 2 1 1 3
  1137. + :header-rows: 1
  1138. +
  1139. + * - Name
  1140. + - Category
  1141. + - Value
  1142. + - Short Description
  1143. +
  1144. + * - ``SDTX_BASE_DETACHED``
  1145. + - ``STATUS``
  1146. + - ``0x0000``
  1147. + - Base has been detached/is not present.
  1148. +
  1149. + * - ``SDTX_BASE_ATTACHED``
  1150. + - ``STATUS``
  1151. + - ``0x0001``
  1152. + - Base has been attached/is present.
  1153. +
  1154. +Again, other codes are reserved for future use.
  1155. +
  1156. +.. _events:
  1157. +
  1158. +Events
  1159. +------
  1160. +
  1161. +Events can be received by reading from the device file. They are disabled by
  1162. +default and have to be enabled by executing ``SDTX_IOCTL_EVENTS_ENABLE``
  1163. +first. All events follow the layout prescribed by |sdtx_event|. Specific
  1164. +event types can be identified by their event code, described in
  1165. +|sdtx_event_code|. Note that other event codes are reserved for future use,
  1166. +thus an event parser must be able to handle any unknown/unsupported event
  1167. +types gracefully, by relying on the payload length given in the event header.
  1168. +
  1169. +Currently provided event types are:
  1170. +
  1171. +.. flat-table:: Overview of DTX events.
  1172. + :widths: 2 1 1 3
  1173. + :header-rows: 1
  1174. +
  1175. + * - Name
  1176. + - Code
  1177. + - Payload
  1178. + - Short Description
  1179. +
  1180. + * - ``SDTX_EVENT_REQUEST``
  1181. + - ``1``
  1182. + - ``0`` bytes
  1183. + - Detachment process initiated/aborted.
  1184. +
  1185. + * - ``SDTX_EVENT_CANCEL``
  1186. + - ``2``
  1187. + - ``2`` bytes
  1188. + - EC canceled detachment process.
  1189. +
  1190. + * - ``SDTX_EVENT_BASE_CONNECTION``
  1191. + - ``3``
  1192. + - ``4`` bytes
  1193. + - Base connection state changed.
  1194. +
  1195. + * - ``SDTX_EVENT_LATCH_STATUS``
  1196. + - ``4``
  1197. + - ``2`` bytes
  1198. + - Latch status changed.
  1199. +
  1200. + * - ``SDTX_EVENT_DEVICE_MODE``
  1201. + - ``5``
  1202. + - ``2`` bytes
  1203. + - Device mode changed.
  1204. +
  1205. +Individual events in more detail:
  1206. +
  1207. +``SDTX_EVENT_REQUEST``
  1208. +^^^^^^^^^^^^^^^^^^^^^^
  1209. +
  1210. +Sent when a detachment process is started or, if in progress, aborted by the
  1211. +user, either via a detach button press or a detach request
  1212. +(``SDTX_IOCTL_LATCH_REQUEST``) being sent from user-space.
  1213. +
  1214. +Does not have any payload.
  1215. +
  1216. +``SDTX_EVENT_CANCEL``
  1217. +^^^^^^^^^^^^^^^^^^^^^
  1218. +
  1219. +Sent when a detachment process is canceled by the EC due to unfulfilled
  1220. +preconditions (e.g. clipboard battery too low to detach) or hardware
  1221. +failure. The reason for cancellation is given in the event payload detailed
  1222. +below and can be one of
  1223. +
  1224. +* ``SDTX_DETACH_TIMEDOUT``: Detachment timed out while the latch was locked.
  1225. + The latch has neither been opened nor unlocked.
  1226. +
  1227. +* ``SDTX_DETACH_NOT_FEASIBLE``: Detachment not feasible due to low clipboard
  1228. + battery.
  1229. +
  1230. +* ``SDTX_ERR_FAILED_TO_OPEN``: Could not open the latch (hardware failure).
  1231. +
  1232. +* ``SDTX_ERR_FAILED_TO_REMAIN_OPEN``: Could not keep the latch open (hardware
  1233. + failure).
  1234. +
  1235. +* ``SDTX_ERR_FAILED_TO_CLOSE``: Could not close the latch (hardware failure).
  1236. +
  1237. +Other error codes in this context are reserved for future use.
  1238. +
  1239. +These codes can be classified via the ``SDTX_CATEGORY()`` macro to discern
  1240. +between critical hardware errors (``SDTX_CATEGORY_HARDWARE_ERROR``) or
  1241. +runtime errors (``SDTX_CATEGORY_RUNTIME_ERROR``), the latter of which may
  1242. +happen during normal operation if certain preconditions for detachment are
  1243. +not given.
  1244. +
  1245. +.. flat-table:: Detachment Cancel Event Payload
  1246. + :widths: 1 1 4
  1247. + :header-rows: 1
  1248. +
  1249. + * - Field
  1250. + - Type
  1251. + - Description
  1252. +
  1253. + * - ``reason``
  1254. + - |__u16|
  1255. + - Reason for cancellation.
  1256. +
  1257. +``SDTX_EVENT_BASE_CONNECTION``
  1258. +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1259. +
  1260. +Sent when the base connection state has changed, i.e. when the base has been
  1261. +attached, detached, or detachment has become infeasible due to low clipboard
  1262. +battery. The new state and, if a base is connected, ID of the base is
  1263. +provided as payload of type |sdtx_base_info| with its layout presented
  1264. +below:
  1265. +
  1266. +.. flat-table:: Base-Connection-Change Event Payload
  1267. + :widths: 1 1 4
  1268. + :header-rows: 1
  1269. +
  1270. + * - Field
  1271. + - Type
  1272. + - Description
  1273. +
  1274. + * - ``state``
  1275. + - |__u16|
  1276. + - Base connection state.
  1277. +
  1278. + * - ``base_id``
  1279. + - |__u16|
  1280. + - Type of base connected (zero if none).
  1281. +
  1282. +Possible values for ``state`` are:
  1283. +
  1284. +* ``SDTX_BASE_DETACHED``,
  1285. +* ``SDTX_BASE_ATTACHED``, and
  1286. +* ``SDTX_DETACH_NOT_FEASIBLE``.
  1287. +
  1288. +Other values are reserved for future use.
  1289. +
  1290. +``SDTX_EVENT_LATCH_STATUS``
  1291. +^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1292. +
  1293. +Sent when the latch status has changed, i.e. when the latch has been opened,
  1294. +closed, or an error occurred. The current status is provided as payload:
  1295. +
  1296. +.. flat-table:: Latch-Status-Change Event Payload
  1297. + :widths: 1 1 4
  1298. + :header-rows: 1
  1299. +
  1300. + * - Field
  1301. + - Type
  1302. + - Description
  1303. +
  1304. + * - ``status``
  1305. + - |__u16|
  1306. + - Latch status.
  1307. +
  1308. +Possible values for ``status`` are:
  1309. +
  1310. +* ``SDTX_LATCH_CLOSED``,
  1311. +* ``SDTX_LATCH_OPENED``,
  1312. +* ``SDTX_ERR_FAILED_TO_OPEN``,
  1313. +* ``SDTX_ERR_FAILED_TO_REMAIN_OPEN``, and
  1314. +* ``SDTX_ERR_FAILED_TO_CLOSE``.
  1315. +
  1316. +Other values are reserved for future use.
  1317. +
  1318. +``SDTX_EVENT_DEVICE_MODE``
  1319. +^^^^^^^^^^^^^^^^^^^^^^^^^^
  1320. +
  1321. +Sent when the device mode has changed. The new device mode is provided as
  1322. +payload:
  1323. +
  1324. +.. flat-table:: Device-Mode-Change Event Payload
  1325. + :widths: 1 1 4
  1326. + :header-rows: 1
  1327. +
  1328. + * - Field
  1329. + - Type
  1330. + - Description
  1331. +
  1332. + * - ``mode``
  1333. + - |__u16|
  1334. + - Device operation mode.
  1335. +
  1336. +Possible values for ``mode`` are:
  1337. +
  1338. +* ``SDTX_DEVICE_MODE_TABLET``,
  1339. +* ``SDTX_DEVICE_MODE_LAPTOP``, and
  1340. +* ``SDTX_DEVICE_MODE_STUDIO``.
  1341. +
  1342. +Other values are reserved for future use.
  1343. +
  1344. +.. _ioctls:
  1345. +
  1346. +IOCTLs
  1347. +------
  1348. +
  1349. +The following IOCTLs are provided:
  1350. +
  1351. +.. flat-table:: Overview of DTX IOCTLs
  1352. + :widths: 1 1 1 1 4
  1353. + :header-rows: 1
  1354. +
  1355. + * - Type
  1356. + - Number
  1357. + - Direction
  1358. + - Name
  1359. + - Description
  1360. +
  1361. + * - ``0xA5``
  1362. + - ``0x21``
  1363. + - ``-``
  1364. + - ``EVENTS_ENABLE``
  1365. + - Enable events for the current file descriptor.
  1366. +
  1367. + * - ``0xA5``
  1368. + - ``0x22``
  1369. + - ``-``
  1370. + - ``EVENTS_DISABLE``
  1371. + - Disable events for the current file descriptor.
  1372. +
  1373. + * - ``0xA5``
  1374. + - ``0x23``
  1375. + - ``-``
  1376. + - ``LATCH_LOCK``
  1377. + - Lock the latch.
  1378. +
  1379. + * - ``0xA5``
  1380. + - ``0x24``
  1381. + - ``-``
  1382. + - ``LATCH_UNLOCK``
  1383. + - Unlock the latch.
  1384. +
  1385. + * - ``0xA5``
  1386. + - ``0x25``
  1387. + - ``-``
  1388. + - ``LATCH_REQUEST``
  1389. + - Request clipboard detachment.
  1390. +
  1391. + * - ``0xA5``
  1392. + - ``0x26``
  1393. + - ``-``
  1394. + - ``LATCH_CONFIRM``
  1395. + - Confirm clipboard detachment request.
  1396. +
  1397. + * - ``0xA5``
  1398. + - ``0x27``
  1399. + - ``-``
  1400. + - ``LATCH_HEARTBEAT``
  1401. + - Send heartbeat signal to EC.
  1402. +
  1403. + * - ``0xA5``
  1404. + - ``0x28``
  1405. + - ``-``
  1406. + - ``LATCH_CANCEL``
  1407. + - Cancel detachment process.
  1408. +
  1409. + * - ``0xA5``
  1410. + - ``0x29``
  1411. + - ``R``
  1412. + - ``GET_BASE_INFO``
  1413. + - Get current base/connection information.
  1414. +
  1415. + * - ``0xA5``
  1416. + - ``0x2A``
  1417. + - ``R``
  1418. + - ``GET_DEVICE_MODE``
  1419. + - Get current device operation mode.
  1420. +
  1421. + * - ``0xA5``
  1422. + - ``0x2B``
  1423. + - ``R``
  1424. + - ``GET_LATCH_STATUS``
  1425. + - Get current device latch status.
  1426. +
  1427. +``SDTX_IOCTL_EVENTS_ENABLE``
  1428. +^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1429. +
  1430. +Defined as ``_IO(0xA5, 0x22)``.
  1431. +
  1432. +Enable events for the current file descriptor. Events can be obtained by
  1433. +reading from the device, if enabled. Events are disabled by default.
  1434. +
  1435. +``SDTX_IOCTL_EVENTS_DISABLE``
  1436. +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1437. +
  1438. +Defined as ``_IO(0xA5, 0x22)``.
  1439. +
  1440. +Disable events for the current file descriptor. Events can be obtained by
  1441. +reading from the device, if enabled. Events are disabled by default.
  1442. +
  1443. +``SDTX_IOCTL_LATCH_LOCK``
  1444. +^^^^^^^^^^^^^^^^^^^^^^^^^
  1445. +
  1446. +Defined as ``_IO(0xA5, 0x23)``.
  1447. +
  1448. +Locks the latch, causing the detachment procedure to abort without opening
  1449. +the latch on timeout. The latch is unlocked by default. This command will be
  1450. +silently ignored if the latch is already locked.
  1451. +
  1452. +``SDTX_IOCTL_LATCH_UNLOCK``
  1453. +^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1454. +
  1455. +Defined as ``_IO(0xA5, 0x24)``.
  1456. +
  1457. +Unlocks the latch, causing the detachment procedure to open the latch on
  1458. +timeout. The latch is unlocked by default. This command will not open the
  1459. +latch when sent during an ongoing detachment process. It will be silently
  1460. +ignored if the latch is already unlocked.
  1461. +
  1462. +``SDTX_IOCTL_LATCH_REQUEST``
  1463. +^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1464. +
  1465. +Defined as ``_IO(0xA5, 0x25)``.
  1466. +
  1467. +Generic latch request. Behavior depends on the context: If no
  1468. +detachment-process is active, detachment is requested. Otherwise the
  1469. +currently active detachment-process will be aborted.
  1470. +
  1471. +If a detachment process is canceled by this operation, a generic detachment
  1472. +request event (``SDTX_EVENT_REQUEST``) will be sent.
  1473. +
  1474. +This essentially behaves the same as a detachment button press.
  1475. +
  1476. +``SDTX_IOCTL_LATCH_CONFIRM``
  1477. +^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1478. +
  1479. +Defined as ``_IO(0xA5, 0x26)``.
  1480. +
  1481. +Acknowledges and confirms a latch request. If sent during an ongoing
  1482. +detachment process, this command causes the latch to be opened immediately.
  1483. +The latch will also be opened if it has been locked. In this case, the latch
  1484. +lock is reset to the unlocked state.
  1485. +
  1486. +This command will be silently ignored if there is currently no detachment
  1487. +procedure in progress.
  1488. +
  1489. +``SDTX_IOCTL_LATCH_HEARTBEAT``
  1490. +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1491. +
  1492. +Defined as ``_IO(0xA5, 0x27)``.
  1493. +
  1494. +Sends a heartbeat, essentially resetting the detachment timeout. This
  1495. +command can be used to keep the detachment process alive while work required
  1496. +for the detachment to succeed is still in progress.
  1497. +
  1498. +This command will be silently ignored if there is currently no detachment
  1499. +procedure in progress.
  1500. +
  1501. +``SDTX_IOCTL_LATCH_CANCEL``
  1502. +^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1503. +
  1504. +Defined as ``_IO(0xA5, 0x28)``.
  1505. +
  1506. +Cancels detachment in progress (if any). If a detachment process is canceled
  1507. +by this operation, a generic detachment request event
  1508. +(``SDTX_EVENT_REQUEST``) will be sent.
  1509. +
  1510. +This command will be silently ignored if there is currently no detachment
  1511. +procedure in progress.
  1512. +
  1513. +``SDTX_IOCTL_GET_BASE_INFO``
  1514. +^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1515. +
  1516. +Defined as ``_IOR(0xA5, 0x29, struct sdtx_base_info)``.
  1517. +
  1518. +Get the current base connection state (i.e. attached/detached) and the type
  1519. +of the base connected to the clipboard. This is command essentially provides
  1520. +a way to query the information provided by the base connection change event
  1521. +(``SDTX_EVENT_BASE_CONNECTION``).
  1522. +
  1523. +Possible values for ``struct sdtx_base_info.state`` are:
  1524. +
  1525. +* ``SDTX_BASE_DETACHED``,
  1526. +* ``SDTX_BASE_ATTACHED``, and
  1527. +* ``SDTX_DETACH_NOT_FEASIBLE``.
  1528. +
  1529. +Other values are reserved for future use.
  1530. +
  1531. +``SDTX_IOCTL_GET_DEVICE_MODE``
  1532. +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1533. +
  1534. +Defined as ``_IOR(0xA5, 0x2A, __u16)``.
  1535. +
  1536. +Returns the device operation mode, indicating if and how the base is
  1537. +attached to the clipboard. This is command essentially provides a way to
  1538. +query the information provided by the device mode change event
  1539. +(``SDTX_EVENT_DEVICE_MODE``).
  1540. +
  1541. +Returned values are:
  1542. +
  1543. +* ``SDTX_DEVICE_MODE_LAPTOP``
  1544. +* ``SDTX_DEVICE_MODE_TABLET``
  1545. +* ``SDTX_DEVICE_MODE_STUDIO``
  1546. +
  1547. +See |sdtx_device_mode| for details. Other values are reserved for future
  1548. +use.
  1549. +
  1550. +
  1551. +``SDTX_IOCTL_GET_LATCH_STATUS``
  1552. +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1553. +
  1554. +Defined as ``_IOR(0xA5, 0x2B, __u16)``.
  1555. +
  1556. +Get the current latch status or (presumably) the last error encountered when
  1557. +trying to open/close the latch. This is command essentially provides a way
  1558. +to query the information provided by the latch status change event
  1559. +(``SDTX_EVENT_LATCH_STATUS``).
  1560. +
  1561. +Returned values are:
  1562. +
  1563. +* ``SDTX_LATCH_CLOSED``,
  1564. +* ``SDTX_LATCH_OPENED``,
  1565. +* ``SDTX_ERR_FAILED_TO_OPEN``,
  1566. +* ``SDTX_ERR_FAILED_TO_REMAIN_OPEN``, and
  1567. +* ``SDTX_ERR_FAILED_TO_CLOSE``.
  1568. +
  1569. +Other values are reserved for future use.
  1570. +
  1571. +A Note on Base IDs
  1572. +------------------
  1573. +
  1574. +Base types/IDs provided via ``SDTX_EVENT_BASE_CONNECTION`` or
  1575. +``SDTX_IOCTL_GET_BASE_INFO`` are directly forwarded from from the EC in the
  1576. +lower byte of the combined |__u16| value, with the driver storing the EC
  1577. +type from which this ID comes in the high byte (without this, base IDs over
  1578. +different types of ECs may be overlapping).
  1579. +
  1580. +The ``SDTX_DEVICE_TYPE()`` macro can be used to determine the EC device
  1581. +type. This can be one of
  1582. +
  1583. +* ``SDTX_DEVICE_TYPE_HID``, for Surface Aggregator Module over HID, and
  1584. +
  1585. +* ``SDTX_DEVICE_TYPE_SSH``, for Surface Aggregator Module over Surface Serial
  1586. + Hub.
  1587. +
  1588. +Note that currently only the ``SSH`` type EC is supported, however ``HID``
  1589. +type is reserved for future use.
  1590. +
  1591. +Structures and Enums
  1592. +--------------------
  1593. +
  1594. +.. kernel-doc:: include/uapi/linux/surface_aggregator/dtx.h
  1595. diff --git a/Documentation/driver-api/surface_aggregator/clients/index.rst b/Documentation/driver-api/surface_aggregator/clients/index.rst
  1596. new file mode 100644
  1597. index 000000000000..98ea9946b8a2
  1598. --- /dev/null
  1599. +++ b/Documentation/driver-api/surface_aggregator/clients/index.rst
  1600. @@ -0,0 +1,22 @@
  1601. +.. SPDX-License-Identifier: GPL-2.0+
  1602. +
  1603. +===========================
  1604. +Client Driver Documentation
  1605. +===========================
  1606. +
  1607. +This is the documentation for client drivers themselves. Refer to
  1608. +:doc:`../client` for documentation on how to write client drivers.
  1609. +
  1610. +.. toctree::
  1611. + :maxdepth: 1
  1612. +
  1613. + cdev
  1614. + dtx
  1615. + san
  1616. +
  1617. +.. only:: subproject and html
  1618. +
  1619. + Indices
  1620. + =======
  1621. +
  1622. + * :ref:`genindex`
  1623. diff --git a/Documentation/driver-api/surface_aggregator/clients/san.rst b/Documentation/driver-api/surface_aggregator/clients/san.rst
  1624. new file mode 100644
  1625. index 000000000000..1bf830ad367d
  1626. --- /dev/null
  1627. +++ b/Documentation/driver-api/surface_aggregator/clients/san.rst
  1628. @@ -0,0 +1,44 @@
  1629. +.. SPDX-License-Identifier: GPL-2.0+
  1630. +
  1631. +.. |san_client_link| replace:: :c:func:`san_client_link`
  1632. +.. |san_dgpu_notifier_register| replace:: :c:func:`san_dgpu_notifier_register`
  1633. +.. |san_dgpu_notifier_unregister| replace:: :c:func:`san_dgpu_notifier_unregister`
  1634. +
  1635. +===================
  1636. +Surface ACPI Notify
  1637. +===================
  1638. +
  1639. +The Surface ACPI Notify (SAN) device provides the bridge between ACPI and
  1640. +SAM controller. Specifically, ACPI code can execute requests and handle
  1641. +battery and thermal events via this interface. In addition to this, events
  1642. +relating to the discrete GPU (dGPU) of the Surface Book 2 can be sent from
  1643. +ACPI code (note: the Surface Book 3 uses a different method for this). The
  1644. +only currently known event sent via this interface is a dGPU power-on
  1645. +notification. While this driver handles the former part internally, it only
  1646. +relays the dGPU events to any other driver interested via its public API and
  1647. +does not handle them.
  1648. +
  1649. +The public interface of this driver is split into two parts: Client
  1650. +registration and notifier-block registration.
  1651. +
  1652. +A client to the SAN interface can be linked as consumer to the SAN device
  1653. +via |san_client_link|. This can be used to ensure that the a client
  1654. +receiving dGPU events does not miss any events due to the SAN interface not
  1655. +being set up as this forces the client driver to unbind once the SAN driver
  1656. +is unbound.
  1657. +
  1658. +Notifier-blocks can be registered by any device for as long as the module is
  1659. +loaded, regardless of being linked as client or not. Registration is done
  1660. +with |san_dgpu_notifier_register|. If the notifier is not needed any more, it
  1661. +should be unregistered via |san_dgpu_notifier_unregister|.
  1662. +
  1663. +Consult the API documentation below for more details.
  1664. +
  1665. +
  1666. +API Documentation
  1667. +=================
  1668. +
  1669. +.. kernel-doc:: include/linux/surface_acpi_notify.h
  1670. +
  1671. +.. kernel-doc:: drivers/misc/surface_aggregator/clients/surface_acpi_notify.c
  1672. + :export:
  1673. diff --git a/Documentation/driver-api/surface_aggregator/index.rst b/Documentation/driver-api/surface_aggregator/index.rst
  1674. new file mode 100644
  1675. index 000000000000..6f3e1094904d
  1676. --- /dev/null
  1677. +++ b/Documentation/driver-api/surface_aggregator/index.rst
  1678. @@ -0,0 +1,21 @@
  1679. +.. SPDX-License-Identifier: GPL-2.0+
  1680. +
  1681. +=======================================
  1682. +Surface System Aggregator Module (SSAM)
  1683. +=======================================
  1684. +
  1685. +.. toctree::
  1686. + :maxdepth: 2
  1687. +
  1688. + overview
  1689. + client
  1690. + clients/index
  1691. + ssh
  1692. + internal
  1693. +
  1694. +.. only:: subproject and html
  1695. +
  1696. + Indices
  1697. + =======
  1698. +
  1699. + * :ref:`genindex`
  1700. diff --git a/Documentation/driver-api/surface_aggregator/internal-api.rst b/Documentation/driver-api/surface_aggregator/internal-api.rst
  1701. new file mode 100644
  1702. index 000000000000..db6a70119f49
  1703. --- /dev/null
  1704. +++ b/Documentation/driver-api/surface_aggregator/internal-api.rst
  1705. @@ -0,0 +1,67 @@
  1706. +.. SPDX-License-Identifier: GPL-2.0+
  1707. +
  1708. +==========================
  1709. +Internal API Documentation
  1710. +==========================
  1711. +
  1712. +.. contents::
  1713. + :depth: 2
  1714. +
  1715. +
  1716. +Packet Transport Layer
  1717. +======================
  1718. +
  1719. +.. kernel-doc:: drivers/misc/surface_aggregator/ssh_parser.h
  1720. + :internal:
  1721. +
  1722. +.. kernel-doc:: drivers/misc/surface_aggregator/ssh_parser.c
  1723. + :internal:
  1724. +
  1725. +.. kernel-doc:: drivers/misc/surface_aggregator/ssh_msgb.h
  1726. + :internal:
  1727. +
  1728. +.. kernel-doc:: drivers/misc/surface_aggregator/ssh_packet_layer.h
  1729. + :internal:
  1730. +
  1731. +.. kernel-doc:: drivers/misc/surface_aggregator/ssh_packet_layer.c
  1732. + :internal:
  1733. +
  1734. +
  1735. +Request Transport Layer
  1736. +=======================
  1737. +
  1738. +.. kernel-doc:: drivers/misc/surface_aggregator/ssh_request_layer.h
  1739. + :internal:
  1740. +
  1741. +.. kernel-doc:: drivers/misc/surface_aggregator/ssh_request_layer.c
  1742. + :internal:
  1743. +
  1744. +
  1745. +Controller
  1746. +==========
  1747. +
  1748. +.. kernel-doc:: drivers/misc/surface_aggregator/controller.h
  1749. + :internal:
  1750. +
  1751. +.. kernel-doc:: drivers/misc/surface_aggregator/controller.c
  1752. + :internal:
  1753. +
  1754. +
  1755. +Client Device Bus
  1756. +=================
  1757. +
  1758. +.. kernel-doc:: drivers/misc/surface_aggregator/bus.c
  1759. + :internal:
  1760. +
  1761. +
  1762. +Core
  1763. +====
  1764. +
  1765. +.. kernel-doc:: drivers/misc/surface_aggregator/core.c
  1766. + :internal:
  1767. +
  1768. +
  1769. +Trace Helpers
  1770. +=============
  1771. +
  1772. +.. kernel-doc:: drivers/misc/surface_aggregator/trace.h
  1773. diff --git a/Documentation/driver-api/surface_aggregator/internal.rst b/Documentation/driver-api/surface_aggregator/internal.rst
  1774. new file mode 100644
  1775. index 000000000000..72704734982a
  1776. --- /dev/null
  1777. +++ b/Documentation/driver-api/surface_aggregator/internal.rst
  1778. @@ -0,0 +1,577 @@
  1779. +.. SPDX-License-Identifier: GPL-2.0+
  1780. +
  1781. +.. |ssh_ptl| replace:: :c:type:`struct ssh_ptl <ssh_ptl>`
  1782. +.. |ssh_ptl_submit| replace:: :c:func:`ssh_ptl_submit`
  1783. +.. |ssh_ptl_cancel| replace:: :c:func:`ssh_ptl_cancel`
  1784. +.. |ssh_ptl_shutdown| replace:: :c:func:`ssh_ptl_shutdown`
  1785. +.. |ssh_ptl_rx_rcvbuf| replace:: :c:func:`ssh_ptl_rx_rcvbuf`
  1786. +.. |ssh_rtl| replace:: :c:type:`struct ssh_rtl <ssh_rtl>`
  1787. +.. |ssh_rtl_submit| replace:: :c:func:`ssh_rtl_submit`
  1788. +.. |ssh_rtl_cancel| replace:: :c:func:`ssh_rtl_cancel`
  1789. +.. |ssh_rtl_shutdown| replace:: :c:func:`ssh_rtl_shutdown`
  1790. +.. |ssh_packet| replace:: :c:type:`struct ssh_packet <ssh_packet>`
  1791. +.. |ssh_packet_get| replace:: :c:func:`ssh_packet_get`
  1792. +.. |ssh_packet_put| replace:: :c:func:`ssh_packet_put`
  1793. +.. |ssh_packet_ops| replace:: :c:type:`struct ssh_packet_ops <ssh_packet_ops>`
  1794. +.. |ssh_packet_base_priority| replace:: :c:type:`enum ssh_packet_base_priority <ssh_packet_base_priority>`
  1795. +.. |ssh_packet_flags| replace:: :c:type:`enum ssh_packet_flags <ssh_packet_flags>`
  1796. +.. |SSH_PACKET_PRIORITY| replace:: :c:func:`SSH_PACKET_PRIORITY`
  1797. +.. |ssh_frame| replace:: :c:type:`struct ssh_frame <ssh_frame>`
  1798. +.. |ssh_command| replace:: :c:type:`struct ssh_command <ssh_command>`
  1799. +.. |ssh_request| replace:: :c:type:`struct ssh_request <ssh_request>`
  1800. +.. |ssh_request_get| replace:: :c:func:`ssh_request_get`
  1801. +.. |ssh_request_put| replace:: :c:func:`ssh_request_put`
  1802. +.. |ssh_request_ops| replace:: :c:type:`struct ssh_request_ops <ssh_request_ops>`
  1803. +.. |ssh_request_init| replace:: :c:func:`ssh_request_init`
  1804. +.. |ssh_request_flags| replace:: :c:type:`enum ssh_request_flags <ssh_request_flags>`
  1805. +.. |ssam_controller| replace:: :c:type:`struct ssam_controller <ssam_controller>`
  1806. +.. |ssam_device| replace:: :c:type:`struct ssam_device <ssam_device>`
  1807. +.. |ssam_device_driver| replace:: :c:type:`struct ssam_device_driver <ssam_device_driver>`
  1808. +.. |ssam_client_bind| replace:: :c:func:`ssam_client_bind`
  1809. +.. |ssam_client_link| replace:: :c:func:`ssam_client_link`
  1810. +.. |ssam_request_sync| replace:: :c:type:`struct ssam_request_sync <ssam_request_sync>`
  1811. +.. |ssam_event_registry| replace:: :c:type:`struct ssam_event_registry <ssam_event_registry>`
  1812. +.. |ssam_event_id| replace:: :c:type:`struct ssam_event_id <ssam_event_id>`
  1813. +.. |ssam_nf| replace:: :c:type:`struct ssam_nf <ssam_nf>`
  1814. +.. |ssam_nf_refcount_inc| replace:: :c:func:`ssam_nf_refcount_inc`
  1815. +.. |ssam_nf_refcount_dec| replace:: :c:func:`ssam_nf_refcount_dec`
  1816. +.. |ssam_notifier_register| replace:: :c:func:`ssam_notifier_register`
  1817. +.. |ssam_notifier_unregister| replace:: :c:func:`ssam_notifier_unregister`
  1818. +.. |ssam_cplt| replace:: :c:type:`struct ssam_cplt <ssam_cplt>`
  1819. +.. |ssam_event_queue| replace:: :c:type:`struct ssam_event_queue <ssam_event_queue>`
  1820. +.. |ssam_request_sync_submit| replace:: :c:func:`ssam_request_sync_submit`
  1821. +
  1822. +=====================
  1823. +Core Driver Internals
  1824. +=====================
  1825. +
  1826. +Architectural overview of the Surface System Aggregator Module (SSAM) core
  1827. +and Surface Serial Hub (SSH) driver. For the API documentation, refer to:
  1828. +
  1829. +.. toctree::
  1830. + :maxdepth: 2
  1831. +
  1832. + internal-api
  1833. +
  1834. +
  1835. +Overview
  1836. +========
  1837. +
  1838. +The SSAM core implementation is structured in layers, somewhat following the
  1839. +SSH protocol structure:
  1840. +
  1841. +Lower-level packet transport is implemented in the *packet transport layer
  1842. +(PTL)*, directly building on top of the serial device (serdev)
  1843. +infrastructure of the kernel. As the name indicates, this layer deals with
  1844. +the packet transport logic and handles things like packet validation, packet
  1845. +acknowledgment (ACKing), packet (retransmission) timeouts, and relaying
  1846. +packet payloads to higher-level layers.
  1847. +
  1848. +Above this sits the *request transport layer (RTL)*. This layer is centered
  1849. +around command-type packet payloads, i.e. requests (sent from host to EC),
  1850. +responses of the EC to those requests, and events (sent from EC to host).
  1851. +It, specifically, distinguishes events from request responses, matches
  1852. +responses to their corresponding requests, and implements request timeouts.
  1853. +
  1854. +The *controller* layer is building on top of this and essentially decides
  1855. +how request responses and, especially, events are dealt with. It provides an
  1856. +event notifier system, handles event activation/deactivation, provides a
  1857. +workqueue for event and asynchronous request completion, and also manages
  1858. +the message counters required for building command messages (``SEQ``,
  1859. +``RQID``). This layer basically provides a fundamental interface to the SAM
  1860. +EC for use in other kernel drivers.
  1861. +
  1862. +While the controller layer already provides an interface for other kernel
  1863. +drivers, the client *bus* extends this interface to provide support for
  1864. +native SSAM devices, i.e. devices that are not defined in ACPI and not
  1865. +implemented as platform devices, via |ssam_device| and |ssam_device_driver|
  1866. +simplify management of client devices and client drivers.
  1867. +
  1868. +Refer to :doc:`client` for documentation regarding the client device/driver
  1869. +API and interface options for other kernel drivers. It is recommended to
  1870. +familiarize oneself with that chapter and the :doc:`ssh` before continuing
  1871. +with the architectural overview below.
  1872. +
  1873. +
  1874. +Packet Transport Layer
  1875. +======================
  1876. +
  1877. +The packet transport layer is represented via |ssh_ptl| and is structured
  1878. +around the following key concepts:
  1879. +
  1880. +Packets
  1881. +-------
  1882. +
  1883. +Packets are the fundamental transmission unit of the SSH protocol. They are
  1884. +managed by the packet transport layer, which is essentially the lowest layer
  1885. +of the driver and is built upon by other components of the SSAM core.
  1886. +Packets to be transmitted by the SSAM core are represented via |ssh_packet|
  1887. +(in contrast, packets received by the core do not have any specific
  1888. +structure and are managed entirely via the raw |ssh_frame|).
  1889. +
  1890. +This structure contains the required fields to manage the packet inside the
  1891. +transport layer, as well as a reference to the buffer containing the data to
  1892. +be transmitted (i.e. the message wrapped in |ssh_frame|). Most notably, it
  1893. +contains an internal reference count, which is used for managing its
  1894. +lifetime (accessible via |ssh_packet_get| and |ssh_packet_put|). When this
  1895. +counter reaches zero, the ``release()`` callback provided to the packet via
  1896. +its |ssh_packet_ops| reference is executed, which may then deallocate the
  1897. +packet or its enclosing structure (e.g. |ssh_request|).
  1898. +
  1899. +In addition to the ``release`` callback, the |ssh_packet_ops| reference also
  1900. +provides a ``complete()`` callback, which is run once the packet has been
  1901. +completed and provides the status of this completion, i.e. zero on success
  1902. +or a negative errno value in case of an error. Once the packet has been
  1903. +submitted to the packet transport layer, the ``complete()`` callback is
  1904. +always guaranteed to be executed before the ``release()`` callback, i.e. the
  1905. +packet will always be completed, either successfully, with an error, or due
  1906. +to cancellation, before it will be released.
  1907. +
  1908. +The state of a packet is managed via its ``state`` flags
  1909. +(|ssh_packet_flags|), which also contains the packet type. In particular,
  1910. +the following bits are noteworthy:
  1911. +
  1912. +* ``SSH_PACKET_SF_LOCKED_BIT``: This bit is set when completion, either
  1913. + through error or success, is imminent. It indicates that no further
  1914. + references of the packet should be taken and any existing references
  1915. + should be dropped as soon as possible. The process setting this bit is
  1916. + responsible for removing any references to this packet from the packet
  1917. + queue and pending set.
  1918. +
  1919. +* ``SSH_PACKET_SF_COMPLETED_BIT``: This bit is set by the process running the
  1920. + ``complete()`` callback and is used to ensure that this callback only runs
  1921. + once.
  1922. +
  1923. +* ``SSH_PACKET_SF_QUEUED_BIT``: This bit is set when the packet is queued on
  1924. + the packet queue and cleared when it is dequeued.
  1925. +
  1926. +* ``SSH_PACKET_SF_PENDING_BIT``: This bit is set when the packet is added to
  1927. + the pending set and cleared when it is removed from it.
  1928. +
  1929. +Packet Queue
  1930. +------------
  1931. +
  1932. +The packet queue is the first of the two fundamental collections in the
  1933. +packet transport layer. It is a priority queue, with priority of the
  1934. +respective packets based on the packet type (major) and number of tries
  1935. +(minor). See |SSH_PACKET_PRIORITY| for more details on the priority value.
  1936. +
  1937. +All packets to be transmitted by the transport layer must be submitted to
  1938. +this queue via |ssh_ptl_submit|. Note that this includes control packets
  1939. +sent by the transport layer itself. Internally, data packets can be
  1940. +re-submitted to this queue due to timeouts or NAK packets sent by the EC.
  1941. +
  1942. +Pending Set
  1943. +-----------
  1944. +
  1945. +The pending set is the second of the two fundamental collections in the
  1946. +packet transport layer. It stores references to packets that have already
  1947. +been transmitted, but wait for acknowledgment (e.g. the corresponding ACK
  1948. +packet) by the EC.
  1949. +
  1950. +Note that a packet may both be pending and queued if it has been
  1951. +re-submitted due to a packet acknowledgment timeout or NAK. On such a
  1952. +re-submission, packets are not removed from the pending set.
  1953. +
  1954. +Transmitter Thread
  1955. +------------------
  1956. +
  1957. +The transmitter thread is responsible for most of the actual work regarding
  1958. +packet transmission. In each iteration, it (waits for and) checks if the
  1959. +next packet on the queue (if any) can be transmitted and, if so, removes it
  1960. +from the queue and increments its counter for the number of transmission
  1961. +attempts, i.e. tries. If the packet is sequenced, i.e. requires an ACK by
  1962. +the EC, the packet is added to the pending set. Next, the packet's data is
  1963. +submitted to the serdev subsystem. In case of an error or timeout during
  1964. +this submission, the packet is completed by the transmitter thread with the
  1965. +status value of the callback set accordingly. In case the packet is
  1966. +unsequenced, i.e. does not require an ACK by the EC, the packet is completed
  1967. +with success on the transmitter thread.
  1968. +
  1969. +Transmission of sequenced packets is limited by the number of concurrently
  1970. +pending packets, i.e. a limit on how many packets may be waiting for an ACK
  1971. +from the EC in parallel. This limit is currently set to one (see :doc:`ssh`
  1972. +for the reasoning behind this). Control packets (i.e. ACK and NAK) can
  1973. +always be transmitted.
  1974. +
  1975. +Receiver Thread
  1976. +---------------
  1977. +
  1978. +Any data received from the EC is put into a FIFO buffer for further
  1979. +processing. This processing happens on the receiver thread. The receiver
  1980. +thread parses and validates the received message into its |ssh_frame| and
  1981. +corresponding payload. It prepares and submits the necessary ACK (and on
  1982. +validation error or invalid data NAK) packets for the received messages.
  1983. +
  1984. +This thread also handles further processing, such as matching ACK messages
  1985. +to the corresponding pending packet (via sequence ID) and completing it, as
  1986. +well as initiating re-submission of all currently pending packets on
  1987. +receival of a NAK message (re-submission in case of a NAK is similar to
  1988. +re-submission due to timeout, see below for more details on that). Note that
  1989. +the successful completion of a sequenced packet will always run on the
  1990. +receiver thread (whereas any failure-indicating completion will run on the
  1991. +process where the failure occurred).
  1992. +
  1993. +Any payload data is forwarded via a callback to the next upper layer, i.e.
  1994. +the request transport layer.
  1995. +
  1996. +Timeout Reaper
  1997. +--------------
  1998. +
  1999. +The packet acknowledgment timeout is a per-packet timeout for sequenced
  2000. +packets, started when the respective packet begins (re-)transmission (i.e.
  2001. +this timeout is armed once per transmission attempt on the transmitter
  2002. +thread). It is used to trigger re-submission or, when the number of tries
  2003. +has been exceeded, cancellation of the packet in question.
  2004. +
  2005. +This timeout is handled via a dedicated reaper task, which is essentially a
  2006. +work item (re-)scheduled to run when the next packet is set to time out. The
  2007. +work item then checks the set of pending packets for any packets that have
  2008. +exceeded the timeout and, if there are any remaining packets, re-schedules
  2009. +itself to the next appropriate point in time.
  2010. +
  2011. +If a timeout has been detected by the reaper, the packet will either be
  2012. +re-submitted if it still has some remaining tries left, or completed with
  2013. +``-ETIMEDOUT`` as status if not. Note that re-submission, in this case and
  2014. +triggered by receival of a NAK, means that the packet is added to the queue
  2015. +with a now incremented number of tries, yielding a higher priority. The
  2016. +timeout for the packet will be disabled until the next transmission attempt
  2017. +and the packet remains on the pending set.
  2018. +
  2019. +Note that due to transmission and packet acknowledgment timeouts, the packet
  2020. +transport layer is always guaranteed to make progress, if only through
  2021. +timing out packets, and will never fully block.
  2022. +
  2023. +Concurrency and Locking
  2024. +-----------------------
  2025. +
  2026. +There are two main locks in the packet transport layer: One guarding access
  2027. +to the packet queue and one guarding access to the pending set. These
  2028. +collections may only be accessed and modified under the respective lock. If
  2029. +access to both collections is needed, the pending lock must be acquired
  2030. +before the queue lock to avoid deadlocks.
  2031. +
  2032. +In addition to guarding the collections, after initial packet submission
  2033. +certain packet fields may only be accessed under one of the locks.
  2034. +Specifically, the packet priority must only be accessed while holding the
  2035. +queue lock and the packet timestamp must only be accessed while holding the
  2036. +pending lock.
  2037. +
  2038. +Other parts of the packet transport layer are guarded independently. State
  2039. +flags are managed by atomic bit operations and, if necessary, memory
  2040. +barriers. Modifications to the timeout reaper work item and expiration date
  2041. +are guarded by their own lock.
  2042. +
  2043. +The reference of the packet to the packet transport layer (``ptl``) is
  2044. +somewhat special. It is either set when the upper layer request is submitted
  2045. +or, if there is none, when the packet is first submitted. After it is set,
  2046. +it will not change its value. Functions that may run concurrently with
  2047. +submission, i.e. cancellation, can not rely on the ``ptl`` reference to be
  2048. +set. Access to it in these functions is guarded by ``READ_ONCE()``, whereas
  2049. +setting ``ptl`` is equally guarded with ``WRITE_ONCE()`` for symmetry.
  2050. +
  2051. +Some packet fields may be read outside of the respective locks guarding
  2052. +them, specifically priority and state for tracing. In those cases, proper
  2053. +access is ensured by employing ``WRITE_ONCE()`` and ``READ_ONCE()``. Such
  2054. +read-only access is only allowed when stale values are not critical.
  2055. +
  2056. +With respect to the interface for higher layers, packet submission
  2057. +(|ssh_ptl_submit|), packet cancellation (|ssh_ptl_cancel|), data receival
  2058. +(|ssh_ptl_rx_rcvbuf|), and layer shutdown (|ssh_ptl_shutdown|) may always be
  2059. +executed concurrently with respect to each other. Note that packet
  2060. +submission may not run concurrently with itself for the same packet.
  2061. +Equally, shutdown and data receival may also not run concurrently with
  2062. +themselves (but may run concurrently with each other).
  2063. +
  2064. +
  2065. +Request Transport Layer
  2066. +=======================
  2067. +
  2068. +The request transport layer is represented via |ssh_rtl| and builds on top
  2069. +of the packet transport layer. It deals with requests, i.e. SSH packets sent
  2070. +by the host containing a |ssh_command| as frame payload. This layer
  2071. +separates responses to requests from events, which are also sent by the EC
  2072. +via a |ssh_command| payload. While responses are handled in this layer,
  2073. +events are relayed to the next upper layer, i.e. the controller layer, via
  2074. +the corresponding callback. The request transport layer is structured around
  2075. +the following key concepts:
  2076. +
  2077. +Request
  2078. +-------
  2079. +
  2080. +Requests are packets with a command-type payload, sent from host to EC to
  2081. +query data from or trigger an action on it (or both simultaneously). They
  2082. +are represented by |ssh_request|, wrapping the underlying |ssh_packet|
  2083. +storing its message data (i.e. SSH frame with command payload). Note that
  2084. +all top-level representations, e.g. |ssam_request_sync| are built upon this
  2085. +struct.
  2086. +
  2087. +As |ssh_request| extends |ssh_packet|, its lifetime is also managed by the
  2088. +reference counter inside the packet struct (which can be accessed via
  2089. +|ssh_request_get| and |ssh_request_put|). Once the counter reaches zero, the
  2090. +``release()`` callback of the |ssh_request_ops| reference of the request is
  2091. +called.
  2092. +
  2093. +Requests can have an optional response that is equally sent via a SSH
  2094. +message with command-type payload (from EC to host). The party constructing
  2095. +the request must know if a response is expected and mark this in the request
  2096. +flags provided to |ssh_request_init|, so that the request transport layer
  2097. +can wait for this response.
  2098. +
  2099. +Similar to |ssh_packet|, |ssh_request| also has a ``complete()`` callback
  2100. +provided via its request ops reference and is guaranteed to be completed
  2101. +before it is released once it has been submitted to the request transport
  2102. +layer via |ssh_rtl_submit|. For a request without a response, successful
  2103. +completion will occur once the underlying packet has been successfully
  2104. +transmitted by the packet transport layer (i.e. from within the packet
  2105. +completion callback). For a request with response, successful completion
  2106. +will occur once the response has been received and matched to the request
  2107. +via its request ID (which happens on the packet layer's data-received
  2108. +callback running on the receiver thread). If the request is completed with
  2109. +an error, the status value will be set to the corresponding (negative) errno
  2110. +value.
  2111. +
  2112. +The state of a request is again managed via its ``state`` flags
  2113. +(|ssh_request_flags|), which also encode the request type. In particular,
  2114. +the following bits are noteworthy:
  2115. +
  2116. +* ``SSH_REQUEST_SF_LOCKED_BIT``: This bit is set when completion, either
  2117. + through error or success, is imminent. It indicates that no further
  2118. + references of the request should be taken and any existing references
  2119. + should be dropped as soon as possible. The process setting this bit is
  2120. + responsible for removing any references to this request from the request
  2121. + queue and pending set.
  2122. +
  2123. +* ``SSH_REQUEST_SF_COMPLETED_BIT``: This bit is set by the process running the
  2124. + ``complete()`` callback and is used to ensure that this callback only runs
  2125. + once.
  2126. +
  2127. +* ``SSH_REQUEST_SF_QUEUED_BIT``: This bit is set when the request is queued on
  2128. + the request queue and cleared when it is dequeued.
  2129. +
  2130. +* ``SSH_REQUEST_SF_PENDING_BIT``: This bit is set when the request is added to
  2131. + the pending set and cleared when it is removed from it.
  2132. +
  2133. +Request Queue
  2134. +-------------
  2135. +
  2136. +The request queue is the first of the two fundamental collections in the
  2137. +request transport layer. In contrast to the packet queue of the packet
  2138. +transport layer, it is not a priority queue and the simple first come first
  2139. +serve principle applies.
  2140. +
  2141. +All requests to be transmitted by the request transport layer must be
  2142. +submitted to this queue via |ssh_rtl_submit|. Once submitted, requests may
  2143. +not be re-submitted, and will not be re-submitted automatically on timeout.
  2144. +Instead, the request is completed with a timeout error. If desired, the
  2145. +caller can create and submit a new request for another try, but it must not
  2146. +submit the same request again.
  2147. +
  2148. +Pending Set
  2149. +-----------
  2150. +
  2151. +The pending set is the second of the two fundamental collections in the
  2152. +request transport layer. This collection stores references to all pending
  2153. +requests, i.e. requests awaiting a response from the EC (similar to what the
  2154. +pending set of the packet transport layer does for packets).
  2155. +
  2156. +Transmitter Task
  2157. +----------------
  2158. +
  2159. +The transmitter task is scheduled when a new request is available for
  2160. +transmission. It checks if the next request on the request queue can be
  2161. +transmitted and, if so, submits its underlying packet to the packet
  2162. +transport layer. This check ensures that only a limited number of
  2163. +requests can be pending, i.e. waiting for a response, at the same time. If
  2164. +the request requires a response, the request is added to the pending set
  2165. +before its packet is submitted.
  2166. +
  2167. +Packet Completion Callback
  2168. +--------------------------
  2169. +
  2170. +The packet completion callback is executed once the underlying packet of a
  2171. +request has been completed. In case of an error completion, the
  2172. +corresponding request is completed with the error value provided in this
  2173. +callback.
  2174. +
  2175. +On successful packet completion, further processing depends on the request.
  2176. +If the request expects a response, it is marked as transmitted and the
  2177. +request timeout is started. If the request does not expect a response, it is
  2178. +completed with success.
  2179. +
  2180. +Data-Received Callback
  2181. +----------------------
  2182. +
  2183. +The data received callback notifies the request transport layer of data
  2184. +being received by the underlying packet transport layer via a data-type
  2185. +frame. In general, this is expected to be a command-type payload.
  2186. +
  2187. +If the request ID of the command is one of the request IDs reserved for
  2188. +events (one to ``SSH_NUM_EVENTS``, inclusively), it is forwarded to the
  2189. +event callback registered in the request transport layer. If the request ID
  2190. +indicates a response to a request, the respective request is looked up in
  2191. +the pending set and, if found and marked as transmitted, completed with
  2192. +success.
  2193. +
  2194. +Timeout Reaper
  2195. +--------------
  2196. +
  2197. +The request-response-timeout is a per-request timeout for requests expecting
  2198. +a response. It is used to ensure that a request does not wait indefinitely
  2199. +on a response from the EC and is started after the underlying packet has
  2200. +been successfully completed.
  2201. +
  2202. +This timeout is, similar to the packet acknowledgment timeout on the packet
  2203. +transport layer, handled via a dedicated reaper task. This task is
  2204. +essentially a work-item (re-)scheduled to run when the next request is set
  2205. +to time out. The work item then scans the set of pending requests for any
  2206. +requests that have timed out and completes them with ``-ETIMEDOUT`` as
  2207. +status. Requests will not be re-submitted automatically. Instead, the issuer
  2208. +of the request must construct and submit a new request, if so desired.
  2209. +
  2210. +Note that this timeout, in combination with packet transmission and
  2211. +acknowledgment timeouts, guarantees that the request layer will always make
  2212. +progress, even if only through timing out packets, and never fully block.
  2213. +
  2214. +Concurrency and Locking
  2215. +-----------------------
  2216. +
  2217. +Similar to the packet transport layer, there are two main locks in the
  2218. +request transport layer: One guarding access to the request queue and one
  2219. +guarding access to the pending set. These collections may only be accessed
  2220. +and modified under the respective lock.
  2221. +
  2222. +Other parts of the request transport layer are guarded independently. State
  2223. +flags are (again) managed by atomic bit operations and, if necessary, memory
  2224. +barriers. Modifications to the timeout reaper work item and expiration date
  2225. +are guarded by their own lock.
  2226. +
  2227. +Some request fields may be read outside of the respective locks guarding
  2228. +them, specifically the state for tracing. In those cases, proper access is
  2229. +ensured by employing ``WRITE_ONCE()`` and ``READ_ONCE()``. Such read-only
  2230. +access is only allowed when stale values are not critical.
  2231. +
  2232. +With respect to the interface for higher layers, request submission
  2233. +(|ssh_rtl_submit|), request cancellation (|ssh_rtl_cancel|), and layer
  2234. +shutdown (|ssh_rtl_shutdown|) may always be executed concurrently with
  2235. +respect to each other. Note that request submission may not run concurrently
  2236. +with itself for the same request (and also may only be called once per
  2237. +request). Equally, shutdown may also not run concurrently with itself.
  2238. +
  2239. +
  2240. +Controller Layer
  2241. +================
  2242. +
  2243. +The controller layer extends on the request transport layer to provide an
  2244. +easy-to-use interface for client drivers. It is represented by
  2245. +|ssam_controller| and the SSH driver. While the lower level transport layers
  2246. +take care of transmitting and handling packets and requests, the controller
  2247. +layer takes on more of a management role. Specifically, it handles device
  2248. +initialization, power management, and event handling, including event
  2249. +delivery and registration via the (event) completion system (|ssam_cplt|).
  2250. +
  2251. +Event Registration
  2252. +------------------
  2253. +
  2254. +In general, an event (or rather a class of events) has to be explicitly
  2255. +requested by the host before the EC will send it (HID input events seem to
  2256. +be the exception). This is done via an event-enable request (similarly,
  2257. +events should be disabled via an event-disable request once no longer
  2258. +desired).
  2259. +
  2260. +The specific request used to enable (or disable) an event is given via an
  2261. +event registry, i.e. the governing authority of this event (so to speak),
  2262. +represented by |ssam_event_registry|. As parameters to this request, the
  2263. +target category and, depending on the event registry, instance ID of the
  2264. +event to be enabled must be provided. This (optional) instance ID must be
  2265. +zero if the registry does not use it. Together, target category and instance
  2266. +ID form the event ID, represented by |ssam_event_id|. In short, both, event
  2267. +registry and event ID, are required to uniquely identify a respective class
  2268. +of events.
  2269. +
  2270. +Note that a further *request ID* parameter must be provided for the
  2271. +enable-event request. This parameter does not influence the class of events
  2272. +being enabled, but instead is set as the request ID (RQID) on each event of
  2273. +this class sent by the EC. It is used to identify events (as a limited
  2274. +number of request IDs is reserved for use in events only, specifically one
  2275. +to ``SSH_NUM_EVENTS`` inclusively) and also map events to their specific
  2276. +class. Currently, the controller always sets this parameter to the target
  2277. +category specified in |ssam_event_id|.
  2278. +
  2279. +As multiple client drivers may rely on the same (or overlapping) classes of
  2280. +events and enable/disable calls are strictly binary (i.e. on/off), the
  2281. +controller has to manage access to these events. It does so via reference
  2282. +counting, storing the counter inside an RB-tree based mapping with event
  2283. +registry and ID as key (there is no known list of valid event registry and
  2284. +event ID combinations). See |ssam_nf|, |ssam_nf_refcount_inc|, and
  2285. +|ssam_nf_refcount_dec| for details.
  2286. +
  2287. +This management is done together with notifier registration (described in
  2288. +the next section) via the top-level |ssam_notifier_register| and
  2289. +|ssam_notifier_unregister| functions.
  2290. +
  2291. +Event Delivery
  2292. +--------------
  2293. +
  2294. +To receive events, a client driver has to register an event notifier via
  2295. +|ssam_notifier_register|. This increments the reference counter for that
  2296. +specific class of events (as detailed in the previous section), enables the
  2297. +class on the EC (if it has not been enabled already), and installs the
  2298. +provided notifier callback.
  2299. +
  2300. +Notifier callbacks are stored in lists, with one (RCU) list per target
  2301. +category (provided via the event ID; NB: there is a fixed known number of
  2302. +target categories). There is no known association from the combination of
  2303. +event registry and event ID to the command data (target ID, target category,
  2304. +command ID, and instance ID) that can be provided by an event class, apart
  2305. +from target category and instance ID given via the event ID.
  2306. +
  2307. +Note that due to the way notifiers are (or rather have to be) stored, client
  2308. +drivers may receive events that they have not requested and need to account
  2309. +for them. Specifically, they will, by default, receive all events from the
  2310. +same target category. To simplify dealing with this, filtering of events by
  2311. +target ID (provided via the event registry) and instance ID (provided via
  2312. +the event ID) can be requested when registering a notifier. This filtering
  2313. +is applied when iterating over the notifiers at the time they are executed.
  2314. +
  2315. +All notifier callbacks are executed on a dedicated workqueue, the so-called
  2316. +completion workqueue. After an event has been received via the callback
  2317. +installed in the request layer (running on the receiver thread of the packet
  2318. +transport layer), it will be put on its respective event queue
  2319. +(|ssam_event_queue|). From this event queue the completion work item of that
  2320. +queue (running on the completion workqueue) will pick up the event and
  2321. +execute the notifier callback. This is done to avoid blocking on the
  2322. +receiver thread.
  2323. +
  2324. +There is one event queue per combination of target ID and target category.
  2325. +This is done to ensure that notifier callbacks are executed in sequence for
  2326. +events of the same target ID and target category. Callbacks can be executed
  2327. +in parallel for events with a different combination of target ID and target
  2328. +category.
  2329. +
  2330. +Concurrency and Locking
  2331. +-----------------------
  2332. +
  2333. +Most of the concurrency related safety guarantees of the controller are
  2334. +provided by the lower-level request transport layer. In addition to this,
  2335. +event (un-)registration is guarded by its own lock.
  2336. +
  2337. +Access to the controller state is guarded by the state lock. This lock is a
  2338. +read/write semaphore. The reader part can be used to ensure that the state
  2339. +does not change while functions depending on the state to stay the same
  2340. +(e.g. |ssam_notifier_register|, |ssam_notifier_unregister|,
  2341. +|ssam_request_sync_submit|, and derivatives) are executed and this guarantee
  2342. +is not already provided otherwise (e.g. through |ssam_client_bind| or
  2343. +|ssam_client_link|). The writer part guards any transitions that will change
  2344. +the state, i.e. initialization, destruction, suspension, and resumption.
  2345. +
  2346. +The controller state may be accessed (read-only) outside the state lock for
  2347. +smoke-testing against invalid API usage (e.g. in |ssam_request_sync_submit|).
  2348. +Note that such checks are not supposed to (and will not) protect against all
  2349. +invalid usages, but rather aim to help catch them. In those cases, proper
  2350. +variable access is ensured by employing ``WRITE_ONCE()`` and ``READ_ONCE()``.
  2351. +
  2352. +Assuming any preconditions on the state not changing have been satisfied,
  2353. +all non-initialization and non-shutdown functions may run concurrently with
  2354. +each other. This includes |ssam_notifier_register|, |ssam_notifier_unregister|,
  2355. +|ssam_request_sync_submit|, as well as all functions building on top of those.
  2356. diff --git a/Documentation/driver-api/surface_aggregator/overview.rst b/Documentation/driver-api/surface_aggregator/overview.rst
  2357. new file mode 100644
  2358. index 000000000000..1e9d57e50063
  2359. --- /dev/null
  2360. +++ b/Documentation/driver-api/surface_aggregator/overview.rst
  2361. @@ -0,0 +1,77 @@
  2362. +.. SPDX-License-Identifier: GPL-2.0+
  2363. +
  2364. +========
  2365. +Overview
  2366. +========
  2367. +
  2368. +The Surface/System Aggregator Module (SAM, SSAM) is an (arguably *the*)
  2369. +embedded controller (EC) on Microsoft Surface devices. It has been originally
  2370. +introduced on 4th generation devices (Surface Pro 4, Surface Book 1), but
  2371. +its responsibilities and feature-set have since been expanded significantly
  2372. +with the following generations.
  2373. +
  2374. +
  2375. +Features and Integration
  2376. +========================
  2377. +
  2378. +Not much is currently known about SAM on 4th generation devices (Surface Pro
  2379. +4, Surface Book 1), due to the use of a different communication interface
  2380. +between host and EC (as detailed below). On 5th (Surface Pro 2017, Surface
  2381. +Book 2, Surface Laptop 1) and later generation devices, SAM is responsible
  2382. +for providing battery information (both current status and static values,
  2383. +such as maximum capacity etc.), as well as an assortment of temperature
  2384. +sensors (e.g. skin temperature) and cooling/performance-mode setting to the
  2385. +host. On the Surface Book 2, specifically, it additionally provides an
  2386. +interface for properly handling clipboard detachment (i.e. separating the
  2387. +display part from the keyboard part of the device), on the Surface Laptop 1
  2388. +and 2 it is required for keyboard HID input. This HID subsystem has been
  2389. +restructured for 7th generation devices and on those, specifically Surface
  2390. +Laptop 3 and Surface Book 3, is responsible for all major HID input (i.e.
  2391. +keyboard and touchpad).
  2392. +
  2393. +While features have not changed much on a coarse level since the 5th
  2394. +generation, internal interfaces have undergone some rather large changes. On
  2395. +5th and 6th generation devices, both battery and temperature information is
  2396. +exposed to ACPI via a shim driver (referred to as Surface ACPI Notify, or
  2397. +SAN), translating ACPI generic serial bus write-/read-accesses to SAM
  2398. +requests. On 7th generation devices, this additional layer is gone and these
  2399. +devices require a driver hooking directly into the SAM interface. Equally,
  2400. +on newer generations, less devices are declared in ACPI, making them a bit
  2401. +harder to discover and requiring us to hard-code a sort of device registry.
  2402. +Due to this, a SSAM bus and subsystem with client devices
  2403. +(:c:type:`struct ssam_device <ssam_device>`) has been implemented.
  2404. +
  2405. +
  2406. +Communication
  2407. +=============
  2408. +
  2409. +The type of communication interface between host and EC depends on the
  2410. +generation of the Surface device. On 4th generation devices, host and EC
  2411. +communicate via HID, specifically using a HID-over-I2C device, whereas on
  2412. +5th and later generations, communication takes place via a USART serial
  2413. +device. In accordance to the drivers found on other operating systems, we
  2414. +refer to the serial device and its driver as Surface Serial Hub (SSH). When
  2415. +needed, we differentiate between both types of SAM by referring to them as
  2416. +SAM-over-SSH and SAM-over-HID.
  2417. +
  2418. +Currently, this subsystem only supports SAM-over-SSH. The SSH communication
  2419. +interface is described in more detail below. The HID interface has not been
  2420. +reverse engineered yet and it is, at the moment, unclear how many (and
  2421. +which) concepts of the SSH interface detailed below can be transferred to
  2422. +it.
  2423. +
  2424. +Surface Serial Hub
  2425. +------------------
  2426. +
  2427. +As already elaborated above, the Surface Serial Hub (SSH) is the
  2428. +communication interface for SAM on 5th- and all later-generation Surface
  2429. +devices. On the highest level, communication can be separated into two main
  2430. +types: Requests, messages sent from host to EC that may trigger a direct
  2431. +response from the EC (explicitly associated with the request), and events
  2432. +(sometimes also referred to as notifications), sent from EC to host without
  2433. +being a direct response to a previous request. We may also refer to requests
  2434. +without response as commands. In general, events need to be enabled via one
  2435. +of multiple dedicated requests before they are sent by the EC.
  2436. +
  2437. +See :doc:`ssh` for a more technical protocol documentation and
  2438. +:doc:`internal` for an overview of the internal driver architecture.
  2439. diff --git a/Documentation/driver-api/surface_aggregator/ssh.rst b/Documentation/driver-api/surface_aggregator/ssh.rst
  2440. new file mode 100644
  2441. index 000000000000..bf007d6c9873
  2442. --- /dev/null
  2443. +++ b/Documentation/driver-api/surface_aggregator/ssh.rst
  2444. @@ -0,0 +1,344 @@
  2445. +.. SPDX-License-Identifier: GPL-2.0+
  2446. +
  2447. +.. |u8| replace:: :c:type:`u8 <u8>`
  2448. +.. |u16| replace:: :c:type:`u16 <u16>`
  2449. +.. |TYPE| replace:: ``TYPE``
  2450. +.. |LEN| replace:: ``LEN``
  2451. +.. |SEQ| replace:: ``SEQ``
  2452. +.. |SYN| replace:: ``SYN``
  2453. +.. |NAK| replace:: ``NAK``
  2454. +.. |ACK| replace:: ``ACK``
  2455. +.. |DATA| replace:: ``DATA``
  2456. +.. |DATA_SEQ| replace:: ``DATA_SEQ``
  2457. +.. |DATA_NSQ| replace:: ``DATA_NSQ``
  2458. +.. |TC| replace:: ``TC``
  2459. +.. |TID| replace:: ``TID``
  2460. +.. |IID| replace:: ``IID``
  2461. +.. |RQID| replace:: ``RQID``
  2462. +.. |CID| replace:: ``CID``
  2463. +
  2464. +===========================
  2465. +Surface Serial Hub Protocol
  2466. +===========================
  2467. +
  2468. +The Surface Serial Hub (SSH) is the central communication interface for the
  2469. +embedded Surface Aggregator Module controller (SAM or EC), found on newer
  2470. +Surface generations. We will refer to this protocol and interface as
  2471. +SAM-over-SSH, as opposed to SAM-over-HID for the older generations.
  2472. +
  2473. +On Surface devices with SAM-over-SSH, SAM is connected to the host via UART
  2474. +and defined in ACPI as device with ID ``MSHW0084``. On these devices,
  2475. +significant functionality is provided via SAM, including access to battery
  2476. +and power information and events, thermal read-outs and events, and many
  2477. +more. For Surface Laptops, keyboard input is handled via HID directed
  2478. +through SAM, on the Surface Laptop 3 and Surface Book 3 this also includes
  2479. +touchpad input.
  2480. +
  2481. +Note that the standard disclaimer for this subsystem also applies to this
  2482. +document: All of this has been reverse-engineered and may thus be erroneous
  2483. +and/or incomplete.
  2484. +
  2485. +All CRCs used in the following are two-byte ``crc_ccitt_false(0xffff, ...)``.
  2486. +All multi-byte values are little-endian, there is no implicit padding between
  2487. +values.
  2488. +
  2489. +
  2490. +SSH Packet Protocol: Definitions
  2491. +================================
  2492. +
  2493. +The fundamental communication unit of the SSH protocol is a frame
  2494. +(:c:type:`struct ssh_frame <ssh_frame>`). A frame consists of the following
  2495. +fields, packed together and in order:
  2496. +
  2497. +.. flat-table:: SSH Frame
  2498. + :widths: 1 1 4
  2499. + :header-rows: 1
  2500. +
  2501. + * - Field
  2502. + - Type
  2503. + - Description
  2504. +
  2505. + * - |TYPE|
  2506. + - |u8|
  2507. + - Type identifier of the frame.
  2508. +
  2509. + * - |LEN|
  2510. + - |u16|
  2511. + - Length of the payload associated with the frame.
  2512. +
  2513. + * - |SEQ|
  2514. + - |u8|
  2515. + - Sequence ID (see explanation below).
  2516. +
  2517. +Each frame structure is followed by a CRC over this structure. The CRC over
  2518. +the frame structure (|TYPE|, |LEN|, and |SEQ| fields) is placed directly
  2519. +after the frame structure and before the payload. The payload is followed by
  2520. +its own CRC (over all payload bytes). If the payload is not present (i.e.
  2521. +the frame has ``LEN=0``), the CRC of the payload is still present and will
  2522. +evaluate to ``0xffff``. The |LEN| field does not include any of the CRCs, it
  2523. +equals the number of bytes inbetween the CRC of the frame and the CRC of the
  2524. +payload.
  2525. +
  2526. +Additionally, the following fixed two-byte sequences are used:
  2527. +
  2528. +.. flat-table:: SSH Byte Sequences
  2529. + :widths: 1 1 4
  2530. + :header-rows: 1
  2531. +
  2532. + * - Name
  2533. + - Value
  2534. + - Description
  2535. +
  2536. + * - |SYN|
  2537. + - ``[0xAA, 0x55]``
  2538. + - Synchronization bytes.
  2539. +
  2540. +A message consists of |SYN|, followed by the frame (|TYPE|, |LEN|, |SEQ| and
  2541. +CRC) and, if specified in the frame (i.e. ``LEN > 0``), payload bytes,
  2542. +followed finally, regardless if the payload is present, the payload CRC. The
  2543. +messages corresponding to an exchange are, in part, identified by having the
  2544. +same sequence ID (|SEQ|), stored inside the frame (more on this in the next
  2545. +section). The sequence ID is a wrapping counter.
  2546. +
  2547. +A frame can have the following types
  2548. +(:c:type:`enum ssh_frame_type <ssh_frame_type>`):
  2549. +
  2550. +.. flat-table:: SSH Frame Types
  2551. + :widths: 1 1 4
  2552. + :header-rows: 1
  2553. +
  2554. + * - Name
  2555. + - Value
  2556. + - Short Description
  2557. +
  2558. + * - |NAK|
  2559. + - ``0x04``
  2560. + - Sent on error in previously received message.
  2561. +
  2562. + * - |ACK|
  2563. + - ``0x40``
  2564. + - Sent to acknowledge receival of |DATA| frame.
  2565. +
  2566. + * - |DATA_SEQ|
  2567. + - ``0x80``
  2568. + - Sent to transfer data. Sequenced.
  2569. +
  2570. + * - |DATA_NSQ|
  2571. + - ``0x00``
  2572. + - Same as |DATA_SEQ|, but does not need to be ACKed.
  2573. +
  2574. +Both |NAK|- and |ACK|-type frames are used to control flow of messages and
  2575. +thus do not carry a payload. |DATA_SEQ|- and |DATA_NSQ|-type frames on the
  2576. +other hand must carry a payload. The flow sequence and interaction of
  2577. +different frame types will be described in more depth in the next section.
  2578. +
  2579. +
  2580. +SSH Packet Protocol: Flow Sequence
  2581. +==================================
  2582. +
  2583. +Each exchange begins with |SYN|, followed by a |DATA_SEQ|- or
  2584. +|DATA_NSQ|-type frame, followed by its CRC, payload, and payload CRC. In
  2585. +case of a |DATA_NSQ|-type frame, the exchange is then finished. In case of a
  2586. +|DATA_SEQ|-type frame, the receiving party has to acknowledge receival of
  2587. +the frame by responding with a message containing an |ACK|-type frame with
  2588. +the same sequence ID of the |DATA| frame. In other words, the sequence ID of
  2589. +the |ACK| frame specifies the |DATA| frame to be acknowledged. In case of an
  2590. +error, e.g. an invalid CRC, the receiving party responds with a message
  2591. +containing an |NAK|-type frame. As the sequence ID of the previous data
  2592. +frame, for which an error is indicated via the |NAK| frame, cannot be relied
  2593. +upon, the sequence ID of the |NAK| frame should not be used and is set to
  2594. +zero. After receival of an |NAK| frame, the sending party should re-send all
  2595. +outstanding (non-ACKed) messages.
  2596. +
  2597. +Sequence IDs are not synchronized between the two parties, meaning that they
  2598. +are managed independently for each party. Identifying the messages
  2599. +corresponding to a single exchange thus relies on the sequence ID as well as
  2600. +the type of the message, and the context. Specifically, the sequence ID is
  2601. +used to associate an ``ACK`` with its ``DATA_SEQ``-type frame, but not
  2602. +``DATA_SEQ``- or ``DATA_NSQ``-type frames with other ``DATA``- type frames.
  2603. +
  2604. +An example exchange might look like this:
  2605. +
  2606. +::
  2607. +
  2608. + tx: -- SYN FRAME(D) CRC(F) PAYLOAD CRC(P) -----------------------------
  2609. + rx: ------------------------------------- SYN FRAME(A) CRC(F) CRC(P) --
  2610. +
  2611. +where both frames have the same sequence ID (``SEQ``). Here, ``FRAME(D)``
  2612. +indicates a |DATA_SEQ|-type frame, ``FRAME(A)`` an ``ACK``-type frame,
  2613. +``CRC(F)`` the CRC over the previous frame, ``CRC(P)`` the CRC over the
  2614. +previous payload. In case of an error, the exchange would look like this:
  2615. +
  2616. +::
  2617. +
  2618. + tx: -- SYN FRAME(D) CRC(F) PAYLOAD CRC(P) -----------------------------
  2619. + rx: ------------------------------------- SYN FRAME(N) CRC(F) CRC(P) --
  2620. +
  2621. +upon which the sender should re-send the message. ``FRAME(N)`` indicates an
  2622. +|NAK|-type frame. Note that the sequence ID of the |NAK|-type frame is fixed
  2623. +to zero. For |DATA_NSQ|-type frames, both exchanges are the same:
  2624. +
  2625. +::
  2626. +
  2627. + tx: -- SYN FRAME(DATA_NSQ) CRC(F) PAYLOAD CRC(P) ----------------------
  2628. + rx: -------------------------------------------------------------------
  2629. +
  2630. +Here, an error can be detected, but not corrected or indicated to the
  2631. +sending party. These exchanges are symmetric, i.e. switching ``rx`` and
  2632. +``tx`` results again in a valid exchange. Currently, no longer exchanges are
  2633. +known.
  2634. +
  2635. +
  2636. +Commands: Requests, Responses, and Events
  2637. +=========================================
  2638. +
  2639. +Commands are sent as payload inside a data frame. Currently, this is the
  2640. +only known payload type of |DATA| frames, with a payload-type value of
  2641. +``0x80`` (:c:type:`SSH_PLD_TYPE_CMD <ssh_payload_type>`).
  2642. +
  2643. +The command-type payload (:c:type:`struct ssh_command <ssh_command>`)
  2644. +consists of an eight-byte command structure, followed by optional and
  2645. +variable length command data. The length of this optional data is derived
  2646. +from the frame payload length given in the corresponding frame, i.e. it is
  2647. +``frame.len - sizeof(struct ssh_command)``. The command struct contains the
  2648. +following fields, packed together and in order:
  2649. +
  2650. +.. flat-table:: SSH Command
  2651. + :widths: 1 1 4
  2652. + :header-rows: 1
  2653. +
  2654. + * - Field
  2655. + - Type
  2656. + - Description
  2657. +
  2658. + * - |TYPE|
  2659. + - |u8|
  2660. + - Type of the payload. For commands always ``0x80``.
  2661. +
  2662. + * - |TC|
  2663. + - |u8|
  2664. + - Target category.
  2665. +
  2666. + * - |TID| (out)
  2667. + - |u8|
  2668. + - Target ID for outgoing (host to EC) commands.
  2669. +
  2670. + * - |TID| (in)
  2671. + - |u8|
  2672. + - Target ID for incoming (EC to host) commands.
  2673. +
  2674. + * - |IID|
  2675. + - |u8|
  2676. + - Instance ID.
  2677. +
  2678. + * - |RQID|
  2679. + - |u16|
  2680. + - Request ID.
  2681. +
  2682. + * - |CID|
  2683. + - |u8|
  2684. + - Command ID.
  2685. +
  2686. +The command struct and data, in general, does not contain any failure
  2687. +detection mechanism (e.g. CRCs), this is solely done on the frame level.
  2688. +
  2689. +Command-type payloads are used by the host to send commands and requests to
  2690. +the EC as well as by the EC to send responses and events back to the host.
  2691. +We differentiate between requests (sent by the host), responses (sent by the
  2692. +EC in response to a request), and events (sent by the EC without a preceding
  2693. +request).
  2694. +
  2695. +Commands and events are uniquely identified by their target category
  2696. +(``TC``) and command ID (``CID``). The target category specifies a general
  2697. +category for the command (e.g. system in general, vs. battery and AC, vs.
  2698. +temperature, and so on), while the command ID specifies the command inside
  2699. +that category. Only the combination of |TC| + |CID| is unique. Additionally,
  2700. +commands have an instance ID (``IID``), which is used to differentiate
  2701. +between different sub-devices. For example ``TC=3`` ``CID=1`` is a
  2702. +request to get the temperature on a thermal sensor, where |IID| specifies
  2703. +the respective sensor. If the instance ID is not used, it should be set to
  2704. +zero. If instance IDs are used, they, in general, start with a value of one,
  2705. +whereas zero may be used for instance independent queries, if applicable. A
  2706. +response to a request should have the same target category, command ID, and
  2707. +instance ID as the corresponding request.
  2708. +
  2709. +Responses are matched to their corresponding request via the request ID
  2710. +(``RQID``) field. This is a 16 bit wrapping counter similar to the sequence
  2711. +ID on the frames. Note that the sequence ID of the frames for a
  2712. +request-response pair does not match. Only the request ID has to match.
  2713. +Frame-protocol wise these are two separate exchanges, and may even be
  2714. +separated, e.g. by an event being sent after the request but before the
  2715. +response. Not all commands produce a response, and this is not detectable by
  2716. +|TC| + |CID|. It is the responsibility of the issuing party to wait for a
  2717. +response (or signal this to the communication framework, as is done in
  2718. +SAN/ACPI via the ``SNC`` flag).
  2719. +
  2720. +Events are identified by unique and reserved request IDs. These IDs should
  2721. +not be used by the host when sending a new request. They are used on the
  2722. +host to, first, detect events and, second, match them with a registered
  2723. +event handler. Request IDs for events are chosen by the host and directed to
  2724. +the EC when setting up and enabling an event source (via the
  2725. +enable-event-source request). The EC then uses the specified request ID for
  2726. +events sent from the respective source. Note that an event should still be
  2727. +identified by its target category, command ID, and, if applicable, instance
  2728. +ID, as a single event source can send multiple different event types. In
  2729. +general, however, a single target category should map to a single reserved
  2730. +event request ID.
  2731. +
  2732. +Furthermore, requests, responses, and events have an associated target ID
  2733. +(``TID``). This target ID is split into output (host to EC) and input (EC to
  2734. +host) fields, with the respecting other field (e.g. output field on incoming
  2735. +messages) set to zero. Two ``TID`` values are known: Primary (``0x01``) and
  2736. +secondary (``0x02``). In general, the response to a request should have the
  2737. +same ``TID`` value, however, the field (output vs. input) should be used in
  2738. +accordance to the direction in which the response is sent (i.e. on the input
  2739. +field, as responses are generally sent from the EC to the host).
  2740. +
  2741. +Note that, even though requests and events should be uniquely identifiable
  2742. +by target category and command ID alone, the EC may require specific
  2743. +target ID and instance ID values to accept a command. A command that is
  2744. +accepted for ``TID=1``, for example, may not be accepted for ``TID=2``
  2745. +and vice versa.
  2746. +
  2747. +
  2748. +Limitations and Observations
  2749. +============================
  2750. +
  2751. +The protocol can, in theory, handle up to ``U8_MAX`` frames in parallel,
  2752. +with up to ``U16_MAX`` pending requests (neglecting request IDs reserved for
  2753. +events). In practice, however, this is more limited. From our testing
  2754. +(although via a python and thus a user-space program), it seems that the EC
  2755. +can handle up to four requests (mostly) reliably in parallel at a certain
  2756. +time. With five or more requests in parallel, consistent discarding of
  2757. +commands (ACKed frame but no command response) has been observed. For five
  2758. +simultaneous commands, this reproducibly resulted in one command being
  2759. +dropped and four commands being handled.
  2760. +
  2761. +However, it has also been noted that, even with three requests in parallel,
  2762. +occasional frame drops happen. Apart from this, with a limit of three
  2763. +pending requests, no dropped commands (i.e. command being dropped but frame
  2764. +carrying command being ACKed) have been observed. In any case, frames (and
  2765. +possibly also commands) should be re-sent by the host if a certain timeout
  2766. +is exceeded. This is done by the EC for frames with a timeout of one second,
  2767. +up to two re-tries (i.e. three transmissions in total). The limit of
  2768. +re-tries also applies to received NAKs, and, in a worst case scenario, can
  2769. +lead to entire messages being dropped.
  2770. +
  2771. +While this also seems to work fine for pending data frames as long as no
  2772. +transmission failures occur, implementation and handling of these seems to
  2773. +depend on the assumption that there is only one non-acknowledged data frame.
  2774. +In particular, the detection of repeated frames relies on the last sequence
  2775. +number. This means that, if a frame that has been successfully received by
  2776. +the EC is sent again, e.g. due to the host not receiving an |ACK|, the EC
  2777. +will only detect this if it has the sequence ID of the last frame received
  2778. +by the EC. As an example: Sending two frames with ``SEQ=0`` and ``SEQ=1``
  2779. +followed by a repetition of ``SEQ=0`` will not detect the second ``SEQ=0``
  2780. +frame as such, and thus execute the command in this frame each time it has
  2781. +been received, i.e. twice in this example. Sending ``SEQ=0``, ``SEQ=1`` and
  2782. +then repeating ``SEQ=1`` will detect the second ``SEQ=1`` as repetition of
  2783. +the first one and ignore it, thus executing the contained command only once.
  2784. +
  2785. +In conclusion, this suggests a limit of at most one pending un-ACKed frame
  2786. +(per party, effectively leading to synchronous communication regarding
  2787. +frames) and at most three pending commands. The limit to synchronous frame
  2788. +transfers seems to be consistent with behavior observed on Windows.
  2789. diff --git a/arch/x86/kernel/acpi/boot.c b/arch/x86/kernel/acpi/boot.c
  2790. index 14cd3186dc77..fa1dcdd119e5 100644
  2791. --- a/arch/x86/kernel/acpi/boot.c
  2792. +++ b/arch/x86/kernel/acpi/boot.c
  2793. @@ -21,6 +21,7 @@
  2794. #include <linux/efi-bgrt.h>
  2795. #include <linux/serial_core.h>
  2796. #include <linux/pgtable.h>
  2797. +#include <linux/dmi.h>
  2798. #include <asm/e820/api.h>
  2799. #include <asm/irqdomain.h>
  2800. @@ -1155,6 +1156,24 @@ static void __init mp_config_acpi_legacy_irqs(void)
  2801. }
  2802. }
  2803. +static const struct dmi_system_id surface_quirk[] __initconst = {
  2804. + {
  2805. + .ident = "Microsoft Surface Laptop 4 (AMD 15\")",
  2806. + .matches = {
  2807. + DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  2808. + DMI_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_4_1952:1953")
  2809. + },
  2810. + },
  2811. + {
  2812. + .ident = "Microsoft Surface Laptop 4 (AMD 13\")",
  2813. + .matches = {
  2814. + DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  2815. + DMI_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_4_1958:1959")
  2816. + },
  2817. + },
  2818. + {}
  2819. +};
  2820. +
  2821. /*
  2822. * Parse IOAPIC related entries in MADT
  2823. * returns 0 on success, < 0 on error
  2824. @@ -1212,6 +1231,11 @@ static int __init acpi_parse_madt_ioapic_entries(void)
  2825. acpi_sci_ioapic_setup(acpi_gbl_FADT.sci_interrupt, 0, 0,
  2826. acpi_gbl_FADT.sci_interrupt);
  2827. + if (dmi_check_system(surface_quirk)) {
  2828. + pr_warn("Surface hack: Override irq 7\n");
  2829. + mp_override_legacy_irq(7, 3, 3, 7);
  2830. + }
  2831. +
  2832. /* Fill in identity legacy mappings where no override */
  2833. mp_config_acpi_legacy_irqs();
  2834. diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
  2835. index 54bc563a8dff..30b142b86bf5 100644
  2836. --- a/drivers/hid/Kconfig
  2837. +++ b/drivers/hid/Kconfig
  2838. @@ -910,7 +910,7 @@ config HID_SONY
  2839. * Logitech Harmony adapter for Sony Playstation 3 (Bluetooth)
  2840. config SONY_FF
  2841. - bool "Sony PS2/3/4 accessories force feedback support"
  2842. + bool "Sony PS2/3/4 accessories force feedback support"
  2843. depends on HID_SONY
  2844. select INPUT_FF_MEMLESS
  2845. help
  2846. @@ -1184,4 +1184,6 @@ source "drivers/hid/i2c-hid/Kconfig"
  2847. source "drivers/hid/intel-ish-hid/Kconfig"
  2848. +source "drivers/hid/surface-hid/Kconfig"
  2849. +
  2850. endmenu
  2851. diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
  2852. index 4acb583c92a6..a13d89358b0c 100644
  2853. --- a/drivers/hid/Makefile
  2854. +++ b/drivers/hid/Makefile
  2855. @@ -142,3 +142,5 @@ obj-$(CONFIG_I2C_HID) += i2c-hid/
  2856. obj-$(CONFIG_INTEL_ISH_HID) += intel-ish-hid/
  2857. obj-$(INTEL_ISH_FIRMWARE_DOWNLOADER) += intel-ish-hid/
  2858. +
  2859. +obj-$(CONFIG_SURFACE_HID_CORE) += surface-hid/
  2860. diff --git a/drivers/hid/surface-hid/Kconfig b/drivers/hid/surface-hid/Kconfig
  2861. new file mode 100644
  2862. index 000000000000..7ce9b5d641eb
  2863. --- /dev/null
  2864. +++ b/drivers/hid/surface-hid/Kconfig
  2865. @@ -0,0 +1,42 @@
  2866. +# SPDX-License-Identifier: GPL-2.0+
  2867. +menu "Surface System Aggregator Module HID support"
  2868. + depends on SURFACE_AGGREGATOR
  2869. + depends on INPUT
  2870. +
  2871. +config SURFACE_HID
  2872. + tristate "HID transport driver for Surface System Aggregator Module"
  2873. + depends on SURFACE_AGGREGATOR_REGISTRY
  2874. + select SURFACE_HID_CORE
  2875. + help
  2876. + Driver to support integrated HID devices on newer Microsoft Surface
  2877. + models.
  2878. +
  2879. + This driver provides support for the HID transport protocol provided
  2880. + by the Surface Aggregator Module (i.e. the embedded controller) on
  2881. + 7th-generation Microsoft Surface devices, i.e. Surface Book 3 and
  2882. + Surface Laptop 3. On those models, it is mainly used to connect the
  2883. + integrated touchpad and keyboard.
  2884. +
  2885. + Say M or Y here, if you want support for integrated HID devices, i.e.
  2886. + integrated touchpad and keyboard, on 7th generation Microsoft Surface
  2887. + models.
  2888. +
  2889. +config SURFACE_KBD
  2890. + tristate "HID keyboard transport driver for Surface System Aggregator Module"
  2891. + select SURFACE_HID_CORE
  2892. + help
  2893. + Driver to support HID keyboards on Surface Laptop 1 and 2 devices.
  2894. +
  2895. + This driver provides support for the HID transport protocol provided
  2896. + by the Surface Aggregator Module (i.e. the embedded controller) on
  2897. + Microsoft Surface Laptops 1 and 2. It is used to connect the
  2898. + integrated keyboard on those devices.
  2899. +
  2900. + Say M or Y here, if you want support for the integrated keyboard on
  2901. + Microsoft Surface Laptops 1 and 2.
  2902. +
  2903. +endmenu
  2904. +
  2905. +config SURFACE_HID_CORE
  2906. + tristate
  2907. + select HID
  2908. diff --git a/drivers/hid/surface-hid/Makefile b/drivers/hid/surface-hid/Makefile
  2909. new file mode 100644
  2910. index 000000000000..4ae11cf09b25
  2911. --- /dev/null
  2912. +++ b/drivers/hid/surface-hid/Makefile
  2913. @@ -0,0 +1,7 @@
  2914. +# SPDX-License-Identifier: GPL-2.0+
  2915. +#
  2916. +# Makefile - Surface System Aggregator Module (SSAM) HID transport driver.
  2917. +#
  2918. +obj-$(CONFIG_SURFACE_HID_CORE) += surface_hid_core.o
  2919. +obj-$(CONFIG_SURFACE_HID) += surface_hid.o
  2920. +obj-$(CONFIG_SURFACE_KBD) += surface_kbd.o
  2921. diff --git a/drivers/hid/surface-hid/surface_hid.c b/drivers/hid/surface-hid/surface_hid.c
  2922. new file mode 100644
  2923. index 000000000000..a3a70e4f3f6c
  2924. --- /dev/null
  2925. +++ b/drivers/hid/surface-hid/surface_hid.c
  2926. @@ -0,0 +1,253 @@
  2927. +// SPDX-License-Identifier: GPL-2.0+
  2928. +/*
  2929. + * Surface System Aggregator Module (SSAM) HID transport driver for the
  2930. + * generic HID interface (HID/TC=0x15 subsystem). Provides support for
  2931. + * integrated HID devices on Surface Laptop 3, Book 3, and later.
  2932. + *
  2933. + * Copyright (C) 2019-2021 Blaž Hrastnik <blaz@mxxn.io>,
  2934. + * Maximilian Luz <luzmaximilian@gmail.com>
  2935. + */
  2936. +
  2937. +#include <asm/unaligned.h>
  2938. +#include <linux/hid.h>
  2939. +#include <linux/kernel.h>
  2940. +#include <linux/module.h>
  2941. +#include <linux/types.h>
  2942. +
  2943. +#include <linux/surface_aggregator/controller.h>
  2944. +#include <linux/surface_aggregator/device.h>
  2945. +
  2946. +#include "surface_hid_core.h"
  2947. +
  2948. +
  2949. +/* -- SAM interface. -------------------------------------------------------- */
  2950. +
  2951. +struct surface_hid_buffer_slice {
  2952. + __u8 entry;
  2953. + __le32 offset;
  2954. + __le32 length;
  2955. + __u8 end;
  2956. + __u8 data[];
  2957. +} __packed;
  2958. +
  2959. +static_assert(sizeof(struct surface_hid_buffer_slice) == 10);
  2960. +
  2961. +enum surface_hid_cid {
  2962. + SURFACE_HID_CID_OUTPUT_REPORT = 0x01,
  2963. + SURFACE_HID_CID_GET_FEATURE_REPORT = 0x02,
  2964. + SURFACE_HID_CID_SET_FEATURE_REPORT = 0x03,
  2965. + SURFACE_HID_CID_GET_DESCRIPTOR = 0x04,
  2966. +};
  2967. +
  2968. +static int ssam_hid_get_descriptor(struct surface_hid_device *shid, u8 entry, u8 *buf, size_t len)
  2969. +{
  2970. + u8 buffer[sizeof(struct surface_hid_buffer_slice) + 0x76];
  2971. + struct surface_hid_buffer_slice *slice;
  2972. + struct ssam_request rqst;
  2973. + struct ssam_response rsp;
  2974. + u32 buffer_len, offset, length;
  2975. + int status;
  2976. +
  2977. + /*
  2978. + * Note: The 0x76 above has been chosen because that's what's used by
  2979. + * the Windows driver. Together with the header, this leads to a 128
  2980. + * byte payload in total.
  2981. + */
  2982. +
  2983. + buffer_len = ARRAY_SIZE(buffer) - sizeof(struct surface_hid_buffer_slice);
  2984. +
  2985. + rqst.target_category = shid->uid.category;
  2986. + rqst.target_id = shid->uid.target;
  2987. + rqst.command_id = SURFACE_HID_CID_GET_DESCRIPTOR;
  2988. + rqst.instance_id = shid->uid.instance;
  2989. + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
  2990. + rqst.length = sizeof(struct surface_hid_buffer_slice);
  2991. + rqst.payload = buffer;
  2992. +
  2993. + rsp.capacity = ARRAY_SIZE(buffer);
  2994. + rsp.pointer = buffer;
  2995. +
  2996. + slice = (struct surface_hid_buffer_slice *)buffer;
  2997. + slice->entry = entry;
  2998. + slice->end = 0;
  2999. +
  3000. + offset = 0;
  3001. + length = buffer_len;
  3002. +
  3003. + while (!slice->end && offset < len) {
  3004. + put_unaligned_le32(offset, &slice->offset);
  3005. + put_unaligned_le32(length, &slice->length);
  3006. +
  3007. + rsp.length = 0;
  3008. +
  3009. + status = ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, &rsp,
  3010. + sizeof(*slice));
  3011. + if (status)
  3012. + return status;
  3013. +
  3014. + offset = get_unaligned_le32(&slice->offset);
  3015. + length = get_unaligned_le32(&slice->length);
  3016. +
  3017. + /* Don't mess stuff up in case we receive garbage. */
  3018. + if (length > buffer_len || offset > len)
  3019. + return -EPROTO;
  3020. +
  3021. + if (offset + length > len)
  3022. + length = len - offset;
  3023. +
  3024. + memcpy(buf + offset, &slice->data[0], length);
  3025. +
  3026. + offset += length;
  3027. + length = buffer_len;
  3028. + }
  3029. +
  3030. + if (offset != len) {
  3031. + dev_err(shid->dev, "unexpected descriptor length: got %u, expected %zu\n",
  3032. + offset, len);
  3033. + return -EPROTO;
  3034. + }
  3035. +
  3036. + return 0;
  3037. +}
  3038. +
  3039. +static int ssam_hid_set_raw_report(struct surface_hid_device *shid, u8 rprt_id, bool feature,
  3040. + u8 *buf, size_t len)
  3041. +{
  3042. + struct ssam_request rqst;
  3043. + u8 cid;
  3044. +
  3045. + if (feature)
  3046. + cid = SURFACE_HID_CID_SET_FEATURE_REPORT;
  3047. + else
  3048. + cid = SURFACE_HID_CID_OUTPUT_REPORT;
  3049. +
  3050. + rqst.target_category = shid->uid.category;
  3051. + rqst.target_id = shid->uid.target;
  3052. + rqst.instance_id = shid->uid.instance;
  3053. + rqst.command_id = cid;
  3054. + rqst.flags = 0;
  3055. + rqst.length = len;
  3056. + rqst.payload = buf;
  3057. +
  3058. + buf[0] = rprt_id;
  3059. +
  3060. + return ssam_retry(ssam_request_sync, shid->ctrl, &rqst, NULL);
  3061. +}
  3062. +
  3063. +static int ssam_hid_get_raw_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
  3064. +{
  3065. + struct ssam_request rqst;
  3066. + struct ssam_response rsp;
  3067. +
  3068. + rqst.target_category = shid->uid.category;
  3069. + rqst.target_id = shid->uid.target;
  3070. + rqst.instance_id = shid->uid.instance;
  3071. + rqst.command_id = SURFACE_HID_CID_GET_FEATURE_REPORT;
  3072. + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
  3073. + rqst.length = sizeof(rprt_id);
  3074. + rqst.payload = &rprt_id;
  3075. +
  3076. + rsp.capacity = len;
  3077. + rsp.length = 0;
  3078. + rsp.pointer = buf;
  3079. +
  3080. + return ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, &rsp, sizeof(rprt_id));
  3081. +}
  3082. +
  3083. +static u32 ssam_hid_event_fn(struct ssam_event_notifier *nf, const struct ssam_event *event)
  3084. +{
  3085. + struct surface_hid_device *shid = container_of(nf, struct surface_hid_device, notif);
  3086. +
  3087. + if (event->command_id != 0x00)
  3088. + return 0;
  3089. +
  3090. + hid_input_report(shid->hid, HID_INPUT_REPORT, (u8 *)&event->data[0], event->length, 0);
  3091. + return SSAM_NOTIF_HANDLED;
  3092. +}
  3093. +
  3094. +
  3095. +/* -- Transport driver. ----------------------------------------------------- */
  3096. +
  3097. +static int shid_output_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
  3098. +{
  3099. + int status;
  3100. +
  3101. + status = ssam_hid_set_raw_report(shid, rprt_id, false, buf, len);
  3102. + return status >= 0 ? len : status;
  3103. +}
  3104. +
  3105. +static int shid_get_feature_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
  3106. +{
  3107. + int status;
  3108. +
  3109. + status = ssam_hid_get_raw_report(shid, rprt_id, buf, len);
  3110. + return status >= 0 ? len : status;
  3111. +}
  3112. +
  3113. +static int shid_set_feature_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
  3114. +{
  3115. + int status;
  3116. +
  3117. + status = ssam_hid_set_raw_report(shid, rprt_id, true, buf, len);
  3118. + return status >= 0 ? len : status;
  3119. +}
  3120. +
  3121. +
  3122. +/* -- Driver setup. --------------------------------------------------------- */
  3123. +
  3124. +static int surface_hid_probe(struct ssam_device *sdev)
  3125. +{
  3126. + struct surface_hid_device *shid;
  3127. +
  3128. + shid = devm_kzalloc(&sdev->dev, sizeof(*shid), GFP_KERNEL);
  3129. + if (!shid)
  3130. + return -ENOMEM;
  3131. +
  3132. + shid->dev = &sdev->dev;
  3133. + shid->ctrl = sdev->ctrl;
  3134. + shid->uid = sdev->uid;
  3135. +
  3136. + shid->notif.base.priority = 1;
  3137. + shid->notif.base.fn = ssam_hid_event_fn;
  3138. + shid->notif.event.reg = SSAM_EVENT_REGISTRY_REG;
  3139. + shid->notif.event.id.target_category = sdev->uid.category;
  3140. + shid->notif.event.id.instance = sdev->uid.instance;
  3141. + shid->notif.event.mask = SSAM_EVENT_MASK_STRICT;
  3142. + shid->notif.event.flags = 0;
  3143. +
  3144. + shid->ops.get_descriptor = ssam_hid_get_descriptor;
  3145. + shid->ops.output_report = shid_output_report;
  3146. + shid->ops.get_feature_report = shid_get_feature_report;
  3147. + shid->ops.set_feature_report = shid_set_feature_report;
  3148. +
  3149. + ssam_device_set_drvdata(sdev, shid);
  3150. + return surface_hid_device_add(shid);
  3151. +}
  3152. +
  3153. +static void surface_hid_remove(struct ssam_device *sdev)
  3154. +{
  3155. + surface_hid_device_destroy(ssam_device_get_drvdata(sdev));
  3156. +}
  3157. +
  3158. +static const struct ssam_device_id surface_hid_match[] = {
  3159. + { SSAM_SDEV(HID, 0x02, SSAM_ANY_IID, 0x00) },
  3160. + { },
  3161. +};
  3162. +MODULE_DEVICE_TABLE(ssam, surface_hid_match);
  3163. +
  3164. +static struct ssam_device_driver surface_hid_driver = {
  3165. + .probe = surface_hid_probe,
  3166. + .remove = surface_hid_remove,
  3167. + .match_table = surface_hid_match,
  3168. + .driver = {
  3169. + .name = "surface_hid",
  3170. + .pm = &surface_hid_pm_ops,
  3171. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  3172. + },
  3173. +};
  3174. +module_ssam_device_driver(surface_hid_driver);
  3175. +
  3176. +MODULE_AUTHOR("Blaž Hrastnik <blaz@mxxn.io>");
  3177. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  3178. +MODULE_DESCRIPTION("HID transport driver for Surface System Aggregator Module");
  3179. +MODULE_LICENSE("GPL");
  3180. diff --git a/drivers/hid/surface-hid/surface_hid_core.c b/drivers/hid/surface-hid/surface_hid_core.c
  3181. new file mode 100644
  3182. index 000000000000..5571e74abe91
  3183. --- /dev/null
  3184. +++ b/drivers/hid/surface-hid/surface_hid_core.c
  3185. @@ -0,0 +1,272 @@
  3186. +// SPDX-License-Identifier: GPL-2.0+
  3187. +/*
  3188. + * Common/core components for the Surface System Aggregator Module (SSAM) HID
  3189. + * transport driver. Provides support for integrated HID devices on Microsoft
  3190. + * Surface models.
  3191. + *
  3192. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  3193. + */
  3194. +
  3195. +#include <asm/unaligned.h>
  3196. +#include <linux/hid.h>
  3197. +#include <linux/kernel.h>
  3198. +#include <linux/module.h>
  3199. +#include <linux/types.h>
  3200. +#include <linux/usb/ch9.h>
  3201. +
  3202. +#include <linux/surface_aggregator/controller.h>
  3203. +
  3204. +#include "surface_hid_core.h"
  3205. +
  3206. +
  3207. +/* -- Device descriptor access. --------------------------------------------- */
  3208. +
  3209. +static int surface_hid_load_hid_descriptor(struct surface_hid_device *shid)
  3210. +{
  3211. + int status;
  3212. +
  3213. + status = shid->ops.get_descriptor(shid, SURFACE_HID_DESC_HID,
  3214. + (u8 *)&shid->hid_desc, sizeof(shid->hid_desc));
  3215. + if (status)
  3216. + return status;
  3217. +
  3218. + if (shid->hid_desc.desc_len != sizeof(shid->hid_desc)) {
  3219. + dev_err(shid->dev, "unexpected HID descriptor length: got %u, expected %zu\n",
  3220. + shid->hid_desc.desc_len, sizeof(shid->hid_desc));
  3221. + return -EPROTO;
  3222. + }
  3223. +
  3224. + if (shid->hid_desc.desc_type != HID_DT_HID) {
  3225. + dev_err(shid->dev, "unexpected HID descriptor type: got %#04x, expected %#04x\n",
  3226. + shid->hid_desc.desc_type, HID_DT_HID);
  3227. + return -EPROTO;
  3228. + }
  3229. +
  3230. + if (shid->hid_desc.num_descriptors != 1) {
  3231. + dev_err(shid->dev, "unexpected number of descriptors: got %u, expected 1\n",
  3232. + shid->hid_desc.num_descriptors);
  3233. + return -EPROTO;
  3234. + }
  3235. +
  3236. + if (shid->hid_desc.report_desc_type != HID_DT_REPORT) {
  3237. + dev_err(shid->dev, "unexpected report descriptor type: got %#04x, expected %#04x\n",
  3238. + shid->hid_desc.report_desc_type, HID_DT_REPORT);
  3239. + return -EPROTO;
  3240. + }
  3241. +
  3242. + return 0;
  3243. +}
  3244. +
  3245. +static int surface_hid_load_device_attributes(struct surface_hid_device *shid)
  3246. +{
  3247. + int status;
  3248. +
  3249. + status = shid->ops.get_descriptor(shid, SURFACE_HID_DESC_ATTRS,
  3250. + (u8 *)&shid->attrs, sizeof(shid->attrs));
  3251. + if (status)
  3252. + return status;
  3253. +
  3254. + if (get_unaligned_le32(&shid->attrs.length) != sizeof(shid->attrs)) {
  3255. + dev_err(shid->dev, "unexpected attribute length: got %u, expected %zu\n",
  3256. + get_unaligned_le32(&shid->attrs.length), sizeof(shid->attrs));
  3257. + return -EPROTO;
  3258. + }
  3259. +
  3260. + return 0;
  3261. +}
  3262. +
  3263. +
  3264. +/* -- Transport driver (common). -------------------------------------------- */
  3265. +
  3266. +static int surface_hid_start(struct hid_device *hid)
  3267. +{
  3268. + struct surface_hid_device *shid = hid->driver_data;
  3269. +
  3270. + return ssam_notifier_register(shid->ctrl, &shid->notif);
  3271. +}
  3272. +
  3273. +static void surface_hid_stop(struct hid_device *hid)
  3274. +{
  3275. + struct surface_hid_device *shid = hid->driver_data;
  3276. +
  3277. + /* Note: This call will log errors for us, so ignore them here. */
  3278. + ssam_notifier_unregister(shid->ctrl, &shid->notif);
  3279. +}
  3280. +
  3281. +static int surface_hid_open(struct hid_device *hid)
  3282. +{
  3283. + return 0;
  3284. +}
  3285. +
  3286. +static void surface_hid_close(struct hid_device *hid)
  3287. +{
  3288. +}
  3289. +
  3290. +static int surface_hid_parse(struct hid_device *hid)
  3291. +{
  3292. + struct surface_hid_device *shid = hid->driver_data;
  3293. + size_t len = get_unaligned_le16(&shid->hid_desc.report_desc_len);
  3294. + u8 *buf;
  3295. + int status;
  3296. +
  3297. + buf = kzalloc(len, GFP_KERNEL);
  3298. + if (!buf)
  3299. + return -ENOMEM;
  3300. +
  3301. + status = shid->ops.get_descriptor(shid, SURFACE_HID_DESC_REPORT, buf, len);
  3302. + if (!status)
  3303. + status = hid_parse_report(hid, buf, len);
  3304. +
  3305. + kfree(buf);
  3306. + return status;
  3307. +}
  3308. +
  3309. +static int surface_hid_raw_request(struct hid_device *hid, unsigned char reportnum, u8 *buf,
  3310. + size_t len, unsigned char rtype, int reqtype)
  3311. +{
  3312. + struct surface_hid_device *shid = hid->driver_data;
  3313. +
  3314. + if (rtype == HID_OUTPUT_REPORT && reqtype == HID_REQ_SET_REPORT)
  3315. + return shid->ops.output_report(shid, reportnum, buf, len);
  3316. +
  3317. + else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_GET_REPORT)
  3318. + return shid->ops.get_feature_report(shid, reportnum, buf, len);
  3319. +
  3320. + else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_SET_REPORT)
  3321. + return shid->ops.set_feature_report(shid, reportnum, buf, len);
  3322. +
  3323. + return -EIO;
  3324. +}
  3325. +
  3326. +static struct hid_ll_driver surface_hid_ll_driver = {
  3327. + .start = surface_hid_start,
  3328. + .stop = surface_hid_stop,
  3329. + .open = surface_hid_open,
  3330. + .close = surface_hid_close,
  3331. + .parse = surface_hid_parse,
  3332. + .raw_request = surface_hid_raw_request,
  3333. +};
  3334. +
  3335. +
  3336. +/* -- Common device setup. -------------------------------------------------- */
  3337. +
  3338. +int surface_hid_device_add(struct surface_hid_device *shid)
  3339. +{
  3340. + int status;
  3341. +
  3342. + status = surface_hid_load_hid_descriptor(shid);
  3343. + if (status)
  3344. + return status;
  3345. +
  3346. + status = surface_hid_load_device_attributes(shid);
  3347. + if (status)
  3348. + return status;
  3349. +
  3350. + shid->hid = hid_allocate_device();
  3351. + if (IS_ERR(shid->hid))
  3352. + return PTR_ERR(shid->hid);
  3353. +
  3354. + shid->hid->dev.parent = shid->dev;
  3355. + shid->hid->bus = BUS_HOST;
  3356. + shid->hid->vendor = get_unaligned_le16(&shid->attrs.vendor);
  3357. + shid->hid->product = get_unaligned_le16(&shid->attrs.product);
  3358. + shid->hid->version = get_unaligned_le16(&shid->hid_desc.hid_version);
  3359. + shid->hid->country = shid->hid_desc.country_code;
  3360. +
  3361. + snprintf(shid->hid->name, sizeof(shid->hid->name), "Microsoft Surface %04X:%04X",
  3362. + shid->hid->vendor, shid->hid->product);
  3363. +
  3364. + strscpy(shid->hid->phys, dev_name(shid->dev), sizeof(shid->hid->phys));
  3365. +
  3366. + shid->hid->driver_data = shid;
  3367. + shid->hid->ll_driver = &surface_hid_ll_driver;
  3368. +
  3369. + status = hid_add_device(shid->hid);
  3370. + if (status)
  3371. + hid_destroy_device(shid->hid);
  3372. +
  3373. + return status;
  3374. +}
  3375. +EXPORT_SYMBOL_GPL(surface_hid_device_add);
  3376. +
  3377. +void surface_hid_device_destroy(struct surface_hid_device *shid)
  3378. +{
  3379. + hid_destroy_device(shid->hid);
  3380. +}
  3381. +EXPORT_SYMBOL_GPL(surface_hid_device_destroy);
  3382. +
  3383. +
  3384. +/* -- PM ops. --------------------------------------------------------------- */
  3385. +
  3386. +#ifdef CONFIG_PM_SLEEP
  3387. +
  3388. +static int surface_hid_suspend(struct device *dev)
  3389. +{
  3390. + struct surface_hid_device *d = dev_get_drvdata(dev);
  3391. +
  3392. + if (d->hid->driver && d->hid->driver->suspend)
  3393. + return d->hid->driver->suspend(d->hid, PMSG_SUSPEND);
  3394. +
  3395. + return 0;
  3396. +}
  3397. +
  3398. +static int surface_hid_resume(struct device *dev)
  3399. +{
  3400. + struct surface_hid_device *d = dev_get_drvdata(dev);
  3401. +
  3402. + if (d->hid->driver && d->hid->driver->resume)
  3403. + return d->hid->driver->resume(d->hid);
  3404. +
  3405. + return 0;
  3406. +}
  3407. +
  3408. +static int surface_hid_freeze(struct device *dev)
  3409. +{
  3410. + struct surface_hid_device *d = dev_get_drvdata(dev);
  3411. +
  3412. + if (d->hid->driver && d->hid->driver->suspend)
  3413. + return d->hid->driver->suspend(d->hid, PMSG_FREEZE);
  3414. +
  3415. + return 0;
  3416. +}
  3417. +
  3418. +static int surface_hid_poweroff(struct device *dev)
  3419. +{
  3420. + struct surface_hid_device *d = dev_get_drvdata(dev);
  3421. +
  3422. + if (d->hid->driver && d->hid->driver->suspend)
  3423. + return d->hid->driver->suspend(d->hid, PMSG_HIBERNATE);
  3424. +
  3425. + return 0;
  3426. +}
  3427. +
  3428. +static int surface_hid_restore(struct device *dev)
  3429. +{
  3430. + struct surface_hid_device *d = dev_get_drvdata(dev);
  3431. +
  3432. + if (d->hid->driver && d->hid->driver->reset_resume)
  3433. + return d->hid->driver->reset_resume(d->hid);
  3434. +
  3435. + return 0;
  3436. +}
  3437. +
  3438. +const struct dev_pm_ops surface_hid_pm_ops = {
  3439. + .freeze = surface_hid_freeze,
  3440. + .thaw = surface_hid_resume,
  3441. + .suspend = surface_hid_suspend,
  3442. + .resume = surface_hid_resume,
  3443. + .poweroff = surface_hid_poweroff,
  3444. + .restore = surface_hid_restore,
  3445. +};
  3446. +EXPORT_SYMBOL_GPL(surface_hid_pm_ops);
  3447. +
  3448. +#else /* CONFIG_PM_SLEEP */
  3449. +
  3450. +const struct dev_pm_ops surface_hid_pm_ops = { };
  3451. +EXPORT_SYMBOL_GPL(surface_hid_pm_ops);
  3452. +
  3453. +#endif /* CONFIG_PM_SLEEP */
  3454. +
  3455. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  3456. +MODULE_DESCRIPTION("HID transport driver core for Surface System Aggregator Module");
  3457. +MODULE_LICENSE("GPL");
  3458. diff --git a/drivers/hid/surface-hid/surface_hid_core.h b/drivers/hid/surface-hid/surface_hid_core.h
  3459. new file mode 100644
  3460. index 000000000000..4b1a7b57e035
  3461. --- /dev/null
  3462. +++ b/drivers/hid/surface-hid/surface_hid_core.h
  3463. @@ -0,0 +1,77 @@
  3464. +/* SPDX-License-Identifier: GPL-2.0+ */
  3465. +/*
  3466. + * Common/core components for the Surface System Aggregator Module (SSAM) HID
  3467. + * transport driver. Provides support for integrated HID devices on Microsoft
  3468. + * Surface models.
  3469. + *
  3470. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  3471. + */
  3472. +
  3473. +#ifndef SURFACE_HID_CORE_H
  3474. +#define SURFACE_HID_CORE_H
  3475. +
  3476. +#include <linux/hid.h>
  3477. +#include <linux/pm.h>
  3478. +#include <linux/types.h>
  3479. +
  3480. +#include <linux/surface_aggregator/controller.h>
  3481. +#include <linux/surface_aggregator/device.h>
  3482. +
  3483. +enum surface_hid_descriptor_entry {
  3484. + SURFACE_HID_DESC_HID = 0,
  3485. + SURFACE_HID_DESC_REPORT = 1,
  3486. + SURFACE_HID_DESC_ATTRS = 2,
  3487. +};
  3488. +
  3489. +struct surface_hid_descriptor {
  3490. + __u8 desc_len; /* = 9 */
  3491. + __u8 desc_type; /* = HID_DT_HID */
  3492. + __le16 hid_version;
  3493. + __u8 country_code;
  3494. + __u8 num_descriptors; /* = 1 */
  3495. +
  3496. + __u8 report_desc_type; /* = HID_DT_REPORT */
  3497. + __le16 report_desc_len;
  3498. +} __packed;
  3499. +
  3500. +static_assert(sizeof(struct surface_hid_descriptor) == 9);
  3501. +
  3502. +struct surface_hid_attributes {
  3503. + __le32 length;
  3504. + __le16 vendor;
  3505. + __le16 product;
  3506. + __le16 version;
  3507. + __u8 _unknown[22];
  3508. +} __packed;
  3509. +
  3510. +static_assert(sizeof(struct surface_hid_attributes) == 32);
  3511. +
  3512. +struct surface_hid_device;
  3513. +
  3514. +struct surface_hid_device_ops {
  3515. + int (*get_descriptor)(struct surface_hid_device *shid, u8 entry, u8 *buf, size_t len);
  3516. + int (*output_report)(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len);
  3517. + int (*get_feature_report)(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len);
  3518. + int (*set_feature_report)(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len);
  3519. +};
  3520. +
  3521. +struct surface_hid_device {
  3522. + struct device *dev;
  3523. + struct ssam_controller *ctrl;
  3524. + struct ssam_device_uid uid;
  3525. +
  3526. + struct surface_hid_descriptor hid_desc;
  3527. + struct surface_hid_attributes attrs;
  3528. +
  3529. + struct ssam_event_notifier notif;
  3530. + struct hid_device *hid;
  3531. +
  3532. + struct surface_hid_device_ops ops;
  3533. +};
  3534. +
  3535. +int surface_hid_device_add(struct surface_hid_device *shid);
  3536. +void surface_hid_device_destroy(struct surface_hid_device *shid);
  3537. +
  3538. +extern const struct dev_pm_ops surface_hid_pm_ops;
  3539. +
  3540. +#endif /* SURFACE_HID_CORE_H */
  3541. diff --git a/drivers/hid/surface-hid/surface_kbd.c b/drivers/hid/surface-hid/surface_kbd.c
  3542. new file mode 100644
  3543. index 000000000000..0635341bc517
  3544. --- /dev/null
  3545. +++ b/drivers/hid/surface-hid/surface_kbd.c
  3546. @@ -0,0 +1,300 @@
  3547. +// SPDX-License-Identifier: GPL-2.0+
  3548. +/*
  3549. + * Surface System Aggregator Module (SSAM) HID transport driver for the legacy
  3550. + * keyboard interface (KBD/TC=0x08 subsystem). Provides support for the
  3551. + * integrated HID keyboard on Surface Laptops 1 and 2.
  3552. + *
  3553. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  3554. + */
  3555. +
  3556. +#include <asm/unaligned.h>
  3557. +#include <linux/hid.h>
  3558. +#include <linux/kernel.h>
  3559. +#include <linux/module.h>
  3560. +#include <linux/platform_device.h>
  3561. +#include <linux/types.h>
  3562. +
  3563. +#include <linux/surface_aggregator/controller.h>
  3564. +
  3565. +#include "surface_hid_core.h"
  3566. +
  3567. +
  3568. +/* -- SAM interface (KBD). -------------------------------------------------- */
  3569. +
  3570. +#define KBD_FEATURE_REPORT_SIZE 7 /* 6 + report ID */
  3571. +
  3572. +enum surface_kbd_cid {
  3573. + SURFACE_KBD_CID_GET_DESCRIPTOR = 0x00,
  3574. + SURFACE_KBD_CID_SET_CAPSLOCK_LED = 0x01,
  3575. + SURFACE_KBD_CID_EVT_INPUT_GENERIC = 0x03,
  3576. + SURFACE_KBD_CID_EVT_INPUT_HOTKEYS = 0x04,
  3577. + SURFACE_KBD_CID_GET_FEATURE_REPORT = 0x0b,
  3578. +};
  3579. +
  3580. +static int ssam_kbd_get_descriptor(struct surface_hid_device *shid, u8 entry, u8 *buf, size_t len)
  3581. +{
  3582. + struct ssam_request rqst;
  3583. + struct ssam_response rsp;
  3584. + int status;
  3585. +
  3586. + rqst.target_category = shid->uid.category;
  3587. + rqst.target_id = shid->uid.target;
  3588. + rqst.command_id = SURFACE_KBD_CID_GET_DESCRIPTOR;
  3589. + rqst.instance_id = shid->uid.instance;
  3590. + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
  3591. + rqst.length = sizeof(entry);
  3592. + rqst.payload = &entry;
  3593. +
  3594. + rsp.capacity = len;
  3595. + rsp.length = 0;
  3596. + rsp.pointer = buf;
  3597. +
  3598. + status = ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, &rsp, sizeof(entry));
  3599. + if (status)
  3600. + return status;
  3601. +
  3602. + if (rsp.length != len) {
  3603. + dev_err(shid->dev, "invalid descriptor length: got %zu, expected, %zu\n",
  3604. + rsp.length, len);
  3605. + return -EPROTO;
  3606. + }
  3607. +
  3608. + return 0;
  3609. +}
  3610. +
  3611. +static int ssam_kbd_set_caps_led(struct surface_hid_device *shid, bool value)
  3612. +{
  3613. + struct ssam_request rqst;
  3614. + u8 value_u8 = value;
  3615. +
  3616. + rqst.target_category = shid->uid.category;
  3617. + rqst.target_id = shid->uid.target;
  3618. + rqst.command_id = SURFACE_KBD_CID_SET_CAPSLOCK_LED;
  3619. + rqst.instance_id = shid->uid.instance;
  3620. + rqst.flags = 0;
  3621. + rqst.length = sizeof(value_u8);
  3622. + rqst.payload = &value_u8;
  3623. +
  3624. + return ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, NULL, sizeof(value_u8));
  3625. +}
  3626. +
  3627. +static int ssam_kbd_get_feature_report(struct surface_hid_device *shid, u8 *buf, size_t len)
  3628. +{
  3629. + struct ssam_request rqst;
  3630. + struct ssam_response rsp;
  3631. + u8 payload = 0;
  3632. + int status;
  3633. +
  3634. + rqst.target_category = shid->uid.category;
  3635. + rqst.target_id = shid->uid.target;
  3636. + rqst.command_id = SURFACE_KBD_CID_GET_FEATURE_REPORT;
  3637. + rqst.instance_id = shid->uid.instance;
  3638. + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
  3639. + rqst.length = sizeof(payload);
  3640. + rqst.payload = &payload;
  3641. +
  3642. + rsp.capacity = len;
  3643. + rsp.length = 0;
  3644. + rsp.pointer = buf;
  3645. +
  3646. + status = ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, &rsp, sizeof(payload));
  3647. + if (status)
  3648. + return status;
  3649. +
  3650. + if (rsp.length != len) {
  3651. + dev_err(shid->dev, "invalid feature report length: got %zu, expected, %zu\n",
  3652. + rsp.length, len);
  3653. + return -EPROTO;
  3654. + }
  3655. +
  3656. + return 0;
  3657. +}
  3658. +
  3659. +static bool ssam_kbd_is_input_event(const struct ssam_event *event)
  3660. +{
  3661. + if (event->command_id == SURFACE_KBD_CID_EVT_INPUT_GENERIC)
  3662. + return true;
  3663. +
  3664. + if (event->command_id == SURFACE_KBD_CID_EVT_INPUT_HOTKEYS)
  3665. + return true;
  3666. +
  3667. + return false;
  3668. +}
  3669. +
  3670. +static u32 ssam_kbd_event_fn(struct ssam_event_notifier *nf, const struct ssam_event *event)
  3671. +{
  3672. + struct surface_hid_device *shid = container_of(nf, struct surface_hid_device, notif);
  3673. +
  3674. + /*
  3675. + * Check against device UID manually, as registry and device target
  3676. + * category doesn't line up.
  3677. + */
  3678. +
  3679. + if (shid->uid.category != event->target_category)
  3680. + return 0;
  3681. +
  3682. + if (shid->uid.target != event->target_id)
  3683. + return 0;
  3684. +
  3685. + if (shid->uid.instance != event->instance_id)
  3686. + return 0;
  3687. +
  3688. + if (!ssam_kbd_is_input_event(event))
  3689. + return 0;
  3690. +
  3691. + hid_input_report(shid->hid, HID_INPUT_REPORT, (u8 *)&event->data[0], event->length, 0);
  3692. + return SSAM_NOTIF_HANDLED;
  3693. +}
  3694. +
  3695. +
  3696. +/* -- Transport driver (KBD). ----------------------------------------------- */
  3697. +
  3698. +static int skbd_get_caps_led_value(struct hid_device *hid, u8 rprt_id, u8 *buf, size_t len)
  3699. +{
  3700. + struct hid_field *field;
  3701. + unsigned int offset, size;
  3702. + int i;
  3703. +
  3704. + /* Get LED field. */
  3705. + field = hidinput_get_led_field(hid);
  3706. + if (!field)
  3707. + return -ENOENT;
  3708. +
  3709. + /* Check if we got the correct report. */
  3710. + if (len != hid_report_len(field->report))
  3711. + return -ENOENT;
  3712. +
  3713. + if (rprt_id != field->report->id)
  3714. + return -ENOENT;
  3715. +
  3716. + /* Get caps lock LED index. */
  3717. + for (i = 0; i < field->report_count; i++)
  3718. + if ((field->usage[i].hid & 0xffff) == 0x02)
  3719. + break;
  3720. +
  3721. + if (i == field->report_count)
  3722. + return -ENOENT;
  3723. +
  3724. + /* Extract value. */
  3725. + size = field->report_size;
  3726. + offset = field->report_offset + i * size;
  3727. + return !!hid_field_extract(hid, buf + 1, size, offset);
  3728. +}
  3729. +
  3730. +static int skbd_output_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
  3731. +{
  3732. + int caps_led;
  3733. + int status;
  3734. +
  3735. + caps_led = skbd_get_caps_led_value(shid->hid, rprt_id, buf, len);
  3736. + if (caps_led < 0)
  3737. + return -EIO; /* Only caps LED output reports are supported. */
  3738. +
  3739. + status = ssam_kbd_set_caps_led(shid, caps_led);
  3740. + if (status < 0)
  3741. + return status;
  3742. +
  3743. + return len;
  3744. +}
  3745. +
  3746. +static int skbd_get_feature_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
  3747. +{
  3748. + u8 report[KBD_FEATURE_REPORT_SIZE];
  3749. + int status;
  3750. +
  3751. + /*
  3752. + * The keyboard only has a single hard-coded read-only feature report
  3753. + * of size KBD_FEATURE_REPORT_SIZE. Try to load it and compare its
  3754. + * report ID against the requested one.
  3755. + */
  3756. +
  3757. + if (len < ARRAY_SIZE(report))
  3758. + return -ENOSPC;
  3759. +
  3760. + status = ssam_kbd_get_feature_report(shid, report, ARRAY_SIZE(report));
  3761. + if (status < 0)
  3762. + return status;
  3763. +
  3764. + if (rprt_id != report[0])
  3765. + return -ENOENT;
  3766. +
  3767. + memcpy(buf, report, ARRAY_SIZE(report));
  3768. + return len;
  3769. +}
  3770. +
  3771. +static int skbd_set_feature_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
  3772. +{
  3773. + /* Not supported. See skbd_get_feature_report() for details. */
  3774. + return -EIO;
  3775. +}
  3776. +
  3777. +
  3778. +/* -- Driver setup. --------------------------------------------------------- */
  3779. +
  3780. +static int surface_kbd_probe(struct platform_device *pdev)
  3781. +{
  3782. + struct ssam_controller *ctrl;
  3783. + struct surface_hid_device *shid;
  3784. +
  3785. + /* Add device link to EC. */
  3786. + ctrl = ssam_client_bind(&pdev->dev);
  3787. + if (IS_ERR(ctrl))
  3788. + return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
  3789. +
  3790. + shid = devm_kzalloc(&pdev->dev, sizeof(*shid), GFP_KERNEL);
  3791. + if (!shid)
  3792. + return -ENOMEM;
  3793. +
  3794. + shid->dev = &pdev->dev;
  3795. + shid->ctrl = ctrl;
  3796. +
  3797. + shid->uid.domain = SSAM_DOMAIN_SERIALHUB;
  3798. + shid->uid.category = SSAM_SSH_TC_KBD;
  3799. + shid->uid.target = 2;
  3800. + shid->uid.instance = 0;
  3801. + shid->uid.function = 0;
  3802. +
  3803. + shid->notif.base.priority = 1;
  3804. + shid->notif.base.fn = ssam_kbd_event_fn;
  3805. + shid->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
  3806. + shid->notif.event.id.target_category = shid->uid.category;
  3807. + shid->notif.event.id.instance = shid->uid.instance;
  3808. + shid->notif.event.mask = SSAM_EVENT_MASK_NONE;
  3809. + shid->notif.event.flags = 0;
  3810. +
  3811. + shid->ops.get_descriptor = ssam_kbd_get_descriptor;
  3812. + shid->ops.output_report = skbd_output_report;
  3813. + shid->ops.get_feature_report = skbd_get_feature_report;
  3814. + shid->ops.set_feature_report = skbd_set_feature_report;
  3815. +
  3816. + platform_set_drvdata(pdev, shid);
  3817. + return surface_hid_device_add(shid);
  3818. +}
  3819. +
  3820. +static int surface_kbd_remove(struct platform_device *pdev)
  3821. +{
  3822. + surface_hid_device_destroy(platform_get_drvdata(pdev));
  3823. + return 0;
  3824. +}
  3825. +
  3826. +static const struct acpi_device_id surface_kbd_match[] = {
  3827. + { "MSHW0096" },
  3828. + { },
  3829. +};
  3830. +MODULE_DEVICE_TABLE(acpi, surface_kbd_match);
  3831. +
  3832. +static struct platform_driver surface_kbd_driver = {
  3833. + .probe = surface_kbd_probe,
  3834. + .remove = surface_kbd_remove,
  3835. + .driver = {
  3836. + .name = "surface_keyboard",
  3837. + .acpi_match_table = surface_kbd_match,
  3838. + .pm = &surface_hid_pm_ops,
  3839. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  3840. + },
  3841. +};
  3842. +module_platform_driver(surface_kbd_driver);
  3843. +
  3844. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  3845. +MODULE_DESCRIPTION("HID legacy transport driver for Surface System Aggregator Module");
  3846. +MODULE_LICENSE("GPL");
  3847. diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
  3848. index 6a53f63bde62..d44d3fb9ca72 100644
  3849. --- a/drivers/platform/x86/Kconfig
  3850. +++ b/drivers/platform/x86/Kconfig
  3851. @@ -870,6 +870,108 @@ config INTEL_VBTN
  3852. To compile this driver as a module, choose M here: the module will
  3853. be called intel_vbtn.
  3854. +source "drivers/platform/x86/surface_aggregator/Kconfig"
  3855. +
  3856. +config SURFACE_AGGREGATOR_REGISTRY
  3857. + tristate "Surface System Aggregator Module Device Registry"
  3858. + depends on SURFACE_AGGREGATOR
  3859. + depends on SURFACE_AGGREGATOR_BUS
  3860. + help
  3861. + Device-registry and device-hubs for Surface System Aggregator Module
  3862. + (SSAM) devices.
  3863. +
  3864. + Provides a module and driver which act as a device-registry for SSAM
  3865. + client devices that cannot be detected automatically, e.g. via ACPI.
  3866. + Such devices are instead provided via this registry and attached via
  3867. + device hubs, also provided in this module.
  3868. +
  3869. + Devices provided via this registry are:
  3870. + - Platform profile (performance-/cooling-mode) device (5th- and later
  3871. + generations).
  3872. + - Battery/AC devices (7th-generation).
  3873. + - HID input devices (7th-generation).
  3874. +
  3875. + Select M (recommended) or Y here if you want support for the above
  3876. + mentioned devices on the corresponding Surface models. Without this
  3877. + module, the respective devices will not be instantiated and thus any
  3878. + functionality provided by them will be missing, even when drivers for
  3879. + these devices are present. In other words, this module only provides
  3880. + the respective client devices. Drivers for these devices still need to
  3881. + be selected via the other options.
  3882. +
  3883. +config SURFACE_AGGREGATOR_CDEV
  3884. + tristate "Surface System Aggregator Module User-Space Interface"
  3885. + depends on SURFACE_AGGREGATOR
  3886. + help
  3887. + Provides a misc-device interface to the Surface System Aggregator
  3888. + Module (SSAM) controller.
  3889. +
  3890. + This option provides a module (called surface_aggregator_cdev), that,
  3891. + when loaded, will add a client device (and its respective driver) to
  3892. + the SSAM controller. Said client device manages a misc-device
  3893. + interface (/dev/surface/aggregator), which can be used by user-space
  3894. + tools to directly communicate with the SSAM EC by sending requests and
  3895. + receiving the corresponding responses.
  3896. +
  3897. + The provided interface is intended for debugging and development only,
  3898. + and should not be used otherwise.
  3899. +
  3900. +config SURFACE_ACPI_NOTIFY
  3901. + tristate "Surface ACPI Notify Driver"
  3902. + depends on SURFACE_AGGREGATOR
  3903. + help
  3904. + Surface ACPI Notify (SAN) driver for Microsoft Surface devices.
  3905. +
  3906. + This driver provides support for the ACPI interface (called SAN) of
  3907. + the Surface System Aggregator Module (SSAM) EC. This interface is used
  3908. + on 5th- and 6th-generation Microsoft Surface devices (including
  3909. + Surface Pro 5 and 6, Surface Book 2, Surface Laptops 1 and 2, and in
  3910. + reduced functionality on the Surface Laptop 3) to execute SSAM
  3911. + requests directly from ACPI code, as well as receive SSAM events and
  3912. + turn them into ACPI notifications. It essentially acts as a
  3913. + translation layer between the SSAM controller and ACPI.
  3914. +
  3915. + Specifically, this driver may be needed for battery status reporting,
  3916. + thermal sensor access, and real-time clock information, depending on
  3917. + the Surface device in question.
  3918. +
  3919. +config SURFACE_PERFMODE
  3920. + tristate "Surface Performance-Mode Driver"
  3921. + depends on SURFACE_AGGREGATOR_BUS
  3922. + depends on SYSFS
  3923. + help
  3924. + Driver for the performance-/cooling-mode interface of Microsoft
  3925. + Surface devices.
  3926. +
  3927. + Microsoft Surface devices using the Surface System Aggregator Module
  3928. + (SSAM) can be switched between different performance modes. This,
  3929. + depending on the device, can influence their cooling behavior and may
  3930. + influence power limits, allowing users to choose between performance
  3931. + and higher power-draw, or lower power-draw and more silent operation.
  3932. +
  3933. + This driver provides a user-space interface (via sysfs) for
  3934. + controlling said mode via the corresponding client device.
  3935. +
  3936. +config SURFACE_DTX
  3937. + tristate "Surface DTX (Detachment System) Driver"
  3938. + depends on SURFACE_AGGREGATOR
  3939. + depends on INPUT
  3940. + help
  3941. + Driver for the Surface Book clipboard detachment system (DTX).
  3942. +
  3943. + On the Surface Book series devices, the display part containing the
  3944. + CPU (called the clipboard) can be detached from the base (containing a
  3945. + battery, the keyboard, and, optionally, a discrete GPU) by (if
  3946. + necessary) unlocking and opening the latch connecting both parts.
  3947. +
  3948. + This driver provides a user-space interface that can influence the
  3949. + behavior of this process, which includes the option to abort it in
  3950. + case the base is still in use or speed it up in case it is not.
  3951. +
  3952. + Note that this module can be built without support for the Surface
  3953. + Aggregator Bus (i.e. CONFIG_SURFACE_AGGREGATOR_BUS=n). In that case,
  3954. + some devices, specifically the Surface Book 3, will not be supported.
  3955. +
  3956. config SURFACE3_WMI
  3957. tristate "Surface 3 WMI Driver"
  3958. depends on ACPI_WMI
  3959. diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
  3960. index 562d83940e7b..2e0a2896c78d 100644
  3961. --- a/drivers/platform/x86/Makefile
  3962. +++ b/drivers/platform/x86/Makefile
  3963. @@ -82,6 +82,13 @@ obj-$(CONFIG_INTEL_OAKTRAIL) += intel_oaktrail.o
  3964. obj-$(CONFIG_INTEL_VBTN) += intel-vbtn.o
  3965. # Microsoft
  3966. +obj-$(CONFIG_SURFACE_AGGREGATOR) += surface_aggregator/
  3967. +obj-$(CONFIG_SURFACE_AGGREGATOR_REGISTRY) += surface_aggregator_registry.o
  3968. +obj-$(CONFIG_SURFACE_AGGREGATOR_CDEV) += surface_aggregator_cdev.o
  3969. +obj-$(CONFIG_SURFACE_ACPI_NOTIFY) += surface_acpi_notify.o
  3970. +obj-$(CONFIG_SURFACE_PERFMODE) += surface_perfmode.o
  3971. +obj-$(CONFIG_SURFACE_DTX) += surface_dtx.o
  3972. +
  3973. obj-$(CONFIG_SURFACE3_WMI) += surface3-wmi.o
  3974. obj-$(CONFIG_SURFACE_3_BUTTON) += surface3_button.o
  3975. obj-$(CONFIG_SURFACE_3_POWER_OPREGION) += surface3_power.o
  3976. diff --git a/drivers/platform/x86/surface_acpi_notify.c b/drivers/platform/x86/surface_acpi_notify.c
  3977. new file mode 100644
  3978. index 000000000000..ef9c1f8e8336
  3979. --- /dev/null
  3980. +++ b/drivers/platform/x86/surface_acpi_notify.c
  3981. @@ -0,0 +1,886 @@
  3982. +// SPDX-License-Identifier: GPL-2.0+
  3983. +/*
  3984. + * Driver for the Surface ACPI Notify (SAN) interface/shim.
  3985. + *
  3986. + * Translates communication from ACPI to Surface System Aggregator Module
  3987. + * (SSAM/SAM) requests and back, specifically SAM-over-SSH. Translates SSAM
  3988. + * events back to ACPI notifications. Allows handling of discrete GPU
  3989. + * notifications sent from ACPI via the SAN interface by providing them to any
  3990. + * registered external driver.
  3991. + *
  3992. + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
  3993. + */
  3994. +
  3995. +#include <asm/unaligned.h>
  3996. +#include <linux/acpi.h>
  3997. +#include <linux/delay.h>
  3998. +#include <linux/jiffies.h>
  3999. +#include <linux/kernel.h>
  4000. +#include <linux/module.h>
  4001. +#include <linux/notifier.h>
  4002. +#include <linux/platform_device.h>
  4003. +#include <linux/rwsem.h>
  4004. +
  4005. +#include <linux/surface_aggregator/controller.h>
  4006. +#include <linux/surface_acpi_notify.h>
  4007. +
  4008. +struct san_data {
  4009. + struct device *dev;
  4010. + struct ssam_controller *ctrl;
  4011. +
  4012. + struct acpi_connection_info info;
  4013. +
  4014. + struct ssam_event_notifier nf_bat;
  4015. + struct ssam_event_notifier nf_tmp;
  4016. +};
  4017. +
  4018. +#define to_san_data(ptr, member) \
  4019. + container_of(ptr, struct san_data, member)
  4020. +
  4021. +
  4022. +/* -- dGPU notifier interface. ---------------------------------------------- */
  4023. +
  4024. +struct san_rqsg_if {
  4025. + struct rw_semaphore lock;
  4026. + struct device *dev;
  4027. + struct blocking_notifier_head nh;
  4028. +};
  4029. +
  4030. +static struct san_rqsg_if san_rqsg_if = {
  4031. + .lock = __RWSEM_INITIALIZER(san_rqsg_if.lock),
  4032. + .dev = NULL,
  4033. + .nh = BLOCKING_NOTIFIER_INIT(san_rqsg_if.nh),
  4034. +};
  4035. +
  4036. +static int san_set_rqsg_interface_device(struct device *dev)
  4037. +{
  4038. + int status = 0;
  4039. +
  4040. + down_write(&san_rqsg_if.lock);
  4041. + if (!san_rqsg_if.dev && dev)
  4042. + san_rqsg_if.dev = dev;
  4043. + else
  4044. + status = -EBUSY;
  4045. + up_write(&san_rqsg_if.lock);
  4046. +
  4047. + return status;
  4048. +}
  4049. +
  4050. +/**
  4051. + * san_client_link() - Link client as consumer to SAN device.
  4052. + * @client: The client to link.
  4053. + *
  4054. + * Sets up a device link between the provided client device as consumer and
  4055. + * the SAN device as provider. This function can be used to ensure that the
  4056. + * SAN interface has been set up and will be set up for as long as the driver
  4057. + * of the client device is bound. This guarantees that, during that time, all
  4058. + * dGPU events will be received by any registered notifier.
  4059. + *
  4060. + * The link will be automatically removed once the client device's driver is
  4061. + * unbound.
  4062. + *
  4063. + * Return: Returns zero on success, %-ENXIO if the SAN interface has not been
  4064. + * set up yet, and %-ENOMEM if device link creation failed.
  4065. + */
  4066. +int san_client_link(struct device *client)
  4067. +{
  4068. + const u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;
  4069. + struct device_link *link;
  4070. +
  4071. + down_read(&san_rqsg_if.lock);
  4072. +
  4073. + if (!san_rqsg_if.dev) {
  4074. + up_read(&san_rqsg_if.lock);
  4075. + return -ENXIO;
  4076. + }
  4077. +
  4078. + link = device_link_add(client, san_rqsg_if.dev, flags);
  4079. + if (!link) {
  4080. + up_read(&san_rqsg_if.lock);
  4081. + return -ENOMEM;
  4082. + }
  4083. +
  4084. + if (READ_ONCE(link->status) == DL_STATE_SUPPLIER_UNBIND) {
  4085. + up_read(&san_rqsg_if.lock);
  4086. + return -ENXIO;
  4087. + }
  4088. +
  4089. + up_read(&san_rqsg_if.lock);
  4090. + return 0;
  4091. +}
  4092. +EXPORT_SYMBOL_GPL(san_client_link);
  4093. +
  4094. +/**
  4095. + * san_dgpu_notifier_register() - Register a SAN dGPU notifier.
  4096. + * @nb: The notifier-block to register.
  4097. + *
  4098. + * Registers a SAN dGPU notifier, receiving any new SAN dGPU events sent from
  4099. + * ACPI. The registered notifier will be called with &struct san_dgpu_event
  4100. + * as notifier data and the command ID of that event as notifier action.
  4101. + */
  4102. +int san_dgpu_notifier_register(struct notifier_block *nb)
  4103. +{
  4104. + return blocking_notifier_chain_register(&san_rqsg_if.nh, nb);
  4105. +}
  4106. +EXPORT_SYMBOL_GPL(san_dgpu_notifier_register);
  4107. +
  4108. +/**
  4109. + * san_dgpu_notifier_unregister() - Unregister a SAN dGPU notifier.
  4110. + * @nb: The notifier-block to unregister.
  4111. + */
  4112. +int san_dgpu_notifier_unregister(struct notifier_block *nb)
  4113. +{
  4114. + return blocking_notifier_chain_unregister(&san_rqsg_if.nh, nb);
  4115. +}
  4116. +EXPORT_SYMBOL_GPL(san_dgpu_notifier_unregister);
  4117. +
  4118. +static int san_dgpu_notifier_call(struct san_dgpu_event *evt)
  4119. +{
  4120. + int ret;
  4121. +
  4122. + ret = blocking_notifier_call_chain(&san_rqsg_if.nh, evt->command, evt);
  4123. + return notifier_to_errno(ret);
  4124. +}
  4125. +
  4126. +
  4127. +/* -- ACPI _DSM event relay. ------------------------------------------------ */
  4128. +
  4129. +#define SAN_DSM_REVISION 0
  4130. +
  4131. +/* 93b666c5-70c6-469f-a215-3d487c91ab3c */
  4132. +static const guid_t SAN_DSM_UUID =
  4133. + GUID_INIT(0x93b666c5, 0x70c6, 0x469f, 0xa2, 0x15, 0x3d,
  4134. + 0x48, 0x7c, 0x91, 0xab, 0x3c);
  4135. +
  4136. +enum san_dsm_event_fn {
  4137. + SAN_DSM_EVENT_FN_BAT1_STAT = 0x03,
  4138. + SAN_DSM_EVENT_FN_BAT1_INFO = 0x04,
  4139. + SAN_DSM_EVENT_FN_ADP1_STAT = 0x05,
  4140. + SAN_DSM_EVENT_FN_ADP1_INFO = 0x06,
  4141. + SAN_DSM_EVENT_FN_BAT2_STAT = 0x07,
  4142. + SAN_DSM_EVENT_FN_BAT2_INFO = 0x08,
  4143. + SAN_DSM_EVENT_FN_THERMAL = 0x09,
  4144. + SAN_DSM_EVENT_FN_DPTF = 0x0a,
  4145. +};
  4146. +
  4147. +enum sam_event_cid_bat {
  4148. + SAM_EVENT_CID_BAT_BIX = 0x15,
  4149. + SAM_EVENT_CID_BAT_BST = 0x16,
  4150. + SAM_EVENT_CID_BAT_ADP = 0x17,
  4151. + SAM_EVENT_CID_BAT_PROT = 0x18,
  4152. + SAM_EVENT_CID_BAT_DPTF = 0x4f,
  4153. +};
  4154. +
  4155. +enum sam_event_cid_tmp {
  4156. + SAM_EVENT_CID_TMP_TRIP = 0x0b,
  4157. +};
  4158. +
  4159. +struct san_event_work {
  4160. + struct delayed_work work;
  4161. + struct device *dev;
  4162. + struct ssam_event event; /* must be last */
  4163. +};
  4164. +
  4165. +static int san_acpi_notify_event(struct device *dev, u64 func,
  4166. + union acpi_object *param)
  4167. +{
  4168. + acpi_handle san = ACPI_HANDLE(dev);
  4169. + union acpi_object *obj;
  4170. + int status = 0;
  4171. +
  4172. + if (!acpi_check_dsm(san, &SAN_DSM_UUID, SAN_DSM_REVISION, BIT_ULL(func)))
  4173. + return 0;
  4174. +
  4175. + dev_dbg(dev, "notify event %#04llx\n", func);
  4176. +
  4177. + obj = acpi_evaluate_dsm_typed(san, &SAN_DSM_UUID, SAN_DSM_REVISION,
  4178. + func, param, ACPI_TYPE_BUFFER);
  4179. + if (!obj)
  4180. + return -EFAULT;
  4181. +
  4182. + if (obj->buffer.length != 1 || obj->buffer.pointer[0] != 0) {
  4183. + dev_err(dev, "got unexpected result from _DSM\n");
  4184. + status = -EPROTO;
  4185. + }
  4186. +
  4187. + ACPI_FREE(obj);
  4188. + return status;
  4189. +}
  4190. +
  4191. +static int san_evt_bat_adp(struct device *dev, const struct ssam_event *event)
  4192. +{
  4193. + int status;
  4194. +
  4195. + status = san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_ADP1_STAT, NULL);
  4196. + if (status)
  4197. + return status;
  4198. +
  4199. + /*
  4200. + * Ensure that the battery states get updated correctly. When the
  4201. + * battery is fully charged and an adapter is plugged in, it sometimes
  4202. + * is not updated correctly, instead showing it as charging.
  4203. + * Explicitly trigger battery updates to fix this.
  4204. + */
  4205. +
  4206. + status = san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_BAT1_STAT, NULL);
  4207. + if (status)
  4208. + return status;
  4209. +
  4210. + return san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_BAT2_STAT, NULL);
  4211. +}
  4212. +
  4213. +static int san_evt_bat_bix(struct device *dev, const struct ssam_event *event)
  4214. +{
  4215. + enum san_dsm_event_fn fn;
  4216. +
  4217. + if (event->instance_id == 0x02)
  4218. + fn = SAN_DSM_EVENT_FN_BAT2_INFO;
  4219. + else
  4220. + fn = SAN_DSM_EVENT_FN_BAT1_INFO;
  4221. +
  4222. + return san_acpi_notify_event(dev, fn, NULL);
  4223. +}
  4224. +
  4225. +static int san_evt_bat_bst(struct device *dev, const struct ssam_event *event)
  4226. +{
  4227. + enum san_dsm_event_fn fn;
  4228. +
  4229. + if (event->instance_id == 0x02)
  4230. + fn = SAN_DSM_EVENT_FN_BAT2_STAT;
  4231. + else
  4232. + fn = SAN_DSM_EVENT_FN_BAT1_STAT;
  4233. +
  4234. + return san_acpi_notify_event(dev, fn, NULL);
  4235. +}
  4236. +
  4237. +static int san_evt_bat_dptf(struct device *dev, const struct ssam_event *event)
  4238. +{
  4239. + union acpi_object payload;
  4240. +
  4241. + /*
  4242. + * The Surface ACPI expects a buffer and not a package. It specifically
  4243. + * checks for ObjectType (Arg3) == 0x03. This will cause a warning in
  4244. + * acpica/nsarguments.c, but that warning can be safely ignored.
  4245. + */
  4246. + payload.type = ACPI_TYPE_BUFFER;
  4247. + payload.buffer.length = event->length;
  4248. + payload.buffer.pointer = (u8 *)&event->data[0];
  4249. +
  4250. + return san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_DPTF, &payload);
  4251. +}
  4252. +
  4253. +static unsigned long san_evt_bat_delay(u8 cid)
  4254. +{
  4255. + switch (cid) {
  4256. + case SAM_EVENT_CID_BAT_ADP:
  4257. + /*
  4258. + * Wait for battery state to update before signaling adapter
  4259. + * change.
  4260. + */
  4261. + return msecs_to_jiffies(5000);
  4262. +
  4263. + case SAM_EVENT_CID_BAT_BST:
  4264. + /* Ensure we do not miss anything important due to caching. */
  4265. + return msecs_to_jiffies(2000);
  4266. +
  4267. + default:
  4268. + return 0;
  4269. + }
  4270. +}
  4271. +
  4272. +static bool san_evt_bat(const struct ssam_event *event, struct device *dev)
  4273. +{
  4274. + int status;
  4275. +
  4276. + switch (event->command_id) {
  4277. + case SAM_EVENT_CID_BAT_BIX:
  4278. + status = san_evt_bat_bix(dev, event);
  4279. + break;
  4280. +
  4281. + case SAM_EVENT_CID_BAT_BST:
  4282. + status = san_evt_bat_bst(dev, event);
  4283. + break;
  4284. +
  4285. + case SAM_EVENT_CID_BAT_ADP:
  4286. + status = san_evt_bat_adp(dev, event);
  4287. + break;
  4288. +
  4289. + case SAM_EVENT_CID_BAT_PROT:
  4290. + /*
  4291. + * TODO: Implement support for battery protection status change
  4292. + * event.
  4293. + */
  4294. + return true;
  4295. +
  4296. + case SAM_EVENT_CID_BAT_DPTF:
  4297. + status = san_evt_bat_dptf(dev, event);
  4298. + break;
  4299. +
  4300. + default:
  4301. + return false;
  4302. + }
  4303. +
  4304. + if (status) {
  4305. + dev_err(dev, "error handling power event (cid = %#04x)\n",
  4306. + event->command_id);
  4307. + }
  4308. +
  4309. + return true;
  4310. +}
  4311. +
  4312. +static void san_evt_bat_workfn(struct work_struct *work)
  4313. +{
  4314. + struct san_event_work *ev;
  4315. +
  4316. + ev = container_of(work, struct san_event_work, work.work);
  4317. + san_evt_bat(&ev->event, ev->dev);
  4318. + kfree(ev);
  4319. +}
  4320. +
  4321. +static u32 san_evt_bat_nf(struct ssam_event_notifier *nf,
  4322. + const struct ssam_event *event)
  4323. +{
  4324. + struct san_data *d = to_san_data(nf, nf_bat);
  4325. + struct san_event_work *work;
  4326. + unsigned long delay = san_evt_bat_delay(event->command_id);
  4327. +
  4328. + if (delay == 0)
  4329. + return san_evt_bat(event, d->dev) ? SSAM_NOTIF_HANDLED : 0;
  4330. +
  4331. + work = kzalloc(sizeof(*work) + event->length, GFP_KERNEL);
  4332. + if (!work)
  4333. + return ssam_notifier_from_errno(-ENOMEM);
  4334. +
  4335. + INIT_DELAYED_WORK(&work->work, san_evt_bat_workfn);
  4336. + work->dev = d->dev;
  4337. +
  4338. + memcpy(&work->event, event, sizeof(struct ssam_event) + event->length);
  4339. +
  4340. + schedule_delayed_work(&work->work, delay);
  4341. + return SSAM_NOTIF_HANDLED;
  4342. +}
  4343. +
  4344. +static int san_evt_tmp_trip(struct device *dev, const struct ssam_event *event)
  4345. +{
  4346. + union acpi_object param;
  4347. +
  4348. + /*
  4349. + * The Surface ACPI expects an integer and not a package. This will
  4350. + * cause a warning in acpica/nsarguments.c, but that warning can be
  4351. + * safely ignored.
  4352. + */
  4353. + param.type = ACPI_TYPE_INTEGER;
  4354. + param.integer.value = event->instance_id;
  4355. +
  4356. + return san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_THERMAL, &param);
  4357. +}
  4358. +
  4359. +static bool san_evt_tmp(const struct ssam_event *event, struct device *dev)
  4360. +{
  4361. + int status;
  4362. +
  4363. + switch (event->command_id) {
  4364. + case SAM_EVENT_CID_TMP_TRIP:
  4365. + status = san_evt_tmp_trip(dev, event);
  4366. + break;
  4367. +
  4368. + default:
  4369. + return false;
  4370. + }
  4371. +
  4372. + if (status) {
  4373. + dev_err(dev, "error handling thermal event (cid = %#04x)\n",
  4374. + event->command_id);
  4375. + }
  4376. +
  4377. + return true;
  4378. +}
  4379. +
  4380. +static u32 san_evt_tmp_nf(struct ssam_event_notifier *nf,
  4381. + const struct ssam_event *event)
  4382. +{
  4383. + struct san_data *d = to_san_data(nf, nf_tmp);
  4384. +
  4385. + return san_evt_tmp(event, d->dev) ? SSAM_NOTIF_HANDLED : 0;
  4386. +}
  4387. +
  4388. +
  4389. +/* -- ACPI GSB OperationRegion handler -------------------------------------- */
  4390. +
  4391. +struct gsb_data_in {
  4392. + u8 cv;
  4393. +} __packed;
  4394. +
  4395. +struct gsb_data_rqsx {
  4396. + u8 cv; /* Command value (san_gsb_request_cv). */
  4397. + u8 tc; /* Target category. */
  4398. + u8 tid; /* Target ID. */
  4399. + u8 iid; /* Instance ID. */
  4400. + u8 snc; /* Expect-response-flag. */
  4401. + u8 cid; /* Command ID. */
  4402. + u16 cdl; /* Payload length. */
  4403. + u8 pld[]; /* Payload. */
  4404. +} __packed;
  4405. +
  4406. +struct gsb_data_etwl {
  4407. + u8 cv; /* Command value (should be 0x02). */
  4408. + u8 etw3; /* Unknown. */
  4409. + u8 etw4; /* Unknown. */
  4410. + u8 msg[]; /* Error message (ASCIIZ). */
  4411. +} __packed;
  4412. +
  4413. +struct gsb_data_out {
  4414. + u8 status; /* _SSH communication status. */
  4415. + u8 len; /* _SSH payload length. */
  4416. + u8 pld[]; /* _SSH payload. */
  4417. +} __packed;
  4418. +
  4419. +union gsb_buffer_data {
  4420. + struct gsb_data_in in; /* Common input. */
  4421. + struct gsb_data_rqsx rqsx; /* RQSX input. */
  4422. + struct gsb_data_etwl etwl; /* ETWL input. */
  4423. + struct gsb_data_out out; /* Output. */
  4424. +};
  4425. +
  4426. +struct gsb_buffer {
  4427. + u8 status; /* GSB AttribRawProcess status. */
  4428. + u8 len; /* GSB AttribRawProcess length. */
  4429. + union gsb_buffer_data data;
  4430. +} __packed;
  4431. +
  4432. +#define SAN_GSB_MAX_RQSX_PAYLOAD (U8_MAX - 2 - sizeof(struct gsb_data_rqsx))
  4433. +#define SAN_GSB_MAX_RESPONSE (U8_MAX - 2 - sizeof(struct gsb_data_out))
  4434. +
  4435. +#define SAN_GSB_COMMAND 0
  4436. +
  4437. +enum san_gsb_request_cv {
  4438. + SAN_GSB_REQUEST_CV_RQST = 0x01,
  4439. + SAN_GSB_REQUEST_CV_ETWL = 0x02,
  4440. + SAN_GSB_REQUEST_CV_RQSG = 0x03,
  4441. +};
  4442. +
  4443. +#define SAN_REQUEST_NUM_TRIES 5
  4444. +
  4445. +static acpi_status san_etwl(struct san_data *d, struct gsb_buffer *b)
  4446. +{
  4447. + struct gsb_data_etwl *etwl = &b->data.etwl;
  4448. +
  4449. + if (b->len < sizeof(struct gsb_data_etwl)) {
  4450. + dev_err(d->dev, "invalid ETWL package (len = %d)\n", b->len);
  4451. + return AE_OK;
  4452. + }
  4453. +
  4454. + dev_err(d->dev, "ETWL(%#04x, %#04x): %.*s\n", etwl->etw3, etwl->etw4,
  4455. + (unsigned int)(b->len - sizeof(struct gsb_data_etwl)),
  4456. + (char *)etwl->msg);
  4457. +
  4458. + /* Indicate success. */
  4459. + b->status = 0x00;
  4460. + b->len = 0x00;
  4461. +
  4462. + return AE_OK;
  4463. +}
  4464. +
  4465. +static
  4466. +struct gsb_data_rqsx *san_validate_rqsx(struct device *dev, const char *type,
  4467. + struct gsb_buffer *b)
  4468. +{
  4469. + struct gsb_data_rqsx *rqsx = &b->data.rqsx;
  4470. +
  4471. + if (b->len < sizeof(struct gsb_data_rqsx)) {
  4472. + dev_err(dev, "invalid %s package (len = %d)\n", type, b->len);
  4473. + return NULL;
  4474. + }
  4475. +
  4476. + if (get_unaligned(&rqsx->cdl) != b->len - sizeof(struct gsb_data_rqsx)) {
  4477. + dev_err(dev, "bogus %s package (len = %d, cdl = %d)\n",
  4478. + type, b->len, get_unaligned(&rqsx->cdl));
  4479. + return NULL;
  4480. + }
  4481. +
  4482. + if (get_unaligned(&rqsx->cdl) > SAN_GSB_MAX_RQSX_PAYLOAD) {
  4483. + dev_err(dev, "payload for %s package too large (cdl = %d)\n",
  4484. + type, get_unaligned(&rqsx->cdl));
  4485. + return NULL;
  4486. + }
  4487. +
  4488. + return rqsx;
  4489. +}
  4490. +
  4491. +static void gsb_rqsx_response_error(struct gsb_buffer *gsb, int status)
  4492. +{
  4493. + gsb->status = 0x00;
  4494. + gsb->len = 0x02;
  4495. + gsb->data.out.status = (u8)(-status);
  4496. + gsb->data.out.len = 0x00;
  4497. +}
  4498. +
  4499. +static void gsb_rqsx_response_success(struct gsb_buffer *gsb, u8 *ptr, size_t len)
  4500. +{
  4501. + gsb->status = 0x00;
  4502. + gsb->len = len + 2;
  4503. + gsb->data.out.status = 0x00;
  4504. + gsb->data.out.len = len;
  4505. +
  4506. + if (len)
  4507. + memcpy(&gsb->data.out.pld[0], ptr, len);
  4508. +}
  4509. +
  4510. +static acpi_status san_rqst_fixup_suspended(struct san_data *d,
  4511. + struct ssam_request *rqst,
  4512. + struct gsb_buffer *gsb)
  4513. +{
  4514. + if (rqst->target_category == SSAM_SSH_TC_BAS && rqst->command_id == 0x0D) {
  4515. + u8 base_state = 1;
  4516. +
  4517. + /* Base state quirk:
  4518. + * The base state may be queried from ACPI when the EC is still
  4519. + * suspended. In this case it will return '-EPERM'. This query
  4520. + * will only be triggered from the ACPI lid GPE interrupt, thus
  4521. + * we are either in laptop or studio mode (base status 0x01 or
  4522. + * 0x02). Furthermore, we will only get here if the device (and
  4523. + * EC) have been suspended.
  4524. + *
  4525. + * We now assume that the device is in laptop mode (0x01). This
  4526. + * has the drawback that it will wake the device when unfolding
  4527. + * it in studio mode, but it also allows us to avoid actively
  4528. + * waiting for the EC to wake up, which may incur a notable
  4529. + * delay.
  4530. + */
  4531. +
  4532. + dev_dbg(d->dev, "rqst: fixup: base-state quirk\n");
  4533. +
  4534. + gsb_rqsx_response_success(gsb, &base_state, sizeof(base_state));
  4535. + return AE_OK;
  4536. + }
  4537. +
  4538. + gsb_rqsx_response_error(gsb, -ENXIO);
  4539. + return AE_OK;
  4540. +}
  4541. +
  4542. +static acpi_status san_rqst(struct san_data *d, struct gsb_buffer *buffer)
  4543. +{
  4544. + u8 rspbuf[SAN_GSB_MAX_RESPONSE];
  4545. + struct gsb_data_rqsx *gsb_rqst;
  4546. + struct ssam_request rqst;
  4547. + struct ssam_response rsp;
  4548. + int status = 0;
  4549. +
  4550. + gsb_rqst = san_validate_rqsx(d->dev, "RQST", buffer);
  4551. + if (!gsb_rqst)
  4552. + return AE_OK;
  4553. +
  4554. + rqst.target_category = gsb_rqst->tc;
  4555. + rqst.target_id = gsb_rqst->tid;
  4556. + rqst.command_id = gsb_rqst->cid;
  4557. + rqst.instance_id = gsb_rqst->iid;
  4558. + rqst.flags = gsb_rqst->snc ? SSAM_REQUEST_HAS_RESPONSE : 0;
  4559. + rqst.length = get_unaligned(&gsb_rqst->cdl);
  4560. + rqst.payload = &gsb_rqst->pld[0];
  4561. +
  4562. + rsp.capacity = ARRAY_SIZE(rspbuf);
  4563. + rsp.length = 0;
  4564. + rsp.pointer = &rspbuf[0];
  4565. +
  4566. + /* Handle suspended device. */
  4567. + if (d->dev->power.is_suspended) {
  4568. + dev_warn(d->dev, "rqst: device is suspended, not executing\n");
  4569. + return san_rqst_fixup_suspended(d, &rqst, buffer);
  4570. + }
  4571. +
  4572. + status = __ssam_retry(ssam_request_sync_onstack, SAN_REQUEST_NUM_TRIES,
  4573. + d->ctrl, &rqst, &rsp, SAN_GSB_MAX_RQSX_PAYLOAD);
  4574. +
  4575. + if (!status) {
  4576. + gsb_rqsx_response_success(buffer, rsp.pointer, rsp.length);
  4577. + } else {
  4578. + dev_err(d->dev, "rqst: failed with error %d\n", status);
  4579. + gsb_rqsx_response_error(buffer, status);
  4580. + }
  4581. +
  4582. + return AE_OK;
  4583. +}
  4584. +
  4585. +static acpi_status san_rqsg(struct san_data *d, struct gsb_buffer *buffer)
  4586. +{
  4587. + struct gsb_data_rqsx *gsb_rqsg;
  4588. + struct san_dgpu_event evt;
  4589. + int status;
  4590. +
  4591. + gsb_rqsg = san_validate_rqsx(d->dev, "RQSG", buffer);
  4592. + if (!gsb_rqsg)
  4593. + return AE_OK;
  4594. +
  4595. + evt.category = gsb_rqsg->tc;
  4596. + evt.target = gsb_rqsg->tid;
  4597. + evt.command = gsb_rqsg->cid;
  4598. + evt.instance = gsb_rqsg->iid;
  4599. + evt.length = get_unaligned(&gsb_rqsg->cdl);
  4600. + evt.payload = &gsb_rqsg->pld[0];
  4601. +
  4602. + status = san_dgpu_notifier_call(&evt);
  4603. + if (!status) {
  4604. + gsb_rqsx_response_success(buffer, NULL, 0);
  4605. + } else {
  4606. + dev_err(d->dev, "rqsg: failed with error %d\n", status);
  4607. + gsb_rqsx_response_error(buffer, status);
  4608. + }
  4609. +
  4610. + return AE_OK;
  4611. +}
  4612. +
  4613. +static acpi_status san_opreg_handler(u32 function, acpi_physical_address command,
  4614. + u32 bits, u64 *value64, void *opreg_context,
  4615. + void *region_context)
  4616. +{
  4617. + struct san_data *d = to_san_data(opreg_context, info);
  4618. + struct gsb_buffer *buffer = (struct gsb_buffer *)value64;
  4619. + int accessor_type = (function & 0xFFFF0000) >> 16;
  4620. +
  4621. + if (command != SAN_GSB_COMMAND) {
  4622. + dev_warn(d->dev, "unsupported command: %#04llx\n", command);
  4623. + return AE_OK;
  4624. + }
  4625. +
  4626. + if (accessor_type != ACPI_GSB_ACCESS_ATTRIB_RAW_PROCESS) {
  4627. + dev_err(d->dev, "invalid access type: %#04x\n", accessor_type);
  4628. + return AE_OK;
  4629. + }
  4630. +
  4631. + /* Buffer must have at least contain the command-value. */
  4632. + if (buffer->len == 0) {
  4633. + dev_err(d->dev, "request-package too small\n");
  4634. + return AE_OK;
  4635. + }
  4636. +
  4637. + switch (buffer->data.in.cv) {
  4638. + case SAN_GSB_REQUEST_CV_RQST:
  4639. + return san_rqst(d, buffer);
  4640. +
  4641. + case SAN_GSB_REQUEST_CV_ETWL:
  4642. + return san_etwl(d, buffer);
  4643. +
  4644. + case SAN_GSB_REQUEST_CV_RQSG:
  4645. + return san_rqsg(d, buffer);
  4646. +
  4647. + default:
  4648. + dev_warn(d->dev, "unsupported SAN0 request (cv: %#04x)\n",
  4649. + buffer->data.in.cv);
  4650. + return AE_OK;
  4651. + }
  4652. +}
  4653. +
  4654. +
  4655. +/* -- Driver setup. --------------------------------------------------------- */
  4656. +
  4657. +static int san_events_register(struct platform_device *pdev)
  4658. +{
  4659. + struct san_data *d = platform_get_drvdata(pdev);
  4660. + int status;
  4661. +
  4662. + d->nf_bat.base.priority = 1;
  4663. + d->nf_bat.base.fn = san_evt_bat_nf;
  4664. + d->nf_bat.event.reg = SSAM_EVENT_REGISTRY_SAM;
  4665. + d->nf_bat.event.id.target_category = SSAM_SSH_TC_BAT;
  4666. + d->nf_bat.event.id.instance = 0;
  4667. + d->nf_bat.event.mask = SSAM_EVENT_MASK_TARGET;
  4668. + d->nf_bat.event.flags = SSAM_EVENT_SEQUENCED;
  4669. +
  4670. + d->nf_tmp.base.priority = 1;
  4671. + d->nf_tmp.base.fn = san_evt_tmp_nf;
  4672. + d->nf_tmp.event.reg = SSAM_EVENT_REGISTRY_SAM;
  4673. + d->nf_tmp.event.id.target_category = SSAM_SSH_TC_TMP;
  4674. + d->nf_tmp.event.id.instance = 0;
  4675. + d->nf_tmp.event.mask = SSAM_EVENT_MASK_TARGET;
  4676. + d->nf_tmp.event.flags = SSAM_EVENT_SEQUENCED;
  4677. +
  4678. + status = ssam_notifier_register(d->ctrl, &d->nf_bat);
  4679. + if (status)
  4680. + return status;
  4681. +
  4682. + status = ssam_notifier_register(d->ctrl, &d->nf_tmp);
  4683. + if (status)
  4684. + ssam_notifier_unregister(d->ctrl, &d->nf_bat);
  4685. +
  4686. + return status;
  4687. +}
  4688. +
  4689. +static void san_events_unregister(struct platform_device *pdev)
  4690. +{
  4691. + struct san_data *d = platform_get_drvdata(pdev);
  4692. +
  4693. + ssam_notifier_unregister(d->ctrl, &d->nf_bat);
  4694. + ssam_notifier_unregister(d->ctrl, &d->nf_tmp);
  4695. +}
  4696. +
  4697. +#define san_consumer_printk(level, dev, handle, fmt, ...) \
  4698. +do { \
  4699. + char *path = "<error getting consumer path>"; \
  4700. + struct acpi_buffer buffer = { \
  4701. + .length = ACPI_ALLOCATE_BUFFER, \
  4702. + .pointer = NULL, \
  4703. + }; \
  4704. + \
  4705. + if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer))) \
  4706. + path = buffer.pointer; \
  4707. + \
  4708. + dev_##level(dev, "[%s]: " fmt, path, ##__VA_ARGS__); \
  4709. + kfree(buffer.pointer); \
  4710. +} while (0)
  4711. +
  4712. +#define san_consumer_dbg(dev, handle, fmt, ...) \
  4713. + san_consumer_printk(dbg, dev, handle, fmt, ##__VA_ARGS__)
  4714. +
  4715. +#define san_consumer_warn(dev, handle, fmt, ...) \
  4716. + san_consumer_printk(warn, dev, handle, fmt, ##__VA_ARGS__)
  4717. +
  4718. +static bool is_san_consumer(struct platform_device *pdev, acpi_handle handle)
  4719. +{
  4720. + struct acpi_handle_list dep_devices;
  4721. + acpi_handle supplier = ACPI_HANDLE(&pdev->dev);
  4722. + acpi_status status;
  4723. + int i;
  4724. +
  4725. + if (!acpi_has_method(handle, "_DEP"))
  4726. + return false;
  4727. +
  4728. + status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
  4729. + if (ACPI_FAILURE(status)) {
  4730. + san_consumer_dbg(&pdev->dev, handle, "failed to evaluate _DEP\n");
  4731. + return false;
  4732. + }
  4733. +
  4734. + for (i = 0; i < dep_devices.count; i++) {
  4735. + if (dep_devices.handles[i] == supplier)
  4736. + return true;
  4737. + }
  4738. +
  4739. + return false;
  4740. +}
  4741. +
  4742. +static acpi_status san_consumer_setup(acpi_handle handle, u32 lvl,
  4743. + void *context, void **rv)
  4744. +{
  4745. + const u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_SUPPLIER;
  4746. + struct platform_device *pdev = context;
  4747. + struct acpi_device *adev;
  4748. + struct device_link *link;
  4749. +
  4750. + if (!is_san_consumer(pdev, handle))
  4751. + return AE_OK;
  4752. +
  4753. + /* Ignore ACPI devices that are not present. */
  4754. + if (acpi_bus_get_device(handle, &adev) != 0)
  4755. + return AE_OK;
  4756. +
  4757. + san_consumer_dbg(&pdev->dev, handle, "creating device link\n");
  4758. +
  4759. + /* Try to set up device links, ignore but log errors. */
  4760. + link = device_link_add(&adev->dev, &pdev->dev, flags);
  4761. + if (!link) {
  4762. + san_consumer_warn(&pdev->dev, handle, "failed to create device link\n");
  4763. + return AE_OK;
  4764. + }
  4765. +
  4766. + return AE_OK;
  4767. +}
  4768. +
  4769. +static int san_consumer_links_setup(struct platform_device *pdev)
  4770. +{
  4771. + acpi_status status;
  4772. +
  4773. + status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  4774. + ACPI_UINT32_MAX, san_consumer_setup, NULL,
  4775. + pdev, NULL);
  4776. +
  4777. + return status ? -EFAULT : 0;
  4778. +}
  4779. +
  4780. +static int san_probe(struct platform_device *pdev)
  4781. +{
  4782. + acpi_handle san = ACPI_HANDLE(&pdev->dev);
  4783. + struct ssam_controller *ctrl;
  4784. + struct san_data *data;
  4785. + acpi_status astatus;
  4786. + int status;
  4787. +
  4788. + ctrl = ssam_client_bind(&pdev->dev);
  4789. + if (IS_ERR(ctrl))
  4790. + return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
  4791. +
  4792. + status = san_consumer_links_setup(pdev);
  4793. + if (status)
  4794. + return status;
  4795. +
  4796. + data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  4797. + if (!data)
  4798. + return -ENOMEM;
  4799. +
  4800. + data->dev = &pdev->dev;
  4801. + data->ctrl = ctrl;
  4802. +
  4803. + platform_set_drvdata(pdev, data);
  4804. +
  4805. + astatus = acpi_install_address_space_handler(san, ACPI_ADR_SPACE_GSBUS,
  4806. + &san_opreg_handler, NULL,
  4807. + &data->info);
  4808. + if (ACPI_FAILURE(astatus))
  4809. + return -ENXIO;
  4810. +
  4811. + status = san_events_register(pdev);
  4812. + if (status)
  4813. + goto err_enable_events;
  4814. +
  4815. + status = san_set_rqsg_interface_device(&pdev->dev);
  4816. + if (status)
  4817. + goto err_install_dev;
  4818. +
  4819. + acpi_walk_dep_device_list(san);
  4820. + return 0;
  4821. +
  4822. +err_install_dev:
  4823. + san_events_unregister(pdev);
  4824. +err_enable_events:
  4825. + acpi_remove_address_space_handler(san, ACPI_ADR_SPACE_GSBUS,
  4826. + &san_opreg_handler);
  4827. + return status;
  4828. +}
  4829. +
  4830. +static int san_remove(struct platform_device *pdev)
  4831. +{
  4832. + acpi_handle san = ACPI_HANDLE(&pdev->dev);
  4833. +
  4834. + san_set_rqsg_interface_device(NULL);
  4835. + acpi_remove_address_space_handler(san, ACPI_ADR_SPACE_GSBUS,
  4836. + &san_opreg_handler);
  4837. + san_events_unregister(pdev);
  4838. +
  4839. + /*
  4840. + * We have unregistered our event sources. Now we need to ensure that
  4841. + * all delayed works they may have spawned are run to completion.
  4842. + */
  4843. + flush_scheduled_work();
  4844. +
  4845. + return 0;
  4846. +}
  4847. +
  4848. +static const struct acpi_device_id san_match[] = {
  4849. + { "MSHW0091" },
  4850. + { },
  4851. +};
  4852. +MODULE_DEVICE_TABLE(acpi, san_match);
  4853. +
  4854. +static struct platform_driver surface_acpi_notify = {
  4855. + .probe = san_probe,
  4856. + .remove = san_remove,
  4857. + .driver = {
  4858. + .name = "surface_acpi_notify",
  4859. + .acpi_match_table = san_match,
  4860. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  4861. + },
  4862. +};
  4863. +module_platform_driver(surface_acpi_notify);
  4864. +
  4865. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  4866. +MODULE_DESCRIPTION("Surface ACPI Notify driver for Surface System Aggregator Module");
  4867. +MODULE_LICENSE("GPL");
  4868. diff --git a/drivers/platform/x86/surface_aggregator/Kconfig b/drivers/platform/x86/surface_aggregator/Kconfig
  4869. new file mode 100644
  4870. index 000000000000..cab020324256
  4871. --- /dev/null
  4872. +++ b/drivers/platform/x86/surface_aggregator/Kconfig
  4873. @@ -0,0 +1,69 @@
  4874. +# SPDX-License-Identifier: GPL-2.0+
  4875. +# Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  4876. +
  4877. +menuconfig SURFACE_AGGREGATOR
  4878. + tristate "Microsoft Surface System Aggregator Module Subsystem and Drivers"
  4879. + depends on SERIAL_DEV_BUS
  4880. + depends on ACPI
  4881. + select CRC_CCITT
  4882. + help
  4883. + The Surface System Aggregator Module (Surface SAM or SSAM) is an
  4884. + embedded controller (EC) found on 5th- and later-generation Microsoft
  4885. + Surface devices (i.e. Surface Pro 5, Surface Book 2, Surface Laptop,
  4886. + and newer, with exception of Surface Go series devices).
  4887. +
  4888. + Depending on the device in question, this EC provides varying
  4889. + functionality, including:
  4890. + - EC access from ACPI via Surface ACPI Notify (5th- and 6th-generation)
  4891. + - battery status information (all devices)
  4892. + - thermal sensor access (all devices)
  4893. + - performance mode / cooling mode control (all devices)
  4894. + - clipboard detachment system control (Surface Book 2 and 3)
  4895. + - HID / keyboard input (Surface Laptops, Surface Book 3)
  4896. +
  4897. + This option controls whether the Surface SAM subsystem core will be
  4898. + built. This includes a driver for the Surface Serial Hub (SSH), which
  4899. + is the device responsible for the communication with the EC, and a
  4900. + basic kernel interface exposing the EC functionality to other client
  4901. + drivers, i.e. allowing them to make requests to the EC and receive
  4902. + events from it. Selecting this option alone will not provide any
  4903. + client drivers and therefore no functionality beyond the in-kernel
  4904. + interface. Said functionality is the responsibility of the respective
  4905. + client drivers.
  4906. +
  4907. + Note: While 4th-generation Surface devices also make use of a SAM EC,
  4908. + due to a difference in the communication interface of the controller,
  4909. + only 5th and later generations are currently supported. Specifically,
  4910. + devices using SAM-over-SSH are supported, whereas devices using
  4911. + SAM-over-HID, which is used on the 4th generation, are currently not
  4912. + supported.
  4913. +
  4914. + Choose m if you want to build the SAM subsystem core and SSH driver as
  4915. + module, y if you want to build it into the kernel and n if you don't
  4916. + want it at all.
  4917. +
  4918. +config SURFACE_AGGREGATOR_BUS
  4919. + bool "Surface System Aggregator Module Bus"
  4920. + depends on SURFACE_AGGREGATOR
  4921. + default y
  4922. + help
  4923. + Expands the Surface System Aggregator Module (SSAM) core driver by
  4924. + providing a dedicated bus and client-device type.
  4925. +
  4926. + This bus and device type are intended to provide and simplify support
  4927. + for non-platform and non-ACPI SSAM devices, i.e. SSAM devices that are
  4928. + not auto-detectable via the conventional means (e.g. ACPI).
  4929. +
  4930. +config SURFACE_AGGREGATOR_ERROR_INJECTION
  4931. + bool "Surface System Aggregator Module Error Injection Capabilities"
  4932. + depends on SURFACE_AGGREGATOR
  4933. + depends on FUNCTION_ERROR_INJECTION
  4934. + help
  4935. + Provides error-injection capabilities for the Surface System
  4936. + Aggregator Module subsystem and Surface Serial Hub driver.
  4937. +
  4938. + Specifically, exports error injection hooks to be used with the
  4939. + kernel's function error injection capabilities to simulate underlying
  4940. + transport and communication problems, such as invalid data sent to or
  4941. + received from the EC, dropped data, and communication timeouts.
  4942. + Intended for development and debugging.
  4943. diff --git a/drivers/platform/x86/surface_aggregator/Makefile b/drivers/platform/x86/surface_aggregator/Makefile
  4944. new file mode 100644
  4945. index 000000000000..c8498c41e758
  4946. --- /dev/null
  4947. +++ b/drivers/platform/x86/surface_aggregator/Makefile
  4948. @@ -0,0 +1,17 @@
  4949. +# SPDX-License-Identifier: GPL-2.0+
  4950. +# Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  4951. +
  4952. +# For include/trace/define_trace.h to include trace.h
  4953. +CFLAGS_core.o = -I$(src)
  4954. +
  4955. +obj-$(CONFIG_SURFACE_AGGREGATOR) += surface_aggregator.o
  4956. +
  4957. +surface_aggregator-objs := core.o
  4958. +surface_aggregator-objs += ssh_parser.o
  4959. +surface_aggregator-objs += ssh_packet_layer.o
  4960. +surface_aggregator-objs += ssh_request_layer.o
  4961. +surface_aggregator-objs += controller.o
  4962. +
  4963. +ifeq ($(CONFIG_SURFACE_AGGREGATOR_BUS),y)
  4964. +surface_aggregator-objs += bus.o
  4965. +endif
  4966. diff --git a/drivers/platform/x86/surface_aggregator/bus.c b/drivers/platform/x86/surface_aggregator/bus.c
  4967. new file mode 100644
  4968. index 000000000000..0169677c243e
  4969. --- /dev/null
  4970. +++ b/drivers/platform/x86/surface_aggregator/bus.c
  4971. @@ -0,0 +1,415 @@
  4972. +// SPDX-License-Identifier: GPL-2.0+
  4973. +/*
  4974. + * Surface System Aggregator Module bus and device integration.
  4975. + *
  4976. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  4977. + */
  4978. +
  4979. +#include <linux/device.h>
  4980. +#include <linux/slab.h>
  4981. +
  4982. +#include <linux/surface_aggregator/controller.h>
  4983. +#include <linux/surface_aggregator/device.h>
  4984. +
  4985. +#include "bus.h"
  4986. +#include "controller.h"
  4987. +
  4988. +static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  4989. + char *buf)
  4990. +{
  4991. + struct ssam_device *sdev = to_ssam_device(dev);
  4992. +
  4993. + return sysfs_emit(buf, "ssam:d%02Xc%02Xt%02Xi%02Xf%02X\n",
  4994. + sdev->uid.domain, sdev->uid.category, sdev->uid.target,
  4995. + sdev->uid.instance, sdev->uid.function);
  4996. +}
  4997. +static DEVICE_ATTR_RO(modalias);
  4998. +
  4999. +static struct attribute *ssam_device_attrs[] = {
  5000. + &dev_attr_modalias.attr,
  5001. + NULL,
  5002. +};
  5003. +ATTRIBUTE_GROUPS(ssam_device);
  5004. +
  5005. +static int ssam_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  5006. +{
  5007. + struct ssam_device *sdev = to_ssam_device(dev);
  5008. +
  5009. + return add_uevent_var(env, "MODALIAS=ssam:d%02Xc%02Xt%02Xi%02Xf%02X",
  5010. + sdev->uid.domain, sdev->uid.category,
  5011. + sdev->uid.target, sdev->uid.instance,
  5012. + sdev->uid.function);
  5013. +}
  5014. +
  5015. +static void ssam_device_release(struct device *dev)
  5016. +{
  5017. + struct ssam_device *sdev = to_ssam_device(dev);
  5018. +
  5019. + ssam_controller_put(sdev->ctrl);
  5020. + kfree(sdev);
  5021. +}
  5022. +
  5023. +const struct device_type ssam_device_type = {
  5024. + .name = "surface_aggregator_device",
  5025. + .groups = ssam_device_groups,
  5026. + .uevent = ssam_device_uevent,
  5027. + .release = ssam_device_release,
  5028. +};
  5029. +EXPORT_SYMBOL_GPL(ssam_device_type);
  5030. +
  5031. +/**
  5032. + * ssam_device_alloc() - Allocate and initialize a SSAM client device.
  5033. + * @ctrl: The controller under which the device should be added.
  5034. + * @uid: The UID of the device to be added.
  5035. + *
  5036. + * Allocates and initializes a new client device. The parent of the device
  5037. + * will be set to the controller device and the name will be set based on the
  5038. + * UID. Note that the device still has to be added via ssam_device_add().
  5039. + * Refer to that function for more details.
  5040. + *
  5041. + * Return: Returns the newly allocated and initialized SSAM client device, or
  5042. + * %NULL if it could not be allocated.
  5043. + */
  5044. +struct ssam_device *ssam_device_alloc(struct ssam_controller *ctrl,
  5045. + struct ssam_device_uid uid)
  5046. +{
  5047. + struct ssam_device *sdev;
  5048. +
  5049. + sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  5050. + if (!sdev)
  5051. + return NULL;
  5052. +
  5053. + device_initialize(&sdev->dev);
  5054. + sdev->dev.bus = &ssam_bus_type;
  5055. + sdev->dev.type = &ssam_device_type;
  5056. + sdev->dev.parent = ssam_controller_device(ctrl);
  5057. + sdev->ctrl = ssam_controller_get(ctrl);
  5058. + sdev->uid = uid;
  5059. +
  5060. + dev_set_name(&sdev->dev, "%02x:%02x:%02x:%02x:%02x",
  5061. + sdev->uid.domain, sdev->uid.category, sdev->uid.target,
  5062. + sdev->uid.instance, sdev->uid.function);
  5063. +
  5064. + return sdev;
  5065. +}
  5066. +EXPORT_SYMBOL_GPL(ssam_device_alloc);
  5067. +
  5068. +/**
  5069. + * ssam_device_add() - Add a SSAM client device.
  5070. + * @sdev: The SSAM client device to be added.
  5071. + *
  5072. + * Added client devices must be guaranteed to always have a valid and active
  5073. + * controller. Thus, this function will fail with %-ENODEV if the controller
  5074. + * of the device has not been initialized yet, has been suspended, or has been
  5075. + * shut down.
  5076. + *
  5077. + * The caller of this function should ensure that the corresponding call to
  5078. + * ssam_device_remove() is issued before the controller is shut down. If the
  5079. + * added device is a direct child of the controller device (default), it will
  5080. + * be automatically removed when the controller is shut down.
  5081. + *
  5082. + * By default, the controller device will become the parent of the newly
  5083. + * created client device. The parent may be changed before ssam_device_add is
  5084. + * called, but care must be taken that a) the correct suspend/resume ordering
  5085. + * is guaranteed and b) the client device does not outlive the controller,
  5086. + * i.e. that the device is removed before the controller is being shut down.
  5087. + * In case these guarantees have to be manually enforced, please refer to the
  5088. + * ssam_client_link() and ssam_client_bind() functions, which are intended to
  5089. + * set up device-links for this purpose.
  5090. + *
  5091. + * Return: Returns zero on success, a negative error code on failure.
  5092. + */
  5093. +int ssam_device_add(struct ssam_device *sdev)
  5094. +{
  5095. + int status;
  5096. +
  5097. + /*
  5098. + * Ensure that we can only add new devices to a controller if it has
  5099. + * been started and is not going away soon. This works in combination
  5100. + * with ssam_controller_remove_clients to ensure driver presence for the
  5101. + * controller device, i.e. it ensures that the controller (sdev->ctrl)
  5102. + * is always valid and can be used for requests as long as the client
  5103. + * device we add here is registered as child under it. This essentially
  5104. + * guarantees that the client driver can always expect the preconditions
  5105. + * for functions like ssam_request_sync (controller has to be started
  5106. + * and is not suspended) to hold and thus does not have to check for
  5107. + * them.
  5108. + *
  5109. + * Note that for this to work, the controller has to be a parent device.
  5110. + * If it is not a direct parent, care has to be taken that the device is
  5111. + * removed via ssam_device_remove(), as device_unregister does not
  5112. + * remove child devices recursively.
  5113. + */
  5114. + ssam_controller_statelock(sdev->ctrl);
  5115. +
  5116. + if (sdev->ctrl->state != SSAM_CONTROLLER_STARTED) {
  5117. + ssam_controller_stateunlock(sdev->ctrl);
  5118. + return -ENODEV;
  5119. + }
  5120. +
  5121. + status = device_add(&sdev->dev);
  5122. +
  5123. + ssam_controller_stateunlock(sdev->ctrl);
  5124. + return status;
  5125. +}
  5126. +EXPORT_SYMBOL_GPL(ssam_device_add);
  5127. +
  5128. +/**
  5129. + * ssam_device_remove() - Remove a SSAM client device.
  5130. + * @sdev: The device to remove.
  5131. + *
  5132. + * Removes and unregisters the provided SSAM client device.
  5133. + */
  5134. +void ssam_device_remove(struct ssam_device *sdev)
  5135. +{
  5136. + device_unregister(&sdev->dev);
  5137. +}
  5138. +EXPORT_SYMBOL_GPL(ssam_device_remove);
  5139. +
  5140. +/**
  5141. + * ssam_device_id_compatible() - Check if a device ID matches a UID.
  5142. + * @id: The device ID as potential match.
  5143. + * @uid: The device UID matching against.
  5144. + *
  5145. + * Check if the given ID is a match for the given UID, i.e. if a device with
  5146. + * the provided UID is compatible to the given ID following the match rules
  5147. + * described in its &ssam_device_id.match_flags member.
  5148. + *
  5149. + * Return: Returns %true if the given UID is compatible to the match rule
  5150. + * described by the given ID, %false otherwise.
  5151. + */
  5152. +static bool ssam_device_id_compatible(const struct ssam_device_id *id,
  5153. + struct ssam_device_uid uid)
  5154. +{
  5155. + if (id->domain != uid.domain || id->category != uid.category)
  5156. + return false;
  5157. +
  5158. + if ((id->match_flags & SSAM_MATCH_TARGET) && id->target != uid.target)
  5159. + return false;
  5160. +
  5161. + if ((id->match_flags & SSAM_MATCH_INSTANCE) && id->instance != uid.instance)
  5162. + return false;
  5163. +
  5164. + if ((id->match_flags & SSAM_MATCH_FUNCTION) && id->function != uid.function)
  5165. + return false;
  5166. +
  5167. + return true;
  5168. +}
  5169. +
  5170. +/**
  5171. + * ssam_device_id_is_null() - Check if a device ID is null.
  5172. + * @id: The device ID to check.
  5173. + *
  5174. + * Check if a given device ID is null, i.e. all zeros. Used to check for the
  5175. + * end of ``MODULE_DEVICE_TABLE(ssam, ...)`` or similar lists.
  5176. + *
  5177. + * Return: Returns %true if the given ID represents a null ID, %false
  5178. + * otherwise.
  5179. + */
  5180. +static bool ssam_device_id_is_null(const struct ssam_device_id *id)
  5181. +{
  5182. + return id->match_flags == 0 &&
  5183. + id->domain == 0 &&
  5184. + id->category == 0 &&
  5185. + id->target == 0 &&
  5186. + id->instance == 0 &&
  5187. + id->function == 0 &&
  5188. + id->driver_data == 0;
  5189. +}
  5190. +
  5191. +/**
  5192. + * ssam_device_id_match() - Find the matching ID table entry for the given UID.
  5193. + * @table: The table to search in.
  5194. + * @uid: The UID to matched against the individual table entries.
  5195. + *
  5196. + * Find the first match for the provided device UID in the provided ID table
  5197. + * and return it. Returns %NULL if no match could be found.
  5198. + */
  5199. +const struct ssam_device_id *ssam_device_id_match(const struct ssam_device_id *table,
  5200. + const struct ssam_device_uid uid)
  5201. +{
  5202. + const struct ssam_device_id *id;
  5203. +
  5204. + for (id = table; !ssam_device_id_is_null(id); ++id)
  5205. + if (ssam_device_id_compatible(id, uid))
  5206. + return id;
  5207. +
  5208. + return NULL;
  5209. +}
  5210. +EXPORT_SYMBOL_GPL(ssam_device_id_match);
  5211. +
  5212. +/**
  5213. + * ssam_device_get_match() - Find and return the ID matching the device in the
  5214. + * ID table of the bound driver.
  5215. + * @dev: The device for which to get the matching ID table entry.
  5216. + *
  5217. + * Find the fist match for the UID of the device in the ID table of the
  5218. + * currently bound driver and return it. Returns %NULL if the device does not
  5219. + * have a driver bound to it, the driver does not have match_table (i.e. it is
  5220. + * %NULL), or there is no match in the driver's match_table.
  5221. + *
  5222. + * This function essentially calls ssam_device_id_match() with the ID table of
  5223. + * the bound device driver and the UID of the device.
  5224. + *
  5225. + * Return: Returns the first match for the UID of the device in the device
  5226. + * driver's match table, or %NULL if no such match could be found.
  5227. + */
  5228. +const struct ssam_device_id *ssam_device_get_match(const struct ssam_device *dev)
  5229. +{
  5230. + const struct ssam_device_driver *sdrv;
  5231. +
  5232. + sdrv = to_ssam_device_driver(dev->dev.driver);
  5233. + if (!sdrv)
  5234. + return NULL;
  5235. +
  5236. + if (!sdrv->match_table)
  5237. + return NULL;
  5238. +
  5239. + return ssam_device_id_match(sdrv->match_table, dev->uid);
  5240. +}
  5241. +EXPORT_SYMBOL_GPL(ssam_device_get_match);
  5242. +
  5243. +/**
  5244. + * ssam_device_get_match_data() - Find the ID matching the device in the
  5245. + * ID table of the bound driver and return its ``driver_data`` member.
  5246. + * @dev: The device for which to get the match data.
  5247. + *
  5248. + * Find the fist match for the UID of the device in the ID table of the
  5249. + * corresponding driver and return its driver_data. Returns %NULL if the
  5250. + * device does not have a driver bound to it, the driver does not have
  5251. + * match_table (i.e. it is %NULL), there is no match in the driver's
  5252. + * match_table, or the match does not have any driver_data.
  5253. + *
  5254. + * This function essentially calls ssam_device_get_match() and, if any match
  5255. + * could be found, returns its ``struct ssam_device_id.driver_data`` member.
  5256. + *
  5257. + * Return: Returns the driver data associated with the first match for the UID
  5258. + * of the device in the device driver's match table, or %NULL if no such match
  5259. + * could be found.
  5260. + */
  5261. +const void *ssam_device_get_match_data(const struct ssam_device *dev)
  5262. +{
  5263. + const struct ssam_device_id *id;
  5264. +
  5265. + id = ssam_device_get_match(dev);
  5266. + if (!id)
  5267. + return NULL;
  5268. +
  5269. + return (const void *)id->driver_data;
  5270. +}
  5271. +EXPORT_SYMBOL_GPL(ssam_device_get_match_data);
  5272. +
  5273. +static int ssam_bus_match(struct device *dev, struct device_driver *drv)
  5274. +{
  5275. + struct ssam_device_driver *sdrv = to_ssam_device_driver(drv);
  5276. + struct ssam_device *sdev = to_ssam_device(dev);
  5277. +
  5278. + if (!is_ssam_device(dev))
  5279. + return 0;
  5280. +
  5281. + return !!ssam_device_id_match(sdrv->match_table, sdev->uid);
  5282. +}
  5283. +
  5284. +static int ssam_bus_probe(struct device *dev)
  5285. +{
  5286. + return to_ssam_device_driver(dev->driver)
  5287. + ->probe(to_ssam_device(dev));
  5288. +}
  5289. +
  5290. +static int ssam_bus_remove(struct device *dev)
  5291. +{
  5292. + struct ssam_device_driver *sdrv = to_ssam_device_driver(dev->driver);
  5293. +
  5294. + if (sdrv->remove)
  5295. + sdrv->remove(to_ssam_device(dev));
  5296. +
  5297. + return 0;
  5298. +}
  5299. +
  5300. +struct bus_type ssam_bus_type = {
  5301. + .name = "surface_aggregator",
  5302. + .match = ssam_bus_match,
  5303. + .probe = ssam_bus_probe,
  5304. + .remove = ssam_bus_remove,
  5305. +};
  5306. +EXPORT_SYMBOL_GPL(ssam_bus_type);
  5307. +
  5308. +/**
  5309. + * __ssam_device_driver_register() - Register a SSAM client device driver.
  5310. + * @sdrv: The driver to register.
  5311. + * @owner: The module owning the provided driver.
  5312. + *
  5313. + * Please refer to the ssam_device_driver_register() macro for the normal way
  5314. + * to register a driver from inside its owning module.
  5315. + */
  5316. +int __ssam_device_driver_register(struct ssam_device_driver *sdrv,
  5317. + struct module *owner)
  5318. +{
  5319. + sdrv->driver.owner = owner;
  5320. + sdrv->driver.bus = &ssam_bus_type;
  5321. +
  5322. + /* force drivers to async probe so I/O is possible in probe */
  5323. + sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
  5324. +
  5325. + return driver_register(&sdrv->driver);
  5326. +}
  5327. +EXPORT_SYMBOL_GPL(__ssam_device_driver_register);
  5328. +
  5329. +/**
  5330. + * ssam_device_driver_unregister - Unregister a SSAM device driver.
  5331. + * @sdrv: The driver to unregister.
  5332. + */
  5333. +void ssam_device_driver_unregister(struct ssam_device_driver *sdrv)
  5334. +{
  5335. + driver_unregister(&sdrv->driver);
  5336. +}
  5337. +EXPORT_SYMBOL_GPL(ssam_device_driver_unregister);
  5338. +
  5339. +static int ssam_remove_device(struct device *dev, void *_data)
  5340. +{
  5341. + struct ssam_device *sdev = to_ssam_device(dev);
  5342. +
  5343. + if (is_ssam_device(dev))
  5344. + ssam_device_remove(sdev);
  5345. +
  5346. + return 0;
  5347. +}
  5348. +
  5349. +/**
  5350. + * ssam_controller_remove_clients() - Remove SSAM client devices registered as
  5351. + * direct children under the given controller.
  5352. + * @ctrl: The controller to remove all direct clients for.
  5353. + *
  5354. + * Remove all SSAM client devices registered as direct children under the
  5355. + * given controller. Note that this only accounts for direct children of the
  5356. + * controller device. This does not take care of any client devices where the
  5357. + * parent device has been manually set before calling ssam_device_add. Refer
  5358. + * to ssam_device_add()/ssam_device_remove() for more details on those cases.
  5359. + *
  5360. + * To avoid new devices being added in parallel to this call, the main
  5361. + * controller lock (not statelock) must be held during this (and if
  5362. + * necessary, any subsequent deinitialization) call.
  5363. + */
  5364. +void ssam_controller_remove_clients(struct ssam_controller *ctrl)
  5365. +{
  5366. + struct device *dev;
  5367. +
  5368. + dev = ssam_controller_device(ctrl);
  5369. + device_for_each_child_reverse(dev, NULL, ssam_remove_device);
  5370. +}
  5371. +
  5372. +/**
  5373. + * ssam_bus_register() - Register and set-up the SSAM client device bus.
  5374. + */
  5375. +int ssam_bus_register(void)
  5376. +{
  5377. + return bus_register(&ssam_bus_type);
  5378. +}
  5379. +
  5380. +/**
  5381. + * ssam_bus_unregister() - Unregister the SSAM client device bus.
  5382. + */
  5383. +void ssam_bus_unregister(void)
  5384. +{
  5385. + return bus_unregister(&ssam_bus_type);
  5386. +}
  5387. diff --git a/drivers/platform/x86/surface_aggregator/bus.h b/drivers/platform/x86/surface_aggregator/bus.h
  5388. new file mode 100644
  5389. index 000000000000..ed032c2cbdb2
  5390. --- /dev/null
  5391. +++ b/drivers/platform/x86/surface_aggregator/bus.h
  5392. @@ -0,0 +1,27 @@
  5393. +/* SPDX-License-Identifier: GPL-2.0+ */
  5394. +/*
  5395. + * Surface System Aggregator Module bus and device integration.
  5396. + *
  5397. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  5398. + */
  5399. +
  5400. +#ifndef _SURFACE_AGGREGATOR_BUS_H
  5401. +#define _SURFACE_AGGREGATOR_BUS_H
  5402. +
  5403. +#include <linux/surface_aggregator/controller.h>
  5404. +
  5405. +#ifdef CONFIG_SURFACE_AGGREGATOR_BUS
  5406. +
  5407. +void ssam_controller_remove_clients(struct ssam_controller *ctrl);
  5408. +
  5409. +int ssam_bus_register(void);
  5410. +void ssam_bus_unregister(void);
  5411. +
  5412. +#else /* CONFIG_SURFACE_AGGREGATOR_BUS */
  5413. +
  5414. +static inline void ssam_controller_remove_clients(struct ssam_controller *ctrl) {}
  5415. +static inline int ssam_bus_register(void) { return 0; }
  5416. +static inline void ssam_bus_unregister(void) {}
  5417. +
  5418. +#endif /* CONFIG_SURFACE_AGGREGATOR_BUS */
  5419. +#endif /* _SURFACE_AGGREGATOR_BUS_H */
  5420. diff --git a/drivers/platform/x86/surface_aggregator/controller.c b/drivers/platform/x86/surface_aggregator/controller.c
  5421. new file mode 100644
  5422. index 000000000000..68585c966de5
  5423. --- /dev/null
  5424. +++ b/drivers/platform/x86/surface_aggregator/controller.c
  5425. @@ -0,0 +1,2789 @@
  5426. +// SPDX-License-Identifier: GPL-2.0+
  5427. +/*
  5428. + * Main SSAM/SSH controller structure and functionality.
  5429. + *
  5430. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  5431. + */
  5432. +
  5433. +#include <linux/acpi.h>
  5434. +#include <linux/atomic.h>
  5435. +#include <linux/completion.h>
  5436. +#include <linux/gpio/consumer.h>
  5437. +#include <linux/interrupt.h>
  5438. +#include <linux/kref.h>
  5439. +#include <linux/limits.h>
  5440. +#include <linux/list.h>
  5441. +#include <linux/lockdep.h>
  5442. +#include <linux/mutex.h>
  5443. +#include <linux/rculist.h>
  5444. +#include <linux/rbtree.h>
  5445. +#include <linux/rwsem.h>
  5446. +#include <linux/serdev.h>
  5447. +#include <linux/slab.h>
  5448. +#include <linux/spinlock.h>
  5449. +#include <linux/srcu.h>
  5450. +#include <linux/types.h>
  5451. +#include <linux/workqueue.h>
  5452. +
  5453. +#include <linux/surface_aggregator/controller.h>
  5454. +#include <linux/surface_aggregator/serial_hub.h>
  5455. +
  5456. +#include "controller.h"
  5457. +#include "ssh_msgb.h"
  5458. +#include "ssh_request_layer.h"
  5459. +
  5460. +#include "trace.h"
  5461. +
  5462. +
  5463. +/* -- Safe counters. -------------------------------------------------------- */
  5464. +
  5465. +/**
  5466. + * ssh_seq_reset() - Reset/initialize sequence ID counter.
  5467. + * @c: The counter to reset.
  5468. + */
  5469. +static void ssh_seq_reset(struct ssh_seq_counter *c)
  5470. +{
  5471. + WRITE_ONCE(c->value, 0);
  5472. +}
  5473. +
  5474. +/**
  5475. + * ssh_seq_next() - Get next sequence ID.
  5476. + * @c: The counter providing the sequence IDs.
  5477. + *
  5478. + * Return: Returns the next sequence ID of the counter.
  5479. + */
  5480. +static u8 ssh_seq_next(struct ssh_seq_counter *c)
  5481. +{
  5482. + u8 old = READ_ONCE(c->value);
  5483. + u8 new = old + 1;
  5484. + u8 ret;
  5485. +
  5486. + while (unlikely((ret = cmpxchg(&c->value, old, new)) != old)) {
  5487. + old = ret;
  5488. + new = old + 1;
  5489. + }
  5490. +
  5491. + return old;
  5492. +}
  5493. +
  5494. +/**
  5495. + * ssh_rqid_reset() - Reset/initialize request ID counter.
  5496. + * @c: The counter to reset.
  5497. + */
  5498. +static void ssh_rqid_reset(struct ssh_rqid_counter *c)
  5499. +{
  5500. + WRITE_ONCE(c->value, 0);
  5501. +}
  5502. +
  5503. +/**
  5504. + * ssh_rqid_next() - Get next request ID.
  5505. + * @c: The counter providing the request IDs.
  5506. + *
  5507. + * Return: Returns the next request ID of the counter, skipping any reserved
  5508. + * request IDs.
  5509. + */
  5510. +static u16 ssh_rqid_next(struct ssh_rqid_counter *c)
  5511. +{
  5512. + u16 old = READ_ONCE(c->value);
  5513. + u16 new = ssh_rqid_next_valid(old);
  5514. + u16 ret;
  5515. +
  5516. + while (unlikely((ret = cmpxchg(&c->value, old, new)) != old)) {
  5517. + old = ret;
  5518. + new = ssh_rqid_next_valid(old);
  5519. + }
  5520. +
  5521. + return old;
  5522. +}
  5523. +
  5524. +
  5525. +/* -- Event notifier/callbacks. --------------------------------------------- */
  5526. +/*
  5527. + * The notifier system is based on linux/notifier.h, specifically the SRCU
  5528. + * implementation. The difference to that is, that some bits of the notifier
  5529. + * call return value can be tracked across multiple calls. This is done so
  5530. + * that handling of events can be tracked and a warning can be issued in case
  5531. + * an event goes unhandled. The idea of that warning is that it should help
  5532. + * discover and identify new/currently unimplemented features.
  5533. + */
  5534. +
  5535. +/**
  5536. + * ssam_event_matches_notifier() - Test if an event matches a notifier.
  5537. + * @n: The event notifier to test against.
  5538. + * @event: The event to test.
  5539. + *
  5540. + * Return: Returns %true if the given event matches the given notifier
  5541. + * according to the rules set in the notifier's event mask, %false otherwise.
  5542. + */
  5543. +static bool ssam_event_matches_notifier(const struct ssam_event_notifier *n,
  5544. + const struct ssam_event *event)
  5545. +{
  5546. + bool match = n->event.id.target_category == event->target_category;
  5547. +
  5548. + if (n->event.mask & SSAM_EVENT_MASK_TARGET)
  5549. + match &= n->event.reg.target_id == event->target_id;
  5550. +
  5551. + if (n->event.mask & SSAM_EVENT_MASK_INSTANCE)
  5552. + match &= n->event.id.instance == event->instance_id;
  5553. +
  5554. + return match;
  5555. +}
  5556. +
  5557. +/**
  5558. + * ssam_nfblk_call_chain() - Call event notifier callbacks of the given chain.
  5559. + * @nh: The notifier head for which the notifier callbacks should be called.
  5560. + * @event: The event data provided to the callbacks.
  5561. + *
  5562. + * Call all registered notifier callbacks in order of their priority until
  5563. + * either no notifier is left or a notifier returns a value with the
  5564. + * %SSAM_NOTIF_STOP bit set. Note that this bit is automatically set via
  5565. + * ssam_notifier_from_errno() on any non-zero error value.
  5566. + *
  5567. + * Return: Returns the notifier status value, which contains the notifier
  5568. + * status bits (%SSAM_NOTIF_HANDLED and %SSAM_NOTIF_STOP) as well as a
  5569. + * potential error value returned from the last executed notifier callback.
  5570. + * Use ssam_notifier_to_errno() to convert this value to the original error
  5571. + * value.
  5572. + */
  5573. +static int ssam_nfblk_call_chain(struct ssam_nf_head *nh, struct ssam_event *event)
  5574. +{
  5575. + struct ssam_event_notifier *nf;
  5576. + int ret = 0, idx;
  5577. +
  5578. + idx = srcu_read_lock(&nh->srcu);
  5579. +
  5580. + list_for_each_entry_rcu(nf, &nh->head, base.node,
  5581. + srcu_read_lock_held(&nh->srcu)) {
  5582. + if (ssam_event_matches_notifier(nf, event)) {
  5583. + ret = (ret & SSAM_NOTIF_STATE_MASK) | nf->base.fn(nf, event);
  5584. + if (ret & SSAM_NOTIF_STOP)
  5585. + break;
  5586. + }
  5587. + }
  5588. +
  5589. + srcu_read_unlock(&nh->srcu, idx);
  5590. + return ret;
  5591. +}
  5592. +
  5593. +/**
  5594. + * ssam_nfblk_insert() - Insert a new notifier block into the given notifier
  5595. + * list.
  5596. + * @nh: The notifier head into which the block should be inserted.
  5597. + * @nb: The notifier block to add.
  5598. + *
  5599. + * Note: This function must be synchronized by the caller with respect to other
  5600. + * insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
  5601. + *
  5602. + * Return: Returns zero on success, %-EEXIST if the notifier block has already
  5603. + * been registered.
  5604. + */
  5605. +static int ssam_nfblk_insert(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
  5606. +{
  5607. + struct ssam_notifier_block *p;
  5608. + struct list_head *h;
  5609. +
  5610. + /* Runs under lock, no need for RCU variant. */
  5611. + list_for_each(h, &nh->head) {
  5612. + p = list_entry(h, struct ssam_notifier_block, node);
  5613. +
  5614. + if (unlikely(p == nb)) {
  5615. + WARN(1, "double register detected");
  5616. + return -EEXIST;
  5617. + }
  5618. +
  5619. + if (nb->priority > p->priority)
  5620. + break;
  5621. + }
  5622. +
  5623. + list_add_tail_rcu(&nb->node, h);
  5624. + return 0;
  5625. +}
  5626. +
  5627. +/**
  5628. + * ssam_nfblk_find() - Check if a notifier block is registered on the given
  5629. + * notifier head.
  5630. + * list.
  5631. + * @nh: The notifier head on which to search.
  5632. + * @nb: The notifier block to search for.
  5633. + *
  5634. + * Note: This function must be synchronized by the caller with respect to other
  5635. + * insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
  5636. + *
  5637. + * Return: Returns true if the given notifier block is registered on the given
  5638. + * notifier head, false otherwise.
  5639. + */
  5640. +static bool ssam_nfblk_find(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
  5641. +{
  5642. + struct ssam_notifier_block *p;
  5643. +
  5644. + /* Runs under lock, no need for RCU variant. */
  5645. + list_for_each_entry(p, &nh->head, node) {
  5646. + if (p == nb)
  5647. + return true;
  5648. + }
  5649. +
  5650. + return false;
  5651. +}
  5652. +
  5653. +/**
  5654. + * ssam_nfblk_remove() - Remove a notifier block from its notifier list.
  5655. + * @nb: The notifier block to be removed.
  5656. + *
  5657. + * Note: This function must be synchronized by the caller with respect to
  5658. + * other insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
  5659. + * Furthermore, the caller _must_ ensure SRCU synchronization by calling
  5660. + * synchronize_srcu() with ``nh->srcu`` after leaving the critical section, to
  5661. + * ensure that the removed notifier block is not in use any more.
  5662. + */
  5663. +static void ssam_nfblk_remove(struct ssam_notifier_block *nb)
  5664. +{
  5665. + list_del_rcu(&nb->node);
  5666. +}
  5667. +
  5668. +/**
  5669. + * ssam_nf_head_init() - Initialize the given notifier head.
  5670. + * @nh: The notifier head to initialize.
  5671. + */
  5672. +static int ssam_nf_head_init(struct ssam_nf_head *nh)
  5673. +{
  5674. + int status;
  5675. +
  5676. + status = init_srcu_struct(&nh->srcu);
  5677. + if (status)
  5678. + return status;
  5679. +
  5680. + INIT_LIST_HEAD(&nh->head);
  5681. + return 0;
  5682. +}
  5683. +
  5684. +/**
  5685. + * ssam_nf_head_destroy() - Deinitialize the given notifier head.
  5686. + * @nh: The notifier head to deinitialize.
  5687. + */
  5688. +static void ssam_nf_head_destroy(struct ssam_nf_head *nh)
  5689. +{
  5690. + cleanup_srcu_struct(&nh->srcu);
  5691. +}
  5692. +
  5693. +
  5694. +/* -- Event/notification registry. ------------------------------------------ */
  5695. +
  5696. +/**
  5697. + * struct ssam_nf_refcount_key - Key used for event activation reference
  5698. + * counting.
  5699. + * @reg: The registry via which the event is enabled/disabled.
  5700. + * @id: The ID uniquely describing the event.
  5701. + */
  5702. +struct ssam_nf_refcount_key {
  5703. + struct ssam_event_registry reg;
  5704. + struct ssam_event_id id;
  5705. +};
  5706. +
  5707. +/**
  5708. + * struct ssam_nf_refcount_entry - RB-tree entry for reference counting event
  5709. + * activations.
  5710. + * @node: The node of this entry in the rb-tree.
  5711. + * @key: The key of the event.
  5712. + * @refcount: The reference-count of the event.
  5713. + * @flags: The flags used when enabling the event.
  5714. + */
  5715. +struct ssam_nf_refcount_entry {
  5716. + struct rb_node node;
  5717. + struct ssam_nf_refcount_key key;
  5718. + int refcount;
  5719. + u8 flags;
  5720. +};
  5721. +
  5722. +/**
  5723. + * ssam_nf_refcount_inc() - Increment reference-/activation-count of the given
  5724. + * event.
  5725. + * @nf: The notifier system reference.
  5726. + * @reg: The registry used to enable/disable the event.
  5727. + * @id: The event ID.
  5728. + *
  5729. + * Increments the reference-/activation-count associated with the specified
  5730. + * event type/ID, allocating a new entry for this event ID if necessary. A
  5731. + * newly allocated entry will have a refcount of one.
  5732. + *
  5733. + * Note: ``nf->lock`` must be held when calling this function.
  5734. + *
  5735. + * Return: Returns the refcount entry on success. Returns an error pointer
  5736. + * with %-ENOSPC if there have already been %INT_MAX events of the specified
  5737. + * ID and type registered, or %-ENOMEM if the entry could not be allocated.
  5738. + */
  5739. +static struct ssam_nf_refcount_entry *
  5740. +ssam_nf_refcount_inc(struct ssam_nf *nf, struct ssam_event_registry reg,
  5741. + struct ssam_event_id id)
  5742. +{
  5743. + struct ssam_nf_refcount_entry *entry;
  5744. + struct ssam_nf_refcount_key key;
  5745. + struct rb_node **link = &nf->refcount.rb_node;
  5746. + struct rb_node *parent = NULL;
  5747. + int cmp;
  5748. +
  5749. + lockdep_assert_held(&nf->lock);
  5750. +
  5751. + key.reg = reg;
  5752. + key.id = id;
  5753. +
  5754. + while (*link) {
  5755. + entry = rb_entry(*link, struct ssam_nf_refcount_entry, node);
  5756. + parent = *link;
  5757. +
  5758. + cmp = memcmp(&key, &entry->key, sizeof(key));
  5759. + if (cmp < 0) {
  5760. + link = &(*link)->rb_left;
  5761. + } else if (cmp > 0) {
  5762. + link = &(*link)->rb_right;
  5763. + } else if (entry->refcount < INT_MAX) {
  5764. + entry->refcount++;
  5765. + return entry;
  5766. + } else {
  5767. + WARN_ON(1);
  5768. + return ERR_PTR(-ENOSPC);
  5769. + }
  5770. + }
  5771. +
  5772. + entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  5773. + if (!entry)
  5774. + return ERR_PTR(-ENOMEM);
  5775. +
  5776. + entry->key = key;
  5777. + entry->refcount = 1;
  5778. +
  5779. + rb_link_node(&entry->node, parent, link);
  5780. + rb_insert_color(&entry->node, &nf->refcount);
  5781. +
  5782. + return entry;
  5783. +}
  5784. +
  5785. +/**
  5786. + * ssam_nf_refcount_dec() - Decrement reference-/activation-count of the given
  5787. + * event.
  5788. + * @nf: The notifier system reference.
  5789. + * @reg: The registry used to enable/disable the event.
  5790. + * @id: The event ID.
  5791. + *
  5792. + * Decrements the reference-/activation-count of the specified event,
  5793. + * returning its entry. If the returned entry has a refcount of zero, the
  5794. + * caller is responsible for freeing it using kfree().
  5795. + *
  5796. + * Note: ``nf->lock`` must be held when calling this function.
  5797. + *
  5798. + * Return: Returns the refcount entry on success or %NULL if the entry has not
  5799. + * been found.
  5800. + */
  5801. +static struct ssam_nf_refcount_entry *
  5802. +ssam_nf_refcount_dec(struct ssam_nf *nf, struct ssam_event_registry reg,
  5803. + struct ssam_event_id id)
  5804. +{
  5805. + struct ssam_nf_refcount_entry *entry;
  5806. + struct ssam_nf_refcount_key key;
  5807. + struct rb_node *node = nf->refcount.rb_node;
  5808. + int cmp;
  5809. +
  5810. + lockdep_assert_held(&nf->lock);
  5811. +
  5812. + key.reg = reg;
  5813. + key.id = id;
  5814. +
  5815. + while (node) {
  5816. + entry = rb_entry(node, struct ssam_nf_refcount_entry, node);
  5817. +
  5818. + cmp = memcmp(&key, &entry->key, sizeof(key));
  5819. + if (cmp < 0) {
  5820. + node = node->rb_left;
  5821. + } else if (cmp > 0) {
  5822. + node = node->rb_right;
  5823. + } else {
  5824. + entry->refcount--;
  5825. + if (entry->refcount == 0)
  5826. + rb_erase(&entry->node, &nf->refcount);
  5827. +
  5828. + return entry;
  5829. + }
  5830. + }
  5831. +
  5832. + return NULL;
  5833. +}
  5834. +
  5835. +/**
  5836. + * ssam_nf_refcount_dec_free() - Decrement reference-/activation-count of the
  5837. + * given event and free its entry if the reference count reaches zero.
  5838. + * @nf: The notifier system reference.
  5839. + * @reg: The registry used to enable/disable the event.
  5840. + * @id: The event ID.
  5841. + *
  5842. + * Decrements the reference-/activation-count of the specified event, freeing
  5843. + * its entry if it reaches zero.
  5844. + *
  5845. + * Note: ``nf->lock`` must be held when calling this function.
  5846. + */
  5847. +static void ssam_nf_refcount_dec_free(struct ssam_nf *nf,
  5848. + struct ssam_event_registry reg,
  5849. + struct ssam_event_id id)
  5850. +{
  5851. + struct ssam_nf_refcount_entry *entry;
  5852. +
  5853. + lockdep_assert_held(&nf->lock);
  5854. +
  5855. + entry = ssam_nf_refcount_dec(nf, reg, id);
  5856. + if (entry && entry->refcount == 0)
  5857. + kfree(entry);
  5858. +}
  5859. +
  5860. +/**
  5861. + * ssam_nf_refcount_empty() - Test if the notification system has any
  5862. + * enabled/active events.
  5863. + * @nf: The notification system.
  5864. + */
  5865. +static bool ssam_nf_refcount_empty(struct ssam_nf *nf)
  5866. +{
  5867. + return RB_EMPTY_ROOT(&nf->refcount);
  5868. +}
  5869. +
  5870. +/**
  5871. + * ssam_nf_call() - Call notification callbacks for the provided event.
  5872. + * @nf: The notifier system
  5873. + * @dev: The associated device, only used for logging.
  5874. + * @rqid: The request ID of the event.
  5875. + * @event: The event provided to the callbacks.
  5876. + *
  5877. + * Execute registered callbacks in order of their priority until either no
  5878. + * callback is left or a callback returns a value with the %SSAM_NOTIF_STOP
  5879. + * bit set. Note that this bit is set automatically when converting non-zero
  5880. + * error values via ssam_notifier_from_errno() to notifier values.
  5881. + *
  5882. + * Also note that any callback that could handle an event should return a value
  5883. + * with bit %SSAM_NOTIF_HANDLED set, indicating that the event does not go
  5884. + * unhandled/ignored. In case no registered callback could handle an event,
  5885. + * this function will emit a warning.
  5886. + *
  5887. + * In case a callback failed, this function will emit an error message.
  5888. + */
  5889. +static void ssam_nf_call(struct ssam_nf *nf, struct device *dev, u16 rqid,
  5890. + struct ssam_event *event)
  5891. +{
  5892. + struct ssam_nf_head *nf_head;
  5893. + int status, nf_ret;
  5894. +
  5895. + if (!ssh_rqid_is_event(rqid)) {
  5896. + dev_warn(dev, "event: unsupported rqid: %#06x\n", rqid);
  5897. + return;
  5898. + }
  5899. +
  5900. + nf_head = &nf->head[ssh_rqid_to_event(rqid)];
  5901. + nf_ret = ssam_nfblk_call_chain(nf_head, event);
  5902. + status = ssam_notifier_to_errno(nf_ret);
  5903. +
  5904. + if (status < 0) {
  5905. + dev_err(dev,
  5906. + "event: error handling event: %d (tc: %#04x, tid: %#04x, cid: %#04x, iid: %#04x)\n",
  5907. + status, event->target_category, event->target_id,
  5908. + event->command_id, event->instance_id);
  5909. + } else if (!(nf_ret & SSAM_NOTIF_HANDLED)) {
  5910. + dev_warn(dev,
  5911. + "event: unhandled event (rqid: %#04x, tc: %#04x, tid: %#04x, cid: %#04x, iid: %#04x)\n",
  5912. + rqid, event->target_category, event->target_id,
  5913. + event->command_id, event->instance_id);
  5914. + }
  5915. +}
  5916. +
  5917. +/**
  5918. + * ssam_nf_init() - Initialize the notifier system.
  5919. + * @nf: The notifier system to initialize.
  5920. + */
  5921. +static int ssam_nf_init(struct ssam_nf *nf)
  5922. +{
  5923. + int i, status;
  5924. +
  5925. + for (i = 0; i < SSH_NUM_EVENTS; i++) {
  5926. + status = ssam_nf_head_init(&nf->head[i]);
  5927. + if (status)
  5928. + break;
  5929. + }
  5930. +
  5931. + if (status) {
  5932. + while (i--)
  5933. + ssam_nf_head_destroy(&nf->head[i]);
  5934. +
  5935. + return status;
  5936. + }
  5937. +
  5938. + mutex_init(&nf->lock);
  5939. + return 0;
  5940. +}
  5941. +
  5942. +/**
  5943. + * ssam_nf_destroy() - Deinitialize the notifier system.
  5944. + * @nf: The notifier system to deinitialize.
  5945. + */
  5946. +static void ssam_nf_destroy(struct ssam_nf *nf)
  5947. +{
  5948. + int i;
  5949. +
  5950. + for (i = 0; i < SSH_NUM_EVENTS; i++)
  5951. + ssam_nf_head_destroy(&nf->head[i]);
  5952. +
  5953. + mutex_destroy(&nf->lock);
  5954. +}
  5955. +
  5956. +
  5957. +/* -- Event/async request completion system. -------------------------------- */
  5958. +
  5959. +#define SSAM_CPLT_WQ_NAME "ssam_cpltq"
  5960. +
  5961. +/*
  5962. + * SSAM_CPLT_WQ_BATCH - Maximum number of event item completions executed per
  5963. + * work execution. Used to prevent livelocking of the workqueue. Value chosen
  5964. + * via educated guess, may be adjusted.
  5965. + */
  5966. +#define SSAM_CPLT_WQ_BATCH 10
  5967. +
  5968. +/*
  5969. + * SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN - Maximum payload length for a cached
  5970. + * &struct ssam_event_item.
  5971. + *
  5972. + * This length has been chosen to be accommodate standard touchpad and
  5973. + * keyboard input events. Events with larger payloads will be allocated
  5974. + * separately.
  5975. + */
  5976. +#define SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN 32
  5977. +
  5978. +static struct kmem_cache *ssam_event_item_cache;
  5979. +
  5980. +/**
  5981. + * ssam_event_item_cache_init() - Initialize the event item cache.
  5982. + */
  5983. +int ssam_event_item_cache_init(void)
  5984. +{
  5985. + const unsigned int size = sizeof(struct ssam_event_item)
  5986. + + SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN;
  5987. + const unsigned int align = __alignof__(struct ssam_event_item);
  5988. + struct kmem_cache *cache;
  5989. +
  5990. + cache = kmem_cache_create("ssam_event_item", size, align, 0, NULL);
  5991. + if (!cache)
  5992. + return -ENOMEM;
  5993. +
  5994. + ssam_event_item_cache = cache;
  5995. + return 0;
  5996. +}
  5997. +
  5998. +/**
  5999. + * ssam_event_item_cache_destroy() - Deinitialize the event item cache.
  6000. + */
  6001. +void ssam_event_item_cache_destroy(void)
  6002. +{
  6003. + kmem_cache_destroy(ssam_event_item_cache);
  6004. + ssam_event_item_cache = NULL;
  6005. +}
  6006. +
  6007. +static void __ssam_event_item_free_cached(struct ssam_event_item *item)
  6008. +{
  6009. + kmem_cache_free(ssam_event_item_cache, item);
  6010. +}
  6011. +
  6012. +static void __ssam_event_item_free_generic(struct ssam_event_item *item)
  6013. +{
  6014. + kfree(item);
  6015. +}
  6016. +
  6017. +/**
  6018. + * ssam_event_item_free() - Free the provided event item.
  6019. + * @item: The event item to free.
  6020. + */
  6021. +static void ssam_event_item_free(struct ssam_event_item *item)
  6022. +{
  6023. + trace_ssam_event_item_free(item);
  6024. + item->ops.free(item);
  6025. +}
  6026. +
  6027. +/**
  6028. + * ssam_event_item_alloc() - Allocate an event item with the given payload size.
  6029. + * @len: The event payload length.
  6030. + * @flags: The flags used for allocation.
  6031. + *
  6032. + * Allocate an event item with the given payload size, preferring allocation
  6033. + * from the event item cache if the payload is small enough (i.e. smaller than
  6034. + * %SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN). Sets the item operations and payload
  6035. + * length values. The item free callback (``ops.free``) should not be
  6036. + * overwritten after this call.
  6037. + *
  6038. + * Return: Returns the newly allocated event item.
  6039. + */
  6040. +static struct ssam_event_item *ssam_event_item_alloc(size_t len, gfp_t flags)
  6041. +{
  6042. + struct ssam_event_item *item;
  6043. +
  6044. + if (len <= SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN) {
  6045. + item = kmem_cache_alloc(ssam_event_item_cache, flags);
  6046. + if (!item)
  6047. + return NULL;
  6048. +
  6049. + item->ops.free = __ssam_event_item_free_cached;
  6050. + } else {
  6051. + item = kzalloc(struct_size(item, event.data, len), flags);
  6052. + if (!item)
  6053. + return NULL;
  6054. +
  6055. + item->ops.free = __ssam_event_item_free_generic;
  6056. + }
  6057. +
  6058. + item->event.length = len;
  6059. +
  6060. + trace_ssam_event_item_alloc(item, len);
  6061. + return item;
  6062. +}
  6063. +
  6064. +/**
  6065. + * ssam_event_queue_push() - Push an event item to the event queue.
  6066. + * @q: The event queue.
  6067. + * @item: The item to add.
  6068. + */
  6069. +static void ssam_event_queue_push(struct ssam_event_queue *q,
  6070. + struct ssam_event_item *item)
  6071. +{
  6072. + spin_lock(&q->lock);
  6073. + list_add_tail(&item->node, &q->head);
  6074. + spin_unlock(&q->lock);
  6075. +}
  6076. +
  6077. +/**
  6078. + * ssam_event_queue_pop() - Pop the next event item from the event queue.
  6079. + * @q: The event queue.
  6080. + *
  6081. + * Returns and removes the next event item from the queue. Returns %NULL If
  6082. + * there is no event item left.
  6083. + */
  6084. +static struct ssam_event_item *ssam_event_queue_pop(struct ssam_event_queue *q)
  6085. +{
  6086. + struct ssam_event_item *item;
  6087. +
  6088. + spin_lock(&q->lock);
  6089. + item = list_first_entry_or_null(&q->head, struct ssam_event_item, node);
  6090. + if (item)
  6091. + list_del(&item->node);
  6092. + spin_unlock(&q->lock);
  6093. +
  6094. + return item;
  6095. +}
  6096. +
  6097. +/**
  6098. + * ssam_event_queue_is_empty() - Check if the event queue is empty.
  6099. + * @q: The event queue.
  6100. + */
  6101. +static bool ssam_event_queue_is_empty(struct ssam_event_queue *q)
  6102. +{
  6103. + bool empty;
  6104. +
  6105. + spin_lock(&q->lock);
  6106. + empty = list_empty(&q->head);
  6107. + spin_unlock(&q->lock);
  6108. +
  6109. + return empty;
  6110. +}
  6111. +
  6112. +/**
  6113. + * ssam_cplt_get_event_queue() - Get the event queue for the given parameters.
  6114. + * @cplt: The completion system on which to look for the queue.
  6115. + * @tid: The target ID of the queue.
  6116. + * @rqid: The request ID representing the event ID for which to get the queue.
  6117. + *
  6118. + * Return: Returns the event queue corresponding to the event type described
  6119. + * by the given parameters. If the request ID does not represent an event,
  6120. + * this function returns %NULL. If the target ID is not supported, this
  6121. + * function will fall back to the default target ID (``tid = 1``).
  6122. + */
  6123. +static
  6124. +struct ssam_event_queue *ssam_cplt_get_event_queue(struct ssam_cplt *cplt,
  6125. + u8 tid, u16 rqid)
  6126. +{
  6127. + u16 event = ssh_rqid_to_event(rqid);
  6128. + u16 tidx = ssh_tid_to_index(tid);
  6129. +
  6130. + if (!ssh_rqid_is_event(rqid)) {
  6131. + dev_err(cplt->dev, "event: unsupported request ID: %#06x\n", rqid);
  6132. + return NULL;
  6133. + }
  6134. +
  6135. + if (!ssh_tid_is_valid(tid)) {
  6136. + dev_warn(cplt->dev, "event: unsupported target ID: %u\n", tid);
  6137. + tidx = 0;
  6138. + }
  6139. +
  6140. + return &cplt->event.target[tidx].queue[event];
  6141. +}
  6142. +
  6143. +/**
  6144. + * ssam_cplt_submit() - Submit a work item to the completion system workqueue.
  6145. + * @cplt: The completion system.
  6146. + * @work: The work item to submit.
  6147. + */
  6148. +static bool ssam_cplt_submit(struct ssam_cplt *cplt, struct work_struct *work)
  6149. +{
  6150. + return queue_work(cplt->wq, work);
  6151. +}
  6152. +
  6153. +/**
  6154. + * ssam_cplt_submit_event() - Submit an event to the completion system.
  6155. + * @cplt: The completion system.
  6156. + * @item: The event item to submit.
  6157. + *
  6158. + * Submits the event to the completion system by queuing it on the event item
  6159. + * queue and queuing the respective event queue work item on the completion
  6160. + * workqueue, which will eventually complete the event.
  6161. + *
  6162. + * Return: Returns zero on success, %-EINVAL if there is no event queue that
  6163. + * can handle the given event item.
  6164. + */
  6165. +static int ssam_cplt_submit_event(struct ssam_cplt *cplt,
  6166. + struct ssam_event_item *item)
  6167. +{
  6168. + struct ssam_event_queue *evq;
  6169. +
  6170. + evq = ssam_cplt_get_event_queue(cplt, item->event.target_id, item->rqid);
  6171. + if (!evq)
  6172. + return -EINVAL;
  6173. +
  6174. + ssam_event_queue_push(evq, item);
  6175. + ssam_cplt_submit(cplt, &evq->work);
  6176. + return 0;
  6177. +}
  6178. +
  6179. +/**
  6180. + * ssam_cplt_flush() - Flush the completion system.
  6181. + * @cplt: The completion system.
  6182. + *
  6183. + * Flush the completion system by waiting until all currently submitted work
  6184. + * items have been completed.
  6185. + *
  6186. + * Note: This function does not guarantee that all events will have been
  6187. + * handled once this call terminates. In case of a larger number of
  6188. + * to-be-completed events, the event queue work function may re-schedule its
  6189. + * work item, which this flush operation will ignore.
  6190. + *
  6191. + * This operation is only intended to, during normal operation prior to
  6192. + * shutdown, try to complete most events and requests to get them out of the
  6193. + * system while the system is still fully operational. It does not aim to
  6194. + * provide any guarantee that all of them have been handled.
  6195. + */
  6196. +static void ssam_cplt_flush(struct ssam_cplt *cplt)
  6197. +{
  6198. + flush_workqueue(cplt->wq);
  6199. +}
  6200. +
  6201. +static void ssam_event_queue_work_fn(struct work_struct *work)
  6202. +{
  6203. + struct ssam_event_queue *queue;
  6204. + struct ssam_event_item *item;
  6205. + struct ssam_nf *nf;
  6206. + struct device *dev;
  6207. + unsigned int iterations = SSAM_CPLT_WQ_BATCH;
  6208. +
  6209. + queue = container_of(work, struct ssam_event_queue, work);
  6210. + nf = &queue->cplt->event.notif;
  6211. + dev = queue->cplt->dev;
  6212. +
  6213. + /* Limit number of processed events to avoid livelocking. */
  6214. + do {
  6215. + item = ssam_event_queue_pop(queue);
  6216. + if (!item)
  6217. + return;
  6218. +
  6219. + ssam_nf_call(nf, dev, item->rqid, &item->event);
  6220. + ssam_event_item_free(item);
  6221. + } while (--iterations);
  6222. +
  6223. + if (!ssam_event_queue_is_empty(queue))
  6224. + ssam_cplt_submit(queue->cplt, &queue->work);
  6225. +}
  6226. +
  6227. +/**
  6228. + * ssam_event_queue_init() - Initialize an event queue.
  6229. + * @cplt: The completion system on which the queue resides.
  6230. + * @evq: The event queue to initialize.
  6231. + */
  6232. +static void ssam_event_queue_init(struct ssam_cplt *cplt,
  6233. + struct ssam_event_queue *evq)
  6234. +{
  6235. + evq->cplt = cplt;
  6236. + spin_lock_init(&evq->lock);
  6237. + INIT_LIST_HEAD(&evq->head);
  6238. + INIT_WORK(&evq->work, ssam_event_queue_work_fn);
  6239. +}
  6240. +
  6241. +/**
  6242. + * ssam_cplt_init() - Initialize completion system.
  6243. + * @cplt: The completion system to initialize.
  6244. + * @dev: The device used for logging.
  6245. + */
  6246. +static int ssam_cplt_init(struct ssam_cplt *cplt, struct device *dev)
  6247. +{
  6248. + struct ssam_event_target *target;
  6249. + int status, c, i;
  6250. +
  6251. + cplt->dev = dev;
  6252. +
  6253. + cplt->wq = create_workqueue(SSAM_CPLT_WQ_NAME);
  6254. + if (!cplt->wq)
  6255. + return -ENOMEM;
  6256. +
  6257. + for (c = 0; c < ARRAY_SIZE(cplt->event.target); c++) {
  6258. + target = &cplt->event.target[c];
  6259. +
  6260. + for (i = 0; i < ARRAY_SIZE(target->queue); i++)
  6261. + ssam_event_queue_init(cplt, &target->queue[i]);
  6262. + }
  6263. +
  6264. + status = ssam_nf_init(&cplt->event.notif);
  6265. + if (status)
  6266. + destroy_workqueue(cplt->wq);
  6267. +
  6268. + return status;
  6269. +}
  6270. +
  6271. +/**
  6272. + * ssam_cplt_destroy() - Deinitialize the completion system.
  6273. + * @cplt: The completion system to deinitialize.
  6274. + *
  6275. + * Deinitialize the given completion system and ensure that all pending, i.e.
  6276. + * yet-to-be-completed, event items and requests have been handled.
  6277. + */
  6278. +static void ssam_cplt_destroy(struct ssam_cplt *cplt)
  6279. +{
  6280. + /*
  6281. + * Note: destroy_workqueue ensures that all currently queued work will
  6282. + * be fully completed and the workqueue drained. This means that this
  6283. + * call will inherently also free any queued ssam_event_items, thus we
  6284. + * don't have to take care of that here explicitly.
  6285. + */
  6286. + destroy_workqueue(cplt->wq);
  6287. + ssam_nf_destroy(&cplt->event.notif);
  6288. +}
  6289. +
  6290. +
  6291. +/* -- Main SSAM device structures. ------------------------------------------ */
  6292. +
  6293. +/**
  6294. + * ssam_controller_device() - Get the &struct device associated with this
  6295. + * controller.
  6296. + * @c: The controller for which to get the device.
  6297. + *
  6298. + * Return: Returns the &struct device associated with this controller,
  6299. + * providing its lower-level transport.
  6300. + */
  6301. +struct device *ssam_controller_device(struct ssam_controller *c)
  6302. +{
  6303. + return ssh_rtl_get_device(&c->rtl);
  6304. +}
  6305. +EXPORT_SYMBOL_GPL(ssam_controller_device);
  6306. +
  6307. +static void __ssam_controller_release(struct kref *kref)
  6308. +{
  6309. + struct ssam_controller *ctrl = to_ssam_controller(kref, kref);
  6310. +
  6311. + /*
  6312. + * The lock-call here is to satisfy lockdep. At this point we really
  6313. + * expect this to be the last remaining reference to the controller.
  6314. + * Anything else is a bug.
  6315. + */
  6316. + ssam_controller_lock(ctrl);
  6317. + ssam_controller_destroy(ctrl);
  6318. + ssam_controller_unlock(ctrl);
  6319. +
  6320. + kfree(ctrl);
  6321. +}
  6322. +
  6323. +/**
  6324. + * ssam_controller_get() - Increment reference count of controller.
  6325. + * @c: The controller.
  6326. + *
  6327. + * Return: Returns the controller provided as input.
  6328. + */
  6329. +struct ssam_controller *ssam_controller_get(struct ssam_controller *c)
  6330. +{
  6331. + if (c)
  6332. + kref_get(&c->kref);
  6333. + return c;
  6334. +}
  6335. +EXPORT_SYMBOL_GPL(ssam_controller_get);
  6336. +
  6337. +/**
  6338. + * ssam_controller_put() - Decrement reference count of controller.
  6339. + * @c: The controller.
  6340. + */
  6341. +void ssam_controller_put(struct ssam_controller *c)
  6342. +{
  6343. + if (c)
  6344. + kref_put(&c->kref, __ssam_controller_release);
  6345. +}
  6346. +EXPORT_SYMBOL_GPL(ssam_controller_put);
  6347. +
  6348. +/**
  6349. + * ssam_controller_statelock() - Lock the controller against state transitions.
  6350. + * @c: The controller to lock.
  6351. + *
  6352. + * Lock the controller against state transitions. Holding this lock guarantees
  6353. + * that the controller will not transition between states, i.e. if the
  6354. + * controller is in state "started", when this lock has been acquired, it will
  6355. + * remain in this state at least until the lock has been released.
  6356. + *
  6357. + * Multiple clients may concurrently hold this lock. In other words: The
  6358. + * ``statelock`` functions represent the read-lock part of a r/w-semaphore.
  6359. + * Actions causing state transitions of the controller must be executed while
  6360. + * holding the write-part of this r/w-semaphore (see ssam_controller_lock()
  6361. + * and ssam_controller_unlock() for that).
  6362. + *
  6363. + * See ssam_controller_stateunlock() for the corresponding unlock function.
  6364. + */
  6365. +void ssam_controller_statelock(struct ssam_controller *c)
  6366. +{
  6367. + down_read(&c->lock);
  6368. +}
  6369. +EXPORT_SYMBOL_GPL(ssam_controller_statelock);
  6370. +
  6371. +/**
  6372. + * ssam_controller_stateunlock() - Unlock controller state transitions.
  6373. + * @c: The controller to unlock.
  6374. + *
  6375. + * See ssam_controller_statelock() for the corresponding lock function.
  6376. + */
  6377. +void ssam_controller_stateunlock(struct ssam_controller *c)
  6378. +{
  6379. + up_read(&c->lock);
  6380. +}
  6381. +EXPORT_SYMBOL_GPL(ssam_controller_stateunlock);
  6382. +
  6383. +/**
  6384. + * ssam_controller_lock() - Acquire the main controller lock.
  6385. + * @c: The controller to lock.
  6386. + *
  6387. + * This lock must be held for any state transitions, including transition to
  6388. + * suspend/resumed states and during shutdown. See ssam_controller_statelock()
  6389. + * for more details on controller locking.
  6390. + *
  6391. + * See ssam_controller_unlock() for the corresponding unlock function.
  6392. + */
  6393. +void ssam_controller_lock(struct ssam_controller *c)
  6394. +{
  6395. + down_write(&c->lock);
  6396. +}
  6397. +
  6398. +/*
  6399. + * ssam_controller_unlock() - Release the main controller lock.
  6400. + * @c: The controller to unlock.
  6401. + *
  6402. + * See ssam_controller_lock() for the corresponding lock function.
  6403. + */
  6404. +void ssam_controller_unlock(struct ssam_controller *c)
  6405. +{
  6406. + up_write(&c->lock);
  6407. +}
  6408. +
  6409. +static void ssam_handle_event(struct ssh_rtl *rtl,
  6410. + const struct ssh_command *cmd,
  6411. + const struct ssam_span *data)
  6412. +{
  6413. + struct ssam_controller *ctrl = to_ssam_controller(rtl, rtl);
  6414. + struct ssam_event_item *item;
  6415. +
  6416. + item = ssam_event_item_alloc(data->len, GFP_KERNEL);
  6417. + if (!item)
  6418. + return;
  6419. +
  6420. + item->rqid = get_unaligned_le16(&cmd->rqid);
  6421. + item->event.target_category = cmd->tc;
  6422. + item->event.target_id = cmd->tid_in;
  6423. + item->event.command_id = cmd->cid;
  6424. + item->event.instance_id = cmd->iid;
  6425. + memcpy(&item->event.data[0], data->ptr, data->len);
  6426. +
  6427. + if (WARN_ON(ssam_cplt_submit_event(&ctrl->cplt, item)))
  6428. + ssam_event_item_free(item);
  6429. +}
  6430. +
  6431. +static const struct ssh_rtl_ops ssam_rtl_ops = {
  6432. + .handle_event = ssam_handle_event,
  6433. +};
  6434. +
  6435. +static bool ssam_notifier_is_empty(struct ssam_controller *ctrl);
  6436. +static void ssam_notifier_unregister_all(struct ssam_controller *ctrl);
  6437. +
  6438. +#define SSAM_SSH_DSM_REVISION 0
  6439. +
  6440. +/* d5e383e1-d892-4a76-89fc-f6aaae7ed5b5 */
  6441. +static const guid_t SSAM_SSH_DSM_GUID =
  6442. + GUID_INIT(0xd5e383e1, 0xd892, 0x4a76,
  6443. + 0x89, 0xfc, 0xf6, 0xaa, 0xae, 0x7e, 0xd5, 0xb5);
  6444. +
  6445. +enum ssh_dsm_fn {
  6446. + SSH_DSM_FN_SSH_POWER_PROFILE = 0x05,
  6447. + SSH_DSM_FN_SCREEN_ON_SLEEP_IDLE_TIMEOUT = 0x06,
  6448. + SSH_DSM_FN_SCREEN_OFF_SLEEP_IDLE_TIMEOUT = 0x07,
  6449. + SSH_DSM_FN_D3_CLOSES_HANDLE = 0x08,
  6450. + SSH_DSM_FN_SSH_BUFFER_SIZE = 0x09,
  6451. +};
  6452. +
  6453. +static int ssam_dsm_get_functions(acpi_handle handle, u64 *funcs)
  6454. +{
  6455. + union acpi_object *obj;
  6456. + u64 mask = 0;
  6457. + int i;
  6458. +
  6459. + *funcs = 0;
  6460. +
  6461. + /*
  6462. + * The _DSM function is only present on newer models. It is not
  6463. + * present on 5th and 6th generation devices (i.e. up to and including
  6464. + * Surface Pro 6, Surface Laptop 2, Surface Book 2).
  6465. + *
  6466. + * If the _DSM is not present, indicate that no function is supported.
  6467. + * This will result in default values being set.
  6468. + */
  6469. + if (!acpi_has_method(handle, "_DSM"))
  6470. + return 0;
  6471. +
  6472. + obj = acpi_evaluate_dsm_typed(handle, &SSAM_SSH_DSM_GUID,
  6473. + SSAM_SSH_DSM_REVISION, 0, NULL,
  6474. + ACPI_TYPE_BUFFER);
  6475. + if (!obj)
  6476. + return -EIO;
  6477. +
  6478. + for (i = 0; i < obj->buffer.length && i < 8; i++)
  6479. + mask |= (((u64)obj->buffer.pointer[i]) << (i * 8));
  6480. +
  6481. + if (mask & BIT(0))
  6482. + *funcs = mask;
  6483. +
  6484. + ACPI_FREE(obj);
  6485. + return 0;
  6486. +}
  6487. +
  6488. +static int ssam_dsm_load_u32(acpi_handle handle, u64 funcs, u64 func, u32 *ret)
  6489. +{
  6490. + union acpi_object *obj;
  6491. + u64 val;
  6492. +
  6493. + if (!(funcs & BIT_ULL(func)))
  6494. + return 0; /* Not supported, leave *ret at its default value */
  6495. +
  6496. + obj = acpi_evaluate_dsm_typed(handle, &SSAM_SSH_DSM_GUID,
  6497. + SSAM_SSH_DSM_REVISION, func, NULL,
  6498. + ACPI_TYPE_INTEGER);
  6499. + if (!obj)
  6500. + return -EIO;
  6501. +
  6502. + val = obj->integer.value;
  6503. + ACPI_FREE(obj);
  6504. +
  6505. + if (val > U32_MAX)
  6506. + return -ERANGE;
  6507. +
  6508. + *ret = val;
  6509. + return 0;
  6510. +}
  6511. +
  6512. +/**
  6513. + * ssam_controller_caps_load_from_acpi() - Load controller capabilities from
  6514. + * ACPI _DSM.
  6515. + * @handle: The handle of the ACPI controller/SSH device.
  6516. + * @caps: Where to store the capabilities in.
  6517. + *
  6518. + * Initializes the given controller capabilities with default values, then
  6519. + * checks and, if the respective _DSM functions are available, loads the
  6520. + * actual capabilities from the _DSM.
  6521. + *
  6522. + * Return: Returns zero on success, a negative error code on failure.
  6523. + */
  6524. +static
  6525. +int ssam_controller_caps_load_from_acpi(acpi_handle handle,
  6526. + struct ssam_controller_caps *caps)
  6527. +{
  6528. + u32 d3_closes_handle = false;
  6529. + u64 funcs;
  6530. + int status;
  6531. +
  6532. + /* Set defaults. */
  6533. + caps->ssh_power_profile = U32_MAX;
  6534. + caps->screen_on_sleep_idle_timeout = U32_MAX;
  6535. + caps->screen_off_sleep_idle_timeout = U32_MAX;
  6536. + caps->d3_closes_handle = false;
  6537. + caps->ssh_buffer_size = U32_MAX;
  6538. +
  6539. + /* Pre-load supported DSM functions. */
  6540. + status = ssam_dsm_get_functions(handle, &funcs);
  6541. + if (status)
  6542. + return status;
  6543. +
  6544. + /* Load actual values from ACPI, if present. */
  6545. + status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_SSH_POWER_PROFILE,
  6546. + &caps->ssh_power_profile);
  6547. + if (status)
  6548. + return status;
  6549. +
  6550. + status = ssam_dsm_load_u32(handle, funcs,
  6551. + SSH_DSM_FN_SCREEN_ON_SLEEP_IDLE_TIMEOUT,
  6552. + &caps->screen_on_sleep_idle_timeout);
  6553. + if (status)
  6554. + return status;
  6555. +
  6556. + status = ssam_dsm_load_u32(handle, funcs,
  6557. + SSH_DSM_FN_SCREEN_OFF_SLEEP_IDLE_TIMEOUT,
  6558. + &caps->screen_off_sleep_idle_timeout);
  6559. + if (status)
  6560. + return status;
  6561. +
  6562. + status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_D3_CLOSES_HANDLE,
  6563. + &d3_closes_handle);
  6564. + if (status)
  6565. + return status;
  6566. +
  6567. + caps->d3_closes_handle = !!d3_closes_handle;
  6568. +
  6569. + status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_SSH_BUFFER_SIZE,
  6570. + &caps->ssh_buffer_size);
  6571. + if (status)
  6572. + return status;
  6573. +
  6574. + return 0;
  6575. +}
  6576. +
  6577. +/**
  6578. + * ssam_controller_init() - Initialize SSAM controller.
  6579. + * @ctrl: The controller to initialize.
  6580. + * @serdev: The serial device representing the underlying data transport.
  6581. + *
  6582. + * Initializes the given controller. Does neither start receiver nor
  6583. + * transmitter threads. After this call, the controller has to be hooked up to
  6584. + * the serdev core separately via &struct serdev_device_ops, relaying calls to
  6585. + * ssam_controller_receive_buf() and ssam_controller_write_wakeup(). Once the
  6586. + * controller has been hooked up, transmitter and receiver threads may be
  6587. + * started via ssam_controller_start(). These setup steps need to be completed
  6588. + * before controller can be used for requests.
  6589. + */
  6590. +int ssam_controller_init(struct ssam_controller *ctrl,
  6591. + struct serdev_device *serdev)
  6592. +{
  6593. + acpi_handle handle = ACPI_HANDLE(&serdev->dev);
  6594. + int status;
  6595. +
  6596. + init_rwsem(&ctrl->lock);
  6597. + kref_init(&ctrl->kref);
  6598. +
  6599. + status = ssam_controller_caps_load_from_acpi(handle, &ctrl->caps);
  6600. + if (status)
  6601. + return status;
  6602. +
  6603. + dev_dbg(&serdev->dev,
  6604. + "device capabilities:\n"
  6605. + " ssh_power_profile: %u\n"
  6606. + " ssh_buffer_size: %u\n"
  6607. + " screen_on_sleep_idle_timeout: %u\n"
  6608. + " screen_off_sleep_idle_timeout: %u\n"
  6609. + " d3_closes_handle: %u\n",
  6610. + ctrl->caps.ssh_power_profile,
  6611. + ctrl->caps.ssh_buffer_size,
  6612. + ctrl->caps.screen_on_sleep_idle_timeout,
  6613. + ctrl->caps.screen_off_sleep_idle_timeout,
  6614. + ctrl->caps.d3_closes_handle);
  6615. +
  6616. + ssh_seq_reset(&ctrl->counter.seq);
  6617. + ssh_rqid_reset(&ctrl->counter.rqid);
  6618. +
  6619. + /* Initialize event/request completion system. */
  6620. + status = ssam_cplt_init(&ctrl->cplt, &serdev->dev);
  6621. + if (status)
  6622. + return status;
  6623. +
  6624. + /* Initialize request and packet transport layers. */
  6625. + status = ssh_rtl_init(&ctrl->rtl, serdev, &ssam_rtl_ops);
  6626. + if (status) {
  6627. + ssam_cplt_destroy(&ctrl->cplt);
  6628. + return status;
  6629. + }
  6630. +
  6631. + /*
  6632. + * Set state via write_once even though we expect to be in an
  6633. + * exclusive context, due to smoke-testing in
  6634. + * ssam_request_sync_submit().
  6635. + */
  6636. + WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_INITIALIZED);
  6637. + return 0;
  6638. +}
  6639. +
  6640. +/**
  6641. + * ssam_controller_start() - Start the receiver and transmitter threads of the
  6642. + * controller.
  6643. + * @ctrl: The controller.
  6644. + *
  6645. + * Note: When this function is called, the controller should be properly
  6646. + * hooked up to the serdev core via &struct serdev_device_ops. Please refer
  6647. + * to ssam_controller_init() for more details on controller initialization.
  6648. + *
  6649. + * This function must be called with the main controller lock held (i.e. by
  6650. + * calling ssam_controller_lock()).
  6651. + */
  6652. +int ssam_controller_start(struct ssam_controller *ctrl)
  6653. +{
  6654. + int status;
  6655. +
  6656. + lockdep_assert_held_write(&ctrl->lock);
  6657. +
  6658. + if (ctrl->state != SSAM_CONTROLLER_INITIALIZED)
  6659. + return -EINVAL;
  6660. +
  6661. + status = ssh_rtl_start(&ctrl->rtl);
  6662. + if (status)
  6663. + return status;
  6664. +
  6665. + /*
  6666. + * Set state via write_once even though we expect to be locked/in an
  6667. + * exclusive context, due to smoke-testing in
  6668. + * ssam_request_sync_submit().
  6669. + */
  6670. + WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STARTED);
  6671. + return 0;
  6672. +}
  6673. +
  6674. +/*
  6675. + * SSAM_CTRL_SHUTDOWN_FLUSH_TIMEOUT - Timeout for flushing requests during
  6676. + * shutdown.
  6677. + *
  6678. + * Chosen to be larger than one full request timeout, including packets timing
  6679. + * out. This value should give ample time to complete any outstanding requests
  6680. + * during normal operation and account for the odd package timeout.
  6681. + */
  6682. +#define SSAM_CTRL_SHUTDOWN_FLUSH_TIMEOUT msecs_to_jiffies(5000)
  6683. +
  6684. +/**
  6685. + * ssam_controller_shutdown() - Shut down the controller.
  6686. + * @ctrl: The controller.
  6687. + *
  6688. + * Shuts down the controller by flushing all pending requests and stopping the
  6689. + * transmitter and receiver threads. All requests submitted after this call
  6690. + * will fail with %-ESHUTDOWN. While it is discouraged to do so, this function
  6691. + * is safe to use in parallel with ongoing request submission.
  6692. + *
  6693. + * In the course of this shutdown procedure, all currently registered
  6694. + * notifiers will be unregistered. It is, however, strongly recommended to not
  6695. + * rely on this behavior, and instead the party registering the notifier
  6696. + * should unregister it before the controller gets shut down, e.g. via the
  6697. + * SSAM bus which guarantees client devices to be removed before a shutdown.
  6698. + *
  6699. + * Note that events may still be pending after this call, but, due to the
  6700. + * notifiers being unregistered, these events will be dropped when the
  6701. + * controller is subsequently destroyed via ssam_controller_destroy().
  6702. + *
  6703. + * This function must be called with the main controller lock held (i.e. by
  6704. + * calling ssam_controller_lock()).
  6705. + */
  6706. +void ssam_controller_shutdown(struct ssam_controller *ctrl)
  6707. +{
  6708. + enum ssam_controller_state s = ctrl->state;
  6709. + int status;
  6710. +
  6711. + lockdep_assert_held_write(&ctrl->lock);
  6712. +
  6713. + if (s == SSAM_CONTROLLER_UNINITIALIZED || s == SSAM_CONTROLLER_STOPPED)
  6714. + return;
  6715. +
  6716. + /*
  6717. + * Try to flush pending events and requests while everything still
  6718. + * works. Note: There may still be packets and/or requests in the
  6719. + * system after this call (e.g. via control packets submitted by the
  6720. + * packet transport layer or flush timeout / failure, ...). Those will
  6721. + * be handled with the ssh_rtl_shutdown() call below.
  6722. + */
  6723. + status = ssh_rtl_flush(&ctrl->rtl, SSAM_CTRL_SHUTDOWN_FLUSH_TIMEOUT);
  6724. + if (status) {
  6725. + ssam_err(ctrl, "failed to flush request transport layer: %d\n",
  6726. + status);
  6727. + }
  6728. +
  6729. + /* Try to flush all currently completing requests and events. */
  6730. + ssam_cplt_flush(&ctrl->cplt);
  6731. +
  6732. + /*
  6733. + * We expect all notifiers to have been removed by the respective client
  6734. + * driver that set them up at this point. If this warning occurs, some
  6735. + * client driver has not done that...
  6736. + */
  6737. + WARN_ON(!ssam_notifier_is_empty(ctrl));
  6738. +
  6739. + /*
  6740. + * Nevertheless, we should still take care of drivers that don't behave
  6741. + * well. Thus disable all enabled events, unregister all notifiers.
  6742. + */
  6743. + ssam_notifier_unregister_all(ctrl);
  6744. +
  6745. + /*
  6746. + * Cancel remaining requests. Ensure no new ones can be queued and stop
  6747. + * threads.
  6748. + */
  6749. + ssh_rtl_shutdown(&ctrl->rtl);
  6750. +
  6751. + /*
  6752. + * Set state via write_once even though we expect to be locked/in an
  6753. + * exclusive context, due to smoke-testing in
  6754. + * ssam_request_sync_submit().
  6755. + */
  6756. + WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STOPPED);
  6757. + ctrl->rtl.ptl.serdev = NULL;
  6758. +}
  6759. +
  6760. +/**
  6761. + * ssam_controller_destroy() - Destroy the controller and free its resources.
  6762. + * @ctrl: The controller.
  6763. + *
  6764. + * Ensures that all resources associated with the controller get freed. This
  6765. + * function should only be called after the controller has been stopped via
  6766. + * ssam_controller_shutdown(). In general, this function should not be called
  6767. + * directly. The only valid place to call this function directly is during
  6768. + * initialization, before the controller has been fully initialized and passed
  6769. + * to other processes. This function is called automatically when the
  6770. + * reference count of the controller reaches zero.
  6771. + *
  6772. + * This function must be called with the main controller lock held (i.e. by
  6773. + * calling ssam_controller_lock()).
  6774. + */
  6775. +void ssam_controller_destroy(struct ssam_controller *ctrl)
  6776. +{
  6777. + lockdep_assert_held_write(&ctrl->lock);
  6778. +
  6779. + if (ctrl->state == SSAM_CONTROLLER_UNINITIALIZED)
  6780. + return;
  6781. +
  6782. + WARN_ON(ctrl->state != SSAM_CONTROLLER_STOPPED);
  6783. +
  6784. + /*
  6785. + * Note: New events could still have been received after the previous
  6786. + * flush in ssam_controller_shutdown, before the request transport layer
  6787. + * has been shut down. At this point, after the shutdown, we can be sure
  6788. + * that no new events will be queued. The call to ssam_cplt_destroy will
  6789. + * ensure that those remaining are being completed and freed.
  6790. + */
  6791. +
  6792. + /* Actually free resources. */
  6793. + ssam_cplt_destroy(&ctrl->cplt);
  6794. + ssh_rtl_destroy(&ctrl->rtl);
  6795. +
  6796. + /*
  6797. + * Set state via write_once even though we expect to be locked/in an
  6798. + * exclusive context, due to smoke-testing in
  6799. + * ssam_request_sync_submit().
  6800. + */
  6801. + WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_UNINITIALIZED);
  6802. +}
  6803. +
  6804. +/**
  6805. + * ssam_controller_suspend() - Suspend the controller.
  6806. + * @ctrl: The controller to suspend.
  6807. + *
  6808. + * Marks the controller as suspended. Note that display-off and D0-exit
  6809. + * notifications have to be sent manually before transitioning the controller
  6810. + * into the suspended state via this function.
  6811. + *
  6812. + * See ssam_controller_resume() for the corresponding resume function.
  6813. + *
  6814. + * Return: Returns %-EINVAL if the controller is currently not in the
  6815. + * "started" state.
  6816. + */
  6817. +int ssam_controller_suspend(struct ssam_controller *ctrl)
  6818. +{
  6819. + ssam_controller_lock(ctrl);
  6820. +
  6821. + if (ctrl->state != SSAM_CONTROLLER_STARTED) {
  6822. + ssam_controller_unlock(ctrl);
  6823. + return -EINVAL;
  6824. + }
  6825. +
  6826. + ssam_dbg(ctrl, "pm: suspending controller\n");
  6827. +
  6828. + /*
  6829. + * Set state via write_once even though we're locked, due to
  6830. + * smoke-testing in ssam_request_sync_submit().
  6831. + */
  6832. + WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_SUSPENDED);
  6833. +
  6834. + ssam_controller_unlock(ctrl);
  6835. + return 0;
  6836. +}
  6837. +
  6838. +/**
  6839. + * ssam_controller_resume() - Resume the controller from suspend.
  6840. + * @ctrl: The controller to resume.
  6841. + *
  6842. + * Resume the controller from the suspended state it was put into via
  6843. + * ssam_controller_suspend(). This function does not issue display-on and
  6844. + * D0-entry notifications. If required, those have to be sent manually after
  6845. + * this call.
  6846. + *
  6847. + * Return: Returns %-EINVAL if the controller is currently not suspended.
  6848. + */
  6849. +int ssam_controller_resume(struct ssam_controller *ctrl)
  6850. +{
  6851. + ssam_controller_lock(ctrl);
  6852. +
  6853. + if (ctrl->state != SSAM_CONTROLLER_SUSPENDED) {
  6854. + ssam_controller_unlock(ctrl);
  6855. + return -EINVAL;
  6856. + }
  6857. +
  6858. + ssam_dbg(ctrl, "pm: resuming controller\n");
  6859. +
  6860. + /*
  6861. + * Set state via write_once even though we're locked, due to
  6862. + * smoke-testing in ssam_request_sync_submit().
  6863. + */
  6864. + WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STARTED);
  6865. +
  6866. + ssam_controller_unlock(ctrl);
  6867. + return 0;
  6868. +}
  6869. +
  6870. +
  6871. +/* -- Top-level request interface ------------------------------------------- */
  6872. +
  6873. +/**
  6874. + * ssam_request_write_data() - Construct and write SAM request message to
  6875. + * buffer.
  6876. + * @buf: The buffer to write the data to.
  6877. + * @ctrl: The controller via which the request will be sent.
  6878. + * @spec: The request data and specification.
  6879. + *
  6880. + * Constructs a SAM/SSH request message and writes it to the provided buffer.
  6881. + * The request and transport counters, specifically RQID and SEQ, will be set
  6882. + * in this call. These counters are obtained from the controller. It is thus
  6883. + * only valid to send the resulting message via the controller specified here.
  6884. + *
  6885. + * For calculation of the required buffer size, refer to the
  6886. + * SSH_COMMAND_MESSAGE_LENGTH() macro.
  6887. + *
  6888. + * Return: Returns the number of bytes used in the buffer on success. Returns
  6889. + * %-EINVAL if the payload length provided in the request specification is too
  6890. + * large (larger than %SSH_COMMAND_MAX_PAYLOAD_SIZE) or if the provided buffer
  6891. + * is too small.
  6892. + */
  6893. +ssize_t ssam_request_write_data(struct ssam_span *buf,
  6894. + struct ssam_controller *ctrl,
  6895. + const struct ssam_request *spec)
  6896. +{
  6897. + struct msgbuf msgb;
  6898. + u16 rqid;
  6899. + u8 seq;
  6900. +
  6901. + if (spec->length > SSH_COMMAND_MAX_PAYLOAD_SIZE)
  6902. + return -EINVAL;
  6903. +
  6904. + if (SSH_COMMAND_MESSAGE_LENGTH(spec->length) > buf->len)
  6905. + return -EINVAL;
  6906. +
  6907. + msgb_init(&msgb, buf->ptr, buf->len);
  6908. + seq = ssh_seq_next(&ctrl->counter.seq);
  6909. + rqid = ssh_rqid_next(&ctrl->counter.rqid);
  6910. + msgb_push_cmd(&msgb, seq, rqid, spec);
  6911. +
  6912. + return msgb_bytes_used(&msgb);
  6913. +}
  6914. +EXPORT_SYMBOL_GPL(ssam_request_write_data);
  6915. +
  6916. +static void ssam_request_sync_complete(struct ssh_request *rqst,
  6917. + const struct ssh_command *cmd,
  6918. + const struct ssam_span *data, int status)
  6919. +{
  6920. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  6921. + struct ssam_request_sync *r;
  6922. +
  6923. + r = container_of(rqst, struct ssam_request_sync, base);
  6924. + r->status = status;
  6925. +
  6926. + if (r->resp)
  6927. + r->resp->length = 0;
  6928. +
  6929. + if (status) {
  6930. + rtl_dbg_cond(rtl, "rsp: request failed: %d\n", status);
  6931. + return;
  6932. + }
  6933. +
  6934. + if (!data) /* Handle requests without a response. */
  6935. + return;
  6936. +
  6937. + if (!r->resp || !r->resp->pointer) {
  6938. + if (data->len)
  6939. + rtl_warn(rtl, "rsp: no response buffer provided, dropping data\n");
  6940. + return;
  6941. + }
  6942. +
  6943. + if (data->len > r->resp->capacity) {
  6944. + rtl_err(rtl,
  6945. + "rsp: response buffer too small, capacity: %zu bytes, got: %zu bytes\n",
  6946. + r->resp->capacity, data->len);
  6947. + r->status = -ENOSPC;
  6948. + return;
  6949. + }
  6950. +
  6951. + r->resp->length = data->len;
  6952. + memcpy(r->resp->pointer, data->ptr, data->len);
  6953. +}
  6954. +
  6955. +static void ssam_request_sync_release(struct ssh_request *rqst)
  6956. +{
  6957. + complete_all(&container_of(rqst, struct ssam_request_sync, base)->comp);
  6958. +}
  6959. +
  6960. +static const struct ssh_request_ops ssam_request_sync_ops = {
  6961. + .release = ssam_request_sync_release,
  6962. + .complete = ssam_request_sync_complete,
  6963. +};
  6964. +
  6965. +/**
  6966. + * ssam_request_sync_alloc() - Allocate a synchronous request.
  6967. + * @payload_len: The length of the request payload.
  6968. + * @flags: Flags used for allocation.
  6969. + * @rqst: Where to store the pointer to the allocated request.
  6970. + * @buffer: Where to store the buffer descriptor for the message buffer of
  6971. + * the request.
  6972. + *
  6973. + * Allocates a synchronous request with corresponding message buffer. The
  6974. + * request still needs to be initialized ssam_request_sync_init() before
  6975. + * it can be submitted, and the message buffer data must still be set to the
  6976. + * returned buffer via ssam_request_sync_set_data() after it has been filled,
  6977. + * if need be with adjusted message length.
  6978. + *
  6979. + * After use, the request and its corresponding message buffer should be freed
  6980. + * via ssam_request_sync_free(). The buffer must not be freed separately.
  6981. + *
  6982. + * Return: Returns zero on success, %-ENOMEM if the request could not be
  6983. + * allocated.
  6984. + */
  6985. +int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
  6986. + struct ssam_request_sync **rqst,
  6987. + struct ssam_span *buffer)
  6988. +{
  6989. + size_t msglen = SSH_COMMAND_MESSAGE_LENGTH(payload_len);
  6990. +
  6991. + *rqst = kzalloc(sizeof(**rqst) + msglen, flags);
  6992. + if (!*rqst)
  6993. + return -ENOMEM;
  6994. +
  6995. + buffer->ptr = (u8 *)(*rqst + 1);
  6996. + buffer->len = msglen;
  6997. +
  6998. + return 0;
  6999. +}
  7000. +EXPORT_SYMBOL_GPL(ssam_request_sync_alloc);
  7001. +
  7002. +/**
  7003. + * ssam_request_sync_free() - Free a synchronous request.
  7004. + * @rqst: The request to be freed.
  7005. + *
  7006. + * Free a synchronous request and its corresponding buffer allocated with
  7007. + * ssam_request_sync_alloc(). Do not use for requests allocated on the stack
  7008. + * or via any other function.
  7009. + *
  7010. + * Warning: The caller must ensure that the request is not in use any more.
  7011. + * I.e. the caller must ensure that it has the only reference to the request
  7012. + * and the request is not currently pending. This means that the caller has
  7013. + * either never submitted the request, request submission has failed, or the
  7014. + * caller has waited until the submitted request has been completed via
  7015. + * ssam_request_sync_wait().
  7016. + */
  7017. +void ssam_request_sync_free(struct ssam_request_sync *rqst)
  7018. +{
  7019. + kfree(rqst);
  7020. +}
  7021. +EXPORT_SYMBOL_GPL(ssam_request_sync_free);
  7022. +
  7023. +/**
  7024. + * ssam_request_sync_init() - Initialize a synchronous request struct.
  7025. + * @rqst: The request to initialize.
  7026. + * @flags: The request flags.
  7027. + *
  7028. + * Initializes the given request struct. Does not initialize the request
  7029. + * message data. This has to be done explicitly after this call via
  7030. + * ssam_request_sync_set_data() and the actual message data has to be written
  7031. + * via ssam_request_write_data().
  7032. + *
  7033. + * Return: Returns zero on success or %-EINVAL if the given flags are invalid.
  7034. + */
  7035. +int ssam_request_sync_init(struct ssam_request_sync *rqst,
  7036. + enum ssam_request_flags flags)
  7037. +{
  7038. + int status;
  7039. +
  7040. + status = ssh_request_init(&rqst->base, flags, &ssam_request_sync_ops);
  7041. + if (status)
  7042. + return status;
  7043. +
  7044. + init_completion(&rqst->comp);
  7045. + rqst->resp = NULL;
  7046. + rqst->status = 0;
  7047. +
  7048. + return 0;
  7049. +}
  7050. +EXPORT_SYMBOL_GPL(ssam_request_sync_init);
  7051. +
  7052. +/**
  7053. + * ssam_request_sync_submit() - Submit a synchronous request.
  7054. + * @ctrl: The controller with which to submit the request.
  7055. + * @rqst: The request to submit.
  7056. + *
  7057. + * Submit a synchronous request. The request has to be initialized and
  7058. + * properly set up, including response buffer (may be %NULL if no response is
  7059. + * expected) and command message data. This function does not wait for the
  7060. + * request to be completed.
  7061. + *
  7062. + * If this function succeeds, ssam_request_sync_wait() must be used to ensure
  7063. + * that the request has been completed before the response data can be
  7064. + * accessed and/or the request can be freed. On failure, the request may
  7065. + * immediately be freed.
  7066. + *
  7067. + * This function may only be used if the controller is active, i.e. has been
  7068. + * initialized and not suspended.
  7069. + */
  7070. +int ssam_request_sync_submit(struct ssam_controller *ctrl,
  7071. + struct ssam_request_sync *rqst)
  7072. +{
  7073. + int status;
  7074. +
  7075. + /*
  7076. + * This is only a superficial check. In general, the caller needs to
  7077. + * ensure that the controller is initialized and is not (and does not
  7078. + * get) suspended during use, i.e. until the request has been completed
  7079. + * (if _absolutely_ necessary, by use of ssam_controller_statelock/
  7080. + * ssam_controller_stateunlock, but something like ssam_client_link
  7081. + * should be preferred as this needs to last until the request has been
  7082. + * completed).
  7083. + *
  7084. + * Note that it is actually safe to use this function while the
  7085. + * controller is in the process of being shut down (as ssh_rtl_submit
  7086. + * is safe with regards to this), but it is generally discouraged to do
  7087. + * so.
  7088. + */
  7089. + if (WARN_ON(READ_ONCE(ctrl->state) != SSAM_CONTROLLER_STARTED)) {
  7090. + ssh_request_put(&rqst->base);
  7091. + return -ENODEV;
  7092. + }
  7093. +
  7094. + status = ssh_rtl_submit(&ctrl->rtl, &rqst->base);
  7095. + ssh_request_put(&rqst->base);
  7096. +
  7097. + return status;
  7098. +}
  7099. +EXPORT_SYMBOL_GPL(ssam_request_sync_submit);
  7100. +
  7101. +/**
  7102. + * ssam_request_sync() - Execute a synchronous request.
  7103. + * @ctrl: The controller via which the request will be submitted.
  7104. + * @spec: The request specification and payload.
  7105. + * @rsp: The response buffer.
  7106. + *
  7107. + * Allocates a synchronous request with its message data buffer on the heap
  7108. + * via ssam_request_sync_alloc(), fully initializes it via the provided
  7109. + * request specification, submits it, and finally waits for its completion
  7110. + * before freeing it and returning its status.
  7111. + *
  7112. + * Return: Returns the status of the request or any failure during setup.
  7113. + */
  7114. +int ssam_request_sync(struct ssam_controller *ctrl,
  7115. + const struct ssam_request *spec,
  7116. + struct ssam_response *rsp)
  7117. +{
  7118. + struct ssam_request_sync *rqst;
  7119. + struct ssam_span buf;
  7120. + ssize_t len;
  7121. + int status;
  7122. +
  7123. + status = ssam_request_sync_alloc(spec->length, GFP_KERNEL, &rqst, &buf);
  7124. + if (status)
  7125. + return status;
  7126. +
  7127. + status = ssam_request_sync_init(rqst, spec->flags);
  7128. + if (status)
  7129. + return status;
  7130. +
  7131. + ssam_request_sync_set_resp(rqst, rsp);
  7132. +
  7133. + len = ssam_request_write_data(&buf, ctrl, spec);
  7134. + if (len < 0) {
  7135. + ssam_request_sync_free(rqst);
  7136. + return len;
  7137. + }
  7138. +
  7139. + ssam_request_sync_set_data(rqst, buf.ptr, len);
  7140. +
  7141. + status = ssam_request_sync_submit(ctrl, rqst);
  7142. + if (!status)
  7143. + status = ssam_request_sync_wait(rqst);
  7144. +
  7145. + ssam_request_sync_free(rqst);
  7146. + return status;
  7147. +}
  7148. +EXPORT_SYMBOL_GPL(ssam_request_sync);
  7149. +
  7150. +/**
  7151. + * ssam_request_sync_with_buffer() - Execute a synchronous request with the
  7152. + * provided buffer as back-end for the message buffer.
  7153. + * @ctrl: The controller via which the request will be submitted.
  7154. + * @spec: The request specification and payload.
  7155. + * @rsp: The response buffer.
  7156. + * @buf: The buffer for the request message data.
  7157. + *
  7158. + * Allocates a synchronous request struct on the stack, fully initializes it
  7159. + * using the provided buffer as message data buffer, submits it, and then
  7160. + * waits for its completion before returning its status. The
  7161. + * SSH_COMMAND_MESSAGE_LENGTH() macro can be used to compute the required
  7162. + * message buffer size.
  7163. + *
  7164. + * This function does essentially the same as ssam_request_sync(), but instead
  7165. + * of dynamically allocating the request and message data buffer, it uses the
  7166. + * provided message data buffer and stores the (small) request struct on the
  7167. + * heap.
  7168. + *
  7169. + * Return: Returns the status of the request or any failure during setup.
  7170. + */
  7171. +int ssam_request_sync_with_buffer(struct ssam_controller *ctrl,
  7172. + const struct ssam_request *spec,
  7173. + struct ssam_response *rsp,
  7174. + struct ssam_span *buf)
  7175. +{
  7176. + struct ssam_request_sync rqst;
  7177. + ssize_t len;
  7178. + int status;
  7179. +
  7180. + status = ssam_request_sync_init(&rqst, spec->flags);
  7181. + if (status)
  7182. + return status;
  7183. +
  7184. + ssam_request_sync_set_resp(&rqst, rsp);
  7185. +
  7186. + len = ssam_request_write_data(buf, ctrl, spec);
  7187. + if (len < 0)
  7188. + return len;
  7189. +
  7190. + ssam_request_sync_set_data(&rqst, buf->ptr, len);
  7191. +
  7192. + status = ssam_request_sync_submit(ctrl, &rqst);
  7193. + if (!status)
  7194. + status = ssam_request_sync_wait(&rqst);
  7195. +
  7196. + return status;
  7197. +}
  7198. +EXPORT_SYMBOL_GPL(ssam_request_sync_with_buffer);
  7199. +
  7200. +
  7201. +/* -- Internal SAM requests. ------------------------------------------------ */
  7202. +
  7203. +SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_get_firmware_version, __le32, {
  7204. + .target_category = SSAM_SSH_TC_SAM,
  7205. + .target_id = 0x01,
  7206. + .command_id = 0x13,
  7207. + .instance_id = 0x00,
  7208. +});
  7209. +
  7210. +SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_off, u8, {
  7211. + .target_category = SSAM_SSH_TC_SAM,
  7212. + .target_id = 0x01,
  7213. + .command_id = 0x15,
  7214. + .instance_id = 0x00,
  7215. +});
  7216. +
  7217. +SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_on, u8, {
  7218. + .target_category = SSAM_SSH_TC_SAM,
  7219. + .target_id = 0x01,
  7220. + .command_id = 0x16,
  7221. + .instance_id = 0x00,
  7222. +});
  7223. +
  7224. +SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_exit, u8, {
  7225. + .target_category = SSAM_SSH_TC_SAM,
  7226. + .target_id = 0x01,
  7227. + .command_id = 0x33,
  7228. + .instance_id = 0x00,
  7229. +});
  7230. +
  7231. +SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_entry, u8, {
  7232. + .target_category = SSAM_SSH_TC_SAM,
  7233. + .target_id = 0x01,
  7234. + .command_id = 0x34,
  7235. + .instance_id = 0x00,
  7236. +});
  7237. +
  7238. +/**
  7239. + * struct ssh_notification_params - Command payload to enable/disable SSH
  7240. + * notifications.
  7241. + * @target_category: The target category for which notifications should be
  7242. + * enabled/disabled.
  7243. + * @flags: Flags determining how notifications are being sent.
  7244. + * @request_id: The request ID that is used to send these notifications.
  7245. + * @instance_id: The specific instance in the given target category for
  7246. + * which notifications should be enabled.
  7247. + */
  7248. +struct ssh_notification_params {
  7249. + u8 target_category;
  7250. + u8 flags;
  7251. + __le16 request_id;
  7252. + u8 instance_id;
  7253. +} __packed;
  7254. +
  7255. +static_assert(sizeof(struct ssh_notification_params) == 5);
  7256. +
  7257. +static int __ssam_ssh_event_request(struct ssam_controller *ctrl,
  7258. + struct ssam_event_registry reg, u8 cid,
  7259. + struct ssam_event_id id, u8 flags)
  7260. +{
  7261. + struct ssh_notification_params params;
  7262. + struct ssam_request rqst;
  7263. + struct ssam_response result;
  7264. + int status;
  7265. +
  7266. + u16 rqid = ssh_tc_to_rqid(id.target_category);
  7267. + u8 buf = 0;
  7268. +
  7269. + /* Only allow RQIDs that lie within the event spectrum. */
  7270. + if (!ssh_rqid_is_event(rqid))
  7271. + return -EINVAL;
  7272. +
  7273. + params.target_category = id.target_category;
  7274. + params.instance_id = id.instance;
  7275. + params.flags = flags;
  7276. + put_unaligned_le16(rqid, &params.request_id);
  7277. +
  7278. + rqst.target_category = reg.target_category;
  7279. + rqst.target_id = reg.target_id;
  7280. + rqst.command_id = cid;
  7281. + rqst.instance_id = 0x00;
  7282. + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
  7283. + rqst.length = sizeof(params);
  7284. + rqst.payload = (u8 *)&params;
  7285. +
  7286. + result.capacity = sizeof(buf);
  7287. + result.length = 0;
  7288. + result.pointer = &buf;
  7289. +
  7290. + status = ssam_retry(ssam_request_sync_onstack, ctrl, &rqst, &result,
  7291. + sizeof(params));
  7292. +
  7293. + return status < 0 ? status : buf;
  7294. +}
  7295. +
  7296. +/**
  7297. + * ssam_ssh_event_enable() - Enable SSH event.
  7298. + * @ctrl: The controller for which to enable the event.
  7299. + * @reg: The event registry describing what request to use for enabling and
  7300. + * disabling the event.
  7301. + * @id: The event identifier.
  7302. + * @flags: The event flags.
  7303. + *
  7304. + * Enables the specified event on the EC. This function does not manage
  7305. + * reference counting of enabled events and is basically only a wrapper for
  7306. + * the raw EC request. If the specified event is already enabled, the EC will
  7307. + * ignore this request.
  7308. + *
  7309. + * Return: Returns the status of the executed SAM request (zero on success and
  7310. + * negative on direct failure) or %-EPROTO if the request response indicates a
  7311. + * failure.
  7312. + */
  7313. +static int ssam_ssh_event_enable(struct ssam_controller *ctrl,
  7314. + struct ssam_event_registry reg,
  7315. + struct ssam_event_id id, u8 flags)
  7316. +{
  7317. + int status;
  7318. +
  7319. + status = __ssam_ssh_event_request(ctrl, reg, reg.cid_enable, id, flags);
  7320. +
  7321. + if (status < 0 && status != -EINVAL) {
  7322. + ssam_err(ctrl,
  7323. + "failed to enable event source (tc: %#04x, iid: %#04x, reg: %#04x)\n",
  7324. + id.target_category, id.instance, reg.target_category);
  7325. + }
  7326. +
  7327. + if (status > 0) {
  7328. + ssam_err(ctrl,
  7329. + "unexpected result while enabling event source: %#04x (tc: %#04x, iid: %#04x, reg: %#04x)\n",
  7330. + status, id.target_category, id.instance, reg.target_category);
  7331. + return -EPROTO;
  7332. + }
  7333. +
  7334. + return status;
  7335. +}
  7336. +
  7337. +/**
  7338. + * ssam_ssh_event_disable() - Disable SSH event.
  7339. + * @ctrl: The controller for which to disable the event.
  7340. + * @reg: The event registry describing what request to use for enabling and
  7341. + * disabling the event (must be same as used when enabling the event).
  7342. + * @id: The event identifier.
  7343. + * @flags: The event flags (likely ignored for disabling of events).
  7344. + *
  7345. + * Disables the specified event on the EC. This function does not manage
  7346. + * reference counting of enabled events and is basically only a wrapper for
  7347. + * the raw EC request. If the specified event is already disabled, the EC will
  7348. + * ignore this request.
  7349. + *
  7350. + * Return: Returns the status of the executed SAM request (zero on success and
  7351. + * negative on direct failure) or %-EPROTO if the request response indicates a
  7352. + * failure.
  7353. + */
  7354. +static int ssam_ssh_event_disable(struct ssam_controller *ctrl,
  7355. + struct ssam_event_registry reg,
  7356. + struct ssam_event_id id, u8 flags)
  7357. +{
  7358. + int status;
  7359. +
  7360. + status = __ssam_ssh_event_request(ctrl, reg, reg.cid_disable, id, flags);
  7361. +
  7362. + if (status < 0 && status != -EINVAL) {
  7363. + ssam_err(ctrl,
  7364. + "failed to disable event source (tc: %#04x, iid: %#04x, reg: %#04x)\n",
  7365. + id.target_category, id.instance, reg.target_category);
  7366. + }
  7367. +
  7368. + if (status > 0) {
  7369. + ssam_err(ctrl,
  7370. + "unexpected result while disabling event source: %#04x (tc: %#04x, iid: %#04x, reg: %#04x)\n",
  7371. + status, id.target_category, id.instance, reg.target_category);
  7372. + return -EPROTO;
  7373. + }
  7374. +
  7375. + return status;
  7376. +}
  7377. +
  7378. +
  7379. +/* -- Wrappers for internal SAM requests. ----------------------------------- */
  7380. +
  7381. +/**
  7382. + * ssam_get_firmware_version() - Get the SAM/EC firmware version.
  7383. + * @ctrl: The controller.
  7384. + * @version: Where to store the version number.
  7385. + *
  7386. + * Return: Returns zero on success or the status of the executed SAM request
  7387. + * if that request failed.
  7388. + */
  7389. +int ssam_get_firmware_version(struct ssam_controller *ctrl, u32 *version)
  7390. +{
  7391. + __le32 __version;
  7392. + int status;
  7393. +
  7394. + status = ssam_retry(ssam_ssh_get_firmware_version, ctrl, &__version);
  7395. + if (status)
  7396. + return status;
  7397. +
  7398. + *version = le32_to_cpu(__version);
  7399. + return 0;
  7400. +}
  7401. +
  7402. +/**
  7403. + * ssam_ctrl_notif_display_off() - Notify EC that the display has been turned
  7404. + * off.
  7405. + * @ctrl: The controller.
  7406. + *
  7407. + * Notify the EC that the display has been turned off and the driver may enter
  7408. + * a lower-power state. This will prevent events from being sent directly.
  7409. + * Rather, the EC signals an event by pulling the wakeup GPIO high for as long
  7410. + * as there are pending events. The events then need to be manually released,
  7411. + * one by one, via the GPIO callback request. All pending events accumulated
  7412. + * during this state can also be released by issuing the display-on
  7413. + * notification, e.g. via ssam_ctrl_notif_display_on(), which will also reset
  7414. + * the GPIO.
  7415. + *
  7416. + * On some devices, specifically ones with an integrated keyboard, the keyboard
  7417. + * backlight will be turned off by this call.
  7418. + *
  7419. + * This function will only send the display-off notification command if
  7420. + * display notifications are supported by the EC. Currently all known devices
  7421. + * support these notifications.
  7422. + *
  7423. + * Use ssam_ctrl_notif_display_on() to reverse the effects of this function.
  7424. + *
  7425. + * Return: Returns zero on success or if no request has been executed, the
  7426. + * status of the executed SAM request if that request failed, or %-EPROTO if
  7427. + * an unexpected response has been received.
  7428. + */
  7429. +int ssam_ctrl_notif_display_off(struct ssam_controller *ctrl)
  7430. +{
  7431. + int status;
  7432. + u8 response;
  7433. +
  7434. + ssam_dbg(ctrl, "pm: notifying display off\n");
  7435. +
  7436. + status = ssam_retry(ssam_ssh_notif_display_off, ctrl, &response);
  7437. + if (status)
  7438. + return status;
  7439. +
  7440. + if (response != 0) {
  7441. + ssam_err(ctrl, "unexpected response from display-off notification: %#04x\n",
  7442. + response);
  7443. + return -EPROTO;
  7444. + }
  7445. +
  7446. + return 0;
  7447. +}
  7448. +
  7449. +/**
  7450. + * ssam_ctrl_notif_display_on() - Notify EC that the display has been turned on.
  7451. + * @ctrl: The controller.
  7452. + *
  7453. + * Notify the EC that the display has been turned back on and the driver has
  7454. + * exited its lower-power state. This notification is the counterpart to the
  7455. + * display-off notification sent via ssam_ctrl_notif_display_off() and will
  7456. + * reverse its effects, including resetting events to their default behavior.
  7457. + *
  7458. + * This function will only send the display-on notification command if display
  7459. + * notifications are supported by the EC. Currently all known devices support
  7460. + * these notifications.
  7461. + *
  7462. + * See ssam_ctrl_notif_display_off() for more details.
  7463. + *
  7464. + * Return: Returns zero on success or if no request has been executed, the
  7465. + * status of the executed SAM request if that request failed, or %-EPROTO if
  7466. + * an unexpected response has been received.
  7467. + */
  7468. +int ssam_ctrl_notif_display_on(struct ssam_controller *ctrl)
  7469. +{
  7470. + int status;
  7471. + u8 response;
  7472. +
  7473. + ssam_dbg(ctrl, "pm: notifying display on\n");
  7474. +
  7475. + status = ssam_retry(ssam_ssh_notif_display_on, ctrl, &response);
  7476. + if (status)
  7477. + return status;
  7478. +
  7479. + if (response != 0) {
  7480. + ssam_err(ctrl, "unexpected response from display-on notification: %#04x\n",
  7481. + response);
  7482. + return -EPROTO;
  7483. + }
  7484. +
  7485. + return 0;
  7486. +}
  7487. +
  7488. +/**
  7489. + * ssam_ctrl_notif_d0_exit() - Notify EC that the driver/device exits the D0
  7490. + * power state.
  7491. + * @ctrl: The controller
  7492. + *
  7493. + * Notifies the EC that the driver prepares to exit the D0 power state in
  7494. + * favor of a lower-power state. Exact effects of this function related to the
  7495. + * EC are currently unknown.
  7496. + *
  7497. + * This function will only send the D0-exit notification command if D0-state
  7498. + * notifications are supported by the EC. Only newer Surface generations
  7499. + * support these notifications.
  7500. + *
  7501. + * Use ssam_ctrl_notif_d0_entry() to reverse the effects of this function.
  7502. + *
  7503. + * Return: Returns zero on success or if no request has been executed, the
  7504. + * status of the executed SAM request if that request failed, or %-EPROTO if
  7505. + * an unexpected response has been received.
  7506. + */
  7507. +int ssam_ctrl_notif_d0_exit(struct ssam_controller *ctrl)
  7508. +{
  7509. + int status;
  7510. + u8 response;
  7511. +
  7512. + if (!ctrl->caps.d3_closes_handle)
  7513. + return 0;
  7514. +
  7515. + ssam_dbg(ctrl, "pm: notifying D0 exit\n");
  7516. +
  7517. + status = ssam_retry(ssam_ssh_notif_d0_exit, ctrl, &response);
  7518. + if (status)
  7519. + return status;
  7520. +
  7521. + if (response != 0) {
  7522. + ssam_err(ctrl, "unexpected response from D0-exit notification: %#04x\n",
  7523. + response);
  7524. + return -EPROTO;
  7525. + }
  7526. +
  7527. + return 0;
  7528. +}
  7529. +
  7530. +/**
  7531. + * ssam_ctrl_notif_d0_entry() - Notify EC that the driver/device enters the D0
  7532. + * power state.
  7533. + * @ctrl: The controller
  7534. + *
  7535. + * Notifies the EC that the driver has exited a lower-power state and entered
  7536. + * the D0 power state. Exact effects of this function related to the EC are
  7537. + * currently unknown.
  7538. + *
  7539. + * This function will only send the D0-entry notification command if D0-state
  7540. + * notifications are supported by the EC. Only newer Surface generations
  7541. + * support these notifications.
  7542. + *
  7543. + * See ssam_ctrl_notif_d0_exit() for more details.
  7544. + *
  7545. + * Return: Returns zero on success or if no request has been executed, the
  7546. + * status of the executed SAM request if that request failed, or %-EPROTO if
  7547. + * an unexpected response has been received.
  7548. + */
  7549. +int ssam_ctrl_notif_d0_entry(struct ssam_controller *ctrl)
  7550. +{
  7551. + int status;
  7552. + u8 response;
  7553. +
  7554. + if (!ctrl->caps.d3_closes_handle)
  7555. + return 0;
  7556. +
  7557. + ssam_dbg(ctrl, "pm: notifying D0 entry\n");
  7558. +
  7559. + status = ssam_retry(ssam_ssh_notif_d0_entry, ctrl, &response);
  7560. + if (status)
  7561. + return status;
  7562. +
  7563. + if (response != 0) {
  7564. + ssam_err(ctrl, "unexpected response from D0-entry notification: %#04x\n",
  7565. + response);
  7566. + return -EPROTO;
  7567. + }
  7568. +
  7569. + return 0;
  7570. +}
  7571. +
  7572. +
  7573. +/* -- Top-level event registry interface. ----------------------------------- */
  7574. +
  7575. +/**
  7576. + * ssam_nf_refcount_enable() - Enable event for reference count entry if it has
  7577. + * not already been enabled.
  7578. + * @ctrl: The controller to enable the event on.
  7579. + * @entry: The reference count entry for the event to be enabled.
  7580. + * @flags: The flags used for enabling the event on the EC.
  7581. + *
  7582. + * Enable the event associated with the given reference count entry if the
  7583. + * reference count equals one, i.e. the event has not previously been enabled.
  7584. + * If the event has already been enabled (i.e. reference count not equal to
  7585. + * one), check that the flags used for enabling match and warn about this if
  7586. + * they do not.
  7587. + *
  7588. + * This does not modify the reference count itself, which is done with
  7589. + * ssam_nf_refcount_inc() / ssam_nf_refcount_dec().
  7590. + *
  7591. + * Note: ``nf->lock`` must be held when calling this function.
  7592. + *
  7593. + * Return: Returns zero on success. If the event is enabled by this call,
  7594. + * returns the status of the event-enable EC command.
  7595. + */
  7596. +static int ssam_nf_refcount_enable(struct ssam_controller *ctrl,
  7597. + struct ssam_nf_refcount_entry *entry, u8 flags)
  7598. +{
  7599. + const struct ssam_event_registry reg = entry->key.reg;
  7600. + const struct ssam_event_id id = entry->key.id;
  7601. + struct ssam_nf *nf = &ctrl->cplt.event.notif;
  7602. + int status;
  7603. +
  7604. + lockdep_assert_held(&nf->lock);
  7605. +
  7606. + ssam_dbg(ctrl, "enabling event (reg: %#04x, tc: %#04x, iid: %#04x, rc: %d)\n",
  7607. + reg.target_category, id.target_category, id.instance, entry->refcount);
  7608. +
  7609. + if (entry->refcount == 1) {
  7610. + status = ssam_ssh_event_enable(ctrl, reg, id, flags);
  7611. + if (status)
  7612. + return status;
  7613. +
  7614. + entry->flags = flags;
  7615. +
  7616. + } else if (entry->flags != flags) {
  7617. + ssam_warn(ctrl,
  7618. + "inconsistent flags when enabling event: got %#04x, expected %#04x (reg: %#04x, tc: %#04x, iid: %#04x)\n",
  7619. + flags, entry->flags, reg.target_category, id.target_category,
  7620. + id.instance);
  7621. + }
  7622. +
  7623. + return 0;
  7624. +}
  7625. +
  7626. +/**
  7627. + * ssam_nf_refcount_disable_free() - Disable event for reference count entry if it is
  7628. + * no longer in use and free the corresponding entry.
  7629. + * @ctrl: The controller to disable the event on.
  7630. + * @entry: The reference count entry for the event to be disabled.
  7631. + * @flags: The flags used for enabling the event on the EC.
  7632. + *
  7633. + * If the reference count equals zero, i.e. the event is no longer requested by
  7634. + * any client, the event will be disabled and the corresponding reference count
  7635. + * entry freed. The reference count entry must not be used any more after a
  7636. + * call to this function.
  7637. + *
  7638. + * Also checks if the flags used for disabling the event match the flags used
  7639. + * for enabling the event and warns if they do not (regardless of reference
  7640. + * count).
  7641. + *
  7642. + * This does not modify the reference count itself, which is done with
  7643. + * ssam_nf_refcount_inc() / ssam_nf_refcount_dec().
  7644. + *
  7645. + * Note: ``nf->lock`` must be held when calling this function.
  7646. + *
  7647. + * Return: Returns zero on success. If the event is disabled by this call,
  7648. + * returns the status of the event-enable EC command.
  7649. + */
  7650. +static int ssam_nf_refcount_disable_free(struct ssam_controller *ctrl,
  7651. + struct ssam_nf_refcount_entry *entry, u8 flags)
  7652. +{
  7653. + const struct ssam_event_registry reg = entry->key.reg;
  7654. + const struct ssam_event_id id = entry->key.id;
  7655. + struct ssam_nf *nf = &ctrl->cplt.event.notif;
  7656. + int status = 0;
  7657. +
  7658. + lockdep_assert_held(&nf->lock);
  7659. +
  7660. + ssam_dbg(ctrl, "disabling event (reg: %#04x, tc: %#04x, iid: %#04x, rc: %d)\n",
  7661. + reg.target_category, id.target_category, id.instance, entry->refcount);
  7662. +
  7663. + if (entry->flags != flags) {
  7664. + ssam_warn(ctrl,
  7665. + "inconsistent flags when disabling event: got %#04x, expected %#04x (reg: %#04x, tc: %#04x, iid: %#04x)\n",
  7666. + flags, entry->flags, reg.target_category, id.target_category,
  7667. + id.instance);
  7668. + }
  7669. +
  7670. + if (entry->refcount == 0) {
  7671. + status = ssam_ssh_event_disable(ctrl, reg, id, flags);
  7672. + kfree(entry);
  7673. + }
  7674. +
  7675. + return status;
  7676. +}
  7677. +
  7678. +/**
  7679. + * ssam_notifier_register() - Register an event notifier.
  7680. + * @ctrl: The controller to register the notifier on.
  7681. + * @n: The event notifier to register.
  7682. + *
  7683. + * Register an event notifier. Increment the usage counter of the associated
  7684. + * SAM event if the notifier is not marked as an observer. If the event is not
  7685. + * marked as an observer and is currently not enabled, it will be enabled
  7686. + * during this call. If the notifier is marked as an observer, no attempt will
  7687. + * be made at enabling any event and no reference count will be modified.
  7688. + *
  7689. + * Notifiers marked as observers do not need to be associated with one specific
  7690. + * event, i.e. as long as no event matching is performed, only the event target
  7691. + * category needs to be set.
  7692. + *
  7693. + * Return: Returns zero on success, %-ENOSPC if there have already been
  7694. + * %INT_MAX notifiers for the event ID/type associated with the notifier block
  7695. + * registered, %-ENOMEM if the corresponding event entry could not be
  7696. + * allocated. If this is the first time that a notifier block is registered
  7697. + * for the specific associated event, returns the status of the event-enable
  7698. + * EC-command.
  7699. + */
  7700. +int ssam_notifier_register(struct ssam_controller *ctrl, struct ssam_event_notifier *n)
  7701. +{
  7702. + u16 rqid = ssh_tc_to_rqid(n->event.id.target_category);
  7703. + struct ssam_nf_refcount_entry *entry = NULL;
  7704. + struct ssam_nf_head *nf_head;
  7705. + struct ssam_nf *nf;
  7706. + int status;
  7707. +
  7708. + if (!ssh_rqid_is_event(rqid))
  7709. + return -EINVAL;
  7710. +
  7711. + nf = &ctrl->cplt.event.notif;
  7712. + nf_head = &nf->head[ssh_rqid_to_event(rqid)];
  7713. +
  7714. + mutex_lock(&nf->lock);
  7715. +
  7716. + if (!(n->flags & SSAM_EVENT_NOTIFIER_OBSERVER)) {
  7717. + entry = ssam_nf_refcount_inc(nf, n->event.reg, n->event.id);
  7718. + if (IS_ERR(entry)) {
  7719. + mutex_unlock(&nf->lock);
  7720. + return PTR_ERR(entry);
  7721. + }
  7722. + }
  7723. +
  7724. + status = ssam_nfblk_insert(nf_head, &n->base);
  7725. + if (status) {
  7726. + if (entry)
  7727. + ssam_nf_refcount_dec_free(nf, n->event.reg, n->event.id);
  7728. +
  7729. + mutex_unlock(&nf->lock);
  7730. + return status;
  7731. + }
  7732. +
  7733. + if (entry) {
  7734. + status = ssam_nf_refcount_enable(ctrl, entry, n->event.flags);
  7735. + if (status) {
  7736. + ssam_nfblk_remove(&n->base);
  7737. + ssam_nf_refcount_dec_free(nf, n->event.reg, n->event.id);
  7738. + mutex_unlock(&nf->lock);
  7739. + synchronize_srcu(&nf_head->srcu);
  7740. + return status;
  7741. + }
  7742. + }
  7743. +
  7744. + mutex_unlock(&nf->lock);
  7745. + return 0;
  7746. +}
  7747. +EXPORT_SYMBOL_GPL(ssam_notifier_register);
  7748. +
  7749. +/**
  7750. + * ssam_notifier_unregister() - Unregister an event notifier.
  7751. + * @ctrl: The controller the notifier has been registered on.
  7752. + * @n: The event notifier to unregister.
  7753. + *
  7754. + * Unregister an event notifier. Decrement the usage counter of the associated
  7755. + * SAM event if the notifier is not marked as an observer. If the usage counter
  7756. + * reaches zero, the event will be disabled.
  7757. + *
  7758. + * Return: Returns zero on success, %-ENOENT if the given notifier block has
  7759. + * not been registered on the controller. If the given notifier block was the
  7760. + * last one associated with its specific event, returns the status of the
  7761. + * event-disable EC-command.
  7762. + */
  7763. +int ssam_notifier_unregister(struct ssam_controller *ctrl, struct ssam_event_notifier *n)
  7764. +{
  7765. + u16 rqid = ssh_tc_to_rqid(n->event.id.target_category);
  7766. + struct ssam_nf_refcount_entry *entry;
  7767. + struct ssam_nf_head *nf_head;
  7768. + struct ssam_nf *nf;
  7769. + int status = 0;
  7770. +
  7771. + if (!ssh_rqid_is_event(rqid))
  7772. + return -EINVAL;
  7773. +
  7774. + nf = &ctrl->cplt.event.notif;
  7775. + nf_head = &nf->head[ssh_rqid_to_event(rqid)];
  7776. +
  7777. + mutex_lock(&nf->lock);
  7778. +
  7779. + if (!ssam_nfblk_find(nf_head, &n->base)) {
  7780. + mutex_unlock(&nf->lock);
  7781. + return -ENOENT;
  7782. + }
  7783. +
  7784. + /*
  7785. + * If this is an observer notifier, do not attempt to disable the
  7786. + * event, just remove it.
  7787. + */
  7788. + if (!(n->flags & SSAM_EVENT_NOTIFIER_OBSERVER)) {
  7789. + entry = ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
  7790. + if (WARN_ON(!entry)) {
  7791. + /*
  7792. + * If this does not return an entry, there's a logic
  7793. + * error somewhere: The notifier block is registered,
  7794. + * but the event refcount entry is not there. Remove
  7795. + * the notifier block anyways.
  7796. + */
  7797. + status = -ENOENT;
  7798. + goto remove;
  7799. + }
  7800. +
  7801. + status = ssam_nf_refcount_disable_free(ctrl, entry, n->event.flags);
  7802. + }
  7803. +
  7804. +remove:
  7805. + ssam_nfblk_remove(&n->base);
  7806. + mutex_unlock(&nf->lock);
  7807. + synchronize_srcu(&nf_head->srcu);
  7808. +
  7809. + return status;
  7810. +}
  7811. +EXPORT_SYMBOL_GPL(ssam_notifier_unregister);
  7812. +
  7813. +/**
  7814. + * ssam_controller_event_enable() - Enable the specified event.
  7815. + * @ctrl: The controller to enable the event for.
  7816. + * @reg: The event registry to use for enabling the event.
  7817. + * @id: The event ID specifying the event to be enabled.
  7818. + * @flags: The SAM event flags used for enabling the event.
  7819. + *
  7820. + * Increment the event reference count of the specified event. If the event has
  7821. + * not been enabled previously, it will be enabled by this call.
  7822. + *
  7823. + * Note: In general, ssam_notifier_register() with a non-observer notifier
  7824. + * should be preferred for enabling/disabling events, as this will guarantee
  7825. + * proper ordering and event forwarding in case of errors during event
  7826. + * enabling/disabling.
  7827. + *
  7828. + * Return: Returns zero on success, %-ENOSPC if the reference count for the
  7829. + * specified event has reached its maximum, %-ENOMEM if the corresponding event
  7830. + * entry could not be allocated. If this is the first time that this event has
  7831. + * been enabled (i.e. the reference count was incremented from zero to one by
  7832. + * this call), returns the status of the event-enable EC-command.
  7833. + */
  7834. +int ssam_controller_event_enable(struct ssam_controller *ctrl,
  7835. + struct ssam_event_registry reg,
  7836. + struct ssam_event_id id, u8 flags)
  7837. +{
  7838. + u16 rqid = ssh_tc_to_rqid(id.target_category);
  7839. + struct ssam_nf *nf = &ctrl->cplt.event.notif;
  7840. + struct ssam_nf_refcount_entry *entry;
  7841. + int status;
  7842. +
  7843. + if (!ssh_rqid_is_event(rqid))
  7844. + return -EINVAL;
  7845. +
  7846. + mutex_lock(&nf->lock);
  7847. +
  7848. + entry = ssam_nf_refcount_inc(nf, reg, id);
  7849. + if (IS_ERR(entry)) {
  7850. + mutex_unlock(&nf->lock);
  7851. + return PTR_ERR(entry);
  7852. + }
  7853. +
  7854. + status = ssam_nf_refcount_enable(ctrl, entry, flags);
  7855. + if (status) {
  7856. + ssam_nf_refcount_dec_free(nf, reg, id);
  7857. + mutex_unlock(&nf->lock);
  7858. + return status;
  7859. + }
  7860. +
  7861. + mutex_unlock(&nf->lock);
  7862. + return 0;
  7863. +}
  7864. +EXPORT_SYMBOL_GPL(ssam_controller_event_enable);
  7865. +
  7866. +/**
  7867. + * ssam_controller_event_disable() - Disable the specified event.
  7868. + * @ctrl: The controller to disable the event for.
  7869. + * @reg: The event registry to use for disabling the event.
  7870. + * @id: The event ID specifying the event to be disabled.
  7871. + * @flags: The flags used when enabling the event.
  7872. + *
  7873. + * Decrement the reference count of the specified event. If the reference count
  7874. + * reaches zero, the event will be disabled.
  7875. + *
  7876. + * Note: In general, ssam_notifier_register()/ssam_notifier_unregister() with a
  7877. + * non-observer notifier should be preferred for enabling/disabling events, as
  7878. + * this will guarantee proper ordering and event forwarding in case of errors
  7879. + * during event enabling/disabling.
  7880. + *
  7881. + * Return: Returns zero on success, %-ENOENT if the given event has not been
  7882. + * enabled on the controller. If the reference count of the event reaches zero
  7883. + * during this call, returns the status of the event-disable EC-command.
  7884. + */
  7885. +int ssam_controller_event_disable(struct ssam_controller *ctrl,
  7886. + struct ssam_event_registry reg,
  7887. + struct ssam_event_id id, u8 flags)
  7888. +{
  7889. + u16 rqid = ssh_tc_to_rqid(id.target_category);
  7890. + struct ssam_nf *nf = &ctrl->cplt.event.notif;
  7891. + struct ssam_nf_refcount_entry *entry;
  7892. + int status;
  7893. +
  7894. + if (!ssh_rqid_is_event(rqid))
  7895. + return -EINVAL;
  7896. +
  7897. + mutex_lock(&nf->lock);
  7898. +
  7899. + entry = ssam_nf_refcount_dec(nf, reg, id);
  7900. + if (!entry) {
  7901. + mutex_unlock(&nf->lock);
  7902. + return -ENOENT;
  7903. + }
  7904. +
  7905. + status = ssam_nf_refcount_disable_free(ctrl, entry, flags);
  7906. +
  7907. + mutex_unlock(&nf->lock);
  7908. + return status;
  7909. +}
  7910. +EXPORT_SYMBOL_GPL(ssam_controller_event_disable);
  7911. +
  7912. +/**
  7913. + * ssam_notifier_disable_registered() - Disable events for all registered
  7914. + * notifiers.
  7915. + * @ctrl: The controller for which to disable the notifiers/events.
  7916. + *
  7917. + * Disables events for all currently registered notifiers. In case of an error
  7918. + * (EC command failing), all previously disabled events will be restored and
  7919. + * the error code returned.
  7920. + *
  7921. + * This function is intended to disable all events prior to hibernation entry.
  7922. + * See ssam_notifier_restore_registered() to restore/re-enable all events
  7923. + * disabled with this function.
  7924. + *
  7925. + * Note that this function will not disable events for notifiers registered
  7926. + * after calling this function. It should thus be made sure that no new
  7927. + * notifiers are going to be added after this call and before the corresponding
  7928. + * call to ssam_notifier_restore_registered().
  7929. + *
  7930. + * Return: Returns zero on success. In case of failure returns the error code
  7931. + * returned by the failed EC command to disable an event.
  7932. + */
  7933. +int ssam_notifier_disable_registered(struct ssam_controller *ctrl)
  7934. +{
  7935. + struct ssam_nf *nf = &ctrl->cplt.event.notif;
  7936. + struct rb_node *n;
  7937. + int status;
  7938. +
  7939. + mutex_lock(&nf->lock);
  7940. + for (n = rb_first(&nf->refcount); n; n = rb_next(n)) {
  7941. + struct ssam_nf_refcount_entry *e;
  7942. +
  7943. + e = rb_entry(n, struct ssam_nf_refcount_entry, node);
  7944. + status = ssam_ssh_event_disable(ctrl, e->key.reg,
  7945. + e->key.id, e->flags);
  7946. + if (status)
  7947. + goto err;
  7948. + }
  7949. + mutex_unlock(&nf->lock);
  7950. +
  7951. + return 0;
  7952. +
  7953. +err:
  7954. + for (n = rb_prev(n); n; n = rb_prev(n)) {
  7955. + struct ssam_nf_refcount_entry *e;
  7956. +
  7957. + e = rb_entry(n, struct ssam_nf_refcount_entry, node);
  7958. + ssam_ssh_event_enable(ctrl, e->key.reg, e->key.id, e->flags);
  7959. + }
  7960. + mutex_unlock(&nf->lock);
  7961. +
  7962. + return status;
  7963. +}
  7964. +
  7965. +/**
  7966. + * ssam_notifier_restore_registered() - Restore/re-enable events for all
  7967. + * registered notifiers.
  7968. + * @ctrl: The controller for which to restore the notifiers/events.
  7969. + *
  7970. + * Restores/re-enables all events for which notifiers have been registered on
  7971. + * the given controller. In case of a failure, the error is logged and the
  7972. + * function continues to try and enable the remaining events.
  7973. + *
  7974. + * This function is intended to restore/re-enable all registered events after
  7975. + * hibernation. See ssam_notifier_disable_registered() for the counter part
  7976. + * disabling the events and more details.
  7977. + */
  7978. +void ssam_notifier_restore_registered(struct ssam_controller *ctrl)
  7979. +{
  7980. + struct ssam_nf *nf = &ctrl->cplt.event.notif;
  7981. + struct rb_node *n;
  7982. +
  7983. + mutex_lock(&nf->lock);
  7984. + for (n = rb_first(&nf->refcount); n; n = rb_next(n)) {
  7985. + struct ssam_nf_refcount_entry *e;
  7986. +
  7987. + e = rb_entry(n, struct ssam_nf_refcount_entry, node);
  7988. +
  7989. + /* Ignore errors, will get logged in call. */
  7990. + ssam_ssh_event_enable(ctrl, e->key.reg, e->key.id, e->flags);
  7991. + }
  7992. + mutex_unlock(&nf->lock);
  7993. +}
  7994. +
  7995. +/**
  7996. + * ssam_notifier_is_empty() - Check if there are any registered notifiers.
  7997. + * @ctrl: The controller to check on.
  7998. + *
  7999. + * Return: Returns %true if there are currently no notifiers registered on the
  8000. + * controller, %false otherwise.
  8001. + */
  8002. +static bool ssam_notifier_is_empty(struct ssam_controller *ctrl)
  8003. +{
  8004. + struct ssam_nf *nf = &ctrl->cplt.event.notif;
  8005. + bool result;
  8006. +
  8007. + mutex_lock(&nf->lock);
  8008. + result = ssam_nf_refcount_empty(nf);
  8009. + mutex_unlock(&nf->lock);
  8010. +
  8011. + return result;
  8012. +}
  8013. +
  8014. +/**
  8015. + * ssam_notifier_unregister_all() - Unregister all currently registered
  8016. + * notifiers.
  8017. + * @ctrl: The controller to unregister the notifiers on.
  8018. + *
  8019. + * Unregisters all currently registered notifiers. This function is used to
  8020. + * ensure that all notifiers will be unregistered and associated
  8021. + * entries/resources freed when the controller is being shut down.
  8022. + */
  8023. +static void ssam_notifier_unregister_all(struct ssam_controller *ctrl)
  8024. +{
  8025. + struct ssam_nf *nf = &ctrl->cplt.event.notif;
  8026. + struct ssam_nf_refcount_entry *e, *n;
  8027. +
  8028. + mutex_lock(&nf->lock);
  8029. + rbtree_postorder_for_each_entry_safe(e, n, &nf->refcount, node) {
  8030. + /* Ignore errors, will get logged in call. */
  8031. + ssam_ssh_event_disable(ctrl, e->key.reg, e->key.id, e->flags);
  8032. + kfree(e);
  8033. + }
  8034. + nf->refcount = RB_ROOT;
  8035. + mutex_unlock(&nf->lock);
  8036. +}
  8037. +
  8038. +
  8039. +/* -- Wakeup IRQ. ----------------------------------------------------------- */
  8040. +
  8041. +static irqreturn_t ssam_irq_handle(int irq, void *dev_id)
  8042. +{
  8043. + struct ssam_controller *ctrl = dev_id;
  8044. +
  8045. + ssam_dbg(ctrl, "pm: wake irq triggered\n");
  8046. +
  8047. + /*
  8048. + * Note: Proper wakeup detection is currently unimplemented.
  8049. + * When the EC is in display-off or any other non-D0 state, it
  8050. + * does not send events/notifications to the host. Instead it
  8051. + * signals that there are events available via the wakeup IRQ.
  8052. + * This driver is responsible for calling back to the EC to
  8053. + * release these events one-by-one.
  8054. + *
  8055. + * This IRQ should not cause a full system resume by its own.
  8056. + * Instead, events should be handled by their respective subsystem
  8057. + * drivers, which in turn should signal whether a full system
  8058. + * resume should be performed.
  8059. + *
  8060. + * TODO: Send GPIO callback command repeatedly to EC until callback
  8061. + * returns 0x00. Return flag of callback is "has more events".
  8062. + * Each time the command is sent, one event is "released". Once
  8063. + * all events have been released (return = 0x00), the GPIO is
  8064. + * re-armed. Detect wakeup events during this process, go back to
  8065. + * sleep if no wakeup event has been received.
  8066. + */
  8067. +
  8068. + return IRQ_HANDLED;
  8069. +}
  8070. +
  8071. +/**
  8072. + * ssam_irq_setup() - Set up SAM EC wakeup-GPIO interrupt.
  8073. + * @ctrl: The controller for which the IRQ should be set up.
  8074. + *
  8075. + * Set up an IRQ for the wakeup-GPIO pin of the SAM EC. This IRQ can be used
  8076. + * to wake the device from a low power state.
  8077. + *
  8078. + * Note that this IRQ can only be triggered while the EC is in the display-off
  8079. + * state. In this state, events are not sent to the host in the usual way.
  8080. + * Instead the wakeup-GPIO gets pulled to "high" as long as there are pending
  8081. + * events and these events need to be released one-by-one via the GPIO
  8082. + * callback request, either until there are no events left and the GPIO is
  8083. + * reset, or all at once by transitioning the EC out of the display-off state,
  8084. + * which will also clear the GPIO.
  8085. + *
  8086. + * Not all events, however, should trigger a full system wakeup. Instead the
  8087. + * driver should, if necessary, inspect and forward each event to the
  8088. + * corresponding subsystem, which in turn should decide if the system needs to
  8089. + * be woken up. This logic has not been implemented yet, thus wakeup by this
  8090. + * IRQ should be disabled by default to avoid spurious wake-ups, caused, for
  8091. + * example, by the remaining battery percentage changing. Refer to comments in
  8092. + * this function and comments in the corresponding IRQ handler for more
  8093. + * details on how this should be implemented.
  8094. + *
  8095. + * See also ssam_ctrl_notif_display_off() and ssam_ctrl_notif_display_off()
  8096. + * for functions to transition the EC into and out of the display-off state as
  8097. + * well as more details on it.
  8098. + *
  8099. + * The IRQ is disabled by default and has to be enabled before it can wake up
  8100. + * the device from suspend via ssam_irq_arm_for_wakeup(). On teardown, the IRQ
  8101. + * should be freed via ssam_irq_free().
  8102. + */
  8103. +int ssam_irq_setup(struct ssam_controller *ctrl)
  8104. +{
  8105. + struct device *dev = ssam_controller_device(ctrl);
  8106. + struct gpio_desc *gpiod;
  8107. + int irq;
  8108. + int status;
  8109. +
  8110. + /*
  8111. + * The actual GPIO interrupt is declared in ACPI as TRIGGER_HIGH.
  8112. + * However, the GPIO line only gets reset by sending the GPIO callback
  8113. + * command to SAM (or alternatively the display-on notification). As
  8114. + * proper handling for this interrupt is not implemented yet, leaving
  8115. + * the IRQ at TRIGGER_HIGH would cause an IRQ storm (as the callback
  8116. + * never gets sent and thus the line never gets reset). To avoid this,
  8117. + * mark the IRQ as TRIGGER_RISING for now, only creating a single
  8118. + * interrupt, and let the SAM resume callback during the controller
  8119. + * resume process clear it.
  8120. + */
  8121. + const int irqf = IRQF_SHARED | IRQF_ONESHOT | IRQF_TRIGGER_RISING;
  8122. +
  8123. + gpiod = gpiod_get(dev, "ssam_wakeup-int", GPIOD_ASIS);
  8124. + if (IS_ERR(gpiod))
  8125. + return PTR_ERR(gpiod);
  8126. +
  8127. + irq = gpiod_to_irq(gpiod);
  8128. + gpiod_put(gpiod);
  8129. +
  8130. + if (irq < 0)
  8131. + return irq;
  8132. +
  8133. + status = request_threaded_irq(irq, NULL, ssam_irq_handle, irqf,
  8134. + "ssam_wakeup", ctrl);
  8135. + if (status)
  8136. + return status;
  8137. +
  8138. + ctrl->irq.num = irq;
  8139. + disable_irq(ctrl->irq.num);
  8140. + return 0;
  8141. +}
  8142. +
  8143. +/**
  8144. + * ssam_irq_free() - Free SAM EC wakeup-GPIO interrupt.
  8145. + * @ctrl: The controller for which the IRQ should be freed.
  8146. + *
  8147. + * Free the wakeup-GPIO IRQ previously set-up via ssam_irq_setup().
  8148. + */
  8149. +void ssam_irq_free(struct ssam_controller *ctrl)
  8150. +{
  8151. + free_irq(ctrl->irq.num, ctrl);
  8152. + ctrl->irq.num = -1;
  8153. +}
  8154. +
  8155. +/**
  8156. + * ssam_irq_arm_for_wakeup() - Arm the EC IRQ for wakeup, if enabled.
  8157. + * @ctrl: The controller for which the IRQ should be armed.
  8158. + *
  8159. + * Sets up the IRQ so that it can be used to wake the device. Specifically,
  8160. + * this function enables the irq and then, if the device is allowed to wake up
  8161. + * the system, calls enable_irq_wake(). See ssam_irq_disarm_wakeup() for the
  8162. + * corresponding function to disable the IRQ.
  8163. + *
  8164. + * This function is intended to arm the IRQ before entering S2idle suspend.
  8165. + *
  8166. + * Note: calls to ssam_irq_arm_for_wakeup() and ssam_irq_disarm_wakeup() must
  8167. + * be balanced.
  8168. + */
  8169. +int ssam_irq_arm_for_wakeup(struct ssam_controller *ctrl)
  8170. +{
  8171. + struct device *dev = ssam_controller_device(ctrl);
  8172. + int status;
  8173. +
  8174. + enable_irq(ctrl->irq.num);
  8175. + if (device_may_wakeup(dev)) {
  8176. + status = enable_irq_wake(ctrl->irq.num);
  8177. + if (status) {
  8178. + ssam_err(ctrl, "failed to enable wake IRQ: %d\n", status);
  8179. + disable_irq(ctrl->irq.num);
  8180. + return status;
  8181. + }
  8182. +
  8183. + ctrl->irq.wakeup_enabled = true;
  8184. + } else {
  8185. + ctrl->irq.wakeup_enabled = false;
  8186. + }
  8187. +
  8188. + return 0;
  8189. +}
  8190. +
  8191. +/**
  8192. + * ssam_irq_disarm_wakeup() - Disarm the wakeup IRQ.
  8193. + * @ctrl: The controller for which the IRQ should be disarmed.
  8194. + *
  8195. + * Disarm the IRQ previously set up for wake via ssam_irq_arm_for_wakeup().
  8196. + *
  8197. + * This function is intended to disarm the IRQ after exiting S2idle suspend.
  8198. + *
  8199. + * Note: calls to ssam_irq_arm_for_wakeup() and ssam_irq_disarm_wakeup() must
  8200. + * be balanced.
  8201. + */
  8202. +void ssam_irq_disarm_wakeup(struct ssam_controller *ctrl)
  8203. +{
  8204. + int status;
  8205. +
  8206. + if (ctrl->irq.wakeup_enabled) {
  8207. + status = disable_irq_wake(ctrl->irq.num);
  8208. + if (status)
  8209. + ssam_err(ctrl, "failed to disable wake IRQ: %d\n", status);
  8210. +
  8211. + ctrl->irq.wakeup_enabled = false;
  8212. + }
  8213. + disable_irq(ctrl->irq.num);
  8214. +}
  8215. diff --git a/drivers/platform/x86/surface_aggregator/controller.h b/drivers/platform/x86/surface_aggregator/controller.h
  8216. new file mode 100644
  8217. index 000000000000..a0963c3562ff
  8218. --- /dev/null
  8219. +++ b/drivers/platform/x86/surface_aggregator/controller.h
  8220. @@ -0,0 +1,285 @@
  8221. +/* SPDX-License-Identifier: GPL-2.0+ */
  8222. +/*
  8223. + * Main SSAM/SSH controller structure and functionality.
  8224. + *
  8225. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  8226. + */
  8227. +
  8228. +#ifndef _SURFACE_AGGREGATOR_CONTROLLER_H
  8229. +#define _SURFACE_AGGREGATOR_CONTROLLER_H
  8230. +
  8231. +#include <linux/kref.h>
  8232. +#include <linux/list.h>
  8233. +#include <linux/mutex.h>
  8234. +#include <linux/rbtree.h>
  8235. +#include <linux/rwsem.h>
  8236. +#include <linux/serdev.h>
  8237. +#include <linux/spinlock.h>
  8238. +#include <linux/srcu.h>
  8239. +#include <linux/types.h>
  8240. +#include <linux/workqueue.h>
  8241. +
  8242. +#include <linux/surface_aggregator/controller.h>
  8243. +#include <linux/surface_aggregator/serial_hub.h>
  8244. +
  8245. +#include "ssh_request_layer.h"
  8246. +
  8247. +
  8248. +/* -- Safe counters. -------------------------------------------------------- */
  8249. +
  8250. +/**
  8251. + * struct ssh_seq_counter - Safe counter for SSH sequence IDs.
  8252. + * @value: The current counter value.
  8253. + */
  8254. +struct ssh_seq_counter {
  8255. + u8 value;
  8256. +};
  8257. +
  8258. +/**
  8259. + * struct ssh_rqid_counter - Safe counter for SSH request IDs.
  8260. + * @value: The current counter value.
  8261. + */
  8262. +struct ssh_rqid_counter {
  8263. + u16 value;
  8264. +};
  8265. +
  8266. +
  8267. +/* -- Event/notification system. -------------------------------------------- */
  8268. +
  8269. +/**
  8270. + * struct ssam_nf_head - Notifier head for SSAM events.
  8271. + * @srcu: The SRCU struct for synchronization.
  8272. + * @head: List-head for notifier blocks registered under this head.
  8273. + */
  8274. +struct ssam_nf_head {
  8275. + struct srcu_struct srcu;
  8276. + struct list_head head;
  8277. +};
  8278. +
  8279. +/**
  8280. + * struct ssam_nf - Notifier callback- and activation-registry for SSAM events.
  8281. + * @lock: Lock guarding (de-)registration of notifier blocks. Note: This
  8282. + * lock does not need to be held for notifier calls, only
  8283. + * registration and deregistration.
  8284. + * @refcount: The root of the RB-tree used for reference-counting enabled
  8285. + * events/notifications.
  8286. + * @head: The list of notifier heads for event/notification callbacks.
  8287. + */
  8288. +struct ssam_nf {
  8289. + struct mutex lock;
  8290. + struct rb_root refcount;
  8291. + struct ssam_nf_head head[SSH_NUM_EVENTS];
  8292. +};
  8293. +
  8294. +
  8295. +/* -- Event/async request completion system. -------------------------------- */
  8296. +
  8297. +struct ssam_cplt;
  8298. +
  8299. +/**
  8300. + * struct ssam_event_item - Struct for event queuing and completion.
  8301. + * @node: The node in the queue.
  8302. + * @rqid: The request ID of the event.
  8303. + * @ops: Instance specific functions.
  8304. + * @ops.free: Callback for freeing this event item.
  8305. + * @event: Actual event data.
  8306. + */
  8307. +struct ssam_event_item {
  8308. + struct list_head node;
  8309. + u16 rqid;
  8310. +
  8311. + struct {
  8312. + void (*free)(struct ssam_event_item *event);
  8313. + } ops;
  8314. +
  8315. + struct ssam_event event; /* must be last */
  8316. +};
  8317. +
  8318. +/**
  8319. + * struct ssam_event_queue - Queue for completing received events.
  8320. + * @cplt: Reference to the completion system on which this queue is active.
  8321. + * @lock: The lock for any operation on the queue.
  8322. + * @head: The list-head of the queue.
  8323. + * @work: The &struct work_struct performing completion work for this queue.
  8324. + */
  8325. +struct ssam_event_queue {
  8326. + struct ssam_cplt *cplt;
  8327. +
  8328. + spinlock_t lock;
  8329. + struct list_head head;
  8330. + struct work_struct work;
  8331. +};
  8332. +
  8333. +/**
  8334. + * struct ssam_event_target - Set of queues for a single SSH target ID.
  8335. + * @queue: The array of queues, one queue per event ID.
  8336. + */
  8337. +struct ssam_event_target {
  8338. + struct ssam_event_queue queue[SSH_NUM_EVENTS];
  8339. +};
  8340. +
  8341. +/**
  8342. + * struct ssam_cplt - SSAM event/async request completion system.
  8343. + * @dev: The device with which this system is associated. Only used
  8344. + * for logging.
  8345. + * @wq: The &struct workqueue_struct on which all completion work
  8346. + * items are queued.
  8347. + * @event: Event completion management.
  8348. + * @event.target: Array of &struct ssam_event_target, one for each target.
  8349. + * @event.notif: Notifier callbacks and event activation reference counting.
  8350. + */
  8351. +struct ssam_cplt {
  8352. + struct device *dev;
  8353. + struct workqueue_struct *wq;
  8354. +
  8355. + struct {
  8356. + struct ssam_event_target target[SSH_NUM_TARGETS];
  8357. + struct ssam_nf notif;
  8358. + } event;
  8359. +};
  8360. +
  8361. +
  8362. +/* -- Main SSAM device structures. ------------------------------------------ */
  8363. +
  8364. +/**
  8365. + * enum ssam_controller_state - State values for &struct ssam_controller.
  8366. + * @SSAM_CONTROLLER_UNINITIALIZED:
  8367. + * The controller has not been initialized yet or has been deinitialized.
  8368. + * @SSAM_CONTROLLER_INITIALIZED:
  8369. + * The controller is initialized, but has not been started yet.
  8370. + * @SSAM_CONTROLLER_STARTED:
  8371. + * The controller has been started and is ready to use.
  8372. + * @SSAM_CONTROLLER_STOPPED:
  8373. + * The controller has been stopped.
  8374. + * @SSAM_CONTROLLER_SUSPENDED:
  8375. + * The controller has been suspended.
  8376. + */
  8377. +enum ssam_controller_state {
  8378. + SSAM_CONTROLLER_UNINITIALIZED,
  8379. + SSAM_CONTROLLER_INITIALIZED,
  8380. + SSAM_CONTROLLER_STARTED,
  8381. + SSAM_CONTROLLER_STOPPED,
  8382. + SSAM_CONTROLLER_SUSPENDED,
  8383. +};
  8384. +
  8385. +/**
  8386. + * struct ssam_controller_caps - Controller device capabilities.
  8387. + * @ssh_power_profile: SSH power profile.
  8388. + * @ssh_buffer_size: SSH driver UART buffer size.
  8389. + * @screen_on_sleep_idle_timeout: SAM UART screen-on sleep idle timeout.
  8390. + * @screen_off_sleep_idle_timeout: SAM UART screen-off sleep idle timeout.
  8391. + * @d3_closes_handle: SAM closes UART handle in D3.
  8392. + *
  8393. + * Controller and SSH device capabilities found in ACPI.
  8394. + */
  8395. +struct ssam_controller_caps {
  8396. + u32 ssh_power_profile;
  8397. + u32 ssh_buffer_size;
  8398. + u32 screen_on_sleep_idle_timeout;
  8399. + u32 screen_off_sleep_idle_timeout;
  8400. + u32 d3_closes_handle:1;
  8401. +};
  8402. +
  8403. +/**
  8404. + * struct ssam_controller - SSAM controller device.
  8405. + * @kref: Reference count of the controller.
  8406. + * @lock: Main lock for the controller, used to guard state changes.
  8407. + * @state: Controller state.
  8408. + * @rtl: Request transport layer for SSH I/O.
  8409. + * @cplt: Completion system for SSH/SSAM events and asynchronous requests.
  8410. + * @counter: Safe SSH message ID counters.
  8411. + * @counter.seq: Sequence ID counter.
  8412. + * @counter.rqid: Request ID counter.
  8413. + * @irq: Wakeup IRQ resources.
  8414. + * @irq.num: The wakeup IRQ number.
  8415. + * @irq.wakeup_enabled: Whether wakeup by IRQ is enabled during suspend.
  8416. + * @caps: The controller device capabilities.
  8417. + */
  8418. +struct ssam_controller {
  8419. + struct kref kref;
  8420. +
  8421. + struct rw_semaphore lock;
  8422. + enum ssam_controller_state state;
  8423. +
  8424. + struct ssh_rtl rtl;
  8425. + struct ssam_cplt cplt;
  8426. +
  8427. + struct {
  8428. + struct ssh_seq_counter seq;
  8429. + struct ssh_rqid_counter rqid;
  8430. + } counter;
  8431. +
  8432. + struct {
  8433. + int num;
  8434. + bool wakeup_enabled;
  8435. + } irq;
  8436. +
  8437. + struct ssam_controller_caps caps;
  8438. +};
  8439. +
  8440. +#define to_ssam_controller(ptr, member) \
  8441. + container_of(ptr, struct ssam_controller, member)
  8442. +
  8443. +#define ssam_dbg(ctrl, fmt, ...) rtl_dbg(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
  8444. +#define ssam_info(ctrl, fmt, ...) rtl_info(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
  8445. +#define ssam_warn(ctrl, fmt, ...) rtl_warn(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
  8446. +#define ssam_err(ctrl, fmt, ...) rtl_err(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
  8447. +
  8448. +/**
  8449. + * ssam_controller_receive_buf() - Provide input-data to the controller.
  8450. + * @ctrl: The controller.
  8451. + * @buf: The input buffer.
  8452. + * @n: The number of bytes in the input buffer.
  8453. + *
  8454. + * Provide input data to be evaluated by the controller, which has been
  8455. + * received via the lower-level transport.
  8456. + *
  8457. + * Return: Returns the number of bytes consumed, or, if the packet transport
  8458. + * layer of the controller has been shut down, %-ESHUTDOWN.
  8459. + */
  8460. +static inline
  8461. +int ssam_controller_receive_buf(struct ssam_controller *ctrl,
  8462. + const unsigned char *buf, size_t n)
  8463. +{
  8464. + return ssh_ptl_rx_rcvbuf(&ctrl->rtl.ptl, buf, n);
  8465. +}
  8466. +
  8467. +/**
  8468. + * ssam_controller_write_wakeup() - Notify the controller that the underlying
  8469. + * device has space available for data to be written.
  8470. + * @ctrl: The controller.
  8471. + */
  8472. +static inline void ssam_controller_write_wakeup(struct ssam_controller *ctrl)
  8473. +{
  8474. + ssh_ptl_tx_wakeup_transfer(&ctrl->rtl.ptl);
  8475. +}
  8476. +
  8477. +int ssam_controller_init(struct ssam_controller *ctrl, struct serdev_device *s);
  8478. +int ssam_controller_start(struct ssam_controller *ctrl);
  8479. +void ssam_controller_shutdown(struct ssam_controller *ctrl);
  8480. +void ssam_controller_destroy(struct ssam_controller *ctrl);
  8481. +
  8482. +int ssam_notifier_disable_registered(struct ssam_controller *ctrl);
  8483. +void ssam_notifier_restore_registered(struct ssam_controller *ctrl);
  8484. +
  8485. +int ssam_irq_setup(struct ssam_controller *ctrl);
  8486. +void ssam_irq_free(struct ssam_controller *ctrl);
  8487. +int ssam_irq_arm_for_wakeup(struct ssam_controller *ctrl);
  8488. +void ssam_irq_disarm_wakeup(struct ssam_controller *ctrl);
  8489. +
  8490. +void ssam_controller_lock(struct ssam_controller *c);
  8491. +void ssam_controller_unlock(struct ssam_controller *c);
  8492. +
  8493. +int ssam_get_firmware_version(struct ssam_controller *ctrl, u32 *version);
  8494. +int ssam_ctrl_notif_display_off(struct ssam_controller *ctrl);
  8495. +int ssam_ctrl_notif_display_on(struct ssam_controller *ctrl);
  8496. +int ssam_ctrl_notif_d0_exit(struct ssam_controller *ctrl);
  8497. +int ssam_ctrl_notif_d0_entry(struct ssam_controller *ctrl);
  8498. +
  8499. +int ssam_controller_suspend(struct ssam_controller *ctrl);
  8500. +int ssam_controller_resume(struct ssam_controller *ctrl);
  8501. +
  8502. +int ssam_event_item_cache_init(void);
  8503. +void ssam_event_item_cache_destroy(void);
  8504. +
  8505. +#endif /* _SURFACE_AGGREGATOR_CONTROLLER_H */
  8506. diff --git a/drivers/platform/x86/surface_aggregator/core.c b/drivers/platform/x86/surface_aggregator/core.c
  8507. new file mode 100644
  8508. index 000000000000..5d780e55f4a1
  8509. --- /dev/null
  8510. +++ b/drivers/platform/x86/surface_aggregator/core.c
  8511. @@ -0,0 +1,839 @@
  8512. +// SPDX-License-Identifier: GPL-2.0+
  8513. +/*
  8514. + * Surface Serial Hub (SSH) driver for communication with the Surface/System
  8515. + * Aggregator Module (SSAM/SAM).
  8516. + *
  8517. + * Provides access to a SAM-over-SSH connected EC via a controller device.
  8518. + * Handles communication via requests as well as enabling, disabling, and
  8519. + * relaying of events.
  8520. + *
  8521. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  8522. + */
  8523. +
  8524. +#include <linux/acpi.h>
  8525. +#include <linux/atomic.h>
  8526. +#include <linux/completion.h>
  8527. +#include <linux/gpio/consumer.h>
  8528. +#include <linux/kernel.h>
  8529. +#include <linux/kref.h>
  8530. +#include <linux/module.h>
  8531. +#include <linux/pm.h>
  8532. +#include <linux/serdev.h>
  8533. +#include <linux/sysfs.h>
  8534. +
  8535. +#include <linux/surface_aggregator/controller.h>
  8536. +
  8537. +#include "bus.h"
  8538. +#include "controller.h"
  8539. +
  8540. +#define CREATE_TRACE_POINTS
  8541. +#include "trace.h"
  8542. +
  8543. +
  8544. +/* -- Static controller reference. ------------------------------------------ */
  8545. +
  8546. +/*
  8547. + * Main controller reference. The corresponding lock must be held while
  8548. + * accessing (reading/writing) the reference.
  8549. + */
  8550. +static struct ssam_controller *__ssam_controller;
  8551. +static DEFINE_SPINLOCK(__ssam_controller_lock);
  8552. +
  8553. +/**
  8554. + * ssam_get_controller() - Get reference to SSAM controller.
  8555. + *
  8556. + * Returns a reference to the SSAM controller of the system or %NULL if there
  8557. + * is none, it hasn't been set up yet, or it has already been unregistered.
  8558. + * This function automatically increments the reference count of the
  8559. + * controller, thus the calling party must ensure that ssam_controller_put()
  8560. + * is called when it doesn't need the controller any more.
  8561. + */
  8562. +struct ssam_controller *ssam_get_controller(void)
  8563. +{
  8564. + struct ssam_controller *ctrl;
  8565. +
  8566. + spin_lock(&__ssam_controller_lock);
  8567. +
  8568. + ctrl = __ssam_controller;
  8569. + if (!ctrl)
  8570. + goto out;
  8571. +
  8572. + if (WARN_ON(!kref_get_unless_zero(&ctrl->kref)))
  8573. + ctrl = NULL;
  8574. +
  8575. +out:
  8576. + spin_unlock(&__ssam_controller_lock);
  8577. + return ctrl;
  8578. +}
  8579. +EXPORT_SYMBOL_GPL(ssam_get_controller);
  8580. +
  8581. +/**
  8582. + * ssam_try_set_controller() - Try to set the main controller reference.
  8583. + * @ctrl: The controller to which the reference should point.
  8584. + *
  8585. + * Set the main controller reference to the given pointer if the reference
  8586. + * hasn't been set already.
  8587. + *
  8588. + * Return: Returns zero on success or %-EEXIST if the reference has already
  8589. + * been set.
  8590. + */
  8591. +static int ssam_try_set_controller(struct ssam_controller *ctrl)
  8592. +{
  8593. + int status = 0;
  8594. +
  8595. + spin_lock(&__ssam_controller_lock);
  8596. + if (!__ssam_controller)
  8597. + __ssam_controller = ctrl;
  8598. + else
  8599. + status = -EEXIST;
  8600. + spin_unlock(&__ssam_controller_lock);
  8601. +
  8602. + return status;
  8603. +}
  8604. +
  8605. +/**
  8606. + * ssam_clear_controller() - Remove/clear the main controller reference.
  8607. + *
  8608. + * Clears the main controller reference, i.e. sets it to %NULL. This function
  8609. + * should be called before the controller is shut down.
  8610. + */
  8611. +static void ssam_clear_controller(void)
  8612. +{
  8613. + spin_lock(&__ssam_controller_lock);
  8614. + __ssam_controller = NULL;
  8615. + spin_unlock(&__ssam_controller_lock);
  8616. +}
  8617. +
  8618. +/**
  8619. + * ssam_client_link() - Link an arbitrary client device to the controller.
  8620. + * @c: The controller to link to.
  8621. + * @client: The client device.
  8622. + *
  8623. + * Link an arbitrary client device to the controller by creating a device link
  8624. + * between it as consumer and the controller device as provider. This function
  8625. + * can be used for non-SSAM devices (or SSAM devices not registered as child
  8626. + * under the controller) to guarantee that the controller is valid for as long
  8627. + * as the driver of the client device is bound, and that proper suspend and
  8628. + * resume ordering is guaranteed.
  8629. + *
  8630. + * The device link does not have to be destructed manually. It is removed
  8631. + * automatically once the driver of the client device unbinds.
  8632. + *
  8633. + * Return: Returns zero on success, %-ENODEV if the controller is not ready or
  8634. + * going to be removed soon, or %-ENOMEM if the device link could not be
  8635. + * created for other reasons.
  8636. + */
  8637. +int ssam_client_link(struct ssam_controller *c, struct device *client)
  8638. +{
  8639. + const u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;
  8640. + struct device_link *link;
  8641. + struct device *ctrldev;
  8642. +
  8643. + ssam_controller_statelock(c);
  8644. +
  8645. + if (c->state != SSAM_CONTROLLER_STARTED) {
  8646. + ssam_controller_stateunlock(c);
  8647. + return -ENODEV;
  8648. + }
  8649. +
  8650. + ctrldev = ssam_controller_device(c);
  8651. + if (!ctrldev) {
  8652. + ssam_controller_stateunlock(c);
  8653. + return -ENODEV;
  8654. + }
  8655. +
  8656. + link = device_link_add(client, ctrldev, flags);
  8657. + if (!link) {
  8658. + ssam_controller_stateunlock(c);
  8659. + return -ENOMEM;
  8660. + }
  8661. +
  8662. + /*
  8663. + * Return -ENODEV if supplier driver is on its way to be removed. In
  8664. + * this case, the controller won't be around for much longer and the
  8665. + * device link is not going to save us any more, as unbinding is
  8666. + * already in progress.
  8667. + */
  8668. + if (READ_ONCE(link->status) == DL_STATE_SUPPLIER_UNBIND) {
  8669. + ssam_controller_stateunlock(c);
  8670. + return -ENODEV;
  8671. + }
  8672. +
  8673. + ssam_controller_stateunlock(c);
  8674. + return 0;
  8675. +}
  8676. +EXPORT_SYMBOL_GPL(ssam_client_link);
  8677. +
  8678. +/**
  8679. + * ssam_client_bind() - Bind an arbitrary client device to the controller.
  8680. + * @client: The client device.
  8681. + *
  8682. + * Link an arbitrary client device to the controller by creating a device link
  8683. + * between it as consumer and the main controller device as provider. This
  8684. + * function can be used for non-SSAM devices to guarantee that the controller
  8685. + * returned by this function is valid for as long as the driver of the client
  8686. + * device is bound, and that proper suspend and resume ordering is guaranteed.
  8687. + *
  8688. + * This function does essentially the same as ssam_client_link(), except that
  8689. + * it first fetches the main controller reference, then creates the link, and
  8690. + * finally returns this reference. Note that this function does not increment
  8691. + * the reference counter of the controller, as, due to the link, the
  8692. + * controller lifetime is assured as long as the driver of the client device
  8693. + * is bound.
  8694. + *
  8695. + * It is not valid to use the controller reference obtained by this method
  8696. + * outside of the driver bound to the client device at the time of calling
  8697. + * this function, without first incrementing the reference count of the
  8698. + * controller via ssam_controller_get(). Even after doing this, care must be
  8699. + * taken that requests are only submitted and notifiers are only
  8700. + * (un-)registered when the controller is active and not suspended. In other
  8701. + * words: The device link only lives as long as the client driver is bound and
  8702. + * any guarantees enforced by this link (e.g. active controller state) can
  8703. + * only be relied upon as long as this link exists and may need to be enforced
  8704. + * in other ways afterwards.
  8705. + *
  8706. + * The created device link does not have to be destructed manually. It is
  8707. + * removed automatically once the driver of the client device unbinds.
  8708. + *
  8709. + * Return: Returns the controller on success, an error pointer with %-ENODEV
  8710. + * if the controller is not present, not ready or going to be removed soon, or
  8711. + * %-ENOMEM if the device link could not be created for other reasons.
  8712. + */
  8713. +struct ssam_controller *ssam_client_bind(struct device *client)
  8714. +{
  8715. + struct ssam_controller *c;
  8716. + int status;
  8717. +
  8718. + c = ssam_get_controller();
  8719. + if (!c)
  8720. + return ERR_PTR(-ENODEV);
  8721. +
  8722. + status = ssam_client_link(c, client);
  8723. +
  8724. + /*
  8725. + * Note that we can drop our controller reference in both success and
  8726. + * failure cases: On success, we have bound the controller lifetime
  8727. + * inherently to the client driver lifetime, i.e. it the controller is
  8728. + * now guaranteed to outlive the client driver. On failure, we're not
  8729. + * going to use the controller any more.
  8730. + */
  8731. + ssam_controller_put(c);
  8732. +
  8733. + return status >= 0 ? c : ERR_PTR(status);
  8734. +}
  8735. +EXPORT_SYMBOL_GPL(ssam_client_bind);
  8736. +
  8737. +
  8738. +/* -- Glue layer (serdev_device -> ssam_controller). ------------------------ */
  8739. +
  8740. +static int ssam_receive_buf(struct serdev_device *dev, const unsigned char *buf,
  8741. + size_t n)
  8742. +{
  8743. + struct ssam_controller *ctrl;
  8744. +
  8745. + ctrl = serdev_device_get_drvdata(dev);
  8746. + return ssam_controller_receive_buf(ctrl, buf, n);
  8747. +}
  8748. +
  8749. +static void ssam_write_wakeup(struct serdev_device *dev)
  8750. +{
  8751. + ssam_controller_write_wakeup(serdev_device_get_drvdata(dev));
  8752. +}
  8753. +
  8754. +static const struct serdev_device_ops ssam_serdev_ops = {
  8755. + .receive_buf = ssam_receive_buf,
  8756. + .write_wakeup = ssam_write_wakeup,
  8757. +};
  8758. +
  8759. +
  8760. +/* -- SysFS and misc. ------------------------------------------------------- */
  8761. +
  8762. +static int ssam_log_firmware_version(struct ssam_controller *ctrl)
  8763. +{
  8764. + u32 version, a, b, c;
  8765. + int status;
  8766. +
  8767. + status = ssam_get_firmware_version(ctrl, &version);
  8768. + if (status)
  8769. + return status;
  8770. +
  8771. + a = (version >> 24) & 0xff;
  8772. + b = ((version >> 8) & 0xffff);
  8773. + c = version & 0xff;
  8774. +
  8775. + ssam_info(ctrl, "SAM firmware version: %u.%u.%u\n", a, b, c);
  8776. + return 0;
  8777. +}
  8778. +
  8779. +static ssize_t firmware_version_show(struct device *dev,
  8780. + struct device_attribute *attr, char *buf)
  8781. +{
  8782. + struct ssam_controller *ctrl = dev_get_drvdata(dev);
  8783. + u32 version, a, b, c;
  8784. + int status;
  8785. +
  8786. + status = ssam_get_firmware_version(ctrl, &version);
  8787. + if (status < 0)
  8788. + return status;
  8789. +
  8790. + a = (version >> 24) & 0xff;
  8791. + b = ((version >> 8) & 0xffff);
  8792. + c = version & 0xff;
  8793. +
  8794. + return sysfs_emit(buf, "%u.%u.%u\n", a, b, c);
  8795. +}
  8796. +static DEVICE_ATTR_RO(firmware_version);
  8797. +
  8798. +static struct attribute *ssam_sam_attrs[] = {
  8799. + &dev_attr_firmware_version.attr,
  8800. + NULL
  8801. +};
  8802. +
  8803. +static const struct attribute_group ssam_sam_group = {
  8804. + .name = "sam",
  8805. + .attrs = ssam_sam_attrs,
  8806. +};
  8807. +
  8808. +
  8809. +/* -- ACPI based device setup. ---------------------------------------------- */
  8810. +
  8811. +static acpi_status ssam_serdev_setup_via_acpi_crs(struct acpi_resource *rsc,
  8812. + void *ctx)
  8813. +{
  8814. + struct serdev_device *serdev = ctx;
  8815. + struct acpi_resource_common_serialbus *serial;
  8816. + struct acpi_resource_uart_serialbus *uart;
  8817. + bool flow_control;
  8818. + int status = 0;
  8819. +
  8820. + if (rsc->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
  8821. + return AE_OK;
  8822. +
  8823. + serial = &rsc->data.common_serial_bus;
  8824. + if (serial->type != ACPI_RESOURCE_SERIAL_TYPE_UART)
  8825. + return AE_OK;
  8826. +
  8827. + uart = &rsc->data.uart_serial_bus;
  8828. +
  8829. + /* Set up serdev device. */
  8830. + serdev_device_set_baudrate(serdev, uart->default_baud_rate);
  8831. +
  8832. + /* serdev currently only supports RTSCTS flow control. */
  8833. + if (uart->flow_control & (~((u8)ACPI_UART_FLOW_CONTROL_HW))) {
  8834. + dev_warn(&serdev->dev, "setup: unsupported flow control (value: %#04x)\n",
  8835. + uart->flow_control);
  8836. + }
  8837. +
  8838. + /* Set RTSCTS flow control. */
  8839. + flow_control = uart->flow_control & ACPI_UART_FLOW_CONTROL_HW;
  8840. + serdev_device_set_flow_control(serdev, flow_control);
  8841. +
  8842. + /* serdev currently only supports EVEN/ODD parity. */
  8843. + switch (uart->parity) {
  8844. + case ACPI_UART_PARITY_NONE:
  8845. + status = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
  8846. + break;
  8847. + case ACPI_UART_PARITY_EVEN:
  8848. + status = serdev_device_set_parity(serdev, SERDEV_PARITY_EVEN);
  8849. + break;
  8850. + case ACPI_UART_PARITY_ODD:
  8851. + status = serdev_device_set_parity(serdev, SERDEV_PARITY_ODD);
  8852. + break;
  8853. + default:
  8854. + dev_warn(&serdev->dev, "setup: unsupported parity (value: %#04x)\n",
  8855. + uart->parity);
  8856. + break;
  8857. + }
  8858. +
  8859. + if (status) {
  8860. + dev_err(&serdev->dev, "setup: failed to set parity (value: %#04x, error: %d)\n",
  8861. + uart->parity, status);
  8862. + return AE_ERROR;
  8863. + }
  8864. +
  8865. + /* We've found the resource and are done. */
  8866. + return AE_CTRL_TERMINATE;
  8867. +}
  8868. +
  8869. +static acpi_status ssam_serdev_setup_via_acpi(acpi_handle handle,
  8870. + struct serdev_device *serdev)
  8871. +{
  8872. + return acpi_walk_resources(handle, METHOD_NAME__CRS,
  8873. + ssam_serdev_setup_via_acpi_crs, serdev);
  8874. +}
  8875. +
  8876. +
  8877. +/* -- Power management. ----------------------------------------------------- */
  8878. +
  8879. +static void ssam_serial_hub_shutdown(struct device *dev)
  8880. +{
  8881. + struct ssam_controller *c = dev_get_drvdata(dev);
  8882. + int status;
  8883. +
  8884. + /*
  8885. + * Try to disable notifiers, signal display-off and D0-exit, ignore any
  8886. + * errors.
  8887. + *
  8888. + * Note: It has not been established yet if this is actually
  8889. + * necessary/useful for shutdown.
  8890. + */
  8891. +
  8892. + status = ssam_notifier_disable_registered(c);
  8893. + if (status) {
  8894. + ssam_err(c, "pm: failed to disable notifiers for shutdown: %d\n",
  8895. + status);
  8896. + }
  8897. +
  8898. + status = ssam_ctrl_notif_display_off(c);
  8899. + if (status)
  8900. + ssam_err(c, "pm: display-off notification failed: %d\n", status);
  8901. +
  8902. + status = ssam_ctrl_notif_d0_exit(c);
  8903. + if (status)
  8904. + ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
  8905. +}
  8906. +
  8907. +#ifdef CONFIG_PM_SLEEP
  8908. +
  8909. +static int ssam_serial_hub_pm_prepare(struct device *dev)
  8910. +{
  8911. + struct ssam_controller *c = dev_get_drvdata(dev);
  8912. + int status;
  8913. +
  8914. + /*
  8915. + * Try to signal display-off, This will quiesce events.
  8916. + *
  8917. + * Note: Signaling display-off/display-on should normally be done from
  8918. + * some sort of display state notifier. As that is not available,
  8919. + * signal it here.
  8920. + */
  8921. +
  8922. + status = ssam_ctrl_notif_display_off(c);
  8923. + if (status)
  8924. + ssam_err(c, "pm: display-off notification failed: %d\n", status);
  8925. +
  8926. + return status;
  8927. +}
  8928. +
  8929. +static void ssam_serial_hub_pm_complete(struct device *dev)
  8930. +{
  8931. + struct ssam_controller *c = dev_get_drvdata(dev);
  8932. + int status;
  8933. +
  8934. + /*
  8935. + * Try to signal display-on. This will restore events.
  8936. + *
  8937. + * Note: Signaling display-off/display-on should normally be done from
  8938. + * some sort of display state notifier. As that is not available,
  8939. + * signal it here.
  8940. + */
  8941. +
  8942. + status = ssam_ctrl_notif_display_on(c);
  8943. + if (status)
  8944. + ssam_err(c, "pm: display-on notification failed: %d\n", status);
  8945. +}
  8946. +
  8947. +static int ssam_serial_hub_pm_suspend(struct device *dev)
  8948. +{
  8949. + struct ssam_controller *c = dev_get_drvdata(dev);
  8950. + int status;
  8951. +
  8952. + /*
  8953. + * Try to signal D0-exit, enable IRQ wakeup if specified. Abort on
  8954. + * error.
  8955. + */
  8956. +
  8957. + status = ssam_ctrl_notif_d0_exit(c);
  8958. + if (status) {
  8959. + ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
  8960. + goto err_notif;
  8961. + }
  8962. +
  8963. + status = ssam_irq_arm_for_wakeup(c);
  8964. + if (status)
  8965. + goto err_irq;
  8966. +
  8967. + WARN_ON(ssam_controller_suspend(c));
  8968. + return 0;
  8969. +
  8970. +err_irq:
  8971. + ssam_ctrl_notif_d0_entry(c);
  8972. +err_notif:
  8973. + ssam_ctrl_notif_display_on(c);
  8974. + return status;
  8975. +}
  8976. +
  8977. +static int ssam_serial_hub_pm_resume(struct device *dev)
  8978. +{
  8979. + struct ssam_controller *c = dev_get_drvdata(dev);
  8980. + int status;
  8981. +
  8982. + WARN_ON(ssam_controller_resume(c));
  8983. +
  8984. + /*
  8985. + * Try to disable IRQ wakeup (if specified) and signal D0-entry. In
  8986. + * case of errors, log them and try to restore normal operation state
  8987. + * as far as possible.
  8988. + *
  8989. + * Note: Signaling display-off/display-on should normally be done from
  8990. + * some sort of display state notifier. As that is not available,
  8991. + * signal it here.
  8992. + */
  8993. +
  8994. + ssam_irq_disarm_wakeup(c);
  8995. +
  8996. + status = ssam_ctrl_notif_d0_entry(c);
  8997. + if (status)
  8998. + ssam_err(c, "pm: D0-entry notification failed: %d\n", status);
  8999. +
  9000. + return 0;
  9001. +}
  9002. +
  9003. +static int ssam_serial_hub_pm_freeze(struct device *dev)
  9004. +{
  9005. + struct ssam_controller *c = dev_get_drvdata(dev);
  9006. + int status;
  9007. +
  9008. + /*
  9009. + * During hibernation image creation, we only have to ensure that the
  9010. + * EC doesn't send us any events. This is done via the display-off
  9011. + * and D0-exit notifications. Note that this sets up the wakeup IRQ
  9012. + * on the EC side, however, we have disabled it by default on our side
  9013. + * and won't enable it here.
  9014. + *
  9015. + * See ssam_serial_hub_poweroff() for more details on the hibernation
  9016. + * process.
  9017. + */
  9018. +
  9019. + status = ssam_ctrl_notif_d0_exit(c);
  9020. + if (status) {
  9021. + ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
  9022. + ssam_ctrl_notif_display_on(c);
  9023. + return status;
  9024. + }
  9025. +
  9026. + WARN_ON(ssam_controller_suspend(c));
  9027. + return 0;
  9028. +}
  9029. +
  9030. +static int ssam_serial_hub_pm_thaw(struct device *dev)
  9031. +{
  9032. + struct ssam_controller *c = dev_get_drvdata(dev);
  9033. + int status;
  9034. +
  9035. + WARN_ON(ssam_controller_resume(c));
  9036. +
  9037. + status = ssam_ctrl_notif_d0_entry(c);
  9038. + if (status)
  9039. + ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
  9040. +
  9041. + return status;
  9042. +}
  9043. +
  9044. +static int ssam_serial_hub_pm_poweroff(struct device *dev)
  9045. +{
  9046. + struct ssam_controller *c = dev_get_drvdata(dev);
  9047. + int status;
  9048. +
  9049. + /*
  9050. + * When entering hibernation and powering off the system, the EC, at
  9051. + * least on some models, may disable events. Without us taking care of
  9052. + * that, this leads to events not being enabled/restored when the
  9053. + * system resumes from hibernation, resulting SAM-HID subsystem devices
  9054. + * (i.e. keyboard, touchpad) not working, AC-plug/AC-unplug events being
  9055. + * gone, etc.
  9056. + *
  9057. + * To avoid these issues, we disable all registered events here (this is
  9058. + * likely not actually required) and restore them during the drivers PM
  9059. + * restore callback.
  9060. + *
  9061. + * Wakeup from the EC interrupt is not supported during hibernation,
  9062. + * so don't arm the IRQ here.
  9063. + */
  9064. +
  9065. + status = ssam_notifier_disable_registered(c);
  9066. + if (status) {
  9067. + ssam_err(c, "pm: failed to disable notifiers for hibernation: %d\n",
  9068. + status);
  9069. + return status;
  9070. + }
  9071. +
  9072. + status = ssam_ctrl_notif_d0_exit(c);
  9073. + if (status) {
  9074. + ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
  9075. + ssam_notifier_restore_registered(c);
  9076. + return status;
  9077. + }
  9078. +
  9079. + WARN_ON(ssam_controller_suspend(c));
  9080. + return 0;
  9081. +}
  9082. +
  9083. +static int ssam_serial_hub_pm_restore(struct device *dev)
  9084. +{
  9085. + struct ssam_controller *c = dev_get_drvdata(dev);
  9086. + int status;
  9087. +
  9088. + /*
  9089. + * Ignore but log errors, try to restore state as much as possible in
  9090. + * case of failures. See ssam_serial_hub_poweroff() for more details on
  9091. + * the hibernation process.
  9092. + */
  9093. +
  9094. + WARN_ON(ssam_controller_resume(c));
  9095. +
  9096. + status = ssam_ctrl_notif_d0_entry(c);
  9097. + if (status)
  9098. + ssam_err(c, "pm: D0-entry notification failed: %d\n", status);
  9099. +
  9100. + ssam_notifier_restore_registered(c);
  9101. + return 0;
  9102. +}
  9103. +
  9104. +static const struct dev_pm_ops ssam_serial_hub_pm_ops = {
  9105. + .prepare = ssam_serial_hub_pm_prepare,
  9106. + .complete = ssam_serial_hub_pm_complete,
  9107. + .suspend = ssam_serial_hub_pm_suspend,
  9108. + .resume = ssam_serial_hub_pm_resume,
  9109. + .freeze = ssam_serial_hub_pm_freeze,
  9110. + .thaw = ssam_serial_hub_pm_thaw,
  9111. + .poweroff = ssam_serial_hub_pm_poweroff,
  9112. + .restore = ssam_serial_hub_pm_restore,
  9113. +};
  9114. +
  9115. +#else /* CONFIG_PM_SLEEP */
  9116. +
  9117. +static const struct dev_pm_ops ssam_serial_hub_pm_ops = { };
  9118. +
  9119. +#endif /* CONFIG_PM_SLEEP */
  9120. +
  9121. +
  9122. +/* -- Device/driver setup. -------------------------------------------------- */
  9123. +
  9124. +static const struct acpi_gpio_params gpio_ssam_wakeup_int = { 0, 0, false };
  9125. +static const struct acpi_gpio_params gpio_ssam_wakeup = { 1, 0, false };
  9126. +
  9127. +static const struct acpi_gpio_mapping ssam_acpi_gpios[] = {
  9128. + { "ssam_wakeup-int-gpio", &gpio_ssam_wakeup_int, 1 },
  9129. + { "ssam_wakeup-gpio", &gpio_ssam_wakeup, 1 },
  9130. + { },
  9131. +};
  9132. +
  9133. +static int ssam_serial_hub_probe(struct serdev_device *serdev)
  9134. +{
  9135. + struct ssam_controller *ctrl;
  9136. + acpi_handle *ssh = ACPI_HANDLE(&serdev->dev);
  9137. + acpi_status astatus;
  9138. + int status;
  9139. +
  9140. + if (gpiod_count(&serdev->dev, NULL) < 0)
  9141. + return -ENODEV;
  9142. +
  9143. + status = devm_acpi_dev_add_driver_gpios(&serdev->dev, ssam_acpi_gpios);
  9144. + if (status)
  9145. + return status;
  9146. +
  9147. + /* Allocate controller. */
  9148. + ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  9149. + if (!ctrl)
  9150. + return -ENOMEM;
  9151. +
  9152. + /* Initialize controller. */
  9153. + status = ssam_controller_init(ctrl, serdev);
  9154. + if (status)
  9155. + goto err_ctrl_init;
  9156. +
  9157. + ssam_controller_lock(ctrl);
  9158. +
  9159. + /* Set up serdev device. */
  9160. + serdev_device_set_drvdata(serdev, ctrl);
  9161. + serdev_device_set_client_ops(serdev, &ssam_serdev_ops);
  9162. + status = serdev_device_open(serdev);
  9163. + if (status)
  9164. + goto err_devopen;
  9165. +
  9166. + astatus = ssam_serdev_setup_via_acpi(ssh, serdev);
  9167. + if (ACPI_FAILURE(astatus)) {
  9168. + status = -ENXIO;
  9169. + goto err_devinit;
  9170. + }
  9171. +
  9172. + /* Start controller. */
  9173. + status = ssam_controller_start(ctrl);
  9174. + if (status)
  9175. + goto err_devinit;
  9176. +
  9177. + ssam_controller_unlock(ctrl);
  9178. +
  9179. + /*
  9180. + * Initial SAM requests: Log version and notify default/init power
  9181. + * states.
  9182. + */
  9183. + status = ssam_log_firmware_version(ctrl);
  9184. + if (status)
  9185. + goto err_initrq;
  9186. +
  9187. + status = ssam_ctrl_notif_d0_entry(ctrl);
  9188. + if (status)
  9189. + goto err_initrq;
  9190. +
  9191. + status = ssam_ctrl_notif_display_on(ctrl);
  9192. + if (status)
  9193. + goto err_initrq;
  9194. +
  9195. + status = sysfs_create_group(&serdev->dev.kobj, &ssam_sam_group);
  9196. + if (status)
  9197. + goto err_initrq;
  9198. +
  9199. + /* Set up IRQ. */
  9200. + status = ssam_irq_setup(ctrl);
  9201. + if (status)
  9202. + goto err_irq;
  9203. +
  9204. + /* Finally, set main controller reference. */
  9205. + status = ssam_try_set_controller(ctrl);
  9206. + if (WARN_ON(status)) /* Currently, we're the only provider. */
  9207. + goto err_mainref;
  9208. +
  9209. + /*
  9210. + * TODO: The EC can wake up the system via the associated GPIO interrupt
  9211. + * in multiple situations. One of which is the remaining battery
  9212. + * capacity falling below a certain threshold. Normally, we should
  9213. + * use the device_init_wakeup function, however, the EC also seems
  9214. + * to have other reasons for waking up the system and it seems
  9215. + * that Windows has additional checks whether the system should be
  9216. + * resumed. In short, this causes some spurious unwanted wake-ups.
  9217. + * For now let's thus default power/wakeup to false.
  9218. + */
  9219. + device_set_wakeup_capable(&serdev->dev, true);
  9220. + acpi_walk_dep_device_list(ssh);
  9221. +
  9222. + return 0;
  9223. +
  9224. +err_mainref:
  9225. + ssam_irq_free(ctrl);
  9226. +err_irq:
  9227. + sysfs_remove_group(&serdev->dev.kobj, &ssam_sam_group);
  9228. +err_initrq:
  9229. + ssam_controller_lock(ctrl);
  9230. + ssam_controller_shutdown(ctrl);
  9231. +err_devinit:
  9232. + serdev_device_close(serdev);
  9233. +err_devopen:
  9234. + ssam_controller_destroy(ctrl);
  9235. + ssam_controller_unlock(ctrl);
  9236. +err_ctrl_init:
  9237. + kfree(ctrl);
  9238. + return status;
  9239. +}
  9240. +
  9241. +static void ssam_serial_hub_remove(struct serdev_device *serdev)
  9242. +{
  9243. + struct ssam_controller *ctrl = serdev_device_get_drvdata(serdev);
  9244. + int status;
  9245. +
  9246. + /* Clear static reference so that no one else can get a new one. */
  9247. + ssam_clear_controller();
  9248. +
  9249. + /* Disable and free IRQ. */
  9250. + ssam_irq_free(ctrl);
  9251. +
  9252. + sysfs_remove_group(&serdev->dev.kobj, &ssam_sam_group);
  9253. + ssam_controller_lock(ctrl);
  9254. +
  9255. + /* Remove all client devices. */
  9256. + ssam_controller_remove_clients(ctrl);
  9257. +
  9258. + /* Act as if suspending to silence events. */
  9259. + status = ssam_ctrl_notif_display_off(ctrl);
  9260. + if (status) {
  9261. + dev_err(&serdev->dev, "display-off notification failed: %d\n",
  9262. + status);
  9263. + }
  9264. +
  9265. + status = ssam_ctrl_notif_d0_exit(ctrl);
  9266. + if (status) {
  9267. + dev_err(&serdev->dev, "D0-exit notification failed: %d\n",
  9268. + status);
  9269. + }
  9270. +
  9271. + /* Shut down controller and remove serdev device reference from it. */
  9272. + ssam_controller_shutdown(ctrl);
  9273. +
  9274. + /* Shut down actual transport. */
  9275. + serdev_device_wait_until_sent(serdev, 0);
  9276. + serdev_device_close(serdev);
  9277. +
  9278. + /* Drop our controller reference. */
  9279. + ssam_controller_unlock(ctrl);
  9280. + ssam_controller_put(ctrl);
  9281. +
  9282. + device_set_wakeup_capable(&serdev->dev, false);
  9283. +}
  9284. +
  9285. +static const struct acpi_device_id ssam_serial_hub_match[] = {
  9286. + { "MSHW0084", 0 },
  9287. + { },
  9288. +};
  9289. +MODULE_DEVICE_TABLE(acpi, ssam_serial_hub_match);
  9290. +
  9291. +static struct serdev_device_driver ssam_serial_hub = {
  9292. + .probe = ssam_serial_hub_probe,
  9293. + .remove = ssam_serial_hub_remove,
  9294. + .driver = {
  9295. + .name = "surface_serial_hub",
  9296. + .acpi_match_table = ssam_serial_hub_match,
  9297. + .pm = &ssam_serial_hub_pm_ops,
  9298. + .shutdown = ssam_serial_hub_shutdown,
  9299. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  9300. + },
  9301. +};
  9302. +
  9303. +
  9304. +/* -- Module setup. --------------------------------------------------------- */
  9305. +
  9306. +static int __init ssam_core_init(void)
  9307. +{
  9308. + int status;
  9309. +
  9310. + status = ssam_bus_register();
  9311. + if (status)
  9312. + goto err_bus;
  9313. +
  9314. + status = ssh_ctrl_packet_cache_init();
  9315. + if (status)
  9316. + goto err_cpkg;
  9317. +
  9318. + status = ssam_event_item_cache_init();
  9319. + if (status)
  9320. + goto err_evitem;
  9321. +
  9322. + status = serdev_device_driver_register(&ssam_serial_hub);
  9323. + if (status)
  9324. + goto err_register;
  9325. +
  9326. + return 0;
  9327. +
  9328. +err_register:
  9329. + ssam_event_item_cache_destroy();
  9330. +err_evitem:
  9331. + ssh_ctrl_packet_cache_destroy();
  9332. +err_cpkg:
  9333. + ssam_bus_unregister();
  9334. +err_bus:
  9335. + return status;
  9336. +}
  9337. +module_init(ssam_core_init);
  9338. +
  9339. +static void __exit ssam_core_exit(void)
  9340. +{
  9341. + serdev_device_driver_unregister(&ssam_serial_hub);
  9342. + ssam_event_item_cache_destroy();
  9343. + ssh_ctrl_packet_cache_destroy();
  9344. + ssam_bus_unregister();
  9345. +}
  9346. +module_exit(ssam_core_exit);
  9347. +
  9348. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  9349. +MODULE_DESCRIPTION("Subsystem and Surface Serial Hub driver for Surface System Aggregator Module");
  9350. +MODULE_LICENSE("GPL");
  9351. diff --git a/drivers/platform/x86/surface_aggregator/ssh_msgb.h b/drivers/platform/x86/surface_aggregator/ssh_msgb.h
  9352. new file mode 100644
  9353. index 000000000000..e562958ffdf0
  9354. --- /dev/null
  9355. +++ b/drivers/platform/x86/surface_aggregator/ssh_msgb.h
  9356. @@ -0,0 +1,205 @@
  9357. +/* SPDX-License-Identifier: GPL-2.0+ */
  9358. +/*
  9359. + * SSH message builder functions.
  9360. + *
  9361. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  9362. + */
  9363. +
  9364. +#ifndef _SURFACE_AGGREGATOR_SSH_MSGB_H
  9365. +#define _SURFACE_AGGREGATOR_SSH_MSGB_H
  9366. +
  9367. +#include <asm/unaligned.h>
  9368. +#include <linux/types.h>
  9369. +
  9370. +#include <linux/surface_aggregator/controller.h>
  9371. +#include <linux/surface_aggregator/serial_hub.h>
  9372. +
  9373. +/**
  9374. + * struct msgbuf - Buffer struct to construct SSH messages.
  9375. + * @begin: Pointer to the beginning of the allocated buffer space.
  9376. + * @end: Pointer to the end (one past last element) of the allocated buffer
  9377. + * space.
  9378. + * @ptr: Pointer to the first free element in the buffer.
  9379. + */
  9380. +struct msgbuf {
  9381. + u8 *begin;
  9382. + u8 *end;
  9383. + u8 *ptr;
  9384. +};
  9385. +
  9386. +/**
  9387. + * msgb_init() - Initialize the given message buffer struct.
  9388. + * @msgb: The buffer struct to initialize
  9389. + * @ptr: Pointer to the underlying memory by which the buffer will be backed.
  9390. + * @cap: Size of the underlying memory.
  9391. + *
  9392. + * Initialize the given message buffer struct using the provided memory as
  9393. + * backing.
  9394. + */
  9395. +static inline void msgb_init(struct msgbuf *msgb, u8 *ptr, size_t cap)
  9396. +{
  9397. + msgb->begin = ptr;
  9398. + msgb->end = ptr + cap;
  9399. + msgb->ptr = ptr;
  9400. +}
  9401. +
  9402. +/**
  9403. + * msgb_bytes_used() - Return the current number of bytes used in the buffer.
  9404. + * @msgb: The message buffer.
  9405. + */
  9406. +static inline size_t msgb_bytes_used(const struct msgbuf *msgb)
  9407. +{
  9408. + return msgb->ptr - msgb->begin;
  9409. +}
  9410. +
  9411. +static inline void __msgb_push_u8(struct msgbuf *msgb, u8 value)
  9412. +{
  9413. + *msgb->ptr = value;
  9414. + msgb->ptr += sizeof(u8);
  9415. +}
  9416. +
  9417. +static inline void __msgb_push_u16(struct msgbuf *msgb, u16 value)
  9418. +{
  9419. + put_unaligned_le16(value, msgb->ptr);
  9420. + msgb->ptr += sizeof(u16);
  9421. +}
  9422. +
  9423. +/**
  9424. + * msgb_push_u16() - Push a u16 value to the buffer.
  9425. + * @msgb: The message buffer.
  9426. + * @value: The value to push to the buffer.
  9427. + */
  9428. +static inline void msgb_push_u16(struct msgbuf *msgb, u16 value)
  9429. +{
  9430. + if (WARN_ON(msgb->ptr + sizeof(u16) > msgb->end))
  9431. + return;
  9432. +
  9433. + __msgb_push_u16(msgb, value);
  9434. +}
  9435. +
  9436. +/**
  9437. + * msgb_push_syn() - Push SSH SYN bytes to the buffer.
  9438. + * @msgb: The message buffer.
  9439. + */
  9440. +static inline void msgb_push_syn(struct msgbuf *msgb)
  9441. +{
  9442. + msgb_push_u16(msgb, SSH_MSG_SYN);
  9443. +}
  9444. +
  9445. +/**
  9446. + * msgb_push_buf() - Push raw data to the buffer.
  9447. + * @msgb: The message buffer.
  9448. + * @buf: The data to push to the buffer.
  9449. + * @len: The length of the data to push to the buffer.
  9450. + */
  9451. +static inline void msgb_push_buf(struct msgbuf *msgb, const u8 *buf, size_t len)
  9452. +{
  9453. + msgb->ptr = memcpy(msgb->ptr, buf, len) + len;
  9454. +}
  9455. +
  9456. +/**
  9457. + * msgb_push_crc() - Compute CRC and push it to the buffer.
  9458. + * @msgb: The message buffer.
  9459. + * @buf: The data for which the CRC should be computed.
  9460. + * @len: The length of the data for which the CRC should be computed.
  9461. + */
  9462. +static inline void msgb_push_crc(struct msgbuf *msgb, const u8 *buf, size_t len)
  9463. +{
  9464. + msgb_push_u16(msgb, ssh_crc(buf, len));
  9465. +}
  9466. +
  9467. +/**
  9468. + * msgb_push_frame() - Push a SSH message frame header to the buffer.
  9469. + * @msgb: The message buffer
  9470. + * @ty: The type of the frame.
  9471. + * @len: The length of the payload of the frame.
  9472. + * @seq: The sequence ID of the frame/packet.
  9473. + */
  9474. +static inline void msgb_push_frame(struct msgbuf *msgb, u8 ty, u16 len, u8 seq)
  9475. +{
  9476. + u8 *const begin = msgb->ptr;
  9477. +
  9478. + if (WARN_ON(msgb->ptr + sizeof(struct ssh_frame) > msgb->end))
  9479. + return;
  9480. +
  9481. + __msgb_push_u8(msgb, ty); /* Frame type. */
  9482. + __msgb_push_u16(msgb, len); /* Frame payload length. */
  9483. + __msgb_push_u8(msgb, seq); /* Frame sequence ID. */
  9484. +
  9485. + msgb_push_crc(msgb, begin, msgb->ptr - begin);
  9486. +}
  9487. +
  9488. +/**
  9489. + * msgb_push_ack() - Push a SSH ACK frame to the buffer.
  9490. + * @msgb: The message buffer
  9491. + * @seq: The sequence ID of the frame/packet to be ACKed.
  9492. + */
  9493. +static inline void msgb_push_ack(struct msgbuf *msgb, u8 seq)
  9494. +{
  9495. + /* SYN. */
  9496. + msgb_push_syn(msgb);
  9497. +
  9498. + /* ACK-type frame + CRC. */
  9499. + msgb_push_frame(msgb, SSH_FRAME_TYPE_ACK, 0x00, seq);
  9500. +
  9501. + /* Payload CRC (ACK-type frames do not have a payload). */
  9502. + msgb_push_crc(msgb, msgb->ptr, 0);
  9503. +}
  9504. +
  9505. +/**
  9506. + * msgb_push_nak() - Push a SSH NAK frame to the buffer.
  9507. + * @msgb: The message buffer
  9508. + */
  9509. +static inline void msgb_push_nak(struct msgbuf *msgb)
  9510. +{
  9511. + /* SYN. */
  9512. + msgb_push_syn(msgb);
  9513. +
  9514. + /* NAK-type frame + CRC. */
  9515. + msgb_push_frame(msgb, SSH_FRAME_TYPE_NAK, 0x00, 0x00);
  9516. +
  9517. + /* Payload CRC (ACK-type frames do not have a payload). */
  9518. + msgb_push_crc(msgb, msgb->ptr, 0);
  9519. +}
  9520. +
  9521. +/**
  9522. + * msgb_push_cmd() - Push a SSH command frame with payload to the buffer.
  9523. + * @msgb: The message buffer.
  9524. + * @seq: The sequence ID (SEQ) of the frame/packet.
  9525. + * @rqid: The request ID (RQID) of the request contained in the frame.
  9526. + * @rqst: The request to wrap in the frame.
  9527. + */
  9528. +static inline void msgb_push_cmd(struct msgbuf *msgb, u8 seq, u16 rqid,
  9529. + const struct ssam_request *rqst)
  9530. +{
  9531. + const u8 type = SSH_FRAME_TYPE_DATA_SEQ;
  9532. + u8 *cmd;
  9533. +
  9534. + /* SYN. */
  9535. + msgb_push_syn(msgb);
  9536. +
  9537. + /* Command frame + CRC. */
  9538. + msgb_push_frame(msgb, type, sizeof(struct ssh_command) + rqst->length, seq);
  9539. +
  9540. + /* Frame payload: Command struct + payload. */
  9541. + if (WARN_ON(msgb->ptr + sizeof(struct ssh_command) > msgb->end))
  9542. + return;
  9543. +
  9544. + cmd = msgb->ptr;
  9545. +
  9546. + __msgb_push_u8(msgb, SSH_PLD_TYPE_CMD); /* Payload type. */
  9547. + __msgb_push_u8(msgb, rqst->target_category); /* Target category. */
  9548. + __msgb_push_u8(msgb, rqst->target_id); /* Target ID (out). */
  9549. + __msgb_push_u8(msgb, 0x00); /* Target ID (in). */
  9550. + __msgb_push_u8(msgb, rqst->instance_id); /* Instance ID. */
  9551. + __msgb_push_u16(msgb, rqid); /* Request ID. */
  9552. + __msgb_push_u8(msgb, rqst->command_id); /* Command ID. */
  9553. +
  9554. + /* Command payload. */
  9555. + msgb_push_buf(msgb, rqst->payload, rqst->length);
  9556. +
  9557. + /* CRC for command struct + payload. */
  9558. + msgb_push_crc(msgb, cmd, msgb->ptr - cmd);
  9559. +}
  9560. +
  9561. +#endif /* _SURFACE_AGGREGATOR_SSH_MSGB_H */
  9562. diff --git a/drivers/platform/x86/surface_aggregator/ssh_packet_layer.c b/drivers/platform/x86/surface_aggregator/ssh_packet_layer.c
  9563. new file mode 100644
  9564. index 000000000000..5e08049fc3ac
  9565. --- /dev/null
  9566. +++ b/drivers/platform/x86/surface_aggregator/ssh_packet_layer.c
  9567. @@ -0,0 +1,2074 @@
  9568. +// SPDX-License-Identifier: GPL-2.0+
  9569. +/*
  9570. + * SSH packet transport layer.
  9571. + *
  9572. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  9573. + */
  9574. +
  9575. +#include <asm/unaligned.h>
  9576. +#include <linux/atomic.h>
  9577. +#include <linux/error-injection.h>
  9578. +#include <linux/jiffies.h>
  9579. +#include <linux/kfifo.h>
  9580. +#include <linux/kref.h>
  9581. +#include <linux/kthread.h>
  9582. +#include <linux/ktime.h>
  9583. +#include <linux/limits.h>
  9584. +#include <linux/list.h>
  9585. +#include <linux/lockdep.h>
  9586. +#include <linux/serdev.h>
  9587. +#include <linux/slab.h>
  9588. +#include <linux/spinlock.h>
  9589. +#include <linux/workqueue.h>
  9590. +
  9591. +#include <linux/surface_aggregator/serial_hub.h>
  9592. +
  9593. +#include "ssh_msgb.h"
  9594. +#include "ssh_packet_layer.h"
  9595. +#include "ssh_parser.h"
  9596. +
  9597. +#include "trace.h"
  9598. +
  9599. +/*
  9600. + * To simplify reasoning about the code below, we define a few concepts. The
  9601. + * system below is similar to a state-machine for packets, however, there are
  9602. + * too many states to explicitly write them down. To (somewhat) manage the
  9603. + * states and packets we rely on flags, reference counting, and some simple
  9604. + * concepts. State transitions are triggered by actions.
  9605. + *
  9606. + * >> Actions <<
  9607. + *
  9608. + * - submit
  9609. + * - transmission start (process next item in queue)
  9610. + * - transmission finished (guaranteed to never be parallel to transmission
  9611. + * start)
  9612. + * - ACK received
  9613. + * - NAK received (this is equivalent to issuing re-submit for all pending
  9614. + * packets)
  9615. + * - timeout (this is equivalent to re-issuing a submit or canceling)
  9616. + * - cancel (non-pending and pending)
  9617. + *
  9618. + * >> Data Structures, Packet Ownership, General Overview <<
  9619. + *
  9620. + * The code below employs two main data structures: The packet queue,
  9621. + * containing all packets scheduled for transmission, and the set of pending
  9622. + * packets, containing all packets awaiting an ACK.
  9623. + *
  9624. + * Shared ownership of a packet is controlled via reference counting. Inside
  9625. + * the transport system are a total of five packet owners:
  9626. + *
  9627. + * - the packet queue,
  9628. + * - the pending set,
  9629. + * - the transmitter thread,
  9630. + * - the receiver thread (via ACKing), and
  9631. + * - the timeout work item.
  9632. + *
  9633. + * Normal operation is as follows: The initial reference of the packet is
  9634. + * obtained by submitting the packet and queuing it. The receiver thread takes
  9635. + * packets from the queue. By doing this, it does not increment the refcount
  9636. + * but takes over the reference (removing it from the queue). If the packet is
  9637. + * sequenced (i.e. needs to be ACKed by the client), the transmitter thread
  9638. + * sets-up the timeout and adds the packet to the pending set before starting
  9639. + * to transmit it. As the timeout is handled by a reaper task, no additional
  9640. + * reference for it is needed. After the transmit is done, the reference held
  9641. + * by the transmitter thread is dropped. If the packet is unsequenced (i.e.
  9642. + * does not need an ACK), the packet is completed by the transmitter thread
  9643. + * before dropping that reference.
  9644. + *
  9645. + * On receival of an ACK, the receiver thread removes and obtains the
  9646. + * reference to the packet from the pending set. The receiver thread will then
  9647. + * complete the packet and drop its reference.
  9648. + *
  9649. + * On receival of a NAK, the receiver thread re-submits all currently pending
  9650. + * packets.
  9651. + *
  9652. + * Packet timeouts are detected by the timeout reaper. This is a task,
  9653. + * scheduled depending on the earliest packet timeout expiration date,
  9654. + * checking all currently pending packets if their timeout has expired. If the
  9655. + * timeout of a packet has expired, it is re-submitted and the number of tries
  9656. + * of this packet is incremented. If this number reaches its limit, the packet
  9657. + * will be completed with a failure.
  9658. + *
  9659. + * On transmission failure (such as repeated packet timeouts), the completion
  9660. + * callback is immediately run by on thread on which the error was detected.
  9661. + *
  9662. + * To ensure that a packet eventually leaves the system it is marked as
  9663. + * "locked" directly before it is going to be completed or when it is
  9664. + * canceled. Marking a packet as "locked" has the effect that passing and
  9665. + * creating new references of the packet is disallowed. This means that the
  9666. + * packet cannot be added to the queue, the pending set, and the timeout, or
  9667. + * be picked up by the transmitter thread or receiver thread. To remove a
  9668. + * packet from the system it has to be marked as locked and subsequently all
  9669. + * references from the data structures (queue, pending) have to be removed.
  9670. + * References held by threads will eventually be dropped automatically as
  9671. + * their execution progresses.
  9672. + *
  9673. + * Note that the packet completion callback is, in case of success and for a
  9674. + * sequenced packet, guaranteed to run on the receiver thread, thus providing
  9675. + * a way to reliably identify responses to the packet. The packet completion
  9676. + * callback is only run once and it does not indicate that the packet has
  9677. + * fully left the system (for this, one should rely on the release method,
  9678. + * triggered when the reference count of the packet reaches zero). In case of
  9679. + * re-submission (and with somewhat unlikely timing), it may be possible that
  9680. + * the packet is being re-transmitted while the completion callback runs.
  9681. + * Completion will occur both on success and internal error, as well as when
  9682. + * the packet is canceled.
  9683. + *
  9684. + * >> Flags <<
  9685. + *
  9686. + * Flags are used to indicate the state and progression of a packet. Some flags
  9687. + * have stricter guarantees than other:
  9688. + *
  9689. + * - locked
  9690. + * Indicates if the packet is locked. If the packet is locked, passing and/or
  9691. + * creating additional references to the packet is forbidden. The packet thus
  9692. + * may not be queued, dequeued, or removed or added to the pending set. Note
  9693. + * that the packet state flags may still change (e.g. it may be marked as
  9694. + * ACKed, transmitted, ...).
  9695. + *
  9696. + * - completed
  9697. + * Indicates if the packet completion callback has been executed or is about
  9698. + * to be executed. This flag is used to ensure that the packet completion
  9699. + * callback is only run once.
  9700. + *
  9701. + * - queued
  9702. + * Indicates if a packet is present in the submission queue or not. This flag
  9703. + * must only be modified with the queue lock held, and must be coherent to the
  9704. + * presence of the packet in the queue.
  9705. + *
  9706. + * - pending
  9707. + * Indicates if a packet is present in the set of pending packets or not.
  9708. + * This flag must only be modified with the pending lock held, and must be
  9709. + * coherent to the presence of the packet in the pending set.
  9710. + *
  9711. + * - transmitting
  9712. + * Indicates if the packet is currently transmitting. In case of
  9713. + * re-transmissions, it is only safe to wait on the "transmitted" completion
  9714. + * after this flag has been set. The completion will be set both in success
  9715. + * and error case.
  9716. + *
  9717. + * - transmitted
  9718. + * Indicates if the packet has been transmitted. This flag is not cleared by
  9719. + * the system, thus it indicates the first transmission only.
  9720. + *
  9721. + * - acked
  9722. + * Indicates if the packet has been acknowledged by the client. There are no
  9723. + * other guarantees given. For example, the packet may still be canceled
  9724. + * and/or the completion may be triggered an error even though this bit is
  9725. + * set. Rely on the status provided to the completion callback instead.
  9726. + *
  9727. + * - canceled
  9728. + * Indicates if the packet has been canceled from the outside. There are no
  9729. + * other guarantees given. Specifically, the packet may be completed by
  9730. + * another part of the system before the cancellation attempts to complete it.
  9731. + *
  9732. + * >> General Notes <<
  9733. + *
  9734. + * - To avoid deadlocks, if both queue and pending locks are required, the
  9735. + * pending lock must be acquired before the queue lock.
  9736. + *
  9737. + * - The packet priority must be accessed only while holding the queue lock.
  9738. + *
  9739. + * - The packet timestamp must be accessed only while holding the pending
  9740. + * lock.
  9741. + */
  9742. +
  9743. +/*
  9744. + * SSH_PTL_MAX_PACKET_TRIES - Maximum transmission attempts for packet.
  9745. + *
  9746. + * Maximum number of transmission attempts per sequenced packet in case of
  9747. + * time-outs. Must be smaller than 16. If the packet times out after this
  9748. + * amount of tries, the packet will be completed with %-ETIMEDOUT as status
  9749. + * code.
  9750. + */
  9751. +#define SSH_PTL_MAX_PACKET_TRIES 3
  9752. +
  9753. +/*
  9754. + * SSH_PTL_TX_TIMEOUT - Packet transmission timeout.
  9755. + *
  9756. + * Timeout in jiffies for packet transmission via the underlying serial
  9757. + * device. If transmitting the packet takes longer than this timeout, the
  9758. + * packet will be completed with -ETIMEDOUT. It will not be re-submitted.
  9759. + */
  9760. +#define SSH_PTL_TX_TIMEOUT HZ
  9761. +
  9762. +/*
  9763. + * SSH_PTL_PACKET_TIMEOUT - Packet response timeout.
  9764. + *
  9765. + * Timeout as ktime_t delta for ACKs. If we have not received an ACK in this
  9766. + * time-frame after starting transmission, the packet will be re-submitted.
  9767. + */
  9768. +#define SSH_PTL_PACKET_TIMEOUT ms_to_ktime(1000)
  9769. +
  9770. +/*
  9771. + * SSH_PTL_PACKET_TIMEOUT_RESOLUTION - Packet timeout granularity.
  9772. + *
  9773. + * Time-resolution for timeouts. Should be larger than one jiffy to avoid
  9774. + * direct re-scheduling of reaper work_struct.
  9775. + */
  9776. +#define SSH_PTL_PACKET_TIMEOUT_RESOLUTION ms_to_ktime(max(2000 / HZ, 50))
  9777. +
  9778. +/*
  9779. + * SSH_PTL_MAX_PENDING - Maximum number of pending packets.
  9780. + *
  9781. + * Maximum number of sequenced packets concurrently waiting for an ACK.
  9782. + * Packets marked as blocking will not be transmitted while this limit is
  9783. + * reached.
  9784. + */
  9785. +#define SSH_PTL_MAX_PENDING 1
  9786. +
  9787. +/*
  9788. + * SSH_PTL_RX_BUF_LEN - Evaluation-buffer size in bytes.
  9789. + */
  9790. +#define SSH_PTL_RX_BUF_LEN 4096
  9791. +
  9792. +/*
  9793. + * SSH_PTL_RX_FIFO_LEN - Fifo input-buffer size in bytes.
  9794. + */
  9795. +#define SSH_PTL_RX_FIFO_LEN 4096
  9796. +
  9797. +#ifdef CONFIG_SURFACE_AGGREGATOR_ERROR_INJECTION
  9798. +
  9799. +/**
  9800. + * ssh_ptl_should_drop_ack_packet() - Error injection hook to drop ACK packets.
  9801. + *
  9802. + * Useful to test detection and handling of automated re-transmits by the EC.
  9803. + * Specifically of packets that the EC considers not-ACKed but the driver
  9804. + * already considers ACKed (due to dropped ACK). In this case, the EC
  9805. + * re-transmits the packet-to-be-ACKed and the driver should detect it as
  9806. + * duplicate/already handled. Note that the driver should still send an ACK
  9807. + * for the re-transmitted packet.
  9808. + */
  9809. +static noinline bool ssh_ptl_should_drop_ack_packet(void)
  9810. +{
  9811. + return false;
  9812. +}
  9813. +ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_ack_packet, TRUE);
  9814. +
  9815. +/**
  9816. + * ssh_ptl_should_drop_nak_packet() - Error injection hook to drop NAK packets.
  9817. + *
  9818. + * Useful to test/force automated (timeout-based) re-transmit by the EC.
  9819. + * Specifically, packets that have not reached the driver completely/with valid
  9820. + * checksums. Only useful in combination with receival of (injected) bad data.
  9821. + */
  9822. +static noinline bool ssh_ptl_should_drop_nak_packet(void)
  9823. +{
  9824. + return false;
  9825. +}
  9826. +ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_nak_packet, TRUE);
  9827. +
  9828. +/**
  9829. + * ssh_ptl_should_drop_dsq_packet() - Error injection hook to drop sequenced
  9830. + * data packet.
  9831. + *
  9832. + * Useful to test re-transmit timeout of the driver. If the data packet has not
  9833. + * been ACKed after a certain time, the driver should re-transmit the packet up
  9834. + * to limited number of times defined in SSH_PTL_MAX_PACKET_TRIES.
  9835. + */
  9836. +static noinline bool ssh_ptl_should_drop_dsq_packet(void)
  9837. +{
  9838. + return false;
  9839. +}
  9840. +ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_dsq_packet, TRUE);
  9841. +
  9842. +/**
  9843. + * ssh_ptl_should_fail_write() - Error injection hook to make
  9844. + * serdev_device_write() fail.
  9845. + *
  9846. + * Hook to simulate errors in serdev_device_write when transmitting packets.
  9847. + */
  9848. +static noinline int ssh_ptl_should_fail_write(void)
  9849. +{
  9850. + return 0;
  9851. +}
  9852. +ALLOW_ERROR_INJECTION(ssh_ptl_should_fail_write, ERRNO);
  9853. +
  9854. +/**
  9855. + * ssh_ptl_should_corrupt_tx_data() - Error injection hook to simulate invalid
  9856. + * data being sent to the EC.
  9857. + *
  9858. + * Hook to simulate corrupt/invalid data being sent from host (driver) to EC.
  9859. + * Causes the packet data to be actively corrupted by overwriting it with
  9860. + * pre-defined values, such that it becomes invalid, causing the EC to respond
  9861. + * with a NAK packet. Useful to test handling of NAK packets received by the
  9862. + * driver.
  9863. + */
  9864. +static noinline bool ssh_ptl_should_corrupt_tx_data(void)
  9865. +{
  9866. + return false;
  9867. +}
  9868. +ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_tx_data, TRUE);
  9869. +
  9870. +/**
  9871. + * ssh_ptl_should_corrupt_rx_syn() - Error injection hook to simulate invalid
  9872. + * data being sent by the EC.
  9873. + *
  9874. + * Hook to simulate invalid SYN bytes, i.e. an invalid start of messages and
  9875. + * test handling thereof in the driver.
  9876. + */
  9877. +static noinline bool ssh_ptl_should_corrupt_rx_syn(void)
  9878. +{
  9879. + return false;
  9880. +}
  9881. +ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_rx_syn, TRUE);
  9882. +
  9883. +/**
  9884. + * ssh_ptl_should_corrupt_rx_data() - Error injection hook to simulate invalid
  9885. + * data being sent by the EC.
  9886. + *
  9887. + * Hook to simulate invalid data/checksum of the message frame and test handling
  9888. + * thereof in the driver.
  9889. + */
  9890. +static noinline bool ssh_ptl_should_corrupt_rx_data(void)
  9891. +{
  9892. + return false;
  9893. +}
  9894. +ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_rx_data, TRUE);
  9895. +
  9896. +static bool __ssh_ptl_should_drop_ack_packet(struct ssh_packet *packet)
  9897. +{
  9898. + if (likely(!ssh_ptl_should_drop_ack_packet()))
  9899. + return false;
  9900. +
  9901. + trace_ssam_ei_tx_drop_ack_packet(packet);
  9902. + ptl_info(packet->ptl, "packet error injection: dropping ACK packet %p\n",
  9903. + packet);
  9904. +
  9905. + return true;
  9906. +}
  9907. +
  9908. +static bool __ssh_ptl_should_drop_nak_packet(struct ssh_packet *packet)
  9909. +{
  9910. + if (likely(!ssh_ptl_should_drop_nak_packet()))
  9911. + return false;
  9912. +
  9913. + trace_ssam_ei_tx_drop_nak_packet(packet);
  9914. + ptl_info(packet->ptl, "packet error injection: dropping NAK packet %p\n",
  9915. + packet);
  9916. +
  9917. + return true;
  9918. +}
  9919. +
  9920. +static bool __ssh_ptl_should_drop_dsq_packet(struct ssh_packet *packet)
  9921. +{
  9922. + if (likely(!ssh_ptl_should_drop_dsq_packet()))
  9923. + return false;
  9924. +
  9925. + trace_ssam_ei_tx_drop_dsq_packet(packet);
  9926. + ptl_info(packet->ptl,
  9927. + "packet error injection: dropping sequenced data packet %p\n",
  9928. + packet);
  9929. +
  9930. + return true;
  9931. +}
  9932. +
  9933. +static bool ssh_ptl_should_drop_packet(struct ssh_packet *packet)
  9934. +{
  9935. + /* Ignore packets that don't carry any data (i.e. flush). */
  9936. + if (!packet->data.ptr || !packet->data.len)
  9937. + return false;
  9938. +
  9939. + switch (packet->data.ptr[SSH_MSGOFFSET_FRAME(type)]) {
  9940. + case SSH_FRAME_TYPE_ACK:
  9941. + return __ssh_ptl_should_drop_ack_packet(packet);
  9942. +
  9943. + case SSH_FRAME_TYPE_NAK:
  9944. + return __ssh_ptl_should_drop_nak_packet(packet);
  9945. +
  9946. + case SSH_FRAME_TYPE_DATA_SEQ:
  9947. + return __ssh_ptl_should_drop_dsq_packet(packet);
  9948. +
  9949. + default:
  9950. + return false;
  9951. + }
  9952. +}
  9953. +
  9954. +static int ssh_ptl_write_buf(struct ssh_ptl *ptl, struct ssh_packet *packet,
  9955. + const unsigned char *buf, size_t count)
  9956. +{
  9957. + int status;
  9958. +
  9959. + status = ssh_ptl_should_fail_write();
  9960. + if (unlikely(status)) {
  9961. + trace_ssam_ei_tx_fail_write(packet, status);
  9962. + ptl_info(packet->ptl,
  9963. + "packet error injection: simulating transmit error %d, packet %p\n",
  9964. + status, packet);
  9965. +
  9966. + return status;
  9967. + }
  9968. +
  9969. + return serdev_device_write_buf(ptl->serdev, buf, count);
  9970. +}
  9971. +
  9972. +static void ssh_ptl_tx_inject_invalid_data(struct ssh_packet *packet)
  9973. +{
  9974. + /* Ignore packets that don't carry any data (i.e. flush). */
  9975. + if (!packet->data.ptr || !packet->data.len)
  9976. + return;
  9977. +
  9978. + /* Only allow sequenced data packets to be modified. */
  9979. + if (packet->data.ptr[SSH_MSGOFFSET_FRAME(type)] != SSH_FRAME_TYPE_DATA_SEQ)
  9980. + return;
  9981. +
  9982. + if (likely(!ssh_ptl_should_corrupt_tx_data()))
  9983. + return;
  9984. +
  9985. + trace_ssam_ei_tx_corrupt_data(packet);
  9986. + ptl_info(packet->ptl,
  9987. + "packet error injection: simulating invalid transmit data on packet %p\n",
  9988. + packet);
  9989. +
  9990. + /*
  9991. + * NB: The value 0xb3 has been chosen more or less randomly so that it
  9992. + * doesn't have any (major) overlap with the SYN bytes (aa 55) and is
  9993. + * non-trivial (i.e. non-zero, non-0xff).
  9994. + */
  9995. + memset(packet->data.ptr, 0xb3, packet->data.len);
  9996. +}
  9997. +
  9998. +static void ssh_ptl_rx_inject_invalid_syn(struct ssh_ptl *ptl,
  9999. + struct ssam_span *data)
  10000. +{
  10001. + struct ssam_span frame;
  10002. +
  10003. + /* Check if there actually is something to corrupt. */
  10004. + if (!sshp_find_syn(data, &frame))
  10005. + return;
  10006. +
  10007. + if (likely(!ssh_ptl_should_corrupt_rx_syn()))
  10008. + return;
  10009. +
  10010. + trace_ssam_ei_rx_corrupt_syn(data->len);
  10011. +
  10012. + data->ptr[1] = 0xb3; /* Set second byte of SYN to "random" value. */
  10013. +}
  10014. +
  10015. +static void ssh_ptl_rx_inject_invalid_data(struct ssh_ptl *ptl,
  10016. + struct ssam_span *frame)
  10017. +{
  10018. + size_t payload_len, message_len;
  10019. + struct ssh_frame *sshf;
  10020. +
  10021. + /* Ignore incomplete messages, will get handled once it's complete. */
  10022. + if (frame->len < SSH_MESSAGE_LENGTH(0))
  10023. + return;
  10024. +
  10025. + /* Ignore incomplete messages, part 2. */
  10026. + payload_len = get_unaligned_le16(&frame->ptr[SSH_MSGOFFSET_FRAME(len)]);
  10027. + message_len = SSH_MESSAGE_LENGTH(payload_len);
  10028. + if (frame->len < message_len)
  10029. + return;
  10030. +
  10031. + if (likely(!ssh_ptl_should_corrupt_rx_data()))
  10032. + return;
  10033. +
  10034. + sshf = (struct ssh_frame *)&frame->ptr[SSH_MSGOFFSET_FRAME(type)];
  10035. + trace_ssam_ei_rx_corrupt_data(sshf);
  10036. +
  10037. + /*
  10038. + * Flip bits in first byte of payload checksum. This is basically
  10039. + * equivalent to a payload/frame data error without us having to worry
  10040. + * about (the, arguably pretty small, probability of) accidental
  10041. + * checksum collisions.
  10042. + */
  10043. + frame->ptr[frame->len - 2] = ~frame->ptr[frame->len - 2];
  10044. +}
  10045. +
  10046. +#else /* CONFIG_SURFACE_AGGREGATOR_ERROR_INJECTION */
  10047. +
  10048. +static inline bool ssh_ptl_should_drop_packet(struct ssh_packet *packet)
  10049. +{
  10050. + return false;
  10051. +}
  10052. +
  10053. +static inline int ssh_ptl_write_buf(struct ssh_ptl *ptl,
  10054. + struct ssh_packet *packet,
  10055. + const unsigned char *buf,
  10056. + size_t count)
  10057. +{
  10058. + return serdev_device_write_buf(ptl->serdev, buf, count);
  10059. +}
  10060. +
  10061. +static inline void ssh_ptl_tx_inject_invalid_data(struct ssh_packet *packet)
  10062. +{
  10063. +}
  10064. +
  10065. +static inline void ssh_ptl_rx_inject_invalid_syn(struct ssh_ptl *ptl,
  10066. + struct ssam_span *data)
  10067. +{
  10068. +}
  10069. +
  10070. +static inline void ssh_ptl_rx_inject_invalid_data(struct ssh_ptl *ptl,
  10071. + struct ssam_span *frame)
  10072. +{
  10073. +}
  10074. +
  10075. +#endif /* CONFIG_SURFACE_AGGREGATOR_ERROR_INJECTION */
  10076. +
  10077. +static void __ssh_ptl_packet_release(struct kref *kref)
  10078. +{
  10079. + struct ssh_packet *p = container_of(kref, struct ssh_packet, refcnt);
  10080. +
  10081. + trace_ssam_packet_release(p);
  10082. +
  10083. + ptl_dbg_cond(p->ptl, "ptl: releasing packet %p\n", p);
  10084. + p->ops->release(p);
  10085. +}
  10086. +
  10087. +/**
  10088. + * ssh_packet_get() - Increment reference count of packet.
  10089. + * @packet: The packet to increment the reference count of.
  10090. + *
  10091. + * Increments the reference count of the given packet. See ssh_packet_put()
  10092. + * for the counter-part of this function.
  10093. + *
  10094. + * Return: Returns the packet provided as input.
  10095. + */
  10096. +struct ssh_packet *ssh_packet_get(struct ssh_packet *packet)
  10097. +{
  10098. + if (packet)
  10099. + kref_get(&packet->refcnt);
  10100. + return packet;
  10101. +}
  10102. +EXPORT_SYMBOL_GPL(ssh_packet_get);
  10103. +
  10104. +/**
  10105. + * ssh_packet_put() - Decrement reference count of packet.
  10106. + * @packet: The packet to decrement the reference count of.
  10107. + *
  10108. + * If the reference count reaches zero, the ``release`` callback specified in
  10109. + * the packet's &struct ssh_packet_ops, i.e. ``packet->ops->release``, will be
  10110. + * called.
  10111. + *
  10112. + * See ssh_packet_get() for the counter-part of this function.
  10113. + */
  10114. +void ssh_packet_put(struct ssh_packet *packet)
  10115. +{
  10116. + if (packet)
  10117. + kref_put(&packet->refcnt, __ssh_ptl_packet_release);
  10118. +}
  10119. +EXPORT_SYMBOL_GPL(ssh_packet_put);
  10120. +
  10121. +static u8 ssh_packet_get_seq(struct ssh_packet *packet)
  10122. +{
  10123. + return packet->data.ptr[SSH_MSGOFFSET_FRAME(seq)];
  10124. +}
  10125. +
  10126. +/**
  10127. + * ssh_packet_init() - Initialize SSH packet.
  10128. + * @packet: The packet to initialize.
  10129. + * @type: Type-flags of the packet.
  10130. + * @priority: Priority of the packet. See SSH_PACKET_PRIORITY() for details.
  10131. + * @ops: Packet operations.
  10132. + *
  10133. + * Initializes the given SSH packet. Sets the transmission buffer pointer to
  10134. + * %NULL and the transmission buffer length to zero. For data-type packets,
  10135. + * this buffer has to be set separately via ssh_packet_set_data() before
  10136. + * submission, and must contain a valid SSH message, i.e. frame with optional
  10137. + * payload of any type.
  10138. + */
  10139. +void ssh_packet_init(struct ssh_packet *packet, unsigned long type,
  10140. + u8 priority, const struct ssh_packet_ops *ops)
  10141. +{
  10142. + kref_init(&packet->refcnt);
  10143. +
  10144. + packet->ptl = NULL;
  10145. + INIT_LIST_HEAD(&packet->queue_node);
  10146. + INIT_LIST_HEAD(&packet->pending_node);
  10147. +
  10148. + packet->state = type & SSH_PACKET_FLAGS_TY_MASK;
  10149. + packet->priority = priority;
  10150. + packet->timestamp = KTIME_MAX;
  10151. +
  10152. + packet->data.ptr = NULL;
  10153. + packet->data.len = 0;
  10154. +
  10155. + packet->ops = ops;
  10156. +}
  10157. +
  10158. +static struct kmem_cache *ssh_ctrl_packet_cache;
  10159. +
  10160. +/**
  10161. + * ssh_ctrl_packet_cache_init() - Initialize the control packet cache.
  10162. + */
  10163. +int ssh_ctrl_packet_cache_init(void)
  10164. +{
  10165. + const unsigned int size = sizeof(struct ssh_packet) + SSH_MSG_LEN_CTRL;
  10166. + const unsigned int align = __alignof__(struct ssh_packet);
  10167. + struct kmem_cache *cache;
  10168. +
  10169. + cache = kmem_cache_create("ssam_ctrl_packet", size, align, 0, NULL);
  10170. + if (!cache)
  10171. + return -ENOMEM;
  10172. +
  10173. + ssh_ctrl_packet_cache = cache;
  10174. + return 0;
  10175. +}
  10176. +
  10177. +/**
  10178. + * ssh_ctrl_packet_cache_destroy() - Deinitialize the control packet cache.
  10179. + */
  10180. +void ssh_ctrl_packet_cache_destroy(void)
  10181. +{
  10182. + kmem_cache_destroy(ssh_ctrl_packet_cache);
  10183. + ssh_ctrl_packet_cache = NULL;
  10184. +}
  10185. +
  10186. +/**
  10187. + * ssh_ctrl_packet_alloc() - Allocate packet from control packet cache.
  10188. + * @packet: Where the pointer to the newly allocated packet should be stored.
  10189. + * @buffer: The buffer corresponding to this packet.
  10190. + * @flags: Flags used for allocation.
  10191. + *
  10192. + * Allocates a packet and corresponding transport buffer from the control
  10193. + * packet cache. Sets the packet's buffer reference to the allocated buffer.
  10194. + * The packet must be freed via ssh_ctrl_packet_free(), which will also free
  10195. + * the corresponding buffer. The corresponding buffer must not be freed
  10196. + * separately. Intended to be used with %ssh_ptl_ctrl_packet_ops as packet
  10197. + * operations.
  10198. + *
  10199. + * Return: Returns zero on success, %-ENOMEM if the allocation failed.
  10200. + */
  10201. +static int ssh_ctrl_packet_alloc(struct ssh_packet **packet,
  10202. + struct ssam_span *buffer, gfp_t flags)
  10203. +{
  10204. + *packet = kmem_cache_alloc(ssh_ctrl_packet_cache, flags);
  10205. + if (!*packet)
  10206. + return -ENOMEM;
  10207. +
  10208. + buffer->ptr = (u8 *)(*packet + 1);
  10209. + buffer->len = SSH_MSG_LEN_CTRL;
  10210. +
  10211. + trace_ssam_ctrl_packet_alloc(*packet, buffer->len);
  10212. + return 0;
  10213. +}
  10214. +
  10215. +/**
  10216. + * ssh_ctrl_packet_free() - Free packet allocated from control packet cache.
  10217. + * @p: The packet to free.
  10218. + */
  10219. +static void ssh_ctrl_packet_free(struct ssh_packet *p)
  10220. +{
  10221. + trace_ssam_ctrl_packet_free(p);
  10222. + kmem_cache_free(ssh_ctrl_packet_cache, p);
  10223. +}
  10224. +
  10225. +static const struct ssh_packet_ops ssh_ptl_ctrl_packet_ops = {
  10226. + .complete = NULL,
  10227. + .release = ssh_ctrl_packet_free,
  10228. +};
  10229. +
  10230. +static void ssh_ptl_timeout_reaper_mod(struct ssh_ptl *ptl, ktime_t now,
  10231. + ktime_t expires)
  10232. +{
  10233. + unsigned long delta = msecs_to_jiffies(ktime_ms_delta(expires, now));
  10234. + ktime_t aexp = ktime_add(expires, SSH_PTL_PACKET_TIMEOUT_RESOLUTION);
  10235. +
  10236. + spin_lock(&ptl->rtx_timeout.lock);
  10237. +
  10238. + /* Re-adjust / schedule reaper only if it is above resolution delta. */
  10239. + if (ktime_before(aexp, ptl->rtx_timeout.expires)) {
  10240. + ptl->rtx_timeout.expires = expires;
  10241. + mod_delayed_work(system_wq, &ptl->rtx_timeout.reaper, delta);
  10242. + }
  10243. +
  10244. + spin_unlock(&ptl->rtx_timeout.lock);
  10245. +}
  10246. +
  10247. +/* Must be called with queue lock held. */
  10248. +static void ssh_packet_next_try(struct ssh_packet *p)
  10249. +{
  10250. + u8 base = ssh_packet_priority_get_base(p->priority);
  10251. + u8 try = ssh_packet_priority_get_try(p->priority);
  10252. +
  10253. + lockdep_assert_held(&p->ptl->queue.lock);
  10254. +
  10255. + /*
  10256. + * Ensure that we write the priority in one go via WRITE_ONCE() so we
  10257. + * can access it via READ_ONCE() for tracing. Note that other access
  10258. + * is guarded by the queue lock, so no need to use READ_ONCE() there.
  10259. + */
  10260. + WRITE_ONCE(p->priority, __SSH_PACKET_PRIORITY(base, try + 1));
  10261. +}
  10262. +
  10263. +/* Must be called with queue lock held. */
  10264. +static struct list_head *__ssh_ptl_queue_find_entrypoint(struct ssh_packet *p)
  10265. +{
  10266. + struct list_head *head;
  10267. + struct ssh_packet *q;
  10268. +
  10269. + lockdep_assert_held(&p->ptl->queue.lock);
  10270. +
  10271. + /*
  10272. + * We generally assume that there are less control (ACK/NAK) packets
  10273. + * and re-submitted data packets as there are normal data packets (at
  10274. + * least in situations in which many packets are queued; if there
  10275. + * aren't many packets queued the decision on how to iterate should be
  10276. + * basically irrelevant; the number of control/data packets is more or
  10277. + * less limited via the maximum number of pending packets). Thus, when
  10278. + * inserting a control or re-submitted data packet, (determined by
  10279. + * their priority), we search from front to back. Normal data packets
  10280. + * are, usually queued directly at the tail of the queue, so for those
  10281. + * search from back to front.
  10282. + */
  10283. +
  10284. + if (p->priority > SSH_PACKET_PRIORITY(DATA, 0)) {
  10285. + list_for_each(head, &p->ptl->queue.head) {
  10286. + q = list_entry(head, struct ssh_packet, queue_node);
  10287. +
  10288. + if (q->priority < p->priority)
  10289. + break;
  10290. + }
  10291. + } else {
  10292. + list_for_each_prev(head, &p->ptl->queue.head) {
  10293. + q = list_entry(head, struct ssh_packet, queue_node);
  10294. +
  10295. + if (q->priority >= p->priority) {
  10296. + head = head->next;
  10297. + break;
  10298. + }
  10299. + }
  10300. + }
  10301. +
  10302. + return head;
  10303. +}
  10304. +
  10305. +/* Must be called with queue lock held. */
  10306. +static int __ssh_ptl_queue_push(struct ssh_packet *packet)
  10307. +{
  10308. + struct ssh_ptl *ptl = packet->ptl;
  10309. + struct list_head *head;
  10310. +
  10311. + lockdep_assert_held(&ptl->queue.lock);
  10312. +
  10313. + if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
  10314. + return -ESHUTDOWN;
  10315. +
  10316. + /* Avoid further transitions when canceling/completing. */
  10317. + if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state))
  10318. + return -EINVAL;
  10319. +
  10320. + /* If this packet has already been queued, do not add it. */
  10321. + if (test_and_set_bit(SSH_PACKET_SF_QUEUED_BIT, &packet->state))
  10322. + return -EALREADY;
  10323. +
  10324. + head = __ssh_ptl_queue_find_entrypoint(packet);
  10325. +
  10326. + list_add_tail(&ssh_packet_get(packet)->queue_node, head);
  10327. + return 0;
  10328. +}
  10329. +
  10330. +static int ssh_ptl_queue_push(struct ssh_packet *packet)
  10331. +{
  10332. + int status;
  10333. +
  10334. + spin_lock(&packet->ptl->queue.lock);
  10335. + status = __ssh_ptl_queue_push(packet);
  10336. + spin_unlock(&packet->ptl->queue.lock);
  10337. +
  10338. + return status;
  10339. +}
  10340. +
  10341. +static void ssh_ptl_queue_remove(struct ssh_packet *packet)
  10342. +{
  10343. + struct ssh_ptl *ptl = packet->ptl;
  10344. +
  10345. + spin_lock(&ptl->queue.lock);
  10346. +
  10347. + if (!test_and_clear_bit(SSH_PACKET_SF_QUEUED_BIT, &packet->state)) {
  10348. + spin_unlock(&ptl->queue.lock);
  10349. + return;
  10350. + }
  10351. +
  10352. + list_del(&packet->queue_node);
  10353. +
  10354. + spin_unlock(&ptl->queue.lock);
  10355. + ssh_packet_put(packet);
  10356. +}
  10357. +
  10358. +static void ssh_ptl_pending_push(struct ssh_packet *p)
  10359. +{
  10360. + struct ssh_ptl *ptl = p->ptl;
  10361. + const ktime_t timestamp = ktime_get_coarse_boottime();
  10362. + const ktime_t timeout = ptl->rtx_timeout.timeout;
  10363. +
  10364. + /*
  10365. + * Note: We can get the time for the timestamp before acquiring the
  10366. + * lock as this is the only place we're setting it and this function
  10367. + * is called only from the transmitter thread. Thus it is not possible
  10368. + * to overwrite the timestamp with an outdated value below.
  10369. + */
  10370. +
  10371. + spin_lock(&ptl->pending.lock);
  10372. +
  10373. + /* If we are canceling/completing this packet, do not add it. */
  10374. + if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state)) {
  10375. + spin_unlock(&ptl->pending.lock);
  10376. + return;
  10377. + }
  10378. +
  10379. + /*
  10380. + * On re-submission, the packet has already been added the pending
  10381. + * set. We still need to update the timestamp as the packet timeout is
  10382. + * reset for each (re-)submission.
  10383. + */
  10384. + p->timestamp = timestamp;
  10385. +
  10386. + /* In case it is already pending (e.g. re-submission), do not add it. */
  10387. + if (!test_and_set_bit(SSH_PACKET_SF_PENDING_BIT, &p->state)) {
  10388. + atomic_inc(&ptl->pending.count);
  10389. + list_add_tail(&ssh_packet_get(p)->pending_node, &ptl->pending.head);
  10390. + }
  10391. +
  10392. + spin_unlock(&ptl->pending.lock);
  10393. +
  10394. + /* Arm/update timeout reaper. */
  10395. + ssh_ptl_timeout_reaper_mod(ptl, timestamp, timestamp + timeout);
  10396. +}
  10397. +
  10398. +static void ssh_ptl_pending_remove(struct ssh_packet *packet)
  10399. +{
  10400. + struct ssh_ptl *ptl = packet->ptl;
  10401. +
  10402. + spin_lock(&ptl->pending.lock);
  10403. +
  10404. + if (!test_and_clear_bit(SSH_PACKET_SF_PENDING_BIT, &packet->state)) {
  10405. + spin_unlock(&ptl->pending.lock);
  10406. + return;
  10407. + }
  10408. +
  10409. + list_del(&packet->pending_node);
  10410. + atomic_dec(&ptl->pending.count);
  10411. +
  10412. + spin_unlock(&ptl->pending.lock);
  10413. +
  10414. + ssh_packet_put(packet);
  10415. +}
  10416. +
  10417. +/* Warning: Does not check/set "completed" bit. */
  10418. +static void __ssh_ptl_complete(struct ssh_packet *p, int status)
  10419. +{
  10420. + struct ssh_ptl *ptl = READ_ONCE(p->ptl);
  10421. +
  10422. + trace_ssam_packet_complete(p, status);
  10423. + ptl_dbg_cond(ptl, "ptl: completing packet %p (status: %d)\n", p, status);
  10424. +
  10425. + if (p->ops->complete)
  10426. + p->ops->complete(p, status);
  10427. +}
  10428. +
  10429. +static void ssh_ptl_remove_and_complete(struct ssh_packet *p, int status)
  10430. +{
  10431. + /*
  10432. + * A call to this function should in general be preceded by
  10433. + * set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->flags) to avoid re-adding the
  10434. + * packet to the structures it's going to be removed from.
  10435. + *
  10436. + * The set_bit call does not need explicit memory barriers as the
  10437. + * implicit barrier of the test_and_set_bit() call below ensure that the
  10438. + * flag is visible before we actually attempt to remove the packet.
  10439. + */
  10440. +
  10441. + if (test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
  10442. + return;
  10443. +
  10444. + ssh_ptl_queue_remove(p);
  10445. + ssh_ptl_pending_remove(p);
  10446. +
  10447. + __ssh_ptl_complete(p, status);
  10448. +}
  10449. +
  10450. +static bool ssh_ptl_tx_can_process(struct ssh_packet *packet)
  10451. +{
  10452. + struct ssh_ptl *ptl = packet->ptl;
  10453. +
  10454. + if (test_bit(SSH_PACKET_TY_FLUSH_BIT, &packet->state))
  10455. + return !atomic_read(&ptl->pending.count);
  10456. +
  10457. + /* We can always process non-blocking packets. */
  10458. + if (!test_bit(SSH_PACKET_TY_BLOCKING_BIT, &packet->state))
  10459. + return true;
  10460. +
  10461. + /* If we are already waiting for this packet, send it again. */
  10462. + if (test_bit(SSH_PACKET_SF_PENDING_BIT, &packet->state))
  10463. + return true;
  10464. +
  10465. + /* Otherwise: Check if we have the capacity to send. */
  10466. + return atomic_read(&ptl->pending.count) < SSH_PTL_MAX_PENDING;
  10467. +}
  10468. +
  10469. +static struct ssh_packet *ssh_ptl_tx_pop(struct ssh_ptl *ptl)
  10470. +{
  10471. + struct ssh_packet *packet = ERR_PTR(-ENOENT);
  10472. + struct ssh_packet *p, *n;
  10473. +
  10474. + spin_lock(&ptl->queue.lock);
  10475. + list_for_each_entry_safe(p, n, &ptl->queue.head, queue_node) {
  10476. + /*
  10477. + * If we are canceling or completing this packet, ignore it.
  10478. + * It's going to be removed from this queue shortly.
  10479. + */
  10480. + if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
  10481. + continue;
  10482. +
  10483. + /*
  10484. + * Packets should be ordered non-blocking/to-be-resent first.
  10485. + * If we cannot process this packet, assume that we can't
  10486. + * process any following packet either and abort.
  10487. + */
  10488. + if (!ssh_ptl_tx_can_process(p)) {
  10489. + packet = ERR_PTR(-EBUSY);
  10490. + break;
  10491. + }
  10492. +
  10493. + /*
  10494. + * We are allowed to change the state now. Remove it from the
  10495. + * queue and mark it as being transmitted.
  10496. + */
  10497. +
  10498. + list_del(&p->queue_node);
  10499. +
  10500. + set_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &p->state);
  10501. + /* Ensure that state never gets zero. */
  10502. + smp_mb__before_atomic();
  10503. + clear_bit(SSH_PACKET_SF_QUEUED_BIT, &p->state);
  10504. +
  10505. + /*
  10506. + * Update number of tries. This directly influences the
  10507. + * priority in case the packet is re-submitted (e.g. via
  10508. + * timeout/NAK). Note that all reads and writes to the
  10509. + * priority after the first submission are guarded by the
  10510. + * queue lock.
  10511. + */
  10512. + ssh_packet_next_try(p);
  10513. +
  10514. + packet = p;
  10515. + break;
  10516. + }
  10517. + spin_unlock(&ptl->queue.lock);
  10518. +
  10519. + return packet;
  10520. +}
  10521. +
  10522. +static struct ssh_packet *ssh_ptl_tx_next(struct ssh_ptl *ptl)
  10523. +{
  10524. + struct ssh_packet *p;
  10525. +
  10526. + p = ssh_ptl_tx_pop(ptl);
  10527. + if (IS_ERR(p))
  10528. + return p;
  10529. +
  10530. + if (test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &p->state)) {
  10531. + ptl_dbg(ptl, "ptl: transmitting sequenced packet %p\n", p);
  10532. + ssh_ptl_pending_push(p);
  10533. + } else {
  10534. + ptl_dbg(ptl, "ptl: transmitting non-sequenced packet %p\n", p);
  10535. + }
  10536. +
  10537. + return p;
  10538. +}
  10539. +
  10540. +static void ssh_ptl_tx_compl_success(struct ssh_packet *packet)
  10541. +{
  10542. + struct ssh_ptl *ptl = packet->ptl;
  10543. +
  10544. + ptl_dbg(ptl, "ptl: successfully transmitted packet %p\n", packet);
  10545. +
  10546. + /* Transition state to "transmitted". */
  10547. + set_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &packet->state);
  10548. + /* Ensure that state never gets zero. */
  10549. + smp_mb__before_atomic();
  10550. + clear_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &packet->state);
  10551. +
  10552. + /* If the packet is unsequenced, we're done: Lock and complete. */
  10553. + if (!test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &packet->state)) {
  10554. + set_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state);
  10555. + ssh_ptl_remove_and_complete(packet, 0);
  10556. + }
  10557. +
  10558. + /*
  10559. + * Notify that a packet transmission has finished. In general we're only
  10560. + * waiting for one packet (if any), so wake_up_all should be fine.
  10561. + */
  10562. + wake_up_all(&ptl->tx.packet_wq);
  10563. +}
  10564. +
  10565. +static void ssh_ptl_tx_compl_error(struct ssh_packet *packet, int status)
  10566. +{
  10567. + /* Transmission failure: Lock the packet and try to complete it. */
  10568. + set_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state);
  10569. + /* Ensure that state never gets zero. */
  10570. + smp_mb__before_atomic();
  10571. + clear_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &packet->state);
  10572. +
  10573. + ptl_err(packet->ptl, "ptl: transmission error: %d\n", status);
  10574. + ptl_dbg(packet->ptl, "ptl: failed to transmit packet: %p\n", packet);
  10575. +
  10576. + ssh_ptl_remove_and_complete(packet, status);
  10577. +
  10578. + /*
  10579. + * Notify that a packet transmission has finished. In general we're only
  10580. + * waiting for one packet (if any), so wake_up_all should be fine.
  10581. + */
  10582. + wake_up_all(&packet->ptl->tx.packet_wq);
  10583. +}
  10584. +
  10585. +static long ssh_ptl_tx_wait_packet(struct ssh_ptl *ptl)
  10586. +{
  10587. + int status;
  10588. +
  10589. + status = wait_for_completion_interruptible(&ptl->tx.thread_cplt_pkt);
  10590. + reinit_completion(&ptl->tx.thread_cplt_pkt);
  10591. +
  10592. + /*
  10593. + * Ensure completion is cleared before continuing to avoid lost update
  10594. + * problems.
  10595. + */
  10596. + smp_mb__after_atomic();
  10597. +
  10598. + return status;
  10599. +}
  10600. +
  10601. +static long ssh_ptl_tx_wait_transfer(struct ssh_ptl *ptl, long timeout)
  10602. +{
  10603. + long status;
  10604. +
  10605. + status = wait_for_completion_interruptible_timeout(&ptl->tx.thread_cplt_tx,
  10606. + timeout);
  10607. + reinit_completion(&ptl->tx.thread_cplt_tx);
  10608. +
  10609. + /*
  10610. + * Ensure completion is cleared before continuing to avoid lost update
  10611. + * problems.
  10612. + */
  10613. + smp_mb__after_atomic();
  10614. +
  10615. + return status;
  10616. +}
  10617. +
  10618. +static int ssh_ptl_tx_packet(struct ssh_ptl *ptl, struct ssh_packet *packet)
  10619. +{
  10620. + long timeout = SSH_PTL_TX_TIMEOUT;
  10621. + size_t offset = 0;
  10622. +
  10623. + /* Note: Flush-packets don't have any data. */
  10624. + if (unlikely(!packet->data.ptr))
  10625. + return 0;
  10626. +
  10627. + /* Error injection: drop packet to simulate transmission problem. */
  10628. + if (ssh_ptl_should_drop_packet(packet))
  10629. + return 0;
  10630. +
  10631. + /* Error injection: simulate invalid packet data. */
  10632. + ssh_ptl_tx_inject_invalid_data(packet);
  10633. +
  10634. + ptl_dbg(ptl, "tx: sending data (length: %zu)\n", packet->data.len);
  10635. + print_hex_dump_debug("tx: ", DUMP_PREFIX_OFFSET, 16, 1,
  10636. + packet->data.ptr, packet->data.len, false);
  10637. +
  10638. + do {
  10639. + ssize_t status, len;
  10640. + u8 *buf;
  10641. +
  10642. + buf = packet->data.ptr + offset;
  10643. + len = packet->data.len - offset;
  10644. +
  10645. + status = ssh_ptl_write_buf(ptl, packet, buf, len);
  10646. + if (status < 0)
  10647. + return status;
  10648. +
  10649. + if (status == len)
  10650. + return 0;
  10651. +
  10652. + offset += status;
  10653. +
  10654. + timeout = ssh_ptl_tx_wait_transfer(ptl, timeout);
  10655. + if (kthread_should_stop() || !atomic_read(&ptl->tx.running))
  10656. + return -ESHUTDOWN;
  10657. +
  10658. + if (timeout < 0)
  10659. + return -EINTR;
  10660. +
  10661. + if (timeout == 0)
  10662. + return -ETIMEDOUT;
  10663. + } while (true);
  10664. +}
  10665. +
  10666. +static int ssh_ptl_tx_threadfn(void *data)
  10667. +{
  10668. + struct ssh_ptl *ptl = data;
  10669. +
  10670. + while (!kthread_should_stop() && atomic_read(&ptl->tx.running)) {
  10671. + struct ssh_packet *packet;
  10672. + int status;
  10673. +
  10674. + /* Try to get the next packet. */
  10675. + packet = ssh_ptl_tx_next(ptl);
  10676. +
  10677. + /* If no packet can be processed, we are done. */
  10678. + if (IS_ERR(packet)) {
  10679. + ssh_ptl_tx_wait_packet(ptl);
  10680. + continue;
  10681. + }
  10682. +
  10683. + /* Transfer and complete packet. */
  10684. + status = ssh_ptl_tx_packet(ptl, packet);
  10685. + if (status)
  10686. + ssh_ptl_tx_compl_error(packet, status);
  10687. + else
  10688. + ssh_ptl_tx_compl_success(packet);
  10689. +
  10690. + ssh_packet_put(packet);
  10691. + }
  10692. +
  10693. + return 0;
  10694. +}
  10695. +
  10696. +/**
  10697. + * ssh_ptl_tx_wakeup_packet() - Wake up packet transmitter thread for new
  10698. + * packet.
  10699. + * @ptl: The packet transport layer.
  10700. + *
  10701. + * Wakes up the packet transmitter thread, notifying it that a new packet has
  10702. + * arrived and is ready for transfer. If the packet transport layer has been
  10703. + * shut down, calls to this function will be ignored.
  10704. + */
  10705. +static void ssh_ptl_tx_wakeup_packet(struct ssh_ptl *ptl)
  10706. +{
  10707. + if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
  10708. + return;
  10709. +
  10710. + complete(&ptl->tx.thread_cplt_pkt);
  10711. +}
  10712. +
  10713. +/**
  10714. + * ssh_ptl_tx_start() - Start packet transmitter thread.
  10715. + * @ptl: The packet transport layer.
  10716. + *
  10717. + * Return: Returns zero on success, a negative error code on failure.
  10718. + */
  10719. +int ssh_ptl_tx_start(struct ssh_ptl *ptl)
  10720. +{
  10721. + atomic_set_release(&ptl->tx.running, 1);
  10722. +
  10723. + ptl->tx.thread = kthread_run(ssh_ptl_tx_threadfn, ptl, "ssam_serial_hub-tx");
  10724. + if (IS_ERR(ptl->tx.thread))
  10725. + return PTR_ERR(ptl->tx.thread);
  10726. +
  10727. + return 0;
  10728. +}
  10729. +
  10730. +/**
  10731. + * ssh_ptl_tx_stop() - Stop packet transmitter thread.
  10732. + * @ptl: The packet transport layer.
  10733. + *
  10734. + * Return: Returns zero on success, a negative error code on failure.
  10735. + */
  10736. +int ssh_ptl_tx_stop(struct ssh_ptl *ptl)
  10737. +{
  10738. + int status = 0;
  10739. +
  10740. + if (!IS_ERR_OR_NULL(ptl->tx.thread)) {
  10741. + /* Tell thread to stop. */
  10742. + atomic_set_release(&ptl->tx.running, 0);
  10743. +
  10744. + /*
  10745. + * Wake up thread in case it is paused. Do not use wakeup
  10746. + * helpers as this may be called when the shutdown bit has
  10747. + * already been set.
  10748. + */
  10749. + complete(&ptl->tx.thread_cplt_pkt);
  10750. + complete(&ptl->tx.thread_cplt_tx);
  10751. +
  10752. + /* Finally, wait for thread to stop. */
  10753. + status = kthread_stop(ptl->tx.thread);
  10754. + ptl->tx.thread = NULL;
  10755. + }
  10756. +
  10757. + return status;
  10758. +}
  10759. +
  10760. +static struct ssh_packet *ssh_ptl_ack_pop(struct ssh_ptl *ptl, u8 seq_id)
  10761. +{
  10762. + struct ssh_packet *packet = ERR_PTR(-ENOENT);
  10763. + struct ssh_packet *p, *n;
  10764. +
  10765. + spin_lock(&ptl->pending.lock);
  10766. + list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
  10767. + /*
  10768. + * We generally expect packets to be in order, so first packet
  10769. + * to be added to pending is first to be sent, is first to be
  10770. + * ACKed.
  10771. + */
  10772. + if (unlikely(ssh_packet_get_seq(p) != seq_id))
  10773. + continue;
  10774. +
  10775. + /*
  10776. + * In case we receive an ACK while handling a transmission
  10777. + * error completion. The packet will be removed shortly.
  10778. + */
  10779. + if (unlikely(test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))) {
  10780. + packet = ERR_PTR(-EPERM);
  10781. + break;
  10782. + }
  10783. +
  10784. + /*
  10785. + * Mark the packet as ACKed and remove it from pending by
  10786. + * removing its node and decrementing the pending counter.
  10787. + */
  10788. + set_bit(SSH_PACKET_SF_ACKED_BIT, &p->state);
  10789. + /* Ensure that state never gets zero. */
  10790. + smp_mb__before_atomic();
  10791. + clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
  10792. +
  10793. + atomic_dec(&ptl->pending.count);
  10794. + list_del(&p->pending_node);
  10795. + packet = p;
  10796. +
  10797. + break;
  10798. + }
  10799. + spin_unlock(&ptl->pending.lock);
  10800. +
  10801. + return packet;
  10802. +}
  10803. +
  10804. +static void ssh_ptl_wait_until_transmitted(struct ssh_packet *packet)
  10805. +{
  10806. + wait_event(packet->ptl->tx.packet_wq,
  10807. + test_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &packet->state) ||
  10808. + test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state));
  10809. +}
  10810. +
  10811. +static void ssh_ptl_acknowledge(struct ssh_ptl *ptl, u8 seq)
  10812. +{
  10813. + struct ssh_packet *p;
  10814. +
  10815. + p = ssh_ptl_ack_pop(ptl, seq);
  10816. + if (IS_ERR(p)) {
  10817. + if (PTR_ERR(p) == -ENOENT) {
  10818. + /*
  10819. + * The packet has not been found in the set of pending
  10820. + * packets.
  10821. + */
  10822. + ptl_warn(ptl, "ptl: received ACK for non-pending packet\n");
  10823. + } else {
  10824. + /*
  10825. + * The packet is pending, but we are not allowed to take
  10826. + * it because it has been locked.
  10827. + */
  10828. + WARN_ON(PTR_ERR(p) != -EPERM);
  10829. + }
  10830. + return;
  10831. + }
  10832. +
  10833. + ptl_dbg(ptl, "ptl: received ACK for packet %p\n", p);
  10834. +
  10835. + /*
  10836. + * It is possible that the packet has been transmitted, but the state
  10837. + * has not been updated from "transmitting" to "transmitted" yet.
  10838. + * In that case, we need to wait for this transition to occur in order
  10839. + * to determine between success or failure.
  10840. + *
  10841. + * On transmission failure, the packet will be locked after this call.
  10842. + * On success, the transmitted bit will be set.
  10843. + */
  10844. + ssh_ptl_wait_until_transmitted(p);
  10845. +
  10846. + /*
  10847. + * The packet will already be locked in case of a transmission error or
  10848. + * cancellation. Let the transmitter or cancellation issuer complete the
  10849. + * packet.
  10850. + */
  10851. + if (unlikely(test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))) {
  10852. + if (unlikely(!test_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &p->state)))
  10853. + ptl_err(ptl, "ptl: received ACK before packet had been fully transmitted\n");
  10854. +
  10855. + ssh_packet_put(p);
  10856. + return;
  10857. + }
  10858. +
  10859. + ssh_ptl_remove_and_complete(p, 0);
  10860. + ssh_packet_put(p);
  10861. +
  10862. + if (atomic_read(&ptl->pending.count) < SSH_PTL_MAX_PENDING)
  10863. + ssh_ptl_tx_wakeup_packet(ptl);
  10864. +}
  10865. +
  10866. +/**
  10867. + * ssh_ptl_submit() - Submit a packet to the transport layer.
  10868. + * @ptl: The packet transport layer to submit the packet to.
  10869. + * @p: The packet to submit.
  10870. + *
  10871. + * Submits a new packet to the transport layer, queuing it to be sent. This
  10872. + * function should not be used for re-submission.
  10873. + *
  10874. + * Return: Returns zero on success, %-EINVAL if a packet field is invalid or
  10875. + * the packet has been canceled prior to submission, %-EALREADY if the packet
  10876. + * has already been submitted, or %-ESHUTDOWN if the packet transport layer
  10877. + * has been shut down.
  10878. + */
  10879. +int ssh_ptl_submit(struct ssh_ptl *ptl, struct ssh_packet *p)
  10880. +{
  10881. + struct ssh_ptl *ptl_old;
  10882. + int status;
  10883. +
  10884. + trace_ssam_packet_submit(p);
  10885. +
  10886. + /* Validate packet fields. */
  10887. + if (test_bit(SSH_PACKET_TY_FLUSH_BIT, &p->state)) {
  10888. + if (p->data.ptr || test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &p->state))
  10889. + return -EINVAL;
  10890. + } else if (!p->data.ptr) {
  10891. + return -EINVAL;
  10892. + }
  10893. +
  10894. + /*
  10895. + * The ptl reference only gets set on or before the first submission.
  10896. + * After the first submission, it has to be read-only.
  10897. + *
  10898. + * Note that ptl may already be set from upper-layer request
  10899. + * submission, thus we cannot expect it to be NULL.
  10900. + */
  10901. + ptl_old = READ_ONCE(p->ptl);
  10902. + if (!ptl_old)
  10903. + WRITE_ONCE(p->ptl, ptl);
  10904. + else if (WARN_ON(ptl_old != ptl))
  10905. + return -EALREADY; /* Submitted on different PTL. */
  10906. +
  10907. + status = ssh_ptl_queue_push(p);
  10908. + if (status)
  10909. + return status;
  10910. +
  10911. + if (!test_bit(SSH_PACKET_TY_BLOCKING_BIT, &p->state) ||
  10912. + (atomic_read(&ptl->pending.count) < SSH_PTL_MAX_PENDING))
  10913. + ssh_ptl_tx_wakeup_packet(ptl);
  10914. +
  10915. + return 0;
  10916. +}
  10917. +
  10918. +/*
  10919. + * __ssh_ptl_resubmit() - Re-submit a packet to the transport layer.
  10920. + * @packet: The packet to re-submit.
  10921. + *
  10922. + * Re-submits the given packet: Checks if it can be re-submitted and queues it
  10923. + * if it can, resetting the packet timestamp in the process. Must be called
  10924. + * with the pending lock held.
  10925. + *
  10926. + * Return: Returns %-ECANCELED if the packet has exceeded its number of tries,
  10927. + * %-EINVAL if the packet has been locked, %-EALREADY if the packet is already
  10928. + * on the queue, and %-ESHUTDOWN if the transmission layer has been shut down.
  10929. + */
  10930. +static int __ssh_ptl_resubmit(struct ssh_packet *packet)
  10931. +{
  10932. + int status;
  10933. + u8 try;
  10934. +
  10935. + lockdep_assert_held(&packet->ptl->pending.lock);
  10936. +
  10937. + trace_ssam_packet_resubmit(packet);
  10938. +
  10939. + spin_lock(&packet->ptl->queue.lock);
  10940. +
  10941. + /* Check if the packet is out of tries. */
  10942. + try = ssh_packet_priority_get_try(packet->priority);
  10943. + if (try >= SSH_PTL_MAX_PACKET_TRIES) {
  10944. + spin_unlock(&packet->ptl->queue.lock);
  10945. + return -ECANCELED;
  10946. + }
  10947. +
  10948. + status = __ssh_ptl_queue_push(packet);
  10949. + if (status) {
  10950. + /*
  10951. + * An error here indicates that the packet has either already
  10952. + * been queued, been locked, or the transport layer is being
  10953. + * shut down. In all cases: Ignore the error.
  10954. + */
  10955. + spin_unlock(&packet->ptl->queue.lock);
  10956. + return status;
  10957. + }
  10958. +
  10959. + packet->timestamp = KTIME_MAX;
  10960. +
  10961. + spin_unlock(&packet->ptl->queue.lock);
  10962. + return 0;
  10963. +}
  10964. +
  10965. +static void ssh_ptl_resubmit_pending(struct ssh_ptl *ptl)
  10966. +{
  10967. + struct ssh_packet *p;
  10968. + bool resub = false;
  10969. +
  10970. + /*
  10971. + * Note: We deliberately do not remove/attempt to cancel and complete
  10972. + * packets that are out of tires in this function. The packet will be
  10973. + * eventually canceled and completed by the timeout. Removing the packet
  10974. + * here could lead to overly eager cancellation if the packet has not
  10975. + * been re-transmitted yet but the tries-counter already updated (i.e
  10976. + * ssh_ptl_tx_next() removed the packet from the queue and updated the
  10977. + * counter, but re-transmission for the last try has not actually
  10978. + * started yet).
  10979. + */
  10980. +
  10981. + spin_lock(&ptl->pending.lock);
  10982. +
  10983. + /* Re-queue all pending packets. */
  10984. + list_for_each_entry(p, &ptl->pending.head, pending_node) {
  10985. + /*
  10986. + * Re-submission fails if the packet is out of tries, has been
  10987. + * locked, is already queued, or the layer is being shut down.
  10988. + * No need to re-schedule tx-thread in those cases.
  10989. + */
  10990. + if (!__ssh_ptl_resubmit(p))
  10991. + resub = true;
  10992. + }
  10993. +
  10994. + spin_unlock(&ptl->pending.lock);
  10995. +
  10996. + if (resub)
  10997. + ssh_ptl_tx_wakeup_packet(ptl);
  10998. +}
  10999. +
  11000. +/**
  11001. + * ssh_ptl_cancel() - Cancel a packet.
  11002. + * @p: The packet to cancel.
  11003. + *
  11004. + * Cancels a packet. There are no guarantees on when completion and release
  11005. + * callbacks will be called. This may occur during execution of this function
  11006. + * or may occur at any point later.
  11007. + *
  11008. + * Note that it is not guaranteed that the packet will actually be canceled if
  11009. + * the packet is concurrently completed by another process. The only guarantee
  11010. + * of this function is that the packet will be completed (with success,
  11011. + * failure, or cancellation) and released from the transport layer in a
  11012. + * reasonable time-frame.
  11013. + *
  11014. + * May be called before the packet has been submitted, in which case any later
  11015. + * packet submission fails.
  11016. + */
  11017. +void ssh_ptl_cancel(struct ssh_packet *p)
  11018. +{
  11019. + if (test_and_set_bit(SSH_PACKET_SF_CANCELED_BIT, &p->state))
  11020. + return;
  11021. +
  11022. + trace_ssam_packet_cancel(p);
  11023. +
  11024. + /*
  11025. + * Lock packet and commit with memory barrier. If this packet has
  11026. + * already been locked, it's going to be removed and completed by
  11027. + * another party, which should have precedence.
  11028. + */
  11029. + if (test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
  11030. + return;
  11031. +
  11032. + /*
  11033. + * By marking the packet as locked and employing the implicit memory
  11034. + * barrier of test_and_set_bit, we have guaranteed that, at this point,
  11035. + * the packet cannot be added to the queue any more.
  11036. + *
  11037. + * In case the packet has never been submitted, packet->ptl is NULL. If
  11038. + * the packet is currently being submitted, packet->ptl may be NULL or
  11039. + * non-NULL. Due marking the packet as locked above and committing with
  11040. + * the memory barrier, we have guaranteed that, if packet->ptl is NULL,
  11041. + * the packet will never be added to the queue. If packet->ptl is
  11042. + * non-NULL, we don't have any guarantees.
  11043. + */
  11044. +
  11045. + if (READ_ONCE(p->ptl)) {
  11046. + ssh_ptl_remove_and_complete(p, -ECANCELED);
  11047. +
  11048. + if (atomic_read(&p->ptl->pending.count) < SSH_PTL_MAX_PENDING)
  11049. + ssh_ptl_tx_wakeup_packet(p->ptl);
  11050. +
  11051. + } else if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state)) {
  11052. + __ssh_ptl_complete(p, -ECANCELED);
  11053. + }
  11054. +}
  11055. +
  11056. +/* Must be called with pending lock held */
  11057. +static ktime_t ssh_packet_get_expiration(struct ssh_packet *p, ktime_t timeout)
  11058. +{
  11059. + lockdep_assert_held(&p->ptl->pending.lock);
  11060. +
  11061. + if (p->timestamp != KTIME_MAX)
  11062. + return ktime_add(p->timestamp, timeout);
  11063. + else
  11064. + return KTIME_MAX;
  11065. +}
  11066. +
  11067. +static void ssh_ptl_timeout_reap(struct work_struct *work)
  11068. +{
  11069. + struct ssh_ptl *ptl = to_ssh_ptl(work, rtx_timeout.reaper.work);
  11070. + struct ssh_packet *p, *n;
  11071. + LIST_HEAD(claimed);
  11072. + ktime_t now = ktime_get_coarse_boottime();
  11073. + ktime_t timeout = ptl->rtx_timeout.timeout;
  11074. + ktime_t next = KTIME_MAX;
  11075. + bool resub = false;
  11076. + int status;
  11077. +
  11078. + trace_ssam_ptl_timeout_reap(atomic_read(&ptl->pending.count));
  11079. +
  11080. + /*
  11081. + * Mark reaper as "not pending". This is done before checking any
  11082. + * packets to avoid lost-update type problems.
  11083. + */
  11084. + spin_lock(&ptl->rtx_timeout.lock);
  11085. + ptl->rtx_timeout.expires = KTIME_MAX;
  11086. + spin_unlock(&ptl->rtx_timeout.lock);
  11087. +
  11088. + spin_lock(&ptl->pending.lock);
  11089. +
  11090. + list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
  11091. + ktime_t expires = ssh_packet_get_expiration(p, timeout);
  11092. +
  11093. + /*
  11094. + * Check if the timeout hasn't expired yet. Find out next
  11095. + * expiration date to be handled after this run.
  11096. + */
  11097. + if (ktime_after(expires, now)) {
  11098. + next = ktime_before(expires, next) ? expires : next;
  11099. + continue;
  11100. + }
  11101. +
  11102. + trace_ssam_packet_timeout(p);
  11103. +
  11104. + status = __ssh_ptl_resubmit(p);
  11105. +
  11106. + /*
  11107. + * Re-submission fails if the packet is out of tries, has been
  11108. + * locked, is already queued, or the layer is being shut down.
  11109. + * No need to re-schedule tx-thread in those cases.
  11110. + */
  11111. + if (!status)
  11112. + resub = true;
  11113. +
  11114. + /* Go to next packet if this packet is not out of tries. */
  11115. + if (status != -ECANCELED)
  11116. + continue;
  11117. +
  11118. + /* No more tries left: Cancel the packet. */
  11119. +
  11120. + /*
  11121. + * If someone else has locked the packet already, don't use it
  11122. + * and let the other party complete it.
  11123. + */
  11124. + if (test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
  11125. + continue;
  11126. +
  11127. + /*
  11128. + * We have now marked the packet as locked. Thus it cannot be
  11129. + * added to the pending list again after we've removed it here.
  11130. + * We can therefore re-use the pending_node of this packet
  11131. + * temporarily.
  11132. + */
  11133. +
  11134. + clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
  11135. +
  11136. + atomic_dec(&ptl->pending.count);
  11137. + list_del(&p->pending_node);
  11138. +
  11139. + list_add_tail(&p->pending_node, &claimed);
  11140. + }
  11141. +
  11142. + spin_unlock(&ptl->pending.lock);
  11143. +
  11144. + /* Cancel and complete the packet. */
  11145. + list_for_each_entry_safe(p, n, &claimed, pending_node) {
  11146. + if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state)) {
  11147. + ssh_ptl_queue_remove(p);
  11148. + __ssh_ptl_complete(p, -ETIMEDOUT);
  11149. + }
  11150. +
  11151. + /*
  11152. + * Drop the reference we've obtained by removing it from
  11153. + * the pending set.
  11154. + */
  11155. + list_del(&p->pending_node);
  11156. + ssh_packet_put(p);
  11157. + }
  11158. +
  11159. + /* Ensure that reaper doesn't run again immediately. */
  11160. + next = max(next, ktime_add(now, SSH_PTL_PACKET_TIMEOUT_RESOLUTION));
  11161. + if (next != KTIME_MAX)
  11162. + ssh_ptl_timeout_reaper_mod(ptl, now, next);
  11163. +
  11164. + if (resub)
  11165. + ssh_ptl_tx_wakeup_packet(ptl);
  11166. +}
  11167. +
  11168. +static bool ssh_ptl_rx_retransmit_check(struct ssh_ptl *ptl, u8 seq)
  11169. +{
  11170. + int i;
  11171. +
  11172. + /*
  11173. + * Check if SEQ has been seen recently (i.e. packet was
  11174. + * re-transmitted and we should ignore it).
  11175. + */
  11176. + for (i = 0; i < ARRAY_SIZE(ptl->rx.blocked.seqs); i++) {
  11177. + if (likely(ptl->rx.blocked.seqs[i] != seq))
  11178. + continue;
  11179. +
  11180. + ptl_dbg(ptl, "ptl: ignoring repeated data packet\n");
  11181. + return true;
  11182. + }
  11183. +
  11184. + /* Update list of blocked sequence IDs. */
  11185. + ptl->rx.blocked.seqs[ptl->rx.blocked.offset] = seq;
  11186. + ptl->rx.blocked.offset = (ptl->rx.blocked.offset + 1)
  11187. + % ARRAY_SIZE(ptl->rx.blocked.seqs);
  11188. +
  11189. + return false;
  11190. +}
  11191. +
  11192. +static void ssh_ptl_rx_dataframe(struct ssh_ptl *ptl,
  11193. + const struct ssh_frame *frame,
  11194. + const struct ssam_span *payload)
  11195. +{
  11196. + if (ssh_ptl_rx_retransmit_check(ptl, frame->seq))
  11197. + return;
  11198. +
  11199. + ptl->ops.data_received(ptl, payload);
  11200. +}
  11201. +
  11202. +static void ssh_ptl_send_ack(struct ssh_ptl *ptl, u8 seq)
  11203. +{
  11204. + struct ssh_packet *packet;
  11205. + struct ssam_span buf;
  11206. + struct msgbuf msgb;
  11207. + int status;
  11208. +
  11209. + status = ssh_ctrl_packet_alloc(&packet, &buf, GFP_KERNEL);
  11210. + if (status) {
  11211. + ptl_err(ptl, "ptl: failed to allocate ACK packet\n");
  11212. + return;
  11213. + }
  11214. +
  11215. + ssh_packet_init(packet, 0, SSH_PACKET_PRIORITY(ACK, 0),
  11216. + &ssh_ptl_ctrl_packet_ops);
  11217. +
  11218. + msgb_init(&msgb, buf.ptr, buf.len);
  11219. + msgb_push_ack(&msgb, seq);
  11220. + ssh_packet_set_data(packet, msgb.begin, msgb_bytes_used(&msgb));
  11221. +
  11222. + ssh_ptl_submit(ptl, packet);
  11223. + ssh_packet_put(packet);
  11224. +}
  11225. +
  11226. +static void ssh_ptl_send_nak(struct ssh_ptl *ptl)
  11227. +{
  11228. + struct ssh_packet *packet;
  11229. + struct ssam_span buf;
  11230. + struct msgbuf msgb;
  11231. + int status;
  11232. +
  11233. + status = ssh_ctrl_packet_alloc(&packet, &buf, GFP_KERNEL);
  11234. + if (status) {
  11235. + ptl_err(ptl, "ptl: failed to allocate NAK packet\n");
  11236. + return;
  11237. + }
  11238. +
  11239. + ssh_packet_init(packet, 0, SSH_PACKET_PRIORITY(NAK, 0),
  11240. + &ssh_ptl_ctrl_packet_ops);
  11241. +
  11242. + msgb_init(&msgb, buf.ptr, buf.len);
  11243. + msgb_push_nak(&msgb);
  11244. + ssh_packet_set_data(packet, msgb.begin, msgb_bytes_used(&msgb));
  11245. +
  11246. + ssh_ptl_submit(ptl, packet);
  11247. + ssh_packet_put(packet);
  11248. +}
  11249. +
  11250. +static size_t ssh_ptl_rx_eval(struct ssh_ptl *ptl, struct ssam_span *source)
  11251. +{
  11252. + struct ssh_frame *frame;
  11253. + struct ssam_span payload;
  11254. + struct ssam_span aligned;
  11255. + bool syn_found;
  11256. + int status;
  11257. +
  11258. + /* Error injection: Modify data to simulate corrupt SYN bytes. */
  11259. + ssh_ptl_rx_inject_invalid_syn(ptl, source);
  11260. +
  11261. + /* Find SYN. */
  11262. + syn_found = sshp_find_syn(source, &aligned);
  11263. +
  11264. + if (unlikely(aligned.ptr != source->ptr)) {
  11265. + /*
  11266. + * We expect aligned.ptr == source->ptr. If this is not the
  11267. + * case, then aligned.ptr > source->ptr and we've encountered
  11268. + * some unexpected data where we'd expect the start of a new
  11269. + * message (i.e. the SYN sequence).
  11270. + *
  11271. + * This can happen when a CRC check for the previous message
  11272. + * failed and we start actively searching for the next one
  11273. + * (via the call to sshp_find_syn() above), or the first bytes
  11274. + * of a message got dropped or corrupted.
  11275. + *
  11276. + * In any case, we issue a warning, send a NAK to the EC to
  11277. + * request re-transmission of any data we haven't acknowledged
  11278. + * yet, and finally, skip everything up to the next SYN
  11279. + * sequence.
  11280. + */
  11281. +
  11282. + ptl_warn(ptl, "rx: parser: invalid start of frame, skipping\n");
  11283. +
  11284. + /*
  11285. + * Notes:
  11286. + * - This might send multiple NAKs in case the communication
  11287. + * starts with an invalid SYN and is broken down into multiple
  11288. + * pieces. This should generally be handled fine, we just
  11289. + * might receive duplicate data in this case, which is
  11290. + * detected when handling data frames.
  11291. + * - This path will also be executed on invalid CRCs: When an
  11292. + * invalid CRC is encountered, the code below will skip data
  11293. + * until directly after the SYN. This causes the search for
  11294. + * the next SYN, which is generally not placed directly after
  11295. + * the last one.
  11296. + *
  11297. + * Open question: Should we send this in case of invalid
  11298. + * payload CRCs if the frame-type is non-sequential (current
  11299. + * implementation) or should we drop that frame without
  11300. + * telling the EC?
  11301. + */
  11302. + ssh_ptl_send_nak(ptl);
  11303. + }
  11304. +
  11305. + if (unlikely(!syn_found))
  11306. + return aligned.ptr - source->ptr;
  11307. +
  11308. + /* Error injection: Modify data to simulate corruption. */
  11309. + ssh_ptl_rx_inject_invalid_data(ptl, &aligned);
  11310. +
  11311. + /* Parse and validate frame. */
  11312. + status = sshp_parse_frame(&ptl->serdev->dev, &aligned, &frame, &payload,
  11313. + SSH_PTL_RX_BUF_LEN);
  11314. + if (status) /* Invalid frame: skip to next SYN. */
  11315. + return aligned.ptr - source->ptr + sizeof(u16);
  11316. + if (!frame) /* Not enough data. */
  11317. + return aligned.ptr - source->ptr;
  11318. +
  11319. + trace_ssam_rx_frame_received(frame);
  11320. +
  11321. + switch (frame->type) {
  11322. + case SSH_FRAME_TYPE_ACK:
  11323. + ssh_ptl_acknowledge(ptl, frame->seq);
  11324. + break;
  11325. +
  11326. + case SSH_FRAME_TYPE_NAK:
  11327. + ssh_ptl_resubmit_pending(ptl);
  11328. + break;
  11329. +
  11330. + case SSH_FRAME_TYPE_DATA_SEQ:
  11331. + ssh_ptl_send_ack(ptl, frame->seq);
  11332. + fallthrough;
  11333. +
  11334. + case SSH_FRAME_TYPE_DATA_NSQ:
  11335. + ssh_ptl_rx_dataframe(ptl, frame, &payload);
  11336. + break;
  11337. +
  11338. + default:
  11339. + ptl_warn(ptl, "ptl: received frame with unknown type %#04x\n",
  11340. + frame->type);
  11341. + break;
  11342. + }
  11343. +
  11344. + return aligned.ptr - source->ptr + SSH_MESSAGE_LENGTH(payload.len);
  11345. +}
  11346. +
  11347. +static int ssh_ptl_rx_threadfn(void *data)
  11348. +{
  11349. + struct ssh_ptl *ptl = data;
  11350. +
  11351. + while (true) {
  11352. + struct ssam_span span;
  11353. + size_t offs = 0;
  11354. + size_t n;
  11355. +
  11356. + wait_event_interruptible(ptl->rx.wq,
  11357. + !kfifo_is_empty(&ptl->rx.fifo) ||
  11358. + kthread_should_stop());
  11359. + if (kthread_should_stop())
  11360. + break;
  11361. +
  11362. + /* Copy from fifo to evaluation buffer. */
  11363. + n = sshp_buf_read_from_fifo(&ptl->rx.buf, &ptl->rx.fifo);
  11364. +
  11365. + ptl_dbg(ptl, "rx: received data (size: %zu)\n", n);
  11366. + print_hex_dump_debug("rx: ", DUMP_PREFIX_OFFSET, 16, 1,
  11367. + ptl->rx.buf.ptr + ptl->rx.buf.len - n,
  11368. + n, false);
  11369. +
  11370. + /* Parse until we need more bytes or buffer is empty. */
  11371. + while (offs < ptl->rx.buf.len) {
  11372. + sshp_buf_span_from(&ptl->rx.buf, offs, &span);
  11373. + n = ssh_ptl_rx_eval(ptl, &span);
  11374. + if (n == 0)
  11375. + break; /* Need more bytes. */
  11376. +
  11377. + offs += n;
  11378. + }
  11379. +
  11380. + /* Throw away the evaluated parts. */
  11381. + sshp_buf_drop(&ptl->rx.buf, offs);
  11382. + }
  11383. +
  11384. + return 0;
  11385. +}
  11386. +
  11387. +static void ssh_ptl_rx_wakeup(struct ssh_ptl *ptl)
  11388. +{
  11389. + wake_up(&ptl->rx.wq);
  11390. +}
  11391. +
  11392. +/**
  11393. + * ssh_ptl_rx_start() - Start packet transport layer receiver thread.
  11394. + * @ptl: The packet transport layer.
  11395. + *
  11396. + * Return: Returns zero on success, a negative error code on failure.
  11397. + */
  11398. +int ssh_ptl_rx_start(struct ssh_ptl *ptl)
  11399. +{
  11400. + if (ptl->rx.thread)
  11401. + return 0;
  11402. +
  11403. + ptl->rx.thread = kthread_run(ssh_ptl_rx_threadfn, ptl,
  11404. + "ssam_serial_hub-rx");
  11405. + if (IS_ERR(ptl->rx.thread))
  11406. + return PTR_ERR(ptl->rx.thread);
  11407. +
  11408. + return 0;
  11409. +}
  11410. +
  11411. +/**
  11412. + * ssh_ptl_rx_stop() - Stop packet transport layer receiver thread.
  11413. + * @ptl: The packet transport layer.
  11414. + *
  11415. + * Return: Returns zero on success, a negative error code on failure.
  11416. + */
  11417. +int ssh_ptl_rx_stop(struct ssh_ptl *ptl)
  11418. +{
  11419. + int status = 0;
  11420. +
  11421. + if (ptl->rx.thread) {
  11422. + status = kthread_stop(ptl->rx.thread);
  11423. + ptl->rx.thread = NULL;
  11424. + }
  11425. +
  11426. + return status;
  11427. +}
  11428. +
  11429. +/**
  11430. + * ssh_ptl_rx_rcvbuf() - Push data from lower-layer transport to the packet
  11431. + * layer.
  11432. + * @ptl: The packet transport layer.
  11433. + * @buf: Pointer to the data to push to the layer.
  11434. + * @n: Size of the data to push to the layer, in bytes.
  11435. + *
  11436. + * Pushes data from a lower-layer transport to the receiver fifo buffer of the
  11437. + * packet layer and notifies the receiver thread. Calls to this function are
  11438. + * ignored once the packet layer has been shut down.
  11439. + *
  11440. + * Return: Returns the number of bytes transferred (positive or zero) on
  11441. + * success. Returns %-ESHUTDOWN if the packet layer has been shut down.
  11442. + */
  11443. +int ssh_ptl_rx_rcvbuf(struct ssh_ptl *ptl, const u8 *buf, size_t n)
  11444. +{
  11445. + int used;
  11446. +
  11447. + if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
  11448. + return -ESHUTDOWN;
  11449. +
  11450. + used = kfifo_in(&ptl->rx.fifo, buf, n);
  11451. + if (used)
  11452. + ssh_ptl_rx_wakeup(ptl);
  11453. +
  11454. + return used;
  11455. +}
  11456. +
  11457. +/**
  11458. + * ssh_ptl_shutdown() - Shut down the packet transport layer.
  11459. + * @ptl: The packet transport layer.
  11460. + *
  11461. + * Shuts down the packet transport layer, removing and canceling all queued
  11462. + * and pending packets. Packets canceled by this operation will be completed
  11463. + * with %-ESHUTDOWN as status. Receiver and transmitter threads will be
  11464. + * stopped.
  11465. + *
  11466. + * As a result of this function, the transport layer will be marked as shut
  11467. + * down. Submission of packets after the transport layer has been shut down
  11468. + * will fail with %-ESHUTDOWN.
  11469. + */
  11470. +void ssh_ptl_shutdown(struct ssh_ptl *ptl)
  11471. +{
  11472. + LIST_HEAD(complete_q);
  11473. + LIST_HEAD(complete_p);
  11474. + struct ssh_packet *p, *n;
  11475. + int status;
  11476. +
  11477. + /* Ensure that no new packets (including ACK/NAK) can be submitted. */
  11478. + set_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state);
  11479. + /*
  11480. + * Ensure that the layer gets marked as shut-down before actually
  11481. + * stopping it. In combination with the check in ssh_ptl_queue_push(),
  11482. + * this guarantees that no new packets can be added and all already
  11483. + * queued packets are properly canceled. In combination with the check
  11484. + * in ssh_ptl_rx_rcvbuf(), this guarantees that received data is
  11485. + * properly cut off.
  11486. + */
  11487. + smp_mb__after_atomic();
  11488. +
  11489. + status = ssh_ptl_rx_stop(ptl);
  11490. + if (status)
  11491. + ptl_err(ptl, "ptl: failed to stop receiver thread\n");
  11492. +
  11493. + status = ssh_ptl_tx_stop(ptl);
  11494. + if (status)
  11495. + ptl_err(ptl, "ptl: failed to stop transmitter thread\n");
  11496. +
  11497. + cancel_delayed_work_sync(&ptl->rtx_timeout.reaper);
  11498. +
  11499. + /*
  11500. + * At this point, all threads have been stopped. This means that the
  11501. + * only references to packets from inside the system are in the queue
  11502. + * and pending set.
  11503. + *
  11504. + * Note: We still need locks here because someone could still be
  11505. + * canceling packets.
  11506. + *
  11507. + * Note 2: We can re-use queue_node (or pending_node) if we mark the
  11508. + * packet as locked an then remove it from the queue (or pending set
  11509. + * respectively). Marking the packet as locked avoids re-queuing
  11510. + * (which should already be prevented by having stopped the treads...)
  11511. + * and not setting QUEUED_BIT (or PENDING_BIT) prevents removal from a
  11512. + * new list via other threads (e.g. cancellation).
  11513. + *
  11514. + * Note 3: There may be overlap between complete_p and complete_q.
  11515. + * This is handled via test_and_set_bit() on the "completed" flag
  11516. + * (also handles cancellation).
  11517. + */
  11518. +
  11519. + /* Mark queued packets as locked and move them to complete_q. */
  11520. + spin_lock(&ptl->queue.lock);
  11521. + list_for_each_entry_safe(p, n, &ptl->queue.head, queue_node) {
  11522. + set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state);
  11523. + /* Ensure that state does not get zero. */
  11524. + smp_mb__before_atomic();
  11525. + clear_bit(SSH_PACKET_SF_QUEUED_BIT, &p->state);
  11526. +
  11527. + list_del(&p->queue_node);
  11528. + list_add_tail(&p->queue_node, &complete_q);
  11529. + }
  11530. + spin_unlock(&ptl->queue.lock);
  11531. +
  11532. + /* Mark pending packets as locked and move them to complete_p. */
  11533. + spin_lock(&ptl->pending.lock);
  11534. + list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
  11535. + set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state);
  11536. + /* Ensure that state does not get zero. */
  11537. + smp_mb__before_atomic();
  11538. + clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
  11539. +
  11540. + list_del(&p->pending_node);
  11541. + list_add_tail(&p->pending_node, &complete_q);
  11542. + }
  11543. + atomic_set(&ptl->pending.count, 0);
  11544. + spin_unlock(&ptl->pending.lock);
  11545. +
  11546. + /* Complete and drop packets on complete_q. */
  11547. + list_for_each_entry(p, &complete_q, queue_node) {
  11548. + if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
  11549. + __ssh_ptl_complete(p, -ESHUTDOWN);
  11550. +
  11551. + ssh_packet_put(p);
  11552. + }
  11553. +
  11554. + /* Complete and drop packets on complete_p. */
  11555. + list_for_each_entry(p, &complete_p, pending_node) {
  11556. + if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
  11557. + __ssh_ptl_complete(p, -ESHUTDOWN);
  11558. +
  11559. + ssh_packet_put(p);
  11560. + }
  11561. +
  11562. + /*
  11563. + * At this point we have guaranteed that the system doesn't reference
  11564. + * any packets any more.
  11565. + */
  11566. +}
  11567. +
  11568. +/**
  11569. + * ssh_ptl_init() - Initialize packet transport layer.
  11570. + * @ptl: The packet transport layer to initialize.
  11571. + * @serdev: The underlying serial device, i.e. the lower-level transport.
  11572. + * @ops: Packet layer operations.
  11573. + *
  11574. + * Initializes the given packet transport layer. Transmitter and receiver
  11575. + * threads must be started separately via ssh_ptl_tx_start() and
  11576. + * ssh_ptl_rx_start(), after the packet-layer has been initialized and the
  11577. + * lower-level transport layer has been set up.
  11578. + *
  11579. + * Return: Returns zero on success and a nonzero error code on failure.
  11580. + */
  11581. +int ssh_ptl_init(struct ssh_ptl *ptl, struct serdev_device *serdev,
  11582. + struct ssh_ptl_ops *ops)
  11583. +{
  11584. + int i, status;
  11585. +
  11586. + ptl->serdev = serdev;
  11587. + ptl->state = 0;
  11588. +
  11589. + spin_lock_init(&ptl->queue.lock);
  11590. + INIT_LIST_HEAD(&ptl->queue.head);
  11591. +
  11592. + spin_lock_init(&ptl->pending.lock);
  11593. + INIT_LIST_HEAD(&ptl->pending.head);
  11594. + atomic_set_release(&ptl->pending.count, 0);
  11595. +
  11596. + ptl->tx.thread = NULL;
  11597. + atomic_set(&ptl->tx.running, 0);
  11598. + init_completion(&ptl->tx.thread_cplt_pkt);
  11599. + init_completion(&ptl->tx.thread_cplt_tx);
  11600. + init_waitqueue_head(&ptl->tx.packet_wq);
  11601. +
  11602. + ptl->rx.thread = NULL;
  11603. + init_waitqueue_head(&ptl->rx.wq);
  11604. +
  11605. + spin_lock_init(&ptl->rtx_timeout.lock);
  11606. + ptl->rtx_timeout.timeout = SSH_PTL_PACKET_TIMEOUT;
  11607. + ptl->rtx_timeout.expires = KTIME_MAX;
  11608. + INIT_DELAYED_WORK(&ptl->rtx_timeout.reaper, ssh_ptl_timeout_reap);
  11609. +
  11610. + ptl->ops = *ops;
  11611. +
  11612. + /* Initialize list of recent/blocked SEQs with invalid sequence IDs. */
  11613. + for (i = 0; i < ARRAY_SIZE(ptl->rx.blocked.seqs); i++)
  11614. + ptl->rx.blocked.seqs[i] = U16_MAX;
  11615. + ptl->rx.blocked.offset = 0;
  11616. +
  11617. + status = kfifo_alloc(&ptl->rx.fifo, SSH_PTL_RX_FIFO_LEN, GFP_KERNEL);
  11618. + if (status)
  11619. + return status;
  11620. +
  11621. + status = sshp_buf_alloc(&ptl->rx.buf, SSH_PTL_RX_BUF_LEN, GFP_KERNEL);
  11622. + if (status)
  11623. + kfifo_free(&ptl->rx.fifo);
  11624. +
  11625. + return status;
  11626. +}
  11627. +
  11628. +/**
  11629. + * ssh_ptl_destroy() - Deinitialize packet transport layer.
  11630. + * @ptl: The packet transport layer to deinitialize.
  11631. + *
  11632. + * Deinitializes the given packet transport layer and frees resources
  11633. + * associated with it. If receiver and/or transmitter threads have been
  11634. + * started, the layer must first be shut down via ssh_ptl_shutdown() before
  11635. + * this function can be called.
  11636. + */
  11637. +void ssh_ptl_destroy(struct ssh_ptl *ptl)
  11638. +{
  11639. + kfifo_free(&ptl->rx.fifo);
  11640. + sshp_buf_free(&ptl->rx.buf);
  11641. +}
  11642. diff --git a/drivers/platform/x86/surface_aggregator/ssh_packet_layer.h b/drivers/platform/x86/surface_aggregator/ssh_packet_layer.h
  11643. new file mode 100644
  11644. index 000000000000..2eb329f0b91a
  11645. --- /dev/null
  11646. +++ b/drivers/platform/x86/surface_aggregator/ssh_packet_layer.h
  11647. @@ -0,0 +1,190 @@
  11648. +/* SPDX-License-Identifier: GPL-2.0+ */
  11649. +/*
  11650. + * SSH packet transport layer.
  11651. + *
  11652. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  11653. + */
  11654. +
  11655. +#ifndef _SURFACE_AGGREGATOR_SSH_PACKET_LAYER_H
  11656. +#define _SURFACE_AGGREGATOR_SSH_PACKET_LAYER_H
  11657. +
  11658. +#include <linux/atomic.h>
  11659. +#include <linux/kfifo.h>
  11660. +#include <linux/ktime.h>
  11661. +#include <linux/list.h>
  11662. +#include <linux/serdev.h>
  11663. +#include <linux/spinlock.h>
  11664. +#include <linux/types.h>
  11665. +#include <linux/wait.h>
  11666. +#include <linux/workqueue.h>
  11667. +
  11668. +#include <linux/surface_aggregator/serial_hub.h>
  11669. +#include "ssh_parser.h"
  11670. +
  11671. +/**
  11672. + * enum ssh_ptl_state_flags - State-flags for &struct ssh_ptl.
  11673. + *
  11674. + * @SSH_PTL_SF_SHUTDOWN_BIT:
  11675. + * Indicates that the packet transport layer has been shut down or is
  11676. + * being shut down and should not accept any new packets/data.
  11677. + */
  11678. +enum ssh_ptl_state_flags {
  11679. + SSH_PTL_SF_SHUTDOWN_BIT,
  11680. +};
  11681. +
  11682. +/**
  11683. + * struct ssh_ptl_ops - Callback operations for packet transport layer.
  11684. + * @data_received: Function called when a data-packet has been received. Both,
  11685. + * the packet layer on which the packet has been received and
  11686. + * the packet's payload data are provided to this function.
  11687. + */
  11688. +struct ssh_ptl_ops {
  11689. + void (*data_received)(struct ssh_ptl *p, const struct ssam_span *data);
  11690. +};
  11691. +
  11692. +/**
  11693. + * struct ssh_ptl - SSH packet transport layer.
  11694. + * @serdev: Serial device providing the underlying data transport.
  11695. + * @state: State(-flags) of the transport layer.
  11696. + * @queue: Packet submission queue.
  11697. + * @queue.lock: Lock for modifying the packet submission queue.
  11698. + * @queue.head: List-head of the packet submission queue.
  11699. + * @pending: Set/list of pending packets.
  11700. + * @pending.lock: Lock for modifying the pending set.
  11701. + * @pending.head: List-head of the pending set/list.
  11702. + * @pending.count: Number of currently pending packets.
  11703. + * @tx: Transmitter subsystem.
  11704. + * @tx.running: Flag indicating (desired) transmitter thread state.
  11705. + * @tx.thread: Transmitter thread.
  11706. + * @tx.thread_cplt_tx: Completion for transmitter thread waiting on transfer.
  11707. + * @tx.thread_cplt_pkt: Completion for transmitter thread waiting on packets.
  11708. + * @tx.packet_wq: Waitqueue-head for packet transmit completion.
  11709. + * @rx: Receiver subsystem.
  11710. + * @rx.thread: Receiver thread.
  11711. + * @rx.wq: Waitqueue-head for receiver thread.
  11712. + * @rx.fifo: Buffer for receiving data/pushing data to receiver thread.
  11713. + * @rx.buf: Buffer for evaluating data on receiver thread.
  11714. + * @rx.blocked: List of recent/blocked sequence IDs to detect retransmission.
  11715. + * @rx.blocked.seqs: Array of blocked sequence IDs.
  11716. + * @rx.blocked.offset: Offset indicating where a new ID should be inserted.
  11717. + * @rtx_timeout: Retransmission timeout subsystem.
  11718. + * @rtx_timeout.lock: Lock for modifying the retransmission timeout reaper.
  11719. + * @rtx_timeout.timeout: Timeout interval for retransmission.
  11720. + * @rtx_timeout.expires: Time specifying when the reaper work is next scheduled.
  11721. + * @rtx_timeout.reaper: Work performing timeout checks and subsequent actions.
  11722. + * @ops: Packet layer operations.
  11723. + */
  11724. +struct ssh_ptl {
  11725. + struct serdev_device *serdev;
  11726. + unsigned long state;
  11727. +
  11728. + struct {
  11729. + spinlock_t lock;
  11730. + struct list_head head;
  11731. + } queue;
  11732. +
  11733. + struct {
  11734. + spinlock_t lock;
  11735. + struct list_head head;
  11736. + atomic_t count;
  11737. + } pending;
  11738. +
  11739. + struct {
  11740. + atomic_t running;
  11741. + struct task_struct *thread;
  11742. + struct completion thread_cplt_tx;
  11743. + struct completion thread_cplt_pkt;
  11744. + struct wait_queue_head packet_wq;
  11745. + } tx;
  11746. +
  11747. + struct {
  11748. + struct task_struct *thread;
  11749. + struct wait_queue_head wq;
  11750. + struct kfifo fifo;
  11751. + struct sshp_buf buf;
  11752. +
  11753. + struct {
  11754. + u16 seqs[8];
  11755. + u16 offset;
  11756. + } blocked;
  11757. + } rx;
  11758. +
  11759. + struct {
  11760. + spinlock_t lock;
  11761. + ktime_t timeout;
  11762. + ktime_t expires;
  11763. + struct delayed_work reaper;
  11764. + } rtx_timeout;
  11765. +
  11766. + struct ssh_ptl_ops ops;
  11767. +};
  11768. +
  11769. +#define __ssam_prcond(func, p, fmt, ...) \
  11770. + do { \
  11771. + typeof(p) __p = (p); \
  11772. + \
  11773. + if (__p) \
  11774. + func(__p, fmt, ##__VA_ARGS__); \
  11775. + } while (0)
  11776. +
  11777. +#define ptl_dbg(p, fmt, ...) dev_dbg(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
  11778. +#define ptl_info(p, fmt, ...) dev_info(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
  11779. +#define ptl_warn(p, fmt, ...) dev_warn(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
  11780. +#define ptl_err(p, fmt, ...) dev_err(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
  11781. +#define ptl_dbg_cond(p, fmt, ...) __ssam_prcond(ptl_dbg, p, fmt, ##__VA_ARGS__)
  11782. +
  11783. +#define to_ssh_ptl(ptr, member) \
  11784. + container_of(ptr, struct ssh_ptl, member)
  11785. +
  11786. +int ssh_ptl_init(struct ssh_ptl *ptl, struct serdev_device *serdev,
  11787. + struct ssh_ptl_ops *ops);
  11788. +
  11789. +void ssh_ptl_destroy(struct ssh_ptl *ptl);
  11790. +
  11791. +/**
  11792. + * ssh_ptl_get_device() - Get device associated with packet transport layer.
  11793. + * @ptl: The packet transport layer.
  11794. + *
  11795. + * Return: Returns the device on which the given packet transport layer builds
  11796. + * upon.
  11797. + */
  11798. +static inline struct device *ssh_ptl_get_device(struct ssh_ptl *ptl)
  11799. +{
  11800. + return ptl->serdev ? &ptl->serdev->dev : NULL;
  11801. +}
  11802. +
  11803. +int ssh_ptl_tx_start(struct ssh_ptl *ptl);
  11804. +int ssh_ptl_tx_stop(struct ssh_ptl *ptl);
  11805. +int ssh_ptl_rx_start(struct ssh_ptl *ptl);
  11806. +int ssh_ptl_rx_stop(struct ssh_ptl *ptl);
  11807. +void ssh_ptl_shutdown(struct ssh_ptl *ptl);
  11808. +
  11809. +int ssh_ptl_submit(struct ssh_ptl *ptl, struct ssh_packet *p);
  11810. +void ssh_ptl_cancel(struct ssh_packet *p);
  11811. +
  11812. +int ssh_ptl_rx_rcvbuf(struct ssh_ptl *ptl, const u8 *buf, size_t n);
  11813. +
  11814. +/**
  11815. + * ssh_ptl_tx_wakeup_transfer() - Wake up packet transmitter thread for
  11816. + * transfer.
  11817. + * @ptl: The packet transport layer.
  11818. + *
  11819. + * Wakes up the packet transmitter thread, notifying it that the underlying
  11820. + * transport has more space for data to be transmitted. If the packet
  11821. + * transport layer has been shut down, calls to this function will be ignored.
  11822. + */
  11823. +static inline void ssh_ptl_tx_wakeup_transfer(struct ssh_ptl *ptl)
  11824. +{
  11825. + if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
  11826. + return;
  11827. +
  11828. + complete(&ptl->tx.thread_cplt_tx);
  11829. +}
  11830. +
  11831. +void ssh_packet_init(struct ssh_packet *packet, unsigned long type,
  11832. + u8 priority, const struct ssh_packet_ops *ops);
  11833. +
  11834. +int ssh_ctrl_packet_cache_init(void);
  11835. +void ssh_ctrl_packet_cache_destroy(void);
  11836. +
  11837. +#endif /* _SURFACE_AGGREGATOR_SSH_PACKET_LAYER_H */
  11838. diff --git a/drivers/platform/x86/surface_aggregator/ssh_parser.c b/drivers/platform/x86/surface_aggregator/ssh_parser.c
  11839. new file mode 100644
  11840. index 000000000000..b77912f8f13b
  11841. --- /dev/null
  11842. +++ b/drivers/platform/x86/surface_aggregator/ssh_parser.c
  11843. @@ -0,0 +1,228 @@
  11844. +// SPDX-License-Identifier: GPL-2.0+
  11845. +/*
  11846. + * SSH message parser.
  11847. + *
  11848. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  11849. + */
  11850. +
  11851. +#include <asm/unaligned.h>
  11852. +#include <linux/compiler.h>
  11853. +#include <linux/device.h>
  11854. +#include <linux/types.h>
  11855. +
  11856. +#include <linux/surface_aggregator/serial_hub.h>
  11857. +#include "ssh_parser.h"
  11858. +
  11859. +/**
  11860. + * sshp_validate_crc() - Validate a CRC in raw message data.
  11861. + * @src: The span of data over which the CRC should be computed.
  11862. + * @crc: The pointer to the expected u16 CRC value.
  11863. + *
  11864. + * Computes the CRC of the provided data span (@src), compares it to the CRC
  11865. + * stored at the given address (@crc), and returns the result of this
  11866. + * comparison, i.e. %true if equal. This function is intended to run on raw
  11867. + * input/message data.
  11868. + *
  11869. + * Return: Returns %true if the computed CRC matches the stored CRC, %false
  11870. + * otherwise.
  11871. + */
  11872. +static bool sshp_validate_crc(const struct ssam_span *src, const u8 *crc)
  11873. +{
  11874. + u16 actual = ssh_crc(src->ptr, src->len);
  11875. + u16 expected = get_unaligned_le16(crc);
  11876. +
  11877. + return actual == expected;
  11878. +}
  11879. +
  11880. +/**
  11881. + * sshp_starts_with_syn() - Check if the given data starts with SSH SYN bytes.
  11882. + * @src: The data span to check the start of.
  11883. + */
  11884. +static bool sshp_starts_with_syn(const struct ssam_span *src)
  11885. +{
  11886. + return src->len >= 2 && get_unaligned_le16(src->ptr) == SSH_MSG_SYN;
  11887. +}
  11888. +
  11889. +/**
  11890. + * sshp_find_syn() - Find SSH SYN bytes in the given data span.
  11891. + * @src: The data span to search in.
  11892. + * @rem: The span (output) indicating the remaining data, starting with SSH
  11893. + * SYN bytes, if found.
  11894. + *
  11895. + * Search for SSH SYN bytes in the given source span. If found, set the @rem
  11896. + * span to the remaining data, starting with the first SYN bytes and capped by
  11897. + * the source span length, and return %true. This function does not copy any
  11898. + * data, but rather only sets pointers to the respective start addresses and
  11899. + * length values.
  11900. + *
  11901. + * If no SSH SYN bytes could be found, set the @rem span to the zero-length
  11902. + * span at the end of the source span and return %false.
  11903. + *
  11904. + * If partial SSH SYN bytes could be found at the end of the source span, set
  11905. + * the @rem span to cover these partial SYN bytes, capped by the end of the
  11906. + * source span, and return %false. This function should then be re-run once
  11907. + * more data is available.
  11908. + *
  11909. + * Return: Returns %true if a complete SSH SYN sequence could be found,
  11910. + * %false otherwise.
  11911. + */
  11912. +bool sshp_find_syn(const struct ssam_span *src, struct ssam_span *rem)
  11913. +{
  11914. + size_t i;
  11915. +
  11916. + for (i = 0; i < src->len - 1; i++) {
  11917. + if (likely(get_unaligned_le16(src->ptr + i) == SSH_MSG_SYN)) {
  11918. + rem->ptr = src->ptr + i;
  11919. + rem->len = src->len - i;
  11920. + return true;
  11921. + }
  11922. + }
  11923. +
  11924. + if (unlikely(src->ptr[src->len - 1] == (SSH_MSG_SYN & 0xff))) {
  11925. + rem->ptr = src->ptr + src->len - 1;
  11926. + rem->len = 1;
  11927. + return false;
  11928. + }
  11929. +
  11930. + rem->ptr = src->ptr + src->len;
  11931. + rem->len = 0;
  11932. + return false;
  11933. +}
  11934. +
  11935. +/**
  11936. + * sshp_parse_frame() - Parse SSH frame.
  11937. + * @dev: The device used for logging.
  11938. + * @source: The source to parse from.
  11939. + * @frame: The parsed frame (output).
  11940. + * @payload: The parsed payload (output).
  11941. + * @maxlen: The maximum supported message length.
  11942. + *
  11943. + * Parses and validates a SSH frame, including its payload, from the given
  11944. + * source. Sets the provided @frame pointer to the start of the frame and
  11945. + * writes the limits of the frame payload to the provided @payload span
  11946. + * pointer.
  11947. + *
  11948. + * This function does not copy any data, but rather only validates the message
  11949. + * data and sets pointers (and length values) to indicate the respective parts.
  11950. + *
  11951. + * If no complete SSH frame could be found, the frame pointer will be set to
  11952. + * the %NULL pointer and the payload span will be set to the null span (start
  11953. + * pointer %NULL, size zero).
  11954. + *
  11955. + * Return: Returns zero on success or if the frame is incomplete, %-ENOMSG if
  11956. + * the start of the message is invalid, %-EBADMSG if any (frame-header or
  11957. + * payload) CRC is invalid, or %-EMSGSIZE if the SSH message is bigger than
  11958. + * the maximum message length specified in the @maxlen parameter.
  11959. + */
  11960. +int sshp_parse_frame(const struct device *dev, const struct ssam_span *source,
  11961. + struct ssh_frame **frame, struct ssam_span *payload,
  11962. + size_t maxlen)
  11963. +{
  11964. + struct ssam_span sf;
  11965. + struct ssam_span sp;
  11966. +
  11967. + /* Initialize output. */
  11968. + *frame = NULL;
  11969. + payload->ptr = NULL;
  11970. + payload->len = 0;
  11971. +
  11972. + if (!sshp_starts_with_syn(source)) {
  11973. + dev_warn(dev, "rx: parser: invalid start of frame\n");
  11974. + return -ENOMSG;
  11975. + }
  11976. +
  11977. + /* Check for minimum packet length. */
  11978. + if (unlikely(source->len < SSH_MESSAGE_LENGTH(0))) {
  11979. + dev_dbg(dev, "rx: parser: not enough data for frame\n");
  11980. + return 0;
  11981. + }
  11982. +
  11983. + /* Pin down frame. */
  11984. + sf.ptr = source->ptr + sizeof(u16);
  11985. + sf.len = sizeof(struct ssh_frame);
  11986. +
  11987. + /* Validate frame CRC. */
  11988. + if (unlikely(!sshp_validate_crc(&sf, sf.ptr + sf.len))) {
  11989. + dev_warn(dev, "rx: parser: invalid frame CRC\n");
  11990. + return -EBADMSG;
  11991. + }
  11992. +
  11993. + /* Ensure packet does not exceed maximum length. */
  11994. + sp.len = get_unaligned_le16(&((struct ssh_frame *)sf.ptr)->len);
  11995. + if (unlikely(SSH_MESSAGE_LENGTH(sp.len) > maxlen)) {
  11996. + dev_warn(dev, "rx: parser: frame too large: %llu bytes\n",
  11997. + SSH_MESSAGE_LENGTH(sp.len));
  11998. + return -EMSGSIZE;
  11999. + }
  12000. +
  12001. + /* Pin down payload. */
  12002. + sp.ptr = sf.ptr + sf.len + sizeof(u16);
  12003. +
  12004. + /* Check for frame + payload length. */
  12005. + if (source->len < SSH_MESSAGE_LENGTH(sp.len)) {
  12006. + dev_dbg(dev, "rx: parser: not enough data for payload\n");
  12007. + return 0;
  12008. + }
  12009. +
  12010. + /* Validate payload CRC. */
  12011. + if (unlikely(!sshp_validate_crc(&sp, sp.ptr + sp.len))) {
  12012. + dev_warn(dev, "rx: parser: invalid payload CRC\n");
  12013. + return -EBADMSG;
  12014. + }
  12015. +
  12016. + *frame = (struct ssh_frame *)sf.ptr;
  12017. + *payload = sp;
  12018. +
  12019. + dev_dbg(dev, "rx: parser: valid frame found (type: %#04x, len: %u)\n",
  12020. + (*frame)->type, (*frame)->len);
  12021. +
  12022. + return 0;
  12023. +}
  12024. +
  12025. +/**
  12026. + * sshp_parse_command() - Parse SSH command frame payload.
  12027. + * @dev: The device used for logging.
  12028. + * @source: The source to parse from.
  12029. + * @command: The parsed command (output).
  12030. + * @command_data: The parsed command data/payload (output).
  12031. + *
  12032. + * Parses and validates a SSH command frame payload. Sets the @command pointer
  12033. + * to the command header and the @command_data span to the command data (i.e.
  12034. + * payload of the command). This will result in a zero-length span if the
  12035. + * command does not have any associated data/payload. This function does not
  12036. + * check the frame-payload-type field, which should be checked by the caller
  12037. + * before calling this function.
  12038. + *
  12039. + * The @source parameter should be the complete frame payload, e.g. returned
  12040. + * by the sshp_parse_frame() command.
  12041. + *
  12042. + * This function does not copy any data, but rather only validates the frame
  12043. + * payload data and sets pointers (and length values) to indicate the
  12044. + * respective parts.
  12045. + *
  12046. + * Return: Returns zero on success or %-ENOMSG if @source does not represent a
  12047. + * valid command-type frame payload, i.e. is too short.
  12048. + */
  12049. +int sshp_parse_command(const struct device *dev, const struct ssam_span *source,
  12050. + struct ssh_command **command,
  12051. + struct ssam_span *command_data)
  12052. +{
  12053. + /* Check for minimum length. */
  12054. + if (unlikely(source->len < sizeof(struct ssh_command))) {
  12055. + *command = NULL;
  12056. + command_data->ptr = NULL;
  12057. + command_data->len = 0;
  12058. +
  12059. + dev_err(dev, "rx: parser: command payload is too short\n");
  12060. + return -ENOMSG;
  12061. + }
  12062. +
  12063. + *command = (struct ssh_command *)source->ptr;
  12064. + command_data->ptr = source->ptr + sizeof(struct ssh_command);
  12065. + command_data->len = source->len - sizeof(struct ssh_command);
  12066. +
  12067. + dev_dbg(dev, "rx: parser: valid command found (tc: %#04x, cid: %#04x)\n",
  12068. + (*command)->tc, (*command)->cid);
  12069. +
  12070. + return 0;
  12071. +}
  12072. diff --git a/drivers/platform/x86/surface_aggregator/ssh_parser.h b/drivers/platform/x86/surface_aggregator/ssh_parser.h
  12073. new file mode 100644
  12074. index 000000000000..3bd6e180fd16
  12075. --- /dev/null
  12076. +++ b/drivers/platform/x86/surface_aggregator/ssh_parser.h
  12077. @@ -0,0 +1,154 @@
  12078. +/* SPDX-License-Identifier: GPL-2.0+ */
  12079. +/*
  12080. + * SSH message parser.
  12081. + *
  12082. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  12083. + */
  12084. +
  12085. +#ifndef _SURFACE_AGGREGATOR_SSH_PARSER_H
  12086. +#define _SURFACE_AGGREGATOR_SSH_PARSER_H
  12087. +
  12088. +#include <linux/device.h>
  12089. +#include <linux/kfifo.h>
  12090. +#include <linux/slab.h>
  12091. +#include <linux/types.h>
  12092. +
  12093. +#include <linux/surface_aggregator/serial_hub.h>
  12094. +
  12095. +/**
  12096. + * struct sshp_buf - Parser buffer for SSH messages.
  12097. + * @ptr: Pointer to the beginning of the buffer.
  12098. + * @len: Number of bytes used in the buffer.
  12099. + * @cap: Maximum capacity of the buffer.
  12100. + */
  12101. +struct sshp_buf {
  12102. + u8 *ptr;
  12103. + size_t len;
  12104. + size_t cap;
  12105. +};
  12106. +
  12107. +/**
  12108. + * sshp_buf_init() - Initialize a SSH parser buffer.
  12109. + * @buf: The buffer to initialize.
  12110. + * @ptr: The memory backing the buffer.
  12111. + * @cap: The length of the memory backing the buffer, i.e. its capacity.
  12112. + *
  12113. + * Initializes the buffer with the given memory as backing and set its used
  12114. + * length to zero.
  12115. + */
  12116. +static inline void sshp_buf_init(struct sshp_buf *buf, u8 *ptr, size_t cap)
  12117. +{
  12118. + buf->ptr = ptr;
  12119. + buf->len = 0;
  12120. + buf->cap = cap;
  12121. +}
  12122. +
  12123. +/**
  12124. + * sshp_buf_alloc() - Allocate and initialize a SSH parser buffer.
  12125. + * @buf: The buffer to initialize/allocate to.
  12126. + * @cap: The desired capacity of the buffer.
  12127. + * @flags: The flags used for allocating the memory.
  12128. + *
  12129. + * Allocates @cap bytes and initializes the provided buffer struct with the
  12130. + * allocated memory.
  12131. + *
  12132. + * Return: Returns zero on success and %-ENOMEM if allocation failed.
  12133. + */
  12134. +static inline int sshp_buf_alloc(struct sshp_buf *buf, size_t cap, gfp_t flags)
  12135. +{
  12136. + u8 *ptr;
  12137. +
  12138. + ptr = kzalloc(cap, flags);
  12139. + if (!ptr)
  12140. + return -ENOMEM;
  12141. +
  12142. + sshp_buf_init(buf, ptr, cap);
  12143. + return 0;
  12144. +}
  12145. +
  12146. +/**
  12147. + * sshp_buf_free() - Free a SSH parser buffer.
  12148. + * @buf: The buffer to free.
  12149. + *
  12150. + * Frees a SSH parser buffer by freeing the memory backing it and then
  12151. + * resetting its pointer to %NULL and length and capacity to zero. Intended to
  12152. + * free a buffer previously allocated with sshp_buf_alloc().
  12153. + */
  12154. +static inline void sshp_buf_free(struct sshp_buf *buf)
  12155. +{
  12156. + kfree(buf->ptr);
  12157. + buf->ptr = NULL;
  12158. + buf->len = 0;
  12159. + buf->cap = 0;
  12160. +}
  12161. +
  12162. +/**
  12163. + * sshp_buf_drop() - Drop data from the beginning of the buffer.
  12164. + * @buf: The buffer to drop data from.
  12165. + * @n: The number of bytes to drop.
  12166. + *
  12167. + * Drops the first @n bytes from the buffer. Re-aligns any remaining data to
  12168. + * the beginning of the buffer.
  12169. + */
  12170. +static inline void sshp_buf_drop(struct sshp_buf *buf, size_t n)
  12171. +{
  12172. + memmove(buf->ptr, buf->ptr + n, buf->len - n);
  12173. + buf->len -= n;
  12174. +}
  12175. +
  12176. +/**
  12177. + * sshp_buf_read_from_fifo() - Transfer data from a fifo to the buffer.
  12178. + * @buf: The buffer to write the data into.
  12179. + * @fifo: The fifo to read the data from.
  12180. + *
  12181. + * Transfers the data contained in the fifo to the buffer, removing it from
  12182. + * the fifo. This function will try to transfer as much data as possible,
  12183. + * limited either by the remaining space in the buffer or by the number of
  12184. + * bytes available in the fifo.
  12185. + *
  12186. + * Return: Returns the number of bytes transferred.
  12187. + */
  12188. +static inline size_t sshp_buf_read_from_fifo(struct sshp_buf *buf,
  12189. + struct kfifo *fifo)
  12190. +{
  12191. + size_t n;
  12192. +
  12193. + n = kfifo_out(fifo, buf->ptr + buf->len, buf->cap - buf->len);
  12194. + buf->len += n;
  12195. +
  12196. + return n;
  12197. +}
  12198. +
  12199. +/**
  12200. + * sshp_buf_span_from() - Initialize a span from the given buffer and offset.
  12201. + * @buf: The buffer to create the span from.
  12202. + * @offset: The offset in the buffer at which the span should start.
  12203. + * @span: The span to initialize (output).
  12204. + *
  12205. + * Initializes the provided span to point to the memory at the given offset in
  12206. + * the buffer, with the length of the span being capped by the number of bytes
  12207. + * used in the buffer after the offset (i.e. bytes remaining after the
  12208. + * offset).
  12209. + *
  12210. + * Warning: This function does not validate that @offset is less than or equal
  12211. + * to the number of bytes used in the buffer or the buffer capacity. This must
  12212. + * be guaranteed by the caller.
  12213. + */
  12214. +static inline void sshp_buf_span_from(struct sshp_buf *buf, size_t offset,
  12215. + struct ssam_span *span)
  12216. +{
  12217. + span->ptr = buf->ptr + offset;
  12218. + span->len = buf->len - offset;
  12219. +}
  12220. +
  12221. +bool sshp_find_syn(const struct ssam_span *src, struct ssam_span *rem);
  12222. +
  12223. +int sshp_parse_frame(const struct device *dev, const struct ssam_span *source,
  12224. + struct ssh_frame **frame, struct ssam_span *payload,
  12225. + size_t maxlen);
  12226. +
  12227. +int sshp_parse_command(const struct device *dev, const struct ssam_span *source,
  12228. + struct ssh_command **command,
  12229. + struct ssam_span *command_data);
  12230. +
  12231. +#endif /* _SURFACE_AGGREGATOR_SSH_PARSER_h */
  12232. diff --git a/drivers/platform/x86/surface_aggregator/ssh_request_layer.c b/drivers/platform/x86/surface_aggregator/ssh_request_layer.c
  12233. new file mode 100644
  12234. index 000000000000..bfe1aaf38065
  12235. --- /dev/null
  12236. +++ b/drivers/platform/x86/surface_aggregator/ssh_request_layer.c
  12237. @@ -0,0 +1,1263 @@
  12238. +// SPDX-License-Identifier: GPL-2.0+
  12239. +/*
  12240. + * SSH request transport layer.
  12241. + *
  12242. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  12243. + */
  12244. +
  12245. +#include <asm/unaligned.h>
  12246. +#include <linux/atomic.h>
  12247. +#include <linux/completion.h>
  12248. +#include <linux/error-injection.h>
  12249. +#include <linux/ktime.h>
  12250. +#include <linux/limits.h>
  12251. +#include <linux/list.h>
  12252. +#include <linux/slab.h>
  12253. +#include <linux/spinlock.h>
  12254. +#include <linux/types.h>
  12255. +#include <linux/workqueue.h>
  12256. +
  12257. +#include <linux/surface_aggregator/serial_hub.h>
  12258. +#include <linux/surface_aggregator/controller.h>
  12259. +
  12260. +#include "ssh_packet_layer.h"
  12261. +#include "ssh_request_layer.h"
  12262. +
  12263. +#include "trace.h"
  12264. +
  12265. +/*
  12266. + * SSH_RTL_REQUEST_TIMEOUT - Request timeout.
  12267. + *
  12268. + * Timeout as ktime_t delta for request responses. If we have not received a
  12269. + * response in this time-frame after finishing the underlying packet
  12270. + * transmission, the request will be completed with %-ETIMEDOUT as status
  12271. + * code.
  12272. + */
  12273. +#define SSH_RTL_REQUEST_TIMEOUT ms_to_ktime(3000)
  12274. +
  12275. +/*
  12276. + * SSH_RTL_REQUEST_TIMEOUT_RESOLUTION - Request timeout granularity.
  12277. + *
  12278. + * Time-resolution for timeouts. Should be larger than one jiffy to avoid
  12279. + * direct re-scheduling of reaper work_struct.
  12280. + */
  12281. +#define SSH_RTL_REQUEST_TIMEOUT_RESOLUTION ms_to_ktime(max(2000 / HZ, 50))
  12282. +
  12283. +/*
  12284. + * SSH_RTL_MAX_PENDING - Maximum number of pending requests.
  12285. + *
  12286. + * Maximum number of requests concurrently waiting to be completed (i.e.
  12287. + * waiting for the corresponding packet transmission to finish if they don't
  12288. + * have a response or waiting for a response if they have one).
  12289. + */
  12290. +#define SSH_RTL_MAX_PENDING 3
  12291. +
  12292. +/*
  12293. + * SSH_RTL_TX_BATCH - Maximum number of requests processed per work execution.
  12294. + * Used to prevent livelocking of the workqueue. Value chosen via educated
  12295. + * guess, may be adjusted.
  12296. + */
  12297. +#define SSH_RTL_TX_BATCH 10
  12298. +
  12299. +#ifdef CONFIG_SURFACE_AGGREGATOR_ERROR_INJECTION
  12300. +
  12301. +/**
  12302. + * ssh_rtl_should_drop_response() - Error injection hook to drop request
  12303. + * responses.
  12304. + *
  12305. + * Useful to cause request transmission timeouts in the driver by dropping the
  12306. + * response to a request.
  12307. + */
  12308. +static noinline bool ssh_rtl_should_drop_response(void)
  12309. +{
  12310. + return false;
  12311. +}
  12312. +ALLOW_ERROR_INJECTION(ssh_rtl_should_drop_response, TRUE);
  12313. +
  12314. +#else
  12315. +
  12316. +static inline bool ssh_rtl_should_drop_response(void)
  12317. +{
  12318. + return false;
  12319. +}
  12320. +
  12321. +#endif
  12322. +
  12323. +static u16 ssh_request_get_rqid(struct ssh_request *rqst)
  12324. +{
  12325. + return get_unaligned_le16(rqst->packet.data.ptr
  12326. + + SSH_MSGOFFSET_COMMAND(rqid));
  12327. +}
  12328. +
  12329. +static u32 ssh_request_get_rqid_safe(struct ssh_request *rqst)
  12330. +{
  12331. + if (!rqst->packet.data.ptr)
  12332. + return U32_MAX;
  12333. +
  12334. + return ssh_request_get_rqid(rqst);
  12335. +}
  12336. +
  12337. +static void ssh_rtl_queue_remove(struct ssh_request *rqst)
  12338. +{
  12339. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  12340. +
  12341. + spin_lock(&rtl->queue.lock);
  12342. +
  12343. + if (!test_and_clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &rqst->state)) {
  12344. + spin_unlock(&rtl->queue.lock);
  12345. + return;
  12346. + }
  12347. +
  12348. + list_del(&rqst->node);
  12349. +
  12350. + spin_unlock(&rtl->queue.lock);
  12351. + ssh_request_put(rqst);
  12352. +}
  12353. +
  12354. +static bool ssh_rtl_queue_empty(struct ssh_rtl *rtl)
  12355. +{
  12356. + bool empty;
  12357. +
  12358. + spin_lock(&rtl->queue.lock);
  12359. + empty = list_empty(&rtl->queue.head);
  12360. + spin_unlock(&rtl->queue.lock);
  12361. +
  12362. + return empty;
  12363. +}
  12364. +
  12365. +static void ssh_rtl_pending_remove(struct ssh_request *rqst)
  12366. +{
  12367. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  12368. +
  12369. + spin_lock(&rtl->pending.lock);
  12370. +
  12371. + if (!test_and_clear_bit(SSH_REQUEST_SF_PENDING_BIT, &rqst->state)) {
  12372. + spin_unlock(&rtl->pending.lock);
  12373. + return;
  12374. + }
  12375. +
  12376. + atomic_dec(&rtl->pending.count);
  12377. + list_del(&rqst->node);
  12378. +
  12379. + spin_unlock(&rtl->pending.lock);
  12380. +
  12381. + ssh_request_put(rqst);
  12382. +}
  12383. +
  12384. +static int ssh_rtl_tx_pending_push(struct ssh_request *rqst)
  12385. +{
  12386. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  12387. +
  12388. + spin_lock(&rtl->pending.lock);
  12389. +
  12390. + if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state)) {
  12391. + spin_unlock(&rtl->pending.lock);
  12392. + return -EINVAL;
  12393. + }
  12394. +
  12395. + if (test_and_set_bit(SSH_REQUEST_SF_PENDING_BIT, &rqst->state)) {
  12396. + spin_unlock(&rtl->pending.lock);
  12397. + return -EALREADY;
  12398. + }
  12399. +
  12400. + atomic_inc(&rtl->pending.count);
  12401. + list_add_tail(&ssh_request_get(rqst)->node, &rtl->pending.head);
  12402. +
  12403. + spin_unlock(&rtl->pending.lock);
  12404. + return 0;
  12405. +}
  12406. +
  12407. +static void ssh_rtl_complete_with_status(struct ssh_request *rqst, int status)
  12408. +{
  12409. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  12410. +
  12411. + trace_ssam_request_complete(rqst, status);
  12412. +
  12413. + /* rtl/ptl may not be set if we're canceling before submitting. */
  12414. + rtl_dbg_cond(rtl, "rtl: completing request (rqid: %#06x, status: %d)\n",
  12415. + ssh_request_get_rqid_safe(rqst), status);
  12416. +
  12417. + rqst->ops->complete(rqst, NULL, NULL, status);
  12418. +}
  12419. +
  12420. +static void ssh_rtl_complete_with_rsp(struct ssh_request *rqst,
  12421. + const struct ssh_command *cmd,
  12422. + const struct ssam_span *data)
  12423. +{
  12424. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  12425. +
  12426. + trace_ssam_request_complete(rqst, 0);
  12427. +
  12428. + rtl_dbg(rtl, "rtl: completing request with response (rqid: %#06x)\n",
  12429. + ssh_request_get_rqid(rqst));
  12430. +
  12431. + rqst->ops->complete(rqst, cmd, data, 0);
  12432. +}
  12433. +
  12434. +static bool ssh_rtl_tx_can_process(struct ssh_request *rqst)
  12435. +{
  12436. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  12437. +
  12438. + if (test_bit(SSH_REQUEST_TY_FLUSH_BIT, &rqst->state))
  12439. + return !atomic_read(&rtl->pending.count);
  12440. +
  12441. + return atomic_read(&rtl->pending.count) < SSH_RTL_MAX_PENDING;
  12442. +}
  12443. +
  12444. +static struct ssh_request *ssh_rtl_tx_next(struct ssh_rtl *rtl)
  12445. +{
  12446. + struct ssh_request *rqst = ERR_PTR(-ENOENT);
  12447. + struct ssh_request *p, *n;
  12448. +
  12449. + spin_lock(&rtl->queue.lock);
  12450. +
  12451. + /* Find first non-locked request and remove it. */
  12452. + list_for_each_entry_safe(p, n, &rtl->queue.head, node) {
  12453. + if (unlikely(test_bit(SSH_REQUEST_SF_LOCKED_BIT, &p->state)))
  12454. + continue;
  12455. +
  12456. + if (!ssh_rtl_tx_can_process(p)) {
  12457. + rqst = ERR_PTR(-EBUSY);
  12458. + break;
  12459. + }
  12460. +
  12461. + /* Remove from queue and mark as transmitting. */
  12462. + set_bit(SSH_REQUEST_SF_TRANSMITTING_BIT, &p->state);
  12463. + /* Ensure state never gets zero. */
  12464. + smp_mb__before_atomic();
  12465. + clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &p->state);
  12466. +
  12467. + list_del(&p->node);
  12468. +
  12469. + rqst = p;
  12470. + break;
  12471. + }
  12472. +
  12473. + spin_unlock(&rtl->queue.lock);
  12474. + return rqst;
  12475. +}
  12476. +
  12477. +static int ssh_rtl_tx_try_process_one(struct ssh_rtl *rtl)
  12478. +{
  12479. + struct ssh_request *rqst;
  12480. + int status;
  12481. +
  12482. + /* Get and prepare next request for transmit. */
  12483. + rqst = ssh_rtl_tx_next(rtl);
  12484. + if (IS_ERR(rqst))
  12485. + return PTR_ERR(rqst);
  12486. +
  12487. + /* Add it to/mark it as pending. */
  12488. + status = ssh_rtl_tx_pending_push(rqst);
  12489. + if (status) {
  12490. + ssh_request_put(rqst);
  12491. + return -EAGAIN;
  12492. + }
  12493. +
  12494. + /* Submit packet. */
  12495. + status = ssh_ptl_submit(&rtl->ptl, &rqst->packet);
  12496. + if (status == -ESHUTDOWN) {
  12497. + /*
  12498. + * Packet has been refused due to the packet layer shutting
  12499. + * down. Complete it here.
  12500. + */
  12501. + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state);
  12502. + /*
  12503. + * Note: A barrier is not required here, as there are only two
  12504. + * references in the system at this point: The one that we have,
  12505. + * and the other one that belongs to the pending set. Due to the
  12506. + * request being marked as "transmitting", our process is the
  12507. + * only one allowed to remove the pending node and change the
  12508. + * state. Normally, the task would fall to the packet callback,
  12509. + * but as this is a path where submission failed, this callback
  12510. + * will never be executed.
  12511. + */
  12512. +
  12513. + ssh_rtl_pending_remove(rqst);
  12514. + ssh_rtl_complete_with_status(rqst, -ESHUTDOWN);
  12515. +
  12516. + ssh_request_put(rqst);
  12517. + return -ESHUTDOWN;
  12518. +
  12519. + } else if (status) {
  12520. + /*
  12521. + * If submitting the packet failed and the packet layer isn't
  12522. + * shutting down, the packet has either been submitted/queued
  12523. + * before (-EALREADY, which cannot happen as we have
  12524. + * guaranteed that requests cannot be re-submitted), or the
  12525. + * packet was marked as locked (-EINVAL). To mark the packet
  12526. + * locked at this stage, the request, and thus the packets
  12527. + * itself, had to have been canceled. Simply drop the
  12528. + * reference. Cancellation itself will remove it from the set
  12529. + * of pending requests.
  12530. + */
  12531. +
  12532. + WARN_ON(status != -EINVAL);
  12533. +
  12534. + ssh_request_put(rqst);
  12535. + return -EAGAIN;
  12536. + }
  12537. +
  12538. + ssh_request_put(rqst);
  12539. + return 0;
  12540. +}
  12541. +
  12542. +static bool ssh_rtl_tx_schedule(struct ssh_rtl *rtl)
  12543. +{
  12544. + if (atomic_read(&rtl->pending.count) >= SSH_RTL_MAX_PENDING)
  12545. + return false;
  12546. +
  12547. + if (ssh_rtl_queue_empty(rtl))
  12548. + return false;
  12549. +
  12550. + return schedule_work(&rtl->tx.work);
  12551. +}
  12552. +
  12553. +static void ssh_rtl_tx_work_fn(struct work_struct *work)
  12554. +{
  12555. + struct ssh_rtl *rtl = to_ssh_rtl(work, tx.work);
  12556. + unsigned int iterations = SSH_RTL_TX_BATCH;
  12557. + int status;
  12558. +
  12559. + /*
  12560. + * Try to be nice and not block/live-lock the workqueue: Run a maximum
  12561. + * of 10 tries, then re-submit if necessary. This should not be
  12562. + * necessary for normal execution, but guarantee it anyway.
  12563. + */
  12564. + do {
  12565. + status = ssh_rtl_tx_try_process_one(rtl);
  12566. + if (status == -ENOENT || status == -EBUSY)
  12567. + return; /* No more requests to process. */
  12568. +
  12569. + if (status == -ESHUTDOWN) {
  12570. + /*
  12571. + * Packet system shutting down. No new packets can be
  12572. + * transmitted. Return silently, the party initiating
  12573. + * the shutdown should handle the rest.
  12574. + */
  12575. + return;
  12576. + }
  12577. +
  12578. + WARN_ON(status != 0 && status != -EAGAIN);
  12579. + } while (--iterations);
  12580. +
  12581. + /* Out of tries, reschedule. */
  12582. + ssh_rtl_tx_schedule(rtl);
  12583. +}
  12584. +
  12585. +/**
  12586. + * ssh_rtl_submit() - Submit a request to the transport layer.
  12587. + * @rtl: The request transport layer.
  12588. + * @rqst: The request to submit.
  12589. + *
  12590. + * Submits a request to the transport layer. A single request may not be
  12591. + * submitted multiple times without reinitializing it.
  12592. + *
  12593. + * Return: Returns zero on success, %-EINVAL if the request type is invalid or
  12594. + * the request has been canceled prior to submission, %-EALREADY if the
  12595. + * request has already been submitted, or %-ESHUTDOWN in case the request
  12596. + * transport layer has been shut down.
  12597. + */
  12598. +int ssh_rtl_submit(struct ssh_rtl *rtl, struct ssh_request *rqst)
  12599. +{
  12600. + trace_ssam_request_submit(rqst);
  12601. +
  12602. + /*
  12603. + * Ensure that requests expecting a response are sequenced. If this
  12604. + * invariant ever changes, see the comment in ssh_rtl_complete() on what
  12605. + * is required to be changed in the code.
  12606. + */
  12607. + if (test_bit(SSH_REQUEST_TY_HAS_RESPONSE_BIT, &rqst->state))
  12608. + if (!test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &rqst->packet.state))
  12609. + return -EINVAL;
  12610. +
  12611. + spin_lock(&rtl->queue.lock);
  12612. +
  12613. + /*
  12614. + * Try to set ptl and check if this request has already been submitted.
  12615. + *
  12616. + * Must be inside lock as we might run into a lost update problem
  12617. + * otherwise: If this were outside of the lock, cancellation in
  12618. + * ssh_rtl_cancel_nonpending() may run after we've set the ptl
  12619. + * reference but before we enter the lock. In that case, we'd detect
  12620. + * that the request is being added to the queue and would try to remove
  12621. + * it from that, but removal might fail because it hasn't actually been
  12622. + * added yet. By putting this cmpxchg in the critical section, we
  12623. + * ensure that the queuing detection only triggers when we are already
  12624. + * in the critical section and the remove process will wait until the
  12625. + * push operation has been completed (via lock) due to that. Only then,
  12626. + * we can safely try to remove it.
  12627. + */
  12628. + if (cmpxchg(&rqst->packet.ptl, NULL, &rtl->ptl)) {
  12629. + spin_unlock(&rtl->queue.lock);
  12630. + return -EALREADY;
  12631. + }
  12632. +
  12633. + /*
  12634. + * Ensure that we set ptl reference before we continue modifying state.
  12635. + * This is required for non-pending cancellation. This barrier is paired
  12636. + * with the one in ssh_rtl_cancel_nonpending().
  12637. + *
  12638. + * By setting the ptl reference before we test for "locked", we can
  12639. + * check if the "locked" test may have already run. See comments in
  12640. + * ssh_rtl_cancel_nonpending() for more detail.
  12641. + */
  12642. + smp_mb__after_atomic();
  12643. +
  12644. + if (test_bit(SSH_RTL_SF_SHUTDOWN_BIT, &rtl->state)) {
  12645. + spin_unlock(&rtl->queue.lock);
  12646. + return -ESHUTDOWN;
  12647. + }
  12648. +
  12649. + if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state)) {
  12650. + spin_unlock(&rtl->queue.lock);
  12651. + return -EINVAL;
  12652. + }
  12653. +
  12654. + set_bit(SSH_REQUEST_SF_QUEUED_BIT, &rqst->state);
  12655. + list_add_tail(&ssh_request_get(rqst)->node, &rtl->queue.head);
  12656. +
  12657. + spin_unlock(&rtl->queue.lock);
  12658. +
  12659. + ssh_rtl_tx_schedule(rtl);
  12660. + return 0;
  12661. +}
  12662. +
  12663. +static void ssh_rtl_timeout_reaper_mod(struct ssh_rtl *rtl, ktime_t now,
  12664. + ktime_t expires)
  12665. +{
  12666. + unsigned long delta = msecs_to_jiffies(ktime_ms_delta(expires, now));
  12667. + ktime_t aexp = ktime_add(expires, SSH_RTL_REQUEST_TIMEOUT_RESOLUTION);
  12668. +
  12669. + spin_lock(&rtl->rtx_timeout.lock);
  12670. +
  12671. + /* Re-adjust / schedule reaper only if it is above resolution delta. */
  12672. + if (ktime_before(aexp, rtl->rtx_timeout.expires)) {
  12673. + rtl->rtx_timeout.expires = expires;
  12674. + mod_delayed_work(system_wq, &rtl->rtx_timeout.reaper, delta);
  12675. + }
  12676. +
  12677. + spin_unlock(&rtl->rtx_timeout.lock);
  12678. +}
  12679. +
  12680. +static void ssh_rtl_timeout_start(struct ssh_request *rqst)
  12681. +{
  12682. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  12683. + ktime_t timestamp = ktime_get_coarse_boottime();
  12684. + ktime_t timeout = rtl->rtx_timeout.timeout;
  12685. +
  12686. + if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state))
  12687. + return;
  12688. +
  12689. + /*
  12690. + * Note: The timestamp gets set only once. This happens on the packet
  12691. + * callback. All other access to it is read-only.
  12692. + */
  12693. + WRITE_ONCE(rqst->timestamp, timestamp);
  12694. + /*
  12695. + * Ensure timestamp is set before starting the reaper. Paired with
  12696. + * implicit barrier following check on ssh_request_get_expiration() in
  12697. + * ssh_rtl_timeout_reap.
  12698. + */
  12699. + smp_mb__after_atomic();
  12700. +
  12701. + ssh_rtl_timeout_reaper_mod(rtl, timestamp, timestamp + timeout);
  12702. +}
  12703. +
  12704. +static void ssh_rtl_complete(struct ssh_rtl *rtl,
  12705. + const struct ssh_command *command,
  12706. + const struct ssam_span *command_data)
  12707. +{
  12708. + struct ssh_request *r = NULL;
  12709. + struct ssh_request *p, *n;
  12710. + u16 rqid = get_unaligned_le16(&command->rqid);
  12711. +
  12712. + trace_ssam_rx_response_received(command, command_data->len);
  12713. +
  12714. + /*
  12715. + * Get request from pending based on request ID and mark it as response
  12716. + * received and locked.
  12717. + */
  12718. + spin_lock(&rtl->pending.lock);
  12719. + list_for_each_entry_safe(p, n, &rtl->pending.head, node) {
  12720. + /* We generally expect requests to be processed in order. */
  12721. + if (unlikely(ssh_request_get_rqid(p) != rqid))
  12722. + continue;
  12723. +
  12724. + /* Simulate response timeout. */
  12725. + if (ssh_rtl_should_drop_response()) {
  12726. + spin_unlock(&rtl->pending.lock);
  12727. +
  12728. + trace_ssam_ei_rx_drop_response(p);
  12729. + rtl_info(rtl, "request error injection: dropping response for request %p\n",
  12730. + &p->packet);
  12731. + return;
  12732. + }
  12733. +
  12734. + /*
  12735. + * Mark as "response received" and "locked" as we're going to
  12736. + * complete it.
  12737. + */
  12738. + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &p->state);
  12739. + set_bit(SSH_REQUEST_SF_RSPRCVD_BIT, &p->state);
  12740. + /* Ensure state never gets zero. */
  12741. + smp_mb__before_atomic();
  12742. + clear_bit(SSH_REQUEST_SF_PENDING_BIT, &p->state);
  12743. +
  12744. + atomic_dec(&rtl->pending.count);
  12745. + list_del(&p->node);
  12746. +
  12747. + r = p;
  12748. + break;
  12749. + }
  12750. + spin_unlock(&rtl->pending.lock);
  12751. +
  12752. + if (!r) {
  12753. + rtl_warn(rtl, "rtl: dropping unexpected command message (rqid = %#06x)\n",
  12754. + rqid);
  12755. + return;
  12756. + }
  12757. +
  12758. + /* If the request hasn't been completed yet, we will do this now. */
  12759. + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state)) {
  12760. + ssh_request_put(r);
  12761. + ssh_rtl_tx_schedule(rtl);
  12762. + return;
  12763. + }
  12764. +
  12765. + /*
  12766. + * Make sure the request has been transmitted. In case of a sequenced
  12767. + * request, we are guaranteed that the completion callback will run on
  12768. + * the receiver thread directly when the ACK for the packet has been
  12769. + * received. Similarly, this function is guaranteed to run on the
  12770. + * receiver thread. Thus we are guaranteed that if the packet has been
  12771. + * successfully transmitted and received an ACK, the transmitted flag
  12772. + * has been set and is visible here.
  12773. + *
  12774. + * We are currently not handling unsequenced packets here, as those
  12775. + * should never expect a response as ensured in ssh_rtl_submit. If this
  12776. + * ever changes, one would have to test for
  12777. + *
  12778. + * (r->state & (transmitting | transmitted))
  12779. + *
  12780. + * on unsequenced packets to determine if they could have been
  12781. + * transmitted. There are no synchronization guarantees as in the
  12782. + * sequenced case, since, in this case, the callback function will not
  12783. + * run on the same thread. Thus an exact determination is impossible.
  12784. + */
  12785. + if (!test_bit(SSH_REQUEST_SF_TRANSMITTED_BIT, &r->state)) {
  12786. + rtl_err(rtl, "rtl: received response before ACK for request (rqid = %#06x)\n",
  12787. + rqid);
  12788. +
  12789. + /*
  12790. + * NB: Timeout has already been canceled, request already been
  12791. + * removed from pending and marked as locked and completed. As
  12792. + * we receive a "false" response, the packet might still be
  12793. + * queued though.
  12794. + */
  12795. + ssh_rtl_queue_remove(r);
  12796. +
  12797. + ssh_rtl_complete_with_status(r, -EREMOTEIO);
  12798. + ssh_request_put(r);
  12799. +
  12800. + ssh_rtl_tx_schedule(rtl);
  12801. + return;
  12802. + }
  12803. +
  12804. + /*
  12805. + * NB: Timeout has already been canceled, request already been
  12806. + * removed from pending and marked as locked and completed. The request
  12807. + * can also not be queued any more, as it has been marked as
  12808. + * transmitting and later transmitted. Thus no need to remove it from
  12809. + * anywhere.
  12810. + */
  12811. +
  12812. + ssh_rtl_complete_with_rsp(r, command, command_data);
  12813. + ssh_request_put(r);
  12814. +
  12815. + ssh_rtl_tx_schedule(rtl);
  12816. +}
  12817. +
  12818. +static bool ssh_rtl_cancel_nonpending(struct ssh_request *r)
  12819. +{
  12820. + struct ssh_rtl *rtl;
  12821. + unsigned long flags, fixed;
  12822. + bool remove;
  12823. +
  12824. + /*
  12825. + * Handle unsubmitted request: Try to mark the packet as locked,
  12826. + * expecting the state to be zero (i.e. unsubmitted). Note that, if
  12827. + * setting the state worked, we might still be adding the packet to the
  12828. + * queue in a currently executing submit call. In that case, however,
  12829. + * ptl reference must have been set previously, as locked is checked
  12830. + * after setting ptl. Furthermore, when the ptl reference is set, the
  12831. + * submission process is guaranteed to have entered the critical
  12832. + * section. Thus only if we successfully locked this request and ptl is
  12833. + * NULL, we have successfully removed the request, i.e. we are
  12834. + * guaranteed that, due to the "locked" check in ssh_rtl_submit(), the
  12835. + * packet will never be added. Otherwise, we need to try and grab it
  12836. + * from the queue, where we are now guaranteed that the packet is or has
  12837. + * been due to the critical section.
  12838. + *
  12839. + * Note that if the cmpxchg() fails, we are guaranteed that ptl has
  12840. + * been set and is non-NULL, as states can only be nonzero after this
  12841. + * has been set. Also note that we need to fetch the static (type)
  12842. + * flags to ensure that they don't cause the cmpxchg() to fail.
  12843. + */
  12844. + fixed = READ_ONCE(r->state) & SSH_REQUEST_FLAGS_TY_MASK;
  12845. + flags = cmpxchg(&r->state, fixed, SSH_REQUEST_SF_LOCKED_BIT);
  12846. +
  12847. + /*
  12848. + * Force correct ordering with regards to state and ptl reference access
  12849. + * to safe-guard cancellation to concurrent submission against a
  12850. + * lost-update problem. First try to exchange state, then also check
  12851. + * ptl if that worked. This barrier is paired with the
  12852. + * one in ssh_rtl_submit().
  12853. + */
  12854. + smp_mb__after_atomic();
  12855. +
  12856. + if (flags == fixed && !READ_ONCE(r->packet.ptl)) {
  12857. + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  12858. + return true;
  12859. +
  12860. + ssh_rtl_complete_with_status(r, -ECANCELED);
  12861. + return true;
  12862. + }
  12863. +
  12864. + rtl = ssh_request_rtl(r);
  12865. + spin_lock(&rtl->queue.lock);
  12866. +
  12867. + /*
  12868. + * Note: 1) Requests cannot be re-submitted. 2) If a request is
  12869. + * queued, it cannot be "transmitting"/"pending" yet. Thus, if we
  12870. + * successfully remove the request here, we have removed all its
  12871. + * occurrences in the system.
  12872. + */
  12873. +
  12874. + remove = test_and_clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &r->state);
  12875. + if (!remove) {
  12876. + spin_unlock(&rtl->queue.lock);
  12877. + return false;
  12878. + }
  12879. +
  12880. + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
  12881. + list_del(&r->node);
  12882. +
  12883. + spin_unlock(&rtl->queue.lock);
  12884. +
  12885. + ssh_request_put(r); /* Drop reference obtained from queue. */
  12886. +
  12887. + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  12888. + return true;
  12889. +
  12890. + ssh_rtl_complete_with_status(r, -ECANCELED);
  12891. + return true;
  12892. +}
  12893. +
  12894. +static bool ssh_rtl_cancel_pending(struct ssh_request *r)
  12895. +{
  12896. + /* If the packet is already locked, it's going to be removed shortly. */
  12897. + if (test_and_set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state))
  12898. + return true;
  12899. +
  12900. + /*
  12901. + * Now that we have locked the packet, we have guaranteed that it can't
  12902. + * be added to the system any more. If ptl is NULL, the locked
  12903. + * check in ssh_rtl_submit() has not been run and any submission,
  12904. + * currently in progress or called later, won't add the packet. Thus we
  12905. + * can directly complete it.
  12906. + *
  12907. + * The implicit memory barrier of test_and_set_bit() should be enough
  12908. + * to ensure that the correct order (first lock, then check ptl) is
  12909. + * ensured. This is paired with the barrier in ssh_rtl_submit().
  12910. + */
  12911. + if (!READ_ONCE(r->packet.ptl)) {
  12912. + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  12913. + return true;
  12914. +
  12915. + ssh_rtl_complete_with_status(r, -ECANCELED);
  12916. + return true;
  12917. + }
  12918. +
  12919. + /*
  12920. + * Try to cancel the packet. If the packet has not been completed yet,
  12921. + * this will subsequently (and synchronously) call the completion
  12922. + * callback of the packet, which will complete the request.
  12923. + */
  12924. + ssh_ptl_cancel(&r->packet);
  12925. +
  12926. + /*
  12927. + * If the packet has been completed with success, i.e. has not been
  12928. + * canceled by the above call, the request may not have been completed
  12929. + * yet (may be waiting for a response). Check if we need to do this
  12930. + * here.
  12931. + */
  12932. + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  12933. + return true;
  12934. +
  12935. + ssh_rtl_queue_remove(r);
  12936. + ssh_rtl_pending_remove(r);
  12937. + ssh_rtl_complete_with_status(r, -ECANCELED);
  12938. +
  12939. + return true;
  12940. +}
  12941. +
  12942. +/**
  12943. + * ssh_rtl_cancel() - Cancel request.
  12944. + * @rqst: The request to cancel.
  12945. + * @pending: Whether to also cancel pending requests.
  12946. + *
  12947. + * Cancels the given request. If @pending is %false, this will not cancel
  12948. + * pending requests, i.e. requests that have already been submitted to the
  12949. + * packet layer but not been completed yet. If @pending is %true, this will
  12950. + * cancel the given request regardless of the state it is in.
  12951. + *
  12952. + * If the request has been canceled by calling this function, both completion
  12953. + * and release callbacks of the request will be executed in a reasonable
  12954. + * time-frame. This may happen during execution of this function, however,
  12955. + * there is no guarantee for this. For example, a request currently
  12956. + * transmitting will be canceled/completed only after transmission has
  12957. + * completed, and the respective callbacks will be executed on the transmitter
  12958. + * thread, which may happen during, but also some time after execution of the
  12959. + * cancel function.
  12960. + *
  12961. + * Return: Returns %true if the given request has been canceled or completed,
  12962. + * either by this function or prior to calling this function, %false
  12963. + * otherwise. If @pending is %true, this function will always return %true.
  12964. + */
  12965. +bool ssh_rtl_cancel(struct ssh_request *rqst, bool pending)
  12966. +{
  12967. + struct ssh_rtl *rtl;
  12968. + bool canceled;
  12969. +
  12970. + if (test_and_set_bit(SSH_REQUEST_SF_CANCELED_BIT, &rqst->state))
  12971. + return true;
  12972. +
  12973. + trace_ssam_request_cancel(rqst);
  12974. +
  12975. + if (pending)
  12976. + canceled = ssh_rtl_cancel_pending(rqst);
  12977. + else
  12978. + canceled = ssh_rtl_cancel_nonpending(rqst);
  12979. +
  12980. + /* Note: rtl may be NULL if request has not been submitted yet. */
  12981. + rtl = ssh_request_rtl(rqst);
  12982. + if (canceled && rtl)
  12983. + ssh_rtl_tx_schedule(rtl);
  12984. +
  12985. + return canceled;
  12986. +}
  12987. +
  12988. +static void ssh_rtl_packet_callback(struct ssh_packet *p, int status)
  12989. +{
  12990. + struct ssh_request *r = to_ssh_request(p);
  12991. +
  12992. + if (unlikely(status)) {
  12993. + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
  12994. +
  12995. + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  12996. + return;
  12997. +
  12998. + /*
  12999. + * The packet may get canceled even though it has not been
  13000. + * submitted yet. The request may still be queued. Check the
  13001. + * queue and remove it if necessary. As the timeout would have
  13002. + * been started in this function on success, there's no need
  13003. + * to cancel it here.
  13004. + */
  13005. + ssh_rtl_queue_remove(r);
  13006. + ssh_rtl_pending_remove(r);
  13007. + ssh_rtl_complete_with_status(r, status);
  13008. +
  13009. + ssh_rtl_tx_schedule(ssh_request_rtl(r));
  13010. + return;
  13011. + }
  13012. +
  13013. + /* Update state: Mark as transmitted and clear transmitting. */
  13014. + set_bit(SSH_REQUEST_SF_TRANSMITTED_BIT, &r->state);
  13015. + /* Ensure state never gets zero. */
  13016. + smp_mb__before_atomic();
  13017. + clear_bit(SSH_REQUEST_SF_TRANSMITTING_BIT, &r->state);
  13018. +
  13019. + /* If we expect a response, we just need to start the timeout. */
  13020. + if (test_bit(SSH_REQUEST_TY_HAS_RESPONSE_BIT, &r->state)) {
  13021. + /*
  13022. + * Note: This is the only place where the timestamp gets set,
  13023. + * all other access to it is read-only.
  13024. + */
  13025. + ssh_rtl_timeout_start(r);
  13026. + return;
  13027. + }
  13028. +
  13029. + /*
  13030. + * If we don't expect a response, lock, remove, and complete the
  13031. + * request. Note that, at this point, the request is guaranteed to have
  13032. + * left the queue and no timeout has been started. Thus we only need to
  13033. + * remove it from pending. If the request has already been completed (it
  13034. + * may have been canceled) return.
  13035. + */
  13036. +
  13037. + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
  13038. + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  13039. + return;
  13040. +
  13041. + ssh_rtl_pending_remove(r);
  13042. + ssh_rtl_complete_with_status(r, 0);
  13043. +
  13044. + ssh_rtl_tx_schedule(ssh_request_rtl(r));
  13045. +}
  13046. +
  13047. +static ktime_t ssh_request_get_expiration(struct ssh_request *r, ktime_t timeout)
  13048. +{
  13049. + ktime_t timestamp = READ_ONCE(r->timestamp);
  13050. +
  13051. + if (timestamp != KTIME_MAX)
  13052. + return ktime_add(timestamp, timeout);
  13053. + else
  13054. + return KTIME_MAX;
  13055. +}
  13056. +
  13057. +static void ssh_rtl_timeout_reap(struct work_struct *work)
  13058. +{
  13059. + struct ssh_rtl *rtl = to_ssh_rtl(work, rtx_timeout.reaper.work);
  13060. + struct ssh_request *r, *n;
  13061. + LIST_HEAD(claimed);
  13062. + ktime_t now = ktime_get_coarse_boottime();
  13063. + ktime_t timeout = rtl->rtx_timeout.timeout;
  13064. + ktime_t next = KTIME_MAX;
  13065. +
  13066. + trace_ssam_rtl_timeout_reap(atomic_read(&rtl->pending.count));
  13067. +
  13068. + /*
  13069. + * Mark reaper as "not pending". This is done before checking any
  13070. + * requests to avoid lost-update type problems.
  13071. + */
  13072. + spin_lock(&rtl->rtx_timeout.lock);
  13073. + rtl->rtx_timeout.expires = KTIME_MAX;
  13074. + spin_unlock(&rtl->rtx_timeout.lock);
  13075. +
  13076. + spin_lock(&rtl->pending.lock);
  13077. + list_for_each_entry_safe(r, n, &rtl->pending.head, node) {
  13078. + ktime_t expires = ssh_request_get_expiration(r, timeout);
  13079. +
  13080. + /*
  13081. + * Check if the timeout hasn't expired yet. Find out next
  13082. + * expiration date to be handled after this run.
  13083. + */
  13084. + if (ktime_after(expires, now)) {
  13085. + next = ktime_before(expires, next) ? expires : next;
  13086. + continue;
  13087. + }
  13088. +
  13089. + /* Avoid further transitions if locked. */
  13090. + if (test_and_set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state))
  13091. + continue;
  13092. +
  13093. + /*
  13094. + * We have now marked the packet as locked. Thus it cannot be
  13095. + * added to the pending or queued lists again after we've
  13096. + * removed it here. We can therefore re-use the node of this
  13097. + * packet temporarily.
  13098. + */
  13099. +
  13100. + clear_bit(SSH_REQUEST_SF_PENDING_BIT, &r->state);
  13101. +
  13102. + atomic_dec(&rtl->pending.count);
  13103. + list_del(&r->node);
  13104. +
  13105. + list_add_tail(&r->node, &claimed);
  13106. + }
  13107. + spin_unlock(&rtl->pending.lock);
  13108. +
  13109. + /* Cancel and complete the request. */
  13110. + list_for_each_entry_safe(r, n, &claimed, node) {
  13111. + trace_ssam_request_timeout(r);
  13112. +
  13113. + /*
  13114. + * At this point we've removed the packet from pending. This
  13115. + * means that we've obtained the last (only) reference of the
  13116. + * system to it. Thus we can just complete it.
  13117. + */
  13118. + if (!test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  13119. + ssh_rtl_complete_with_status(r, -ETIMEDOUT);
  13120. +
  13121. + /*
  13122. + * Drop the reference we've obtained by removing it from the
  13123. + * pending set.
  13124. + */
  13125. + list_del(&r->node);
  13126. + ssh_request_put(r);
  13127. + }
  13128. +
  13129. + /* Ensure that the reaper doesn't run again immediately. */
  13130. + next = max(next, ktime_add(now, SSH_RTL_REQUEST_TIMEOUT_RESOLUTION));
  13131. + if (next != KTIME_MAX)
  13132. + ssh_rtl_timeout_reaper_mod(rtl, now, next);
  13133. +
  13134. + ssh_rtl_tx_schedule(rtl);
  13135. +}
  13136. +
  13137. +static void ssh_rtl_rx_event(struct ssh_rtl *rtl, const struct ssh_command *cmd,
  13138. + const struct ssam_span *data)
  13139. +{
  13140. + trace_ssam_rx_event_received(cmd, data->len);
  13141. +
  13142. + rtl_dbg(rtl, "rtl: handling event (rqid: %#06x)\n",
  13143. + get_unaligned_le16(&cmd->rqid));
  13144. +
  13145. + rtl->ops.handle_event(rtl, cmd, data);
  13146. +}
  13147. +
  13148. +static void ssh_rtl_rx_command(struct ssh_ptl *p, const struct ssam_span *data)
  13149. +{
  13150. + struct ssh_rtl *rtl = to_ssh_rtl(p, ptl);
  13151. + struct device *dev = &p->serdev->dev;
  13152. + struct ssh_command *command;
  13153. + struct ssam_span command_data;
  13154. +
  13155. + if (sshp_parse_command(dev, data, &command, &command_data))
  13156. + return;
  13157. +
  13158. + if (ssh_rqid_is_event(get_unaligned_le16(&command->rqid)))
  13159. + ssh_rtl_rx_event(rtl, command, &command_data);
  13160. + else
  13161. + ssh_rtl_complete(rtl, command, &command_data);
  13162. +}
  13163. +
  13164. +static void ssh_rtl_rx_data(struct ssh_ptl *p, const struct ssam_span *data)
  13165. +{
  13166. + if (!data->len) {
  13167. + ptl_err(p, "rtl: rx: no data frame payload\n");
  13168. + return;
  13169. + }
  13170. +
  13171. + switch (data->ptr[0]) {
  13172. + case SSH_PLD_TYPE_CMD:
  13173. + ssh_rtl_rx_command(p, data);
  13174. + break;
  13175. +
  13176. + default:
  13177. + ptl_err(p, "rtl: rx: unknown frame payload type (type: %#04x)\n",
  13178. + data->ptr[0]);
  13179. + break;
  13180. + }
  13181. +}
  13182. +
  13183. +static void ssh_rtl_packet_release(struct ssh_packet *p)
  13184. +{
  13185. + struct ssh_request *rqst;
  13186. +
  13187. + rqst = to_ssh_request(p);
  13188. + rqst->ops->release(rqst);
  13189. +}
  13190. +
  13191. +static const struct ssh_packet_ops ssh_rtl_packet_ops = {
  13192. + .complete = ssh_rtl_packet_callback,
  13193. + .release = ssh_rtl_packet_release,
  13194. +};
  13195. +
  13196. +/**
  13197. + * ssh_request_init() - Initialize SSH request.
  13198. + * @rqst: The request to initialize.
  13199. + * @flags: Request flags, determining the type of the request.
  13200. + * @ops: Request operations.
  13201. + *
  13202. + * Initializes the given SSH request and underlying packet. Sets the message
  13203. + * buffer pointer to %NULL and the message buffer length to zero. This buffer
  13204. + * has to be set separately via ssh_request_set_data() before submission and
  13205. + * must contain a valid SSH request message.
  13206. + *
  13207. + * Return: Returns zero on success or %-EINVAL if the given flags are invalid.
  13208. + */
  13209. +int ssh_request_init(struct ssh_request *rqst, enum ssam_request_flags flags,
  13210. + const struct ssh_request_ops *ops)
  13211. +{
  13212. + unsigned long type = BIT(SSH_PACKET_TY_BLOCKING_BIT);
  13213. +
  13214. + /* Unsequenced requests cannot have a response. */
  13215. + if (flags & SSAM_REQUEST_UNSEQUENCED && flags & SSAM_REQUEST_HAS_RESPONSE)
  13216. + return -EINVAL;
  13217. +
  13218. + if (!(flags & SSAM_REQUEST_UNSEQUENCED))
  13219. + type |= BIT(SSH_PACKET_TY_SEQUENCED_BIT);
  13220. +
  13221. + ssh_packet_init(&rqst->packet, type, SSH_PACKET_PRIORITY(DATA, 0),
  13222. + &ssh_rtl_packet_ops);
  13223. +
  13224. + INIT_LIST_HEAD(&rqst->node);
  13225. +
  13226. + rqst->state = 0;
  13227. + if (flags & SSAM_REQUEST_HAS_RESPONSE)
  13228. + rqst->state |= BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT);
  13229. +
  13230. + rqst->timestamp = KTIME_MAX;
  13231. + rqst->ops = ops;
  13232. +
  13233. + return 0;
  13234. +}
  13235. +
  13236. +/**
  13237. + * ssh_rtl_init() - Initialize request transport layer.
  13238. + * @rtl: The request transport layer to initialize.
  13239. + * @serdev: The underlying serial device, i.e. the lower-level transport.
  13240. + * @ops: Request transport layer operations.
  13241. + *
  13242. + * Initializes the given request transport layer and associated packet
  13243. + * transport layer. Transmitter and receiver threads must be started
  13244. + * separately via ssh_rtl_start(), after the request-layer has been
  13245. + * initialized and the lower-level serial device layer has been set up.
  13246. + *
  13247. + * Return: Returns zero on success and a nonzero error code on failure.
  13248. + */
  13249. +int ssh_rtl_init(struct ssh_rtl *rtl, struct serdev_device *serdev,
  13250. + const struct ssh_rtl_ops *ops)
  13251. +{
  13252. + struct ssh_ptl_ops ptl_ops;
  13253. + int status;
  13254. +
  13255. + ptl_ops.data_received = ssh_rtl_rx_data;
  13256. +
  13257. + status = ssh_ptl_init(&rtl->ptl, serdev, &ptl_ops);
  13258. + if (status)
  13259. + return status;
  13260. +
  13261. + spin_lock_init(&rtl->queue.lock);
  13262. + INIT_LIST_HEAD(&rtl->queue.head);
  13263. +
  13264. + spin_lock_init(&rtl->pending.lock);
  13265. + INIT_LIST_HEAD(&rtl->pending.head);
  13266. + atomic_set_release(&rtl->pending.count, 0);
  13267. +
  13268. + INIT_WORK(&rtl->tx.work, ssh_rtl_tx_work_fn);
  13269. +
  13270. + spin_lock_init(&rtl->rtx_timeout.lock);
  13271. + rtl->rtx_timeout.timeout = SSH_RTL_REQUEST_TIMEOUT;
  13272. + rtl->rtx_timeout.expires = KTIME_MAX;
  13273. + INIT_DELAYED_WORK(&rtl->rtx_timeout.reaper, ssh_rtl_timeout_reap);
  13274. +
  13275. + rtl->ops = *ops;
  13276. +
  13277. + return 0;
  13278. +}
  13279. +
  13280. +/**
  13281. + * ssh_rtl_destroy() - Deinitialize request transport layer.
  13282. + * @rtl: The request transport layer to deinitialize.
  13283. + *
  13284. + * Deinitializes the given request transport layer and frees resources
  13285. + * associated with it. If receiver and/or transmitter threads have been
  13286. + * started, the layer must first be shut down via ssh_rtl_shutdown() before
  13287. + * this function can be called.
  13288. + */
  13289. +void ssh_rtl_destroy(struct ssh_rtl *rtl)
  13290. +{
  13291. + ssh_ptl_destroy(&rtl->ptl);
  13292. +}
  13293. +
  13294. +/**
  13295. + * ssh_rtl_start() - Start request transmitter and receiver.
  13296. + * @rtl: The request transport layer.
  13297. + *
  13298. + * Return: Returns zero on success, a negative error code on failure.
  13299. + */
  13300. +int ssh_rtl_start(struct ssh_rtl *rtl)
  13301. +{
  13302. + int status;
  13303. +
  13304. + status = ssh_ptl_tx_start(&rtl->ptl);
  13305. + if (status)
  13306. + return status;
  13307. +
  13308. + ssh_rtl_tx_schedule(rtl);
  13309. +
  13310. + status = ssh_ptl_rx_start(&rtl->ptl);
  13311. + if (status) {
  13312. + ssh_rtl_flush(rtl, msecs_to_jiffies(5000));
  13313. + ssh_ptl_tx_stop(&rtl->ptl);
  13314. + return status;
  13315. + }
  13316. +
  13317. + return 0;
  13318. +}
  13319. +
  13320. +struct ssh_flush_request {
  13321. + struct ssh_request base;
  13322. + struct completion completion;
  13323. + int status;
  13324. +};
  13325. +
  13326. +static void ssh_rtl_flush_request_complete(struct ssh_request *r,
  13327. + const struct ssh_command *cmd,
  13328. + const struct ssam_span *data,
  13329. + int status)
  13330. +{
  13331. + struct ssh_flush_request *rqst;
  13332. +
  13333. + rqst = container_of(r, struct ssh_flush_request, base);
  13334. + rqst->status = status;
  13335. +}
  13336. +
  13337. +static void ssh_rtl_flush_request_release(struct ssh_request *r)
  13338. +{
  13339. + struct ssh_flush_request *rqst;
  13340. +
  13341. + rqst = container_of(r, struct ssh_flush_request, base);
  13342. + complete_all(&rqst->completion);
  13343. +}
  13344. +
  13345. +static const struct ssh_request_ops ssh_rtl_flush_request_ops = {
  13346. + .complete = ssh_rtl_flush_request_complete,
  13347. + .release = ssh_rtl_flush_request_release,
  13348. +};
  13349. +
  13350. +/**
  13351. + * ssh_rtl_flush() - Flush the request transport layer.
  13352. + * @rtl: request transport layer
  13353. + * @timeout: timeout for the flush operation in jiffies
  13354. + *
  13355. + * Queue a special flush request and wait for its completion. This request
  13356. + * will be completed after all other currently queued and pending requests
  13357. + * have been completed. Instead of a normal data packet, this request submits
  13358. + * a special flush packet, meaning that upon completion, also the underlying
  13359. + * packet transport layer has been flushed.
  13360. + *
  13361. + * Flushing the request layer guarantees that all previously submitted
  13362. + * requests have been fully completed before this call returns. Additionally,
  13363. + * flushing blocks execution of all later submitted requests until the flush
  13364. + * has been completed.
  13365. + *
  13366. + * If the caller ensures that no new requests are submitted after a call to
  13367. + * this function, the request transport layer is guaranteed to have no
  13368. + * remaining requests when this call returns. The same guarantee does not hold
  13369. + * for the packet layer, on which control packets may still be queued after
  13370. + * this call.
  13371. + *
  13372. + * Return: Returns zero on success, %-ETIMEDOUT if the flush timed out and has
  13373. + * been canceled as a result of the timeout, or %-ESHUTDOWN if the packet
  13374. + * and/or request transport layer has been shut down before this call. May
  13375. + * also return %-EINTR if the underlying packet transmission has been
  13376. + * interrupted.
  13377. + */
  13378. +int ssh_rtl_flush(struct ssh_rtl *rtl, unsigned long timeout)
  13379. +{
  13380. + const unsigned int init_flags = SSAM_REQUEST_UNSEQUENCED;
  13381. + struct ssh_flush_request rqst;
  13382. + int status;
  13383. +
  13384. + ssh_request_init(&rqst.base, init_flags, &ssh_rtl_flush_request_ops);
  13385. + rqst.base.packet.state |= BIT(SSH_PACKET_TY_FLUSH_BIT);
  13386. + rqst.base.packet.priority = SSH_PACKET_PRIORITY(FLUSH, 0);
  13387. + rqst.base.state |= BIT(SSH_REQUEST_TY_FLUSH_BIT);
  13388. +
  13389. + init_completion(&rqst.completion);
  13390. +
  13391. + status = ssh_rtl_submit(rtl, &rqst.base);
  13392. + if (status)
  13393. + return status;
  13394. +
  13395. + ssh_request_put(&rqst.base);
  13396. +
  13397. + if (!wait_for_completion_timeout(&rqst.completion, timeout)) {
  13398. + ssh_rtl_cancel(&rqst.base, true);
  13399. + wait_for_completion(&rqst.completion);
  13400. + }
  13401. +
  13402. + WARN_ON(rqst.status != 0 && rqst.status != -ECANCELED &&
  13403. + rqst.status != -ESHUTDOWN && rqst.status != -EINTR);
  13404. +
  13405. + return rqst.status == -ECANCELED ? -ETIMEDOUT : rqst.status;
  13406. +}
  13407. +
  13408. +/**
  13409. + * ssh_rtl_shutdown() - Shut down request transport layer.
  13410. + * @rtl: The request transport layer.
  13411. + *
  13412. + * Shuts down the request transport layer, removing and canceling all queued
  13413. + * and pending requests. Requests canceled by this operation will be completed
  13414. + * with %-ESHUTDOWN as status. Receiver and transmitter threads will be
  13415. + * stopped, the lower-level packet layer will be shutdown.
  13416. + *
  13417. + * As a result of this function, the transport layer will be marked as shut
  13418. + * down. Submission of requests after the transport layer has been shut down
  13419. + * will fail with %-ESHUTDOWN.
  13420. + */
  13421. +void ssh_rtl_shutdown(struct ssh_rtl *rtl)
  13422. +{
  13423. + struct ssh_request *r, *n;
  13424. + LIST_HEAD(claimed);
  13425. + int pending;
  13426. +
  13427. + set_bit(SSH_RTL_SF_SHUTDOWN_BIT, &rtl->state);
  13428. + /*
  13429. + * Ensure that the layer gets marked as shut-down before actually
  13430. + * stopping it. In combination with the check in ssh_rtl_submit(),
  13431. + * this guarantees that no new requests can be added and all already
  13432. + * queued requests are properly canceled.
  13433. + */
  13434. + smp_mb__after_atomic();
  13435. +
  13436. + /* Remove requests from queue. */
  13437. + spin_lock(&rtl->queue.lock);
  13438. + list_for_each_entry_safe(r, n, &rtl->queue.head, node) {
  13439. + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
  13440. + /* Ensure state never gets zero. */
  13441. + smp_mb__before_atomic();
  13442. + clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &r->state);
  13443. +
  13444. + list_del(&r->node);
  13445. + list_add_tail(&r->node, &claimed);
  13446. + }
  13447. + spin_unlock(&rtl->queue.lock);
  13448. +
  13449. + /*
  13450. + * We have now guaranteed that the queue is empty and no more new
  13451. + * requests can be submitted (i.e. it will stay empty). This means that
  13452. + * calling ssh_rtl_tx_schedule() will not schedule tx.work any more. So
  13453. + * we can simply call cancel_work_sync() on tx.work here and when that
  13454. + * returns, we've locked it down. This also means that after this call,
  13455. + * we don't submit any more packets to the underlying packet layer, so
  13456. + * we can also shut that down.
  13457. + */
  13458. +
  13459. + cancel_work_sync(&rtl->tx.work);
  13460. + ssh_ptl_shutdown(&rtl->ptl);
  13461. + cancel_delayed_work_sync(&rtl->rtx_timeout.reaper);
  13462. +
  13463. + /*
  13464. + * Shutting down the packet layer should also have canceled all
  13465. + * requests. Thus the pending set should be empty. Attempt to handle
  13466. + * this gracefully anyways, even though this should be dead code.
  13467. + */
  13468. +
  13469. + pending = atomic_read(&rtl->pending.count);
  13470. + if (WARN_ON(pending)) {
  13471. + spin_lock(&rtl->pending.lock);
  13472. + list_for_each_entry_safe(r, n, &rtl->pending.head, node) {
  13473. + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
  13474. + /* Ensure state never gets zero. */
  13475. + smp_mb__before_atomic();
  13476. + clear_bit(SSH_REQUEST_SF_PENDING_BIT, &r->state);
  13477. +
  13478. + list_del(&r->node);
  13479. + list_add_tail(&r->node, &claimed);
  13480. + }
  13481. + spin_unlock(&rtl->pending.lock);
  13482. + }
  13483. +
  13484. + /* Finally, cancel and complete the requests we claimed before. */
  13485. + list_for_each_entry_safe(r, n, &claimed, node) {
  13486. + /*
  13487. + * We need test_and_set() because we still might compete with
  13488. + * cancellation.
  13489. + */
  13490. + if (!test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  13491. + ssh_rtl_complete_with_status(r, -ESHUTDOWN);
  13492. +
  13493. + /*
  13494. + * Drop the reference we've obtained by removing it from the
  13495. + * lists.
  13496. + */
  13497. + list_del(&r->node);
  13498. + ssh_request_put(r);
  13499. + }
  13500. +}
  13501. diff --git a/drivers/platform/x86/surface_aggregator/ssh_request_layer.h b/drivers/platform/x86/surface_aggregator/ssh_request_layer.h
  13502. new file mode 100644
  13503. index 000000000000..9c3cbae2d4bd
  13504. --- /dev/null
  13505. +++ b/drivers/platform/x86/surface_aggregator/ssh_request_layer.h
  13506. @@ -0,0 +1,143 @@
  13507. +/* SPDX-License-Identifier: GPL-2.0+ */
  13508. +/*
  13509. + * SSH request transport layer.
  13510. + *
  13511. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  13512. + */
  13513. +
  13514. +#ifndef _SURFACE_AGGREGATOR_SSH_REQUEST_LAYER_H
  13515. +#define _SURFACE_AGGREGATOR_SSH_REQUEST_LAYER_H
  13516. +
  13517. +#include <linux/atomic.h>
  13518. +#include <linux/ktime.h>
  13519. +#include <linux/list.h>
  13520. +#include <linux/spinlock.h>
  13521. +#include <linux/workqueue.h>
  13522. +
  13523. +#include <linux/surface_aggregator/serial_hub.h>
  13524. +#include <linux/surface_aggregator/controller.h>
  13525. +
  13526. +#include "ssh_packet_layer.h"
  13527. +
  13528. +/**
  13529. + * enum ssh_rtl_state_flags - State-flags for &struct ssh_rtl.
  13530. + *
  13531. + * @SSH_RTL_SF_SHUTDOWN_BIT:
  13532. + * Indicates that the request transport layer has been shut down or is
  13533. + * being shut down and should not accept any new requests.
  13534. + */
  13535. +enum ssh_rtl_state_flags {
  13536. + SSH_RTL_SF_SHUTDOWN_BIT,
  13537. +};
  13538. +
  13539. +/**
  13540. + * struct ssh_rtl_ops - Callback operations for request transport layer.
  13541. + * @handle_event: Function called when a SSH event has been received. The
  13542. + * specified function takes the request layer, received command
  13543. + * struct, and corresponding payload as arguments. If the event
  13544. + * has no payload, the payload span is empty (not %NULL).
  13545. + */
  13546. +struct ssh_rtl_ops {
  13547. + void (*handle_event)(struct ssh_rtl *rtl, const struct ssh_command *cmd,
  13548. + const struct ssam_span *data);
  13549. +};
  13550. +
  13551. +/**
  13552. + * struct ssh_rtl - SSH request transport layer.
  13553. + * @ptl: Underlying packet transport layer.
  13554. + * @state: State(-flags) of the transport layer.
  13555. + * @queue: Request submission queue.
  13556. + * @queue.lock: Lock for modifying the request submission queue.
  13557. + * @queue.head: List-head of the request submission queue.
  13558. + * @pending: Set/list of pending requests.
  13559. + * @pending.lock: Lock for modifying the request set.
  13560. + * @pending.head: List-head of the pending set/list.
  13561. + * @pending.count: Number of currently pending requests.
  13562. + * @tx: Transmitter subsystem.
  13563. + * @tx.work: Transmitter work item.
  13564. + * @rtx_timeout: Retransmission timeout subsystem.
  13565. + * @rtx_timeout.lock: Lock for modifying the retransmission timeout reaper.
  13566. + * @rtx_timeout.timeout: Timeout interval for retransmission.
  13567. + * @rtx_timeout.expires: Time specifying when the reaper work is next scheduled.
  13568. + * @rtx_timeout.reaper: Work performing timeout checks and subsequent actions.
  13569. + * @ops: Request layer operations.
  13570. + */
  13571. +struct ssh_rtl {
  13572. + struct ssh_ptl ptl;
  13573. + unsigned long state;
  13574. +
  13575. + struct {
  13576. + spinlock_t lock;
  13577. + struct list_head head;
  13578. + } queue;
  13579. +
  13580. + struct {
  13581. + spinlock_t lock;
  13582. + struct list_head head;
  13583. + atomic_t count;
  13584. + } pending;
  13585. +
  13586. + struct {
  13587. + struct work_struct work;
  13588. + } tx;
  13589. +
  13590. + struct {
  13591. + spinlock_t lock;
  13592. + ktime_t timeout;
  13593. + ktime_t expires;
  13594. + struct delayed_work reaper;
  13595. + } rtx_timeout;
  13596. +
  13597. + struct ssh_rtl_ops ops;
  13598. +};
  13599. +
  13600. +#define rtl_dbg(r, fmt, ...) ptl_dbg(&(r)->ptl, fmt, ##__VA_ARGS__)
  13601. +#define rtl_info(p, fmt, ...) ptl_info(&(p)->ptl, fmt, ##__VA_ARGS__)
  13602. +#define rtl_warn(r, fmt, ...) ptl_warn(&(r)->ptl, fmt, ##__VA_ARGS__)
  13603. +#define rtl_err(r, fmt, ...) ptl_err(&(r)->ptl, fmt, ##__VA_ARGS__)
  13604. +#define rtl_dbg_cond(r, fmt, ...) __ssam_prcond(rtl_dbg, r, fmt, ##__VA_ARGS__)
  13605. +
  13606. +#define to_ssh_rtl(ptr, member) \
  13607. + container_of(ptr, struct ssh_rtl, member)
  13608. +
  13609. +/**
  13610. + * ssh_rtl_get_device() - Get device associated with request transport layer.
  13611. + * @rtl: The request transport layer.
  13612. + *
  13613. + * Return: Returns the device on which the given request transport layer
  13614. + * builds upon.
  13615. + */
  13616. +static inline struct device *ssh_rtl_get_device(struct ssh_rtl *rtl)
  13617. +{
  13618. + return ssh_ptl_get_device(&rtl->ptl);
  13619. +}
  13620. +
  13621. +/**
  13622. + * ssh_request_rtl() - Get request transport layer associated with request.
  13623. + * @rqst: The request to get the request transport layer reference for.
  13624. + *
  13625. + * Return: Returns the &struct ssh_rtl associated with the given SSH request.
  13626. + */
  13627. +static inline struct ssh_rtl *ssh_request_rtl(struct ssh_request *rqst)
  13628. +{
  13629. + struct ssh_ptl *ptl;
  13630. +
  13631. + ptl = READ_ONCE(rqst->packet.ptl);
  13632. + return likely(ptl) ? to_ssh_rtl(ptl, ptl) : NULL;
  13633. +}
  13634. +
  13635. +int ssh_rtl_submit(struct ssh_rtl *rtl, struct ssh_request *rqst);
  13636. +bool ssh_rtl_cancel(struct ssh_request *rqst, bool pending);
  13637. +
  13638. +int ssh_rtl_init(struct ssh_rtl *rtl, struct serdev_device *serdev,
  13639. + const struct ssh_rtl_ops *ops);
  13640. +
  13641. +int ssh_rtl_start(struct ssh_rtl *rtl);
  13642. +int ssh_rtl_flush(struct ssh_rtl *rtl, unsigned long timeout);
  13643. +void ssh_rtl_shutdown(struct ssh_rtl *rtl);
  13644. +void ssh_rtl_destroy(struct ssh_rtl *rtl);
  13645. +
  13646. +int ssh_request_init(struct ssh_request *rqst, enum ssam_request_flags flags,
  13647. + const struct ssh_request_ops *ops);
  13648. +
  13649. +#endif /* _SURFACE_AGGREGATOR_SSH_REQUEST_LAYER_H */
  13650. diff --git a/drivers/platform/x86/surface_aggregator/trace.h b/drivers/platform/x86/surface_aggregator/trace.h
  13651. new file mode 100644
  13652. index 000000000000..de64cf169060
  13653. --- /dev/null
  13654. +++ b/drivers/platform/x86/surface_aggregator/trace.h
  13655. @@ -0,0 +1,632 @@
  13656. +/* SPDX-License-Identifier: GPL-2.0+ */
  13657. +/*
  13658. + * Trace points for SSAM/SSH.
  13659. + *
  13660. + * Copyright (C) 2020-2021 Maximilian Luz <luzmaximilian@gmail.com>
  13661. + */
  13662. +
  13663. +#undef TRACE_SYSTEM
  13664. +#define TRACE_SYSTEM surface_aggregator
  13665. +
  13666. +#if !defined(_SURFACE_AGGREGATOR_TRACE_H) || defined(TRACE_HEADER_MULTI_READ)
  13667. +#define _SURFACE_AGGREGATOR_TRACE_H
  13668. +
  13669. +#include <linux/surface_aggregator/serial_hub.h>
  13670. +
  13671. +#include <asm/unaligned.h>
  13672. +#include <linux/tracepoint.h>
  13673. +
  13674. +TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_DATA_SEQ);
  13675. +TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_DATA_NSQ);
  13676. +TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_ACK);
  13677. +TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_NAK);
  13678. +
  13679. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_LOCKED_BIT);
  13680. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_QUEUED_BIT);
  13681. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_PENDING_BIT);
  13682. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_TRANSMITTING_BIT);
  13683. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_TRANSMITTED_BIT);
  13684. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_ACKED_BIT);
  13685. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_CANCELED_BIT);
  13686. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_COMPLETED_BIT);
  13687. +
  13688. +TRACE_DEFINE_ENUM(SSH_PACKET_TY_FLUSH_BIT);
  13689. +TRACE_DEFINE_ENUM(SSH_PACKET_TY_SEQUENCED_BIT);
  13690. +TRACE_DEFINE_ENUM(SSH_PACKET_TY_BLOCKING_BIT);
  13691. +
  13692. +TRACE_DEFINE_ENUM(SSH_PACKET_FLAGS_SF_MASK);
  13693. +TRACE_DEFINE_ENUM(SSH_PACKET_FLAGS_TY_MASK);
  13694. +
  13695. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_LOCKED_BIT);
  13696. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_QUEUED_BIT);
  13697. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_PENDING_BIT);
  13698. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_TRANSMITTING_BIT);
  13699. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_TRANSMITTED_BIT);
  13700. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_RSPRCVD_BIT);
  13701. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_CANCELED_BIT);
  13702. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_COMPLETED_BIT);
  13703. +
  13704. +TRACE_DEFINE_ENUM(SSH_REQUEST_TY_FLUSH_BIT);
  13705. +TRACE_DEFINE_ENUM(SSH_REQUEST_TY_HAS_RESPONSE_BIT);
  13706. +
  13707. +TRACE_DEFINE_ENUM(SSH_REQUEST_FLAGS_SF_MASK);
  13708. +TRACE_DEFINE_ENUM(SSH_REQUEST_FLAGS_TY_MASK);
  13709. +
  13710. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SAM);
  13711. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_BAT);
  13712. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_TMP);
  13713. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_PMC);
  13714. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_FAN);
  13715. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_PoM);
  13716. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_DBG);
  13717. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_KBD);
  13718. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_FWU);
  13719. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_UNI);
  13720. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_LPC);
  13721. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_TCL);
  13722. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SFL);
  13723. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_KIP);
  13724. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_EXT);
  13725. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_BLD);
  13726. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_BAS);
  13727. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SEN);
  13728. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SRQ);
  13729. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_MCU);
  13730. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_HID);
  13731. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_TCH);
  13732. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_BKL);
  13733. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_TAM);
  13734. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_ACC);
  13735. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_UFI);
  13736. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_USC);
  13737. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_PEN);
  13738. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_VID);
  13739. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_AUD);
  13740. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SMC);
  13741. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_KPD);
  13742. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_REG);
  13743. +
  13744. +#define SSAM_PTR_UID_LEN 9
  13745. +#define SSAM_U8_FIELD_NOT_APPLICABLE ((u16)-1)
  13746. +#define SSAM_SEQ_NOT_APPLICABLE ((u16)-1)
  13747. +#define SSAM_RQID_NOT_APPLICABLE ((u32)-1)
  13748. +#define SSAM_SSH_TC_NOT_APPLICABLE 0
  13749. +
  13750. +#ifndef _SURFACE_AGGREGATOR_TRACE_HELPERS
  13751. +#define _SURFACE_AGGREGATOR_TRACE_HELPERS
  13752. +
  13753. +/**
  13754. + * ssam_trace_ptr_uid() - Convert the pointer to a non-pointer UID string.
  13755. + * @ptr: The pointer to convert.
  13756. + * @uid_str: A buffer of length SSAM_PTR_UID_LEN where the UID will be stored.
  13757. + *
  13758. + * Converts the given pointer into a UID string that is safe to be shared
  13759. + * with userspace and logs, i.e. doesn't give away the real memory location.
  13760. + */
  13761. +static inline void ssam_trace_ptr_uid(const void *ptr, char *uid_str)
  13762. +{
  13763. + char buf[2 * sizeof(void *) + 1];
  13764. +
  13765. + BUILD_BUG_ON(ARRAY_SIZE(buf) < SSAM_PTR_UID_LEN);
  13766. +
  13767. + snprintf(buf, ARRAY_SIZE(buf), "%p", ptr);
  13768. + memcpy(uid_str, &buf[ARRAY_SIZE(buf) - SSAM_PTR_UID_LEN],
  13769. + SSAM_PTR_UID_LEN);
  13770. +}
  13771. +
  13772. +/**
  13773. + * ssam_trace_get_packet_seq() - Read the packet's sequence ID.
  13774. + * @p: The packet.
  13775. + *
  13776. + * Return: Returns the packet's sequence ID (SEQ) field if present, or
  13777. + * %SSAM_SEQ_NOT_APPLICABLE if not (e.g. flush packet).
  13778. + */
  13779. +static inline u16 ssam_trace_get_packet_seq(const struct ssh_packet *p)
  13780. +{
  13781. + if (!p->data.ptr || p->data.len < SSH_MESSAGE_LENGTH(0))
  13782. + return SSAM_SEQ_NOT_APPLICABLE;
  13783. +
  13784. + return p->data.ptr[SSH_MSGOFFSET_FRAME(seq)];
  13785. +}
  13786. +
  13787. +/**
  13788. + * ssam_trace_get_request_id() - Read the packet's request ID.
  13789. + * @p: The packet.
  13790. + *
  13791. + * Return: Returns the packet's request ID (RQID) field if the packet
  13792. + * represents a request with command data, or %SSAM_RQID_NOT_APPLICABLE if not
  13793. + * (e.g. flush request, control packet).
  13794. + */
  13795. +static inline u32 ssam_trace_get_request_id(const struct ssh_packet *p)
  13796. +{
  13797. + if (!p->data.ptr || p->data.len < SSH_COMMAND_MESSAGE_LENGTH(0))
  13798. + return SSAM_RQID_NOT_APPLICABLE;
  13799. +
  13800. + return get_unaligned_le16(&p->data.ptr[SSH_MSGOFFSET_COMMAND(rqid)]);
  13801. +}
  13802. +
  13803. +/**
  13804. + * ssam_trace_get_request_tc() - Read the packet's request target category.
  13805. + * @p: The packet.
  13806. + *
  13807. + * Return: Returns the packet's request target category (TC) field if the
  13808. + * packet represents a request with command data, or %SSAM_TC_NOT_APPLICABLE
  13809. + * if not (e.g. flush request, control packet).
  13810. + */
  13811. +static inline u32 ssam_trace_get_request_tc(const struct ssh_packet *p)
  13812. +{
  13813. + if (!p->data.ptr || p->data.len < SSH_COMMAND_MESSAGE_LENGTH(0))
  13814. + return SSAM_SSH_TC_NOT_APPLICABLE;
  13815. +
  13816. + return get_unaligned_le16(&p->data.ptr[SSH_MSGOFFSET_COMMAND(tc)]);
  13817. +}
  13818. +
  13819. +#endif /* _SURFACE_AGGREGATOR_TRACE_HELPERS */
  13820. +
  13821. +#define ssam_trace_get_command_field_u8(packet, field) \
  13822. + ((!(packet) || (packet)->data.len < SSH_COMMAND_MESSAGE_LENGTH(0)) \
  13823. + ? 0 : (packet)->data.ptr[SSH_MSGOFFSET_COMMAND(field)])
  13824. +
  13825. +#define ssam_show_generic_u8_field(value) \
  13826. + __print_symbolic(value, \
  13827. + { SSAM_U8_FIELD_NOT_APPLICABLE, "N/A" } \
  13828. + )
  13829. +
  13830. +#define ssam_show_frame_type(ty) \
  13831. + __print_symbolic(ty, \
  13832. + { SSH_FRAME_TYPE_DATA_SEQ, "DSEQ" }, \
  13833. + { SSH_FRAME_TYPE_DATA_NSQ, "DNSQ" }, \
  13834. + { SSH_FRAME_TYPE_ACK, "ACK" }, \
  13835. + { SSH_FRAME_TYPE_NAK, "NAK" } \
  13836. + )
  13837. +
  13838. +#define ssam_show_packet_type(type) \
  13839. + __print_flags(flags & SSH_PACKET_FLAGS_TY_MASK, "", \
  13840. + { BIT(SSH_PACKET_TY_FLUSH_BIT), "F" }, \
  13841. + { BIT(SSH_PACKET_TY_SEQUENCED_BIT), "S" }, \
  13842. + { BIT(SSH_PACKET_TY_BLOCKING_BIT), "B" } \
  13843. + )
  13844. +
  13845. +#define ssam_show_packet_state(state) \
  13846. + __print_flags(flags & SSH_PACKET_FLAGS_SF_MASK, "", \
  13847. + { BIT(SSH_PACKET_SF_LOCKED_BIT), "L" }, \
  13848. + { BIT(SSH_PACKET_SF_QUEUED_BIT), "Q" }, \
  13849. + { BIT(SSH_PACKET_SF_PENDING_BIT), "P" }, \
  13850. + { BIT(SSH_PACKET_SF_TRANSMITTING_BIT), "S" }, \
  13851. + { BIT(SSH_PACKET_SF_TRANSMITTED_BIT), "T" }, \
  13852. + { BIT(SSH_PACKET_SF_ACKED_BIT), "A" }, \
  13853. + { BIT(SSH_PACKET_SF_CANCELED_BIT), "C" }, \
  13854. + { BIT(SSH_PACKET_SF_COMPLETED_BIT), "F" } \
  13855. + )
  13856. +
  13857. +#define ssam_show_packet_seq(seq) \
  13858. + __print_symbolic(seq, \
  13859. + { SSAM_SEQ_NOT_APPLICABLE, "N/A" } \
  13860. + )
  13861. +
  13862. +#define ssam_show_request_type(flags) \
  13863. + __print_flags((flags) & SSH_REQUEST_FLAGS_TY_MASK, "", \
  13864. + { BIT(SSH_REQUEST_TY_FLUSH_BIT), "F" }, \
  13865. + { BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT), "R" } \
  13866. + )
  13867. +
  13868. +#define ssam_show_request_state(flags) \
  13869. + __print_flags((flags) & SSH_REQUEST_FLAGS_SF_MASK, "", \
  13870. + { BIT(SSH_REQUEST_SF_LOCKED_BIT), "L" }, \
  13871. + { BIT(SSH_REQUEST_SF_QUEUED_BIT), "Q" }, \
  13872. + { BIT(SSH_REQUEST_SF_PENDING_BIT), "P" }, \
  13873. + { BIT(SSH_REQUEST_SF_TRANSMITTING_BIT), "S" }, \
  13874. + { BIT(SSH_REQUEST_SF_TRANSMITTED_BIT), "T" }, \
  13875. + { BIT(SSH_REQUEST_SF_RSPRCVD_BIT), "A" }, \
  13876. + { BIT(SSH_REQUEST_SF_CANCELED_BIT), "C" }, \
  13877. + { BIT(SSH_REQUEST_SF_COMPLETED_BIT), "F" } \
  13878. + )
  13879. +
  13880. +#define ssam_show_request_id(rqid) \
  13881. + __print_symbolic(rqid, \
  13882. + { SSAM_RQID_NOT_APPLICABLE, "N/A" } \
  13883. + )
  13884. +
  13885. +#define ssam_show_ssh_tc(rqid) \
  13886. + __print_symbolic(rqid, \
  13887. + { SSAM_SSH_TC_NOT_APPLICABLE, "N/A" }, \
  13888. + { SSAM_SSH_TC_SAM, "SAM" }, \
  13889. + { SSAM_SSH_TC_BAT, "BAT" }, \
  13890. + { SSAM_SSH_TC_TMP, "TMP" }, \
  13891. + { SSAM_SSH_TC_PMC, "PMC" }, \
  13892. + { SSAM_SSH_TC_FAN, "FAN" }, \
  13893. + { SSAM_SSH_TC_PoM, "PoM" }, \
  13894. + { SSAM_SSH_TC_DBG, "DBG" }, \
  13895. + { SSAM_SSH_TC_KBD, "KBD" }, \
  13896. + { SSAM_SSH_TC_FWU, "FWU" }, \
  13897. + { SSAM_SSH_TC_UNI, "UNI" }, \
  13898. + { SSAM_SSH_TC_LPC, "LPC" }, \
  13899. + { SSAM_SSH_TC_TCL, "TCL" }, \
  13900. + { SSAM_SSH_TC_SFL, "SFL" }, \
  13901. + { SSAM_SSH_TC_KIP, "KIP" }, \
  13902. + { SSAM_SSH_TC_EXT, "EXT" }, \
  13903. + { SSAM_SSH_TC_BLD, "BLD" }, \
  13904. + { SSAM_SSH_TC_BAS, "BAS" }, \
  13905. + { SSAM_SSH_TC_SEN, "SEN" }, \
  13906. + { SSAM_SSH_TC_SRQ, "SRQ" }, \
  13907. + { SSAM_SSH_TC_MCU, "MCU" }, \
  13908. + { SSAM_SSH_TC_HID, "HID" }, \
  13909. + { SSAM_SSH_TC_TCH, "TCH" }, \
  13910. + { SSAM_SSH_TC_BKL, "BKL" }, \
  13911. + { SSAM_SSH_TC_TAM, "TAM" }, \
  13912. + { SSAM_SSH_TC_ACC, "ACC" }, \
  13913. + { SSAM_SSH_TC_UFI, "UFI" }, \
  13914. + { SSAM_SSH_TC_USC, "USC" }, \
  13915. + { SSAM_SSH_TC_PEN, "PEN" }, \
  13916. + { SSAM_SSH_TC_VID, "VID" }, \
  13917. + { SSAM_SSH_TC_AUD, "AUD" }, \
  13918. + { SSAM_SSH_TC_SMC, "SMC" }, \
  13919. + { SSAM_SSH_TC_KPD, "KPD" }, \
  13920. + { SSAM_SSH_TC_REG, "REG" } \
  13921. + )
  13922. +
  13923. +DECLARE_EVENT_CLASS(ssam_frame_class,
  13924. + TP_PROTO(const struct ssh_frame *frame),
  13925. +
  13926. + TP_ARGS(frame),
  13927. +
  13928. + TP_STRUCT__entry(
  13929. + __field(u8, type)
  13930. + __field(u8, seq)
  13931. + __field(u16, len)
  13932. + ),
  13933. +
  13934. + TP_fast_assign(
  13935. + __entry->type = frame->type;
  13936. + __entry->seq = frame->seq;
  13937. + __entry->len = get_unaligned_le16(&frame->len);
  13938. + ),
  13939. +
  13940. + TP_printk("ty=%s, seq=%#04x, len=%u",
  13941. + ssam_show_frame_type(__entry->type),
  13942. + __entry->seq,
  13943. + __entry->len
  13944. + )
  13945. +);
  13946. +
  13947. +#define DEFINE_SSAM_FRAME_EVENT(name) \
  13948. + DEFINE_EVENT(ssam_frame_class, ssam_##name, \
  13949. + TP_PROTO(const struct ssh_frame *frame), \
  13950. + TP_ARGS(frame) \
  13951. + )
  13952. +
  13953. +DECLARE_EVENT_CLASS(ssam_command_class,
  13954. + TP_PROTO(const struct ssh_command *cmd, u16 len),
  13955. +
  13956. + TP_ARGS(cmd, len),
  13957. +
  13958. + TP_STRUCT__entry(
  13959. + __field(u16, rqid)
  13960. + __field(u16, len)
  13961. + __field(u8, tc)
  13962. + __field(u8, cid)
  13963. + __field(u8, iid)
  13964. + ),
  13965. +
  13966. + TP_fast_assign(
  13967. + __entry->rqid = get_unaligned_le16(&cmd->rqid);
  13968. + __entry->tc = cmd->tc;
  13969. + __entry->cid = cmd->cid;
  13970. + __entry->iid = cmd->iid;
  13971. + __entry->len = len;
  13972. + ),
  13973. +
  13974. + TP_printk("rqid=%#06x, tc=%s, cid=%#04x, iid=%#04x, len=%u",
  13975. + __entry->rqid,
  13976. + ssam_show_ssh_tc(__entry->tc),
  13977. + __entry->cid,
  13978. + __entry->iid,
  13979. + __entry->len
  13980. + )
  13981. +);
  13982. +
  13983. +#define DEFINE_SSAM_COMMAND_EVENT(name) \
  13984. + DEFINE_EVENT(ssam_command_class, ssam_##name, \
  13985. + TP_PROTO(const struct ssh_command *cmd, u16 len), \
  13986. + TP_ARGS(cmd, len) \
  13987. + )
  13988. +
  13989. +DECLARE_EVENT_CLASS(ssam_packet_class,
  13990. + TP_PROTO(const struct ssh_packet *packet),
  13991. +
  13992. + TP_ARGS(packet),
  13993. +
  13994. + TP_STRUCT__entry(
  13995. + __field(unsigned long, state)
  13996. + __array(char, uid, SSAM_PTR_UID_LEN)
  13997. + __field(u8, priority)
  13998. + __field(u16, length)
  13999. + __field(u16, seq)
  14000. + ),
  14001. +
  14002. + TP_fast_assign(
  14003. + __entry->state = READ_ONCE(packet->state);
  14004. + ssam_trace_ptr_uid(packet, __entry->uid);
  14005. + __entry->priority = READ_ONCE(packet->priority);
  14006. + __entry->length = packet->data.len;
  14007. + __entry->seq = ssam_trace_get_packet_seq(packet);
  14008. + ),
  14009. +
  14010. + TP_printk("uid=%s, seq=%s, ty=%s, pri=%#04x, len=%u, sta=%s",
  14011. + __entry->uid,
  14012. + ssam_show_packet_seq(__entry->seq),
  14013. + ssam_show_packet_type(__entry->state),
  14014. + __entry->priority,
  14015. + __entry->length,
  14016. + ssam_show_packet_state(__entry->state)
  14017. + )
  14018. +);
  14019. +
  14020. +#define DEFINE_SSAM_PACKET_EVENT(name) \
  14021. + DEFINE_EVENT(ssam_packet_class, ssam_##name, \
  14022. + TP_PROTO(const struct ssh_packet *packet), \
  14023. + TP_ARGS(packet) \
  14024. + )
  14025. +
  14026. +DECLARE_EVENT_CLASS(ssam_packet_status_class,
  14027. + TP_PROTO(const struct ssh_packet *packet, int status),
  14028. +
  14029. + TP_ARGS(packet, status),
  14030. +
  14031. + TP_STRUCT__entry(
  14032. + __field(unsigned long, state)
  14033. + __field(int, status)
  14034. + __array(char, uid, SSAM_PTR_UID_LEN)
  14035. + __field(u8, priority)
  14036. + __field(u16, length)
  14037. + __field(u16, seq)
  14038. + ),
  14039. +
  14040. + TP_fast_assign(
  14041. + __entry->state = READ_ONCE(packet->state);
  14042. + __entry->status = status;
  14043. + ssam_trace_ptr_uid(packet, __entry->uid);
  14044. + __entry->priority = READ_ONCE(packet->priority);
  14045. + __entry->length = packet->data.len;
  14046. + __entry->seq = ssam_trace_get_packet_seq(packet);
  14047. + ),
  14048. +
  14049. + TP_printk("uid=%s, seq=%s, ty=%s, pri=%#04x, len=%u, sta=%s, status=%d",
  14050. + __entry->uid,
  14051. + ssam_show_packet_seq(__entry->seq),
  14052. + ssam_show_packet_type(__entry->state),
  14053. + __entry->priority,
  14054. + __entry->length,
  14055. + ssam_show_packet_state(__entry->state),
  14056. + __entry->status
  14057. + )
  14058. +);
  14059. +
  14060. +#define DEFINE_SSAM_PACKET_STATUS_EVENT(name) \
  14061. + DEFINE_EVENT(ssam_packet_status_class, ssam_##name, \
  14062. + TP_PROTO(const struct ssh_packet *packet, int status), \
  14063. + TP_ARGS(packet, status) \
  14064. + )
  14065. +
  14066. +DECLARE_EVENT_CLASS(ssam_request_class,
  14067. + TP_PROTO(const struct ssh_request *request),
  14068. +
  14069. + TP_ARGS(request),
  14070. +
  14071. + TP_STRUCT__entry(
  14072. + __field(unsigned long, state)
  14073. + __field(u32, rqid)
  14074. + __array(char, uid, SSAM_PTR_UID_LEN)
  14075. + __field(u8, tc)
  14076. + __field(u16, cid)
  14077. + __field(u16, iid)
  14078. + ),
  14079. +
  14080. + TP_fast_assign(
  14081. + const struct ssh_packet *p = &request->packet;
  14082. +
  14083. + /* Use packet for UID so we can match requests to packets. */
  14084. + __entry->state = READ_ONCE(request->state);
  14085. + __entry->rqid = ssam_trace_get_request_id(p);
  14086. + ssam_trace_ptr_uid(p, __entry->uid);
  14087. + __entry->tc = ssam_trace_get_request_tc(p);
  14088. + __entry->cid = ssam_trace_get_command_field_u8(p, cid);
  14089. + __entry->iid = ssam_trace_get_command_field_u8(p, iid);
  14090. + ),
  14091. +
  14092. + TP_printk("uid=%s, rqid=%s, ty=%s, sta=%s, tc=%s, cid=%s, iid=%s",
  14093. + __entry->uid,
  14094. + ssam_show_request_id(__entry->rqid),
  14095. + ssam_show_request_type(__entry->state),
  14096. + ssam_show_request_state(__entry->state),
  14097. + ssam_show_ssh_tc(__entry->tc),
  14098. + ssam_show_generic_u8_field(__entry->cid),
  14099. + ssam_show_generic_u8_field(__entry->iid)
  14100. + )
  14101. +);
  14102. +
  14103. +#define DEFINE_SSAM_REQUEST_EVENT(name) \
  14104. + DEFINE_EVENT(ssam_request_class, ssam_##name, \
  14105. + TP_PROTO(const struct ssh_request *request), \
  14106. + TP_ARGS(request) \
  14107. + )
  14108. +
  14109. +DECLARE_EVENT_CLASS(ssam_request_status_class,
  14110. + TP_PROTO(const struct ssh_request *request, int status),
  14111. +
  14112. + TP_ARGS(request, status),
  14113. +
  14114. + TP_STRUCT__entry(
  14115. + __field(unsigned long, state)
  14116. + __field(u32, rqid)
  14117. + __field(int, status)
  14118. + __array(char, uid, SSAM_PTR_UID_LEN)
  14119. + __field(u8, tc)
  14120. + __field(u16, cid)
  14121. + __field(u16, iid)
  14122. + ),
  14123. +
  14124. + TP_fast_assign(
  14125. + const struct ssh_packet *p = &request->packet;
  14126. +
  14127. + /* Use packet for UID so we can match requests to packets. */
  14128. + __entry->state = READ_ONCE(request->state);
  14129. + __entry->rqid = ssam_trace_get_request_id(p);
  14130. + __entry->status = status;
  14131. + ssam_trace_ptr_uid(p, __entry->uid);
  14132. + __entry->tc = ssam_trace_get_request_tc(p);
  14133. + __entry->cid = ssam_trace_get_command_field_u8(p, cid);
  14134. + __entry->iid = ssam_trace_get_command_field_u8(p, iid);
  14135. + ),
  14136. +
  14137. + TP_printk("uid=%s, rqid=%s, ty=%s, sta=%s, tc=%s, cid=%s, iid=%s, status=%d",
  14138. + __entry->uid,
  14139. + ssam_show_request_id(__entry->rqid),
  14140. + ssam_show_request_type(__entry->state),
  14141. + ssam_show_request_state(__entry->state),
  14142. + ssam_show_ssh_tc(__entry->tc),
  14143. + ssam_show_generic_u8_field(__entry->cid),
  14144. + ssam_show_generic_u8_field(__entry->iid),
  14145. + __entry->status
  14146. + )
  14147. +);
  14148. +
  14149. +#define DEFINE_SSAM_REQUEST_STATUS_EVENT(name) \
  14150. + DEFINE_EVENT(ssam_request_status_class, ssam_##name, \
  14151. + TP_PROTO(const struct ssh_request *request, int status),\
  14152. + TP_ARGS(request, status) \
  14153. + )
  14154. +
  14155. +DECLARE_EVENT_CLASS(ssam_alloc_class,
  14156. + TP_PROTO(void *ptr, size_t len),
  14157. +
  14158. + TP_ARGS(ptr, len),
  14159. +
  14160. + TP_STRUCT__entry(
  14161. + __field(size_t, len)
  14162. + __array(char, uid, SSAM_PTR_UID_LEN)
  14163. + ),
  14164. +
  14165. + TP_fast_assign(
  14166. + __entry->len = len;
  14167. + ssam_trace_ptr_uid(ptr, __entry->uid);
  14168. + ),
  14169. +
  14170. + TP_printk("uid=%s, len=%zu", __entry->uid, __entry->len)
  14171. +);
  14172. +
  14173. +#define DEFINE_SSAM_ALLOC_EVENT(name) \
  14174. + DEFINE_EVENT(ssam_alloc_class, ssam_##name, \
  14175. + TP_PROTO(void *ptr, size_t len), \
  14176. + TP_ARGS(ptr, len) \
  14177. + )
  14178. +
  14179. +DECLARE_EVENT_CLASS(ssam_free_class,
  14180. + TP_PROTO(void *ptr),
  14181. +
  14182. + TP_ARGS(ptr),
  14183. +
  14184. + TP_STRUCT__entry(
  14185. + __array(char, uid, SSAM_PTR_UID_LEN)
  14186. + ),
  14187. +
  14188. + TP_fast_assign(
  14189. + ssam_trace_ptr_uid(ptr, __entry->uid);
  14190. + ),
  14191. +
  14192. + TP_printk("uid=%s", __entry->uid)
  14193. +);
  14194. +
  14195. +#define DEFINE_SSAM_FREE_EVENT(name) \
  14196. + DEFINE_EVENT(ssam_free_class, ssam_##name, \
  14197. + TP_PROTO(void *ptr), \
  14198. + TP_ARGS(ptr) \
  14199. + )
  14200. +
  14201. +DECLARE_EVENT_CLASS(ssam_pending_class,
  14202. + TP_PROTO(unsigned int pending),
  14203. +
  14204. + TP_ARGS(pending),
  14205. +
  14206. + TP_STRUCT__entry(
  14207. + __field(unsigned int, pending)
  14208. + ),
  14209. +
  14210. + TP_fast_assign(
  14211. + __entry->pending = pending;
  14212. + ),
  14213. +
  14214. + TP_printk("pending=%u", __entry->pending)
  14215. +);
  14216. +
  14217. +#define DEFINE_SSAM_PENDING_EVENT(name) \
  14218. + DEFINE_EVENT(ssam_pending_class, ssam_##name, \
  14219. + TP_PROTO(unsigned int pending), \
  14220. + TP_ARGS(pending) \
  14221. + )
  14222. +
  14223. +DECLARE_EVENT_CLASS(ssam_data_class,
  14224. + TP_PROTO(size_t length),
  14225. +
  14226. + TP_ARGS(length),
  14227. +
  14228. + TP_STRUCT__entry(
  14229. + __field(size_t, length)
  14230. + ),
  14231. +
  14232. + TP_fast_assign(
  14233. + __entry->length = length;
  14234. + ),
  14235. +
  14236. + TP_printk("length=%zu", __entry->length)
  14237. +);
  14238. +
  14239. +#define DEFINE_SSAM_DATA_EVENT(name) \
  14240. + DEFINE_EVENT(ssam_data_class, ssam_##name, \
  14241. + TP_PROTO(size_t length), \
  14242. + TP_ARGS(length) \
  14243. + )
  14244. +
  14245. +DEFINE_SSAM_FRAME_EVENT(rx_frame_received);
  14246. +DEFINE_SSAM_COMMAND_EVENT(rx_response_received);
  14247. +DEFINE_SSAM_COMMAND_EVENT(rx_event_received);
  14248. +
  14249. +DEFINE_SSAM_PACKET_EVENT(packet_release);
  14250. +DEFINE_SSAM_PACKET_EVENT(packet_submit);
  14251. +DEFINE_SSAM_PACKET_EVENT(packet_resubmit);
  14252. +DEFINE_SSAM_PACKET_EVENT(packet_timeout);
  14253. +DEFINE_SSAM_PACKET_EVENT(packet_cancel);
  14254. +DEFINE_SSAM_PACKET_STATUS_EVENT(packet_complete);
  14255. +DEFINE_SSAM_PENDING_EVENT(ptl_timeout_reap);
  14256. +
  14257. +DEFINE_SSAM_REQUEST_EVENT(request_submit);
  14258. +DEFINE_SSAM_REQUEST_EVENT(request_timeout);
  14259. +DEFINE_SSAM_REQUEST_EVENT(request_cancel);
  14260. +DEFINE_SSAM_REQUEST_STATUS_EVENT(request_complete);
  14261. +DEFINE_SSAM_PENDING_EVENT(rtl_timeout_reap);
  14262. +
  14263. +DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_ack_packet);
  14264. +DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_nak_packet);
  14265. +DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_dsq_packet);
  14266. +DEFINE_SSAM_PACKET_STATUS_EVENT(ei_tx_fail_write);
  14267. +DEFINE_SSAM_PACKET_EVENT(ei_tx_corrupt_data);
  14268. +DEFINE_SSAM_DATA_EVENT(ei_rx_corrupt_syn);
  14269. +DEFINE_SSAM_FRAME_EVENT(ei_rx_corrupt_data);
  14270. +DEFINE_SSAM_REQUEST_EVENT(ei_rx_drop_response);
  14271. +
  14272. +DEFINE_SSAM_ALLOC_EVENT(ctrl_packet_alloc);
  14273. +DEFINE_SSAM_FREE_EVENT(ctrl_packet_free);
  14274. +
  14275. +DEFINE_SSAM_ALLOC_EVENT(event_item_alloc);
  14276. +DEFINE_SSAM_FREE_EVENT(event_item_free);
  14277. +
  14278. +#endif /* _SURFACE_AGGREGATOR_TRACE_H */
  14279. +
  14280. +/* This part must be outside protection */
  14281. +#undef TRACE_INCLUDE_PATH
  14282. +#undef TRACE_INCLUDE_FILE
  14283. +
  14284. +#define TRACE_INCLUDE_PATH .
  14285. +#define TRACE_INCLUDE_FILE trace
  14286. +
  14287. +#include <trace/define_trace.h>
  14288. diff --git a/drivers/platform/x86/surface_aggregator_cdev.c b/drivers/platform/x86/surface_aggregator_cdev.c
  14289. new file mode 100644
  14290. index 000000000000..30fb50fde450
  14291. --- /dev/null
  14292. +++ b/drivers/platform/x86/surface_aggregator_cdev.c
  14293. @@ -0,0 +1,810 @@
  14294. +// SPDX-License-Identifier: GPL-2.0+
  14295. +/*
  14296. + * Provides user-space access to the SSAM EC via the /dev/surface/aggregator
  14297. + * misc device. Intended for debugging and development.
  14298. + *
  14299. + * Copyright (C) 2020-2021 Maximilian Luz <luzmaximilian@gmail.com>
  14300. + */
  14301. +
  14302. +#include <linux/fs.h>
  14303. +#include <linux/ioctl.h>
  14304. +#include <linux/kernel.h>
  14305. +#include <linux/kfifo.h>
  14306. +#include <linux/kref.h>
  14307. +#include <linux/miscdevice.h>
  14308. +#include <linux/module.h>
  14309. +#include <linux/platform_device.h>
  14310. +#include <linux/poll.h>
  14311. +#include <linux/rwsem.h>
  14312. +#include <linux/slab.h>
  14313. +#include <linux/uaccess.h>
  14314. +#include <linux/vmalloc.h>
  14315. +
  14316. +#include <linux/surface_aggregator/cdev.h>
  14317. +#include <linux/surface_aggregator/controller.h>
  14318. +#include <linux/surface_aggregator/serial_hub.h>
  14319. +
  14320. +#define SSAM_CDEV_DEVICE_NAME "surface_aggregator_cdev"
  14321. +
  14322. +
  14323. +/* -- Main structures. ------------------------------------------------------ */
  14324. +
  14325. +enum ssam_cdev_device_state {
  14326. + SSAM_CDEV_DEVICE_SHUTDOWN_BIT = BIT(0),
  14327. +};
  14328. +
  14329. +struct ssam_cdev {
  14330. + struct kref kref;
  14331. + struct rw_semaphore lock;
  14332. +
  14333. + struct device *dev;
  14334. + struct ssam_controller *ctrl;
  14335. + struct miscdevice mdev;
  14336. + unsigned long flags;
  14337. +
  14338. + struct rw_semaphore client_lock; /* Guards client list. */
  14339. + struct list_head client_list;
  14340. +};
  14341. +
  14342. +struct ssam_cdev_client;
  14343. +
  14344. +struct ssam_cdev_notifier {
  14345. + struct ssam_cdev_client *client;
  14346. + struct ssam_event_notifier nf;
  14347. +};
  14348. +
  14349. +struct ssam_cdev_client {
  14350. + struct ssam_cdev *cdev;
  14351. + struct list_head node;
  14352. +
  14353. + struct mutex notifier_lock; /* Guards notifier access for registration */
  14354. + struct ssam_cdev_notifier *notifier[SSH_NUM_EVENTS];
  14355. +
  14356. + struct mutex read_lock; /* Guards FIFO buffer read access */
  14357. + struct mutex write_lock; /* Guards FIFO buffer write access */
  14358. + DECLARE_KFIFO(buffer, u8, 4096);
  14359. +
  14360. + wait_queue_head_t waitq;
  14361. + struct fasync_struct *fasync;
  14362. +};
  14363. +
  14364. +static void __ssam_cdev_release(struct kref *kref)
  14365. +{
  14366. + kfree(container_of(kref, struct ssam_cdev, kref));
  14367. +}
  14368. +
  14369. +static struct ssam_cdev *ssam_cdev_get(struct ssam_cdev *cdev)
  14370. +{
  14371. + if (cdev)
  14372. + kref_get(&cdev->kref);
  14373. +
  14374. + return cdev;
  14375. +}
  14376. +
  14377. +static void ssam_cdev_put(struct ssam_cdev *cdev)
  14378. +{
  14379. + if (cdev)
  14380. + kref_put(&cdev->kref, __ssam_cdev_release);
  14381. +}
  14382. +
  14383. +
  14384. +/* -- Notifier handling. ---------------------------------------------------- */
  14385. +
  14386. +static u32 ssam_cdev_notifier(struct ssam_event_notifier *nf, const struct ssam_event *in)
  14387. +{
  14388. + struct ssam_cdev_notifier *cdev_nf = container_of(nf, struct ssam_cdev_notifier, nf);
  14389. + struct ssam_cdev_client *client = cdev_nf->client;
  14390. + struct ssam_cdev_event event;
  14391. + size_t n = struct_size(&event, data, in->length);
  14392. +
  14393. + /* Translate event. */
  14394. + event.target_category = in->target_category;
  14395. + event.target_id = in->target_id;
  14396. + event.command_id = in->command_id;
  14397. + event.instance_id = in->instance_id;
  14398. + event.length = in->length;
  14399. +
  14400. + mutex_lock(&client->write_lock);
  14401. +
  14402. + /* Make sure we have enough space. */
  14403. + if (kfifo_avail(&client->buffer) < n) {
  14404. + dev_warn(client->cdev->dev,
  14405. + "buffer full, dropping event (tc: %#04x, tid: %#04x, cid: %#04x, iid: %#04x)\n",
  14406. + in->target_category, in->target_id, in->command_id, in->instance_id);
  14407. + mutex_unlock(&client->write_lock);
  14408. + return 0;
  14409. + }
  14410. +
  14411. + /* Copy event header and payload. */
  14412. + kfifo_in(&client->buffer, (const u8 *)&event, struct_size(&event, data, 0));
  14413. + kfifo_in(&client->buffer, &in->data[0], in->length);
  14414. +
  14415. + mutex_unlock(&client->write_lock);
  14416. +
  14417. + /* Notify waiting readers. */
  14418. + kill_fasync(&client->fasync, SIGIO, POLL_IN);
  14419. + wake_up_interruptible(&client->waitq);
  14420. +
  14421. + /*
  14422. + * Don't mark events as handled, this is the job of a proper driver and
  14423. + * not the debugging interface.
  14424. + */
  14425. + return 0;
  14426. +}
  14427. +
  14428. +static int ssam_cdev_notifier_register(struct ssam_cdev_client *client, u8 tc, int priority)
  14429. +{
  14430. + const u16 rqid = ssh_tc_to_rqid(tc);
  14431. + const u16 event = ssh_rqid_to_event(rqid);
  14432. + struct ssam_cdev_notifier *nf;
  14433. + int status;
  14434. +
  14435. + lockdep_assert_held_read(&client->cdev->lock);
  14436. +
  14437. + /* Validate notifier target category. */
  14438. + if (!ssh_rqid_is_event(rqid))
  14439. + return -EINVAL;
  14440. +
  14441. + mutex_lock(&client->notifier_lock);
  14442. +
  14443. + /* Check if the notifier has already been registered. */
  14444. + if (client->notifier[event]) {
  14445. + mutex_unlock(&client->notifier_lock);
  14446. + return -EEXIST;
  14447. + }
  14448. +
  14449. + /* Allocate new notifier. */
  14450. + nf = kzalloc(sizeof(*nf), GFP_KERNEL);
  14451. + if (!nf) {
  14452. + mutex_unlock(&client->notifier_lock);
  14453. + return -ENOMEM;
  14454. + }
  14455. +
  14456. + /*
  14457. + * Create a dummy notifier with the minimal required fields for
  14458. + * observer registration. Note that we can skip fully specifying event
  14459. + * and registry here as we do not need any matching and use silent
  14460. + * registration, which does not enable the corresponding event.
  14461. + */
  14462. + nf->client = client;
  14463. + nf->nf.base.fn = ssam_cdev_notifier;
  14464. + nf->nf.base.priority = priority;
  14465. + nf->nf.event.id.target_category = tc;
  14466. + nf->nf.event.mask = 0; /* Do not do any matching. */
  14467. + nf->nf.flags = SSAM_EVENT_NOTIFIER_OBSERVER;
  14468. +
  14469. + /* Register notifier. */
  14470. + status = ssam_notifier_register(client->cdev->ctrl, &nf->nf);
  14471. + if (status)
  14472. + kfree(nf);
  14473. + else
  14474. + client->notifier[event] = nf;
  14475. +
  14476. + mutex_unlock(&client->notifier_lock);
  14477. + return status;
  14478. +}
  14479. +
  14480. +static int ssam_cdev_notifier_unregister(struct ssam_cdev_client *client, u8 tc)
  14481. +{
  14482. + const u16 rqid = ssh_tc_to_rqid(tc);
  14483. + const u16 event = ssh_rqid_to_event(rqid);
  14484. + int status;
  14485. +
  14486. + lockdep_assert_held_read(&client->cdev->lock);
  14487. +
  14488. + /* Validate notifier target category. */
  14489. + if (!ssh_rqid_is_event(rqid))
  14490. + return -EINVAL;
  14491. +
  14492. + mutex_lock(&client->notifier_lock);
  14493. +
  14494. + /* Check if the notifier is currently registered. */
  14495. + if (!client->notifier[event]) {
  14496. + mutex_unlock(&client->notifier_lock);
  14497. + return -ENOENT;
  14498. + }
  14499. +
  14500. + /* Unregister and free notifier. */
  14501. + status = ssam_notifier_unregister(client->cdev->ctrl, &client->notifier[event]->nf);
  14502. + kfree(client->notifier[event]);
  14503. + client->notifier[event] = NULL;
  14504. +
  14505. + mutex_unlock(&client->notifier_lock);
  14506. + return status;
  14507. +}
  14508. +
  14509. +static void ssam_cdev_notifier_unregister_all(struct ssam_cdev_client *client)
  14510. +{
  14511. + int i;
  14512. +
  14513. + down_read(&client->cdev->lock);
  14514. +
  14515. + /*
  14516. + * This function may be used during shutdown, thus we need to test for
  14517. + * cdev->ctrl instead of the SSAM_CDEV_DEVICE_SHUTDOWN_BIT bit.
  14518. + */
  14519. + if (client->cdev->ctrl) {
  14520. + for (i = 0; i < SSH_NUM_EVENTS; i++)
  14521. + ssam_cdev_notifier_unregister(client, i + 1);
  14522. +
  14523. + } else {
  14524. + int count = 0;
  14525. +
  14526. + /*
  14527. + * Device has been shut down. Any notifier remaining is a bug,
  14528. + * so warn about that as this would otherwise hardly be
  14529. + * noticeable. Nevertheless, free them as well.
  14530. + */
  14531. + mutex_lock(&client->notifier_lock);
  14532. + for (i = 0; i < SSH_NUM_EVENTS; i++) {
  14533. + count += !!(client->notifier[i]);
  14534. + kfree(client->notifier[i]);
  14535. + client->notifier[i] = NULL;
  14536. + }
  14537. + mutex_unlock(&client->notifier_lock);
  14538. +
  14539. + WARN_ON(count > 0);
  14540. + }
  14541. +
  14542. + up_read(&client->cdev->lock);
  14543. +}
  14544. +
  14545. +
  14546. +/* -- IOCTL functions. ------------------------------------------------------ */
  14547. +
  14548. +static long ssam_cdev_request(struct ssam_cdev_client *client, struct ssam_cdev_request __user *r)
  14549. +{
  14550. + struct ssam_cdev_request rqst;
  14551. + struct ssam_request spec = {};
  14552. + struct ssam_response rsp = {};
  14553. + const void __user *plddata;
  14554. + void __user *rspdata;
  14555. + int status = 0, ret = 0, tmp;
  14556. +
  14557. + lockdep_assert_held_read(&client->cdev->lock);
  14558. +
  14559. + ret = copy_struct_from_user(&rqst, sizeof(rqst), r, sizeof(*r));
  14560. + if (ret)
  14561. + goto out;
  14562. +
  14563. + plddata = u64_to_user_ptr(rqst.payload.data);
  14564. + rspdata = u64_to_user_ptr(rqst.response.data);
  14565. +
  14566. + /* Setup basic request fields. */
  14567. + spec.target_category = rqst.target_category;
  14568. + spec.target_id = rqst.target_id;
  14569. + spec.command_id = rqst.command_id;
  14570. + spec.instance_id = rqst.instance_id;
  14571. + spec.flags = 0;
  14572. + spec.length = rqst.payload.length;
  14573. + spec.payload = NULL;
  14574. +
  14575. + if (rqst.flags & SSAM_CDEV_REQUEST_HAS_RESPONSE)
  14576. + spec.flags |= SSAM_REQUEST_HAS_RESPONSE;
  14577. +
  14578. + if (rqst.flags & SSAM_CDEV_REQUEST_UNSEQUENCED)
  14579. + spec.flags |= SSAM_REQUEST_UNSEQUENCED;
  14580. +
  14581. + rsp.capacity = rqst.response.length;
  14582. + rsp.length = 0;
  14583. + rsp.pointer = NULL;
  14584. +
  14585. + /* Get request payload from user-space. */
  14586. + if (spec.length) {
  14587. + if (!plddata) {
  14588. + ret = -EINVAL;
  14589. + goto out;
  14590. + }
  14591. +
  14592. + /*
  14593. + * Note: spec.length is limited to U16_MAX bytes via struct
  14594. + * ssam_cdev_request. This is slightly larger than the
  14595. + * theoretical maximum (SSH_COMMAND_MAX_PAYLOAD_SIZE) of the
  14596. + * underlying protocol (note that nothing remotely this size
  14597. + * should ever be allocated in any normal case). This size is
  14598. + * validated later in ssam_request_sync(), for allocation the
  14599. + * bound imposed by u16 should be enough.
  14600. + */
  14601. + spec.payload = kzalloc(spec.length, GFP_KERNEL);
  14602. + if (!spec.payload) {
  14603. + ret = -ENOMEM;
  14604. + goto out;
  14605. + }
  14606. +
  14607. + if (copy_from_user((void *)spec.payload, plddata, spec.length)) {
  14608. + ret = -EFAULT;
  14609. + goto out;
  14610. + }
  14611. + }
  14612. +
  14613. + /* Allocate response buffer. */
  14614. + if (rsp.capacity) {
  14615. + if (!rspdata) {
  14616. + ret = -EINVAL;
  14617. + goto out;
  14618. + }
  14619. +
  14620. + /*
  14621. + * Note: rsp.capacity is limited to U16_MAX bytes via struct
  14622. + * ssam_cdev_request. This is slightly larger than the
  14623. + * theoretical maximum (SSH_COMMAND_MAX_PAYLOAD_SIZE) of the
  14624. + * underlying protocol (note that nothing remotely this size
  14625. + * should ever be allocated in any normal case). In later use,
  14626. + * this capacity does not have to be strictly bounded, as it
  14627. + * is only used as an output buffer to be written to. For
  14628. + * allocation the bound imposed by u16 should be enough.
  14629. + */
  14630. + rsp.pointer = kzalloc(rsp.capacity, GFP_KERNEL);
  14631. + if (!rsp.pointer) {
  14632. + ret = -ENOMEM;
  14633. + goto out;
  14634. + }
  14635. + }
  14636. +
  14637. + /* Perform request. */
  14638. + status = ssam_request_sync(client->cdev->ctrl, &spec, &rsp);
  14639. + if (status)
  14640. + goto out;
  14641. +
  14642. + /* Copy response to user-space. */
  14643. + if (rsp.length && copy_to_user(rspdata, rsp.pointer, rsp.length))
  14644. + ret = -EFAULT;
  14645. +
  14646. +out:
  14647. + /* Always try to set response-length and status. */
  14648. + tmp = put_user(rsp.length, &r->response.length);
  14649. + if (tmp)
  14650. + ret = tmp;
  14651. +
  14652. + tmp = put_user(status, &r->status);
  14653. + if (tmp)
  14654. + ret = tmp;
  14655. +
  14656. + /* Cleanup. */
  14657. + kfree(spec.payload);
  14658. + kfree(rsp.pointer);
  14659. +
  14660. + return ret;
  14661. +}
  14662. +
  14663. +static long ssam_cdev_notif_register(struct ssam_cdev_client *client,
  14664. + const struct ssam_cdev_notifier_desc __user *d)
  14665. +{
  14666. + struct ssam_cdev_notifier_desc desc;
  14667. + long ret;
  14668. +
  14669. + lockdep_assert_held_read(&client->cdev->lock);
  14670. +
  14671. + ret = copy_struct_from_user(&desc, sizeof(desc), d, sizeof(*d));
  14672. + if (ret)
  14673. + return ret;
  14674. +
  14675. + return ssam_cdev_notifier_register(client, desc.target_category, desc.priority);
  14676. +}
  14677. +
  14678. +static long ssam_cdev_notif_unregister(struct ssam_cdev_client *client,
  14679. + const struct ssam_cdev_notifier_desc __user *d)
  14680. +{
  14681. + struct ssam_cdev_notifier_desc desc;
  14682. + long ret;
  14683. +
  14684. + lockdep_assert_held_read(&client->cdev->lock);
  14685. +
  14686. + ret = copy_struct_from_user(&desc, sizeof(desc), d, sizeof(*d));
  14687. + if (ret)
  14688. + return ret;
  14689. +
  14690. + return ssam_cdev_notifier_unregister(client, desc.target_category);
  14691. +}
  14692. +
  14693. +static long ssam_cdev_event_enable(struct ssam_cdev_client *client,
  14694. + const struct ssam_cdev_event_desc __user *d)
  14695. +{
  14696. + struct ssam_cdev_event_desc desc;
  14697. + struct ssam_event_registry reg;
  14698. + struct ssam_event_id id;
  14699. + long ret;
  14700. +
  14701. + lockdep_assert_held_read(&client->cdev->lock);
  14702. +
  14703. + /* Read descriptor from user-space. */
  14704. + ret = copy_struct_from_user(&desc, sizeof(desc), d, sizeof(*d));
  14705. + if (ret)
  14706. + return ret;
  14707. +
  14708. + /* Translate descriptor. */
  14709. + reg.target_category = desc.reg.target_category;
  14710. + reg.target_id = desc.reg.target_id;
  14711. + reg.cid_enable = desc.reg.cid_enable;
  14712. + reg.cid_disable = desc.reg.cid_disable;
  14713. +
  14714. + id.target_category = desc.id.target_category;
  14715. + id.instance = desc.id.instance;
  14716. +
  14717. + /* Disable event. */
  14718. + return ssam_controller_event_enable(client->cdev->ctrl, reg, id, desc.flags);
  14719. +}
  14720. +
  14721. +static long ssam_cdev_event_disable(struct ssam_cdev_client *client,
  14722. + const struct ssam_cdev_event_desc __user *d)
  14723. +{
  14724. + struct ssam_cdev_event_desc desc;
  14725. + struct ssam_event_registry reg;
  14726. + struct ssam_event_id id;
  14727. + long ret;
  14728. +
  14729. + lockdep_assert_held_read(&client->cdev->lock);
  14730. +
  14731. + /* Read descriptor from user-space. */
  14732. + ret = copy_struct_from_user(&desc, sizeof(desc), d, sizeof(*d));
  14733. + if (ret)
  14734. + return ret;
  14735. +
  14736. + /* Translate descriptor. */
  14737. + reg.target_category = desc.reg.target_category;
  14738. + reg.target_id = desc.reg.target_id;
  14739. + reg.cid_enable = desc.reg.cid_enable;
  14740. + reg.cid_disable = desc.reg.cid_disable;
  14741. +
  14742. + id.target_category = desc.id.target_category;
  14743. + id.instance = desc.id.instance;
  14744. +
  14745. + /* Disable event. */
  14746. + return ssam_controller_event_disable(client->cdev->ctrl, reg, id, desc.flags);
  14747. +}
  14748. +
  14749. +
  14750. +/* -- File operations. ------------------------------------------------------ */
  14751. +
  14752. +static int ssam_cdev_device_open(struct inode *inode, struct file *filp)
  14753. +{
  14754. + struct miscdevice *mdev = filp->private_data;
  14755. + struct ssam_cdev_client *client;
  14756. + struct ssam_cdev *cdev = container_of(mdev, struct ssam_cdev, mdev);
  14757. +
  14758. + /* Initialize client */
  14759. + client = vzalloc(sizeof(*client));
  14760. + if (!client)
  14761. + return -ENOMEM;
  14762. +
  14763. + client->cdev = ssam_cdev_get(cdev);
  14764. +
  14765. + INIT_LIST_HEAD(&client->node);
  14766. +
  14767. + mutex_init(&client->notifier_lock);
  14768. +
  14769. + mutex_init(&client->read_lock);
  14770. + mutex_init(&client->write_lock);
  14771. + INIT_KFIFO(client->buffer);
  14772. + init_waitqueue_head(&client->waitq);
  14773. +
  14774. + filp->private_data = client;
  14775. +
  14776. + /* Attach client. */
  14777. + down_write(&cdev->client_lock);
  14778. +
  14779. + if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags)) {
  14780. + up_write(&cdev->client_lock);
  14781. + mutex_destroy(&client->write_lock);
  14782. + mutex_destroy(&client->read_lock);
  14783. + mutex_destroy(&client->notifier_lock);
  14784. + ssam_cdev_put(client->cdev);
  14785. + vfree(client);
  14786. + return -ENODEV;
  14787. + }
  14788. + list_add_tail(&client->node, &cdev->client_list);
  14789. +
  14790. + up_write(&cdev->client_lock);
  14791. +
  14792. + stream_open(inode, filp);
  14793. + return 0;
  14794. +}
  14795. +
  14796. +static int ssam_cdev_device_release(struct inode *inode, struct file *filp)
  14797. +{
  14798. + struct ssam_cdev_client *client = filp->private_data;
  14799. +
  14800. + /* Force-unregister all remaining notifiers of this client. */
  14801. + ssam_cdev_notifier_unregister_all(client);
  14802. +
  14803. + /* Detach client. */
  14804. + down_write(&client->cdev->client_lock);
  14805. + list_del(&client->node);
  14806. + up_write(&client->cdev->client_lock);
  14807. +
  14808. + /* Free client. */
  14809. + mutex_destroy(&client->write_lock);
  14810. + mutex_destroy(&client->read_lock);
  14811. +
  14812. + mutex_destroy(&client->notifier_lock);
  14813. +
  14814. + ssam_cdev_put(client->cdev);
  14815. + vfree(client);
  14816. +
  14817. + return 0;
  14818. +}
  14819. +
  14820. +static long __ssam_cdev_device_ioctl(struct ssam_cdev_client *client, unsigned int cmd,
  14821. + unsigned long arg)
  14822. +{
  14823. + lockdep_assert_held_read(&client->cdev->lock);
  14824. +
  14825. + switch (cmd) {
  14826. + case SSAM_CDEV_REQUEST:
  14827. + return ssam_cdev_request(client, (struct ssam_cdev_request __user *)arg);
  14828. +
  14829. + case SSAM_CDEV_NOTIF_REGISTER:
  14830. + return ssam_cdev_notif_register(client,
  14831. + (struct ssam_cdev_notifier_desc __user *)arg);
  14832. +
  14833. + case SSAM_CDEV_NOTIF_UNREGISTER:
  14834. + return ssam_cdev_notif_unregister(client,
  14835. + (struct ssam_cdev_notifier_desc __user *)arg);
  14836. +
  14837. + case SSAM_CDEV_EVENT_ENABLE:
  14838. + return ssam_cdev_event_enable(client, (struct ssam_cdev_event_desc __user *)arg);
  14839. +
  14840. + case SSAM_CDEV_EVENT_DISABLE:
  14841. + return ssam_cdev_event_disable(client, (struct ssam_cdev_event_desc __user *)arg);
  14842. +
  14843. + default:
  14844. + return -ENOTTY;
  14845. + }
  14846. +}
  14847. +
  14848. +static long ssam_cdev_device_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  14849. +{
  14850. + struct ssam_cdev_client *client = file->private_data;
  14851. + long status;
  14852. +
  14853. + /* Ensure that controller is valid for as long as we need it. */
  14854. + if (down_read_killable(&client->cdev->lock))
  14855. + return -ERESTARTSYS;
  14856. +
  14857. + if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &client->cdev->flags)) {
  14858. + up_read(&client->cdev->lock);
  14859. + return -ENODEV;
  14860. + }
  14861. +
  14862. + status = __ssam_cdev_device_ioctl(client, cmd, arg);
  14863. +
  14864. + up_read(&client->cdev->lock);
  14865. + return status;
  14866. +}
  14867. +
  14868. +static ssize_t ssam_cdev_read(struct file *file, char __user *buf, size_t count, loff_t *offs)
  14869. +{
  14870. + struct ssam_cdev_client *client = file->private_data;
  14871. + struct ssam_cdev *cdev = client->cdev;
  14872. + unsigned int copied;
  14873. + int status = 0;
  14874. +
  14875. + if (down_read_killable(&cdev->lock))
  14876. + return -ERESTARTSYS;
  14877. +
  14878. + /* Make sure we're not shut down. */
  14879. + if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags)) {
  14880. + up_read(&cdev->lock);
  14881. + return -ENODEV;
  14882. + }
  14883. +
  14884. + do {
  14885. + /* Check availability, wait if necessary. */
  14886. + if (kfifo_is_empty(&client->buffer)) {
  14887. + up_read(&cdev->lock);
  14888. +
  14889. + if (file->f_flags & O_NONBLOCK)
  14890. + return -EAGAIN;
  14891. +
  14892. + status = wait_event_interruptible(client->waitq,
  14893. + !kfifo_is_empty(&client->buffer) ||
  14894. + test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT,
  14895. + &cdev->flags));
  14896. + if (status < 0)
  14897. + return status;
  14898. +
  14899. + if (down_read_killable(&cdev->lock))
  14900. + return -ERESTARTSYS;
  14901. +
  14902. + /* Need to check that we're not shut down again. */
  14903. + if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags)) {
  14904. + up_read(&cdev->lock);
  14905. + return -ENODEV;
  14906. + }
  14907. + }
  14908. +
  14909. + /* Try to read from FIFO. */
  14910. + if (mutex_lock_interruptible(&client->read_lock)) {
  14911. + up_read(&cdev->lock);
  14912. + return -ERESTARTSYS;
  14913. + }
  14914. +
  14915. + status = kfifo_to_user(&client->buffer, buf, count, &copied);
  14916. + mutex_unlock(&client->read_lock);
  14917. +
  14918. + if (status < 0) {
  14919. + up_read(&cdev->lock);
  14920. + return status;
  14921. + }
  14922. +
  14923. + /* We might not have gotten anything, check this here. */
  14924. + if (copied == 0 && (file->f_flags & O_NONBLOCK)) {
  14925. + up_read(&cdev->lock);
  14926. + return -EAGAIN;
  14927. + }
  14928. + } while (copied == 0);
  14929. +
  14930. + up_read(&cdev->lock);
  14931. + return copied;
  14932. +}
  14933. +
  14934. +static __poll_t ssam_cdev_poll(struct file *file, struct poll_table_struct *pt)
  14935. +{
  14936. + struct ssam_cdev_client *client = file->private_data;
  14937. + __poll_t events = 0;
  14938. +
  14939. + if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &client->cdev->flags))
  14940. + return EPOLLHUP | EPOLLERR;
  14941. +
  14942. + poll_wait(file, &client->waitq, pt);
  14943. +
  14944. + if (!kfifo_is_empty(&client->buffer))
  14945. + events |= EPOLLIN | EPOLLRDNORM;
  14946. +
  14947. + return events;
  14948. +}
  14949. +
  14950. +static int ssam_cdev_fasync(int fd, struct file *file, int on)
  14951. +{
  14952. + struct ssam_cdev_client *client = file->private_data;
  14953. +
  14954. + return fasync_helper(fd, file, on, &client->fasync);
  14955. +}
  14956. +
  14957. +static const struct file_operations ssam_controller_fops = {
  14958. + .owner = THIS_MODULE,
  14959. + .open = ssam_cdev_device_open,
  14960. + .release = ssam_cdev_device_release,
  14961. + .read = ssam_cdev_read,
  14962. + .poll = ssam_cdev_poll,
  14963. + .fasync = ssam_cdev_fasync,
  14964. + .unlocked_ioctl = ssam_cdev_device_ioctl,
  14965. + .compat_ioctl = ssam_cdev_device_ioctl,
  14966. + .llseek = no_llseek,
  14967. +};
  14968. +
  14969. +
  14970. +/* -- Device and driver setup ----------------------------------------------- */
  14971. +
  14972. +static int ssam_dbg_device_probe(struct platform_device *pdev)
  14973. +{
  14974. + struct ssam_controller *ctrl;
  14975. + struct ssam_cdev *cdev;
  14976. + int status;
  14977. +
  14978. + ctrl = ssam_client_bind(&pdev->dev);
  14979. + if (IS_ERR(ctrl))
  14980. + return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
  14981. +
  14982. + cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  14983. + if (!cdev)
  14984. + return -ENOMEM;
  14985. +
  14986. + kref_init(&cdev->kref);
  14987. + init_rwsem(&cdev->lock);
  14988. + cdev->ctrl = ctrl;
  14989. + cdev->dev = &pdev->dev;
  14990. +
  14991. + cdev->mdev.parent = &pdev->dev;
  14992. + cdev->mdev.minor = MISC_DYNAMIC_MINOR;
  14993. + cdev->mdev.name = "surface_aggregator";
  14994. + cdev->mdev.nodename = "surface/aggregator";
  14995. + cdev->mdev.fops = &ssam_controller_fops;
  14996. +
  14997. + init_rwsem(&cdev->client_lock);
  14998. + INIT_LIST_HEAD(&cdev->client_list);
  14999. +
  15000. + status = misc_register(&cdev->mdev);
  15001. + if (status) {
  15002. + kfree(cdev);
  15003. + return status;
  15004. + }
  15005. +
  15006. + platform_set_drvdata(pdev, cdev);
  15007. + return 0;
  15008. +}
  15009. +
  15010. +static int ssam_dbg_device_remove(struct platform_device *pdev)
  15011. +{
  15012. + struct ssam_cdev *cdev = platform_get_drvdata(pdev);
  15013. + struct ssam_cdev_client *client;
  15014. +
  15015. + /*
  15016. + * Mark device as shut-down. Prevent new clients from being added and
  15017. + * new operations from being executed.
  15018. + */
  15019. + set_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags);
  15020. +
  15021. + down_write(&cdev->client_lock);
  15022. +
  15023. + /* Remove all notifiers registered by us. */
  15024. + list_for_each_entry(client, &cdev->client_list, node) {
  15025. + ssam_cdev_notifier_unregister_all(client);
  15026. + }
  15027. +
  15028. + /* Wake up async clients. */
  15029. + list_for_each_entry(client, &cdev->client_list, node) {
  15030. + kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  15031. + }
  15032. +
  15033. + /* Wake up blocking clients. */
  15034. + list_for_each_entry(client, &cdev->client_list, node) {
  15035. + wake_up_interruptible(&client->waitq);
  15036. + }
  15037. +
  15038. + up_write(&cdev->client_lock);
  15039. +
  15040. + /*
  15041. + * The controller is only guaranteed to be valid for as long as the
  15042. + * driver is bound. Remove controller so that any lingering open files
  15043. + * cannot access it any more after we're gone.
  15044. + */
  15045. + down_write(&cdev->lock);
  15046. + cdev->ctrl = NULL;
  15047. + cdev->dev = NULL;
  15048. + up_write(&cdev->lock);
  15049. +
  15050. + misc_deregister(&cdev->mdev);
  15051. +
  15052. + ssam_cdev_put(cdev);
  15053. + return 0;
  15054. +}
  15055. +
  15056. +static struct platform_device *ssam_cdev_device;
  15057. +
  15058. +static struct platform_driver ssam_cdev_driver = {
  15059. + .probe = ssam_dbg_device_probe,
  15060. + .remove = ssam_dbg_device_remove,
  15061. + .driver = {
  15062. + .name = SSAM_CDEV_DEVICE_NAME,
  15063. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  15064. + },
  15065. +};
  15066. +
  15067. +static int __init ssam_debug_init(void)
  15068. +{
  15069. + int status;
  15070. +
  15071. + ssam_cdev_device = platform_device_alloc(SSAM_CDEV_DEVICE_NAME,
  15072. + PLATFORM_DEVID_NONE);
  15073. + if (!ssam_cdev_device)
  15074. + return -ENOMEM;
  15075. +
  15076. + status = platform_device_add(ssam_cdev_device);
  15077. + if (status)
  15078. + goto err_device;
  15079. +
  15080. + status = platform_driver_register(&ssam_cdev_driver);
  15081. + if (status)
  15082. + goto err_driver;
  15083. +
  15084. + return 0;
  15085. +
  15086. +err_driver:
  15087. + platform_device_del(ssam_cdev_device);
  15088. +err_device:
  15089. + platform_device_put(ssam_cdev_device);
  15090. + return status;
  15091. +}
  15092. +module_init(ssam_debug_init);
  15093. +
  15094. +static void __exit ssam_debug_exit(void)
  15095. +{
  15096. + platform_driver_unregister(&ssam_cdev_driver);
  15097. + platform_device_unregister(ssam_cdev_device);
  15098. +}
  15099. +module_exit(ssam_debug_exit);
  15100. +
  15101. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  15102. +MODULE_DESCRIPTION("User-space interface for Surface System Aggregator Module");
  15103. +MODULE_LICENSE("GPL");
  15104. diff --git a/drivers/platform/x86/surface_aggregator_registry.c b/drivers/platform/x86/surface_aggregator_registry.c
  15105. new file mode 100644
  15106. index 000000000000..4428c4330229
  15107. --- /dev/null
  15108. +++ b/drivers/platform/x86/surface_aggregator_registry.c
  15109. @@ -0,0 +1,606 @@
  15110. +// SPDX-License-Identifier: GPL-2.0+
  15111. +/*
  15112. + * Surface System Aggregator Module (SSAM) client device registry.
  15113. + *
  15114. + * Registry for non-platform/non-ACPI SSAM client devices, i.e. devices that
  15115. + * cannot be auto-detected. Provides device-hubs and performs instantiation
  15116. + * for these devices.
  15117. + *
  15118. + * Copyright (C) 2020-2021 Maximilian Luz <luzmaximilian@gmail.com>
  15119. + */
  15120. +
  15121. +#include <linux/acpi.h>
  15122. +#include <linux/kernel.h>
  15123. +#include <linux/limits.h>
  15124. +#include <linux/module.h>
  15125. +#include <linux/platform_device.h>
  15126. +#include <linux/property.h>
  15127. +#include <linux/types.h>
  15128. +#include <linux/workqueue.h>
  15129. +
  15130. +#include <linux/surface_aggregator/controller.h>
  15131. +#include <linux/surface_aggregator/device.h>
  15132. +
  15133. +
  15134. +/* -- Device registry. ------------------------------------------------------ */
  15135. +
  15136. +/*
  15137. + * SSAM device names follow the SSAM module alias, meaning they are prefixed
  15138. + * with 'ssam:', followed by domain, category, target ID, instance ID, and
  15139. + * function, each encoded as two-digit hexadecimal, separated by ':'. In other
  15140. + * words, it follows the scheme
  15141. + *
  15142. + * ssam:dd:cc:tt:ii:ff
  15143. + *
  15144. + * Where, 'dd', 'cc', 'tt', 'ii', and 'ff' are the two-digit hexadecimal
  15145. + * values mentioned above, respectively.
  15146. + */
  15147. +
  15148. +/* Root node. */
  15149. +static const struct software_node ssam_node_root = {
  15150. + .name = "ssam_platform_hub",
  15151. +};
  15152. +
  15153. +/* Base device hub (devices attached to Surface Book 3 base). */
  15154. +static const struct software_node ssam_node_hub_base = {
  15155. + .name = "ssam:00:00:02:00:00",
  15156. + .parent = &ssam_node_root,
  15157. +};
  15158. +
  15159. +/* AC adapter. */
  15160. +static const struct software_node ssam_node_bat_ac = {
  15161. + .name = "ssam:01:02:01:01:01",
  15162. + .parent = &ssam_node_root,
  15163. +};
  15164. +
  15165. +/* Primary battery. */
  15166. +static const struct software_node ssam_node_bat_main = {
  15167. + .name = "ssam:01:02:01:01:00",
  15168. + .parent = &ssam_node_root,
  15169. +};
  15170. +
  15171. +/* Secondary battery (Surface Book 3). */
  15172. +static const struct software_node ssam_node_bat_sb3base = {
  15173. + .name = "ssam:01:02:02:01:00",
  15174. + .parent = &ssam_node_hub_base,
  15175. +};
  15176. +
  15177. +/* Platform profile / performance-mode device. */
  15178. +static const struct software_node ssam_node_tmp_pprof = {
  15179. + .name = "ssam:01:03:01:00:01",
  15180. + .parent = &ssam_node_root,
  15181. +};
  15182. +
  15183. +/* DTX / detachment-system device (Surface Book 3). */
  15184. +static const struct software_node ssam_node_bas_dtx = {
  15185. + .name = "ssam:01:11:01:00:00",
  15186. + .parent = &ssam_node_root,
  15187. +};
  15188. +
  15189. +/* HID keyboard. */
  15190. +static const struct software_node ssam_node_hid_main_keyboard = {
  15191. + .name = "ssam:01:15:02:01:00",
  15192. + .parent = &ssam_node_root,
  15193. +};
  15194. +
  15195. +/* HID touchpad. */
  15196. +static const struct software_node ssam_node_hid_main_touchpad = {
  15197. + .name = "ssam:01:15:02:03:00",
  15198. + .parent = &ssam_node_root,
  15199. +};
  15200. +
  15201. +/* HID device instance 5 (unknown HID device). */
  15202. +static const struct software_node ssam_node_hid_main_iid5 = {
  15203. + .name = "ssam:01:15:02:05:00",
  15204. + .parent = &ssam_node_root,
  15205. +};
  15206. +
  15207. +/* HID keyboard (base hub). */
  15208. +static const struct software_node ssam_node_hid_base_keyboard = {
  15209. + .name = "ssam:01:15:02:01:00",
  15210. + .parent = &ssam_node_hub_base,
  15211. +};
  15212. +
  15213. +/* HID touchpad (base hub). */
  15214. +static const struct software_node ssam_node_hid_base_touchpad = {
  15215. + .name = "ssam:01:15:02:03:00",
  15216. + .parent = &ssam_node_hub_base,
  15217. +};
  15218. +
  15219. +/* HID device instance 5 (unknown HID device, base hub). */
  15220. +static const struct software_node ssam_node_hid_base_iid5 = {
  15221. + .name = "ssam:01:15:02:05:00",
  15222. + .parent = &ssam_node_hub_base,
  15223. +};
  15224. +
  15225. +/* HID device instance 6 (unknown HID device, base hub). */
  15226. +static const struct software_node ssam_node_hid_base_iid6 = {
  15227. + .name = "ssam:01:15:02:06:00",
  15228. + .parent = &ssam_node_hub_base,
  15229. +};
  15230. +
  15231. +/*
  15232. + * Devices for 5th- and 6th-generations models:
  15233. + * - Surface Book 2,
  15234. + * - Surface Laptop 1 and 2,
  15235. + * - Surface Pro 5 and 6.
  15236. + */
  15237. +static const struct software_node *ssam_node_group_gen5[] = {
  15238. + &ssam_node_root,
  15239. + &ssam_node_tmp_pprof,
  15240. + NULL,
  15241. +};
  15242. +
  15243. +/* Devices for Surface Book 3. */
  15244. +static const struct software_node *ssam_node_group_sb3[] = {
  15245. + &ssam_node_root,
  15246. + &ssam_node_hub_base,
  15247. + &ssam_node_bat_ac,
  15248. + &ssam_node_bat_main,
  15249. + &ssam_node_bat_sb3base,
  15250. + &ssam_node_tmp_pprof,
  15251. + &ssam_node_bas_dtx,
  15252. + &ssam_node_hid_base_keyboard,
  15253. + &ssam_node_hid_base_touchpad,
  15254. + &ssam_node_hid_base_iid5,
  15255. + &ssam_node_hid_base_iid6,
  15256. + NULL,
  15257. +};
  15258. +
  15259. +/* Devices for Surface Laptop 3 and 4. */
  15260. +static const struct software_node *ssam_node_group_sl3[] = {
  15261. + &ssam_node_root,
  15262. + &ssam_node_bat_ac,
  15263. + &ssam_node_bat_main,
  15264. + &ssam_node_tmp_pprof,
  15265. + &ssam_node_hid_main_keyboard,
  15266. + &ssam_node_hid_main_touchpad,
  15267. + &ssam_node_hid_main_iid5,
  15268. + NULL,
  15269. +};
  15270. +
  15271. +/* Devices for Surface Laptop Go. */
  15272. +static const struct software_node *ssam_node_group_slg1[] = {
  15273. + &ssam_node_root,
  15274. + &ssam_node_bat_ac,
  15275. + &ssam_node_bat_main,
  15276. + &ssam_node_tmp_pprof,
  15277. + NULL,
  15278. +};
  15279. +
  15280. +/* Devices for Surface Pro 7 and Surface Pro 7+. */
  15281. +static const struct software_node *ssam_node_group_sp7[] = {
  15282. + &ssam_node_root,
  15283. + &ssam_node_bat_ac,
  15284. + &ssam_node_bat_main,
  15285. + &ssam_node_tmp_pprof,
  15286. + NULL,
  15287. +};
  15288. +
  15289. +
  15290. +/* -- Device registry helper functions. ------------------------------------- */
  15291. +
  15292. +static int ssam_uid_from_string(const char *str, struct ssam_device_uid *uid)
  15293. +{
  15294. + u8 d, tc, tid, iid, fn;
  15295. + int n;
  15296. +
  15297. + n = sscanf(str, "ssam:%hhx:%hhx:%hhx:%hhx:%hhx", &d, &tc, &tid, &iid, &fn);
  15298. + if (n != 5)
  15299. + return -EINVAL;
  15300. +
  15301. + uid->domain = d;
  15302. + uid->category = tc;
  15303. + uid->target = tid;
  15304. + uid->instance = iid;
  15305. + uid->function = fn;
  15306. +
  15307. + return 0;
  15308. +}
  15309. +
  15310. +static int ssam_hub_remove_devices_fn(struct device *dev, void *data)
  15311. +{
  15312. + if (!is_ssam_device(dev))
  15313. + return 0;
  15314. +
  15315. + ssam_device_remove(to_ssam_device(dev));
  15316. + return 0;
  15317. +}
  15318. +
  15319. +static void ssam_hub_remove_devices(struct device *parent)
  15320. +{
  15321. + device_for_each_child_reverse(parent, NULL, ssam_hub_remove_devices_fn);
  15322. +}
  15323. +
  15324. +static int ssam_hub_add_device(struct device *parent, struct ssam_controller *ctrl,
  15325. + struct fwnode_handle *node)
  15326. +{
  15327. + struct ssam_device_uid uid;
  15328. + struct ssam_device *sdev;
  15329. + int status;
  15330. +
  15331. + status = ssam_uid_from_string(fwnode_get_name(node), &uid);
  15332. + if (status)
  15333. + return status;
  15334. +
  15335. + sdev = ssam_device_alloc(ctrl, uid);
  15336. + if (!sdev)
  15337. + return -ENOMEM;
  15338. +
  15339. + sdev->dev.parent = parent;
  15340. + sdev->dev.fwnode = node;
  15341. +
  15342. + status = ssam_device_add(sdev);
  15343. + if (status)
  15344. + ssam_device_put(sdev);
  15345. +
  15346. + return status;
  15347. +}
  15348. +
  15349. +static int ssam_hub_add_devices(struct device *parent, struct ssam_controller *ctrl,
  15350. + struct fwnode_handle *node)
  15351. +{
  15352. + struct fwnode_handle *child;
  15353. + int status;
  15354. +
  15355. + fwnode_for_each_child_node(node, child) {
  15356. + /*
  15357. + * Try to add the device specified in the firmware node. If
  15358. + * this fails with -EINVAL, the node does not specify any SSAM
  15359. + * device, so ignore it and continue with the next one.
  15360. + */
  15361. +
  15362. + status = ssam_hub_add_device(parent, ctrl, child);
  15363. + if (status && status != -EINVAL)
  15364. + goto err;
  15365. + }
  15366. +
  15367. + return 0;
  15368. +err:
  15369. + ssam_hub_remove_devices(parent);
  15370. + return status;
  15371. +}
  15372. +
  15373. +
  15374. +/* -- SSAM base-hub driver. ------------------------------------------------- */
  15375. +
  15376. +/*
  15377. + * Some devices (especially battery) may need a bit of time to be fully usable
  15378. + * after being (re-)connected. This delay has been determined via
  15379. + * experimentation.
  15380. + */
  15381. +#define SSAM_BASE_UPDATE_CONNECT_DELAY msecs_to_jiffies(2500)
  15382. +
  15383. +enum ssam_base_hub_state {
  15384. + SSAM_BASE_HUB_UNINITIALIZED,
  15385. + SSAM_BASE_HUB_CONNECTED,
  15386. + SSAM_BASE_HUB_DISCONNECTED,
  15387. +};
  15388. +
  15389. +struct ssam_base_hub {
  15390. + struct ssam_device *sdev;
  15391. +
  15392. + enum ssam_base_hub_state state;
  15393. + struct delayed_work update_work;
  15394. +
  15395. + struct ssam_event_notifier notif;
  15396. +};
  15397. +
  15398. +SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_query_opmode, u8, {
  15399. + .target_category = SSAM_SSH_TC_BAS,
  15400. + .target_id = 0x01,
  15401. + .command_id = 0x0d,
  15402. + .instance_id = 0x00,
  15403. +});
  15404. +
  15405. +#define SSAM_BAS_OPMODE_TABLET 0x00
  15406. +#define SSAM_EVENT_BAS_CID_CONNECTION 0x0c
  15407. +
  15408. +static int ssam_base_hub_query_state(struct ssam_base_hub *hub, enum ssam_base_hub_state *state)
  15409. +{
  15410. + u8 opmode;
  15411. + int status;
  15412. +
  15413. + status = ssam_retry(ssam_bas_query_opmode, hub->sdev->ctrl, &opmode);
  15414. + if (status < 0) {
  15415. + dev_err(&hub->sdev->dev, "failed to query base state: %d\n", status);
  15416. + return status;
  15417. + }
  15418. +
  15419. + if (opmode != SSAM_BAS_OPMODE_TABLET)
  15420. + *state = SSAM_BASE_HUB_CONNECTED;
  15421. + else
  15422. + *state = SSAM_BASE_HUB_DISCONNECTED;
  15423. +
  15424. + return 0;
  15425. +}
  15426. +
  15427. +static ssize_t ssam_base_hub_state_show(struct device *dev, struct device_attribute *attr,
  15428. + char *buf)
  15429. +{
  15430. + struct ssam_base_hub *hub = dev_get_drvdata(dev);
  15431. + bool connected = hub->state == SSAM_BASE_HUB_CONNECTED;
  15432. +
  15433. + return sysfs_emit(buf, "%d\n", connected);
  15434. +}
  15435. +
  15436. +static struct device_attribute ssam_base_hub_attr_state =
  15437. + __ATTR(state, 0444, ssam_base_hub_state_show, NULL);
  15438. +
  15439. +static struct attribute *ssam_base_hub_attrs[] = {
  15440. + &ssam_base_hub_attr_state.attr,
  15441. + NULL,
  15442. +};
  15443. +
  15444. +static const struct attribute_group ssam_base_hub_group = {
  15445. + .attrs = ssam_base_hub_attrs,
  15446. +};
  15447. +
  15448. +static void ssam_base_hub_update_workfn(struct work_struct *work)
  15449. +{
  15450. + struct ssam_base_hub *hub = container_of(work, struct ssam_base_hub, update_work.work);
  15451. + struct fwnode_handle *node = dev_fwnode(&hub->sdev->dev);
  15452. + enum ssam_base_hub_state state;
  15453. + int status = 0;
  15454. +
  15455. + status = ssam_base_hub_query_state(hub, &state);
  15456. + if (status)
  15457. + return;
  15458. +
  15459. + if (hub->state == state)
  15460. + return;
  15461. + hub->state = state;
  15462. +
  15463. + if (hub->state == SSAM_BASE_HUB_CONNECTED)
  15464. + status = ssam_hub_add_devices(&hub->sdev->dev, hub->sdev->ctrl, node);
  15465. + else
  15466. + ssam_hub_remove_devices(&hub->sdev->dev);
  15467. +
  15468. + if (status)
  15469. + dev_err(&hub->sdev->dev, "failed to update base-hub devices: %d\n", status);
  15470. +}
  15471. +
  15472. +static u32 ssam_base_hub_notif(struct ssam_event_notifier *nf, const struct ssam_event *event)
  15473. +{
  15474. + struct ssam_base_hub *hub = container_of(nf, struct ssam_base_hub, notif);
  15475. + unsigned long delay;
  15476. +
  15477. + if (event->command_id != SSAM_EVENT_BAS_CID_CONNECTION)
  15478. + return 0;
  15479. +
  15480. + if (event->length < 1) {
  15481. + dev_err(&hub->sdev->dev, "unexpected payload size: %u\n", event->length);
  15482. + return 0;
  15483. + }
  15484. +
  15485. + /*
  15486. + * Delay update when the base is being connected to give devices/EC
  15487. + * some time to set up.
  15488. + */
  15489. + delay = event->data[0] ? SSAM_BASE_UPDATE_CONNECT_DELAY : 0;
  15490. +
  15491. + schedule_delayed_work(&hub->update_work, delay);
  15492. +
  15493. + /*
  15494. + * Do not return SSAM_NOTIF_HANDLED: The event should be picked up and
  15495. + * consumed by the detachment system driver. We're just a (more or less)
  15496. + * silent observer.
  15497. + */
  15498. + return 0;
  15499. +}
  15500. +
  15501. +static int __maybe_unused ssam_base_hub_resume(struct device *dev)
  15502. +{
  15503. + struct ssam_base_hub *hub = dev_get_drvdata(dev);
  15504. +
  15505. + schedule_delayed_work(&hub->update_work, 0);
  15506. + return 0;
  15507. +}
  15508. +static SIMPLE_DEV_PM_OPS(ssam_base_hub_pm_ops, NULL, ssam_base_hub_resume);
  15509. +
  15510. +static int ssam_base_hub_probe(struct ssam_device *sdev)
  15511. +{
  15512. + struct ssam_base_hub *hub;
  15513. + int status;
  15514. +
  15515. + hub = devm_kzalloc(&sdev->dev, sizeof(*hub), GFP_KERNEL);
  15516. + if (!hub)
  15517. + return -ENOMEM;
  15518. +
  15519. + hub->sdev = sdev;
  15520. + hub->state = SSAM_BASE_HUB_UNINITIALIZED;
  15521. +
  15522. + hub->notif.base.priority = INT_MAX; /* This notifier should run first. */
  15523. + hub->notif.base.fn = ssam_base_hub_notif;
  15524. + hub->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
  15525. + hub->notif.event.id.target_category = SSAM_SSH_TC_BAS,
  15526. + hub->notif.event.id.instance = 0,
  15527. + hub->notif.event.mask = SSAM_EVENT_MASK_NONE;
  15528. + hub->notif.event.flags = SSAM_EVENT_SEQUENCED;
  15529. +
  15530. + INIT_DELAYED_WORK(&hub->update_work, ssam_base_hub_update_workfn);
  15531. +
  15532. + ssam_device_set_drvdata(sdev, hub);
  15533. +
  15534. + status = ssam_notifier_register(sdev->ctrl, &hub->notif);
  15535. + if (status)
  15536. + return status;
  15537. +
  15538. + status = sysfs_create_group(&sdev->dev.kobj, &ssam_base_hub_group);
  15539. + if (status)
  15540. + goto err;
  15541. +
  15542. + schedule_delayed_work(&hub->update_work, 0);
  15543. + return 0;
  15544. +
  15545. +err:
  15546. + ssam_notifier_unregister(sdev->ctrl, &hub->notif);
  15547. + cancel_delayed_work_sync(&hub->update_work);
  15548. + ssam_hub_remove_devices(&sdev->dev);
  15549. + return status;
  15550. +}
  15551. +
  15552. +static void ssam_base_hub_remove(struct ssam_device *sdev)
  15553. +{
  15554. + struct ssam_base_hub *hub = ssam_device_get_drvdata(sdev);
  15555. +
  15556. + sysfs_remove_group(&sdev->dev.kobj, &ssam_base_hub_group);
  15557. +
  15558. + ssam_notifier_unregister(sdev->ctrl, &hub->notif);
  15559. + cancel_delayed_work_sync(&hub->update_work);
  15560. + ssam_hub_remove_devices(&sdev->dev);
  15561. +}
  15562. +
  15563. +static const struct ssam_device_id ssam_base_hub_match[] = {
  15564. + { SSAM_VDEV(HUB, 0x02, SSAM_ANY_IID, 0x00) },
  15565. + { },
  15566. +};
  15567. +
  15568. +static struct ssam_device_driver ssam_base_hub_driver = {
  15569. + .probe = ssam_base_hub_probe,
  15570. + .remove = ssam_base_hub_remove,
  15571. + .match_table = ssam_base_hub_match,
  15572. + .driver = {
  15573. + .name = "surface_aggregator_base_hub",
  15574. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  15575. + .pm = &ssam_base_hub_pm_ops,
  15576. + },
  15577. +};
  15578. +
  15579. +
  15580. +/* -- SSAM platform/meta-hub driver. ---------------------------------------- */
  15581. +
  15582. +static const struct acpi_device_id ssam_platform_hub_match[] = {
  15583. + /* Surface Pro 4, 5, and 6 (OMBR < 0x10) */
  15584. + { "MSHW0081", (unsigned long)ssam_node_group_gen5 },
  15585. +
  15586. + /* Surface Pro 6 (OMBR >= 0x10) */
  15587. + { "MSHW0111", (unsigned long)ssam_node_group_gen5 },
  15588. +
  15589. + /* Surface Pro 7 */
  15590. + { "MSHW0116", (unsigned long)ssam_node_group_sp7 },
  15591. +
  15592. + /* Surface Pro 7+ */
  15593. + { "MSHW0119", (unsigned long)ssam_node_group_sp7 },
  15594. +
  15595. + /* Surface Book 2 */
  15596. + { "MSHW0107", (unsigned long)ssam_node_group_gen5 },
  15597. +
  15598. + /* Surface Book 3 */
  15599. + { "MSHW0117", (unsigned long)ssam_node_group_sb3 },
  15600. +
  15601. + /* Surface Laptop 1 */
  15602. + { "MSHW0086", (unsigned long)ssam_node_group_gen5 },
  15603. +
  15604. + /* Surface Laptop 2 */
  15605. + { "MSHW0112", (unsigned long)ssam_node_group_gen5 },
  15606. +
  15607. + /* Surface Laptop 3 (13", Intel) */
  15608. + { "MSHW0114", (unsigned long)ssam_node_group_sl3 },
  15609. +
  15610. + /* Surface Laptop 3 (15", AMD) and 4 (15", AMD) */
  15611. + { "MSHW0110", (unsigned long)ssam_node_group_sl3 },
  15612. +
  15613. + /* Surface Laptop 4 (13", Intel) */
  15614. + { "MSHW0250", (unsigned long)ssam_node_group_sl3 },
  15615. +
  15616. + /* Surface Laptop Go 1 */
  15617. + { "MSHW0118", (unsigned long)ssam_node_group_slg1 },
  15618. +
  15619. + { },
  15620. +};
  15621. +MODULE_DEVICE_TABLE(acpi, ssam_platform_hub_match);
  15622. +
  15623. +static int ssam_platform_hub_probe(struct platform_device *pdev)
  15624. +{
  15625. + const struct software_node **nodes;
  15626. + struct ssam_controller *ctrl;
  15627. + struct fwnode_handle *root;
  15628. + int status;
  15629. +
  15630. + nodes = (const struct software_node **)acpi_device_get_match_data(&pdev->dev);
  15631. + if (!nodes)
  15632. + return -ENODEV;
  15633. +
  15634. + /*
  15635. + * As we're adding the SSAM client devices as children under this device
  15636. + * and not the SSAM controller, we need to add a device link to the
  15637. + * controller to ensure that we remove all of our devices before the
  15638. + * controller is removed. This also guarantees proper ordering for
  15639. + * suspend/resume of the devices on this hub.
  15640. + */
  15641. + ctrl = ssam_client_bind(&pdev->dev);
  15642. + if (IS_ERR(ctrl))
  15643. + return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
  15644. +
  15645. + status = software_node_register_node_group(nodes);
  15646. + if (status)
  15647. + return status;
  15648. +
  15649. + root = software_node_fwnode(&ssam_node_root);
  15650. + if (!root) {
  15651. + software_node_unregister_node_group(nodes);
  15652. + return -ENOENT;
  15653. + }
  15654. +
  15655. + set_secondary_fwnode(&pdev->dev, root);
  15656. +
  15657. + status = ssam_hub_add_devices(&pdev->dev, ctrl, root);
  15658. + if (status) {
  15659. + set_secondary_fwnode(&pdev->dev, NULL);
  15660. + software_node_unregister_node_group(nodes);
  15661. + }
  15662. +
  15663. + platform_set_drvdata(pdev, nodes);
  15664. + return status;
  15665. +}
  15666. +
  15667. +static int ssam_platform_hub_remove(struct platform_device *pdev)
  15668. +{
  15669. + const struct software_node **nodes = platform_get_drvdata(pdev);
  15670. +
  15671. + ssam_hub_remove_devices(&pdev->dev);
  15672. + set_secondary_fwnode(&pdev->dev, NULL);
  15673. + software_node_unregister_node_group(nodes);
  15674. + return 0;
  15675. +}
  15676. +
  15677. +static struct platform_driver ssam_platform_hub_driver = {
  15678. + .probe = ssam_platform_hub_probe,
  15679. + .remove = ssam_platform_hub_remove,
  15680. + .driver = {
  15681. + .name = "surface_aggregator_platform_hub",
  15682. + .acpi_match_table = ssam_platform_hub_match,
  15683. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  15684. + },
  15685. +};
  15686. +
  15687. +
  15688. +/* -- Module initialization. ------------------------------------------------ */
  15689. +
  15690. +static int __init ssam_device_hub_init(void)
  15691. +{
  15692. + int status;
  15693. +
  15694. + status = platform_driver_register(&ssam_platform_hub_driver);
  15695. + if (status)
  15696. + return status;
  15697. +
  15698. + status = ssam_device_driver_register(&ssam_base_hub_driver);
  15699. + if (status)
  15700. + platform_driver_unregister(&ssam_platform_hub_driver);
  15701. +
  15702. + return status;
  15703. +}
  15704. +module_init(ssam_device_hub_init);
  15705. +
  15706. +static void __exit ssam_device_hub_exit(void)
  15707. +{
  15708. + ssam_device_driver_unregister(&ssam_base_hub_driver);
  15709. + platform_driver_unregister(&ssam_platform_hub_driver);
  15710. +}
  15711. +module_exit(ssam_device_hub_exit);
  15712. +
  15713. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  15714. +MODULE_DESCRIPTION("Device-registry for Surface System Aggregator Module");
  15715. +MODULE_LICENSE("GPL");
  15716. diff --git a/drivers/platform/x86/surface_dtx.c b/drivers/platform/x86/surface_dtx.c
  15717. new file mode 100644
  15718. index 000000000000..f486fabf9319
  15719. --- /dev/null
  15720. +++ b/drivers/platform/x86/surface_dtx.c
  15721. @@ -0,0 +1,1283 @@
  15722. +// SPDX-License-Identifier: GPL-2.0+
  15723. +/*
  15724. + * Surface Book (gen. 2 and later) detachment system (DTX) driver.
  15725. + *
  15726. + * Provides a user-space interface to properly handle clipboard/tablet
  15727. + * (containing screen and processor) detachment from the base of the device
  15728. + * (containing the keyboard and optionally a discrete GPU). Allows to
  15729. + * acknowledge (to speed things up), abort (e.g. in case the dGPU is still in
  15730. + * use), or request detachment via user-space.
  15731. + *
  15732. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  15733. + */
  15734. +
  15735. +#include <linux/fs.h>
  15736. +#include <linux/input.h>
  15737. +#include <linux/ioctl.h>
  15738. +#include <linux/kernel.h>
  15739. +#include <linux/kfifo.h>
  15740. +#include <linux/kref.h>
  15741. +#include <linux/miscdevice.h>
  15742. +#include <linux/module.h>
  15743. +#include <linux/mutex.h>
  15744. +#include <linux/platform_device.h>
  15745. +#include <linux/poll.h>
  15746. +#include <linux/rwsem.h>
  15747. +#include <linux/slab.h>
  15748. +#include <linux/workqueue.h>
  15749. +
  15750. +#include <linux/surface_aggregator/controller.h>
  15751. +#include <linux/surface_aggregator/device.h>
  15752. +#include <linux/surface_aggregator/dtx.h>
  15753. +
  15754. +
  15755. +/* -- SSAM interface. ------------------------------------------------------- */
  15756. +
  15757. +enum sam_event_cid_bas {
  15758. + SAM_EVENT_CID_DTX_CONNECTION = 0x0c,
  15759. + SAM_EVENT_CID_DTX_REQUEST = 0x0e,
  15760. + SAM_EVENT_CID_DTX_CANCEL = 0x0f,
  15761. + SAM_EVENT_CID_DTX_LATCH_STATUS = 0x11,
  15762. +};
  15763. +
  15764. +enum ssam_bas_base_state {
  15765. + SSAM_BAS_BASE_STATE_DETACH_SUCCESS = 0x00,
  15766. + SSAM_BAS_BASE_STATE_ATTACHED = 0x01,
  15767. + SSAM_BAS_BASE_STATE_NOT_FEASIBLE = 0x02,
  15768. +};
  15769. +
  15770. +enum ssam_bas_latch_status {
  15771. + SSAM_BAS_LATCH_STATUS_CLOSED = 0x00,
  15772. + SSAM_BAS_LATCH_STATUS_OPENED = 0x01,
  15773. + SSAM_BAS_LATCH_STATUS_FAILED_TO_OPEN = 0x02,
  15774. + SSAM_BAS_LATCH_STATUS_FAILED_TO_REMAIN_OPEN = 0x03,
  15775. + SSAM_BAS_LATCH_STATUS_FAILED_TO_CLOSE = 0x04,
  15776. +};
  15777. +
  15778. +enum ssam_bas_cancel_reason {
  15779. + SSAM_BAS_CANCEL_REASON_NOT_FEASIBLE = 0x00, /* Low battery. */
  15780. + SSAM_BAS_CANCEL_REASON_TIMEOUT = 0x02,
  15781. + SSAM_BAS_CANCEL_REASON_FAILED_TO_OPEN = 0x03,
  15782. + SSAM_BAS_CANCEL_REASON_FAILED_TO_REMAIN_OPEN = 0x04,
  15783. + SSAM_BAS_CANCEL_REASON_FAILED_TO_CLOSE = 0x05,
  15784. +};
  15785. +
  15786. +struct ssam_bas_base_info {
  15787. + u8 state;
  15788. + u8 base_id;
  15789. +} __packed;
  15790. +
  15791. +static_assert(sizeof(struct ssam_bas_base_info) == 2);
  15792. +
  15793. +SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_lock, {
  15794. + .target_category = SSAM_SSH_TC_BAS,
  15795. + .target_id = 0x01,
  15796. + .command_id = 0x06,
  15797. + .instance_id = 0x00,
  15798. +});
  15799. +
  15800. +SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_unlock, {
  15801. + .target_category = SSAM_SSH_TC_BAS,
  15802. + .target_id = 0x01,
  15803. + .command_id = 0x07,
  15804. + .instance_id = 0x00,
  15805. +});
  15806. +
  15807. +SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_request, {
  15808. + .target_category = SSAM_SSH_TC_BAS,
  15809. + .target_id = 0x01,
  15810. + .command_id = 0x08,
  15811. + .instance_id = 0x00,
  15812. +});
  15813. +
  15814. +SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_confirm, {
  15815. + .target_category = SSAM_SSH_TC_BAS,
  15816. + .target_id = 0x01,
  15817. + .command_id = 0x09,
  15818. + .instance_id = 0x00,
  15819. +});
  15820. +
  15821. +SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_heartbeat, {
  15822. + .target_category = SSAM_SSH_TC_BAS,
  15823. + .target_id = 0x01,
  15824. + .command_id = 0x0a,
  15825. + .instance_id = 0x00,
  15826. +});
  15827. +
  15828. +SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_cancel, {
  15829. + .target_category = SSAM_SSH_TC_BAS,
  15830. + .target_id = 0x01,
  15831. + .command_id = 0x0b,
  15832. + .instance_id = 0x00,
  15833. +});
  15834. +
  15835. +SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_base, struct ssam_bas_base_info, {
  15836. + .target_category = SSAM_SSH_TC_BAS,
  15837. + .target_id = 0x01,
  15838. + .command_id = 0x0c,
  15839. + .instance_id = 0x00,
  15840. +});
  15841. +
  15842. +SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_device_mode, u8, {
  15843. + .target_category = SSAM_SSH_TC_BAS,
  15844. + .target_id = 0x01,
  15845. + .command_id = 0x0d,
  15846. + .instance_id = 0x00,
  15847. +});
  15848. +
  15849. +SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_latch_status, u8, {
  15850. + .target_category = SSAM_SSH_TC_BAS,
  15851. + .target_id = 0x01,
  15852. + .command_id = 0x11,
  15853. + .instance_id = 0x00,
  15854. +});
  15855. +
  15856. +
  15857. +/* -- Main structures. ------------------------------------------------------ */
  15858. +
  15859. +enum sdtx_device_state {
  15860. + SDTX_DEVICE_SHUTDOWN_BIT = BIT(0),
  15861. + SDTX_DEVICE_DIRTY_BASE_BIT = BIT(1),
  15862. + SDTX_DEVICE_DIRTY_MODE_BIT = BIT(2),
  15863. + SDTX_DEVICE_DIRTY_LATCH_BIT = BIT(3),
  15864. +};
  15865. +
  15866. +struct sdtx_device {
  15867. + struct kref kref;
  15868. + struct rw_semaphore lock; /* Guards device and controller reference. */
  15869. +
  15870. + struct device *dev;
  15871. + struct ssam_controller *ctrl;
  15872. + unsigned long flags;
  15873. +
  15874. + struct miscdevice mdev;
  15875. + wait_queue_head_t waitq;
  15876. + struct mutex write_lock; /* Guards order of events/notifications. */
  15877. + struct rw_semaphore client_lock; /* Guards client list. */
  15878. + struct list_head client_list;
  15879. +
  15880. + struct delayed_work state_work;
  15881. + struct {
  15882. + struct ssam_bas_base_info base;
  15883. + u8 device_mode;
  15884. + u8 latch_status;
  15885. + } state;
  15886. +
  15887. + struct delayed_work mode_work;
  15888. + struct input_dev *mode_switch;
  15889. +
  15890. + struct ssam_event_notifier notif;
  15891. +};
  15892. +
  15893. +enum sdtx_client_state {
  15894. + SDTX_CLIENT_EVENTS_ENABLED_BIT = BIT(0),
  15895. +};
  15896. +
  15897. +struct sdtx_client {
  15898. + struct sdtx_device *ddev;
  15899. + struct list_head node;
  15900. + unsigned long flags;
  15901. +
  15902. + struct fasync_struct *fasync;
  15903. +
  15904. + struct mutex read_lock; /* Guards FIFO buffer read access. */
  15905. + DECLARE_KFIFO(buffer, u8, 512);
  15906. +};
  15907. +
  15908. +static void __sdtx_device_release(struct kref *kref)
  15909. +{
  15910. + struct sdtx_device *ddev = container_of(kref, struct sdtx_device, kref);
  15911. +
  15912. + mutex_destroy(&ddev->write_lock);
  15913. + kfree(ddev);
  15914. +}
  15915. +
  15916. +static struct sdtx_device *sdtx_device_get(struct sdtx_device *ddev)
  15917. +{
  15918. + if (ddev)
  15919. + kref_get(&ddev->kref);
  15920. +
  15921. + return ddev;
  15922. +}
  15923. +
  15924. +static void sdtx_device_put(struct sdtx_device *ddev)
  15925. +{
  15926. + if (ddev)
  15927. + kref_put(&ddev->kref, __sdtx_device_release);
  15928. +}
  15929. +
  15930. +
  15931. +/* -- Firmware value translations. ------------------------------------------ */
  15932. +
  15933. +static u16 sdtx_translate_base_state(struct sdtx_device *ddev, u8 state)
  15934. +{
  15935. + switch (state) {
  15936. + case SSAM_BAS_BASE_STATE_ATTACHED:
  15937. + return SDTX_BASE_ATTACHED;
  15938. +
  15939. + case SSAM_BAS_BASE_STATE_DETACH_SUCCESS:
  15940. + return SDTX_BASE_DETACHED;
  15941. +
  15942. + case SSAM_BAS_BASE_STATE_NOT_FEASIBLE:
  15943. + return SDTX_DETACH_NOT_FEASIBLE;
  15944. +
  15945. + default:
  15946. + dev_err(ddev->dev, "unknown base state: %#04x\n", state);
  15947. + return SDTX_UNKNOWN(state);
  15948. + }
  15949. +}
  15950. +
  15951. +static u16 sdtx_translate_latch_status(struct sdtx_device *ddev, u8 status)
  15952. +{
  15953. + switch (status) {
  15954. + case SSAM_BAS_LATCH_STATUS_CLOSED:
  15955. + return SDTX_LATCH_CLOSED;
  15956. +
  15957. + case SSAM_BAS_LATCH_STATUS_OPENED:
  15958. + return SDTX_LATCH_OPENED;
  15959. +
  15960. + case SSAM_BAS_LATCH_STATUS_FAILED_TO_OPEN:
  15961. + return SDTX_ERR_FAILED_TO_OPEN;
  15962. +
  15963. + case SSAM_BAS_LATCH_STATUS_FAILED_TO_REMAIN_OPEN:
  15964. + return SDTX_ERR_FAILED_TO_REMAIN_OPEN;
  15965. +
  15966. + case SSAM_BAS_LATCH_STATUS_FAILED_TO_CLOSE:
  15967. + return SDTX_ERR_FAILED_TO_CLOSE;
  15968. +
  15969. + default:
  15970. + dev_err(ddev->dev, "unknown latch status: %#04x\n", status);
  15971. + return SDTX_UNKNOWN(status);
  15972. + }
  15973. +}
  15974. +
  15975. +static u16 sdtx_translate_cancel_reason(struct sdtx_device *ddev, u8 reason)
  15976. +{
  15977. + switch (reason) {
  15978. + case SSAM_BAS_CANCEL_REASON_NOT_FEASIBLE:
  15979. + return SDTX_DETACH_NOT_FEASIBLE;
  15980. +
  15981. + case SSAM_BAS_CANCEL_REASON_TIMEOUT:
  15982. + return SDTX_DETACH_TIMEDOUT;
  15983. +
  15984. + case SSAM_BAS_CANCEL_REASON_FAILED_TO_OPEN:
  15985. + return SDTX_ERR_FAILED_TO_OPEN;
  15986. +
  15987. + case SSAM_BAS_CANCEL_REASON_FAILED_TO_REMAIN_OPEN:
  15988. + return SDTX_ERR_FAILED_TO_REMAIN_OPEN;
  15989. +
  15990. + case SSAM_BAS_CANCEL_REASON_FAILED_TO_CLOSE:
  15991. + return SDTX_ERR_FAILED_TO_CLOSE;
  15992. +
  15993. + default:
  15994. + dev_err(ddev->dev, "unknown cancel reason: %#04x\n", reason);
  15995. + return SDTX_UNKNOWN(reason);
  15996. + }
  15997. +}
  15998. +
  15999. +
  16000. +/* -- IOCTLs. --------------------------------------------------------------- */
  16001. +
  16002. +static int sdtx_ioctl_get_base_info(struct sdtx_device *ddev,
  16003. + struct sdtx_base_info __user *buf)
  16004. +{
  16005. + struct ssam_bas_base_info raw;
  16006. + struct sdtx_base_info info;
  16007. + int status;
  16008. +
  16009. + lockdep_assert_held_read(&ddev->lock);
  16010. +
  16011. + status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &raw);
  16012. + if (status < 0)
  16013. + return status;
  16014. +
  16015. + info.state = sdtx_translate_base_state(ddev, raw.state);
  16016. + info.base_id = SDTX_BASE_TYPE_SSH(raw.base_id);
  16017. +
  16018. + if (copy_to_user(buf, &info, sizeof(info)))
  16019. + return -EFAULT;
  16020. +
  16021. + return 0;
  16022. +}
  16023. +
  16024. +static int sdtx_ioctl_get_device_mode(struct sdtx_device *ddev, u16 __user *buf)
  16025. +{
  16026. + u8 mode;
  16027. + int status;
  16028. +
  16029. + lockdep_assert_held_read(&ddev->lock);
  16030. +
  16031. + status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &mode);
  16032. + if (status < 0)
  16033. + return status;
  16034. +
  16035. + return put_user(mode, buf);
  16036. +}
  16037. +
  16038. +static int sdtx_ioctl_get_latch_status(struct sdtx_device *ddev, u16 __user *buf)
  16039. +{
  16040. + u8 latch;
  16041. + int status;
  16042. +
  16043. + lockdep_assert_held_read(&ddev->lock);
  16044. +
  16045. + status = ssam_retry(ssam_bas_get_latch_status, ddev->ctrl, &latch);
  16046. + if (status < 0)
  16047. + return status;
  16048. +
  16049. + return put_user(sdtx_translate_latch_status(ddev, latch), buf);
  16050. +}
  16051. +
  16052. +static long __surface_dtx_ioctl(struct sdtx_client *client, unsigned int cmd, unsigned long arg)
  16053. +{
  16054. + struct sdtx_device *ddev = client->ddev;
  16055. +
  16056. + lockdep_assert_held_read(&ddev->lock);
  16057. +
  16058. + switch (cmd) {
  16059. + case SDTX_IOCTL_EVENTS_ENABLE:
  16060. + set_bit(SDTX_CLIENT_EVENTS_ENABLED_BIT, &client->flags);
  16061. + return 0;
  16062. +
  16063. + case SDTX_IOCTL_EVENTS_DISABLE:
  16064. + clear_bit(SDTX_CLIENT_EVENTS_ENABLED_BIT, &client->flags);
  16065. + return 0;
  16066. +
  16067. + case SDTX_IOCTL_LATCH_LOCK:
  16068. + return ssam_retry(ssam_bas_latch_lock, ddev->ctrl);
  16069. +
  16070. + case SDTX_IOCTL_LATCH_UNLOCK:
  16071. + return ssam_retry(ssam_bas_latch_unlock, ddev->ctrl);
  16072. +
  16073. + case SDTX_IOCTL_LATCH_REQUEST:
  16074. + return ssam_retry(ssam_bas_latch_request, ddev->ctrl);
  16075. +
  16076. + case SDTX_IOCTL_LATCH_CONFIRM:
  16077. + return ssam_retry(ssam_bas_latch_confirm, ddev->ctrl);
  16078. +
  16079. + case SDTX_IOCTL_LATCH_HEARTBEAT:
  16080. + return ssam_retry(ssam_bas_latch_heartbeat, ddev->ctrl);
  16081. +
  16082. + case SDTX_IOCTL_LATCH_CANCEL:
  16083. + return ssam_retry(ssam_bas_latch_cancel, ddev->ctrl);
  16084. +
  16085. + case SDTX_IOCTL_GET_BASE_INFO:
  16086. + return sdtx_ioctl_get_base_info(ddev, (struct sdtx_base_info __user *)arg);
  16087. +
  16088. + case SDTX_IOCTL_GET_DEVICE_MODE:
  16089. + return sdtx_ioctl_get_device_mode(ddev, (u16 __user *)arg);
  16090. +
  16091. + case SDTX_IOCTL_GET_LATCH_STATUS:
  16092. + return sdtx_ioctl_get_latch_status(ddev, (u16 __user *)arg);
  16093. +
  16094. + default:
  16095. + return -EINVAL;
  16096. + }
  16097. +}
  16098. +
  16099. +static long surface_dtx_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  16100. +{
  16101. + struct sdtx_client *client = file->private_data;
  16102. + long status;
  16103. +
  16104. + if (down_read_killable(&client->ddev->lock))
  16105. + return -ERESTARTSYS;
  16106. +
  16107. + if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &client->ddev->flags)) {
  16108. + up_read(&client->ddev->lock);
  16109. + return -ENODEV;
  16110. + }
  16111. +
  16112. + status = __surface_dtx_ioctl(client, cmd, arg);
  16113. +
  16114. + up_read(&client->ddev->lock);
  16115. + return status;
  16116. +}
  16117. +
  16118. +
  16119. +/* -- File operations. ------------------------------------------------------ */
  16120. +
  16121. +static int surface_dtx_open(struct inode *inode, struct file *file)
  16122. +{
  16123. + struct sdtx_device *ddev = container_of(file->private_data, struct sdtx_device, mdev);
  16124. + struct sdtx_client *client;
  16125. +
  16126. + /* Initialize client. */
  16127. + client = kzalloc(sizeof(*client), GFP_KERNEL);
  16128. + if (!client)
  16129. + return -ENOMEM;
  16130. +
  16131. + client->ddev = sdtx_device_get(ddev);
  16132. +
  16133. + INIT_LIST_HEAD(&client->node);
  16134. +
  16135. + mutex_init(&client->read_lock);
  16136. + INIT_KFIFO(client->buffer);
  16137. +
  16138. + file->private_data = client;
  16139. +
  16140. + /* Attach client. */
  16141. + down_write(&ddev->client_lock);
  16142. +
  16143. + /*
  16144. + * Do not add a new client if the device has been shut down. Note that
  16145. + * it's enough to hold the client_lock here as, during shutdown, we
  16146. + * only acquire that lock and remove clients after marking the device
  16147. + * as shut down.
  16148. + */
  16149. + if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags)) {
  16150. + up_write(&ddev->client_lock);
  16151. + sdtx_device_put(client->ddev);
  16152. + kfree(client);
  16153. + return -ENODEV;
  16154. + }
  16155. +
  16156. + list_add_tail(&client->node, &ddev->client_list);
  16157. + up_write(&ddev->client_lock);
  16158. +
  16159. + stream_open(inode, file);
  16160. + return 0;
  16161. +}
  16162. +
  16163. +static int surface_dtx_release(struct inode *inode, struct file *file)
  16164. +{
  16165. + struct sdtx_client *client = file->private_data;
  16166. +
  16167. + /* Detach client. */
  16168. + down_write(&client->ddev->client_lock);
  16169. + list_del(&client->node);
  16170. + up_write(&client->ddev->client_lock);
  16171. +
  16172. + /* Free client. */
  16173. + sdtx_device_put(client->ddev);
  16174. + mutex_destroy(&client->read_lock);
  16175. + kfree(client);
  16176. +
  16177. + return 0;
  16178. +}
  16179. +
  16180. +static ssize_t surface_dtx_read(struct file *file, char __user *buf, size_t count, loff_t *offs)
  16181. +{
  16182. + struct sdtx_client *client = file->private_data;
  16183. + struct sdtx_device *ddev = client->ddev;
  16184. + unsigned int copied;
  16185. + int status = 0;
  16186. +
  16187. + if (down_read_killable(&ddev->lock))
  16188. + return -ERESTARTSYS;
  16189. +
  16190. + /* Make sure we're not shut down. */
  16191. + if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags)) {
  16192. + up_read(&ddev->lock);
  16193. + return -ENODEV;
  16194. + }
  16195. +
  16196. + do {
  16197. + /* Check availability, wait if necessary. */
  16198. + if (kfifo_is_empty(&client->buffer)) {
  16199. + up_read(&ddev->lock);
  16200. +
  16201. + if (file->f_flags & O_NONBLOCK)
  16202. + return -EAGAIN;
  16203. +
  16204. + status = wait_event_interruptible(ddev->waitq,
  16205. + !kfifo_is_empty(&client->buffer) ||
  16206. + test_bit(SDTX_DEVICE_SHUTDOWN_BIT,
  16207. + &ddev->flags));
  16208. + if (status < 0)
  16209. + return status;
  16210. +
  16211. + if (down_read_killable(&client->ddev->lock))
  16212. + return -ERESTARTSYS;
  16213. +
  16214. + /* Need to check that we're not shut down again. */
  16215. + if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags)) {
  16216. + up_read(&ddev->lock);
  16217. + return -ENODEV;
  16218. + }
  16219. + }
  16220. +
  16221. + /* Try to read from FIFO. */
  16222. + if (mutex_lock_interruptible(&client->read_lock)) {
  16223. + up_read(&ddev->lock);
  16224. + return -ERESTARTSYS;
  16225. + }
  16226. +
  16227. + status = kfifo_to_user(&client->buffer, buf, count, &copied);
  16228. + mutex_unlock(&client->read_lock);
  16229. +
  16230. + if (status < 0) {
  16231. + up_read(&ddev->lock);
  16232. + return status;
  16233. + }
  16234. +
  16235. + /* We might not have gotten anything, check this here. */
  16236. + if (copied == 0 && (file->f_flags & O_NONBLOCK)) {
  16237. + up_read(&ddev->lock);
  16238. + return -EAGAIN;
  16239. + }
  16240. + } while (copied == 0);
  16241. +
  16242. + up_read(&ddev->lock);
  16243. + return copied;
  16244. +}
  16245. +
  16246. +static __poll_t surface_dtx_poll(struct file *file, struct poll_table_struct *pt)
  16247. +{
  16248. + struct sdtx_client *client = file->private_data;
  16249. + __poll_t events = 0;
  16250. +
  16251. + if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &client->ddev->flags))
  16252. + return EPOLLHUP | EPOLLERR;
  16253. +
  16254. + poll_wait(file, &client->ddev->waitq, pt);
  16255. +
  16256. + if (!kfifo_is_empty(&client->buffer))
  16257. + events |= EPOLLIN | EPOLLRDNORM;
  16258. +
  16259. + return events;
  16260. +}
  16261. +
  16262. +static int surface_dtx_fasync(int fd, struct file *file, int on)
  16263. +{
  16264. + struct sdtx_client *client = file->private_data;
  16265. +
  16266. + return fasync_helper(fd, file, on, &client->fasync);
  16267. +}
  16268. +
  16269. +static const struct file_operations surface_dtx_fops = {
  16270. + .owner = THIS_MODULE,
  16271. + .open = surface_dtx_open,
  16272. + .release = surface_dtx_release,
  16273. + .read = surface_dtx_read,
  16274. + .poll = surface_dtx_poll,
  16275. + .fasync = surface_dtx_fasync,
  16276. + .unlocked_ioctl = surface_dtx_ioctl,
  16277. + .compat_ioctl = surface_dtx_ioctl,
  16278. + .llseek = no_llseek,
  16279. +};
  16280. +
  16281. +
  16282. +/* -- Event handling/forwarding. -------------------------------------------- */
  16283. +
  16284. +/*
  16285. + * The device operation mode is not immediately updated on the EC when the
  16286. + * base has been connected, i.e. querying the device mode inside the
  16287. + * connection event callback yields an outdated value. Thus, we can only
  16288. + * determine the new tablet-mode switch and device mode values after some
  16289. + * time.
  16290. + *
  16291. + * These delays have been chosen by experimenting. We first delay on connect
  16292. + * events, then check and validate the device mode against the base state and
  16293. + * if invalid delay again by the "recheck" delay.
  16294. + */
  16295. +#define SDTX_DEVICE_MODE_DELAY_CONNECT msecs_to_jiffies(100)
  16296. +#define SDTX_DEVICE_MODE_DELAY_RECHECK msecs_to_jiffies(100)
  16297. +
  16298. +struct sdtx_status_event {
  16299. + struct sdtx_event e;
  16300. + __u16 v;
  16301. +} __packed;
  16302. +
  16303. +struct sdtx_base_info_event {
  16304. + struct sdtx_event e;
  16305. + struct sdtx_base_info v;
  16306. +} __packed;
  16307. +
  16308. +union sdtx_generic_event {
  16309. + struct sdtx_event common;
  16310. + struct sdtx_status_event status;
  16311. + struct sdtx_base_info_event base;
  16312. +};
  16313. +
  16314. +static void sdtx_update_device_mode(struct sdtx_device *ddev, unsigned long delay);
  16315. +
  16316. +/* Must be executed with ddev->write_lock held. */
  16317. +static void sdtx_push_event(struct sdtx_device *ddev, struct sdtx_event *evt)
  16318. +{
  16319. + const size_t len = sizeof(struct sdtx_event) + evt->length;
  16320. + struct sdtx_client *client;
  16321. +
  16322. + lockdep_assert_held(&ddev->write_lock);
  16323. +
  16324. + down_read(&ddev->client_lock);
  16325. + list_for_each_entry(client, &ddev->client_list, node) {
  16326. + if (!test_bit(SDTX_CLIENT_EVENTS_ENABLED_BIT, &client->flags))
  16327. + continue;
  16328. +
  16329. + if (likely(kfifo_avail(&client->buffer) >= len))
  16330. + kfifo_in(&client->buffer, (const u8 *)evt, len);
  16331. + else
  16332. + dev_warn(ddev->dev, "event buffer overrun\n");
  16333. +
  16334. + kill_fasync(&client->fasync, SIGIO, POLL_IN);
  16335. + }
  16336. + up_read(&ddev->client_lock);
  16337. +
  16338. + wake_up_interruptible(&ddev->waitq);
  16339. +}
  16340. +
  16341. +static u32 sdtx_notifier(struct ssam_event_notifier *nf, const struct ssam_event *in)
  16342. +{
  16343. + struct sdtx_device *ddev = container_of(nf, struct sdtx_device, notif);
  16344. + union sdtx_generic_event event;
  16345. + size_t len;
  16346. +
  16347. + /* Validate event payload length. */
  16348. + switch (in->command_id) {
  16349. + case SAM_EVENT_CID_DTX_CONNECTION:
  16350. + len = 2 * sizeof(u8);
  16351. + break;
  16352. +
  16353. + case SAM_EVENT_CID_DTX_REQUEST:
  16354. + len = 0;
  16355. + break;
  16356. +
  16357. + case SAM_EVENT_CID_DTX_CANCEL:
  16358. + len = sizeof(u8);
  16359. + break;
  16360. +
  16361. + case SAM_EVENT_CID_DTX_LATCH_STATUS:
  16362. + len = sizeof(u8);
  16363. + break;
  16364. +
  16365. + default:
  16366. + return 0;
  16367. + }
  16368. +
  16369. + if (in->length != len) {
  16370. + dev_err(ddev->dev,
  16371. + "unexpected payload size for event %#04x: got %u, expected %zu\n",
  16372. + in->command_id, in->length, len);
  16373. + return 0;
  16374. + }
  16375. +
  16376. + mutex_lock(&ddev->write_lock);
  16377. +
  16378. + /* Translate event. */
  16379. + switch (in->command_id) {
  16380. + case SAM_EVENT_CID_DTX_CONNECTION:
  16381. + clear_bit(SDTX_DEVICE_DIRTY_BASE_BIT, &ddev->flags);
  16382. +
  16383. + /* If state has not changed: do not send new event. */
  16384. + if (ddev->state.base.state == in->data[0] &&
  16385. + ddev->state.base.base_id == in->data[1])
  16386. + goto out;
  16387. +
  16388. + ddev->state.base.state = in->data[0];
  16389. + ddev->state.base.base_id = in->data[1];
  16390. +
  16391. + event.base.e.length = sizeof(struct sdtx_base_info);
  16392. + event.base.e.code = SDTX_EVENT_BASE_CONNECTION;
  16393. + event.base.v.state = sdtx_translate_base_state(ddev, in->data[0]);
  16394. + event.base.v.base_id = SDTX_BASE_TYPE_SSH(in->data[1]);
  16395. + break;
  16396. +
  16397. + case SAM_EVENT_CID_DTX_REQUEST:
  16398. + event.common.code = SDTX_EVENT_REQUEST;
  16399. + event.common.length = 0;
  16400. + break;
  16401. +
  16402. + case SAM_EVENT_CID_DTX_CANCEL:
  16403. + event.status.e.length = sizeof(u16);
  16404. + event.status.e.code = SDTX_EVENT_CANCEL;
  16405. + event.status.v = sdtx_translate_cancel_reason(ddev, in->data[0]);
  16406. + break;
  16407. +
  16408. + case SAM_EVENT_CID_DTX_LATCH_STATUS:
  16409. + clear_bit(SDTX_DEVICE_DIRTY_LATCH_BIT, &ddev->flags);
  16410. +
  16411. + /* If state has not changed: do not send new event. */
  16412. + if (ddev->state.latch_status == in->data[0])
  16413. + goto out;
  16414. +
  16415. + ddev->state.latch_status = in->data[0];
  16416. +
  16417. + event.status.e.length = sizeof(u16);
  16418. + event.status.e.code = SDTX_EVENT_LATCH_STATUS;
  16419. + event.status.v = sdtx_translate_latch_status(ddev, in->data[0]);
  16420. + break;
  16421. + }
  16422. +
  16423. + sdtx_push_event(ddev, &event.common);
  16424. +
  16425. + /* Update device mode on base connection change. */
  16426. + if (in->command_id == SAM_EVENT_CID_DTX_CONNECTION) {
  16427. + unsigned long delay;
  16428. +
  16429. + delay = in->data[0] ? SDTX_DEVICE_MODE_DELAY_CONNECT : 0;
  16430. + sdtx_update_device_mode(ddev, delay);
  16431. + }
  16432. +
  16433. +out:
  16434. + mutex_unlock(&ddev->write_lock);
  16435. + return SSAM_NOTIF_HANDLED;
  16436. +}
  16437. +
  16438. +
  16439. +/* -- State update functions. ----------------------------------------------- */
  16440. +
  16441. +static bool sdtx_device_mode_invalid(u8 mode, u8 base_state)
  16442. +{
  16443. + return ((base_state == SSAM_BAS_BASE_STATE_ATTACHED) &&
  16444. + (mode == SDTX_DEVICE_MODE_TABLET)) ||
  16445. + ((base_state == SSAM_BAS_BASE_STATE_DETACH_SUCCESS) &&
  16446. + (mode != SDTX_DEVICE_MODE_TABLET));
  16447. +}
  16448. +
  16449. +static void sdtx_device_mode_workfn(struct work_struct *work)
  16450. +{
  16451. + struct sdtx_device *ddev = container_of(work, struct sdtx_device, mode_work.work);
  16452. + struct sdtx_status_event event;
  16453. + struct ssam_bas_base_info base;
  16454. + int status, tablet;
  16455. + u8 mode;
  16456. +
  16457. + /* Get operation mode. */
  16458. + status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &mode);
  16459. + if (status) {
  16460. + dev_err(ddev->dev, "failed to get device mode: %d\n", status);
  16461. + return;
  16462. + }
  16463. +
  16464. + /* Get base info. */
  16465. + status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &base);
  16466. + if (status) {
  16467. + dev_err(ddev->dev, "failed to get base info: %d\n", status);
  16468. + return;
  16469. + }
  16470. +
  16471. + /*
  16472. + * In some cases (specifically when attaching the base), the device
  16473. + * mode isn't updated right away. Thus we check if the device mode
  16474. + * makes sense for the given base state and try again later if it
  16475. + * doesn't.
  16476. + */
  16477. + if (sdtx_device_mode_invalid(mode, base.state)) {
  16478. + dev_dbg(ddev->dev, "device mode is invalid, trying again\n");
  16479. + sdtx_update_device_mode(ddev, SDTX_DEVICE_MODE_DELAY_RECHECK);
  16480. + return;
  16481. + }
  16482. +
  16483. + mutex_lock(&ddev->write_lock);
  16484. + clear_bit(SDTX_DEVICE_DIRTY_MODE_BIT, &ddev->flags);
  16485. +
  16486. + /* Avoid sending duplicate device-mode events. */
  16487. + if (ddev->state.device_mode == mode) {
  16488. + mutex_unlock(&ddev->write_lock);
  16489. + return;
  16490. + }
  16491. +
  16492. + ddev->state.device_mode = mode;
  16493. +
  16494. + event.e.length = sizeof(u16);
  16495. + event.e.code = SDTX_EVENT_DEVICE_MODE;
  16496. + event.v = mode;
  16497. +
  16498. + sdtx_push_event(ddev, &event.e);
  16499. +
  16500. + /* Send SW_TABLET_MODE event. */
  16501. + tablet = mode != SDTX_DEVICE_MODE_LAPTOP;
  16502. + input_report_switch(ddev->mode_switch, SW_TABLET_MODE, tablet);
  16503. + input_sync(ddev->mode_switch);
  16504. +
  16505. + mutex_unlock(&ddev->write_lock);
  16506. +}
  16507. +
  16508. +static void sdtx_update_device_mode(struct sdtx_device *ddev, unsigned long delay)
  16509. +{
  16510. + schedule_delayed_work(&ddev->mode_work, delay);
  16511. +}
  16512. +
  16513. +/* Must be executed with ddev->write_lock held. */
  16514. +static void __sdtx_device_state_update_base(struct sdtx_device *ddev,
  16515. + struct ssam_bas_base_info info)
  16516. +{
  16517. + struct sdtx_base_info_event event;
  16518. +
  16519. + lockdep_assert_held(&ddev->write_lock);
  16520. +
  16521. + /* Prevent duplicate events. */
  16522. + if (ddev->state.base.state == info.state &&
  16523. + ddev->state.base.base_id == info.base_id)
  16524. + return;
  16525. +
  16526. + ddev->state.base = info;
  16527. +
  16528. + event.e.length = sizeof(struct sdtx_base_info);
  16529. + event.e.code = SDTX_EVENT_BASE_CONNECTION;
  16530. + event.v.state = sdtx_translate_base_state(ddev, info.state);
  16531. + event.v.base_id = SDTX_BASE_TYPE_SSH(info.base_id);
  16532. +
  16533. + sdtx_push_event(ddev, &event.e);
  16534. +}
  16535. +
  16536. +/* Must be executed with ddev->write_lock held. */
  16537. +static void __sdtx_device_state_update_mode(struct sdtx_device *ddev, u8 mode)
  16538. +{
  16539. + struct sdtx_status_event event;
  16540. + int tablet;
  16541. +
  16542. + /*
  16543. + * Note: This function must be called after updating the base state
  16544. + * via __sdtx_device_state_update_base(), as we rely on the updated
  16545. + * base state value in the validity check below.
  16546. + */
  16547. +
  16548. + lockdep_assert_held(&ddev->write_lock);
  16549. +
  16550. + if (sdtx_device_mode_invalid(mode, ddev->state.base.state)) {
  16551. + dev_dbg(ddev->dev, "device mode is invalid, trying again\n");
  16552. + sdtx_update_device_mode(ddev, SDTX_DEVICE_MODE_DELAY_RECHECK);
  16553. + return;
  16554. + }
  16555. +
  16556. + /* Prevent duplicate events. */
  16557. + if (ddev->state.device_mode == mode)
  16558. + return;
  16559. +
  16560. + ddev->state.device_mode = mode;
  16561. +
  16562. + /* Send event. */
  16563. + event.e.length = sizeof(u16);
  16564. + event.e.code = SDTX_EVENT_DEVICE_MODE;
  16565. + event.v = mode;
  16566. +
  16567. + sdtx_push_event(ddev, &event.e);
  16568. +
  16569. + /* Send SW_TABLET_MODE event. */
  16570. + tablet = mode != SDTX_DEVICE_MODE_LAPTOP;
  16571. + input_report_switch(ddev->mode_switch, SW_TABLET_MODE, tablet);
  16572. + input_sync(ddev->mode_switch);
  16573. +}
  16574. +
  16575. +/* Must be executed with ddev->write_lock held. */
  16576. +static void __sdtx_device_state_update_latch(struct sdtx_device *ddev, u8 status)
  16577. +{
  16578. + struct sdtx_status_event event;
  16579. +
  16580. + lockdep_assert_held(&ddev->write_lock);
  16581. +
  16582. + /* Prevent duplicate events. */
  16583. + if (ddev->state.latch_status == status)
  16584. + return;
  16585. +
  16586. + ddev->state.latch_status = status;
  16587. +
  16588. + event.e.length = sizeof(struct sdtx_base_info);
  16589. + event.e.code = SDTX_EVENT_BASE_CONNECTION;
  16590. + event.v = sdtx_translate_latch_status(ddev, status);
  16591. +
  16592. + sdtx_push_event(ddev, &event.e);
  16593. +}
  16594. +
  16595. +static void sdtx_device_state_workfn(struct work_struct *work)
  16596. +{
  16597. + struct sdtx_device *ddev = container_of(work, struct sdtx_device, state_work.work);
  16598. + struct ssam_bas_base_info base;
  16599. + u8 mode, latch;
  16600. + int status;
  16601. +
  16602. + /* Mark everything as dirty. */
  16603. + set_bit(SDTX_DEVICE_DIRTY_BASE_BIT, &ddev->flags);
  16604. + set_bit(SDTX_DEVICE_DIRTY_MODE_BIT, &ddev->flags);
  16605. + set_bit(SDTX_DEVICE_DIRTY_LATCH_BIT, &ddev->flags);
  16606. +
  16607. + /*
  16608. + * Ensure that the state gets marked as dirty before continuing to
  16609. + * query it. Necessary to ensure that clear_bit() calls in
  16610. + * sdtx_notifier() and sdtx_device_mode_workfn() actually clear these
  16611. + * bits if an event is received while updating the state here.
  16612. + */
  16613. + smp_mb__after_atomic();
  16614. +
  16615. + status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &base);
  16616. + if (status) {
  16617. + dev_err(ddev->dev, "failed to get base state: %d\n", status);
  16618. + return;
  16619. + }
  16620. +
  16621. + status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &mode);
  16622. + if (status) {
  16623. + dev_err(ddev->dev, "failed to get device mode: %d\n", status);
  16624. + return;
  16625. + }
  16626. +
  16627. + status = ssam_retry(ssam_bas_get_latch_status, ddev->ctrl, &latch);
  16628. + if (status) {
  16629. + dev_err(ddev->dev, "failed to get latch status: %d\n", status);
  16630. + return;
  16631. + }
  16632. +
  16633. + mutex_lock(&ddev->write_lock);
  16634. +
  16635. + /*
  16636. + * If the respective dirty-bit has been cleared, an event has been
  16637. + * received, updating this state. The queried state may thus be out of
  16638. + * date. At this point, we can safely assume that the state provided
  16639. + * by the event is either up to date, or we're about to receive
  16640. + * another event updating it.
  16641. + */
  16642. +
  16643. + if (test_and_clear_bit(SDTX_DEVICE_DIRTY_BASE_BIT, &ddev->flags))
  16644. + __sdtx_device_state_update_base(ddev, base);
  16645. +
  16646. + if (test_and_clear_bit(SDTX_DEVICE_DIRTY_MODE_BIT, &ddev->flags))
  16647. + __sdtx_device_state_update_mode(ddev, mode);
  16648. +
  16649. + if (test_and_clear_bit(SDTX_DEVICE_DIRTY_LATCH_BIT, &ddev->flags))
  16650. + __sdtx_device_state_update_latch(ddev, latch);
  16651. +
  16652. + mutex_unlock(&ddev->write_lock);
  16653. +}
  16654. +
  16655. +static void sdtx_update_device_state(struct sdtx_device *ddev, unsigned long delay)
  16656. +{
  16657. + schedule_delayed_work(&ddev->state_work, delay);
  16658. +}
  16659. +
  16660. +
  16661. +/* -- Common device initialization. ----------------------------------------- */
  16662. +
  16663. +static int sdtx_device_init(struct sdtx_device *ddev, struct device *dev,
  16664. + struct ssam_controller *ctrl)
  16665. +{
  16666. + int status, tablet_mode;
  16667. +
  16668. + /* Basic initialization. */
  16669. + kref_init(&ddev->kref);
  16670. + init_rwsem(&ddev->lock);
  16671. + ddev->dev = dev;
  16672. + ddev->ctrl = ctrl;
  16673. +
  16674. + ddev->mdev.minor = MISC_DYNAMIC_MINOR;
  16675. + ddev->mdev.name = "surface_dtx";
  16676. + ddev->mdev.nodename = "surface/dtx";
  16677. + ddev->mdev.fops = &surface_dtx_fops;
  16678. +
  16679. + ddev->notif.base.priority = 1;
  16680. + ddev->notif.base.fn = sdtx_notifier;
  16681. + ddev->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
  16682. + ddev->notif.event.id.target_category = SSAM_SSH_TC_BAS;
  16683. + ddev->notif.event.id.instance = 0;
  16684. + ddev->notif.event.mask = SSAM_EVENT_MASK_NONE;
  16685. + ddev->notif.event.flags = SSAM_EVENT_SEQUENCED;
  16686. +
  16687. + init_waitqueue_head(&ddev->waitq);
  16688. + mutex_init(&ddev->write_lock);
  16689. + init_rwsem(&ddev->client_lock);
  16690. + INIT_LIST_HEAD(&ddev->client_list);
  16691. +
  16692. + INIT_DELAYED_WORK(&ddev->mode_work, sdtx_device_mode_workfn);
  16693. + INIT_DELAYED_WORK(&ddev->state_work, sdtx_device_state_workfn);
  16694. +
  16695. + /*
  16696. + * Get current device state. We want to guarantee that events are only
  16697. + * sent when state actually changes. Thus we cannot use special
  16698. + * "uninitialized" values, as that would cause problems when manually
  16699. + * querying the state in surface_dtx_pm_complete(). I.e. we would not
  16700. + * be able to detect state changes there if no change event has been
  16701. + * received between driver initialization and first device suspension.
  16702. + *
  16703. + * Note that we also need to do this before registering the event
  16704. + * notifier, as that may access the state values.
  16705. + */
  16706. + status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &ddev->state.base);
  16707. + if (status)
  16708. + return status;
  16709. +
  16710. + status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &ddev->state.device_mode);
  16711. + if (status)
  16712. + return status;
  16713. +
  16714. + status = ssam_retry(ssam_bas_get_latch_status, ddev->ctrl, &ddev->state.latch_status);
  16715. + if (status)
  16716. + return status;
  16717. +
  16718. + /* Set up tablet mode switch. */
  16719. + ddev->mode_switch = input_allocate_device();
  16720. + if (!ddev->mode_switch)
  16721. + return -ENOMEM;
  16722. +
  16723. + ddev->mode_switch->name = "Microsoft Surface DTX Device Mode Switch";
  16724. + ddev->mode_switch->phys = "ssam/01:11:01:00:00/input0";
  16725. + ddev->mode_switch->id.bustype = BUS_HOST;
  16726. + ddev->mode_switch->dev.parent = ddev->dev;
  16727. +
  16728. + tablet_mode = (ddev->state.device_mode != SDTX_DEVICE_MODE_LAPTOP);
  16729. + input_set_capability(ddev->mode_switch, EV_SW, SW_TABLET_MODE);
  16730. + input_report_switch(ddev->mode_switch, SW_TABLET_MODE, tablet_mode);
  16731. +
  16732. + status = input_register_device(ddev->mode_switch);
  16733. + if (status) {
  16734. + input_free_device(ddev->mode_switch);
  16735. + return status;
  16736. + }
  16737. +
  16738. + /* Set up event notifier. */
  16739. + status = ssam_notifier_register(ddev->ctrl, &ddev->notif);
  16740. + if (status)
  16741. + goto err_notif;
  16742. +
  16743. + /* Register miscdevice. */
  16744. + status = misc_register(&ddev->mdev);
  16745. + if (status)
  16746. + goto err_mdev;
  16747. +
  16748. + /*
  16749. + * Update device state in case it has changed between getting the
  16750. + * initial mode and registering the event notifier.
  16751. + */
  16752. + sdtx_update_device_state(ddev, 0);
  16753. + return 0;
  16754. +
  16755. +err_notif:
  16756. + ssam_notifier_unregister(ddev->ctrl, &ddev->notif);
  16757. + cancel_delayed_work_sync(&ddev->mode_work);
  16758. +err_mdev:
  16759. + input_unregister_device(ddev->mode_switch);
  16760. + return status;
  16761. +}
  16762. +
  16763. +static struct sdtx_device *sdtx_device_create(struct device *dev, struct ssam_controller *ctrl)
  16764. +{
  16765. + struct sdtx_device *ddev;
  16766. + int status;
  16767. +
  16768. + ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
  16769. + if (!ddev)
  16770. + return ERR_PTR(-ENOMEM);
  16771. +
  16772. + status = sdtx_device_init(ddev, dev, ctrl);
  16773. + if (status) {
  16774. + sdtx_device_put(ddev);
  16775. + return ERR_PTR(status);
  16776. + }
  16777. +
  16778. + return ddev;
  16779. +}
  16780. +
  16781. +static void sdtx_device_destroy(struct sdtx_device *ddev)
  16782. +{
  16783. + struct sdtx_client *client;
  16784. +
  16785. + /*
  16786. + * Mark device as shut-down. Prevent new clients from being added and
  16787. + * new operations from being executed.
  16788. + */
  16789. + set_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags);
  16790. +
  16791. + /* Disable notifiers, prevent new events from arriving. */
  16792. + ssam_notifier_unregister(ddev->ctrl, &ddev->notif);
  16793. +
  16794. + /* Stop mode_work, prevent access to mode_switch. */
  16795. + cancel_delayed_work_sync(&ddev->mode_work);
  16796. +
  16797. + /* Stop state_work. */
  16798. + cancel_delayed_work_sync(&ddev->state_work);
  16799. +
  16800. + /* With mode_work canceled, we can unregister the mode_switch. */
  16801. + input_unregister_device(ddev->mode_switch);
  16802. +
  16803. + /* Wake up async clients. */
  16804. + down_write(&ddev->client_lock);
  16805. + list_for_each_entry(client, &ddev->client_list, node) {
  16806. + kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  16807. + }
  16808. + up_write(&ddev->client_lock);
  16809. +
  16810. + /* Wake up blocking clients. */
  16811. + wake_up_interruptible(&ddev->waitq);
  16812. +
  16813. + /*
  16814. + * Wait for clients to finish their current operation. After this, the
  16815. + * controller and device references are guaranteed to be no longer in
  16816. + * use.
  16817. + */
  16818. + down_write(&ddev->lock);
  16819. + ddev->dev = NULL;
  16820. + ddev->ctrl = NULL;
  16821. + up_write(&ddev->lock);
  16822. +
  16823. + /* Finally remove the misc-device. */
  16824. + misc_deregister(&ddev->mdev);
  16825. +
  16826. + /*
  16827. + * We're now guaranteed that sdtx_device_open() won't be called any
  16828. + * more, so we can now drop out reference.
  16829. + */
  16830. + sdtx_device_put(ddev);
  16831. +}
  16832. +
  16833. +
  16834. +/* -- PM ops. --------------------------------------------------------------- */
  16835. +
  16836. +#ifdef CONFIG_PM_SLEEP
  16837. +
  16838. +static void surface_dtx_pm_complete(struct device *dev)
  16839. +{
  16840. + struct sdtx_device *ddev = dev_get_drvdata(dev);
  16841. +
  16842. + /*
  16843. + * Normally, the EC will store events while suspended (i.e. in
  16844. + * display-off state) and release them when resumed (i.e. transitioned
  16845. + * to display-on state). During hibernation, however, the EC will be
  16846. + * shut down and does not store events. Furthermore, events might be
  16847. + * dropped during prolonged suspension (it is currently unknown how
  16848. + * big this event buffer is and how it behaves on overruns).
  16849. + *
  16850. + * To prevent any problems, we update the device state here. We do
  16851. + * this delayed to ensure that any events sent by the EC directly
  16852. + * after resuming will be handled first. The delay below has been
  16853. + * chosen (experimentally), so that there should be ample time for
  16854. + * these events to be handled, before we check and, if necessary,
  16855. + * update the state.
  16856. + */
  16857. + sdtx_update_device_state(ddev, msecs_to_jiffies(1000));
  16858. +}
  16859. +
  16860. +static const struct dev_pm_ops surface_dtx_pm_ops = {
  16861. + .complete = surface_dtx_pm_complete,
  16862. +};
  16863. +
  16864. +#else /* CONFIG_PM_SLEEP */
  16865. +
  16866. +static const struct dev_pm_ops surface_dtx_pm_ops = {};
  16867. +
  16868. +#endif /* CONFIG_PM_SLEEP */
  16869. +
  16870. +
  16871. +/* -- Platform driver. ------------------------------------------------------ */
  16872. +
  16873. +static int surface_dtx_platform_probe(struct platform_device *pdev)
  16874. +{
  16875. + struct ssam_controller *ctrl;
  16876. + struct sdtx_device *ddev;
  16877. +
  16878. + /* Link to EC. */
  16879. + ctrl = ssam_client_bind(&pdev->dev);
  16880. + if (IS_ERR(ctrl))
  16881. + return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
  16882. +
  16883. + ddev = sdtx_device_create(&pdev->dev, ctrl);
  16884. + if (IS_ERR(ddev))
  16885. + return PTR_ERR(ddev);
  16886. +
  16887. + platform_set_drvdata(pdev, ddev);
  16888. + return 0;
  16889. +}
  16890. +
  16891. +static int surface_dtx_platform_remove(struct platform_device *pdev)
  16892. +{
  16893. + sdtx_device_destroy(platform_get_drvdata(pdev));
  16894. + return 0;
  16895. +}
  16896. +
  16897. +static const struct acpi_device_id surface_dtx_acpi_match[] = {
  16898. + { "MSHW0133", 0 },
  16899. + { },
  16900. +};
  16901. +MODULE_DEVICE_TABLE(acpi, surface_dtx_acpi_match);
  16902. +
  16903. +static struct platform_driver surface_dtx_platform_driver = {
  16904. + .probe = surface_dtx_platform_probe,
  16905. + .remove = surface_dtx_platform_remove,
  16906. + .driver = {
  16907. + .name = "surface_dtx_pltf",
  16908. + .acpi_match_table = surface_dtx_acpi_match,
  16909. + .pm = &surface_dtx_pm_ops,
  16910. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  16911. + },
  16912. +};
  16913. +
  16914. +
  16915. +/* -- SSAM device driver. --------------------------------------------------- */
  16916. +
  16917. +#ifdef CONFIG_SURFACE_AGGREGATOR_BUS
  16918. +
  16919. +static int surface_dtx_ssam_probe(struct ssam_device *sdev)
  16920. +{
  16921. + struct sdtx_device *ddev;
  16922. +
  16923. + ddev = sdtx_device_create(&sdev->dev, sdev->ctrl);
  16924. + if (IS_ERR(ddev))
  16925. + return PTR_ERR(ddev);
  16926. +
  16927. + ssam_device_set_drvdata(sdev, ddev);
  16928. + return 0;
  16929. +}
  16930. +
  16931. +static void surface_dtx_ssam_remove(struct ssam_device *sdev)
  16932. +{
  16933. + sdtx_device_destroy(ssam_device_get_drvdata(sdev));
  16934. +}
  16935. +
  16936. +static const struct ssam_device_id surface_dtx_ssam_match[] = {
  16937. + { SSAM_SDEV(BAS, 0x01, 0x00, 0x00) },
  16938. + { },
  16939. +};
  16940. +MODULE_DEVICE_TABLE(ssam, surface_dtx_ssam_match);
  16941. +
  16942. +static struct ssam_device_driver surface_dtx_ssam_driver = {
  16943. + .probe = surface_dtx_ssam_probe,
  16944. + .remove = surface_dtx_ssam_remove,
  16945. + .match_table = surface_dtx_ssam_match,
  16946. + .driver = {
  16947. + .name = "surface_dtx",
  16948. + .pm = &surface_dtx_pm_ops,
  16949. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  16950. + },
  16951. +};
  16952. +
  16953. +static int ssam_dtx_driver_register(void)
  16954. +{
  16955. + return ssam_device_driver_register(&surface_dtx_ssam_driver);
  16956. +}
  16957. +
  16958. +static void ssam_dtx_driver_unregister(void)
  16959. +{
  16960. + ssam_device_driver_unregister(&surface_dtx_ssam_driver);
  16961. +}
  16962. +
  16963. +#else /* CONFIG_SURFACE_AGGREGATOR_BUS */
  16964. +
  16965. +static int ssam_dtx_driver_register(void)
  16966. +{
  16967. + return 0;
  16968. +}
  16969. +
  16970. +static void ssam_dtx_driver_unregister(void)
  16971. +{
  16972. +}
  16973. +
  16974. +#endif /* CONFIG_SURFACE_AGGREGATOR_BUS */
  16975. +
  16976. +
  16977. +/* -- Module setup. --------------------------------------------------------- */
  16978. +
  16979. +static int __init surface_dtx_init(void)
  16980. +{
  16981. + int status;
  16982. +
  16983. + status = ssam_dtx_driver_register();
  16984. + if (status)
  16985. + return status;
  16986. +
  16987. + status = platform_driver_register(&surface_dtx_platform_driver);
  16988. + if (status)
  16989. + ssam_dtx_driver_unregister();
  16990. +
  16991. + return status;
  16992. +}
  16993. +module_init(surface_dtx_init);
  16994. +
  16995. +static void __exit surface_dtx_exit(void)
  16996. +{
  16997. + platform_driver_unregister(&surface_dtx_platform_driver);
  16998. + ssam_dtx_driver_unregister();
  16999. +}
  17000. +module_exit(surface_dtx_exit);
  17001. +
  17002. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  17003. +MODULE_DESCRIPTION("Detachment-system driver for Surface System Aggregator Module");
  17004. +MODULE_LICENSE("GPL");
  17005. diff --git a/drivers/platform/x86/surface_perfmode.c b/drivers/platform/x86/surface_perfmode.c
  17006. new file mode 100644
  17007. index 000000000000..a9114e001d0d
  17008. --- /dev/null
  17009. +++ b/drivers/platform/x86/surface_perfmode.c
  17010. @@ -0,0 +1,122 @@
  17011. +// SPDX-License-Identifier: GPL-2.0+
  17012. +/*
  17013. + * Surface performance-mode driver.
  17014. + *
  17015. + * Provides a user-space interface for the performance mode control provided
  17016. + * by the Surface System Aggregator Module (SSAM), influencing cooling
  17017. + * behavior of the device and potentially managing power limits.
  17018. + *
  17019. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  17020. + */
  17021. +
  17022. +#include <asm/unaligned.h>
  17023. +#include <linux/kernel.h>
  17024. +#include <linux/module.h>
  17025. +#include <linux/sysfs.h>
  17026. +#include <linux/types.h>
  17027. +
  17028. +#include <linux/surface_aggregator/device.h>
  17029. +
  17030. +enum sam_perf_mode {
  17031. + SAM_PERF_MODE_NORMAL = 1,
  17032. + SAM_PERF_MODE_BATTERY = 2,
  17033. + SAM_PERF_MODE_PERF1 = 3,
  17034. + SAM_PERF_MODE_PERF2 = 4,
  17035. +
  17036. + __SAM_PERF_MODE__MIN = 1,
  17037. + __SAM_PERF_MODE__MAX = 4,
  17038. +};
  17039. +
  17040. +struct ssam_perf_info {
  17041. + __le32 mode;
  17042. + __le16 unknown1;
  17043. + __le16 unknown2;
  17044. +} __packed;
  17045. +
  17046. +SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_tmp_perf_mode_get, struct ssam_perf_info, {
  17047. + .target_category = SSAM_SSH_TC_TMP,
  17048. + .command_id = 0x02,
  17049. +});
  17050. +
  17051. +SSAM_DEFINE_SYNC_REQUEST_CL_W(__ssam_tmp_perf_mode_set, __le32, {
  17052. + .target_category = SSAM_SSH_TC_TMP,
  17053. + .command_id = 0x03,
  17054. +});
  17055. +
  17056. +static int ssam_tmp_perf_mode_set(struct ssam_device *sdev, u32 mode)
  17057. +{
  17058. + __le32 mode_le = cpu_to_le32(mode);
  17059. +
  17060. + if (mode < __SAM_PERF_MODE__MIN || mode > __SAM_PERF_MODE__MAX)
  17061. + return -EINVAL;
  17062. +
  17063. + return ssam_retry(__ssam_tmp_perf_mode_set, sdev, &mode_le);
  17064. +}
  17065. +
  17066. +static ssize_t perf_mode_show(struct device *dev, struct device_attribute *attr,
  17067. + char *data)
  17068. +{
  17069. + struct ssam_device *sdev = to_ssam_device(dev);
  17070. + struct ssam_perf_info info;
  17071. + int status;
  17072. +
  17073. + status = ssam_retry(ssam_tmp_perf_mode_get, sdev, &info);
  17074. + if (status) {
  17075. + dev_err(dev, "failed to get current performance mode: %d\n",
  17076. + status);
  17077. + return -EIO;
  17078. + }
  17079. +
  17080. + return sprintf(data, "%d\n", le32_to_cpu(info.mode));
  17081. +}
  17082. +
  17083. +static ssize_t perf_mode_store(struct device *dev, struct device_attribute *attr,
  17084. + const char *data, size_t count)
  17085. +{
  17086. + struct ssam_device *sdev = to_ssam_device(dev);
  17087. + int perf_mode;
  17088. + int status;
  17089. +
  17090. + status = kstrtoint(data, 0, &perf_mode);
  17091. + if (status < 0)
  17092. + return status;
  17093. +
  17094. + status = ssam_tmp_perf_mode_set(sdev, perf_mode);
  17095. + if (status < 0)
  17096. + return status;
  17097. +
  17098. + return count;
  17099. +}
  17100. +
  17101. +static const DEVICE_ATTR_RW(perf_mode);
  17102. +
  17103. +static int surface_sam_sid_perfmode_probe(struct ssam_device *sdev)
  17104. +{
  17105. + return sysfs_create_file(&sdev->dev.kobj, &dev_attr_perf_mode.attr);
  17106. +}
  17107. +
  17108. +static void surface_sam_sid_perfmode_remove(struct ssam_device *sdev)
  17109. +{
  17110. + sysfs_remove_file(&sdev->dev.kobj, &dev_attr_perf_mode.attr);
  17111. +}
  17112. +
  17113. +static const struct ssam_device_id ssam_perfmode_match[] = {
  17114. + { SSAM_SDEV(TMP, 0x01, 0x00, 0x01) },
  17115. + { },
  17116. +};
  17117. +MODULE_DEVICE_TABLE(ssam, ssam_perfmode_match);
  17118. +
  17119. +static struct ssam_device_driver surface_sam_sid_perfmode = {
  17120. + .probe = surface_sam_sid_perfmode_probe,
  17121. + .remove = surface_sam_sid_perfmode_remove,
  17122. + .match_table = ssam_perfmode_match,
  17123. + .driver = {
  17124. + .name = "surface_performance_mode",
  17125. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  17126. + },
  17127. +};
  17128. +module_ssam_device_driver(surface_sam_sid_perfmode);
  17129. +
  17130. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  17131. +MODULE_DESCRIPTION("Performance mode interface for Surface System Aggregator Module");
  17132. +MODULE_LICENSE("GPL");
  17133. diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
  17134. index 0aa46b451017..e96e136c60b4 100644
  17135. --- a/drivers/power/supply/Kconfig
  17136. +++ b/drivers/power/supply/Kconfig
  17137. @@ -776,4 +776,36 @@ config RN5T618_POWER
  17138. This driver can also be built as a module. If so, the module will be
  17139. called rn5t618_power.
  17140. +config BATTERY_SURFACE
  17141. + tristate "Battery driver for 7th-generation Microsoft Surface devices"
  17142. + depends on SURFACE_AGGREGATOR_REGISTRY
  17143. + help
  17144. + Driver for battery devices connected via/managed by the Surface System
  17145. + Aggregator Module (SSAM).
  17146. +
  17147. + This driver provides battery-information and -status support for
  17148. + Surface devices where said data is not exposed via the standard ACPI
  17149. + devices. On those models (7th-generation), battery-information is
  17150. + instead handled directly via SSAM client devices and this driver.
  17151. +
  17152. + Say M or Y here to include battery status support for 7th-generation
  17153. + Microsoft Surface devices, i.e. Surface Pro 7, Surface Laptop 3,
  17154. + Surface Book 3, and Surface Laptop Go.
  17155. +
  17156. +config CHARGER_SURFACE
  17157. + tristate "AC driver for 7th-generation Microsoft Surface devices"
  17158. + depends on SURFACE_AGGREGATOR_REGISTRY
  17159. + help
  17160. + Driver for AC devices connected via/managed by the Surface System
  17161. + Aggregator Module (SSAM).
  17162. +
  17163. + This driver provides AC-information and -status support for Surface
  17164. + devices where said data is not exposed via the standard ACPI devices.
  17165. + On those models (7th-generation), AC-information is instead handled
  17166. + directly via a SSAM client device and this driver.
  17167. +
  17168. + Say M or Y here to include AC status support for 7th-generation
  17169. + Microsoft Surface devices, i.e. Surface Pro 7, Surface Laptop 3,
  17170. + Surface Book 3, and Surface Laptop Go.
  17171. +
  17172. endif # POWER_SUPPLY
  17173. diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
  17174. index dd4b86318cd9..9fdd34956153 100644
  17175. --- a/drivers/power/supply/Makefile
  17176. +++ b/drivers/power/supply/Makefile
  17177. @@ -98,3 +98,5 @@ obj-$(CONFIG_CHARGER_BD70528) += bd70528-charger.o
  17178. obj-$(CONFIG_CHARGER_BD99954) += bd99954-charger.o
  17179. obj-$(CONFIG_CHARGER_WILCO) += wilco-charger.o
  17180. obj-$(CONFIG_RN5T618_POWER) += rn5t618_power.o
  17181. +obj-$(CONFIG_BATTERY_SURFACE) += surface_battery.o
  17182. +obj-$(CONFIG_CHARGER_SURFACE) += surface_charger.o
  17183. diff --git a/drivers/power/supply/surface_battery.c b/drivers/power/supply/surface_battery.c
  17184. new file mode 100644
  17185. index 000000000000..5ec2e6bb2465
  17186. --- /dev/null
  17187. +++ b/drivers/power/supply/surface_battery.c
  17188. @@ -0,0 +1,875 @@
  17189. +// SPDX-License-Identifier: GPL-2.0+
  17190. +/*
  17191. + * Battery driver for 7th-generation Microsoft Surface devices via Surface
  17192. + * System Aggregator Module (SSAM).
  17193. + *
  17194. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  17195. + */
  17196. +
  17197. +#include <asm/unaligned.h>
  17198. +#include <linux/jiffies.h>
  17199. +#include <linux/kernel.h>
  17200. +#include <linux/module.h>
  17201. +#include <linux/mutex.h>
  17202. +#include <linux/power_supply.h>
  17203. +#include <linux/sysfs.h>
  17204. +#include <linux/types.h>
  17205. +#include <linux/workqueue.h>
  17206. +
  17207. +#include <linux/surface_aggregator/device.h>
  17208. +
  17209. +
  17210. +/* -- SAM interface. -------------------------------------------------------- */
  17211. +
  17212. +enum sam_event_cid_bat {
  17213. + SAM_EVENT_CID_BAT_BIX = 0x15,
  17214. + SAM_EVENT_CID_BAT_BST = 0x16,
  17215. + SAM_EVENT_CID_BAT_ADP = 0x17,
  17216. + SAM_EVENT_CID_BAT_PROT = 0x18,
  17217. + SAM_EVENT_CID_BAT_DPTF = 0x53,
  17218. +};
  17219. +
  17220. +enum sam_battery_sta {
  17221. + SAM_BATTERY_STA_OK = 0x0f,
  17222. + SAM_BATTERY_STA_PRESENT = 0x10,
  17223. +};
  17224. +
  17225. +enum sam_battery_state {
  17226. + SAM_BATTERY_STATE_DISCHARGING = BIT(0),
  17227. + SAM_BATTERY_STATE_CHARGING = BIT(1),
  17228. + SAM_BATTERY_STATE_CRITICAL = BIT(2),
  17229. +};
  17230. +
  17231. +enum sam_battery_power_unit {
  17232. + SAM_BATTERY_POWER_UNIT_mW = 0,
  17233. + SAM_BATTERY_POWER_UNIT_mA = 1,
  17234. +};
  17235. +
  17236. +/* Equivalent to data returned in ACPI _BIX method, revision 0. */
  17237. +struct spwr_bix {
  17238. + u8 revision;
  17239. + __le32 power_unit;
  17240. + __le32 design_cap;
  17241. + __le32 last_full_charge_cap;
  17242. + __le32 technology;
  17243. + __le32 design_voltage;
  17244. + __le32 design_cap_warn;
  17245. + __le32 design_cap_low;
  17246. + __le32 cycle_count;
  17247. + __le32 measurement_accuracy;
  17248. + __le32 max_sampling_time;
  17249. + __le32 min_sampling_time;
  17250. + __le32 max_avg_interval;
  17251. + __le32 min_avg_interval;
  17252. + __le32 bat_cap_granularity_1;
  17253. + __le32 bat_cap_granularity_2;
  17254. + __u8 model[21];
  17255. + __u8 serial[11];
  17256. + __u8 type[5];
  17257. + __u8 oem_info[21];
  17258. +} __packed;
  17259. +
  17260. +static_assert(sizeof(struct spwr_bix) == 119);
  17261. +
  17262. +/* Equivalent to data returned in ACPI _BST method. */
  17263. +struct spwr_bst {
  17264. + __le32 state;
  17265. + __le32 present_rate;
  17266. + __le32 remaining_cap;
  17267. + __le32 present_voltage;
  17268. +} __packed;
  17269. +
  17270. +static_assert(sizeof(struct spwr_bst) == 16);
  17271. +
  17272. +#define SPWR_BIX_REVISION 0
  17273. +#define SPWR_BATTERY_VALUE_UNKNOWN 0xffffffff
  17274. +
  17275. +/* Get battery status (_STA) */
  17276. +SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_sta, __le32, {
  17277. + .target_category = SSAM_SSH_TC_BAT,
  17278. + .command_id = 0x01,
  17279. +});
  17280. +
  17281. +/* Get battery static information (_BIX). */
  17282. +SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_bix, struct spwr_bix, {
  17283. + .target_category = SSAM_SSH_TC_BAT,
  17284. + .command_id = 0x02,
  17285. +});
  17286. +
  17287. +/* Get battery dynamic information (_BST). */
  17288. +SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_bst, struct spwr_bst, {
  17289. + .target_category = SSAM_SSH_TC_BAT,
  17290. + .command_id = 0x03,
  17291. +});
  17292. +
  17293. +/* Set battery trip point (_BTP). */
  17294. +SSAM_DEFINE_SYNC_REQUEST_CL_W(ssam_bat_set_btp, __le32, {
  17295. + .target_category = SSAM_SSH_TC_BAT,
  17296. + .command_id = 0x04,
  17297. +});
  17298. +
  17299. +
  17300. +/* -- Device structures. ---------------------------------------------------- */
  17301. +
  17302. +struct spwr_psy_properties {
  17303. + const char *name;
  17304. + struct ssam_event_registry registry;
  17305. +};
  17306. +
  17307. +struct spwr_battery_device {
  17308. + struct ssam_device *sdev;
  17309. +
  17310. + char name[32];
  17311. + struct power_supply *psy;
  17312. + struct power_supply_desc psy_desc;
  17313. +
  17314. + struct delayed_work update_work;
  17315. +
  17316. + struct ssam_event_notifier notif;
  17317. +
  17318. + struct mutex lock; /* Guards access to state data below. */
  17319. + unsigned long timestamp;
  17320. +
  17321. + __le32 sta;
  17322. + struct spwr_bix bix;
  17323. + struct spwr_bst bst;
  17324. + u32 alarm;
  17325. +};
  17326. +
  17327. +
  17328. +/* -- Module parameters. ---------------------------------------------------- */
  17329. +
  17330. +static unsigned int cache_time = 1000;
  17331. +module_param(cache_time, uint, 0644);
  17332. +MODULE_PARM_DESC(cache_time, "battery state caching time in milliseconds [default: 1000]");
  17333. +
  17334. +
  17335. +/* -- State management. ----------------------------------------------------- */
  17336. +
  17337. +/*
  17338. + * Delay for battery update quirk. See spwr_external_power_changed() below
  17339. + * for more details.
  17340. + */
  17341. +#define SPWR_AC_BAT_UPDATE_DELAY msecs_to_jiffies(5000)
  17342. +
  17343. +static bool spwr_battery_present(struct spwr_battery_device *bat)
  17344. +{
  17345. + lockdep_assert_held(&bat->lock);
  17346. +
  17347. + return le32_to_cpu(bat->sta) & SAM_BATTERY_STA_PRESENT;
  17348. +}
  17349. +
  17350. +static int spwr_battery_load_sta(struct spwr_battery_device *bat)
  17351. +{
  17352. + lockdep_assert_held(&bat->lock);
  17353. +
  17354. + return ssam_retry(ssam_bat_get_sta, bat->sdev, &bat->sta);
  17355. +}
  17356. +
  17357. +static int spwr_battery_load_bix(struct spwr_battery_device *bat)
  17358. +{
  17359. + int status;
  17360. +
  17361. + lockdep_assert_held(&bat->lock);
  17362. +
  17363. + if (!spwr_battery_present(bat))
  17364. + return 0;
  17365. +
  17366. + status = ssam_retry(ssam_bat_get_bix, bat->sdev, &bat->bix);
  17367. +
  17368. + /* Enforce NULL terminated strings in case anything goes wrong... */
  17369. + bat->bix.model[ARRAY_SIZE(bat->bix.model) - 1] = 0;
  17370. + bat->bix.serial[ARRAY_SIZE(bat->bix.serial) - 1] = 0;
  17371. + bat->bix.type[ARRAY_SIZE(bat->bix.type) - 1] = 0;
  17372. + bat->bix.oem_info[ARRAY_SIZE(bat->bix.oem_info) - 1] = 0;
  17373. +
  17374. + return status;
  17375. +}
  17376. +
  17377. +static int spwr_battery_load_bst(struct spwr_battery_device *bat)
  17378. +{
  17379. + lockdep_assert_held(&bat->lock);
  17380. +
  17381. + if (!spwr_battery_present(bat))
  17382. + return 0;
  17383. +
  17384. + return ssam_retry(ssam_bat_get_bst, bat->sdev, &bat->bst);
  17385. +}
  17386. +
  17387. +static int spwr_battery_set_alarm_unlocked(struct spwr_battery_device *bat, u32 value)
  17388. +{
  17389. + __le32 value_le = cpu_to_le32(value);
  17390. +
  17391. + lockdep_assert_held(&bat->lock);
  17392. +
  17393. + bat->alarm = value;
  17394. + return ssam_retry(ssam_bat_set_btp, bat->sdev, &value_le);
  17395. +}
  17396. +
  17397. +static int spwr_battery_update_bst_unlocked(struct spwr_battery_device *bat, bool cached)
  17398. +{
  17399. + unsigned long cache_deadline = bat->timestamp + msecs_to_jiffies(cache_time);
  17400. + int status;
  17401. +
  17402. + lockdep_assert_held(&bat->lock);
  17403. +
  17404. + if (cached && bat->timestamp && time_is_after_jiffies(cache_deadline))
  17405. + return 0;
  17406. +
  17407. + status = spwr_battery_load_sta(bat);
  17408. + if (status)
  17409. + return status;
  17410. +
  17411. + status = spwr_battery_load_bst(bat);
  17412. + if (status)
  17413. + return status;
  17414. +
  17415. + bat->timestamp = jiffies;
  17416. + return 0;
  17417. +}
  17418. +
  17419. +static int spwr_battery_update_bst(struct spwr_battery_device *bat, bool cached)
  17420. +{
  17421. + int status;
  17422. +
  17423. + mutex_lock(&bat->lock);
  17424. + status = spwr_battery_update_bst_unlocked(bat, cached);
  17425. + mutex_unlock(&bat->lock);
  17426. +
  17427. + return status;
  17428. +}
  17429. +
  17430. +static int spwr_battery_update_bix_unlocked(struct spwr_battery_device *bat)
  17431. +{
  17432. + int status;
  17433. +
  17434. + lockdep_assert_held(&bat->lock);
  17435. +
  17436. + status = spwr_battery_load_sta(bat);
  17437. + if (status)
  17438. + return status;
  17439. +
  17440. + status = spwr_battery_load_bix(bat);
  17441. + if (status)
  17442. + return status;
  17443. +
  17444. + status = spwr_battery_load_bst(bat);
  17445. + if (status)
  17446. + return status;
  17447. +
  17448. + if (bat->bix.revision != SPWR_BIX_REVISION)
  17449. + dev_warn(&bat->sdev->dev, "unsupported battery revision: %u\n", bat->bix.revision);
  17450. +
  17451. + bat->timestamp = jiffies;
  17452. + return 0;
  17453. +}
  17454. +
  17455. +static u32 sprw_battery_get_full_cap_safe(struct spwr_battery_device *bat)
  17456. +{
  17457. + u32 full_cap = get_unaligned_le32(&bat->bix.last_full_charge_cap);
  17458. +
  17459. + lockdep_assert_held(&bat->lock);
  17460. +
  17461. + if (full_cap == 0 || full_cap == SPWR_BATTERY_VALUE_UNKNOWN)
  17462. + full_cap = get_unaligned_le32(&bat->bix.design_cap);
  17463. +
  17464. + return full_cap;
  17465. +}
  17466. +
  17467. +static bool spwr_battery_is_full(struct spwr_battery_device *bat)
  17468. +{
  17469. + u32 state = get_unaligned_le32(&bat->bst.state);
  17470. + u32 full_cap = sprw_battery_get_full_cap_safe(bat);
  17471. + u32 remaining_cap = get_unaligned_le32(&bat->bst.remaining_cap);
  17472. +
  17473. + lockdep_assert_held(&bat->lock);
  17474. +
  17475. + return full_cap != SPWR_BATTERY_VALUE_UNKNOWN && full_cap != 0 &&
  17476. + remaining_cap != SPWR_BATTERY_VALUE_UNKNOWN &&
  17477. + remaining_cap >= full_cap &&
  17478. + state == 0;
  17479. +}
  17480. +
  17481. +static int spwr_battery_recheck_full(struct spwr_battery_device *bat)
  17482. +{
  17483. + bool present;
  17484. + u32 unit;
  17485. + int status;
  17486. +
  17487. + mutex_lock(&bat->lock);
  17488. + unit = get_unaligned_le32(&bat->bix.power_unit);
  17489. + present = spwr_battery_present(bat);
  17490. +
  17491. + status = spwr_battery_update_bix_unlocked(bat);
  17492. + if (status)
  17493. + goto out;
  17494. +
  17495. + /* If battery has been attached, (re-)initialize alarm. */
  17496. + if (!present && spwr_battery_present(bat)) {
  17497. + u32 cap_warn = get_unaligned_le32(&bat->bix.design_cap_warn);
  17498. +
  17499. + status = spwr_battery_set_alarm_unlocked(bat, cap_warn);
  17500. + if (status)
  17501. + goto out;
  17502. + }
  17503. +
  17504. + /*
  17505. + * Warn if the unit has changed. This is something we genuinely don't
  17506. + * expect to happen, so make this a big warning. If it does, we'll
  17507. + * need to add support for it.
  17508. + */
  17509. + WARN_ON(unit != get_unaligned_le32(&bat->bix.power_unit));
  17510. +
  17511. +out:
  17512. + mutex_unlock(&bat->lock);
  17513. +
  17514. + if (!status)
  17515. + power_supply_changed(bat->psy);
  17516. +
  17517. + return status;
  17518. +}
  17519. +
  17520. +static int spwr_battery_recheck_status(struct spwr_battery_device *bat)
  17521. +{
  17522. + int status;
  17523. +
  17524. + status = spwr_battery_update_bst(bat, false);
  17525. + if (!status)
  17526. + power_supply_changed(bat->psy);
  17527. +
  17528. + return status;
  17529. +}
  17530. +
  17531. +static u32 spwr_notify_bat(struct ssam_event_notifier *nf, const struct ssam_event *event)
  17532. +{
  17533. + struct spwr_battery_device *bat = container_of(nf, struct spwr_battery_device, notif);
  17534. + int status;
  17535. +
  17536. + /*
  17537. + * We cannot use strict matching when registering the notifier as the
  17538. + * EC expects us to register it against instance ID 0. Strict matching
  17539. + * would thus drop events, as those may have non-zero instance IDs in
  17540. + * this subsystem. So we need to check the instance ID of the event
  17541. + * here manually.
  17542. + */
  17543. + if (event->instance_id != bat->sdev->uid.instance)
  17544. + return 0;
  17545. +
  17546. + dev_dbg(&bat->sdev->dev, "power event (cid = %#04x, iid = %#04x, tid = %#04x)\n",
  17547. + event->command_id, event->instance_id, event->target_id);
  17548. +
  17549. + switch (event->command_id) {
  17550. + case SAM_EVENT_CID_BAT_BIX:
  17551. + status = spwr_battery_recheck_full(bat);
  17552. + break;
  17553. +
  17554. + case SAM_EVENT_CID_BAT_BST:
  17555. + status = spwr_battery_recheck_status(bat);
  17556. + break;
  17557. +
  17558. + case SAM_EVENT_CID_BAT_PROT:
  17559. + /*
  17560. + * TODO: Implement support for battery protection status change
  17561. + * event.
  17562. + */
  17563. + status = 0;
  17564. + break;
  17565. +
  17566. + case SAM_EVENT_CID_BAT_DPTF:
  17567. + /*
  17568. + * TODO: Implement support for DPTF event.
  17569. + */
  17570. + status = 0;
  17571. + break;
  17572. +
  17573. + default:
  17574. + return 0;
  17575. + }
  17576. +
  17577. + return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
  17578. +}
  17579. +
  17580. +static void spwr_battery_update_bst_workfn(struct work_struct *work)
  17581. +{
  17582. + struct delayed_work *dwork = to_delayed_work(work);
  17583. + struct spwr_battery_device *bat;
  17584. + int status;
  17585. +
  17586. + bat = container_of(dwork, struct spwr_battery_device, update_work);
  17587. +
  17588. + status = spwr_battery_update_bst(bat, false);
  17589. + if (status) {
  17590. + dev_err(&bat->sdev->dev, "failed to update battery state: %d\n", status);
  17591. + return;
  17592. + }
  17593. +
  17594. + power_supply_changed(bat->psy);
  17595. +}
  17596. +
  17597. +static void spwr_external_power_changed(struct power_supply *psy)
  17598. +{
  17599. + struct spwr_battery_device *bat = power_supply_get_drvdata(psy);
  17600. +
  17601. + /*
  17602. + * Handle battery update quirk: When the battery is fully charged (or
  17603. + * charged up to the limit imposed by the UEFI battery limit) and the
  17604. + * adapter is plugged in or removed, the EC does not send a separate
  17605. + * event for the state (charging/discharging) change. Furthermore it
  17606. + * may take some time until the state is updated on the battery.
  17607. + * Schedule an update to solve this.
  17608. + */
  17609. +
  17610. + schedule_delayed_work(&bat->update_work, SPWR_AC_BAT_UPDATE_DELAY);
  17611. +}
  17612. +
  17613. +
  17614. +/* -- Properties. ----------------------------------------------------------- */
  17615. +
  17616. +static const enum power_supply_property spwr_battery_props_chg[] = {
  17617. + POWER_SUPPLY_PROP_STATUS,
  17618. + POWER_SUPPLY_PROP_PRESENT,
  17619. + POWER_SUPPLY_PROP_TECHNOLOGY,
  17620. + POWER_SUPPLY_PROP_CYCLE_COUNT,
  17621. + POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  17622. + POWER_SUPPLY_PROP_VOLTAGE_NOW,
  17623. + POWER_SUPPLY_PROP_CURRENT_NOW,
  17624. + POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  17625. + POWER_SUPPLY_PROP_CHARGE_FULL,
  17626. + POWER_SUPPLY_PROP_CHARGE_NOW,
  17627. + POWER_SUPPLY_PROP_CAPACITY,
  17628. + POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  17629. + POWER_SUPPLY_PROP_MODEL_NAME,
  17630. + POWER_SUPPLY_PROP_MANUFACTURER,
  17631. + POWER_SUPPLY_PROP_SERIAL_NUMBER,
  17632. +};
  17633. +
  17634. +static const enum power_supply_property spwr_battery_props_eng[] = {
  17635. + POWER_SUPPLY_PROP_STATUS,
  17636. + POWER_SUPPLY_PROP_PRESENT,
  17637. + POWER_SUPPLY_PROP_TECHNOLOGY,
  17638. + POWER_SUPPLY_PROP_CYCLE_COUNT,
  17639. + POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  17640. + POWER_SUPPLY_PROP_VOLTAGE_NOW,
  17641. + POWER_SUPPLY_PROP_POWER_NOW,
  17642. + POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  17643. + POWER_SUPPLY_PROP_ENERGY_FULL,
  17644. + POWER_SUPPLY_PROP_ENERGY_NOW,
  17645. + POWER_SUPPLY_PROP_CAPACITY,
  17646. + POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  17647. + POWER_SUPPLY_PROP_MODEL_NAME,
  17648. + POWER_SUPPLY_PROP_MANUFACTURER,
  17649. + POWER_SUPPLY_PROP_SERIAL_NUMBER,
  17650. +};
  17651. +
  17652. +static int spwr_battery_prop_status(struct spwr_battery_device *bat)
  17653. +{
  17654. + u32 state = get_unaligned_le32(&bat->bst.state);
  17655. + u32 present_rate = get_unaligned_le32(&bat->bst.present_rate);
  17656. +
  17657. + lockdep_assert_held(&bat->lock);
  17658. +
  17659. + if (state & SAM_BATTERY_STATE_DISCHARGING)
  17660. + return POWER_SUPPLY_STATUS_DISCHARGING;
  17661. +
  17662. + if (state & SAM_BATTERY_STATE_CHARGING)
  17663. + return POWER_SUPPLY_STATUS_CHARGING;
  17664. +
  17665. + if (spwr_battery_is_full(bat))
  17666. + return POWER_SUPPLY_STATUS_FULL;
  17667. +
  17668. + if (present_rate == 0)
  17669. + return POWER_SUPPLY_STATUS_NOT_CHARGING;
  17670. +
  17671. + return POWER_SUPPLY_STATUS_UNKNOWN;
  17672. +}
  17673. +
  17674. +static int spwr_battery_prop_technology(struct spwr_battery_device *bat)
  17675. +{
  17676. + lockdep_assert_held(&bat->lock);
  17677. +
  17678. + if (!strcasecmp("NiCd", bat->bix.type))
  17679. + return POWER_SUPPLY_TECHNOLOGY_NiCd;
  17680. +
  17681. + if (!strcasecmp("NiMH", bat->bix.type))
  17682. + return POWER_SUPPLY_TECHNOLOGY_NiMH;
  17683. +
  17684. + if (!strcasecmp("LION", bat->bix.type))
  17685. + return POWER_SUPPLY_TECHNOLOGY_LION;
  17686. +
  17687. + if (!strncasecmp("LI-ION", bat->bix.type, 6))
  17688. + return POWER_SUPPLY_TECHNOLOGY_LION;
  17689. +
  17690. + if (!strcasecmp("LiP", bat->bix.type))
  17691. + return POWER_SUPPLY_TECHNOLOGY_LIPO;
  17692. +
  17693. + return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  17694. +}
  17695. +
  17696. +static int spwr_battery_prop_capacity(struct spwr_battery_device *bat)
  17697. +{
  17698. + u32 full_cap = sprw_battery_get_full_cap_safe(bat);
  17699. + u32 remaining_cap = get_unaligned_le32(&bat->bst.remaining_cap);
  17700. +
  17701. + lockdep_assert_held(&bat->lock);
  17702. +
  17703. + if (full_cap == 0 || full_cap == SPWR_BATTERY_VALUE_UNKNOWN)
  17704. + return -ENODATA;
  17705. +
  17706. + if (remaining_cap == SPWR_BATTERY_VALUE_UNKNOWN)
  17707. + return -ENODATA;
  17708. +
  17709. + return remaining_cap * 100 / full_cap;
  17710. +}
  17711. +
  17712. +static int spwr_battery_prop_capacity_level(struct spwr_battery_device *bat)
  17713. +{
  17714. + u32 state = get_unaligned_le32(&bat->bst.state);
  17715. + u32 remaining_cap = get_unaligned_le32(&bat->bst.remaining_cap);
  17716. +
  17717. + lockdep_assert_held(&bat->lock);
  17718. +
  17719. + if (state & SAM_BATTERY_STATE_CRITICAL)
  17720. + return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  17721. +
  17722. + if (spwr_battery_is_full(bat))
  17723. + return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  17724. +
  17725. + if (remaining_cap <= bat->alarm)
  17726. + return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  17727. +
  17728. + return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  17729. +}
  17730. +
  17731. +static int spwr_battery_get_property(struct power_supply *psy, enum power_supply_property psp,
  17732. + union power_supply_propval *val)
  17733. +{
  17734. + struct spwr_battery_device *bat = power_supply_get_drvdata(psy);
  17735. + u32 value;
  17736. + int status;
  17737. +
  17738. + mutex_lock(&bat->lock);
  17739. +
  17740. + status = spwr_battery_update_bst_unlocked(bat, true);
  17741. + if (status)
  17742. + goto out;
  17743. +
  17744. + /* Abort if battery is not present. */
  17745. + if (!spwr_battery_present(bat) && psp != POWER_SUPPLY_PROP_PRESENT) {
  17746. + status = -ENODEV;
  17747. + goto out;
  17748. + }
  17749. +
  17750. + switch (psp) {
  17751. + case POWER_SUPPLY_PROP_STATUS:
  17752. + val->intval = spwr_battery_prop_status(bat);
  17753. + break;
  17754. +
  17755. + case POWER_SUPPLY_PROP_PRESENT:
  17756. + val->intval = spwr_battery_present(bat);
  17757. + break;
  17758. +
  17759. + case POWER_SUPPLY_PROP_TECHNOLOGY:
  17760. + val->intval = spwr_battery_prop_technology(bat);
  17761. + break;
  17762. +
  17763. + case POWER_SUPPLY_PROP_CYCLE_COUNT:
  17764. + value = get_unaligned_le32(&bat->bix.cycle_count);
  17765. + if (value != SPWR_BATTERY_VALUE_UNKNOWN)
  17766. + val->intval = value;
  17767. + else
  17768. + status = -ENODATA;
  17769. + break;
  17770. +
  17771. + case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  17772. + value = get_unaligned_le32(&bat->bix.design_voltage);
  17773. + if (value != SPWR_BATTERY_VALUE_UNKNOWN)
  17774. + val->intval = value * 1000;
  17775. + else
  17776. + status = -ENODATA;
  17777. + break;
  17778. +
  17779. + case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  17780. + value = get_unaligned_le32(&bat->bst.present_voltage);
  17781. + if (value != SPWR_BATTERY_VALUE_UNKNOWN)
  17782. + val->intval = value * 1000;
  17783. + else
  17784. + status = -ENODATA;
  17785. + break;
  17786. +
  17787. + case POWER_SUPPLY_PROP_CURRENT_NOW:
  17788. + case POWER_SUPPLY_PROP_POWER_NOW:
  17789. + value = get_unaligned_le32(&bat->bst.present_rate);
  17790. + if (value != SPWR_BATTERY_VALUE_UNKNOWN)
  17791. + val->intval = value * 1000;
  17792. + else
  17793. + status = -ENODATA;
  17794. + break;
  17795. +
  17796. + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  17797. + case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  17798. + value = get_unaligned_le32(&bat->bix.design_cap);
  17799. + if (value != SPWR_BATTERY_VALUE_UNKNOWN)
  17800. + val->intval = value * 1000;
  17801. + else
  17802. + status = -ENODATA;
  17803. + break;
  17804. +
  17805. + case POWER_SUPPLY_PROP_CHARGE_FULL:
  17806. + case POWER_SUPPLY_PROP_ENERGY_FULL:
  17807. + value = get_unaligned_le32(&bat->bix.last_full_charge_cap);
  17808. + if (value != SPWR_BATTERY_VALUE_UNKNOWN)
  17809. + val->intval = value * 1000;
  17810. + else
  17811. + status = -ENODATA;
  17812. + break;
  17813. +
  17814. + case POWER_SUPPLY_PROP_CHARGE_NOW:
  17815. + case POWER_SUPPLY_PROP_ENERGY_NOW:
  17816. + value = get_unaligned_le32(&bat->bst.remaining_cap);
  17817. + if (value != SPWR_BATTERY_VALUE_UNKNOWN)
  17818. + val->intval = value * 1000;
  17819. + else
  17820. + status = -ENODATA;
  17821. + break;
  17822. +
  17823. + case POWER_SUPPLY_PROP_CAPACITY:
  17824. + val->intval = spwr_battery_prop_capacity(bat);
  17825. + break;
  17826. +
  17827. + case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  17828. + val->intval = spwr_battery_prop_capacity_level(bat);
  17829. + break;
  17830. +
  17831. + case POWER_SUPPLY_PROP_MODEL_NAME:
  17832. + val->strval = bat->bix.model;
  17833. + break;
  17834. +
  17835. + case POWER_SUPPLY_PROP_MANUFACTURER:
  17836. + val->strval = bat->bix.oem_info;
  17837. + break;
  17838. +
  17839. + case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  17840. + val->strval = bat->bix.serial;
  17841. + break;
  17842. +
  17843. + default:
  17844. + status = -EINVAL;
  17845. + break;
  17846. + }
  17847. +
  17848. +out:
  17849. + mutex_unlock(&bat->lock);
  17850. + return status;
  17851. +}
  17852. +
  17853. +
  17854. +/* -- Alarm attribute. ------------------------------------------------------ */
  17855. +
  17856. +static ssize_t alarm_show(struct device *dev, struct device_attribute *attr, char *buf)
  17857. +{
  17858. + struct power_supply *psy = dev_get_drvdata(dev);
  17859. + struct spwr_battery_device *bat = power_supply_get_drvdata(psy);
  17860. + int status;
  17861. +
  17862. + mutex_lock(&bat->lock);
  17863. + status = sysfs_emit(buf, "%d\n", bat->alarm * 1000);
  17864. + mutex_unlock(&bat->lock);
  17865. +
  17866. + return status;
  17867. +}
  17868. +
  17869. +static ssize_t alarm_store(struct device *dev, struct device_attribute *attr, const char *buf,
  17870. + size_t count)
  17871. +{
  17872. + struct power_supply *psy = dev_get_drvdata(dev);
  17873. + struct spwr_battery_device *bat = power_supply_get_drvdata(psy);
  17874. + unsigned long value;
  17875. + int status;
  17876. +
  17877. + status = kstrtoul(buf, 0, &value);
  17878. + if (status)
  17879. + return status;
  17880. +
  17881. + mutex_lock(&bat->lock);
  17882. +
  17883. + if (!spwr_battery_present(bat)) {
  17884. + mutex_unlock(&bat->lock);
  17885. + return -ENODEV;
  17886. + }
  17887. +
  17888. + status = spwr_battery_set_alarm_unlocked(bat, value / 1000);
  17889. + if (status) {
  17890. + mutex_unlock(&bat->lock);
  17891. + return status;
  17892. + }
  17893. +
  17894. + mutex_unlock(&bat->lock);
  17895. + return count;
  17896. +}
  17897. +
  17898. +static DEVICE_ATTR_RW(alarm);
  17899. +
  17900. +static struct attribute *spwr_battery_attrs[] = {
  17901. + &dev_attr_alarm.attr,
  17902. + NULL,
  17903. +};
  17904. +ATTRIBUTE_GROUPS(spwr_battery);
  17905. +
  17906. +
  17907. +/* -- Device setup. --------------------------------------------------------- */
  17908. +
  17909. +static void spwr_battery_init(struct spwr_battery_device *bat, struct ssam_device *sdev,
  17910. + struct ssam_event_registry registry, const char *name)
  17911. +{
  17912. + mutex_init(&bat->lock);
  17913. + strncpy(bat->name, name, ARRAY_SIZE(bat->name) - 1);
  17914. +
  17915. + bat->sdev = sdev;
  17916. +
  17917. + bat->notif.base.priority = 1;
  17918. + bat->notif.base.fn = spwr_notify_bat;
  17919. + bat->notif.event.reg = registry;
  17920. + bat->notif.event.id.target_category = sdev->uid.category;
  17921. + bat->notif.event.id.instance = 0; /* need to register with instance 0 */
  17922. + bat->notif.event.mask = SSAM_EVENT_MASK_TARGET;
  17923. + bat->notif.event.flags = SSAM_EVENT_SEQUENCED;
  17924. +
  17925. + bat->psy_desc.name = bat->name;
  17926. + bat->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  17927. + bat->psy_desc.get_property = spwr_battery_get_property;
  17928. +
  17929. + INIT_DELAYED_WORK(&bat->update_work, spwr_battery_update_bst_workfn);
  17930. +}
  17931. +
  17932. +static int spwr_battery_register(struct spwr_battery_device *bat)
  17933. +{
  17934. + struct power_supply_config psy_cfg = {};
  17935. + __le32 sta;
  17936. + int status;
  17937. +
  17938. + /* Make sure the device is there and functioning properly. */
  17939. + status = ssam_retry(ssam_bat_get_sta, bat->sdev, &sta);
  17940. + if (status)
  17941. + return status;
  17942. +
  17943. + if ((le32_to_cpu(sta) & SAM_BATTERY_STA_OK) != SAM_BATTERY_STA_OK)
  17944. + return -ENODEV;
  17945. +
  17946. + /* Satisfy lockdep although we are in an exclusive context here. */
  17947. + mutex_lock(&bat->lock);
  17948. +
  17949. + status = spwr_battery_update_bix_unlocked(bat);
  17950. + if (status) {
  17951. + mutex_unlock(&bat->lock);
  17952. + return status;
  17953. + }
  17954. +
  17955. + if (spwr_battery_present(bat)) {
  17956. + u32 cap_warn = get_unaligned_le32(&bat->bix.design_cap_warn);
  17957. +
  17958. + status = spwr_battery_set_alarm_unlocked(bat, cap_warn);
  17959. + if (status) {
  17960. + mutex_unlock(&bat->lock);
  17961. + return status;
  17962. + }
  17963. + }
  17964. +
  17965. + mutex_unlock(&bat->lock);
  17966. +
  17967. + bat->psy_desc.external_power_changed = spwr_external_power_changed;
  17968. +
  17969. + switch (get_unaligned_le32(&bat->bix.power_unit)) {
  17970. + case SAM_BATTERY_POWER_UNIT_mW:
  17971. + bat->psy_desc.properties = spwr_battery_props_eng;
  17972. + bat->psy_desc.num_properties = ARRAY_SIZE(spwr_battery_props_eng);
  17973. + break;
  17974. +
  17975. + case SAM_BATTERY_POWER_UNIT_mA:
  17976. + bat->psy_desc.properties = spwr_battery_props_chg;
  17977. + bat->psy_desc.num_properties = ARRAY_SIZE(spwr_battery_props_chg);
  17978. + break;
  17979. +
  17980. + default:
  17981. + dev_err(&bat->sdev->dev, "unsupported battery power unit: %u\n",
  17982. + get_unaligned_le32(&bat->bix.power_unit));
  17983. + return -EINVAL;
  17984. + }
  17985. +
  17986. + psy_cfg.drv_data = bat;
  17987. + psy_cfg.attr_grp = spwr_battery_groups;
  17988. +
  17989. + bat->psy = devm_power_supply_register(&bat->sdev->dev, &bat->psy_desc, &psy_cfg);
  17990. + if (IS_ERR(bat->psy))
  17991. + return PTR_ERR(bat->psy);
  17992. +
  17993. + return ssam_notifier_register(bat->sdev->ctrl, &bat->notif);
  17994. +}
  17995. +
  17996. +
  17997. +/* -- Driver setup. --------------------------------------------------------- */
  17998. +
  17999. +static int __maybe_unused surface_battery_resume(struct device *dev)
  18000. +{
  18001. + return spwr_battery_recheck_full(dev_get_drvdata(dev));
  18002. +}
  18003. +static SIMPLE_DEV_PM_OPS(surface_battery_pm_ops, NULL, surface_battery_resume);
  18004. +
  18005. +static int surface_battery_probe(struct ssam_device *sdev)
  18006. +{
  18007. + const struct spwr_psy_properties *p;
  18008. + struct spwr_battery_device *bat;
  18009. +
  18010. + p = ssam_device_get_match_data(sdev);
  18011. + if (!p)
  18012. + return -ENODEV;
  18013. +
  18014. + bat = devm_kzalloc(&sdev->dev, sizeof(*bat), GFP_KERNEL);
  18015. + if (!bat)
  18016. + return -ENOMEM;
  18017. +
  18018. + spwr_battery_init(bat, sdev, p->registry, p->name);
  18019. + ssam_device_set_drvdata(sdev, bat);
  18020. +
  18021. + return spwr_battery_register(bat);
  18022. +}
  18023. +
  18024. +static void surface_battery_remove(struct ssam_device *sdev)
  18025. +{
  18026. + struct spwr_battery_device *bat = ssam_device_get_drvdata(sdev);
  18027. +
  18028. + ssam_notifier_unregister(sdev->ctrl, &bat->notif);
  18029. + cancel_delayed_work_sync(&bat->update_work);
  18030. +}
  18031. +
  18032. +static const struct spwr_psy_properties spwr_psy_props_bat1 = {
  18033. + .name = "BAT1",
  18034. + .registry = SSAM_EVENT_REGISTRY_SAM,
  18035. +};
  18036. +
  18037. +static const struct spwr_psy_properties spwr_psy_props_bat2_sb3 = {
  18038. + .name = "BAT2",
  18039. + .registry = SSAM_EVENT_REGISTRY_KIP,
  18040. +};
  18041. +
  18042. +static const struct ssam_device_id surface_battery_match[] = {
  18043. + { SSAM_SDEV(BAT, 0x01, 0x01, 0x00), (unsigned long)&spwr_psy_props_bat1 },
  18044. + { SSAM_SDEV(BAT, 0x02, 0x01, 0x00), (unsigned long)&spwr_psy_props_bat2_sb3 },
  18045. + { },
  18046. +};
  18047. +MODULE_DEVICE_TABLE(ssam, surface_battery_match);
  18048. +
  18049. +static struct ssam_device_driver surface_battery_driver = {
  18050. + .probe = surface_battery_probe,
  18051. + .remove = surface_battery_remove,
  18052. + .match_table = surface_battery_match,
  18053. + .driver = {
  18054. + .name = "surface_battery",
  18055. + .pm = &surface_battery_pm_ops,
  18056. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  18057. + },
  18058. +};
  18059. +module_ssam_device_driver(surface_battery_driver);
  18060. +
  18061. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  18062. +MODULE_DESCRIPTION("Battery driver for Surface System Aggregator Module");
  18063. +MODULE_LICENSE("GPL");
  18064. diff --git a/drivers/power/supply/surface_charger.c b/drivers/power/supply/surface_charger.c
  18065. new file mode 100644
  18066. index 000000000000..a060c36c7766
  18067. --- /dev/null
  18068. +++ b/drivers/power/supply/surface_charger.c
  18069. @@ -0,0 +1,282 @@
  18070. +// SPDX-License-Identifier: GPL-2.0+
  18071. +/*
  18072. + * AC driver for 7th-generation Microsoft Surface devices via Surface System
  18073. + * Aggregator Module (SSAM).
  18074. + *
  18075. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  18076. + */
  18077. +
  18078. +#include <asm/unaligned.h>
  18079. +#include <linux/kernel.h>
  18080. +#include <linux/module.h>
  18081. +#include <linux/mutex.h>
  18082. +#include <linux/power_supply.h>
  18083. +#include <linux/types.h>
  18084. +
  18085. +#include <linux/surface_aggregator/device.h>
  18086. +
  18087. +
  18088. +/* -- SAM interface. -------------------------------------------------------- */
  18089. +
  18090. +enum sam_event_cid_bat {
  18091. + SAM_EVENT_CID_BAT_ADP = 0x17,
  18092. +};
  18093. +
  18094. +enum sam_battery_sta {
  18095. + SAM_BATTERY_STA_OK = 0x0f,
  18096. + SAM_BATTERY_STA_PRESENT = 0x10,
  18097. +};
  18098. +
  18099. +/* Get battery status (_STA). */
  18100. +SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_sta, __le32, {
  18101. + .target_category = SSAM_SSH_TC_BAT,
  18102. + .command_id = 0x01,
  18103. +});
  18104. +
  18105. +/* Get platform power source for battery (_PSR / DPTF PSRC). */
  18106. +SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_psrc, __le32, {
  18107. + .target_category = SSAM_SSH_TC_BAT,
  18108. + .command_id = 0x0d,
  18109. +});
  18110. +
  18111. +
  18112. +/* -- Device structures. ---------------------------------------------------- */
  18113. +
  18114. +struct spwr_psy_properties {
  18115. + const char *name;
  18116. + struct ssam_event_registry registry;
  18117. +};
  18118. +
  18119. +struct spwr_ac_device {
  18120. + struct ssam_device *sdev;
  18121. +
  18122. + char name[32];
  18123. + struct power_supply *psy;
  18124. + struct power_supply_desc psy_desc;
  18125. +
  18126. + struct ssam_event_notifier notif;
  18127. +
  18128. + struct mutex lock; /* Guards access to state below. */
  18129. +
  18130. + __le32 state;
  18131. +};
  18132. +
  18133. +
  18134. +/* -- State management. ----------------------------------------------------- */
  18135. +
  18136. +static int spwr_ac_update_unlocked(struct spwr_ac_device *ac)
  18137. +{
  18138. + __le32 old = ac->state;
  18139. + int status;
  18140. +
  18141. + lockdep_assert_held(&ac->lock);
  18142. +
  18143. + status = ssam_retry(ssam_bat_get_psrc, ac->sdev, &ac->state);
  18144. + if (status < 0)
  18145. + return status;
  18146. +
  18147. + return old != ac->state;
  18148. +}
  18149. +
  18150. +static int spwr_ac_update(struct spwr_ac_device *ac)
  18151. +{
  18152. + int status;
  18153. +
  18154. + mutex_lock(&ac->lock);
  18155. + status = spwr_ac_update_unlocked(ac);
  18156. + mutex_unlock(&ac->lock);
  18157. +
  18158. + return status;
  18159. +}
  18160. +
  18161. +static int spwr_ac_recheck(struct spwr_ac_device *ac)
  18162. +{
  18163. + int status;
  18164. +
  18165. + status = spwr_ac_update(ac);
  18166. + if (status > 0)
  18167. + power_supply_changed(ac->psy);
  18168. +
  18169. + return status >= 0 ? 0 : status;
  18170. +}
  18171. +
  18172. +static u32 spwr_notify_ac(struct ssam_event_notifier *nf, const struct ssam_event *event)
  18173. +{
  18174. + struct spwr_ac_device *ac;
  18175. + int status;
  18176. +
  18177. + ac = container_of(nf, struct spwr_ac_device, notif);
  18178. +
  18179. + dev_dbg(&ac->sdev->dev, "power event (cid = %#04x, iid = %#04x, tid = %#04x)\n",
  18180. + event->command_id, event->instance_id, event->target_id);
  18181. +
  18182. + /*
  18183. + * Allow events of all targets/instances here. Global adapter status
  18184. + * seems to be handled via target=1 and instance=1, but events are
  18185. + * reported on all targets/instances in use.
  18186. + *
  18187. + * While it should be enough to just listen on 1/1, listen everywhere to
  18188. + * make sure we don't miss anything.
  18189. + */
  18190. +
  18191. + switch (event->command_id) {
  18192. + case SAM_EVENT_CID_BAT_ADP:
  18193. + status = spwr_ac_recheck(ac);
  18194. + return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
  18195. +
  18196. + default:
  18197. + return 0;
  18198. + }
  18199. +}
  18200. +
  18201. +
  18202. +/* -- Properties. ----------------------------------------------------------- */
  18203. +
  18204. +static const enum power_supply_property spwr_ac_props[] = {
  18205. + POWER_SUPPLY_PROP_ONLINE,
  18206. +};
  18207. +
  18208. +static int spwr_ac_get_property(struct power_supply *psy, enum power_supply_property psp,
  18209. + union power_supply_propval *val)
  18210. +{
  18211. + struct spwr_ac_device *ac = power_supply_get_drvdata(psy);
  18212. + int status;
  18213. +
  18214. + mutex_lock(&ac->lock);
  18215. +
  18216. + status = spwr_ac_update_unlocked(ac);
  18217. + if (status)
  18218. + goto out;
  18219. +
  18220. + switch (psp) {
  18221. + case POWER_SUPPLY_PROP_ONLINE:
  18222. + val->intval = !!le32_to_cpu(ac->state);
  18223. + break;
  18224. +
  18225. + default:
  18226. + status = -EINVAL;
  18227. + goto out;
  18228. + }
  18229. +
  18230. +out:
  18231. + mutex_unlock(&ac->lock);
  18232. + return status;
  18233. +}
  18234. +
  18235. +
  18236. +/* -- Device setup. --------------------------------------------------------- */
  18237. +
  18238. +static char *battery_supplied_to[] = {
  18239. + "BAT1",
  18240. + "BAT2",
  18241. +};
  18242. +
  18243. +static void spwr_ac_init(struct spwr_ac_device *ac, struct ssam_device *sdev,
  18244. + struct ssam_event_registry registry, const char *name)
  18245. +{
  18246. + mutex_init(&ac->lock);
  18247. + strncpy(ac->name, name, ARRAY_SIZE(ac->name) - 1);
  18248. +
  18249. + ac->sdev = sdev;
  18250. +
  18251. + ac->notif.base.priority = 1;
  18252. + ac->notif.base.fn = spwr_notify_ac;
  18253. + ac->notif.event.reg = registry;
  18254. + ac->notif.event.id.target_category = sdev->uid.category;
  18255. + ac->notif.event.id.instance = 0;
  18256. + ac->notif.event.mask = SSAM_EVENT_MASK_NONE;
  18257. + ac->notif.event.flags = SSAM_EVENT_SEQUENCED;
  18258. +
  18259. + ac->psy_desc.name = ac->name;
  18260. + ac->psy_desc.type = POWER_SUPPLY_TYPE_MAINS;
  18261. + ac->psy_desc.properties = spwr_ac_props;
  18262. + ac->psy_desc.num_properties = ARRAY_SIZE(spwr_ac_props);
  18263. + ac->psy_desc.get_property = spwr_ac_get_property;
  18264. +}
  18265. +
  18266. +static int spwr_ac_register(struct spwr_ac_device *ac)
  18267. +{
  18268. + struct power_supply_config psy_cfg = {};
  18269. + __le32 sta;
  18270. + int status;
  18271. +
  18272. + /* Make sure the device is there and functioning properly. */
  18273. + status = ssam_retry(ssam_bat_get_sta, ac->sdev, &sta);
  18274. + if (status)
  18275. + return status;
  18276. +
  18277. + if ((le32_to_cpu(sta) & SAM_BATTERY_STA_OK) != SAM_BATTERY_STA_OK)
  18278. + return -ENODEV;
  18279. +
  18280. + psy_cfg.drv_data = ac;
  18281. + psy_cfg.supplied_to = battery_supplied_to;
  18282. + psy_cfg.num_supplicants = ARRAY_SIZE(battery_supplied_to);
  18283. +
  18284. + ac->psy = devm_power_supply_register(&ac->sdev->dev, &ac->psy_desc, &psy_cfg);
  18285. + if (IS_ERR(ac->psy))
  18286. + return PTR_ERR(ac->psy);
  18287. +
  18288. + return ssam_notifier_register(ac->sdev->ctrl, &ac->notif);
  18289. +}
  18290. +
  18291. +
  18292. +/* -- Driver setup. --------------------------------------------------------- */
  18293. +
  18294. +static int __maybe_unused surface_ac_resume(struct device *dev)
  18295. +{
  18296. + return spwr_ac_recheck(dev_get_drvdata(dev));
  18297. +}
  18298. +static SIMPLE_DEV_PM_OPS(surface_ac_pm_ops, NULL, surface_ac_resume);
  18299. +
  18300. +static int surface_ac_probe(struct ssam_device *sdev)
  18301. +{
  18302. + const struct spwr_psy_properties *p;
  18303. + struct spwr_ac_device *ac;
  18304. +
  18305. + p = ssam_device_get_match_data(sdev);
  18306. + if (!p)
  18307. + return -ENODEV;
  18308. +
  18309. + ac = devm_kzalloc(&sdev->dev, sizeof(*ac), GFP_KERNEL);
  18310. + if (!ac)
  18311. + return -ENOMEM;
  18312. +
  18313. + spwr_ac_init(ac, sdev, p->registry, p->name);
  18314. + ssam_device_set_drvdata(sdev, ac);
  18315. +
  18316. + return spwr_ac_register(ac);
  18317. +}
  18318. +
  18319. +static void surface_ac_remove(struct ssam_device *sdev)
  18320. +{
  18321. + struct spwr_ac_device *ac = ssam_device_get_drvdata(sdev);
  18322. +
  18323. + ssam_notifier_unregister(sdev->ctrl, &ac->notif);
  18324. +}
  18325. +
  18326. +static const struct spwr_psy_properties spwr_psy_props_adp1 = {
  18327. + .name = "ADP1",
  18328. + .registry = SSAM_EVENT_REGISTRY_SAM,
  18329. +};
  18330. +
  18331. +static const struct ssam_device_id surface_ac_match[] = {
  18332. + { SSAM_SDEV(BAT, 0x01, 0x01, 0x01), (unsigned long)&spwr_psy_props_adp1 },
  18333. + { },
  18334. +};
  18335. +MODULE_DEVICE_TABLE(ssam, surface_ac_match);
  18336. +
  18337. +static struct ssam_device_driver surface_ac_driver = {
  18338. + .probe = surface_ac_probe,
  18339. + .remove = surface_ac_remove,
  18340. + .match_table = surface_ac_match,
  18341. + .driver = {
  18342. + .name = "surface_ac",
  18343. + .pm = &surface_ac_pm_ops,
  18344. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  18345. + },
  18346. +};
  18347. +module_ssam_device_driver(surface_ac_driver);
  18348. +
  18349. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  18350. +MODULE_DESCRIPTION("AC driver for Surface System Aggregator Module");
  18351. +MODULE_LICENSE("GPL");
  18352. diff --git a/include/linux/mod_devicetable.h b/include/linux/mod_devicetable.h
  18353. index ef64063fac30..0b8f1feefe0e 100644
  18354. --- a/include/linux/mod_devicetable.h
  18355. +++ b/include/linux/mod_devicetable.h
  18356. @@ -840,15 +840,16 @@ struct mhi_device_id {
  18357. /* Surface System Aggregator Module */
  18358. -#define SSAM_MATCH_CHANNEL 0x1
  18359. +#define SSAM_MATCH_TARGET 0x1
  18360. #define SSAM_MATCH_INSTANCE 0x2
  18361. #define SSAM_MATCH_FUNCTION 0x4
  18362. struct ssam_device_id {
  18363. __u8 match_flags;
  18364. + __u8 domain;
  18365. __u8 category;
  18366. - __u8 channel;
  18367. + __u8 target;
  18368. __u8 instance;
  18369. __u8 function;
  18370. diff --git a/include/linux/surface_acpi_notify.h b/include/linux/surface_acpi_notify.h
  18371. new file mode 100644
  18372. index 000000000000..8e3e86c7d78c
  18373. --- /dev/null
  18374. +++ b/include/linux/surface_acpi_notify.h
  18375. @@ -0,0 +1,39 @@
  18376. +/* SPDX-License-Identifier: GPL-2.0+ */
  18377. +/*
  18378. + * Interface for Surface ACPI Notify (SAN) driver.
  18379. + *
  18380. + * Provides access to discrete GPU notifications sent from ACPI via the SAN
  18381. + * driver, which are not handled by this driver directly.
  18382. + *
  18383. + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
  18384. + */
  18385. +
  18386. +#ifndef _LINUX_SURFACE_ACPI_NOTIFY_H
  18387. +#define _LINUX_SURFACE_ACPI_NOTIFY_H
  18388. +
  18389. +#include <linux/notifier.h>
  18390. +#include <linux/types.h>
  18391. +
  18392. +/**
  18393. + * struct san_dgpu_event - Discrete GPU ACPI event.
  18394. + * @category: Category of the event.
  18395. + * @target: Target ID of the event source.
  18396. + * @command: Command ID of the event.
  18397. + * @instance: Instance ID of the event source.
  18398. + * @length: Length of the event's payload data (in bytes).
  18399. + * @payload: Pointer to the event's payload data.
  18400. + */
  18401. +struct san_dgpu_event {
  18402. + u8 category;
  18403. + u8 target;
  18404. + u8 command;
  18405. + u8 instance;
  18406. + u16 length;
  18407. + u8 *payload;
  18408. +};
  18409. +
  18410. +int san_client_link(struct device *client);
  18411. +int san_dgpu_notifier_register(struct notifier_block *nb);
  18412. +int san_dgpu_notifier_unregister(struct notifier_block *nb);
  18413. +
  18414. +#endif /* _LINUX_SURFACE_ACPI_NOTIFY_H */
  18415. diff --git a/include/linux/surface_aggregator/controller.h b/include/linux/surface_aggregator/controller.h
  18416. new file mode 100644
  18417. index 000000000000..068e1982ad37
  18418. --- /dev/null
  18419. +++ b/include/linux/surface_aggregator/controller.h
  18420. @@ -0,0 +1,849 @@
  18421. +/* SPDX-License-Identifier: GPL-2.0+ */
  18422. +/*
  18423. + * Surface System Aggregator Module (SSAM) controller interface.
  18424. + *
  18425. + * Main communication interface for the SSAM EC. Provides a controller
  18426. + * managing access and communication to and from the SSAM EC, as well as main
  18427. + * communication structures and definitions.
  18428. + *
  18429. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  18430. + */
  18431. +
  18432. +#ifndef _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H
  18433. +#define _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H
  18434. +
  18435. +#include <linux/completion.h>
  18436. +#include <linux/device.h>
  18437. +#include <linux/types.h>
  18438. +
  18439. +#include <linux/surface_aggregator/serial_hub.h>
  18440. +
  18441. +
  18442. +/* -- Main data types and definitions --------------------------------------- */
  18443. +
  18444. +/**
  18445. + * enum ssam_event_flags - Flags for enabling/disabling SAM events
  18446. + * @SSAM_EVENT_SEQUENCED: The event will be sent via a sequenced data frame.
  18447. + */
  18448. +enum ssam_event_flags {
  18449. + SSAM_EVENT_SEQUENCED = BIT(0),
  18450. +};
  18451. +
  18452. +/**
  18453. + * struct ssam_event - SAM event sent from the EC to the host.
  18454. + * @target_category: Target category of the event source. See &enum ssam_ssh_tc.
  18455. + * @target_id: Target ID of the event source.
  18456. + * @command_id: Command ID of the event.
  18457. + * @instance_id: Instance ID of the event source.
  18458. + * @length: Length of the event payload in bytes.
  18459. + * @data: Event payload data.
  18460. + */
  18461. +struct ssam_event {
  18462. + u8 target_category;
  18463. + u8 target_id;
  18464. + u8 command_id;
  18465. + u8 instance_id;
  18466. + u16 length;
  18467. + u8 data[];
  18468. +};
  18469. +
  18470. +/**
  18471. + * enum ssam_request_flags - Flags for SAM requests.
  18472. + *
  18473. + * @SSAM_REQUEST_HAS_RESPONSE:
  18474. + * Specifies that the request expects a response. If not set, the request
  18475. + * will be directly completed after its underlying packet has been
  18476. + * transmitted. If set, the request transport system waits for a response
  18477. + * of the request.
  18478. + *
  18479. + * @SSAM_REQUEST_UNSEQUENCED:
  18480. + * Specifies that the request should be transmitted via an unsequenced
  18481. + * packet. If set, the request must not have a response, meaning that this
  18482. + * flag and the %SSAM_REQUEST_HAS_RESPONSE flag are mutually exclusive.
  18483. + */
  18484. +enum ssam_request_flags {
  18485. + SSAM_REQUEST_HAS_RESPONSE = BIT(0),
  18486. + SSAM_REQUEST_UNSEQUENCED = BIT(1),
  18487. +};
  18488. +
  18489. +/**
  18490. + * struct ssam_request - SAM request description.
  18491. + * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
  18492. + * @target_id: ID of the request's target.
  18493. + * @command_id: Command ID of the request.
  18494. + * @instance_id: Instance ID of the request's target.
  18495. + * @flags: Flags for the request. See &enum ssam_request_flags.
  18496. + * @length: Length of the request payload in bytes.
  18497. + * @payload: Request payload data.
  18498. + *
  18499. + * This struct fully describes a SAM request with payload. It is intended to
  18500. + * help set up the actual transport struct, e.g. &struct ssam_request_sync,
  18501. + * and specifically its raw message data via ssam_request_write_data().
  18502. + */
  18503. +struct ssam_request {
  18504. + u8 target_category;
  18505. + u8 target_id;
  18506. + u8 command_id;
  18507. + u8 instance_id;
  18508. + u16 flags;
  18509. + u16 length;
  18510. + const u8 *payload;
  18511. +};
  18512. +
  18513. +/**
  18514. + * struct ssam_response - Response buffer for SAM request.
  18515. + * @capacity: Capacity of the buffer, in bytes.
  18516. + * @length: Length of the actual data stored in the memory pointed to by
  18517. + * @pointer, in bytes. Set by the transport system.
  18518. + * @pointer: Pointer to the buffer's memory, storing the response payload data.
  18519. + */
  18520. +struct ssam_response {
  18521. + size_t capacity;
  18522. + size_t length;
  18523. + u8 *pointer;
  18524. +};
  18525. +
  18526. +struct ssam_controller;
  18527. +
  18528. +struct ssam_controller *ssam_get_controller(void);
  18529. +struct ssam_controller *ssam_client_bind(struct device *client);
  18530. +int ssam_client_link(struct ssam_controller *ctrl, struct device *client);
  18531. +
  18532. +struct device *ssam_controller_device(struct ssam_controller *c);
  18533. +
  18534. +struct ssam_controller *ssam_controller_get(struct ssam_controller *c);
  18535. +void ssam_controller_put(struct ssam_controller *c);
  18536. +
  18537. +void ssam_controller_statelock(struct ssam_controller *c);
  18538. +void ssam_controller_stateunlock(struct ssam_controller *c);
  18539. +
  18540. +ssize_t ssam_request_write_data(struct ssam_span *buf,
  18541. + struct ssam_controller *ctrl,
  18542. + const struct ssam_request *spec);
  18543. +
  18544. +
  18545. +/* -- Synchronous request interface. ---------------------------------------- */
  18546. +
  18547. +/**
  18548. + * struct ssam_request_sync - Synchronous SAM request struct.
  18549. + * @base: Underlying SSH request.
  18550. + * @comp: Completion used to signal full completion of the request. After the
  18551. + * request has been submitted, this struct may only be modified or
  18552. + * deallocated after the completion has been signaled.
  18553. + * request has been submitted,
  18554. + * @resp: Buffer to store the response.
  18555. + * @status: Status of the request, set after the base request has been
  18556. + * completed or has failed.
  18557. + */
  18558. +struct ssam_request_sync {
  18559. + struct ssh_request base;
  18560. + struct completion comp;
  18561. + struct ssam_response *resp;
  18562. + int status;
  18563. +};
  18564. +
  18565. +int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
  18566. + struct ssam_request_sync **rqst,
  18567. + struct ssam_span *buffer);
  18568. +
  18569. +void ssam_request_sync_free(struct ssam_request_sync *rqst);
  18570. +
  18571. +int ssam_request_sync_init(struct ssam_request_sync *rqst,
  18572. + enum ssam_request_flags flags);
  18573. +
  18574. +/**
  18575. + * ssam_request_sync_set_data - Set message data of a synchronous request.
  18576. + * @rqst: The request.
  18577. + * @ptr: Pointer to the request message data.
  18578. + * @len: Length of the request message data.
  18579. + *
  18580. + * Set the request message data of a synchronous request. The provided buffer
  18581. + * needs to live until the request has been completed.
  18582. + */
  18583. +static inline void ssam_request_sync_set_data(struct ssam_request_sync *rqst,
  18584. + u8 *ptr, size_t len)
  18585. +{
  18586. + ssh_request_set_data(&rqst->base, ptr, len);
  18587. +}
  18588. +
  18589. +/**
  18590. + * ssam_request_sync_set_resp - Set response buffer of a synchronous request.
  18591. + * @rqst: The request.
  18592. + * @resp: The response buffer.
  18593. + *
  18594. + * Sets the response buffer of a synchronous request. This buffer will store
  18595. + * the response of the request after it has been completed. May be %NULL if no
  18596. + * response is expected.
  18597. + */
  18598. +static inline void ssam_request_sync_set_resp(struct ssam_request_sync *rqst,
  18599. + struct ssam_response *resp)
  18600. +{
  18601. + rqst->resp = resp;
  18602. +}
  18603. +
  18604. +int ssam_request_sync_submit(struct ssam_controller *ctrl,
  18605. + struct ssam_request_sync *rqst);
  18606. +
  18607. +/**
  18608. + * ssam_request_sync_wait - Wait for completion of a synchronous request.
  18609. + * @rqst: The request to wait for.
  18610. + *
  18611. + * Wait for completion and release of a synchronous request. After this
  18612. + * function terminates, the request is guaranteed to have left the transport
  18613. + * system. After successful submission of a request, this function must be
  18614. + * called before accessing the response of the request, freeing the request,
  18615. + * or freeing any of the buffers associated with the request.
  18616. + *
  18617. + * This function must not be called if the request has not been submitted yet
  18618. + * and may lead to a deadlock/infinite wait if a subsequent request submission
  18619. + * fails in that case, due to the completion never triggering.
  18620. + *
  18621. + * Return: Returns the status of the given request, which is set on completion
  18622. + * of the packet. This value is zero on success and negative on failure.
  18623. + */
  18624. +static inline int ssam_request_sync_wait(struct ssam_request_sync *rqst)
  18625. +{
  18626. + wait_for_completion(&rqst->comp);
  18627. + return rqst->status;
  18628. +}
  18629. +
  18630. +int ssam_request_sync(struct ssam_controller *ctrl,
  18631. + const struct ssam_request *spec,
  18632. + struct ssam_response *rsp);
  18633. +
  18634. +int ssam_request_sync_with_buffer(struct ssam_controller *ctrl,
  18635. + const struct ssam_request *spec,
  18636. + struct ssam_response *rsp,
  18637. + struct ssam_span *buf);
  18638. +
  18639. +/**
  18640. + * ssam_request_sync_onstack - Execute a synchronous request on the stack.
  18641. + * @ctrl: The controller via which the request is submitted.
  18642. + * @rqst: The request specification.
  18643. + * @rsp: The response buffer.
  18644. + * @payload_len: The (maximum) request payload length.
  18645. + *
  18646. + * Allocates a synchronous request with specified payload length on the stack,
  18647. + * fully initializes it via the provided request specification, submits it,
  18648. + * and finally waits for its completion before returning its status. This
  18649. + * helper macro essentially allocates the request message buffer on the stack
  18650. + * and then calls ssam_request_sync_with_buffer().
  18651. + *
  18652. + * Note: The @payload_len parameter specifies the maximum payload length, used
  18653. + * for buffer allocation. The actual payload length may be smaller.
  18654. + *
  18655. + * Return: Returns the status of the request or any failure during setup, i.e.
  18656. + * zero on success and a negative value on failure.
  18657. + */
  18658. +#define ssam_request_sync_onstack(ctrl, rqst, rsp, payload_len) \
  18659. + ({ \
  18660. + u8 __data[SSH_COMMAND_MESSAGE_LENGTH(payload_len)]; \
  18661. + struct ssam_span __buf = { &__data[0], ARRAY_SIZE(__data) }; \
  18662. + \
  18663. + ssam_request_sync_with_buffer(ctrl, rqst, rsp, &__buf); \
  18664. + })
  18665. +
  18666. +/**
  18667. + * __ssam_retry - Retry request in case of I/O errors or timeouts.
  18668. + * @request: The request function to execute. Must return an integer.
  18669. + * @n: Number of tries.
  18670. + * @args: Arguments for the request function.
  18671. + *
  18672. + * Executes the given request function, i.e. calls @request. In case the
  18673. + * request returns %-EREMOTEIO (indicates I/O error) or %-ETIMEDOUT (request
  18674. + * or underlying packet timed out), @request will be re-executed again, up to
  18675. + * @n times in total.
  18676. + *
  18677. + * Return: Returns the return value of the last execution of @request.
  18678. + */
  18679. +#define __ssam_retry(request, n, args...) \
  18680. + ({ \
  18681. + int __i, __s = 0; \
  18682. + \
  18683. + for (__i = (n); __i > 0; __i--) { \
  18684. + __s = request(args); \
  18685. + if (__s != -ETIMEDOUT && __s != -EREMOTEIO) \
  18686. + break; \
  18687. + } \
  18688. + __s; \
  18689. + })
  18690. +
  18691. +/**
  18692. + * ssam_retry - Retry request in case of I/O errors or timeouts up to three
  18693. + * times in total.
  18694. + * @request: The request function to execute. Must return an integer.
  18695. + * @args: Arguments for the request function.
  18696. + *
  18697. + * Executes the given request function, i.e. calls @request. In case the
  18698. + * request returns %-EREMOTEIO (indicates I/O error) or -%ETIMEDOUT (request
  18699. + * or underlying packet timed out), @request will be re-executed again, up to
  18700. + * three times in total.
  18701. + *
  18702. + * See __ssam_retry() for a more generic macro for this purpose.
  18703. + *
  18704. + * Return: Returns the return value of the last execution of @request.
  18705. + */
  18706. +#define ssam_retry(request, args...) \
  18707. + __ssam_retry(request, 3, args)
  18708. +
  18709. +/**
  18710. + * struct ssam_request_spec - Blue-print specification of SAM request.
  18711. + * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
  18712. + * @target_id: ID of the request's target.
  18713. + * @command_id: Command ID of the request.
  18714. + * @instance_id: Instance ID of the request's target.
  18715. + * @flags: Flags for the request. See &enum ssam_request_flags.
  18716. + *
  18717. + * Blue-print specification for a SAM request. This struct describes the
  18718. + * unique static parameters of a request (i.e. type) without specifying any of
  18719. + * its instance-specific data (e.g. payload). It is intended to be used as base
  18720. + * for defining simple request functions via the
  18721. + * ``SSAM_DEFINE_SYNC_REQUEST_x()`` family of macros.
  18722. + */
  18723. +struct ssam_request_spec {
  18724. + u8 target_category;
  18725. + u8 target_id;
  18726. + u8 command_id;
  18727. + u8 instance_id;
  18728. + u8 flags;
  18729. +};
  18730. +
  18731. +/**
  18732. + * struct ssam_request_spec_md - Blue-print specification for multi-device SAM
  18733. + * request.
  18734. + * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
  18735. + * @command_id: Command ID of the request.
  18736. + * @flags: Flags for the request. See &enum ssam_request_flags.
  18737. + *
  18738. + * Blue-print specification for a multi-device SAM request, i.e. a request
  18739. + * that is applicable to multiple device instances, described by their
  18740. + * individual target and instance IDs. This struct describes the unique static
  18741. + * parameters of a request (i.e. type) without specifying any of its
  18742. + * instance-specific data (e.g. payload) and without specifying any of its
  18743. + * device specific IDs (i.e. target and instance ID). It is intended to be
  18744. + * used as base for defining simple multi-device request functions via the
  18745. + * ``SSAM_DEFINE_SYNC_REQUEST_MD_x()`` and ``SSAM_DEFINE_SYNC_REQUEST_CL_x()``
  18746. + * families of macros.
  18747. + */
  18748. +struct ssam_request_spec_md {
  18749. + u8 target_category;
  18750. + u8 command_id;
  18751. + u8 flags;
  18752. +};
  18753. +
  18754. +/**
  18755. + * SSAM_DEFINE_SYNC_REQUEST_N() - Define synchronous SAM request function
  18756. + * with neither argument nor return value.
  18757. + * @name: Name of the generated function.
  18758. + * @spec: Specification (&struct ssam_request_spec) defining the request.
  18759. + *
  18760. + * Defines a function executing the synchronous SAM request specified by
  18761. + * @spec, with the request having neither argument nor return value. The
  18762. + * generated function takes care of setting up the request struct and buffer
  18763. + * allocation, as well as execution of the request itself, returning once the
  18764. + * request has been fully completed. The required transport buffer will be
  18765. + * allocated on the stack.
  18766. + *
  18767. + * The generated function is defined as ``static int name(struct
  18768. + * ssam_controller *ctrl)``, returning the status of the request, which is
  18769. + * zero on success and negative on failure. The ``ctrl`` parameter is the
  18770. + * controller via which the request is being sent.
  18771. + *
  18772. + * Refer to ssam_request_sync_onstack() for more details on the behavior of
  18773. + * the generated function.
  18774. + */
  18775. +#define SSAM_DEFINE_SYNC_REQUEST_N(name, spec...) \
  18776. + static int name(struct ssam_controller *ctrl) \
  18777. + { \
  18778. + struct ssam_request_spec s = (struct ssam_request_spec)spec; \
  18779. + struct ssam_request rqst; \
  18780. + \
  18781. + rqst.target_category = s.target_category; \
  18782. + rqst.target_id = s.target_id; \
  18783. + rqst.command_id = s.command_id; \
  18784. + rqst.instance_id = s.instance_id; \
  18785. + rqst.flags = s.flags; \
  18786. + rqst.length = 0; \
  18787. + rqst.payload = NULL; \
  18788. + \
  18789. + return ssam_request_sync_onstack(ctrl, &rqst, NULL, 0); \
  18790. + }
  18791. +
  18792. +/**
  18793. + * SSAM_DEFINE_SYNC_REQUEST_W() - Define synchronous SAM request function with
  18794. + * argument.
  18795. + * @name: Name of the generated function.
  18796. + * @atype: Type of the request's argument.
  18797. + * @spec: Specification (&struct ssam_request_spec) defining the request.
  18798. + *
  18799. + * Defines a function executing the synchronous SAM request specified by
  18800. + * @spec, with the request taking an argument of type @atype and having no
  18801. + * return value. The generated function takes care of setting up the request
  18802. + * struct, buffer allocation, as well as execution of the request itself,
  18803. + * returning once the request has been fully completed. The required transport
  18804. + * buffer will be allocated on the stack.
  18805. + *
  18806. + * The generated function is defined as ``static int name(struct
  18807. + * ssam_controller *ctrl, const atype *arg)``, returning the status of the
  18808. + * request, which is zero on success and negative on failure. The ``ctrl``
  18809. + * parameter is the controller via which the request is sent. The request
  18810. + * argument is specified via the ``arg`` pointer.
  18811. + *
  18812. + * Refer to ssam_request_sync_onstack() for more details on the behavior of
  18813. + * the generated function.
  18814. + */
  18815. +#define SSAM_DEFINE_SYNC_REQUEST_W(name, atype, spec...) \
  18816. + static int name(struct ssam_controller *ctrl, const atype *arg) \
  18817. + { \
  18818. + struct ssam_request_spec s = (struct ssam_request_spec)spec; \
  18819. + struct ssam_request rqst; \
  18820. + \
  18821. + rqst.target_category = s.target_category; \
  18822. + rqst.target_id = s.target_id; \
  18823. + rqst.command_id = s.command_id; \
  18824. + rqst.instance_id = s.instance_id; \
  18825. + rqst.flags = s.flags; \
  18826. + rqst.length = sizeof(atype); \
  18827. + rqst.payload = (u8 *)arg; \
  18828. + \
  18829. + return ssam_request_sync_onstack(ctrl, &rqst, NULL, \
  18830. + sizeof(atype)); \
  18831. + }
  18832. +
  18833. +/**
  18834. + * SSAM_DEFINE_SYNC_REQUEST_R() - Define synchronous SAM request function with
  18835. + * return value.
  18836. + * @name: Name of the generated function.
  18837. + * @rtype: Type of the request's return value.
  18838. + * @spec: Specification (&struct ssam_request_spec) defining the request.
  18839. + *
  18840. + * Defines a function executing the synchronous SAM request specified by
  18841. + * @spec, with the request taking no argument but having a return value of
  18842. + * type @rtype. The generated function takes care of setting up the request
  18843. + * and response structs, buffer allocation, as well as execution of the
  18844. + * request itself, returning once the request has been fully completed. The
  18845. + * required transport buffer will be allocated on the stack.
  18846. + *
  18847. + * The generated function is defined as ``static int name(struct
  18848. + * ssam_controller *ctrl, rtype *ret)``, returning the status of the request,
  18849. + * which is zero on success and negative on failure. The ``ctrl`` parameter is
  18850. + * the controller via which the request is sent. The request's return value is
  18851. + * written to the memory pointed to by the ``ret`` parameter.
  18852. + *
  18853. + * Refer to ssam_request_sync_onstack() for more details on the behavior of
  18854. + * the generated function.
  18855. + */
  18856. +#define SSAM_DEFINE_SYNC_REQUEST_R(name, rtype, spec...) \
  18857. + static int name(struct ssam_controller *ctrl, rtype *ret) \
  18858. + { \
  18859. + struct ssam_request_spec s = (struct ssam_request_spec)spec; \
  18860. + struct ssam_request rqst; \
  18861. + struct ssam_response rsp; \
  18862. + int status; \
  18863. + \
  18864. + rqst.target_category = s.target_category; \
  18865. + rqst.target_id = s.target_id; \
  18866. + rqst.command_id = s.command_id; \
  18867. + rqst.instance_id = s.instance_id; \
  18868. + rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE; \
  18869. + rqst.length = 0; \
  18870. + rqst.payload = NULL; \
  18871. + \
  18872. + rsp.capacity = sizeof(rtype); \
  18873. + rsp.length = 0; \
  18874. + rsp.pointer = (u8 *)ret; \
  18875. + \
  18876. + status = ssam_request_sync_onstack(ctrl, &rqst, &rsp, 0); \
  18877. + if (status) \
  18878. + return status; \
  18879. + \
  18880. + if (rsp.length != sizeof(rtype)) { \
  18881. + struct device *dev = ssam_controller_device(ctrl); \
  18882. + dev_err(dev, \
  18883. + "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
  18884. + sizeof(rtype), rsp.length, rqst.target_category,\
  18885. + rqst.command_id); \
  18886. + return -EIO; \
  18887. + } \
  18888. + \
  18889. + return 0; \
  18890. + }
  18891. +
  18892. +/**
  18893. + * SSAM_DEFINE_SYNC_REQUEST_MD_N() - Define synchronous multi-device SAM
  18894. + * request function with neither argument nor return value.
  18895. + * @name: Name of the generated function.
  18896. + * @spec: Specification (&struct ssam_request_spec_md) defining the request.
  18897. + *
  18898. + * Defines a function executing the synchronous SAM request specified by
  18899. + * @spec, with the request having neither argument nor return value. Device
  18900. + * specifying parameters are not hard-coded, but instead must be provided to
  18901. + * the function. The generated function takes care of setting up the request
  18902. + * struct, buffer allocation, as well as execution of the request itself,
  18903. + * returning once the request has been fully completed. The required transport
  18904. + * buffer will be allocated on the stack.
  18905. + *
  18906. + * The generated function is defined as ``static int name(struct
  18907. + * ssam_controller *ctrl, u8 tid, u8 iid)``, returning the status of the
  18908. + * request, which is zero on success and negative on failure. The ``ctrl``
  18909. + * parameter is the controller via which the request is sent, ``tid`` the
  18910. + * target ID for the request, and ``iid`` the instance ID.
  18911. + *
  18912. + * Refer to ssam_request_sync_onstack() for more details on the behavior of
  18913. + * the generated function.
  18914. + */
  18915. +#define SSAM_DEFINE_SYNC_REQUEST_MD_N(name, spec...) \
  18916. + static int name(struct ssam_controller *ctrl, u8 tid, u8 iid) \
  18917. + { \
  18918. + struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
  18919. + struct ssam_request rqst; \
  18920. + \
  18921. + rqst.target_category = s.target_category; \
  18922. + rqst.target_id = tid; \
  18923. + rqst.command_id = s.command_id; \
  18924. + rqst.instance_id = iid; \
  18925. + rqst.flags = s.flags; \
  18926. + rqst.length = 0; \
  18927. + rqst.payload = NULL; \
  18928. + \
  18929. + return ssam_request_sync_onstack(ctrl, &rqst, NULL, 0); \
  18930. + }
  18931. +
  18932. +/**
  18933. + * SSAM_DEFINE_SYNC_REQUEST_MD_W() - Define synchronous multi-device SAM
  18934. + * request function with argument.
  18935. + * @name: Name of the generated function.
  18936. + * @atype: Type of the request's argument.
  18937. + * @spec: Specification (&struct ssam_request_spec_md) defining the request.
  18938. + *
  18939. + * Defines a function executing the synchronous SAM request specified by
  18940. + * @spec, with the request taking an argument of type @atype and having no
  18941. + * return value. Device specifying parameters are not hard-coded, but instead
  18942. + * must be provided to the function. The generated function takes care of
  18943. + * setting up the request struct, buffer allocation, as well as execution of
  18944. + * the request itself, returning once the request has been fully completed.
  18945. + * The required transport buffer will be allocated on the stack.
  18946. + *
  18947. + * The generated function is defined as ``static int name(struct
  18948. + * ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg)``, returning the
  18949. + * status of the request, which is zero on success and negative on failure.
  18950. + * The ``ctrl`` parameter is the controller via which the request is sent,
  18951. + * ``tid`` the target ID for the request, and ``iid`` the instance ID. The
  18952. + * request argument is specified via the ``arg`` pointer.
  18953. + *
  18954. + * Refer to ssam_request_sync_onstack() for more details on the behavior of
  18955. + * the generated function.
  18956. + */
  18957. +#define SSAM_DEFINE_SYNC_REQUEST_MD_W(name, atype, spec...) \
  18958. + static int name(struct ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg) \
  18959. + { \
  18960. + struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
  18961. + struct ssam_request rqst; \
  18962. + \
  18963. + rqst.target_category = s.target_category; \
  18964. + rqst.target_id = tid; \
  18965. + rqst.command_id = s.command_id; \
  18966. + rqst.instance_id = iid; \
  18967. + rqst.flags = s.flags; \
  18968. + rqst.length = sizeof(atype); \
  18969. + rqst.payload = (u8 *)arg; \
  18970. + \
  18971. + return ssam_request_sync_onstack(ctrl, &rqst, NULL, \
  18972. + sizeof(atype)); \
  18973. + }
  18974. +
  18975. +/**
  18976. + * SSAM_DEFINE_SYNC_REQUEST_MD_R() - Define synchronous multi-device SAM
  18977. + * request function with return value.
  18978. + * @name: Name of the generated function.
  18979. + * @rtype: Type of the request's return value.
  18980. + * @spec: Specification (&struct ssam_request_spec_md) defining the request.
  18981. + *
  18982. + * Defines a function executing the synchronous SAM request specified by
  18983. + * @spec, with the request taking no argument but having a return value of
  18984. + * type @rtype. Device specifying parameters are not hard-coded, but instead
  18985. + * must be provided to the function. The generated function takes care of
  18986. + * setting up the request and response structs, buffer allocation, as well as
  18987. + * execution of the request itself, returning once the request has been fully
  18988. + * completed. The required transport buffer will be allocated on the stack.
  18989. + *
  18990. + * The generated function is defined as ``static int name(struct
  18991. + * ssam_controller *ctrl, u8 tid, u8 iid, rtype *ret)``, returning the status
  18992. + * of the request, which is zero on success and negative on failure. The
  18993. + * ``ctrl`` parameter is the controller via which the request is sent, ``tid``
  18994. + * the target ID for the request, and ``iid`` the instance ID. The request's
  18995. + * return value is written to the memory pointed to by the ``ret`` parameter.
  18996. + *
  18997. + * Refer to ssam_request_sync_onstack() for more details on the behavior of
  18998. + * the generated function.
  18999. + */
  19000. +#define SSAM_DEFINE_SYNC_REQUEST_MD_R(name, rtype, spec...) \
  19001. + static int name(struct ssam_controller *ctrl, u8 tid, u8 iid, rtype *ret) \
  19002. + { \
  19003. + struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
  19004. + struct ssam_request rqst; \
  19005. + struct ssam_response rsp; \
  19006. + int status; \
  19007. + \
  19008. + rqst.target_category = s.target_category; \
  19009. + rqst.target_id = tid; \
  19010. + rqst.command_id = s.command_id; \
  19011. + rqst.instance_id = iid; \
  19012. + rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE; \
  19013. + rqst.length = 0; \
  19014. + rqst.payload = NULL; \
  19015. + \
  19016. + rsp.capacity = sizeof(rtype); \
  19017. + rsp.length = 0; \
  19018. + rsp.pointer = (u8 *)ret; \
  19019. + \
  19020. + status = ssam_request_sync_onstack(ctrl, &rqst, &rsp, 0); \
  19021. + if (status) \
  19022. + return status; \
  19023. + \
  19024. + if (rsp.length != sizeof(rtype)) { \
  19025. + struct device *dev = ssam_controller_device(ctrl); \
  19026. + dev_err(dev, \
  19027. + "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
  19028. + sizeof(rtype), rsp.length, rqst.target_category,\
  19029. + rqst.command_id); \
  19030. + return -EIO; \
  19031. + } \
  19032. + \
  19033. + return 0; \
  19034. + }
  19035. +
  19036. +
  19037. +/* -- Event notifier/callbacks. --------------------------------------------- */
  19038. +
  19039. +#define SSAM_NOTIF_STATE_SHIFT 2
  19040. +#define SSAM_NOTIF_STATE_MASK ((1 << SSAM_NOTIF_STATE_SHIFT) - 1)
  19041. +
  19042. +/**
  19043. + * enum ssam_notif_flags - Flags used in return values from SSAM notifier
  19044. + * callback functions.
  19045. + *
  19046. + * @SSAM_NOTIF_HANDLED:
  19047. + * Indicates that the notification has been handled. This flag should be
  19048. + * set by the handler if the handler can act/has acted upon the event
  19049. + * provided to it. This flag should not be set if the handler is not a
  19050. + * primary handler intended for the provided event.
  19051. + *
  19052. + * If this flag has not been set by any handler after the notifier chain
  19053. + * has been traversed, a warning will be emitted, stating that the event
  19054. + * has not been handled.
  19055. + *
  19056. + * @SSAM_NOTIF_STOP:
  19057. + * Indicates that the notifier traversal should stop. If this flag is
  19058. + * returned from a notifier callback, notifier chain traversal will
  19059. + * immediately stop and any remaining notifiers will not be called. This
  19060. + * flag is automatically set when ssam_notifier_from_errno() is called
  19061. + * with a negative error value.
  19062. + */
  19063. +enum ssam_notif_flags {
  19064. + SSAM_NOTIF_HANDLED = BIT(0),
  19065. + SSAM_NOTIF_STOP = BIT(1),
  19066. +};
  19067. +
  19068. +struct ssam_event_notifier;
  19069. +
  19070. +typedef u32 (*ssam_notifier_fn_t)(struct ssam_event_notifier *nf,
  19071. + const struct ssam_event *event);
  19072. +
  19073. +/**
  19074. + * struct ssam_notifier_block - Base notifier block for SSAM event
  19075. + * notifications.
  19076. + * @node: The node for the list of notifiers.
  19077. + * @fn: The callback function of this notifier. This function takes the
  19078. + * respective notifier block and event as input and should return
  19079. + * a notifier value, which can either be obtained from the flags
  19080. + * provided in &enum ssam_notif_flags, converted from a standard
  19081. + * error value via ssam_notifier_from_errno(), or a combination of
  19082. + * both (e.g. ``ssam_notifier_from_errno(e) | SSAM_NOTIF_HANDLED``).
  19083. + * @priority: Priority value determining the order in which notifier callbacks
  19084. + * will be called. A higher value means higher priority, i.e. the
  19085. + * associated callback will be executed earlier than other (lower
  19086. + * priority) callbacks.
  19087. + */
  19088. +struct ssam_notifier_block {
  19089. + struct list_head node;
  19090. + ssam_notifier_fn_t fn;
  19091. + int priority;
  19092. +};
  19093. +
  19094. +/**
  19095. + * ssam_notifier_from_errno() - Convert standard error value to notifier
  19096. + * return code.
  19097. + * @err: The error code to convert, must be negative (in case of failure) or
  19098. + * zero (in case of success).
  19099. + *
  19100. + * Return: Returns the notifier return value obtained by converting the
  19101. + * specified @err value. In case @err is negative, the %SSAM_NOTIF_STOP flag
  19102. + * will be set, causing notifier call chain traversal to abort.
  19103. + */
  19104. +static inline u32 ssam_notifier_from_errno(int err)
  19105. +{
  19106. + if (WARN_ON(err > 0) || err == 0)
  19107. + return 0;
  19108. + else
  19109. + return ((-err) << SSAM_NOTIF_STATE_SHIFT) | SSAM_NOTIF_STOP;
  19110. +}
  19111. +
  19112. +/**
  19113. + * ssam_notifier_to_errno() - Convert notifier return code to standard error
  19114. + * value.
  19115. + * @ret: The notifier return value to convert.
  19116. + *
  19117. + * Return: Returns the negative error value encoded in @ret or zero if @ret
  19118. + * indicates success.
  19119. + */
  19120. +static inline int ssam_notifier_to_errno(u32 ret)
  19121. +{
  19122. + return -(ret >> SSAM_NOTIF_STATE_SHIFT);
  19123. +}
  19124. +
  19125. +
  19126. +/* -- Event/notification registry. ------------------------------------------ */
  19127. +
  19128. +/**
  19129. + * struct ssam_event_registry - Registry specification used for enabling events.
  19130. + * @target_category: Target category for the event registry requests.
  19131. + * @target_id: Target ID for the event registry requests.
  19132. + * @cid_enable: Command ID for the event-enable request.
  19133. + * @cid_disable: Command ID for the event-disable request.
  19134. + *
  19135. + * This struct describes a SAM event registry via the minimal collection of
  19136. + * SAM IDs specifying the requests to use for enabling and disabling an event.
  19137. + * The individual event to be enabled/disabled itself is specified via &struct
  19138. + * ssam_event_id.
  19139. + */
  19140. +struct ssam_event_registry {
  19141. + u8 target_category;
  19142. + u8 target_id;
  19143. + u8 cid_enable;
  19144. + u8 cid_disable;
  19145. +};
  19146. +
  19147. +/**
  19148. + * struct ssam_event_id - Unique event ID used for enabling events.
  19149. + * @target_category: Target category of the event source.
  19150. + * @instance: Instance ID of the event source.
  19151. + *
  19152. + * This struct specifies the event to be enabled/disabled via an externally
  19153. + * provided registry. It does not specify the registry to be used itself, this
  19154. + * is done via &struct ssam_event_registry.
  19155. + */
  19156. +struct ssam_event_id {
  19157. + u8 target_category;
  19158. + u8 instance;
  19159. +};
  19160. +
  19161. +/**
  19162. + * enum ssam_event_mask - Flags specifying how events are matched to notifiers.
  19163. + *
  19164. + * @SSAM_EVENT_MASK_NONE:
  19165. + * Run the callback for any event with matching target category. Do not
  19166. + * do any additional filtering.
  19167. + *
  19168. + * @SSAM_EVENT_MASK_TARGET:
  19169. + * In addition to filtering by target category, only execute the notifier
  19170. + * callback for events with a target ID matching to the one of the
  19171. + * registry used for enabling/disabling the event.
  19172. + *
  19173. + * @SSAM_EVENT_MASK_INSTANCE:
  19174. + * In addition to filtering by target category, only execute the notifier
  19175. + * callback for events with an instance ID matching to the instance ID
  19176. + * used when enabling the event.
  19177. + *
  19178. + * @SSAM_EVENT_MASK_STRICT:
  19179. + * Do all the filtering above.
  19180. + */
  19181. +enum ssam_event_mask {
  19182. + SSAM_EVENT_MASK_TARGET = BIT(0),
  19183. + SSAM_EVENT_MASK_INSTANCE = BIT(1),
  19184. +
  19185. + SSAM_EVENT_MASK_NONE = 0,
  19186. + SSAM_EVENT_MASK_STRICT =
  19187. + SSAM_EVENT_MASK_TARGET
  19188. + | SSAM_EVENT_MASK_INSTANCE,
  19189. +};
  19190. +
  19191. +/**
  19192. + * SSAM_EVENT_REGISTRY() - Define a new event registry.
  19193. + * @tc: Target category for the event registry requests.
  19194. + * @tid: Target ID for the event registry requests.
  19195. + * @cid_en: Command ID for the event-enable request.
  19196. + * @cid_dis: Command ID for the event-disable request.
  19197. + *
  19198. + * Return: Returns the &struct ssam_event_registry specified by the given
  19199. + * parameters.
  19200. + */
  19201. +#define SSAM_EVENT_REGISTRY(tc, tid, cid_en, cid_dis) \
  19202. + ((struct ssam_event_registry) { \
  19203. + .target_category = (tc), \
  19204. + .target_id = (tid), \
  19205. + .cid_enable = (cid_en), \
  19206. + .cid_disable = (cid_dis), \
  19207. + })
  19208. +
  19209. +#define SSAM_EVENT_REGISTRY_SAM \
  19210. + SSAM_EVENT_REGISTRY(SSAM_SSH_TC_SAM, 0x01, 0x0b, 0x0c)
  19211. +
  19212. +#define SSAM_EVENT_REGISTRY_KIP \
  19213. + SSAM_EVENT_REGISTRY(SSAM_SSH_TC_KIP, 0x02, 0x27, 0x28)
  19214. +
  19215. +#define SSAM_EVENT_REGISTRY_REG \
  19216. + SSAM_EVENT_REGISTRY(SSAM_SSH_TC_REG, 0x02, 0x01, 0x02)
  19217. +
  19218. +/**
  19219. + * enum ssam_event_notifier_flags - Flags for event notifiers.
  19220. + * @SSAM_EVENT_NOTIFIER_OBSERVER:
  19221. + * The corresponding notifier acts as observer. Registering a notifier
  19222. + * with this flag set will not attempt to enable any event. Equally,
  19223. + * unregistering will not attempt to disable any event. Note that a
  19224. + * notifier with this flag may not even correspond to a certain event at
  19225. + * all, only to a specific event target category. Event matching will not
  19226. + * be influenced by this flag.
  19227. + */
  19228. +enum ssam_event_notifier_flags {
  19229. + SSAM_EVENT_NOTIFIER_OBSERVER = BIT(0),
  19230. +};
  19231. +
  19232. +/**
  19233. + * struct ssam_event_notifier - Notifier block for SSAM events.
  19234. + * @base: The base notifier block with callback function and priority.
  19235. + * @event: The event for which this block will receive notifications.
  19236. + * @event.reg: Registry via which the event will be enabled/disabled.
  19237. + * @event.id: ID specifying the event.
  19238. + * @event.mask: Flags determining how events are matched to the notifier.
  19239. + * @event.flags: Flags used for enabling the event.
  19240. + * @flags: Notifier flags (see &enum ssam_event_notifier_flags).
  19241. + */
  19242. +struct ssam_event_notifier {
  19243. + struct ssam_notifier_block base;
  19244. +
  19245. + struct {
  19246. + struct ssam_event_registry reg;
  19247. + struct ssam_event_id id;
  19248. + enum ssam_event_mask mask;
  19249. + u8 flags;
  19250. + } event;
  19251. +
  19252. + unsigned long flags;
  19253. +};
  19254. +
  19255. +int ssam_notifier_register(struct ssam_controller *ctrl,
  19256. + struct ssam_event_notifier *n);
  19257. +
  19258. +int ssam_notifier_unregister(struct ssam_controller *ctrl,
  19259. + struct ssam_event_notifier *n);
  19260. +
  19261. +int ssam_controller_event_enable(struct ssam_controller *ctrl,
  19262. + struct ssam_event_registry reg,
  19263. + struct ssam_event_id id, u8 flags);
  19264. +
  19265. +int ssam_controller_event_disable(struct ssam_controller *ctrl,
  19266. + struct ssam_event_registry reg,
  19267. + struct ssam_event_id id, u8 flags);
  19268. +
  19269. +#endif /* _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H */
  19270. diff --git a/include/linux/surface_aggregator/device.h b/include/linux/surface_aggregator/device.h
  19271. new file mode 100644
  19272. index 000000000000..f636c5310321
  19273. --- /dev/null
  19274. +++ b/include/linux/surface_aggregator/device.h
  19275. @@ -0,0 +1,424 @@
  19276. +/* SPDX-License-Identifier: GPL-2.0+ */
  19277. +/*
  19278. + * Surface System Aggregator Module (SSAM) bus and client-device subsystem.
  19279. + *
  19280. + * Main interface for the surface-aggregator bus, surface-aggregator client
  19281. + * devices, and respective drivers building on top of the SSAM controller.
  19282. + * Provides support for non-platform/non-ACPI SSAM clients via dedicated
  19283. + * subsystem.
  19284. + *
  19285. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  19286. + */
  19287. +
  19288. +#ifndef _LINUX_SURFACE_AGGREGATOR_DEVICE_H
  19289. +#define _LINUX_SURFACE_AGGREGATOR_DEVICE_H
  19290. +
  19291. +#include <linux/device.h>
  19292. +#include <linux/mod_devicetable.h>
  19293. +#include <linux/types.h>
  19294. +
  19295. +#include <linux/surface_aggregator/controller.h>
  19296. +
  19297. +
  19298. +/* -- Surface System Aggregator Module bus. --------------------------------- */
  19299. +
  19300. +/**
  19301. + * enum ssam_device_domain - SAM device domain.
  19302. + * @SSAM_DOMAIN_VIRTUAL: Virtual device.
  19303. + * @SSAM_DOMAIN_SERIALHUB: Physical device connected via Surface Serial Hub.
  19304. + */
  19305. +enum ssam_device_domain {
  19306. + SSAM_DOMAIN_VIRTUAL = 0x00,
  19307. + SSAM_DOMAIN_SERIALHUB = 0x01,
  19308. +};
  19309. +
  19310. +/**
  19311. + * enum ssam_virtual_tc - Target categories for the virtual SAM domain.
  19312. + * @SSAM_VIRTUAL_TC_HUB: Device hub category.
  19313. + */
  19314. +enum ssam_virtual_tc {
  19315. + SSAM_VIRTUAL_TC_HUB = 0x00,
  19316. +};
  19317. +
  19318. +/**
  19319. + * struct ssam_device_uid - Unique identifier for SSAM device.
  19320. + * @domain: Domain of the device.
  19321. + * @category: Target category of the device.
  19322. + * @target: Target ID of the device.
  19323. + * @instance: Instance ID of the device.
  19324. + * @function: Sub-function of the device. This field can be used to split a
  19325. + * single SAM device into multiple virtual subdevices to separate
  19326. + * different functionality of that device and allow one driver per
  19327. + * such functionality.
  19328. + */
  19329. +struct ssam_device_uid {
  19330. + u8 domain;
  19331. + u8 category;
  19332. + u8 target;
  19333. + u8 instance;
  19334. + u8 function;
  19335. +};
  19336. +
  19337. +/*
  19338. + * Special values for device matching.
  19339. + *
  19340. + * These values are intended to be used with SSAM_DEVICE(), SSAM_VDEV(), and
  19341. + * SSAM_SDEV() exclusively. Specifically, they are used to initialize the
  19342. + * match_flags member of the device ID structure. Do not use them directly
  19343. + * with struct ssam_device_id or struct ssam_device_uid.
  19344. + */
  19345. +#define SSAM_ANY_TID 0xffff
  19346. +#define SSAM_ANY_IID 0xffff
  19347. +#define SSAM_ANY_FUN 0xffff
  19348. +
  19349. +/**
  19350. + * SSAM_DEVICE() - Initialize a &struct ssam_device_id with the given
  19351. + * parameters.
  19352. + * @d: Domain of the device.
  19353. + * @cat: Target category of the device.
  19354. + * @tid: Target ID of the device.
  19355. + * @iid: Instance ID of the device.
  19356. + * @fun: Sub-function of the device.
  19357. + *
  19358. + * Initializes a &struct ssam_device_id with the given parameters. See &struct
  19359. + * ssam_device_uid for details regarding the parameters. The special values
  19360. + * %SSAM_ANY_TID, %SSAM_ANY_IID, and %SSAM_ANY_FUN can be used to specify that
  19361. + * matching should ignore target ID, instance ID, and/or sub-function,
  19362. + * respectively. This macro initializes the ``match_flags`` field based on the
  19363. + * given parameters.
  19364. + *
  19365. + * Note: The parameters @d and @cat must be valid &u8 values, the parameters
  19366. + * @tid, @iid, and @fun must be either valid &u8 values or %SSAM_ANY_TID,
  19367. + * %SSAM_ANY_IID, or %SSAM_ANY_FUN, respectively. Other non-&u8 values are not
  19368. + * allowed.
  19369. + */
  19370. +#define SSAM_DEVICE(d, cat, tid, iid, fun) \
  19371. + .match_flags = (((tid) != SSAM_ANY_TID) ? SSAM_MATCH_TARGET : 0) \
  19372. + | (((iid) != SSAM_ANY_IID) ? SSAM_MATCH_INSTANCE : 0) \
  19373. + | (((fun) != SSAM_ANY_FUN) ? SSAM_MATCH_FUNCTION : 0), \
  19374. + .domain = d, \
  19375. + .category = cat, \
  19376. + .target = __builtin_choose_expr((tid) != SSAM_ANY_TID, (tid), 0), \
  19377. + .instance = __builtin_choose_expr((iid) != SSAM_ANY_IID, (iid), 0), \
  19378. + .function = __builtin_choose_expr((fun) != SSAM_ANY_FUN, (fun), 0)
  19379. +
  19380. +/**
  19381. + * SSAM_VDEV() - Initialize a &struct ssam_device_id as virtual device with
  19382. + * the given parameters.
  19383. + * @cat: Target category of the device.
  19384. + * @tid: Target ID of the device.
  19385. + * @iid: Instance ID of the device.
  19386. + * @fun: Sub-function of the device.
  19387. + *
  19388. + * Initializes a &struct ssam_device_id with the given parameters in the
  19389. + * virtual domain. See &struct ssam_device_uid for details regarding the
  19390. + * parameters. The special values %SSAM_ANY_TID, %SSAM_ANY_IID, and
  19391. + * %SSAM_ANY_FUN can be used to specify that matching should ignore target ID,
  19392. + * instance ID, and/or sub-function, respectively. This macro initializes the
  19393. + * ``match_flags`` field based on the given parameters.
  19394. + *
  19395. + * Note: The parameter @cat must be a valid &u8 value, the parameters @tid,
  19396. + * @iid, and @fun must be either valid &u8 values or %SSAM_ANY_TID,
  19397. + * %SSAM_ANY_IID, or %SSAM_ANY_FUN, respectively. Other non-&u8 values are not
  19398. + * allowed.
  19399. + */
  19400. +#define SSAM_VDEV(cat, tid, iid, fun) \
  19401. + SSAM_DEVICE(SSAM_DOMAIN_VIRTUAL, SSAM_VIRTUAL_TC_##cat, tid, iid, fun)
  19402. +
  19403. +/**
  19404. + * SSAM_SDEV() - Initialize a &struct ssam_device_id as physical SSH device
  19405. + * with the given parameters.
  19406. + * @cat: Target category of the device.
  19407. + * @tid: Target ID of the device.
  19408. + * @iid: Instance ID of the device.
  19409. + * @fun: Sub-function of the device.
  19410. + *
  19411. + * Initializes a &struct ssam_device_id with the given parameters in the SSH
  19412. + * domain. See &struct ssam_device_uid for details regarding the parameters.
  19413. + * The special values %SSAM_ANY_TID, %SSAM_ANY_IID, and %SSAM_ANY_FUN can be
  19414. + * used to specify that matching should ignore target ID, instance ID, and/or
  19415. + * sub-function, respectively. This macro initializes the ``match_flags``
  19416. + * field based on the given parameters.
  19417. + *
  19418. + * Note: The parameter @cat must be a valid &u8 value, the parameters @tid,
  19419. + * @iid, and @fun must be either valid &u8 values or %SSAM_ANY_TID,
  19420. + * %SSAM_ANY_IID, or %SSAM_ANY_FUN, respectively. Other non-&u8 values are not
  19421. + * allowed.
  19422. + */
  19423. +#define SSAM_SDEV(cat, tid, iid, fun) \
  19424. + SSAM_DEVICE(SSAM_DOMAIN_SERIALHUB, SSAM_SSH_TC_##cat, tid, iid, fun)
  19425. +
  19426. +/**
  19427. + * struct ssam_device - SSAM client device.
  19428. + * @dev: Driver model representation of the device.
  19429. + * @ctrl: SSAM controller managing this device.
  19430. + * @uid: UID identifying the device.
  19431. + */
  19432. +struct ssam_device {
  19433. + struct device dev;
  19434. + struct ssam_controller *ctrl;
  19435. +
  19436. + struct ssam_device_uid uid;
  19437. +};
  19438. +
  19439. +/**
  19440. + * struct ssam_device_driver - SSAM client device driver.
  19441. + * @driver: Base driver model structure.
  19442. + * @match_table: Match table specifying which devices the driver should bind to.
  19443. + * @probe: Called when the driver is being bound to a device.
  19444. + * @remove: Called when the driver is being unbound from the device.
  19445. + */
  19446. +struct ssam_device_driver {
  19447. + struct device_driver driver;
  19448. +
  19449. + const struct ssam_device_id *match_table;
  19450. +
  19451. + int (*probe)(struct ssam_device *sdev);
  19452. + void (*remove)(struct ssam_device *sdev);
  19453. +};
  19454. +
  19455. +extern struct bus_type ssam_bus_type;
  19456. +extern const struct device_type ssam_device_type;
  19457. +
  19458. +/**
  19459. + * is_ssam_device() - Check if the given device is a SSAM client device.
  19460. + * @d: The device to test the type of.
  19461. + *
  19462. + * Return: Returns %true if the specified device is of type &struct
  19463. + * ssam_device, i.e. the device type points to %ssam_device_type, and %false
  19464. + * otherwise.
  19465. + */
  19466. +static inline bool is_ssam_device(struct device *d)
  19467. +{
  19468. + return d->type == &ssam_device_type;
  19469. +}
  19470. +
  19471. +/**
  19472. + * to_ssam_device() - Casts the given device to a SSAM client device.
  19473. + * @d: The device to cast.
  19474. + *
  19475. + * Casts the given &struct device to a &struct ssam_device. The caller has to
  19476. + * ensure that the given device is actually enclosed in a &struct ssam_device,
  19477. + * e.g. by calling is_ssam_device().
  19478. + *
  19479. + * Return: Returns a pointer to the &struct ssam_device wrapping the given
  19480. + * device @d.
  19481. + */
  19482. +static inline struct ssam_device *to_ssam_device(struct device *d)
  19483. +{
  19484. + return container_of(d, struct ssam_device, dev);
  19485. +}
  19486. +
  19487. +/**
  19488. + * to_ssam_device_driver() - Casts the given device driver to a SSAM client
  19489. + * device driver.
  19490. + * @d: The driver to cast.
  19491. + *
  19492. + * Casts the given &struct device_driver to a &struct ssam_device_driver. The
  19493. + * caller has to ensure that the given driver is actually enclosed in a
  19494. + * &struct ssam_device_driver.
  19495. + *
  19496. + * Return: Returns the pointer to the &struct ssam_device_driver wrapping the
  19497. + * given device driver @d.
  19498. + */
  19499. +static inline
  19500. +struct ssam_device_driver *to_ssam_device_driver(struct device_driver *d)
  19501. +{
  19502. + return container_of(d, struct ssam_device_driver, driver);
  19503. +}
  19504. +
  19505. +const struct ssam_device_id *ssam_device_id_match(const struct ssam_device_id *table,
  19506. + const struct ssam_device_uid uid);
  19507. +
  19508. +const struct ssam_device_id *ssam_device_get_match(const struct ssam_device *dev);
  19509. +
  19510. +const void *ssam_device_get_match_data(const struct ssam_device *dev);
  19511. +
  19512. +struct ssam_device *ssam_device_alloc(struct ssam_controller *ctrl,
  19513. + struct ssam_device_uid uid);
  19514. +
  19515. +int ssam_device_add(struct ssam_device *sdev);
  19516. +void ssam_device_remove(struct ssam_device *sdev);
  19517. +
  19518. +/**
  19519. + * ssam_device_get() - Increment reference count of SSAM client device.
  19520. + * @sdev: The device to increment the reference count of.
  19521. + *
  19522. + * Increments the reference count of the given SSAM client device by
  19523. + * incrementing the reference count of the enclosed &struct device via
  19524. + * get_device().
  19525. + *
  19526. + * See ssam_device_put() for the counter-part of this function.
  19527. + *
  19528. + * Return: Returns the device provided as input.
  19529. + */
  19530. +static inline struct ssam_device *ssam_device_get(struct ssam_device *sdev)
  19531. +{
  19532. + return sdev ? to_ssam_device(get_device(&sdev->dev)) : NULL;
  19533. +}
  19534. +
  19535. +/**
  19536. + * ssam_device_put() - Decrement reference count of SSAM client device.
  19537. + * @sdev: The device to decrement the reference count of.
  19538. + *
  19539. + * Decrements the reference count of the given SSAM client device by
  19540. + * decrementing the reference count of the enclosed &struct device via
  19541. + * put_device().
  19542. + *
  19543. + * See ssam_device_get() for the counter-part of this function.
  19544. + */
  19545. +static inline void ssam_device_put(struct ssam_device *sdev)
  19546. +{
  19547. + if (sdev)
  19548. + put_device(&sdev->dev);
  19549. +}
  19550. +
  19551. +/**
  19552. + * ssam_device_get_drvdata() - Get driver-data of SSAM client device.
  19553. + * @sdev: The device to get the driver-data from.
  19554. + *
  19555. + * Return: Returns the driver-data of the given device, previously set via
  19556. + * ssam_device_set_drvdata().
  19557. + */
  19558. +static inline void *ssam_device_get_drvdata(struct ssam_device *sdev)
  19559. +{
  19560. + return dev_get_drvdata(&sdev->dev);
  19561. +}
  19562. +
  19563. +/**
  19564. + * ssam_device_set_drvdata() - Set driver-data of SSAM client device.
  19565. + * @sdev: The device to set the driver-data of.
  19566. + * @data: The data to set the device's driver-data pointer to.
  19567. + */
  19568. +static inline void ssam_device_set_drvdata(struct ssam_device *sdev, void *data)
  19569. +{
  19570. + dev_set_drvdata(&sdev->dev, data);
  19571. +}
  19572. +
  19573. +int __ssam_device_driver_register(struct ssam_device_driver *d, struct module *o);
  19574. +void ssam_device_driver_unregister(struct ssam_device_driver *d);
  19575. +
  19576. +/**
  19577. + * ssam_device_driver_register() - Register a SSAM client device driver.
  19578. + * @drv: The driver to register.
  19579. + */
  19580. +#define ssam_device_driver_register(drv) \
  19581. + __ssam_device_driver_register(drv, THIS_MODULE)
  19582. +
  19583. +/**
  19584. + * module_ssam_device_driver() - Helper macro for SSAM device driver
  19585. + * registration.
  19586. + * @drv: The driver managed by this module.
  19587. + *
  19588. + * Helper macro to register a SSAM device driver via module_init() and
  19589. + * module_exit(). This macro may only be used once per module and replaces the
  19590. + * aforementioned definitions.
  19591. + */
  19592. +#define module_ssam_device_driver(drv) \
  19593. + module_driver(drv, ssam_device_driver_register, \
  19594. + ssam_device_driver_unregister)
  19595. +
  19596. +
  19597. +/* -- Helpers for client-device requests. ----------------------------------- */
  19598. +
  19599. +/**
  19600. + * SSAM_DEFINE_SYNC_REQUEST_CL_N() - Define synchronous client-device SAM
  19601. + * request function with neither argument nor return value.
  19602. + * @name: Name of the generated function.
  19603. + * @spec: Specification (&struct ssam_request_spec_md) defining the request.
  19604. + *
  19605. + * Defines a function executing the synchronous SAM request specified by
  19606. + * @spec, with the request having neither argument nor return value. Device
  19607. + * specifying parameters are not hard-coded, but instead are provided via the
  19608. + * client device, specifically its UID, supplied when calling this function.
  19609. + * The generated function takes care of setting up the request struct, buffer
  19610. + * allocation, as well as execution of the request itself, returning once the
  19611. + * request has been fully completed. The required transport buffer will be
  19612. + * allocated on the stack.
  19613. + *
  19614. + * The generated function is defined as ``static int name(struct ssam_device
  19615. + * *sdev)``, returning the status of the request, which is zero on success and
  19616. + * negative on failure. The ``sdev`` parameter specifies both the target
  19617. + * device of the request and by association the controller via which the
  19618. + * request is sent.
  19619. + *
  19620. + * Refer to ssam_request_sync_onstack() for more details on the behavior of
  19621. + * the generated function.
  19622. + */
  19623. +#define SSAM_DEFINE_SYNC_REQUEST_CL_N(name, spec...) \
  19624. + SSAM_DEFINE_SYNC_REQUEST_MD_N(__raw_##name, spec) \
  19625. + static int name(struct ssam_device *sdev) \
  19626. + { \
  19627. + return __raw_##name(sdev->ctrl, sdev->uid.target, \
  19628. + sdev->uid.instance); \
  19629. + }
  19630. +
  19631. +/**
  19632. + * SSAM_DEFINE_SYNC_REQUEST_CL_W() - Define synchronous client-device SAM
  19633. + * request function with argument.
  19634. + * @name: Name of the generated function.
  19635. + * @atype: Type of the request's argument.
  19636. + * @spec: Specification (&struct ssam_request_spec_md) defining the request.
  19637. + *
  19638. + * Defines a function executing the synchronous SAM request specified by
  19639. + * @spec, with the request taking an argument of type @atype and having no
  19640. + * return value. Device specifying parameters are not hard-coded, but instead
  19641. + * are provided via the client device, specifically its UID, supplied when
  19642. + * calling this function. The generated function takes care of setting up the
  19643. + * request struct, buffer allocation, as well as execution of the request
  19644. + * itself, returning once the request has been fully completed. The required
  19645. + * transport buffer will be allocated on the stack.
  19646. + *
  19647. + * The generated function is defined as ``static int name(struct ssam_device
  19648. + * *sdev, const atype *arg)``, returning the status of the request, which is
  19649. + * zero on success and negative on failure. The ``sdev`` parameter specifies
  19650. + * both the target device of the request and by association the controller via
  19651. + * which the request is sent. The request's argument is specified via the
  19652. + * ``arg`` pointer.
  19653. + *
  19654. + * Refer to ssam_request_sync_onstack() for more details on the behavior of
  19655. + * the generated function.
  19656. + */
  19657. +#define SSAM_DEFINE_SYNC_REQUEST_CL_W(name, atype, spec...) \
  19658. + SSAM_DEFINE_SYNC_REQUEST_MD_W(__raw_##name, atype, spec) \
  19659. + static int name(struct ssam_device *sdev, const atype *arg) \
  19660. + { \
  19661. + return __raw_##name(sdev->ctrl, sdev->uid.target, \
  19662. + sdev->uid.instance, arg); \
  19663. + }
  19664. +
  19665. +/**
  19666. + * SSAM_DEFINE_SYNC_REQUEST_CL_R() - Define synchronous client-device SAM
  19667. + * request function with return value.
  19668. + * @name: Name of the generated function.
  19669. + * @rtype: Type of the request's return value.
  19670. + * @spec: Specification (&struct ssam_request_spec_md) defining the request.
  19671. + *
  19672. + * Defines a function executing the synchronous SAM request specified by
  19673. + * @spec, with the request taking no argument but having a return value of
  19674. + * type @rtype. Device specifying parameters are not hard-coded, but instead
  19675. + * are provided via the client device, specifically its UID, supplied when
  19676. + * calling this function. The generated function takes care of setting up the
  19677. + * request struct, buffer allocation, as well as execution of the request
  19678. + * itself, returning once the request has been fully completed. The required
  19679. + * transport buffer will be allocated on the stack.
  19680. + *
  19681. + * The generated function is defined as ``static int name(struct ssam_device
  19682. + * *sdev, rtype *ret)``, returning the status of the request, which is zero on
  19683. + * success and negative on failure. The ``sdev`` parameter specifies both the
  19684. + * target device of the request and by association the controller via which
  19685. + * the request is sent. The request's return value is written to the memory
  19686. + * pointed to by the ``ret`` parameter.
  19687. + *
  19688. + * Refer to ssam_request_sync_onstack() for more details on the behavior of
  19689. + * the generated function.
  19690. + */
  19691. +#define SSAM_DEFINE_SYNC_REQUEST_CL_R(name, rtype, spec...) \
  19692. + SSAM_DEFINE_SYNC_REQUEST_MD_R(__raw_##name, rtype, spec) \
  19693. + static int name(struct ssam_device *sdev, rtype *ret) \
  19694. + { \
  19695. + return __raw_##name(sdev->ctrl, sdev->uid.target, \
  19696. + sdev->uid.instance, ret); \
  19697. + }
  19698. +
  19699. +#endif /* _LINUX_SURFACE_AGGREGATOR_DEVICE_H */
  19700. diff --git a/include/linux/surface_aggregator/serial_hub.h b/include/linux/surface_aggregator/serial_hub.h
  19701. new file mode 100644
  19702. index 000000000000..c3de43edcffa
  19703. --- /dev/null
  19704. +++ b/include/linux/surface_aggregator/serial_hub.h
  19705. @@ -0,0 +1,672 @@
  19706. +/* SPDX-License-Identifier: GPL-2.0+ */
  19707. +/*
  19708. + * Surface Serial Hub (SSH) protocol and communication interface.
  19709. + *
  19710. + * Lower-level communication layers and SSH protocol definitions for the
  19711. + * Surface System Aggregator Module (SSAM). Provides the interface for basic
  19712. + * packet- and request-based communication with the SSAM EC via SSH.
  19713. + *
  19714. + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
  19715. + */
  19716. +
  19717. +#ifndef _LINUX_SURFACE_AGGREGATOR_SERIAL_HUB_H
  19718. +#define _LINUX_SURFACE_AGGREGATOR_SERIAL_HUB_H
  19719. +
  19720. +#include <linux/crc-ccitt.h>
  19721. +#include <linux/kref.h>
  19722. +#include <linux/ktime.h>
  19723. +#include <linux/list.h>
  19724. +#include <linux/types.h>
  19725. +
  19726. +
  19727. +/* -- Data structures for SAM-over-SSH communication. ----------------------- */
  19728. +
  19729. +/**
  19730. + * enum ssh_frame_type - Frame types for SSH frames.
  19731. + *
  19732. + * @SSH_FRAME_TYPE_DATA_SEQ:
  19733. + * Indicates a data frame, followed by a payload with the length specified
  19734. + * in the ``struct ssh_frame.len`` field. This frame is sequenced, meaning
  19735. + * that an ACK is required.
  19736. + *
  19737. + * @SSH_FRAME_TYPE_DATA_NSQ:
  19738. + * Same as %SSH_FRAME_TYPE_DATA_SEQ, but unsequenced, meaning that the
  19739. + * message does not have to be ACKed.
  19740. + *
  19741. + * @SSH_FRAME_TYPE_ACK:
  19742. + * Indicates an ACK message.
  19743. + *
  19744. + * @SSH_FRAME_TYPE_NAK:
  19745. + * Indicates an error response for previously sent frame. In general, this
  19746. + * means that the frame and/or payload is malformed, e.g. a CRC is wrong.
  19747. + * For command-type payloads, this can also mean that the command is
  19748. + * invalid.
  19749. + */
  19750. +enum ssh_frame_type {
  19751. + SSH_FRAME_TYPE_DATA_SEQ = 0x80,
  19752. + SSH_FRAME_TYPE_DATA_NSQ = 0x00,
  19753. + SSH_FRAME_TYPE_ACK = 0x40,
  19754. + SSH_FRAME_TYPE_NAK = 0x04,
  19755. +};
  19756. +
  19757. +/**
  19758. + * struct ssh_frame - SSH communication frame.
  19759. + * @type: The type of the frame. See &enum ssh_frame_type.
  19760. + * @len: The length of the frame payload directly following the CRC for this
  19761. + * frame. Does not include the final CRC for that payload.
  19762. + * @seq: The sequence number for this message/exchange.
  19763. + */
  19764. +struct ssh_frame {
  19765. + u8 type;
  19766. + __le16 len;
  19767. + u8 seq;
  19768. +} __packed;
  19769. +
  19770. +static_assert(sizeof(struct ssh_frame) == 4);
  19771. +
  19772. +/*
  19773. + * SSH_FRAME_MAX_PAYLOAD_SIZE - Maximum SSH frame payload length in bytes.
  19774. + *
  19775. + * This is the physical maximum length of the protocol. Implementations may
  19776. + * set a more constrained limit.
  19777. + */
  19778. +#define SSH_FRAME_MAX_PAYLOAD_SIZE U16_MAX
  19779. +
  19780. +/**
  19781. + * enum ssh_payload_type - Type indicator for the SSH payload.
  19782. + * @SSH_PLD_TYPE_CMD: The payload is a command structure with optional command
  19783. + * payload.
  19784. + */
  19785. +enum ssh_payload_type {
  19786. + SSH_PLD_TYPE_CMD = 0x80,
  19787. +};
  19788. +
  19789. +/**
  19790. + * struct ssh_command - Payload of a command-type frame.
  19791. + * @type: The type of the payload. See &enum ssh_payload_type. Should be
  19792. + * SSH_PLD_TYPE_CMD for this struct.
  19793. + * @tc: Command target category.
  19794. + * @tid_out: Output target ID. Should be zero if this an incoming (EC to host)
  19795. + * message.
  19796. + * @tid_in: Input target ID. Should be zero if this is an outgoing (host to
  19797. + * EC) message.
  19798. + * @iid: Instance ID.
  19799. + * @rqid: Request ID. Used to match requests with responses and differentiate
  19800. + * between responses and events.
  19801. + * @cid: Command ID.
  19802. + */
  19803. +struct ssh_command {
  19804. + u8 type;
  19805. + u8 tc;
  19806. + u8 tid_out;
  19807. + u8 tid_in;
  19808. + u8 iid;
  19809. + __le16 rqid;
  19810. + u8 cid;
  19811. +} __packed;
  19812. +
  19813. +static_assert(sizeof(struct ssh_command) == 8);
  19814. +
  19815. +/*
  19816. + * SSH_COMMAND_MAX_PAYLOAD_SIZE - Maximum SSH command payload length in bytes.
  19817. + *
  19818. + * This is the physical maximum length of the protocol. Implementations may
  19819. + * set a more constrained limit.
  19820. + */
  19821. +#define SSH_COMMAND_MAX_PAYLOAD_SIZE \
  19822. + (SSH_FRAME_MAX_PAYLOAD_SIZE - sizeof(struct ssh_command))
  19823. +
  19824. +/*
  19825. + * SSH_MSG_LEN_BASE - Base-length of a SSH message.
  19826. + *
  19827. + * This is the minimum number of bytes required to form a message. The actual
  19828. + * message length is SSH_MSG_LEN_BASE plus the length of the frame payload.
  19829. + */
  19830. +#define SSH_MSG_LEN_BASE (sizeof(struct ssh_frame) + 3ull * sizeof(u16))
  19831. +
  19832. +/*
  19833. + * SSH_MSG_LEN_CTRL - Length of a SSH control message.
  19834. + *
  19835. + * This is the length of a SSH control message, which is equal to a SSH
  19836. + * message without any payload.
  19837. + */
  19838. +#define SSH_MSG_LEN_CTRL SSH_MSG_LEN_BASE
  19839. +
  19840. +/**
  19841. + * SSH_MESSAGE_LENGTH() - Compute length of SSH message.
  19842. + * @payload_size: Length of the payload inside the SSH frame.
  19843. + *
  19844. + * Return: Returns the length of a SSH message with payload of specified size.
  19845. + */
  19846. +#define SSH_MESSAGE_LENGTH(payload_size) (SSH_MSG_LEN_BASE + (payload_size))
  19847. +
  19848. +/**
  19849. + * SSH_COMMAND_MESSAGE_LENGTH() - Compute length of SSH command message.
  19850. + * @payload_size: Length of the command payload.
  19851. + *
  19852. + * Return: Returns the length of a SSH command message with command payload of
  19853. + * specified size.
  19854. + */
  19855. +#define SSH_COMMAND_MESSAGE_LENGTH(payload_size) \
  19856. + SSH_MESSAGE_LENGTH(sizeof(struct ssh_command) + (payload_size))
  19857. +
  19858. +/**
  19859. + * SSH_MSGOFFSET_FRAME() - Compute offset in SSH message to specified field in
  19860. + * frame.
  19861. + * @field: The field for which the offset should be computed.
  19862. + *
  19863. + * Return: Returns the offset of the specified &struct ssh_frame field in the
  19864. + * raw SSH message data as. Takes SYN bytes (u16) preceding the frame into
  19865. + * account.
  19866. + */
  19867. +#define SSH_MSGOFFSET_FRAME(field) \
  19868. + (sizeof(u16) + offsetof(struct ssh_frame, field))
  19869. +
  19870. +/**
  19871. + * SSH_MSGOFFSET_COMMAND() - Compute offset in SSH message to specified field
  19872. + * in command.
  19873. + * @field: The field for which the offset should be computed.
  19874. + *
  19875. + * Return: Returns the offset of the specified &struct ssh_command field in
  19876. + * the raw SSH message data. Takes SYN bytes (u16) preceding the frame and the
  19877. + * frame CRC (u16) between frame and command into account.
  19878. + */
  19879. +#define SSH_MSGOFFSET_COMMAND(field) \
  19880. + (2ull * sizeof(u16) + sizeof(struct ssh_frame) \
  19881. + + offsetof(struct ssh_command, field))
  19882. +
  19883. +/*
  19884. + * SSH_MSG_SYN - SSH message synchronization (SYN) bytes as u16.
  19885. + */
  19886. +#define SSH_MSG_SYN ((u16)0x55aa)
  19887. +
  19888. +/**
  19889. + * ssh_crc() - Compute CRC for SSH messages.
  19890. + * @buf: The pointer pointing to the data for which the CRC should be computed.
  19891. + * @len: The length of the data for which the CRC should be computed.
  19892. + *
  19893. + * Return: Returns the CRC computed on the provided data, as used for SSH
  19894. + * messages.
  19895. + */
  19896. +static inline u16 ssh_crc(const u8 *buf, size_t len)
  19897. +{
  19898. + return crc_ccitt_false(0xffff, buf, len);
  19899. +}
  19900. +
  19901. +/*
  19902. + * SSH_NUM_EVENTS - The number of reserved event IDs.
  19903. + *
  19904. + * The number of reserved event IDs, used for registering an SSH event
  19905. + * handler. Valid event IDs are numbers below or equal to this value, with
  19906. + * exception of zero, which is not an event ID. Thus, this is also the
  19907. + * absolute maximum number of event handlers that can be registered.
  19908. + */
  19909. +#define SSH_NUM_EVENTS 34
  19910. +
  19911. +/*
  19912. + * SSH_NUM_TARGETS - The number of communication targets used in the protocol.
  19913. + */
  19914. +#define SSH_NUM_TARGETS 2
  19915. +
  19916. +/**
  19917. + * ssh_rqid_next_valid() - Return the next valid request ID.
  19918. + * @rqid: The current request ID.
  19919. + *
  19920. + * Return: Returns the next valid request ID, following the current request ID
  19921. + * provided to this function. This function skips any request IDs reserved for
  19922. + * events.
  19923. + */
  19924. +static inline u16 ssh_rqid_next_valid(u16 rqid)
  19925. +{
  19926. + return rqid > 0 ? rqid + 1u : rqid + SSH_NUM_EVENTS + 1u;
  19927. +}
  19928. +
  19929. +/**
  19930. + * ssh_rqid_to_event() - Convert request ID to its corresponding event ID.
  19931. + * @rqid: The request ID to convert.
  19932. + */
  19933. +static inline u16 ssh_rqid_to_event(u16 rqid)
  19934. +{
  19935. + return rqid - 1u;
  19936. +}
  19937. +
  19938. +/**
  19939. + * ssh_rqid_is_event() - Check if given request ID is a valid event ID.
  19940. + * @rqid: The request ID to check.
  19941. + */
  19942. +static inline bool ssh_rqid_is_event(u16 rqid)
  19943. +{
  19944. + return ssh_rqid_to_event(rqid) < SSH_NUM_EVENTS;
  19945. +}
  19946. +
  19947. +/**
  19948. + * ssh_tc_to_rqid() - Convert target category to its corresponding request ID.
  19949. + * @tc: The target category to convert.
  19950. + */
  19951. +static inline u16 ssh_tc_to_rqid(u8 tc)
  19952. +{
  19953. + return tc;
  19954. +}
  19955. +
  19956. +/**
  19957. + * ssh_tid_to_index() - Convert target ID to its corresponding target index.
  19958. + * @tid: The target ID to convert.
  19959. + */
  19960. +static inline u8 ssh_tid_to_index(u8 tid)
  19961. +{
  19962. + return tid - 1u;
  19963. +}
  19964. +
  19965. +/**
  19966. + * ssh_tid_is_valid() - Check if target ID is valid/supported.
  19967. + * @tid: The target ID to check.
  19968. + */
  19969. +static inline bool ssh_tid_is_valid(u8 tid)
  19970. +{
  19971. + return ssh_tid_to_index(tid) < SSH_NUM_TARGETS;
  19972. +}
  19973. +
  19974. +/**
  19975. + * struct ssam_span - Reference to a buffer region.
  19976. + * @ptr: Pointer to the buffer region.
  19977. + * @len: Length of the buffer region.
  19978. + *
  19979. + * A reference to a (non-owned) buffer segment, consisting of pointer and
  19980. + * length. Use of this struct indicates non-owned data, i.e. data of which the
  19981. + * life-time is managed (i.e. it is allocated/freed) via another pointer.
  19982. + */
  19983. +struct ssam_span {
  19984. + u8 *ptr;
  19985. + size_t len;
  19986. +};
  19987. +
  19988. +/*
  19989. + * Known SSH/EC target categories.
  19990. + *
  19991. + * List of currently known target category values; "Known" as in we know they
  19992. + * exist and are valid on at least some device/model. Detailed functionality
  19993. + * or the full category name is only known for some of these categories and
  19994. + * is detailed in the respective comment below.
  19995. + *
  19996. + * These values and abbreviations have been extracted from strings inside the
  19997. + * Windows driver.
  19998. + */
  19999. +enum ssam_ssh_tc {
  20000. + /* Category 0x00 is invalid for EC use. */
  20001. + SSAM_SSH_TC_SAM = 0x01, /* Generic system functionality, real-time clock. */
  20002. + SSAM_SSH_TC_BAT = 0x02, /* Battery/power subsystem. */
  20003. + SSAM_SSH_TC_TMP = 0x03, /* Thermal subsystem. */
  20004. + SSAM_SSH_TC_PMC = 0x04,
  20005. + SSAM_SSH_TC_FAN = 0x05,
  20006. + SSAM_SSH_TC_PoM = 0x06,
  20007. + SSAM_SSH_TC_DBG = 0x07,
  20008. + SSAM_SSH_TC_KBD = 0x08, /* Legacy keyboard (Laptop 1/2). */
  20009. + SSAM_SSH_TC_FWU = 0x09,
  20010. + SSAM_SSH_TC_UNI = 0x0a,
  20011. + SSAM_SSH_TC_LPC = 0x0b,
  20012. + SSAM_SSH_TC_TCL = 0x0c,
  20013. + SSAM_SSH_TC_SFL = 0x0d,
  20014. + SSAM_SSH_TC_KIP = 0x0e,
  20015. + SSAM_SSH_TC_EXT = 0x0f,
  20016. + SSAM_SSH_TC_BLD = 0x10,
  20017. + SSAM_SSH_TC_BAS = 0x11, /* Detachment system (Surface Book 2/3). */
  20018. + SSAM_SSH_TC_SEN = 0x12,
  20019. + SSAM_SSH_TC_SRQ = 0x13,
  20020. + SSAM_SSH_TC_MCU = 0x14,
  20021. + SSAM_SSH_TC_HID = 0x15, /* Generic HID input subsystem. */
  20022. + SSAM_SSH_TC_TCH = 0x16,
  20023. + SSAM_SSH_TC_BKL = 0x17,
  20024. + SSAM_SSH_TC_TAM = 0x18,
  20025. + SSAM_SSH_TC_ACC = 0x19,
  20026. + SSAM_SSH_TC_UFI = 0x1a,
  20027. + SSAM_SSH_TC_USC = 0x1b,
  20028. + SSAM_SSH_TC_PEN = 0x1c,
  20029. + SSAM_SSH_TC_VID = 0x1d,
  20030. + SSAM_SSH_TC_AUD = 0x1e,
  20031. + SSAM_SSH_TC_SMC = 0x1f,
  20032. + SSAM_SSH_TC_KPD = 0x20,
  20033. + SSAM_SSH_TC_REG = 0x21, /* Extended event registry. */
  20034. +};
  20035. +
  20036. +
  20037. +/* -- Packet transport layer (ptl). ----------------------------------------- */
  20038. +
  20039. +/**
  20040. + * enum ssh_packet_base_priority - Base priorities for &struct ssh_packet.
  20041. + * @SSH_PACKET_PRIORITY_FLUSH: Base priority for flush packets.
  20042. + * @SSH_PACKET_PRIORITY_DATA: Base priority for normal data packets.
  20043. + * @SSH_PACKET_PRIORITY_NAK: Base priority for NAK packets.
  20044. + * @SSH_PACKET_PRIORITY_ACK: Base priority for ACK packets.
  20045. + */
  20046. +enum ssh_packet_base_priority {
  20047. + SSH_PACKET_PRIORITY_FLUSH = 0, /* same as DATA to sequence flush */
  20048. + SSH_PACKET_PRIORITY_DATA = 0,
  20049. + SSH_PACKET_PRIORITY_NAK = 1,
  20050. + SSH_PACKET_PRIORITY_ACK = 2,
  20051. +};
  20052. +
  20053. +/*
  20054. + * Same as SSH_PACKET_PRIORITY() below, only with actual values.
  20055. + */
  20056. +#define __SSH_PACKET_PRIORITY(base, try) \
  20057. + (((base) << 4) | ((try) & 0x0f))
  20058. +
  20059. +/**
  20060. + * SSH_PACKET_PRIORITY() - Compute packet priority from base priority and
  20061. + * number of tries.
  20062. + * @base: The base priority as suffix of &enum ssh_packet_base_priority, e.g.
  20063. + * ``FLUSH``, ``DATA``, ``ACK``, or ``NAK``.
  20064. + * @try: The number of tries (must be less than 16).
  20065. + *
  20066. + * Compute the combined packet priority. The combined priority is dominated by
  20067. + * the base priority, whereas the number of (re-)tries decides the precedence
  20068. + * of packets with the same base priority, giving higher priority to packets
  20069. + * that already have more tries.
  20070. + *
  20071. + * Return: Returns the computed priority as value fitting inside a &u8. A
  20072. + * higher number means a higher priority.
  20073. + */
  20074. +#define SSH_PACKET_PRIORITY(base, try) \
  20075. + __SSH_PACKET_PRIORITY(SSH_PACKET_PRIORITY_##base, (try))
  20076. +
  20077. +/**
  20078. + * ssh_packet_priority_get_try() - Get number of tries from packet priority.
  20079. + * @priority: The packet priority.
  20080. + *
  20081. + * Return: Returns the number of tries encoded in the specified packet
  20082. + * priority.
  20083. + */
  20084. +static inline u8 ssh_packet_priority_get_try(u8 priority)
  20085. +{
  20086. + return priority & 0x0f;
  20087. +}
  20088. +
  20089. +/**
  20090. + * ssh_packet_priority_get_base - Get base priority from packet priority.
  20091. + * @priority: The packet priority.
  20092. + *
  20093. + * Return: Returns the base priority encoded in the given packet priority.
  20094. + */
  20095. +static inline u8 ssh_packet_priority_get_base(u8 priority)
  20096. +{
  20097. + return (priority & 0xf0) >> 4;
  20098. +}
  20099. +
  20100. +enum ssh_packet_flags {
  20101. + /* state flags */
  20102. + SSH_PACKET_SF_LOCKED_BIT,
  20103. + SSH_PACKET_SF_QUEUED_BIT,
  20104. + SSH_PACKET_SF_PENDING_BIT,
  20105. + SSH_PACKET_SF_TRANSMITTING_BIT,
  20106. + SSH_PACKET_SF_TRANSMITTED_BIT,
  20107. + SSH_PACKET_SF_ACKED_BIT,
  20108. + SSH_PACKET_SF_CANCELED_BIT,
  20109. + SSH_PACKET_SF_COMPLETED_BIT,
  20110. +
  20111. + /* type flags */
  20112. + SSH_PACKET_TY_FLUSH_BIT,
  20113. + SSH_PACKET_TY_SEQUENCED_BIT,
  20114. + SSH_PACKET_TY_BLOCKING_BIT,
  20115. +
  20116. + /* mask for state flags */
  20117. + SSH_PACKET_FLAGS_SF_MASK =
  20118. + BIT(SSH_PACKET_SF_LOCKED_BIT)
  20119. + | BIT(SSH_PACKET_SF_QUEUED_BIT)
  20120. + | BIT(SSH_PACKET_SF_PENDING_BIT)
  20121. + | BIT(SSH_PACKET_SF_TRANSMITTING_BIT)
  20122. + | BIT(SSH_PACKET_SF_TRANSMITTED_BIT)
  20123. + | BIT(SSH_PACKET_SF_ACKED_BIT)
  20124. + | BIT(SSH_PACKET_SF_CANCELED_BIT)
  20125. + | BIT(SSH_PACKET_SF_COMPLETED_BIT),
  20126. +
  20127. + /* mask for type flags */
  20128. + SSH_PACKET_FLAGS_TY_MASK =
  20129. + BIT(SSH_PACKET_TY_FLUSH_BIT)
  20130. + | BIT(SSH_PACKET_TY_SEQUENCED_BIT)
  20131. + | BIT(SSH_PACKET_TY_BLOCKING_BIT),
  20132. +};
  20133. +
  20134. +struct ssh_ptl;
  20135. +struct ssh_packet;
  20136. +
  20137. +/**
  20138. + * struct ssh_packet_ops - Callback operations for a SSH packet.
  20139. + * @release: Function called when the packet reference count reaches zero.
  20140. + * This callback must be relied upon to ensure that the packet has
  20141. + * left the transport system(s).
  20142. + * @complete: Function called when the packet is completed, either with
  20143. + * success or failure. In case of failure, the reason for the
  20144. + * failure is indicated by the value of the provided status code
  20145. + * argument. This value will be zero in case of success. Note that
  20146. + * a call to this callback does not guarantee that the packet is
  20147. + * not in use by the transport system any more.
  20148. + */
  20149. +struct ssh_packet_ops {
  20150. + void (*release)(struct ssh_packet *p);
  20151. + void (*complete)(struct ssh_packet *p, int status);
  20152. +};
  20153. +
  20154. +/**
  20155. + * struct ssh_packet - SSH transport packet.
  20156. + * @ptl: Pointer to the packet transport layer. May be %NULL if the packet
  20157. + * (or enclosing request) has not been submitted yet.
  20158. + * @refcnt: Reference count of the packet.
  20159. + * @priority: Priority of the packet. Must be computed via
  20160. + * SSH_PACKET_PRIORITY(). Must only be accessed while holding the
  20161. + * queue lock after first submission.
  20162. + * @data: Raw message data.
  20163. + * @data.len: Length of the raw message data.
  20164. + * @data.ptr: Pointer to the raw message data buffer.
  20165. + * @state: State and type flags describing current packet state (dynamic)
  20166. + * and type (static). See &enum ssh_packet_flags for possible
  20167. + * options.
  20168. + * @timestamp: Timestamp specifying when the latest transmission of a
  20169. + * currently pending packet has been started. May be %KTIME_MAX
  20170. + * before or in-between transmission attempts. Used for the packet
  20171. + * timeout implementation. Must only be accessed while holding the
  20172. + * pending lock after first submission.
  20173. + * @queue_node: The list node for the packet queue.
  20174. + * @pending_node: The list node for the set of pending packets.
  20175. + * @ops: Packet operations.
  20176. + */
  20177. +struct ssh_packet {
  20178. + struct ssh_ptl *ptl;
  20179. + struct kref refcnt;
  20180. +
  20181. + u8 priority;
  20182. +
  20183. + struct {
  20184. + size_t len;
  20185. + u8 *ptr;
  20186. + } data;
  20187. +
  20188. + unsigned long state;
  20189. + ktime_t timestamp;
  20190. +
  20191. + struct list_head queue_node;
  20192. + struct list_head pending_node;
  20193. +
  20194. + const struct ssh_packet_ops *ops;
  20195. +};
  20196. +
  20197. +struct ssh_packet *ssh_packet_get(struct ssh_packet *p);
  20198. +void ssh_packet_put(struct ssh_packet *p);
  20199. +
  20200. +/**
  20201. + * ssh_packet_set_data() - Set raw message data of packet.
  20202. + * @p: The packet for which the message data should be set.
  20203. + * @ptr: Pointer to the memory holding the message data.
  20204. + * @len: Length of the message data.
  20205. + *
  20206. + * Sets the raw message data buffer of the packet to the provided memory. The
  20207. + * memory is not copied. Instead, the caller is responsible for management
  20208. + * (i.e. allocation and deallocation) of the memory. The caller must ensure
  20209. + * that the provided memory is valid and contains a valid SSH message,
  20210. + * starting from the time of submission of the packet until the ``release``
  20211. + * callback has been called. During this time, the memory may not be altered
  20212. + * in any way.
  20213. + */
  20214. +static inline void ssh_packet_set_data(struct ssh_packet *p, u8 *ptr, size_t len)
  20215. +{
  20216. + p->data.ptr = ptr;
  20217. + p->data.len = len;
  20218. +}
  20219. +
  20220. +
  20221. +/* -- Request transport layer (rtl). ---------------------------------------- */
  20222. +
  20223. +enum ssh_request_flags {
  20224. + /* state flags */
  20225. + SSH_REQUEST_SF_LOCKED_BIT,
  20226. + SSH_REQUEST_SF_QUEUED_BIT,
  20227. + SSH_REQUEST_SF_PENDING_BIT,
  20228. + SSH_REQUEST_SF_TRANSMITTING_BIT,
  20229. + SSH_REQUEST_SF_TRANSMITTED_BIT,
  20230. + SSH_REQUEST_SF_RSPRCVD_BIT,
  20231. + SSH_REQUEST_SF_CANCELED_BIT,
  20232. + SSH_REQUEST_SF_COMPLETED_BIT,
  20233. +
  20234. + /* type flags */
  20235. + SSH_REQUEST_TY_FLUSH_BIT,
  20236. + SSH_REQUEST_TY_HAS_RESPONSE_BIT,
  20237. +
  20238. + /* mask for state flags */
  20239. + SSH_REQUEST_FLAGS_SF_MASK =
  20240. + BIT(SSH_REQUEST_SF_LOCKED_BIT)
  20241. + | BIT(SSH_REQUEST_SF_QUEUED_BIT)
  20242. + | BIT(SSH_REQUEST_SF_PENDING_BIT)
  20243. + | BIT(SSH_REQUEST_SF_TRANSMITTING_BIT)
  20244. + | BIT(SSH_REQUEST_SF_TRANSMITTED_BIT)
  20245. + | BIT(SSH_REQUEST_SF_RSPRCVD_BIT)
  20246. + | BIT(SSH_REQUEST_SF_CANCELED_BIT)
  20247. + | BIT(SSH_REQUEST_SF_COMPLETED_BIT),
  20248. +
  20249. + /* mask for type flags */
  20250. + SSH_REQUEST_FLAGS_TY_MASK =
  20251. + BIT(SSH_REQUEST_TY_FLUSH_BIT)
  20252. + | BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT),
  20253. +};
  20254. +
  20255. +struct ssh_rtl;
  20256. +struct ssh_request;
  20257. +
  20258. +/**
  20259. + * struct ssh_request_ops - Callback operations for a SSH request.
  20260. + * @release: Function called when the request's reference count reaches zero.
  20261. + * This callback must be relied upon to ensure that the request has
  20262. + * left the transport systems (both, packet an request systems).
  20263. + * @complete: Function called when the request is completed, either with
  20264. + * success or failure. The command data for the request response
  20265. + * is provided via the &struct ssh_command parameter (``cmd``),
  20266. + * the command payload of the request response via the &struct
  20267. + * ssh_span parameter (``data``).
  20268. + *
  20269. + * If the request does not have any response or has not been
  20270. + * completed with success, both ``cmd`` and ``data`` parameters will
  20271. + * be NULL. If the request response does not have any command
  20272. + * payload, the ``data`` span will be an empty (zero-length) span.
  20273. + *
  20274. + * In case of failure, the reason for the failure is indicated by
  20275. + * the value of the provided status code argument (``status``). This
  20276. + * value will be zero in case of success and a regular errno
  20277. + * otherwise.
  20278. + *
  20279. + * Note that a call to this callback does not guarantee that the
  20280. + * request is not in use by the transport systems any more.
  20281. + */
  20282. +struct ssh_request_ops {
  20283. + void (*release)(struct ssh_request *rqst);
  20284. + void (*complete)(struct ssh_request *rqst,
  20285. + const struct ssh_command *cmd,
  20286. + const struct ssam_span *data, int status);
  20287. +};
  20288. +
  20289. +/**
  20290. + * struct ssh_request - SSH transport request.
  20291. + * @packet: The underlying SSH transport packet.
  20292. + * @node: List node for the request queue and pending set.
  20293. + * @state: State and type flags describing current request state (dynamic)
  20294. + * and type (static). See &enum ssh_request_flags for possible
  20295. + * options.
  20296. + * @timestamp: Timestamp specifying when we start waiting on the response of
  20297. + * the request. This is set once the underlying packet has been
  20298. + * completed and may be %KTIME_MAX before that, or when the request
  20299. + * does not expect a response. Used for the request timeout
  20300. + * implementation.
  20301. + * @ops: Request Operations.
  20302. + */
  20303. +struct ssh_request {
  20304. + struct ssh_packet packet;
  20305. + struct list_head node;
  20306. +
  20307. + unsigned long state;
  20308. + ktime_t timestamp;
  20309. +
  20310. + const struct ssh_request_ops *ops;
  20311. +};
  20312. +
  20313. +/**
  20314. + * to_ssh_request() - Cast a SSH packet to its enclosing SSH request.
  20315. + * @p: The packet to cast.
  20316. + *
  20317. + * Casts the given &struct ssh_packet to its enclosing &struct ssh_request.
  20318. + * The caller is responsible for making sure that the packet is actually
  20319. + * wrapped in a &struct ssh_request.
  20320. + *
  20321. + * Return: Returns the &struct ssh_request wrapping the provided packet.
  20322. + */
  20323. +static inline struct ssh_request *to_ssh_request(struct ssh_packet *p)
  20324. +{
  20325. + return container_of(p, struct ssh_request, packet);
  20326. +}
  20327. +
  20328. +/**
  20329. + * ssh_request_get() - Increment reference count of request.
  20330. + * @r: The request to increment the reference count of.
  20331. + *
  20332. + * Increments the reference count of the given request by incrementing the
  20333. + * reference count of the underlying &struct ssh_packet, enclosed in it.
  20334. + *
  20335. + * See also ssh_request_put(), ssh_packet_get().
  20336. + *
  20337. + * Return: Returns the request provided as input.
  20338. + */
  20339. +static inline struct ssh_request *ssh_request_get(struct ssh_request *r)
  20340. +{
  20341. + return r ? to_ssh_request(ssh_packet_get(&r->packet)) : NULL;
  20342. +}
  20343. +
  20344. +/**
  20345. + * ssh_request_put() - Decrement reference count of request.
  20346. + * @r: The request to decrement the reference count of.
  20347. + *
  20348. + * Decrements the reference count of the given request by decrementing the
  20349. + * reference count of the underlying &struct ssh_packet, enclosed in it. If
  20350. + * the reference count reaches zero, the ``release`` callback specified in the
  20351. + * request's &struct ssh_request_ops, i.e. ``r->ops->release``, will be
  20352. + * called.
  20353. + *
  20354. + * See also ssh_request_get(), ssh_packet_put().
  20355. + */
  20356. +static inline void ssh_request_put(struct ssh_request *r)
  20357. +{
  20358. + if (r)
  20359. + ssh_packet_put(&r->packet);
  20360. +}
  20361. +
  20362. +/**
  20363. + * ssh_request_set_data() - Set raw message data of request.
  20364. + * @r: The request for which the message data should be set.
  20365. + * @ptr: Pointer to the memory holding the message data.
  20366. + * @len: Length of the message data.
  20367. + *
  20368. + * Sets the raw message data buffer of the underlying packet to the specified
  20369. + * buffer. Does not copy the actual message data, just sets the buffer pointer
  20370. + * and length. Refer to ssh_packet_set_data() for more details.
  20371. + */
  20372. +static inline void ssh_request_set_data(struct ssh_request *r, u8 *ptr, size_t len)
  20373. +{
  20374. + ssh_packet_set_data(&r->packet, ptr, len);
  20375. +}
  20376. +
  20377. +#endif /* _LINUX_SURFACE_AGGREGATOR_SERIAL_HUB_H */
  20378. diff --git a/include/uapi/linux/surface_aggregator/cdev.h b/include/uapi/linux/surface_aggregator/cdev.h
  20379. new file mode 100644
  20380. index 000000000000..08f46b60b151
  20381. --- /dev/null
  20382. +++ b/include/uapi/linux/surface_aggregator/cdev.h
  20383. @@ -0,0 +1,147 @@
  20384. +/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
  20385. +/*
  20386. + * Surface System Aggregator Module (SSAM) user-space EC interface.
  20387. + *
  20388. + * Definitions, structs, and IOCTLs for the /dev/surface/aggregator misc
  20389. + * device. This device provides direct user-space access to the SSAM EC.
  20390. + * Intended for debugging and development.
  20391. + *
  20392. + * Copyright (C) 2020-2021 Maximilian Luz <luzmaximilian@gmail.com>
  20393. + */
  20394. +
  20395. +#ifndef _UAPI_LINUX_SURFACE_AGGREGATOR_CDEV_H
  20396. +#define _UAPI_LINUX_SURFACE_AGGREGATOR_CDEV_H
  20397. +
  20398. +#include <linux/ioctl.h>
  20399. +#include <linux/types.h>
  20400. +
  20401. +/**
  20402. + * enum ssam_cdev_request_flags - Request flags for SSAM cdev request IOCTL.
  20403. + *
  20404. + * @SSAM_CDEV_REQUEST_HAS_RESPONSE:
  20405. + * Specifies that the request expects a response. If not set, the request
  20406. + * will be directly completed after its underlying packet has been
  20407. + * transmitted. If set, the request transport system waits for a response
  20408. + * of the request.
  20409. + *
  20410. + * @SSAM_CDEV_REQUEST_UNSEQUENCED:
  20411. + * Specifies that the request should be transmitted via an unsequenced
  20412. + * packet. If set, the request must not have a response, meaning that this
  20413. + * flag and the %SSAM_CDEV_REQUEST_HAS_RESPONSE flag are mutually
  20414. + * exclusive.
  20415. + */
  20416. +enum ssam_cdev_request_flags {
  20417. + SSAM_CDEV_REQUEST_HAS_RESPONSE = 0x01,
  20418. + SSAM_CDEV_REQUEST_UNSEQUENCED = 0x02,
  20419. +};
  20420. +
  20421. +/**
  20422. + * struct ssam_cdev_request - Controller request IOCTL argument.
  20423. + * @target_category: Target category of the SAM request.
  20424. + * @target_id: Target ID of the SAM request.
  20425. + * @command_id: Command ID of the SAM request.
  20426. + * @instance_id: Instance ID of the SAM request.
  20427. + * @flags: Request flags (see &enum ssam_cdev_request_flags).
  20428. + * @status: Request status (output).
  20429. + * @payload: Request payload (input data).
  20430. + * @payload.data: Pointer to request payload data.
  20431. + * @payload.length: Length of request payload data (in bytes).
  20432. + * @response: Request response (output data).
  20433. + * @response.data: Pointer to response buffer.
  20434. + * @response.length: On input: Capacity of response buffer (in bytes).
  20435. + * On output: Length of request response (number of bytes
  20436. + * in the buffer that are actually used).
  20437. + */
  20438. +struct ssam_cdev_request {
  20439. + __u8 target_category;
  20440. + __u8 target_id;
  20441. + __u8 command_id;
  20442. + __u8 instance_id;
  20443. + __u16 flags;
  20444. + __s16 status;
  20445. +
  20446. + struct {
  20447. + __u64 data;
  20448. + __u16 length;
  20449. + __u8 __pad[6];
  20450. + } payload;
  20451. +
  20452. + struct {
  20453. + __u64 data;
  20454. + __u16 length;
  20455. + __u8 __pad[6];
  20456. + } response;
  20457. +} __attribute__((__packed__));
  20458. +
  20459. +/**
  20460. + * struct ssam_cdev_notifier_desc - Notifier descriptor.
  20461. + * @priority: Priority value determining the order in which notifier
  20462. + * callbacks will be called. A higher value means higher
  20463. + * priority, i.e. the associated callback will be executed
  20464. + * earlier than other (lower priority) callbacks.
  20465. + * @target_category: The event target category for which this notifier should
  20466. + * receive events.
  20467. + *
  20468. + * Specifies the notifier that should be registered or unregistered,
  20469. + * specifically with which priority and for which target category of events.
  20470. + */
  20471. +struct ssam_cdev_notifier_desc {
  20472. + __s32 priority;
  20473. + __u8 target_category;
  20474. +} __attribute__((__packed__));
  20475. +
  20476. +/**
  20477. + * struct ssam_cdev_event_desc - Event descriptor.
  20478. + * @reg: Registry via which the event will be enabled/disabled.
  20479. + * @reg.target_category: Target category for the event registry requests.
  20480. + * @reg.target_id: Target ID for the event registry requests.
  20481. + * @reg.cid_enable: Command ID for the event-enable request.
  20482. + * @reg.cid_disable: Command ID for the event-disable request.
  20483. + * @id: ID specifying the event.
  20484. + * @id.target_category: Target category of the event source.
  20485. + * @id.instance: Instance ID of the event source.
  20486. + * @flags: Flags used for enabling the event.
  20487. + *
  20488. + * Specifies which event should be enabled/disabled and how to do that.
  20489. + */
  20490. +struct ssam_cdev_event_desc {
  20491. + struct {
  20492. + __u8 target_category;
  20493. + __u8 target_id;
  20494. + __u8 cid_enable;
  20495. + __u8 cid_disable;
  20496. + } reg;
  20497. +
  20498. + struct {
  20499. + __u8 target_category;
  20500. + __u8 instance;
  20501. + } id;
  20502. +
  20503. + __u8 flags;
  20504. +} __attribute__((__packed__));
  20505. +
  20506. +/**
  20507. + * struct ssam_cdev_event - SSAM event sent by the EC.
  20508. + * @target_category: Target category of the event source. See &enum ssam_ssh_tc.
  20509. + * @target_id: Target ID of the event source.
  20510. + * @command_id: Command ID of the event.
  20511. + * @instance_id: Instance ID of the event source.
  20512. + * @length: Length of the event payload in bytes.
  20513. + * @data: Event payload data.
  20514. + */
  20515. +struct ssam_cdev_event {
  20516. + __u8 target_category;
  20517. + __u8 target_id;
  20518. + __u8 command_id;
  20519. + __u8 instance_id;
  20520. + __u16 length;
  20521. + __u8 data[];
  20522. +} __attribute__((__packed__));
  20523. +
  20524. +#define SSAM_CDEV_REQUEST _IOWR(0xA5, 1, struct ssam_cdev_request)
  20525. +#define SSAM_CDEV_NOTIF_REGISTER _IOW(0xA5, 2, struct ssam_cdev_notifier_desc)
  20526. +#define SSAM_CDEV_NOTIF_UNREGISTER _IOW(0xA5, 3, struct ssam_cdev_notifier_desc)
  20527. +#define SSAM_CDEV_EVENT_ENABLE _IOW(0xA5, 4, struct ssam_cdev_event_desc)
  20528. +#define SSAM_CDEV_EVENT_DISABLE _IOW(0xA5, 5, struct ssam_cdev_event_desc)
  20529. +
  20530. +#endif /* _UAPI_LINUX_SURFACE_AGGREGATOR_CDEV_H */
  20531. diff --git a/include/uapi/linux/surface_aggregator/dtx.h b/include/uapi/linux/surface_aggregator/dtx.h
  20532. new file mode 100644
  20533. index 000000000000..fc0ba6cbe3e8
  20534. --- /dev/null
  20535. +++ b/include/uapi/linux/surface_aggregator/dtx.h
  20536. @@ -0,0 +1,146 @@
  20537. +/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
  20538. +/*
  20539. + * Surface DTX (clipboard detachment system driver) user-space interface.
  20540. + *
  20541. + * Definitions, structs, and IOCTLs for the /dev/surface/dtx misc device. This
  20542. + * device allows user-space to control the clipboard detachment process on
  20543. + * Surface Book series devices.
  20544. + *
  20545. + * Copyright (C) 2020 Maximilian Luz <luzmaximilian@gmail.com>
  20546. + */
  20547. +
  20548. +#ifndef _UAPI_LINUX_SURFACE_AGGREGATOR_DTX_H
  20549. +#define _UAPI_LINUX_SURFACE_AGGREGATOR_DTX_H
  20550. +
  20551. +#include <linux/ioctl.h>
  20552. +#include <linux/types.h>
  20553. +
  20554. +/* Status/error categories */
  20555. +#define SDTX_CATEGORY_STATUS 0x0000
  20556. +#define SDTX_CATEGORY_RUNTIME_ERROR 0x1000
  20557. +#define SDTX_CATEGORY_HARDWARE_ERROR 0x2000
  20558. +#define SDTX_CATEGORY_UNKNOWN 0xf000
  20559. +
  20560. +#define SDTX_CATEGORY_MASK 0xf000
  20561. +#define SDTX_CATEGORY(value) ((value) & SDTX_CATEGORY_MASK)
  20562. +
  20563. +#define SDTX_STATUS(code) ((code) | SDTX_CATEGORY_STATUS)
  20564. +#define SDTX_ERR_RT(code) ((code) | SDTX_CATEGORY_RUNTIME_ERROR)
  20565. +#define SDTX_ERR_HW(code) ((code) | SDTX_CATEGORY_HARDWARE_ERROR)
  20566. +#define SDTX_UNKNOWN(code) ((code) | SDTX_CATEGORY_UNKNOWN)
  20567. +
  20568. +#define SDTX_SUCCESS(value) (SDTX_CATEGORY(value) == SDTX_CATEGORY_STATUS)
  20569. +
  20570. +/* Latch status values */
  20571. +#define SDTX_LATCH_CLOSED SDTX_STATUS(0x00)
  20572. +#define SDTX_LATCH_OPENED SDTX_STATUS(0x01)
  20573. +
  20574. +/* Base state values */
  20575. +#define SDTX_BASE_DETACHED SDTX_STATUS(0x00)
  20576. +#define SDTX_BASE_ATTACHED SDTX_STATUS(0x01)
  20577. +
  20578. +/* Runtime errors (non-critical) */
  20579. +#define SDTX_DETACH_NOT_FEASIBLE SDTX_ERR_RT(0x01)
  20580. +#define SDTX_DETACH_TIMEDOUT SDTX_ERR_RT(0x02)
  20581. +
  20582. +/* Hardware errors (critical) */
  20583. +#define SDTX_ERR_FAILED_TO_OPEN SDTX_ERR_HW(0x01)
  20584. +#define SDTX_ERR_FAILED_TO_REMAIN_OPEN SDTX_ERR_HW(0x02)
  20585. +#define SDTX_ERR_FAILED_TO_CLOSE SDTX_ERR_HW(0x03)
  20586. +
  20587. +/* Base types */
  20588. +#define SDTX_DEVICE_TYPE_HID 0x0100
  20589. +#define SDTX_DEVICE_TYPE_SSH 0x0200
  20590. +
  20591. +#define SDTX_DEVICE_TYPE_MASK 0x0f00
  20592. +#define SDTX_DEVICE_TYPE(value) ((value) & SDTX_DEVICE_TYPE_MASK)
  20593. +
  20594. +#define SDTX_BASE_TYPE_HID(id) ((id) | SDTX_DEVICE_TYPE_HID)
  20595. +#define SDTX_BASE_TYPE_SSH(id) ((id) | SDTX_DEVICE_TYPE_SSH)
  20596. +
  20597. +/**
  20598. + * enum sdtx_device_mode - Mode describing how (and if) the clipboard is
  20599. + * attached to the base of the device.
  20600. + * @SDTX_DEVICE_MODE_TABLET: The clipboard is detached from the base and the
  20601. + * device operates as tablet.
  20602. + * @SDTX_DEVICE_MODE_LAPTOP: The clipboard is attached normally to the base
  20603. + * and the device operates as laptop.
  20604. + * @SDTX_DEVICE_MODE_STUDIO: The clipboard is attached to the base in reverse.
  20605. + * The device operates as tablet with keyboard and
  20606. + * touchpad deactivated, however, the base battery
  20607. + * and, if present in the specific device model, dGPU
  20608. + * are available to the system.
  20609. + */
  20610. +enum sdtx_device_mode {
  20611. + SDTX_DEVICE_MODE_TABLET = 0x00,
  20612. + SDTX_DEVICE_MODE_LAPTOP = 0x01,
  20613. + SDTX_DEVICE_MODE_STUDIO = 0x02,
  20614. +};
  20615. +
  20616. +/**
  20617. + * struct sdtx_event - Event provided by reading from the DTX device file.
  20618. + * @length: Length of the event payload, in bytes.
  20619. + * @code: Event code, detailing what type of event this is.
  20620. + * @data: Payload of the event, containing @length bytes.
  20621. + *
  20622. + * See &enum sdtx_event_code for currently valid event codes.
  20623. + */
  20624. +struct sdtx_event {
  20625. + __u16 length;
  20626. + __u16 code;
  20627. + __u8 data[];
  20628. +} __attribute__((__packed__));
  20629. +
  20630. +/**
  20631. + * enum sdtx_event_code - Code describing the type of an event.
  20632. + * @SDTX_EVENT_REQUEST: Detachment request event type.
  20633. + * @SDTX_EVENT_CANCEL: Cancel detachment process event type.
  20634. + * @SDTX_EVENT_BASE_CONNECTION: Base/clipboard connection change event type.
  20635. + * @SDTX_EVENT_LATCH_STATUS: Latch status change event type.
  20636. + * @SDTX_EVENT_DEVICE_MODE: Device mode change event type.
  20637. + *
  20638. + * Used in @struct sdtx_event to describe the type of the event. Further event
  20639. + * codes are reserved for future use. Any event parser should be able to
  20640. + * gracefully handle unknown events, i.e. by simply skipping them.
  20641. + *
  20642. + * Consult the DTX user-space interface documentation for details regarding
  20643. + * the individual event types.
  20644. + */
  20645. +enum sdtx_event_code {
  20646. + SDTX_EVENT_REQUEST = 1,
  20647. + SDTX_EVENT_CANCEL = 2,
  20648. + SDTX_EVENT_BASE_CONNECTION = 3,
  20649. + SDTX_EVENT_LATCH_STATUS = 4,
  20650. + SDTX_EVENT_DEVICE_MODE = 5,
  20651. +};
  20652. +
  20653. +/**
  20654. + * struct sdtx_base_info - Describes if and what type of base is connected.
  20655. + * @state: The state of the connection. Valid values are %SDTX_BASE_DETACHED,
  20656. + * %SDTX_BASE_ATTACHED, and %SDTX_DETACH_NOT_FEASIBLE (in case a base
  20657. + * is attached but low clipboard battery prevents detachment). Other
  20658. + * values are currently reserved.
  20659. + * @base_id: The type of base connected. Zero if no base is connected.
  20660. + */
  20661. +struct sdtx_base_info {
  20662. + __u16 state;
  20663. + __u16 base_id;
  20664. +} __attribute__((__packed__));
  20665. +
  20666. +/* IOCTLs */
  20667. +#define SDTX_IOCTL_EVENTS_ENABLE _IO(0xa5, 0x21)
  20668. +#define SDTX_IOCTL_EVENTS_DISABLE _IO(0xa5, 0x22)
  20669. +
  20670. +#define SDTX_IOCTL_LATCH_LOCK _IO(0xa5, 0x23)
  20671. +#define SDTX_IOCTL_LATCH_UNLOCK _IO(0xa5, 0x24)
  20672. +
  20673. +#define SDTX_IOCTL_LATCH_REQUEST _IO(0xa5, 0x25)
  20674. +#define SDTX_IOCTL_LATCH_CONFIRM _IO(0xa5, 0x26)
  20675. +#define SDTX_IOCTL_LATCH_HEARTBEAT _IO(0xa5, 0x27)
  20676. +#define SDTX_IOCTL_LATCH_CANCEL _IO(0xa5, 0x28)
  20677. +
  20678. +#define SDTX_IOCTL_GET_BASE_INFO _IOR(0xa5, 0x29, struct sdtx_base_info)
  20679. +#define SDTX_IOCTL_GET_DEVICE_MODE _IOR(0xa5, 0x2a, __u16)
  20680. +#define SDTX_IOCTL_GET_LATCH_STATUS _IOR(0xa5, 0x2b, __u16)
  20681. +
  20682. +#endif /* _UAPI_LINUX_SURFACE_AGGREGATOR_DTX_H */
  20683. diff --git a/scripts/mod/devicetable-offsets.c b/scripts/mod/devicetable-offsets.c
  20684. index bcff122d0dc8..4339377ad929 100644
  20685. --- a/scripts/mod/devicetable-offsets.c
  20686. +++ b/scripts/mod/devicetable-offsets.c
  20687. @@ -245,8 +245,9 @@ int main(void)
  20688. DEVID(ssam_device_id);
  20689. DEVID_FIELD(ssam_device_id, match_flags);
  20690. + DEVID_FIELD(ssam_device_id, domain);
  20691. DEVID_FIELD(ssam_device_id, category);
  20692. - DEVID_FIELD(ssam_device_id, channel);
  20693. + DEVID_FIELD(ssam_device_id, target);
  20694. DEVID_FIELD(ssam_device_id, instance);
  20695. DEVID_FIELD(ssam_device_id, function);
  20696. diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
  20697. index a6c583362b92..5b79fdc42641 100644
  20698. --- a/scripts/mod/file2alias.c
  20699. +++ b/scripts/mod/file2alias.c
  20700. @@ -1368,20 +1368,22 @@ static int do_mhi_entry(const char *filename, void *symval, char *alias)
  20701. return 1;
  20702. }
  20703. -/* Looks like: ssam:cNtNiNfN
  20704. +/*
  20705. + * Looks like: ssam:dNcNtNiNfN
  20706. *
  20707. * N is exactly 2 digits, where each is an upper-case hex digit.
  20708. */
  20709. static int do_ssam_entry(const char *filename, void *symval, char *alias)
  20710. {
  20711. DEF_FIELD(symval, ssam_device_id, match_flags);
  20712. + DEF_FIELD(symval, ssam_device_id, domain);
  20713. DEF_FIELD(symval, ssam_device_id, category);
  20714. - DEF_FIELD(symval, ssam_device_id, channel);
  20715. + DEF_FIELD(symval, ssam_device_id, target);
  20716. DEF_FIELD(symval, ssam_device_id, instance);
  20717. DEF_FIELD(symval, ssam_device_id, function);
  20718. - sprintf(alias, "ssam:c%02X", category);
  20719. - ADD(alias, "t", match_flags & SSAM_MATCH_CHANNEL, channel);
  20720. + sprintf(alias, "ssam:d%02Xc%02X", domain, category);
  20721. + ADD(alias, "t", match_flags & SSAM_MATCH_TARGET, target);
  20722. ADD(alias, "i", match_flags & SSAM_MATCH_INSTANCE, instance);
  20723. ADD(alias, "f", match_flags & SSAM_MATCH_FUNCTION, function);
  20724. --
  20725. 2.33.0