12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501 |
- From 0c700275d9f211fd8a11f7d6614e2f520638ba1f Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Mon, 21 Dec 2020 19:39:51 +0100
- Subject: [PATCH] platform/surface: Add Surface Aggregator subsystem
- Add Surface System Aggregator Module core and Surface Serial Hub driver,
- required for the embedded controller found on Microsoft Surface devices.
- The Surface System Aggregator Module (SSAM, SAM or Surface Aggregator)
- is an embedded controller (EC) found on 4th and later generation
- Microsoft Surface devices, with the exception of the Surface Go series.
- This EC provides various functionality, depending on the device in
- question. This can include battery status and thermal reporting (5th and
- later generations), but also HID keyboard (6th+) and touchpad input
- (7th+) on Surface Laptop and Surface Book 3 series devices.
- This patch provides the basic necessities for communication with the SAM
- EC on 5th and later generation devices. On these devices, the EC
- provides an interface that acts as serial device, called the Surface
- Serial Hub (SSH). 4th generation devices, on which the EC interface is
- provided via an HID-over-I2C device, are not supported by this patch.
- Specifically, this patch adds a driver for the SSH device (device HID
- MSHW0084 in ACPI), as well as a controller structure and associated API.
- This represents the functional core of the Surface Aggregator kernel
- subsystem, introduced with this patch, and will be expanded upon in
- subsequent commits.
- The SSH driver acts as the main attachment point for this subsystem and
- sets-up and manages the controller structure. The controller in turn
- provides a basic communication interface, allowing to send requests from
- host to EC and receiving the corresponding responses, as well as
- managing and receiving events, sent from EC to host. It is structured
- into multiple layers, with the top layer presenting the API used by
- other kernel drivers and the lower layers modeled after the serial
- protocol used for communication.
- Said other drivers are then responsible for providing the (Surface model
- specific) functionality accessible through the EC (e.g. battery status
- reporting, thermal information, ...) via said controller structure and
- API, and will be added in future commits.
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Link: https://lore.kernel.org/r/20201221183959.1186143-2-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- MAINTAINERS | 8 +
- drivers/platform/surface/Kconfig | 2 +
- drivers/platform/surface/Makefile | 1 +
- drivers/platform/surface/aggregator/Kconfig | 42 +
- drivers/platform/surface/aggregator/Makefile | 10 +
- .../platform/surface/aggregator/controller.c | 2504 +++++++++++++++++
- .../platform/surface/aggregator/controller.h | 276 ++
- drivers/platform/surface/aggregator/core.c | 787 ++++++
- .../platform/surface/aggregator/ssh_msgb.h | 205 ++
- .../surface/aggregator/ssh_packet_layer.c | 1710 +++++++++++
- .../surface/aggregator/ssh_packet_layer.h | 187 ++
- .../platform/surface/aggregator/ssh_parser.c | 228 ++
- .../platform/surface/aggregator/ssh_parser.h | 154 +
- .../surface/aggregator/ssh_request_layer.c | 1211 ++++++++
- .../surface/aggregator/ssh_request_layer.h | 143 +
- include/linux/surface_aggregator/controller.h | 824 ++++++
- include/linux/surface_aggregator/serial_hub.h | 672 +++++
- 17 files changed, 8964 insertions(+)
- create mode 100644 drivers/platform/surface/aggregator/Kconfig
- create mode 100644 drivers/platform/surface/aggregator/Makefile
- create mode 100644 drivers/platform/surface/aggregator/controller.c
- create mode 100644 drivers/platform/surface/aggregator/controller.h
- create mode 100644 drivers/platform/surface/aggregator/core.c
- create mode 100644 drivers/platform/surface/aggregator/ssh_msgb.h
- create mode 100644 drivers/platform/surface/aggregator/ssh_packet_layer.c
- create mode 100644 drivers/platform/surface/aggregator/ssh_packet_layer.h
- create mode 100644 drivers/platform/surface/aggregator/ssh_parser.c
- create mode 100644 drivers/platform/surface/aggregator/ssh_parser.h
- create mode 100644 drivers/platform/surface/aggregator/ssh_request_layer.c
- create mode 100644 drivers/platform/surface/aggregator/ssh_request_layer.h
- create mode 100644 include/linux/surface_aggregator/controller.h
- create mode 100644 include/linux/surface_aggregator/serial_hub.h
- diff --git a/MAINTAINERS b/MAINTAINERS
- index b6ab9c1a2119..530792c869c4 100644
- --- a/MAINTAINERS
- +++ b/MAINTAINERS
- @@ -11806,6 +11806,14 @@ L: platform-driver-x86@vger.kernel.org
- S: Supported
- F: drivers/platform/surface/surfacepro3_button.c
-
- +MICROSOFT SURFACE SYSTEM AGGREGATOR SUBSYSTEM
- +M: Maximilian Luz <luzmaximilian@gmail.com>
- +S: Maintained
- +W: https://github.com/linux-surface/surface-aggregator-module
- +C: irc://chat.freenode.net/##linux-surface
- +F: drivers/platform/surface/aggregator/
- +F: include/linux/surface_aggregator/
- +
- MICROTEK X6 SCANNER
- M: Oliver Neukum <oliver@neukum.org>
- S: Maintained
- diff --git a/drivers/platform/surface/Kconfig b/drivers/platform/surface/Kconfig
- index b5dc9148066c..ef6b4051e7c8 100644
- --- a/drivers/platform/surface/Kconfig
- +++ b/drivers/platform/surface/Kconfig
- @@ -63,4 +63,6 @@ config SURFACE_PRO3_BUTTON
- help
- This driver handles the power/home/volume buttons on the Microsoft Surface Pro 3/4 tablet.
-
- +source "drivers/platform/surface/aggregator/Kconfig"
- +
- endif # SURFACE_PLATFORMS
- diff --git a/drivers/platform/surface/Makefile b/drivers/platform/surface/Makefile
- index 3d5fa0daa56b..c5392098cfb9 100644
- --- a/drivers/platform/surface/Makefile
- +++ b/drivers/platform/surface/Makefile
- @@ -7,6 +7,7 @@
- obj-$(CONFIG_SURFACE3_WMI) += surface3-wmi.o
- obj-$(CONFIG_SURFACE_3_BUTTON) += surface3_button.o
- obj-$(CONFIG_SURFACE_3_POWER_OPREGION) += surface3_power.o
- +obj-$(CONFIG_SURFACE_AGGREGATOR) += aggregator/
- obj-$(CONFIG_SURFACE_BOOK1_DGPU_SWITCH) += surfacebook1_dgpu_switch.o
- obj-$(CONFIG_SURFACE_GPE) += surface_gpe.o
- obj-$(CONFIG_SURFACE_PRO3_BUTTON) += surfacepro3_button.o
- diff --git a/drivers/platform/surface/aggregator/Kconfig b/drivers/platform/surface/aggregator/Kconfig
- new file mode 100644
- index 000000000000..e9f4ad96e40a
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/Kconfig
- @@ -0,0 +1,42 @@
- +# SPDX-License-Identifier: GPL-2.0+
- +# Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- +
- +menuconfig SURFACE_AGGREGATOR
- + tristate "Microsoft Surface System Aggregator Module Subsystem and Drivers"
- + depends on SERIAL_DEV_BUS
- + select CRC_CCITT
- + help
- + The Surface System Aggregator Module (Surface SAM or SSAM) is an
- + embedded controller (EC) found on 5th- and later-generation Microsoft
- + Surface devices (i.e. Surface Pro 5, Surface Book 2, Surface Laptop,
- + and newer, with exception of Surface Go series devices).
- +
- + Depending on the device in question, this EC provides varying
- + functionality, including:
- + - EC access from ACPI via Surface ACPI Notify (5th- and 6th-generation)
- + - battery status information (all devices)
- + - thermal sensor access (all devices)
- + - performance mode / cooling mode control (all devices)
- + - clipboard detachment system control (Surface Book 2 and 3)
- + - HID / keyboard input (Surface Laptops, Surface Book 3)
- +
- + This option controls whether the Surface SAM subsystem core will be
- + built. This includes a driver for the Surface Serial Hub (SSH), which
- + is the device responsible for the communication with the EC, and a
- + basic kernel interface exposing the EC functionality to other client
- + drivers, i.e. allowing them to make requests to the EC and receive
- + events from it. Selecting this option alone will not provide any
- + client drivers and therefore no functionality beyond the in-kernel
- + interface. Said functionality is the responsibility of the respective
- + client drivers.
- +
- + Note: While 4th-generation Surface devices also make use of a SAM EC,
- + due to a difference in the communication interface of the controller,
- + only 5th and later generations are currently supported. Specifically,
- + devices using SAM-over-SSH are supported, whereas devices using
- + SAM-over-HID, which is used on the 4th generation, are currently not
- + supported.
- +
- + Choose m if you want to build the SAM subsystem core and SSH driver as
- + module, y if you want to build it into the kernel and n if you don't
- + want it at all.
- diff --git a/drivers/platform/surface/aggregator/Makefile b/drivers/platform/surface/aggregator/Makefile
- new file mode 100644
- index 000000000000..faad18d4a7f2
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/Makefile
- @@ -0,0 +1,10 @@
- +# SPDX-License-Identifier: GPL-2.0+
- +# Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- +
- +obj-$(CONFIG_SURFACE_AGGREGATOR) += surface_aggregator.o
- +
- +surface_aggregator-objs := core.o
- +surface_aggregator-objs += ssh_parser.o
- +surface_aggregator-objs += ssh_packet_layer.o
- +surface_aggregator-objs += ssh_request_layer.o
- +surface_aggregator-objs += controller.o
- diff --git a/drivers/platform/surface/aggregator/controller.c b/drivers/platform/surface/aggregator/controller.c
- new file mode 100644
- index 000000000000..488318cf2098
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/controller.c
- @@ -0,0 +1,2504 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * Main SSAM/SSH controller structure and functionality.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <linux/acpi.h>
- +#include <linux/atomic.h>
- +#include <linux/completion.h>
- +#include <linux/gpio/consumer.h>
- +#include <linux/interrupt.h>
- +#include <linux/kref.h>
- +#include <linux/limits.h>
- +#include <linux/list.h>
- +#include <linux/lockdep.h>
- +#include <linux/mutex.h>
- +#include <linux/rculist.h>
- +#include <linux/rbtree.h>
- +#include <linux/rwsem.h>
- +#include <linux/serdev.h>
- +#include <linux/slab.h>
- +#include <linux/spinlock.h>
- +#include <linux/srcu.h>
- +#include <linux/types.h>
- +#include <linux/workqueue.h>
- +
- +#include <linux/surface_aggregator/controller.h>
- +#include <linux/surface_aggregator/serial_hub.h>
- +
- +#include "controller.h"
- +#include "ssh_msgb.h"
- +#include "ssh_request_layer.h"
- +
- +
- +/* -- Safe counters. -------------------------------------------------------- */
- +
- +/**
- + * ssh_seq_reset() - Reset/initialize sequence ID counter.
- + * @c: The counter to reset.
- + */
- +static void ssh_seq_reset(struct ssh_seq_counter *c)
- +{
- + WRITE_ONCE(c->value, 0);
- +}
- +
- +/**
- + * ssh_seq_next() - Get next sequence ID.
- + * @c: The counter providing the sequence IDs.
- + *
- + * Return: Returns the next sequence ID of the counter.
- + */
- +static u8 ssh_seq_next(struct ssh_seq_counter *c)
- +{
- + u8 old = READ_ONCE(c->value);
- + u8 new = old + 1;
- + u8 ret;
- +
- + while (unlikely((ret = cmpxchg(&c->value, old, new)) != old)) {
- + old = ret;
- + new = old + 1;
- + }
- +
- + return old;
- +}
- +
- +/**
- + * ssh_rqid_reset() - Reset/initialize request ID counter.
- + * @c: The counter to reset.
- + */
- +static void ssh_rqid_reset(struct ssh_rqid_counter *c)
- +{
- + WRITE_ONCE(c->value, 0);
- +}
- +
- +/**
- + * ssh_rqid_next() - Get next request ID.
- + * @c: The counter providing the request IDs.
- + *
- + * Return: Returns the next request ID of the counter, skipping any reserved
- + * request IDs.
- + */
- +static u16 ssh_rqid_next(struct ssh_rqid_counter *c)
- +{
- + u16 old = READ_ONCE(c->value);
- + u16 new = ssh_rqid_next_valid(old);
- + u16 ret;
- +
- + while (unlikely((ret = cmpxchg(&c->value, old, new)) != old)) {
- + old = ret;
- + new = ssh_rqid_next_valid(old);
- + }
- +
- + return old;
- +}
- +
- +
- +/* -- Event notifier/callbacks. --------------------------------------------- */
- +/*
- + * The notifier system is based on linux/notifier.h, specifically the SRCU
- + * implementation. The difference to that is, that some bits of the notifier
- + * call return value can be tracked across multiple calls. This is done so
- + * that handling of events can be tracked and a warning can be issued in case
- + * an event goes unhandled. The idea of that warning is that it should help
- + * discover and identify new/currently unimplemented features.
- + */
- +
- +/**
- + * ssam_event_matches_notifier() - Test if an event matches a notifier.
- + * @n: The event notifier to test against.
- + * @event: The event to test.
- + *
- + * Return: Returns %true if the given event matches the given notifier
- + * according to the rules set in the notifier's event mask, %false otherwise.
- + */
- +static bool ssam_event_matches_notifier(const struct ssam_event_notifier *n,
- + const struct ssam_event *event)
- +{
- + bool match = n->event.id.target_category == event->target_category;
- +
- + if (n->event.mask & SSAM_EVENT_MASK_TARGET)
- + match &= n->event.reg.target_id == event->target_id;
- +
- + if (n->event.mask & SSAM_EVENT_MASK_INSTANCE)
- + match &= n->event.id.instance == event->instance_id;
- +
- + return match;
- +}
- +
- +/**
- + * ssam_nfblk_call_chain() - Call event notifier callbacks of the given chain.
- + * @nh: The notifier head for which the notifier callbacks should be called.
- + * @event: The event data provided to the callbacks.
- + *
- + * Call all registered notifier callbacks in order of their priority until
- + * either no notifier is left or a notifier returns a value with the
- + * %SSAM_NOTIF_STOP bit set. Note that this bit is automatically set via
- + * ssam_notifier_from_errno() on any non-zero error value.
- + *
- + * Return: Returns the notifier status value, which contains the notifier
- + * status bits (%SSAM_NOTIF_HANDLED and %SSAM_NOTIF_STOP) as well as a
- + * potential error value returned from the last executed notifier callback.
- + * Use ssam_notifier_to_errno() to convert this value to the original error
- + * value.
- + */
- +static int ssam_nfblk_call_chain(struct ssam_nf_head *nh, struct ssam_event *event)
- +{
- + struct ssam_event_notifier *nf;
- + int ret = 0, idx;
- +
- + idx = srcu_read_lock(&nh->srcu);
- +
- + list_for_each_entry_rcu(nf, &nh->head, base.node,
- + srcu_read_lock_held(&nh->srcu)) {
- + if (ssam_event_matches_notifier(nf, event)) {
- + ret = (ret & SSAM_NOTIF_STATE_MASK) | nf->base.fn(nf, event);
- + if (ret & SSAM_NOTIF_STOP)
- + break;
- + }
- + }
- +
- + srcu_read_unlock(&nh->srcu, idx);
- + return ret;
- +}
- +
- +/**
- + * ssam_nfblk_insert() - Insert a new notifier block into the given notifier
- + * list.
- + * @nh: The notifier head into which the block should be inserted.
- + * @nb: The notifier block to add.
- + *
- + * Note: This function must be synchronized by the caller with respect to other
- + * insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
- + *
- + * Return: Returns zero on success, %-EEXIST if the notifier block has already
- + * been registered.
- + */
- +static int ssam_nfblk_insert(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
- +{
- + struct ssam_notifier_block *p;
- + struct list_head *h;
- +
- + /* Runs under lock, no need for RCU variant. */
- + list_for_each(h, &nh->head) {
- + p = list_entry(h, struct ssam_notifier_block, node);
- +
- + if (unlikely(p == nb)) {
- + WARN(1, "double register detected");
- + return -EEXIST;
- + }
- +
- + if (nb->priority > p->priority)
- + break;
- + }
- +
- + list_add_tail_rcu(&nb->node, h);
- + return 0;
- +}
- +
- +/**
- + * ssam_nfblk_find() - Check if a notifier block is registered on the given
- + * notifier head.
- + * list.
- + * @nh: The notifier head on which to search.
- + * @nb: The notifier block to search for.
- + *
- + * Note: This function must be synchronized by the caller with respect to other
- + * insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
- + *
- + * Return: Returns true if the given notifier block is registered on the given
- + * notifier head, false otherwise.
- + */
- +static bool ssam_nfblk_find(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
- +{
- + struct ssam_notifier_block *p;
- +
- + /* Runs under lock, no need for RCU variant. */
- + list_for_each_entry(p, &nh->head, node) {
- + if (p == nb)
- + return true;
- + }
- +
- + return false;
- +}
- +
- +/**
- + * ssam_nfblk_remove() - Remove a notifier block from its notifier list.
- + * @nb: The notifier block to be removed.
- + *
- + * Note: This function must be synchronized by the caller with respect to
- + * other insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
- + * Furthermore, the caller _must_ ensure SRCU synchronization by calling
- + * synchronize_srcu() with ``nh->srcu`` after leaving the critical section, to
- + * ensure that the removed notifier block is not in use any more.
- + */
- +static void ssam_nfblk_remove(struct ssam_notifier_block *nb)
- +{
- + list_del_rcu(&nb->node);
- +}
- +
- +/**
- + * ssam_nf_head_init() - Initialize the given notifier head.
- + * @nh: The notifier head to initialize.
- + */
- +static int ssam_nf_head_init(struct ssam_nf_head *nh)
- +{
- + int status;
- +
- + status = init_srcu_struct(&nh->srcu);
- + if (status)
- + return status;
- +
- + INIT_LIST_HEAD(&nh->head);
- + return 0;
- +}
- +
- +/**
- + * ssam_nf_head_destroy() - Deinitialize the given notifier head.
- + * @nh: The notifier head to deinitialize.
- + */
- +static void ssam_nf_head_destroy(struct ssam_nf_head *nh)
- +{
- + cleanup_srcu_struct(&nh->srcu);
- +}
- +
- +
- +/* -- Event/notification registry. ------------------------------------------ */
- +
- +/**
- + * struct ssam_nf_refcount_key - Key used for event activation reference
- + * counting.
- + * @reg: The registry via which the event is enabled/disabled.
- + * @id: The ID uniquely describing the event.
- + */
- +struct ssam_nf_refcount_key {
- + struct ssam_event_registry reg;
- + struct ssam_event_id id;
- +};
- +
- +/**
- + * struct ssam_nf_refcount_entry - RB-tree entry for reference counting event
- + * activations.
- + * @node: The node of this entry in the rb-tree.
- + * @key: The key of the event.
- + * @refcount: The reference-count of the event.
- + * @flags: The flags used when enabling the event.
- + */
- +struct ssam_nf_refcount_entry {
- + struct rb_node node;
- + struct ssam_nf_refcount_key key;
- + int refcount;
- + u8 flags;
- +};
- +
- +/**
- + * ssam_nf_refcount_inc() - Increment reference-/activation-count of the given
- + * event.
- + * @nf: The notifier system reference.
- + * @reg: The registry used to enable/disable the event.
- + * @id: The event ID.
- + *
- + * Increments the reference-/activation-count associated with the specified
- + * event type/ID, allocating a new entry for this event ID if necessary. A
- + * newly allocated entry will have a refcount of one.
- + *
- + * Note: ``nf->lock`` must be held when calling this function.
- + *
- + * Return: Returns the refcount entry on success. Returns an error pointer
- + * with %-ENOSPC if there have already been %INT_MAX events of the specified
- + * ID and type registered, or %-ENOMEM if the entry could not be allocated.
- + */
- +static struct ssam_nf_refcount_entry *
- +ssam_nf_refcount_inc(struct ssam_nf *nf, struct ssam_event_registry reg,
- + struct ssam_event_id id)
- +{
- + struct ssam_nf_refcount_entry *entry;
- + struct ssam_nf_refcount_key key;
- + struct rb_node **link = &nf->refcount.rb_node;
- + struct rb_node *parent = NULL;
- + int cmp;
- +
- + lockdep_assert_held(&nf->lock);
- +
- + key.reg = reg;
- + key.id = id;
- +
- + while (*link) {
- + entry = rb_entry(*link, struct ssam_nf_refcount_entry, node);
- + parent = *link;
- +
- + cmp = memcmp(&key, &entry->key, sizeof(key));
- + if (cmp < 0) {
- + link = &(*link)->rb_left;
- + } else if (cmp > 0) {
- + link = &(*link)->rb_right;
- + } else if (entry->refcount < INT_MAX) {
- + entry->refcount++;
- + return entry;
- + } else {
- + WARN_ON(1);
- + return ERR_PTR(-ENOSPC);
- + }
- + }
- +
- + entry = kzalloc(sizeof(*entry), GFP_KERNEL);
- + if (!entry)
- + return ERR_PTR(-ENOMEM);
- +
- + entry->key = key;
- + entry->refcount = 1;
- +
- + rb_link_node(&entry->node, parent, link);
- + rb_insert_color(&entry->node, &nf->refcount);
- +
- + return entry;
- +}
- +
- +/**
- + * ssam_nf_refcount_dec() - Decrement reference-/activation-count of the given
- + * event.
- + * @nf: The notifier system reference.
- + * @reg: The registry used to enable/disable the event.
- + * @id: The event ID.
- + *
- + * Decrements the reference-/activation-count of the specified event,
- + * returning its entry. If the returned entry has a refcount of zero, the
- + * caller is responsible for freeing it using kfree().
- + *
- + * Note: ``nf->lock`` must be held when calling this function.
- + *
- + * Return: Returns the refcount entry on success or %NULL if the entry has not
- + * been found.
- + */
- +static struct ssam_nf_refcount_entry *
- +ssam_nf_refcount_dec(struct ssam_nf *nf, struct ssam_event_registry reg,
- + struct ssam_event_id id)
- +{
- + struct ssam_nf_refcount_entry *entry;
- + struct ssam_nf_refcount_key key;
- + struct rb_node *node = nf->refcount.rb_node;
- + int cmp;
- +
- + lockdep_assert_held(&nf->lock);
- +
- + key.reg = reg;
- + key.id = id;
- +
- + while (node) {
- + entry = rb_entry(node, struct ssam_nf_refcount_entry, node);
- +
- + cmp = memcmp(&key, &entry->key, sizeof(key));
- + if (cmp < 0) {
- + node = node->rb_left;
- + } else if (cmp > 0) {
- + node = node->rb_right;
- + } else {
- + entry->refcount--;
- + if (entry->refcount == 0)
- + rb_erase(&entry->node, &nf->refcount);
- +
- + return entry;
- + }
- + }
- +
- + return NULL;
- +}
- +
- +/**
- + * ssam_nf_refcount_empty() - Test if the notification system has any
- + * enabled/active events.
- + * @nf: The notification system.
- + */
- +static bool ssam_nf_refcount_empty(struct ssam_nf *nf)
- +{
- + return RB_EMPTY_ROOT(&nf->refcount);
- +}
- +
- +/**
- + * ssam_nf_call() - Call notification callbacks for the provided event.
- + * @nf: The notifier system
- + * @dev: The associated device, only used for logging.
- + * @rqid: The request ID of the event.
- + * @event: The event provided to the callbacks.
- + *
- + * Execute registered callbacks in order of their priority until either no
- + * callback is left or a callback returns a value with the %SSAM_NOTIF_STOP
- + * bit set. Note that this bit is set automatically when converting non-zero
- + * error values via ssam_notifier_from_errno() to notifier values.
- + *
- + * Also note that any callback that could handle an event should return a value
- + * with bit %SSAM_NOTIF_HANDLED set, indicating that the event does not go
- + * unhandled/ignored. In case no registered callback could handle an event,
- + * this function will emit a warning.
- + *
- + * In case a callback failed, this function will emit an error message.
- + */
- +static void ssam_nf_call(struct ssam_nf *nf, struct device *dev, u16 rqid,
- + struct ssam_event *event)
- +{
- + struct ssam_nf_head *nf_head;
- + int status, nf_ret;
- +
- + if (!ssh_rqid_is_event(rqid)) {
- + dev_warn(dev, "event: unsupported rqid: %#06x\n", rqid);
- + return;
- + }
- +
- + nf_head = &nf->head[ssh_rqid_to_event(rqid)];
- + nf_ret = ssam_nfblk_call_chain(nf_head, event);
- + status = ssam_notifier_to_errno(nf_ret);
- +
- + if (status < 0) {
- + dev_err(dev,
- + "event: error handling event: %d (tc: %#04x, tid: %#04x, cid: %#04x, iid: %#04x)\n",
- + status, event->target_category, event->target_id,
- + event->command_id, event->instance_id);
- + } else if (!(nf_ret & SSAM_NOTIF_HANDLED)) {
- + dev_warn(dev,
- + "event: unhandled event (rqid: %#04x, tc: %#04x, tid: %#04x, cid: %#04x, iid: %#04x)\n",
- + rqid, event->target_category, event->target_id,
- + event->command_id, event->instance_id);
- + }
- +}
- +
- +/**
- + * ssam_nf_init() - Initialize the notifier system.
- + * @nf: The notifier system to initialize.
- + */
- +static int ssam_nf_init(struct ssam_nf *nf)
- +{
- + int i, status;
- +
- + for (i = 0; i < SSH_NUM_EVENTS; i++) {
- + status = ssam_nf_head_init(&nf->head[i]);
- + if (status)
- + break;
- + }
- +
- + if (status) {
- + while (i--)
- + ssam_nf_head_destroy(&nf->head[i]);
- +
- + return status;
- + }
- +
- + mutex_init(&nf->lock);
- + return 0;
- +}
- +
- +/**
- + * ssam_nf_destroy() - Deinitialize the notifier system.
- + * @nf: The notifier system to deinitialize.
- + */
- +static void ssam_nf_destroy(struct ssam_nf *nf)
- +{
- + int i;
- +
- + for (i = 0; i < SSH_NUM_EVENTS; i++)
- + ssam_nf_head_destroy(&nf->head[i]);
- +
- + mutex_destroy(&nf->lock);
- +}
- +
- +
- +/* -- Event/async request completion system. -------------------------------- */
- +
- +#define SSAM_CPLT_WQ_NAME "ssam_cpltq"
- +
- +/*
- + * SSAM_CPLT_WQ_BATCH - Maximum number of event item completions executed per
- + * work execution. Used to prevent livelocking of the workqueue. Value chosen
- + * via educated guess, may be adjusted.
- + */
- +#define SSAM_CPLT_WQ_BATCH 10
- +
- +/**
- + * ssam_event_item_alloc() - Allocate an event item with the given payload size.
- + * @len: The event payload length.
- + * @flags: The flags used for allocation.
- + *
- + * Allocate an event item with the given payload size. Sets the item
- + * operations and payload length values. The item free callback (``ops.free``)
- + * should not be overwritten after this call.
- + *
- + * Return: Returns the newly allocated event item.
- + */
- +static struct ssam_event_item *ssam_event_item_alloc(size_t len, gfp_t flags)
- +{
- + struct ssam_event_item *item;
- +
- + item = kzalloc(struct_size(item, event.data, len), flags);
- + if (!item)
- + return NULL;
- +
- + item->event.length = len;
- + return item;
- +}
- +
- +/**
- + * ssam_event_queue_push() - Push an event item to the event queue.
- + * @q: The event queue.
- + * @item: The item to add.
- + */
- +static void ssam_event_queue_push(struct ssam_event_queue *q,
- + struct ssam_event_item *item)
- +{
- + spin_lock(&q->lock);
- + list_add_tail(&item->node, &q->head);
- + spin_unlock(&q->lock);
- +}
- +
- +/**
- + * ssam_event_queue_pop() - Pop the next event item from the event queue.
- + * @q: The event queue.
- + *
- + * Returns and removes the next event item from the queue. Returns %NULL If
- + * there is no event item left.
- + */
- +static struct ssam_event_item *ssam_event_queue_pop(struct ssam_event_queue *q)
- +{
- + struct ssam_event_item *item;
- +
- + spin_lock(&q->lock);
- + item = list_first_entry_or_null(&q->head, struct ssam_event_item, node);
- + if (item)
- + list_del(&item->node);
- + spin_unlock(&q->lock);
- +
- + return item;
- +}
- +
- +/**
- + * ssam_event_queue_is_empty() - Check if the event queue is empty.
- + * @q: The event queue.
- + */
- +static bool ssam_event_queue_is_empty(struct ssam_event_queue *q)
- +{
- + bool empty;
- +
- + spin_lock(&q->lock);
- + empty = list_empty(&q->head);
- + spin_unlock(&q->lock);
- +
- + return empty;
- +}
- +
- +/**
- + * ssam_cplt_get_event_queue() - Get the event queue for the given parameters.
- + * @cplt: The completion system on which to look for the queue.
- + * @tid: The target ID of the queue.
- + * @rqid: The request ID representing the event ID for which to get the queue.
- + *
- + * Return: Returns the event queue corresponding to the event type described
- + * by the given parameters. If the request ID does not represent an event,
- + * this function returns %NULL. If the target ID is not supported, this
- + * function will fall back to the default target ID (``tid = 1``).
- + */
- +static
- +struct ssam_event_queue *ssam_cplt_get_event_queue(struct ssam_cplt *cplt,
- + u8 tid, u16 rqid)
- +{
- + u16 event = ssh_rqid_to_event(rqid);
- + u16 tidx = ssh_tid_to_index(tid);
- +
- + if (!ssh_rqid_is_event(rqid)) {
- + dev_err(cplt->dev, "event: unsupported request ID: %#06x\n", rqid);
- + return NULL;
- + }
- +
- + if (!ssh_tid_is_valid(tid)) {
- + dev_warn(cplt->dev, "event: unsupported target ID: %u\n", tid);
- + tidx = 0;
- + }
- +
- + return &cplt->event.target[tidx].queue[event];
- +}
- +
- +/**
- + * ssam_cplt_submit() - Submit a work item to the completion system workqueue.
- + * @cplt: The completion system.
- + * @work: The work item to submit.
- + */
- +static bool ssam_cplt_submit(struct ssam_cplt *cplt, struct work_struct *work)
- +{
- + return queue_work(cplt->wq, work);
- +}
- +
- +/**
- + * ssam_cplt_submit_event() - Submit an event to the completion system.
- + * @cplt: The completion system.
- + * @item: The event item to submit.
- + *
- + * Submits the event to the completion system by queuing it on the event item
- + * queue and queuing the respective event queue work item on the completion
- + * workqueue, which will eventually complete the event.
- + *
- + * Return: Returns zero on success, %-EINVAL if there is no event queue that
- + * can handle the given event item.
- + */
- +static int ssam_cplt_submit_event(struct ssam_cplt *cplt,
- + struct ssam_event_item *item)
- +{
- + struct ssam_event_queue *evq;
- +
- + evq = ssam_cplt_get_event_queue(cplt, item->event.target_id, item->rqid);
- + if (!evq)
- + return -EINVAL;
- +
- + ssam_event_queue_push(evq, item);
- + ssam_cplt_submit(cplt, &evq->work);
- + return 0;
- +}
- +
- +/**
- + * ssam_cplt_flush() - Flush the completion system.
- + * @cplt: The completion system.
- + *
- + * Flush the completion system by waiting until all currently submitted work
- + * items have been completed.
- + *
- + * Note: This function does not guarantee that all events will have been
- + * handled once this call terminates. In case of a larger number of
- + * to-be-completed events, the event queue work function may re-schedule its
- + * work item, which this flush operation will ignore.
- + *
- + * This operation is only intended to, during normal operation prior to
- + * shutdown, try to complete most events and requests to get them out of the
- + * system while the system is still fully operational. It does not aim to
- + * provide any guarantee that all of them have been handled.
- + */
- +static void ssam_cplt_flush(struct ssam_cplt *cplt)
- +{
- + flush_workqueue(cplt->wq);
- +}
- +
- +static void ssam_event_queue_work_fn(struct work_struct *work)
- +{
- + struct ssam_event_queue *queue;
- + struct ssam_event_item *item;
- + struct ssam_nf *nf;
- + struct device *dev;
- + unsigned int iterations = SSAM_CPLT_WQ_BATCH;
- +
- + queue = container_of(work, struct ssam_event_queue, work);
- + nf = &queue->cplt->event.notif;
- + dev = queue->cplt->dev;
- +
- + /* Limit number of processed events to avoid livelocking. */
- + do {
- + item = ssam_event_queue_pop(queue);
- + if (!item)
- + return;
- +
- + ssam_nf_call(nf, dev, item->rqid, &item->event);
- + kfree(item);
- + } while (--iterations);
- +
- + if (!ssam_event_queue_is_empty(queue))
- + ssam_cplt_submit(queue->cplt, &queue->work);
- +}
- +
- +/**
- + * ssam_event_queue_init() - Initialize an event queue.
- + * @cplt: The completion system on which the queue resides.
- + * @evq: The event queue to initialize.
- + */
- +static void ssam_event_queue_init(struct ssam_cplt *cplt,
- + struct ssam_event_queue *evq)
- +{
- + evq->cplt = cplt;
- + spin_lock_init(&evq->lock);
- + INIT_LIST_HEAD(&evq->head);
- + INIT_WORK(&evq->work, ssam_event_queue_work_fn);
- +}
- +
- +/**
- + * ssam_cplt_init() - Initialize completion system.
- + * @cplt: The completion system to initialize.
- + * @dev: The device used for logging.
- + */
- +static int ssam_cplt_init(struct ssam_cplt *cplt, struct device *dev)
- +{
- + struct ssam_event_target *target;
- + int status, c, i;
- +
- + cplt->dev = dev;
- +
- + cplt->wq = create_workqueue(SSAM_CPLT_WQ_NAME);
- + if (!cplt->wq)
- + return -ENOMEM;
- +
- + for (c = 0; c < ARRAY_SIZE(cplt->event.target); c++) {
- + target = &cplt->event.target[c];
- +
- + for (i = 0; i < ARRAY_SIZE(target->queue); i++)
- + ssam_event_queue_init(cplt, &target->queue[i]);
- + }
- +
- + status = ssam_nf_init(&cplt->event.notif);
- + if (status)
- + destroy_workqueue(cplt->wq);
- +
- + return status;
- +}
- +
- +/**
- + * ssam_cplt_destroy() - Deinitialize the completion system.
- + * @cplt: The completion system to deinitialize.
- + *
- + * Deinitialize the given completion system and ensure that all pending, i.e.
- + * yet-to-be-completed, event items and requests have been handled.
- + */
- +static void ssam_cplt_destroy(struct ssam_cplt *cplt)
- +{
- + /*
- + * Note: destroy_workqueue ensures that all currently queued work will
- + * be fully completed and the workqueue drained. This means that this
- + * call will inherently also free any queued ssam_event_items, thus we
- + * don't have to take care of that here explicitly.
- + */
- + destroy_workqueue(cplt->wq);
- + ssam_nf_destroy(&cplt->event.notif);
- +}
- +
- +
- +/* -- Main SSAM device structures. ------------------------------------------ */
- +
- +/**
- + * ssam_controller_device() - Get the &struct device associated with this
- + * controller.
- + * @c: The controller for which to get the device.
- + *
- + * Return: Returns the &struct device associated with this controller,
- + * providing its lower-level transport.
- + */
- +struct device *ssam_controller_device(struct ssam_controller *c)
- +{
- + return ssh_rtl_get_device(&c->rtl);
- +}
- +EXPORT_SYMBOL_GPL(ssam_controller_device);
- +
- +static void __ssam_controller_release(struct kref *kref)
- +{
- + struct ssam_controller *ctrl = to_ssam_controller(kref, kref);
- +
- + /*
- + * The lock-call here is to satisfy lockdep. At this point we really
- + * expect this to be the last remaining reference to the controller.
- + * Anything else is a bug.
- + */
- + ssam_controller_lock(ctrl);
- + ssam_controller_destroy(ctrl);
- + ssam_controller_unlock(ctrl);
- +
- + kfree(ctrl);
- +}
- +
- +/**
- + * ssam_controller_get() - Increment reference count of controller.
- + * @c: The controller.
- + *
- + * Return: Returns the controller provided as input.
- + */
- +struct ssam_controller *ssam_controller_get(struct ssam_controller *c)
- +{
- + if (c)
- + kref_get(&c->kref);
- + return c;
- +}
- +EXPORT_SYMBOL_GPL(ssam_controller_get);
- +
- +/**
- + * ssam_controller_put() - Decrement reference count of controller.
- + * @c: The controller.
- + */
- +void ssam_controller_put(struct ssam_controller *c)
- +{
- + if (c)
- + kref_put(&c->kref, __ssam_controller_release);
- +}
- +EXPORT_SYMBOL_GPL(ssam_controller_put);
- +
- +/**
- + * ssam_controller_statelock() - Lock the controller against state transitions.
- + * @c: The controller to lock.
- + *
- + * Lock the controller against state transitions. Holding this lock guarantees
- + * that the controller will not transition between states, i.e. if the
- + * controller is in state "started", when this lock has been acquired, it will
- + * remain in this state at least until the lock has been released.
- + *
- + * Multiple clients may concurrently hold this lock. In other words: The
- + * ``statelock`` functions represent the read-lock part of a r/w-semaphore.
- + * Actions causing state transitions of the controller must be executed while
- + * holding the write-part of this r/w-semaphore (see ssam_controller_lock()
- + * and ssam_controller_unlock() for that).
- + *
- + * See ssam_controller_stateunlock() for the corresponding unlock function.
- + */
- +void ssam_controller_statelock(struct ssam_controller *c)
- +{
- + down_read(&c->lock);
- +}
- +EXPORT_SYMBOL_GPL(ssam_controller_statelock);
- +
- +/**
- + * ssam_controller_stateunlock() - Unlock controller state transitions.
- + * @c: The controller to unlock.
- + *
- + * See ssam_controller_statelock() for the corresponding lock function.
- + */
- +void ssam_controller_stateunlock(struct ssam_controller *c)
- +{
- + up_read(&c->lock);
- +}
- +EXPORT_SYMBOL_GPL(ssam_controller_stateunlock);
- +
- +/**
- + * ssam_controller_lock() - Acquire the main controller lock.
- + * @c: The controller to lock.
- + *
- + * This lock must be held for any state transitions, including transition to
- + * suspend/resumed states and during shutdown. See ssam_controller_statelock()
- + * for more details on controller locking.
- + *
- + * See ssam_controller_unlock() for the corresponding unlock function.
- + */
- +void ssam_controller_lock(struct ssam_controller *c)
- +{
- + down_write(&c->lock);
- +}
- +
- +/*
- + * ssam_controller_unlock() - Release the main controller lock.
- + * @c: The controller to unlock.
- + *
- + * See ssam_controller_lock() for the corresponding lock function.
- + */
- +void ssam_controller_unlock(struct ssam_controller *c)
- +{
- + up_write(&c->lock);
- +}
- +
- +static void ssam_handle_event(struct ssh_rtl *rtl,
- + const struct ssh_command *cmd,
- + const struct ssam_span *data)
- +{
- + struct ssam_controller *ctrl = to_ssam_controller(rtl, rtl);
- + struct ssam_event_item *item;
- +
- + item = ssam_event_item_alloc(data->len, GFP_KERNEL);
- + if (!item)
- + return;
- +
- + item->rqid = get_unaligned_le16(&cmd->rqid);
- + item->event.target_category = cmd->tc;
- + item->event.target_id = cmd->tid_in;
- + item->event.command_id = cmd->cid;
- + item->event.instance_id = cmd->iid;
- + memcpy(&item->event.data[0], data->ptr, data->len);
- +
- + if (WARN_ON(ssam_cplt_submit_event(&ctrl->cplt, item)))
- + kfree(item);
- +}
- +
- +static const struct ssh_rtl_ops ssam_rtl_ops = {
- + .handle_event = ssam_handle_event,
- +};
- +
- +static bool ssam_notifier_is_empty(struct ssam_controller *ctrl);
- +static void ssam_notifier_unregister_all(struct ssam_controller *ctrl);
- +
- +#define SSAM_SSH_DSM_REVISION 0
- +
- +/* d5e383e1-d892-4a76-89fc-f6aaae7ed5b5 */
- +static const guid_t SSAM_SSH_DSM_GUID =
- + GUID_INIT(0xd5e383e1, 0xd892, 0x4a76,
- + 0x89, 0xfc, 0xf6, 0xaa, 0xae, 0x7e, 0xd5, 0xb5);
- +
- +enum ssh_dsm_fn {
- + SSH_DSM_FN_SSH_POWER_PROFILE = 0x05,
- + SSH_DSM_FN_SCREEN_ON_SLEEP_IDLE_TIMEOUT = 0x06,
- + SSH_DSM_FN_SCREEN_OFF_SLEEP_IDLE_TIMEOUT = 0x07,
- + SSH_DSM_FN_D3_CLOSES_HANDLE = 0x08,
- + SSH_DSM_FN_SSH_BUFFER_SIZE = 0x09,
- +};
- +
- +static int ssam_dsm_get_functions(acpi_handle handle, u64 *funcs)
- +{
- + union acpi_object *obj;
- + u64 mask = 0;
- + int i;
- +
- + *funcs = 0;
- +
- + /*
- + * The _DSM function is only present on newer models. It is not
- + * present on 5th and 6th generation devices (i.e. up to and including
- + * Surface Pro 6, Surface Laptop 2, Surface Book 2).
- + *
- + * If the _DSM is not present, indicate that no function is supported.
- + * This will result in default values being set.
- + */
- + if (!acpi_has_method(handle, "_DSM"))
- + return 0;
- +
- + obj = acpi_evaluate_dsm_typed(handle, &SSAM_SSH_DSM_GUID,
- + SSAM_SSH_DSM_REVISION, 0, NULL,
- + ACPI_TYPE_BUFFER);
- + if (!obj)
- + return -EIO;
- +
- + for (i = 0; i < obj->buffer.length && i < 8; i++)
- + mask |= (((u64)obj->buffer.pointer[i]) << (i * 8));
- +
- + if (mask & BIT(0))
- + *funcs = mask;
- +
- + ACPI_FREE(obj);
- + return 0;
- +}
- +
- +static int ssam_dsm_load_u32(acpi_handle handle, u64 funcs, u64 func, u32 *ret)
- +{
- + union acpi_object *obj;
- + u64 val;
- +
- + if (!(funcs & BIT(func)))
- + return 0; /* Not supported, leave *ret at its default value */
- +
- + obj = acpi_evaluate_dsm_typed(handle, &SSAM_SSH_DSM_GUID,
- + SSAM_SSH_DSM_REVISION, func, NULL,
- + ACPI_TYPE_INTEGER);
- + if (!obj)
- + return -EIO;
- +
- + val = obj->integer.value;
- + ACPI_FREE(obj);
- +
- + if (val > U32_MAX)
- + return -ERANGE;
- +
- + *ret = val;
- + return 0;
- +}
- +
- +/**
- + * ssam_controller_caps_load_from_acpi() - Load controller capabilities from
- + * ACPI _DSM.
- + * @handle: The handle of the ACPI controller/SSH device.
- + * @caps: Where to store the capabilities in.
- + *
- + * Initializes the given controller capabilities with default values, then
- + * checks and, if the respective _DSM functions are available, loads the
- + * actual capabilities from the _DSM.
- + *
- + * Return: Returns zero on success, a negative error code on failure.
- + */
- +static
- +int ssam_controller_caps_load_from_acpi(acpi_handle handle,
- + struct ssam_controller_caps *caps)
- +{
- + u32 d3_closes_handle = false;
- + u64 funcs;
- + int status;
- +
- + /* Set defaults. */
- + caps->ssh_power_profile = U32_MAX;
- + caps->screen_on_sleep_idle_timeout = U32_MAX;
- + caps->screen_off_sleep_idle_timeout = U32_MAX;
- + caps->d3_closes_handle = false;
- + caps->ssh_buffer_size = U32_MAX;
- +
- + /* Pre-load supported DSM functions. */
- + status = ssam_dsm_get_functions(handle, &funcs);
- + if (status)
- + return status;
- +
- + /* Load actual values from ACPI, if present. */
- + status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_SSH_POWER_PROFILE,
- + &caps->ssh_power_profile);
- + if (status)
- + return status;
- +
- + status = ssam_dsm_load_u32(handle, funcs,
- + SSH_DSM_FN_SCREEN_ON_SLEEP_IDLE_TIMEOUT,
- + &caps->screen_on_sleep_idle_timeout);
- + if (status)
- + return status;
- +
- + status = ssam_dsm_load_u32(handle, funcs,
- + SSH_DSM_FN_SCREEN_OFF_SLEEP_IDLE_TIMEOUT,
- + &caps->screen_off_sleep_idle_timeout);
- + if (status)
- + return status;
- +
- + status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_D3_CLOSES_HANDLE,
- + &d3_closes_handle);
- + if (status)
- + return status;
- +
- + caps->d3_closes_handle = !!d3_closes_handle;
- +
- + status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_SSH_BUFFER_SIZE,
- + &caps->ssh_buffer_size);
- + if (status)
- + return status;
- +
- + return 0;
- +}
- +
- +/**
- + * ssam_controller_init() - Initialize SSAM controller.
- + * @ctrl: The controller to initialize.
- + * @serdev: The serial device representing the underlying data transport.
- + *
- + * Initializes the given controller. Does neither start receiver nor
- + * transmitter threads. After this call, the controller has to be hooked up to
- + * the serdev core separately via &struct serdev_device_ops, relaying calls to
- + * ssam_controller_receive_buf() and ssam_controller_write_wakeup(). Once the
- + * controller has been hooked up, transmitter and receiver threads may be
- + * started via ssam_controller_start(). These setup steps need to be completed
- + * before controller can be used for requests.
- + */
- +int ssam_controller_init(struct ssam_controller *ctrl,
- + struct serdev_device *serdev)
- +{
- + acpi_handle handle = ACPI_HANDLE(&serdev->dev);
- + int status;
- +
- + init_rwsem(&ctrl->lock);
- + kref_init(&ctrl->kref);
- +
- + status = ssam_controller_caps_load_from_acpi(handle, &ctrl->caps);
- + if (status)
- + return status;
- +
- + dev_dbg(&serdev->dev,
- + "device capabilities:\n"
- + " ssh_power_profile: %u\n"
- + " ssh_buffer_size: %u\n"
- + " screen_on_sleep_idle_timeout: %u\n"
- + " screen_off_sleep_idle_timeout: %u\n"
- + " d3_closes_handle: %u\n",
- + ctrl->caps.ssh_power_profile,
- + ctrl->caps.ssh_buffer_size,
- + ctrl->caps.screen_on_sleep_idle_timeout,
- + ctrl->caps.screen_off_sleep_idle_timeout,
- + ctrl->caps.d3_closes_handle);
- +
- + ssh_seq_reset(&ctrl->counter.seq);
- + ssh_rqid_reset(&ctrl->counter.rqid);
- +
- + /* Initialize event/request completion system. */
- + status = ssam_cplt_init(&ctrl->cplt, &serdev->dev);
- + if (status)
- + return status;
- +
- + /* Initialize request and packet transport layers. */
- + status = ssh_rtl_init(&ctrl->rtl, serdev, &ssam_rtl_ops);
- + if (status) {
- + ssam_cplt_destroy(&ctrl->cplt);
- + return status;
- + }
- +
- + /*
- + * Set state via write_once even though we expect to be in an
- + * exclusive context, due to smoke-testing in
- + * ssam_request_sync_submit().
- + */
- + WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_INITIALIZED);
- + return 0;
- +}
- +
- +/**
- + * ssam_controller_start() - Start the receiver and transmitter threads of the
- + * controller.
- + * @ctrl: The controller.
- + *
- + * Note: When this function is called, the controller should be properly
- + * hooked up to the serdev core via &struct serdev_device_ops. Please refer
- + * to ssam_controller_init() for more details on controller initialization.
- + *
- + * This function must be called with the main controller lock held (i.e. by
- + * calling ssam_controller_lock()).
- + */
- +int ssam_controller_start(struct ssam_controller *ctrl)
- +{
- + int status;
- +
- + lockdep_assert_held_write(&ctrl->lock);
- +
- + if (ctrl->state != SSAM_CONTROLLER_INITIALIZED)
- + return -EINVAL;
- +
- + status = ssh_rtl_start(&ctrl->rtl);
- + if (status)
- + return status;
- +
- + /*
- + * Set state via write_once even though we expect to be locked/in an
- + * exclusive context, due to smoke-testing in
- + * ssam_request_sync_submit().
- + */
- + WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STARTED);
- + return 0;
- +}
- +
- +/*
- + * SSAM_CTRL_SHUTDOWN_FLUSH_TIMEOUT - Timeout for flushing requests during
- + * shutdown.
- + *
- + * Chosen to be larger than one full request timeout, including packets timing
- + * out. This value should give ample time to complete any outstanding requests
- + * during normal operation and account for the odd package timeout.
- + */
- +#define SSAM_CTRL_SHUTDOWN_FLUSH_TIMEOUT msecs_to_jiffies(5000)
- +
- +/**
- + * ssam_controller_shutdown() - Shut down the controller.
- + * @ctrl: The controller.
- + *
- + * Shuts down the controller by flushing all pending requests and stopping the
- + * transmitter and receiver threads. All requests submitted after this call
- + * will fail with %-ESHUTDOWN. While it is discouraged to do so, this function
- + * is safe to use in parallel with ongoing request submission.
- + *
- + * In the course of this shutdown procedure, all currently registered
- + * notifiers will be unregistered. It is, however, strongly recommended to not
- + * rely on this behavior, and instead the party registering the notifier
- + * should unregister it before the controller gets shut down, e.g. via the
- + * SSAM bus which guarantees client devices to be removed before a shutdown.
- + *
- + * Note that events may still be pending after this call, but, due to the
- + * notifiers being unregistered, these events will be dropped when the
- + * controller is subsequently destroyed via ssam_controller_destroy().
- + *
- + * This function must be called with the main controller lock held (i.e. by
- + * calling ssam_controller_lock()).
- + */
- +void ssam_controller_shutdown(struct ssam_controller *ctrl)
- +{
- + enum ssam_controller_state s = ctrl->state;
- + int status;
- +
- + lockdep_assert_held_write(&ctrl->lock);
- +
- + if (s == SSAM_CONTROLLER_UNINITIALIZED || s == SSAM_CONTROLLER_STOPPED)
- + return;
- +
- + /*
- + * Try to flush pending events and requests while everything still
- + * works. Note: There may still be packets and/or requests in the
- + * system after this call (e.g. via control packets submitted by the
- + * packet transport layer or flush timeout / failure, ...). Those will
- + * be handled with the ssh_rtl_shutdown() call below.
- + */
- + status = ssh_rtl_flush(&ctrl->rtl, SSAM_CTRL_SHUTDOWN_FLUSH_TIMEOUT);
- + if (status) {
- + ssam_err(ctrl, "failed to flush request transport layer: %d\n",
- + status);
- + }
- +
- + /* Try to flush all currently completing requests and events. */
- + ssam_cplt_flush(&ctrl->cplt);
- +
- + /*
- + * We expect all notifiers to have been removed by the respective client
- + * driver that set them up at this point. If this warning occurs, some
- + * client driver has not done that...
- + */
- + WARN_ON(!ssam_notifier_is_empty(ctrl));
- +
- + /*
- + * Nevertheless, we should still take care of drivers that don't behave
- + * well. Thus disable all enabled events, unregister all notifiers.
- + */
- + ssam_notifier_unregister_all(ctrl);
- +
- + /*
- + * Cancel remaining requests. Ensure no new ones can be queued and stop
- + * threads.
- + */
- + ssh_rtl_shutdown(&ctrl->rtl);
- +
- + /*
- + * Set state via write_once even though we expect to be locked/in an
- + * exclusive context, due to smoke-testing in
- + * ssam_request_sync_submit().
- + */
- + WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STOPPED);
- + ctrl->rtl.ptl.serdev = NULL;
- +}
- +
- +/**
- + * ssam_controller_destroy() - Destroy the controller and free its resources.
- + * @ctrl: The controller.
- + *
- + * Ensures that all resources associated with the controller get freed. This
- + * function should only be called after the controller has been stopped via
- + * ssam_controller_shutdown(). In general, this function should not be called
- + * directly. The only valid place to call this function directly is during
- + * initialization, before the controller has been fully initialized and passed
- + * to other processes. This function is called automatically when the
- + * reference count of the controller reaches zero.
- + *
- + * This function must be called with the main controller lock held (i.e. by
- + * calling ssam_controller_lock()).
- + */
- +void ssam_controller_destroy(struct ssam_controller *ctrl)
- +{
- + lockdep_assert_held_write(&ctrl->lock);
- +
- + if (ctrl->state == SSAM_CONTROLLER_UNINITIALIZED)
- + return;
- +
- + WARN_ON(ctrl->state != SSAM_CONTROLLER_STOPPED);
- +
- + /*
- + * Note: New events could still have been received after the previous
- + * flush in ssam_controller_shutdown, before the request transport layer
- + * has been shut down. At this point, after the shutdown, we can be sure
- + * that no new events will be queued. The call to ssam_cplt_destroy will
- + * ensure that those remaining are being completed and freed.
- + */
- +
- + /* Actually free resources. */
- + ssam_cplt_destroy(&ctrl->cplt);
- + ssh_rtl_destroy(&ctrl->rtl);
- +
- + /*
- + * Set state via write_once even though we expect to be locked/in an
- + * exclusive context, due to smoke-testing in
- + * ssam_request_sync_submit().
- + */
- + WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_UNINITIALIZED);
- +}
- +
- +/**
- + * ssam_controller_suspend() - Suspend the controller.
- + * @ctrl: The controller to suspend.
- + *
- + * Marks the controller as suspended. Note that display-off and D0-exit
- + * notifications have to be sent manually before transitioning the controller
- + * into the suspended state via this function.
- + *
- + * See ssam_controller_resume() for the corresponding resume function.
- + *
- + * Return: Returns %-EINVAL if the controller is currently not in the
- + * "started" state.
- + */
- +int ssam_controller_suspend(struct ssam_controller *ctrl)
- +{
- + ssam_controller_lock(ctrl);
- +
- + if (ctrl->state != SSAM_CONTROLLER_STARTED) {
- + ssam_controller_unlock(ctrl);
- + return -EINVAL;
- + }
- +
- + ssam_dbg(ctrl, "pm: suspending controller\n");
- +
- + /*
- + * Set state via write_once even though we're locked, due to
- + * smoke-testing in ssam_request_sync_submit().
- + */
- + WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_SUSPENDED);
- +
- + ssam_controller_unlock(ctrl);
- + return 0;
- +}
- +
- +/**
- + * ssam_controller_resume() - Resume the controller from suspend.
- + * @ctrl: The controller to resume.
- + *
- + * Resume the controller from the suspended state it was put into via
- + * ssam_controller_suspend(). This function does not issue display-on and
- + * D0-entry notifications. If required, those have to be sent manually after
- + * this call.
- + *
- + * Return: Returns %-EINVAL if the controller is currently not suspended.
- + */
- +int ssam_controller_resume(struct ssam_controller *ctrl)
- +{
- + ssam_controller_lock(ctrl);
- +
- + if (ctrl->state != SSAM_CONTROLLER_SUSPENDED) {
- + ssam_controller_unlock(ctrl);
- + return -EINVAL;
- + }
- +
- + ssam_dbg(ctrl, "pm: resuming controller\n");
- +
- + /*
- + * Set state via write_once even though we're locked, due to
- + * smoke-testing in ssam_request_sync_submit().
- + */
- + WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STARTED);
- +
- + ssam_controller_unlock(ctrl);
- + return 0;
- +}
- +
- +
- +/* -- Top-level request interface ------------------------------------------- */
- +
- +/**
- + * ssam_request_write_data() - Construct and write SAM request message to
- + * buffer.
- + * @buf: The buffer to write the data to.
- + * @ctrl: The controller via which the request will be sent.
- + * @spec: The request data and specification.
- + *
- + * Constructs a SAM/SSH request message and writes it to the provided buffer.
- + * The request and transport counters, specifically RQID and SEQ, will be set
- + * in this call. These counters are obtained from the controller. It is thus
- + * only valid to send the resulting message via the controller specified here.
- + *
- + * For calculation of the required buffer size, refer to the
- + * SSH_COMMAND_MESSAGE_LENGTH() macro.
- + *
- + * Return: Returns the number of bytes used in the buffer on success. Returns
- + * %-EINVAL if the payload length provided in the request specification is too
- + * large (larger than %SSH_COMMAND_MAX_PAYLOAD_SIZE) or if the provided buffer
- + * is too small.
- + */
- +ssize_t ssam_request_write_data(struct ssam_span *buf,
- + struct ssam_controller *ctrl,
- + const struct ssam_request *spec)
- +{
- + struct msgbuf msgb;
- + u16 rqid;
- + u8 seq;
- +
- + if (spec->length > SSH_COMMAND_MAX_PAYLOAD_SIZE)
- + return -EINVAL;
- +
- + if (SSH_COMMAND_MESSAGE_LENGTH(spec->length) > buf->len)
- + return -EINVAL;
- +
- + msgb_init(&msgb, buf->ptr, buf->len);
- + seq = ssh_seq_next(&ctrl->counter.seq);
- + rqid = ssh_rqid_next(&ctrl->counter.rqid);
- + msgb_push_cmd(&msgb, seq, rqid, spec);
- +
- + return msgb_bytes_used(&msgb);
- +}
- +EXPORT_SYMBOL_GPL(ssam_request_write_data);
- +
- +static void ssam_request_sync_complete(struct ssh_request *rqst,
- + const struct ssh_command *cmd,
- + const struct ssam_span *data, int status)
- +{
- + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
- + struct ssam_request_sync *r;
- +
- + r = container_of(rqst, struct ssam_request_sync, base);
- + r->status = status;
- +
- + if (r->resp)
- + r->resp->length = 0;
- +
- + if (status) {
- + rtl_dbg_cond(rtl, "rsp: request failed: %d\n", status);
- + return;
- + }
- +
- + if (!data) /* Handle requests without a response. */
- + return;
- +
- + if (!r->resp || !r->resp->pointer) {
- + if (data->len)
- + rtl_warn(rtl, "rsp: no response buffer provided, dropping data\n");
- + return;
- + }
- +
- + if (data->len > r->resp->capacity) {
- + rtl_err(rtl,
- + "rsp: response buffer too small, capacity: %zu bytes, got: %zu bytes\n",
- + r->resp->capacity, data->len);
- + r->status = -ENOSPC;
- + return;
- + }
- +
- + r->resp->length = data->len;
- + memcpy(r->resp->pointer, data->ptr, data->len);
- +}
- +
- +static void ssam_request_sync_release(struct ssh_request *rqst)
- +{
- + complete_all(&container_of(rqst, struct ssam_request_sync, base)->comp);
- +}
- +
- +static const struct ssh_request_ops ssam_request_sync_ops = {
- + .release = ssam_request_sync_release,
- + .complete = ssam_request_sync_complete,
- +};
- +
- +/**
- + * ssam_request_sync_alloc() - Allocate a synchronous request.
- + * @payload_len: The length of the request payload.
- + * @flags: Flags used for allocation.
- + * @rqst: Where to store the pointer to the allocated request.
- + * @buffer: Where to store the buffer descriptor for the message buffer of
- + * the request.
- + *
- + * Allocates a synchronous request with corresponding message buffer. The
- + * request still needs to be initialized ssam_request_sync_init() before
- + * it can be submitted, and the message buffer data must still be set to the
- + * returned buffer via ssam_request_sync_set_data() after it has been filled,
- + * if need be with adjusted message length.
- + *
- + * After use, the request and its corresponding message buffer should be freed
- + * via ssam_request_sync_free(). The buffer must not be freed separately.
- + *
- + * Return: Returns zero on success, %-ENOMEM if the request could not be
- + * allocated.
- + */
- +int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
- + struct ssam_request_sync **rqst,
- + struct ssam_span *buffer)
- +{
- + size_t msglen = SSH_COMMAND_MESSAGE_LENGTH(payload_len);
- +
- + *rqst = kzalloc(sizeof(**rqst) + msglen, flags);
- + if (!*rqst)
- + return -ENOMEM;
- +
- + buffer->ptr = (u8 *)(*rqst + 1);
- + buffer->len = msglen;
- +
- + return 0;
- +}
- +EXPORT_SYMBOL_GPL(ssam_request_sync_alloc);
- +
- +/**
- + * ssam_request_sync_free() - Free a synchronous request.
- + * @rqst: The request to be freed.
- + *
- + * Free a synchronous request and its corresponding buffer allocated with
- + * ssam_request_sync_alloc(). Do not use for requests allocated on the stack
- + * or via any other function.
- + *
- + * Warning: The caller must ensure that the request is not in use any more.
- + * I.e. the caller must ensure that it has the only reference to the request
- + * and the request is not currently pending. This means that the caller has
- + * either never submitted the request, request submission has failed, or the
- + * caller has waited until the submitted request has been completed via
- + * ssam_request_sync_wait().
- + */
- +void ssam_request_sync_free(struct ssam_request_sync *rqst)
- +{
- + kfree(rqst);
- +}
- +EXPORT_SYMBOL_GPL(ssam_request_sync_free);
- +
- +/**
- + * ssam_request_sync_init() - Initialize a synchronous request struct.
- + * @rqst: The request to initialize.
- + * @flags: The request flags.
- + *
- + * Initializes the given request struct. Does not initialize the request
- + * message data. This has to be done explicitly after this call via
- + * ssam_request_sync_set_data() and the actual message data has to be written
- + * via ssam_request_write_data().
- + *
- + * Return: Returns zero on success or %-EINVAL if the given flags are invalid.
- + */
- +int ssam_request_sync_init(struct ssam_request_sync *rqst,
- + enum ssam_request_flags flags)
- +{
- + int status;
- +
- + status = ssh_request_init(&rqst->base, flags, &ssam_request_sync_ops);
- + if (status)
- + return status;
- +
- + init_completion(&rqst->comp);
- + rqst->resp = NULL;
- + rqst->status = 0;
- +
- + return 0;
- +}
- +EXPORT_SYMBOL_GPL(ssam_request_sync_init);
- +
- +/**
- + * ssam_request_sync_submit() - Submit a synchronous request.
- + * @ctrl: The controller with which to submit the request.
- + * @rqst: The request to submit.
- + *
- + * Submit a synchronous request. The request has to be initialized and
- + * properly set up, including response buffer (may be %NULL if no response is
- + * expected) and command message data. This function does not wait for the
- + * request to be completed.
- + *
- + * If this function succeeds, ssam_request_sync_wait() must be used to ensure
- + * that the request has been completed before the response data can be
- + * accessed and/or the request can be freed. On failure, the request may
- + * immediately be freed.
- + *
- + * This function may only be used if the controller is active, i.e. has been
- + * initialized and not suspended.
- + */
- +int ssam_request_sync_submit(struct ssam_controller *ctrl,
- + struct ssam_request_sync *rqst)
- +{
- + int status;
- +
- + /*
- + * This is only a superficial check. In general, the caller needs to
- + * ensure that the controller is initialized and is not (and does not
- + * get) suspended during use, i.e. until the request has been completed
- + * (if _absolutely_ necessary, by use of ssam_controller_statelock/
- + * ssam_controller_stateunlock, but something like ssam_client_link
- + * should be preferred as this needs to last until the request has been
- + * completed).
- + *
- + * Note that it is actually safe to use this function while the
- + * controller is in the process of being shut down (as ssh_rtl_submit
- + * is safe with regards to this), but it is generally discouraged to do
- + * so.
- + */
- + if (WARN_ON(READ_ONCE(ctrl->state) != SSAM_CONTROLLER_STARTED)) {
- + ssh_request_put(&rqst->base);
- + return -ENODEV;
- + }
- +
- + status = ssh_rtl_submit(&ctrl->rtl, &rqst->base);
- + ssh_request_put(&rqst->base);
- +
- + return status;
- +}
- +EXPORT_SYMBOL_GPL(ssam_request_sync_submit);
- +
- +/**
- + * ssam_request_sync() - Execute a synchronous request.
- + * @ctrl: The controller via which the request will be submitted.
- + * @spec: The request specification and payload.
- + * @rsp: The response buffer.
- + *
- + * Allocates a synchronous request with its message data buffer on the heap
- + * via ssam_request_sync_alloc(), fully initializes it via the provided
- + * request specification, submits it, and finally waits for its completion
- + * before freeing it and returning its status.
- + *
- + * Return: Returns the status of the request or any failure during setup.
- + */
- +int ssam_request_sync(struct ssam_controller *ctrl,
- + const struct ssam_request *spec,
- + struct ssam_response *rsp)
- +{
- + struct ssam_request_sync *rqst;
- + struct ssam_span buf;
- + ssize_t len;
- + int status;
- +
- + status = ssam_request_sync_alloc(spec->length, GFP_KERNEL, &rqst, &buf);
- + if (status)
- + return status;
- +
- + status = ssam_request_sync_init(rqst, spec->flags);
- + if (status)
- + return status;
- +
- + ssam_request_sync_set_resp(rqst, rsp);
- +
- + len = ssam_request_write_data(&buf, ctrl, spec);
- + if (len < 0) {
- + ssam_request_sync_free(rqst);
- + return len;
- + }
- +
- + ssam_request_sync_set_data(rqst, buf.ptr, len);
- +
- + status = ssam_request_sync_submit(ctrl, rqst);
- + if (!status)
- + status = ssam_request_sync_wait(rqst);
- +
- + ssam_request_sync_free(rqst);
- + return status;
- +}
- +EXPORT_SYMBOL_GPL(ssam_request_sync);
- +
- +/**
- + * ssam_request_sync_with_buffer() - Execute a synchronous request with the
- + * provided buffer as back-end for the message buffer.
- + * @ctrl: The controller via which the request will be submitted.
- + * @spec: The request specification and payload.
- + * @rsp: The response buffer.
- + * @buf: The buffer for the request message data.
- + *
- + * Allocates a synchronous request struct on the stack, fully initializes it
- + * using the provided buffer as message data buffer, submits it, and then
- + * waits for its completion before returning its status. The
- + * SSH_COMMAND_MESSAGE_LENGTH() macro can be used to compute the required
- + * message buffer size.
- + *
- + * This function does essentially the same as ssam_request_sync(), but instead
- + * of dynamically allocating the request and message data buffer, it uses the
- + * provided message data buffer and stores the (small) request struct on the
- + * heap.
- + *
- + * Return: Returns the status of the request or any failure during setup.
- + */
- +int ssam_request_sync_with_buffer(struct ssam_controller *ctrl,
- + const struct ssam_request *spec,
- + struct ssam_response *rsp,
- + struct ssam_span *buf)
- +{
- + struct ssam_request_sync rqst;
- + ssize_t len;
- + int status;
- +
- + status = ssam_request_sync_init(&rqst, spec->flags);
- + if (status)
- + return status;
- +
- + ssam_request_sync_set_resp(&rqst, rsp);
- +
- + len = ssam_request_write_data(buf, ctrl, spec);
- + if (len < 0)
- + return len;
- +
- + ssam_request_sync_set_data(&rqst, buf->ptr, len);
- +
- + status = ssam_request_sync_submit(ctrl, &rqst);
- + if (!status)
- + status = ssam_request_sync_wait(&rqst);
- +
- + return status;
- +}
- +EXPORT_SYMBOL_GPL(ssam_request_sync_with_buffer);
- +
- +
- +/* -- Internal SAM requests. ------------------------------------------------ */
- +
- +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_get_firmware_version, __le32, {
- + .target_category = SSAM_SSH_TC_SAM,
- + .target_id = 0x01,
- + .command_id = 0x13,
- + .instance_id = 0x00,
- +});
- +
- +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_off, u8, {
- + .target_category = SSAM_SSH_TC_SAM,
- + .target_id = 0x01,
- + .command_id = 0x15,
- + .instance_id = 0x00,
- +});
- +
- +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_on, u8, {
- + .target_category = SSAM_SSH_TC_SAM,
- + .target_id = 0x01,
- + .command_id = 0x16,
- + .instance_id = 0x00,
- +});
- +
- +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_exit, u8, {
- + .target_category = SSAM_SSH_TC_SAM,
- + .target_id = 0x01,
- + .command_id = 0x33,
- + .instance_id = 0x00,
- +});
- +
- +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_entry, u8, {
- + .target_category = SSAM_SSH_TC_SAM,
- + .target_id = 0x01,
- + .command_id = 0x34,
- + .instance_id = 0x00,
- +});
- +
- +/**
- + * struct ssh_notification_params - Command payload to enable/disable SSH
- + * notifications.
- + * @target_category: The target category for which notifications should be
- + * enabled/disabled.
- + * @flags: Flags determining how notifications are being sent.
- + * @request_id: The request ID that is used to send these notifications.
- + * @instance_id: The specific instance in the given target category for
- + * which notifications should be enabled.
- + */
- +struct ssh_notification_params {
- + u8 target_category;
- + u8 flags;
- + __le16 request_id;
- + u8 instance_id;
- +} __packed;
- +
- +static_assert(sizeof(struct ssh_notification_params) == 5);
- +
- +static int __ssam_ssh_event_request(struct ssam_controller *ctrl,
- + struct ssam_event_registry reg, u8 cid,
- + struct ssam_event_id id, u8 flags)
- +{
- + struct ssh_notification_params params;
- + struct ssam_request rqst;
- + struct ssam_response result;
- + int status;
- +
- + u16 rqid = ssh_tc_to_rqid(id.target_category);
- + u8 buf = 0;
- +
- + /* Only allow RQIDs that lie within the event spectrum. */
- + if (!ssh_rqid_is_event(rqid))
- + return -EINVAL;
- +
- + params.target_category = id.target_category;
- + params.instance_id = id.instance;
- + params.flags = flags;
- + put_unaligned_le16(rqid, ¶ms.request_id);
- +
- + rqst.target_category = reg.target_category;
- + rqst.target_id = reg.target_id;
- + rqst.command_id = cid;
- + rqst.instance_id = 0x00;
- + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
- + rqst.length = sizeof(params);
- + rqst.payload = (u8 *)¶ms;
- +
- + result.capacity = sizeof(buf);
- + result.length = 0;
- + result.pointer = &buf;
- +
- + status = ssam_retry(ssam_request_sync_onstack, ctrl, &rqst, &result,
- + sizeof(params));
- +
- + return status < 0 ? status : buf;
- +}
- +
- +/**
- + * ssam_ssh_event_enable() - Enable SSH event.
- + * @ctrl: The controller for which to enable the event.
- + * @reg: The event registry describing what request to use for enabling and
- + * disabling the event.
- + * @id: The event identifier.
- + * @flags: The event flags.
- + *
- + * Enables the specified event on the EC. This function does not manage
- + * reference counting of enabled events and is basically only a wrapper for
- + * the raw EC request. If the specified event is already enabled, the EC will
- + * ignore this request.
- + *
- + * Return: Returns the status of the executed SAM request (zero on success and
- + * negative on direct failure) or %-EPROTO if the request response indicates a
- + * failure.
- + */
- +static int ssam_ssh_event_enable(struct ssam_controller *ctrl,
- + struct ssam_event_registry reg,
- + struct ssam_event_id id, u8 flags)
- +{
- + int status;
- +
- + status = __ssam_ssh_event_request(ctrl, reg, reg.cid_enable, id, flags);
- +
- + if (status < 0 && status != -EINVAL) {
- + ssam_err(ctrl,
- + "failed to enable event source (tc: %#04x, iid: %#04x, reg: %#04x)\n",
- + id.target_category, id.instance, reg.target_category);
- + }
- +
- + if (status > 0) {
- + ssam_err(ctrl,
- + "unexpected result while enabling event source: %#04x (tc: %#04x, iid: %#04x, reg: %#04x)\n",
- + status, id.target_category, id.instance, reg.target_category);
- + return -EPROTO;
- + }
- +
- + return status;
- +}
- +
- +/**
- + * ssam_ssh_event_disable() - Disable SSH event.
- + * @ctrl: The controller for which to disable the event.
- + * @reg: The event registry describing what request to use for enabling and
- + * disabling the event (must be same as used when enabling the event).
- + * @id: The event identifier.
- + * @flags: The event flags (likely ignored for disabling of events).
- + *
- + * Disables the specified event on the EC. This function does not manage
- + * reference counting of enabled events and is basically only a wrapper for
- + * the raw EC request. If the specified event is already disabled, the EC will
- + * ignore this request.
- + *
- + * Return: Returns the status of the executed SAM request (zero on success and
- + * negative on direct failure) or %-EPROTO if the request response indicates a
- + * failure.
- + */
- +static int ssam_ssh_event_disable(struct ssam_controller *ctrl,
- + struct ssam_event_registry reg,
- + struct ssam_event_id id, u8 flags)
- +{
- + int status;
- +
- + status = __ssam_ssh_event_request(ctrl, reg, reg.cid_enable, id, flags);
- +
- + if (status < 0 && status != -EINVAL) {
- + ssam_err(ctrl,
- + "failed to disable event source (tc: %#04x, iid: %#04x, reg: %#04x)\n",
- + id.target_category, id.instance, reg.target_category);
- + }
- +
- + if (status > 0) {
- + ssam_err(ctrl,
- + "unexpected result while disabling event source: %#04x (tc: %#04x, iid: %#04x, reg: %#04x)\n",
- + status, id.target_category, id.instance, reg.target_category);
- + return -EPROTO;
- + }
- +
- + return status;
- +}
- +
- +
- +/* -- Wrappers for internal SAM requests. ----------------------------------- */
- +
- +/**
- + * ssam_get_firmware_version() - Get the SAM/EC firmware version.
- + * @ctrl: The controller.
- + * @version: Where to store the version number.
- + *
- + * Return: Returns zero on success or the status of the executed SAM request
- + * if that request failed.
- + */
- +int ssam_get_firmware_version(struct ssam_controller *ctrl, u32 *version)
- +{
- + __le32 __version;
- + int status;
- +
- + status = ssam_retry(ssam_ssh_get_firmware_version, ctrl, &__version);
- + if (status)
- + return status;
- +
- + *version = le32_to_cpu(__version);
- + return 0;
- +}
- +
- +/**
- + * ssam_ctrl_notif_display_off() - Notify EC that the display has been turned
- + * off.
- + * @ctrl: The controller.
- + *
- + * Notify the EC that the display has been turned off and the driver may enter
- + * a lower-power state. This will prevent events from being sent directly.
- + * Rather, the EC signals an event by pulling the wakeup GPIO high for as long
- + * as there are pending events. The events then need to be manually released,
- + * one by one, via the GPIO callback request. All pending events accumulated
- + * during this state can also be released by issuing the display-on
- + * notification, e.g. via ssam_ctrl_notif_display_on(), which will also reset
- + * the GPIO.
- + *
- + * On some devices, specifically ones with an integrated keyboard, the keyboard
- + * backlight will be turned off by this call.
- + *
- + * This function will only send the display-off notification command if
- + * display notifications are supported by the EC. Currently all known devices
- + * support these notifications.
- + *
- + * Use ssam_ctrl_notif_display_on() to reverse the effects of this function.
- + *
- + * Return: Returns zero on success or if no request has been executed, the
- + * status of the executed SAM request if that request failed, or %-EPROTO if
- + * an unexpected response has been received.
- + */
- +int ssam_ctrl_notif_display_off(struct ssam_controller *ctrl)
- +{
- + int status;
- + u8 response;
- +
- + ssam_dbg(ctrl, "pm: notifying display off\n");
- +
- + status = ssam_retry(ssam_ssh_notif_display_off, ctrl, &response);
- + if (status)
- + return status;
- +
- + if (response != 0) {
- + ssam_err(ctrl, "unexpected response from display-off notification: %#04x\n",
- + response);
- + return -EPROTO;
- + }
- +
- + return 0;
- +}
- +
- +/**
- + * ssam_ctrl_notif_display_on() - Notify EC that the display has been turned on.
- + * @ctrl: The controller.
- + *
- + * Notify the EC that the display has been turned back on and the driver has
- + * exited its lower-power state. This notification is the counterpart to the
- + * display-off notification sent via ssam_ctrl_notif_display_off() and will
- + * reverse its effects, including resetting events to their default behavior.
- + *
- + * This function will only send the display-on notification command if display
- + * notifications are supported by the EC. Currently all known devices support
- + * these notifications.
- + *
- + * See ssam_ctrl_notif_display_off() for more details.
- + *
- + * Return: Returns zero on success or if no request has been executed, the
- + * status of the executed SAM request if that request failed, or %-EPROTO if
- + * an unexpected response has been received.
- + */
- +int ssam_ctrl_notif_display_on(struct ssam_controller *ctrl)
- +{
- + int status;
- + u8 response;
- +
- + ssam_dbg(ctrl, "pm: notifying display on\n");
- +
- + status = ssam_retry(ssam_ssh_notif_display_on, ctrl, &response);
- + if (status)
- + return status;
- +
- + if (response != 0) {
- + ssam_err(ctrl, "unexpected response from display-on notification: %#04x\n",
- + response);
- + return -EPROTO;
- + }
- +
- + return 0;
- +}
- +
- +/**
- + * ssam_ctrl_notif_d0_exit() - Notify EC that the driver/device exits the D0
- + * power state.
- + * @ctrl: The controller
- + *
- + * Notifies the EC that the driver prepares to exit the D0 power state in
- + * favor of a lower-power state. Exact effects of this function related to the
- + * EC are currently unknown.
- + *
- + * This function will only send the D0-exit notification command if D0-state
- + * notifications are supported by the EC. Only newer Surface generations
- + * support these notifications.
- + *
- + * Use ssam_ctrl_notif_d0_entry() to reverse the effects of this function.
- + *
- + * Return: Returns zero on success or if no request has been executed, the
- + * status of the executed SAM request if that request failed, or %-EPROTO if
- + * an unexpected response has been received.
- + */
- +int ssam_ctrl_notif_d0_exit(struct ssam_controller *ctrl)
- +{
- + int status;
- + u8 response;
- +
- + if (!ctrl->caps.d3_closes_handle)
- + return 0;
- +
- + ssam_dbg(ctrl, "pm: notifying D0 exit\n");
- +
- + status = ssam_retry(ssam_ssh_notif_d0_exit, ctrl, &response);
- + if (status)
- + return status;
- +
- + if (response != 0) {
- + ssam_err(ctrl, "unexpected response from D0-exit notification: %#04x\n",
- + response);
- + return -EPROTO;
- + }
- +
- + return 0;
- +}
- +
- +/**
- + * ssam_ctrl_notif_d0_entry() - Notify EC that the driver/device enters the D0
- + * power state.
- + * @ctrl: The controller
- + *
- + * Notifies the EC that the driver has exited a lower-power state and entered
- + * the D0 power state. Exact effects of this function related to the EC are
- + * currently unknown.
- + *
- + * This function will only send the D0-entry notification command if D0-state
- + * notifications are supported by the EC. Only newer Surface generations
- + * support these notifications.
- + *
- + * See ssam_ctrl_notif_d0_exit() for more details.
- + *
- + * Return: Returns zero on success or if no request has been executed, the
- + * status of the executed SAM request if that request failed, or %-EPROTO if
- + * an unexpected response has been received.
- + */
- +int ssam_ctrl_notif_d0_entry(struct ssam_controller *ctrl)
- +{
- + int status;
- + u8 response;
- +
- + if (!ctrl->caps.d3_closes_handle)
- + return 0;
- +
- + ssam_dbg(ctrl, "pm: notifying D0 entry\n");
- +
- + status = ssam_retry(ssam_ssh_notif_d0_entry, ctrl, &response);
- + if (status)
- + return status;
- +
- + if (response != 0) {
- + ssam_err(ctrl, "unexpected response from D0-entry notification: %#04x\n",
- + response);
- + return -EPROTO;
- + }
- +
- + return 0;
- +}
- +
- +
- +/* -- Top-level event registry interface. ----------------------------------- */
- +
- +/**
- + * ssam_notifier_register() - Register an event notifier.
- + * @ctrl: The controller to register the notifier on.
- + * @n: The event notifier to register.
- + *
- + * Register an event notifier and increment the usage counter of the
- + * associated SAM event. If the event was previously not enabled, it will be
- + * enabled during this call.
- + *
- + * Return: Returns zero on success, %-ENOSPC if there have already been
- + * %INT_MAX notifiers for the event ID/type associated with the notifier block
- + * registered, %-ENOMEM if the corresponding event entry could not be
- + * allocated. If this is the first time that a notifier block is registered
- + * for the specific associated event, returns the status of the event-enable
- + * EC-command.
- + */
- +int ssam_notifier_register(struct ssam_controller *ctrl,
- + struct ssam_event_notifier *n)
- +{
- + u16 rqid = ssh_tc_to_rqid(n->event.id.target_category);
- + struct ssam_nf_refcount_entry *entry;
- + struct ssam_nf_head *nf_head;
- + struct ssam_nf *nf;
- + int status;
- +
- + if (!ssh_rqid_is_event(rqid))
- + return -EINVAL;
- +
- + nf = &ctrl->cplt.event.notif;
- + nf_head = &nf->head[ssh_rqid_to_event(rqid)];
- +
- + mutex_lock(&nf->lock);
- +
- + entry = ssam_nf_refcount_inc(nf, n->event.reg, n->event.id);
- + if (IS_ERR(entry)) {
- + mutex_unlock(&nf->lock);
- + return PTR_ERR(entry);
- + }
- +
- + ssam_dbg(ctrl, "enabling event (reg: %#04x, tc: %#04x, iid: %#04x, rc: %d)\n",
- + n->event.reg.target_category, n->event.id.target_category,
- + n->event.id.instance, entry->refcount);
- +
- + status = ssam_nfblk_insert(nf_head, &n->base);
- + if (status) {
- + entry = ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
- + if (entry->refcount == 0)
- + kfree(entry);
- +
- + mutex_unlock(&nf->lock);
- + return status;
- + }
- +
- + if (entry->refcount == 1) {
- + status = ssam_ssh_event_enable(ctrl, n->event.reg, n->event.id,
- + n->event.flags);
- + if (status) {
- + ssam_nfblk_remove(&n->base);
- + kfree(ssam_nf_refcount_dec(nf, n->event.reg, n->event.id));
- + mutex_unlock(&nf->lock);
- + synchronize_srcu(&nf_head->srcu);
- + return status;
- + }
- +
- + entry->flags = n->event.flags;
- +
- + } else if (entry->flags != n->event.flags) {
- + ssam_warn(ctrl,
- + "inconsistent flags when enabling event: got %#04x, expected %#04x (reg: %#04x, tc: %#04x, iid: %#04x)\n",
- + n->event.flags, entry->flags, n->event.reg.target_category,
- + n->event.id.target_category, n->event.id.instance);
- + }
- +
- + mutex_unlock(&nf->lock);
- + return 0;
- +}
- +EXPORT_SYMBOL_GPL(ssam_notifier_register);
- +
- +/**
- + * ssam_notifier_unregister() - Unregister an event notifier.
- + * @ctrl: The controller the notifier has been registered on.
- + * @n: The event notifier to unregister.
- + *
- + * Unregister an event notifier and decrement the usage counter of the
- + * associated SAM event. If the usage counter reaches zero, the event will be
- + * disabled.
- + *
- + * Return: Returns zero on success, %-ENOENT if the given notifier block has
- + * not been registered on the controller. If the given notifier block was the
- + * last one associated with its specific event, returns the status of the
- + * event-disable EC-command.
- + */
- +int ssam_notifier_unregister(struct ssam_controller *ctrl,
- + struct ssam_event_notifier *n)
- +{
- + u16 rqid = ssh_tc_to_rqid(n->event.id.target_category);
- + struct ssam_nf_refcount_entry *entry;
- + struct ssam_nf_head *nf_head;
- + struct ssam_nf *nf;
- + int status = 0;
- +
- + if (!ssh_rqid_is_event(rqid))
- + return -EINVAL;
- +
- + nf = &ctrl->cplt.event.notif;
- + nf_head = &nf->head[ssh_rqid_to_event(rqid)];
- +
- + mutex_lock(&nf->lock);
- +
- + if (!ssam_nfblk_find(nf_head, &n->base)) {
- + mutex_unlock(&nf->lock);
- + return -ENOENT;
- + }
- +
- + entry = ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
- + if (WARN_ON(!entry)) {
- + /*
- + * If this does not return an entry, there's a logic error
- + * somewhere: The notifier block is registered, but the event
- + * refcount entry is not there. Remove the notifier block
- + * anyways.
- + */
- + status = -ENOENT;
- + goto remove;
- + }
- +
- + ssam_dbg(ctrl, "disabling event (reg: %#04x, tc: %#04x, iid: %#04x, rc: %d)\n",
- + n->event.reg.target_category, n->event.id.target_category,
- + n->event.id.instance, entry->refcount);
- +
- + if (entry->flags != n->event.flags) {
- + ssam_warn(ctrl,
- + "inconsistent flags when disabling event: got %#04x, expected %#04x (reg: %#04x, tc: %#04x, iid: %#04x)\n",
- + n->event.flags, entry->flags, n->event.reg.target_category,
- + n->event.id.target_category, n->event.id.instance);
- + }
- +
- + if (entry->refcount == 0) {
- + status = ssam_ssh_event_disable(ctrl, n->event.reg, n->event.id,
- + n->event.flags);
- + kfree(entry);
- + }
- +
- +remove:
- + ssam_nfblk_remove(&n->base);
- + mutex_unlock(&nf->lock);
- + synchronize_srcu(&nf_head->srcu);
- +
- + return status;
- +}
- +EXPORT_SYMBOL_GPL(ssam_notifier_unregister);
- +
- +/**
- + * ssam_notifier_disable_registered() - Disable events for all registered
- + * notifiers.
- + * @ctrl: The controller for which to disable the notifiers/events.
- + *
- + * Disables events for all currently registered notifiers. In case of an error
- + * (EC command failing), all previously disabled events will be restored and
- + * the error code returned.
- + *
- + * This function is intended to disable all events prior to hibernation entry.
- + * See ssam_notifier_restore_registered() to restore/re-enable all events
- + * disabled with this function.
- + *
- + * Note that this function will not disable events for notifiers registered
- + * after calling this function. It should thus be made sure that no new
- + * notifiers are going to be added after this call and before the corresponding
- + * call to ssam_notifier_restore_registered().
- + *
- + * Return: Returns zero on success. In case of failure returns the error code
- + * returned by the failed EC command to disable an event.
- + */
- +int ssam_notifier_disable_registered(struct ssam_controller *ctrl)
- +{
- + struct ssam_nf *nf = &ctrl->cplt.event.notif;
- + struct rb_node *n;
- + int status;
- +
- + mutex_lock(&nf->lock);
- + for (n = rb_first(&nf->refcount); n; n = rb_next(n)) {
- + struct ssam_nf_refcount_entry *e;
- +
- + e = rb_entry(n, struct ssam_nf_refcount_entry, node);
- + status = ssam_ssh_event_disable(ctrl, e->key.reg,
- + e->key.id, e->flags);
- + if (status)
- + goto err;
- + }
- + mutex_unlock(&nf->lock);
- +
- + return 0;
- +
- +err:
- + for (n = rb_prev(n); n; n = rb_prev(n)) {
- + struct ssam_nf_refcount_entry *e;
- +
- + e = rb_entry(n, struct ssam_nf_refcount_entry, node);
- + ssam_ssh_event_enable(ctrl, e->key.reg, e->key.id, e->flags);
- + }
- + mutex_unlock(&nf->lock);
- +
- + return status;
- +}
- +
- +/**
- + * ssam_notifier_restore_registered() - Restore/re-enable events for all
- + * registered notifiers.
- + * @ctrl: The controller for which to restore the notifiers/events.
- + *
- + * Restores/re-enables all events for which notifiers have been registered on
- + * the given controller. In case of a failure, the error is logged and the
- + * function continues to try and enable the remaining events.
- + *
- + * This function is intended to restore/re-enable all registered events after
- + * hibernation. See ssam_notifier_disable_registered() for the counter part
- + * disabling the events and more details.
- + */
- +void ssam_notifier_restore_registered(struct ssam_controller *ctrl)
- +{
- + struct ssam_nf *nf = &ctrl->cplt.event.notif;
- + struct rb_node *n;
- +
- + mutex_lock(&nf->lock);
- + for (n = rb_first(&nf->refcount); n; n = rb_next(n)) {
- + struct ssam_nf_refcount_entry *e;
- +
- + e = rb_entry(n, struct ssam_nf_refcount_entry, node);
- +
- + /* Ignore errors, will get logged in call. */
- + ssam_ssh_event_enable(ctrl, e->key.reg, e->key.id, e->flags);
- + }
- + mutex_unlock(&nf->lock);
- +}
- +
- +/**
- + * ssam_notifier_is_empty() - Check if there are any registered notifiers.
- + * @ctrl: The controller to check on.
- + *
- + * Return: Returns %true if there are currently no notifiers registered on the
- + * controller, %false otherwise.
- + */
- +static bool ssam_notifier_is_empty(struct ssam_controller *ctrl)
- +{
- + struct ssam_nf *nf = &ctrl->cplt.event.notif;
- + bool result;
- +
- + mutex_lock(&nf->lock);
- + result = ssam_nf_refcount_empty(nf);
- + mutex_unlock(&nf->lock);
- +
- + return result;
- +}
- +
- +/**
- + * ssam_notifier_unregister_all() - Unregister all currently registered
- + * notifiers.
- + * @ctrl: The controller to unregister the notifiers on.
- + *
- + * Unregisters all currently registered notifiers. This function is used to
- + * ensure that all notifiers will be unregistered and associated
- + * entries/resources freed when the controller is being shut down.
- + */
- +static void ssam_notifier_unregister_all(struct ssam_controller *ctrl)
- +{
- + struct ssam_nf *nf = &ctrl->cplt.event.notif;
- + struct ssam_nf_refcount_entry *e, *n;
- +
- + mutex_lock(&nf->lock);
- + rbtree_postorder_for_each_entry_safe(e, n, &nf->refcount, node) {
- + /* Ignore errors, will get logged in call. */
- + ssam_ssh_event_disable(ctrl, e->key.reg, e->key.id, e->flags);
- + kfree(e);
- + }
- + nf->refcount = RB_ROOT;
- + mutex_unlock(&nf->lock);
- +}
- +
- +
- +/* -- Wakeup IRQ. ----------------------------------------------------------- */
- +
- +static irqreturn_t ssam_irq_handle(int irq, void *dev_id)
- +{
- + struct ssam_controller *ctrl = dev_id;
- +
- + ssam_dbg(ctrl, "pm: wake irq triggered\n");
- +
- + /*
- + * Note: Proper wakeup detection is currently unimplemented.
- + * When the EC is in display-off or any other non-D0 state, it
- + * does not send events/notifications to the host. Instead it
- + * signals that there are events available via the wakeup IRQ.
- + * This driver is responsible for calling back to the EC to
- + * release these events one-by-one.
- + *
- + * This IRQ should not cause a full system resume by its own.
- + * Instead, events should be handled by their respective subsystem
- + * drivers, which in turn should signal whether a full system
- + * resume should be performed.
- + *
- + * TODO: Send GPIO callback command repeatedly to EC until callback
- + * returns 0x00. Return flag of callback is "has more events".
- + * Each time the command is sent, one event is "released". Once
- + * all events have been released (return = 0x00), the GPIO is
- + * re-armed. Detect wakeup events during this process, go back to
- + * sleep if no wakeup event has been received.
- + */
- +
- + return IRQ_HANDLED;
- +}
- +
- +/**
- + * ssam_irq_setup() - Set up SAM EC wakeup-GPIO interrupt.
- + * @ctrl: The controller for which the IRQ should be set up.
- + *
- + * Set up an IRQ for the wakeup-GPIO pin of the SAM EC. This IRQ can be used
- + * to wake the device from a low power state.
- + *
- + * Note that this IRQ can only be triggered while the EC is in the display-off
- + * state. In this state, events are not sent to the host in the usual way.
- + * Instead the wakeup-GPIO gets pulled to "high" as long as there are pending
- + * events and these events need to be released one-by-one via the GPIO
- + * callback request, either until there are no events left and the GPIO is
- + * reset, or all at once by transitioning the EC out of the display-off state,
- + * which will also clear the GPIO.
- + *
- + * Not all events, however, should trigger a full system wakeup. Instead the
- + * driver should, if necessary, inspect and forward each event to the
- + * corresponding subsystem, which in turn should decide if the system needs to
- + * be woken up. This logic has not been implemented yet, thus wakeup by this
- + * IRQ should be disabled by default to avoid spurious wake-ups, caused, for
- + * example, by the remaining battery percentage changing. Refer to comments in
- + * this function and comments in the corresponding IRQ handler for more
- + * details on how this should be implemented.
- + *
- + * See also ssam_ctrl_notif_display_off() and ssam_ctrl_notif_display_off()
- + * for functions to transition the EC into and out of the display-off state as
- + * well as more details on it.
- + *
- + * The IRQ is disabled by default and has to be enabled before it can wake up
- + * the device from suspend via ssam_irq_arm_for_wakeup(). On teardown, the IRQ
- + * should be freed via ssam_irq_free().
- + */
- +int ssam_irq_setup(struct ssam_controller *ctrl)
- +{
- + struct device *dev = ssam_controller_device(ctrl);
- + struct gpio_desc *gpiod;
- + int irq;
- + int status;
- +
- + /*
- + * The actual GPIO interrupt is declared in ACPI as TRIGGER_HIGH.
- + * However, the GPIO line only gets reset by sending the GPIO callback
- + * command to SAM (or alternatively the display-on notification). As
- + * proper handling for this interrupt is not implemented yet, leaving
- + * the IRQ at TRIGGER_HIGH would cause an IRQ storm (as the callback
- + * never gets sent and thus the line never gets reset). To avoid this,
- + * mark the IRQ as TRIGGER_RISING for now, only creating a single
- + * interrupt, and let the SAM resume callback during the controller
- + * resume process clear it.
- + */
- + const int irqf = IRQF_SHARED | IRQF_ONESHOT | IRQF_TRIGGER_RISING;
- +
- + gpiod = gpiod_get(dev, "ssam_wakeup-int", GPIOD_ASIS);
- + if (IS_ERR(gpiod))
- + return PTR_ERR(gpiod);
- +
- + irq = gpiod_to_irq(gpiod);
- + gpiod_put(gpiod);
- +
- + if (irq < 0)
- + return irq;
- +
- + status = request_threaded_irq(irq, NULL, ssam_irq_handle, irqf,
- + "ssam_wakeup", ctrl);
- + if (status)
- + return status;
- +
- + ctrl->irq.num = irq;
- + disable_irq(ctrl->irq.num);
- + return 0;
- +}
- +
- +/**
- + * ssam_irq_free() - Free SAM EC wakeup-GPIO interrupt.
- + * @ctrl: The controller for which the IRQ should be freed.
- + *
- + * Free the wakeup-GPIO IRQ previously set-up via ssam_irq_setup().
- + */
- +void ssam_irq_free(struct ssam_controller *ctrl)
- +{
- + free_irq(ctrl->irq.num, ctrl);
- + ctrl->irq.num = -1;
- +}
- +
- +/**
- + * ssam_irq_arm_for_wakeup() - Arm the EC IRQ for wakeup, if enabled.
- + * @ctrl: The controller for which the IRQ should be armed.
- + *
- + * Sets up the IRQ so that it can be used to wake the device. Specifically,
- + * this function enables the irq and then, if the device is allowed to wake up
- + * the system, calls enable_irq_wake(). See ssam_irq_disarm_wakeup() for the
- + * corresponding function to disable the IRQ.
- + *
- + * This function is intended to arm the IRQ before entering S2idle suspend.
- + *
- + * Note: calls to ssam_irq_arm_for_wakeup() and ssam_irq_disarm_wakeup() must
- + * be balanced.
- + */
- +int ssam_irq_arm_for_wakeup(struct ssam_controller *ctrl)
- +{
- + struct device *dev = ssam_controller_device(ctrl);
- + int status;
- +
- + enable_irq(ctrl->irq.num);
- + if (device_may_wakeup(dev)) {
- + status = enable_irq_wake(ctrl->irq.num);
- + if (status) {
- + ssam_err(ctrl, "failed to enable wake IRQ: %d\n", status);
- + disable_irq(ctrl->irq.num);
- + return status;
- + }
- +
- + ctrl->irq.wakeup_enabled = true;
- + } else {
- + ctrl->irq.wakeup_enabled = false;
- + }
- +
- + return 0;
- +}
- +
- +/**
- + * ssam_irq_disarm_wakeup() - Disarm the wakeup IRQ.
- + * @ctrl: The controller for which the IRQ should be disarmed.
- + *
- + * Disarm the IRQ previously set up for wake via ssam_irq_arm_for_wakeup().
- + *
- + * This function is intended to disarm the IRQ after exiting S2idle suspend.
- + *
- + * Note: calls to ssam_irq_arm_for_wakeup() and ssam_irq_disarm_wakeup() must
- + * be balanced.
- + */
- +void ssam_irq_disarm_wakeup(struct ssam_controller *ctrl)
- +{
- + int status;
- +
- + if (ctrl->irq.wakeup_enabled) {
- + status = disable_irq_wake(ctrl->irq.num);
- + if (status)
- + ssam_err(ctrl, "failed to disable wake IRQ: %d\n", status);
- +
- + ctrl->irq.wakeup_enabled = false;
- + }
- + disable_irq(ctrl->irq.num);
- +}
- diff --git a/drivers/platform/surface/aggregator/controller.h b/drivers/platform/surface/aggregator/controller.h
- new file mode 100644
- index 000000000000..5ee9e966f1d7
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/controller.h
- @@ -0,0 +1,276 @@
- +/* SPDX-License-Identifier: GPL-2.0+ */
- +/*
- + * Main SSAM/SSH controller structure and functionality.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#ifndef _SURFACE_AGGREGATOR_CONTROLLER_H
- +#define _SURFACE_AGGREGATOR_CONTROLLER_H
- +
- +#include <linux/kref.h>
- +#include <linux/list.h>
- +#include <linux/mutex.h>
- +#include <linux/rbtree.h>
- +#include <linux/rwsem.h>
- +#include <linux/serdev.h>
- +#include <linux/spinlock.h>
- +#include <linux/srcu.h>
- +#include <linux/types.h>
- +#include <linux/workqueue.h>
- +
- +#include <linux/surface_aggregator/controller.h>
- +#include <linux/surface_aggregator/serial_hub.h>
- +
- +#include "ssh_request_layer.h"
- +
- +
- +/* -- Safe counters. -------------------------------------------------------- */
- +
- +/**
- + * struct ssh_seq_counter - Safe counter for SSH sequence IDs.
- + * @value: The current counter value.
- + */
- +struct ssh_seq_counter {
- + u8 value;
- +};
- +
- +/**
- + * struct ssh_rqid_counter - Safe counter for SSH request IDs.
- + * @value: The current counter value.
- + */
- +struct ssh_rqid_counter {
- + u16 value;
- +};
- +
- +
- +/* -- Event/notification system. -------------------------------------------- */
- +
- +/**
- + * struct ssam_nf_head - Notifier head for SSAM events.
- + * @srcu: The SRCU struct for synchronization.
- + * @head: List-head for notifier blocks registered under this head.
- + */
- +struct ssam_nf_head {
- + struct srcu_struct srcu;
- + struct list_head head;
- +};
- +
- +/**
- + * struct ssam_nf - Notifier callback- and activation-registry for SSAM events.
- + * @lock: Lock guarding (de-)registration of notifier blocks. Note: This
- + * lock does not need to be held for notifier calls, only
- + * registration and deregistration.
- + * @refcount: The root of the RB-tree used for reference-counting enabled
- + * events/notifications.
- + * @head: The list of notifier heads for event/notification callbacks.
- + */
- +struct ssam_nf {
- + struct mutex lock;
- + struct rb_root refcount;
- + struct ssam_nf_head head[SSH_NUM_EVENTS];
- +};
- +
- +
- +/* -- Event/async request completion system. -------------------------------- */
- +
- +struct ssam_cplt;
- +
- +/**
- + * struct ssam_event_item - Struct for event queuing and completion.
- + * @node: The node in the queue.
- + * @rqid: The request ID of the event.
- + * @event: Actual event data.
- + */
- +struct ssam_event_item {
- + struct list_head node;
- + u16 rqid;
- +
- + struct ssam_event event; /* must be last */
- +};
- +
- +/**
- + * struct ssam_event_queue - Queue for completing received events.
- + * @cplt: Reference to the completion system on which this queue is active.
- + * @lock: The lock for any operation on the queue.
- + * @head: The list-head of the queue.
- + * @work: The &struct work_struct performing completion work for this queue.
- + */
- +struct ssam_event_queue {
- + struct ssam_cplt *cplt;
- +
- + spinlock_t lock;
- + struct list_head head;
- + struct work_struct work;
- +};
- +
- +/**
- + * struct ssam_event_target - Set of queues for a single SSH target ID.
- + * @queue: The array of queues, one queue per event ID.
- + */
- +struct ssam_event_target {
- + struct ssam_event_queue queue[SSH_NUM_EVENTS];
- +};
- +
- +/**
- + * struct ssam_cplt - SSAM event/async request completion system.
- + * @dev: The device with which this system is associated. Only used
- + * for logging.
- + * @wq: The &struct workqueue_struct on which all completion work
- + * items are queued.
- + * @event: Event completion management.
- + * @event.target: Array of &struct ssam_event_target, one for each target.
- + * @event.notif: Notifier callbacks and event activation reference counting.
- + */
- +struct ssam_cplt {
- + struct device *dev;
- + struct workqueue_struct *wq;
- +
- + struct {
- + struct ssam_event_target target[SSH_NUM_TARGETS];
- + struct ssam_nf notif;
- + } event;
- +};
- +
- +
- +/* -- Main SSAM device structures. ------------------------------------------ */
- +
- +/**
- + * enum ssam_controller_state - State values for &struct ssam_controller.
- + * @SSAM_CONTROLLER_UNINITIALIZED:
- + * The controller has not been initialized yet or has been deinitialized.
- + * @SSAM_CONTROLLER_INITIALIZED:
- + * The controller is initialized, but has not been started yet.
- + * @SSAM_CONTROLLER_STARTED:
- + * The controller has been started and is ready to use.
- + * @SSAM_CONTROLLER_STOPPED:
- + * The controller has been stopped.
- + * @SSAM_CONTROLLER_SUSPENDED:
- + * The controller has been suspended.
- + */
- +enum ssam_controller_state {
- + SSAM_CONTROLLER_UNINITIALIZED,
- + SSAM_CONTROLLER_INITIALIZED,
- + SSAM_CONTROLLER_STARTED,
- + SSAM_CONTROLLER_STOPPED,
- + SSAM_CONTROLLER_SUSPENDED,
- +};
- +
- +/**
- + * struct ssam_controller_caps - Controller device capabilities.
- + * @ssh_power_profile: SSH power profile.
- + * @ssh_buffer_size: SSH driver UART buffer size.
- + * @screen_on_sleep_idle_timeout: SAM UART screen-on sleep idle timeout.
- + * @screen_off_sleep_idle_timeout: SAM UART screen-off sleep idle timeout.
- + * @d3_closes_handle: SAM closes UART handle in D3.
- + *
- + * Controller and SSH device capabilities found in ACPI.
- + */
- +struct ssam_controller_caps {
- + u32 ssh_power_profile;
- + u32 ssh_buffer_size;
- + u32 screen_on_sleep_idle_timeout;
- + u32 screen_off_sleep_idle_timeout;
- + u32 d3_closes_handle:1;
- +};
- +
- +/**
- + * struct ssam_controller - SSAM controller device.
- + * @kref: Reference count of the controller.
- + * @lock: Main lock for the controller, used to guard state changes.
- + * @state: Controller state.
- + * @rtl: Request transport layer for SSH I/O.
- + * @cplt: Completion system for SSH/SSAM events and asynchronous requests.
- + * @counter: Safe SSH message ID counters.
- + * @counter.seq: Sequence ID counter.
- + * @counter.rqid: Request ID counter.
- + * @irq: Wakeup IRQ resources.
- + * @irq.num: The wakeup IRQ number.
- + * @irq.wakeup_enabled: Whether wakeup by IRQ is enabled during suspend.
- + * @caps: The controller device capabilities.
- + */
- +struct ssam_controller {
- + struct kref kref;
- +
- + struct rw_semaphore lock;
- + enum ssam_controller_state state;
- +
- + struct ssh_rtl rtl;
- + struct ssam_cplt cplt;
- +
- + struct {
- + struct ssh_seq_counter seq;
- + struct ssh_rqid_counter rqid;
- + } counter;
- +
- + struct {
- + int num;
- + bool wakeup_enabled;
- + } irq;
- +
- + struct ssam_controller_caps caps;
- +};
- +
- +#define to_ssam_controller(ptr, member) \
- + container_of(ptr, struct ssam_controller, member)
- +
- +#define ssam_dbg(ctrl, fmt, ...) rtl_dbg(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
- +#define ssam_info(ctrl, fmt, ...) rtl_info(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
- +#define ssam_warn(ctrl, fmt, ...) rtl_warn(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
- +#define ssam_err(ctrl, fmt, ...) rtl_err(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
- +
- +/**
- + * ssam_controller_receive_buf() - Provide input-data to the controller.
- + * @ctrl: The controller.
- + * @buf: The input buffer.
- + * @n: The number of bytes in the input buffer.
- + *
- + * Provide input data to be evaluated by the controller, which has been
- + * received via the lower-level transport.
- + *
- + * Return: Returns the number of bytes consumed, or, if the packet transport
- + * layer of the controller has been shut down, %-ESHUTDOWN.
- + */
- +static inline
- +int ssam_controller_receive_buf(struct ssam_controller *ctrl,
- + const unsigned char *buf, size_t n)
- +{
- + return ssh_ptl_rx_rcvbuf(&ctrl->rtl.ptl, buf, n);
- +}
- +
- +/**
- + * ssam_controller_write_wakeup() - Notify the controller that the underlying
- + * device has space available for data to be written.
- + * @ctrl: The controller.
- + */
- +static inline void ssam_controller_write_wakeup(struct ssam_controller *ctrl)
- +{
- + ssh_ptl_tx_wakeup_transfer(&ctrl->rtl.ptl);
- +}
- +
- +int ssam_controller_init(struct ssam_controller *ctrl, struct serdev_device *s);
- +int ssam_controller_start(struct ssam_controller *ctrl);
- +void ssam_controller_shutdown(struct ssam_controller *ctrl);
- +void ssam_controller_destroy(struct ssam_controller *ctrl);
- +
- +int ssam_notifier_disable_registered(struct ssam_controller *ctrl);
- +void ssam_notifier_restore_registered(struct ssam_controller *ctrl);
- +
- +int ssam_irq_setup(struct ssam_controller *ctrl);
- +void ssam_irq_free(struct ssam_controller *ctrl);
- +int ssam_irq_arm_for_wakeup(struct ssam_controller *ctrl);
- +void ssam_irq_disarm_wakeup(struct ssam_controller *ctrl);
- +
- +void ssam_controller_lock(struct ssam_controller *c);
- +void ssam_controller_unlock(struct ssam_controller *c);
- +
- +int ssam_get_firmware_version(struct ssam_controller *ctrl, u32 *version);
- +int ssam_ctrl_notif_display_off(struct ssam_controller *ctrl);
- +int ssam_ctrl_notif_display_on(struct ssam_controller *ctrl);
- +int ssam_ctrl_notif_d0_exit(struct ssam_controller *ctrl);
- +int ssam_ctrl_notif_d0_entry(struct ssam_controller *ctrl);
- +
- +int ssam_controller_suspend(struct ssam_controller *ctrl);
- +int ssam_controller_resume(struct ssam_controller *ctrl);
- +
- +#endif /* _SURFACE_AGGREGATOR_CONTROLLER_H */
- diff --git a/drivers/platform/surface/aggregator/core.c b/drivers/platform/surface/aggregator/core.c
- new file mode 100644
- index 000000000000..18e0e9e34e7b
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/core.c
- @@ -0,0 +1,787 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * Surface Serial Hub (SSH) driver for communication with the Surface/System
- + * Aggregator Module (SSAM/SAM).
- + *
- + * Provides access to a SAM-over-SSH connected EC via a controller device.
- + * Handles communication via requests as well as enabling, disabling, and
- + * relaying of events.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <linux/acpi.h>
- +#include <linux/atomic.h>
- +#include <linux/completion.h>
- +#include <linux/gpio/consumer.h>
- +#include <linux/kernel.h>
- +#include <linux/kref.h>
- +#include <linux/module.h>
- +#include <linux/pm.h>
- +#include <linux/serdev.h>
- +#include <linux/sysfs.h>
- +
- +#include <linux/surface_aggregator/controller.h>
- +#include "controller.h"
- +
- +
- +/* -- Static controller reference. ------------------------------------------ */
- +
- +/*
- + * Main controller reference. The corresponding lock must be held while
- + * accessing (reading/writing) the reference.
- + */
- +static struct ssam_controller *__ssam_controller;
- +static DEFINE_SPINLOCK(__ssam_controller_lock);
- +
- +/**
- + * ssam_get_controller() - Get reference to SSAM controller.
- + *
- + * Returns a reference to the SSAM controller of the system or %NULL if there
- + * is none, it hasn't been set up yet, or it has already been unregistered.
- + * This function automatically increments the reference count of the
- + * controller, thus the calling party must ensure that ssam_controller_put()
- + * is called when it doesn't need the controller any more.
- + */
- +struct ssam_controller *ssam_get_controller(void)
- +{
- + struct ssam_controller *ctrl;
- +
- + spin_lock(&__ssam_controller_lock);
- +
- + ctrl = __ssam_controller;
- + if (!ctrl)
- + goto out;
- +
- + if (WARN_ON(!kref_get_unless_zero(&ctrl->kref)))
- + ctrl = NULL;
- +
- +out:
- + spin_unlock(&__ssam_controller_lock);
- + return ctrl;
- +}
- +EXPORT_SYMBOL_GPL(ssam_get_controller);
- +
- +/**
- + * ssam_try_set_controller() - Try to set the main controller reference.
- + * @ctrl: The controller to which the reference should point.
- + *
- + * Set the main controller reference to the given pointer if the reference
- + * hasn't been set already.
- + *
- + * Return: Returns zero on success or %-EEXIST if the reference has already
- + * been set.
- + */
- +static int ssam_try_set_controller(struct ssam_controller *ctrl)
- +{
- + int status = 0;
- +
- + spin_lock(&__ssam_controller_lock);
- + if (!__ssam_controller)
- + __ssam_controller = ctrl;
- + else
- + status = -EEXIST;
- + spin_unlock(&__ssam_controller_lock);
- +
- + return status;
- +}
- +
- +/**
- + * ssam_clear_controller() - Remove/clear the main controller reference.
- + *
- + * Clears the main controller reference, i.e. sets it to %NULL. This function
- + * should be called before the controller is shut down.
- + */
- +static void ssam_clear_controller(void)
- +{
- + spin_lock(&__ssam_controller_lock);
- + __ssam_controller = NULL;
- + spin_unlock(&__ssam_controller_lock);
- +}
- +
- +/**
- + * ssam_client_link() - Link an arbitrary client device to the controller.
- + * @c: The controller to link to.
- + * @client: The client device.
- + *
- + * Link an arbitrary client device to the controller by creating a device link
- + * between it as consumer and the controller device as provider. This function
- + * can be used for non-SSAM devices (or SSAM devices not registered as child
- + * under the controller) to guarantee that the controller is valid for as long
- + * as the driver of the client device is bound, and that proper suspend and
- + * resume ordering is guaranteed.
- + *
- + * The device link does not have to be destructed manually. It is removed
- + * automatically once the driver of the client device unbinds.
- + *
- + * Return: Returns zero on success, %-ENODEV if the controller is not ready or
- + * going to be removed soon, or %-ENOMEM if the device link could not be
- + * created for other reasons.
- + */
- +int ssam_client_link(struct ssam_controller *c, struct device *client)
- +{
- + const u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;
- + struct device_link *link;
- + struct device *ctrldev;
- +
- + ssam_controller_statelock(c);
- +
- + if (c->state != SSAM_CONTROLLER_STARTED) {
- + ssam_controller_stateunlock(c);
- + return -ENODEV;
- + }
- +
- + ctrldev = ssam_controller_device(c);
- + if (!ctrldev) {
- + ssam_controller_stateunlock(c);
- + return -ENODEV;
- + }
- +
- + link = device_link_add(client, ctrldev, flags);
- + if (!link) {
- + ssam_controller_stateunlock(c);
- + return -ENOMEM;
- + }
- +
- + /*
- + * Return -ENODEV if supplier driver is on its way to be removed. In
- + * this case, the controller won't be around for much longer and the
- + * device link is not going to save us any more, as unbinding is
- + * already in progress.
- + */
- + if (READ_ONCE(link->status) == DL_STATE_SUPPLIER_UNBIND) {
- + ssam_controller_stateunlock(c);
- + return -ENODEV;
- + }
- +
- + ssam_controller_stateunlock(c);
- + return 0;
- +}
- +EXPORT_SYMBOL_GPL(ssam_client_link);
- +
- +/**
- + * ssam_client_bind() - Bind an arbitrary client device to the controller.
- + * @client: The client device.
- + *
- + * Link an arbitrary client device to the controller by creating a device link
- + * between it as consumer and the main controller device as provider. This
- + * function can be used for non-SSAM devices to guarantee that the controller
- + * returned by this function is valid for as long as the driver of the client
- + * device is bound, and that proper suspend and resume ordering is guaranteed.
- + *
- + * This function does essentially the same as ssam_client_link(), except that
- + * it first fetches the main controller reference, then creates the link, and
- + * finally returns this reference. Note that this function does not increment
- + * the reference counter of the controller, as, due to the link, the
- + * controller lifetime is assured as long as the driver of the client device
- + * is bound.
- + *
- + * It is not valid to use the controller reference obtained by this method
- + * outside of the driver bound to the client device at the time of calling
- + * this function, without first incrementing the reference count of the
- + * controller via ssam_controller_get(). Even after doing this, care must be
- + * taken that requests are only submitted and notifiers are only
- + * (un-)registered when the controller is active and not suspended. In other
- + * words: The device link only lives as long as the client driver is bound and
- + * any guarantees enforced by this link (e.g. active controller state) can
- + * only be relied upon as long as this link exists and may need to be enforced
- + * in other ways afterwards.
- + *
- + * The created device link does not have to be destructed manually. It is
- + * removed automatically once the driver of the client device unbinds.
- + *
- + * Return: Returns the controller on success, an error pointer with %-ENODEV
- + * if the controller is not present, not ready or going to be removed soon, or
- + * %-ENOMEM if the device link could not be created for other reasons.
- + */
- +struct ssam_controller *ssam_client_bind(struct device *client)
- +{
- + struct ssam_controller *c;
- + int status;
- +
- + c = ssam_get_controller();
- + if (!c)
- + return ERR_PTR(-ENODEV);
- +
- + status = ssam_client_link(c, client);
- +
- + /*
- + * Note that we can drop our controller reference in both success and
- + * failure cases: On success, we have bound the controller lifetime
- + * inherently to the client driver lifetime, i.e. it the controller is
- + * now guaranteed to outlive the client driver. On failure, we're not
- + * going to use the controller any more.
- + */
- + ssam_controller_put(c);
- +
- + return status >= 0 ? c : ERR_PTR(status);
- +}
- +EXPORT_SYMBOL_GPL(ssam_client_bind);
- +
- +
- +/* -- Glue layer (serdev_device -> ssam_controller). ------------------------ */
- +
- +static int ssam_receive_buf(struct serdev_device *dev, const unsigned char *buf,
- + size_t n)
- +{
- + struct ssam_controller *ctrl;
- +
- + ctrl = serdev_device_get_drvdata(dev);
- + return ssam_controller_receive_buf(ctrl, buf, n);
- +}
- +
- +static void ssam_write_wakeup(struct serdev_device *dev)
- +{
- + ssam_controller_write_wakeup(serdev_device_get_drvdata(dev));
- +}
- +
- +static const struct serdev_device_ops ssam_serdev_ops = {
- + .receive_buf = ssam_receive_buf,
- + .write_wakeup = ssam_write_wakeup,
- +};
- +
- +
- +/* -- SysFS and misc. ------------------------------------------------------- */
- +
- +static int ssam_log_firmware_version(struct ssam_controller *ctrl)
- +{
- + u32 version, a, b, c;
- + int status;
- +
- + status = ssam_get_firmware_version(ctrl, &version);
- + if (status)
- + return status;
- +
- + a = (version >> 24) & 0xff;
- + b = ((version >> 8) & 0xffff);
- + c = version & 0xff;
- +
- + ssam_info(ctrl, "SAM firmware version: %u.%u.%u\n", a, b, c);
- + return 0;
- +}
- +
- +static ssize_t firmware_version_show(struct device *dev,
- + struct device_attribute *attr, char *buf)
- +{
- + struct ssam_controller *ctrl = dev_get_drvdata(dev);
- + u32 version, a, b, c;
- + int status;
- +
- + status = ssam_get_firmware_version(ctrl, &version);
- + if (status < 0)
- + return status;
- +
- + a = (version >> 24) & 0xff;
- + b = ((version >> 8) & 0xffff);
- + c = version & 0xff;
- +
- + return sysfs_emit(buf, "%u.%u.%u\n", a, b, c);
- +}
- +static DEVICE_ATTR_RO(firmware_version);
- +
- +static struct attribute *ssam_sam_attrs[] = {
- + &dev_attr_firmware_version.attr,
- + NULL
- +};
- +
- +static const struct attribute_group ssam_sam_group = {
- + .name = "sam",
- + .attrs = ssam_sam_attrs,
- +};
- +
- +
- +/* -- ACPI based device setup. ---------------------------------------------- */
- +
- +static acpi_status ssam_serdev_setup_via_acpi_crs(struct acpi_resource *rsc,
- + void *ctx)
- +{
- + struct serdev_device *serdev = ctx;
- + struct acpi_resource_common_serialbus *serial;
- + struct acpi_resource_uart_serialbus *uart;
- + bool flow_control;
- + int status = 0;
- +
- + if (rsc->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
- + return AE_OK;
- +
- + serial = &rsc->data.common_serial_bus;
- + if (serial->type != ACPI_RESOURCE_SERIAL_TYPE_UART)
- + return AE_OK;
- +
- + uart = &rsc->data.uart_serial_bus;
- +
- + /* Set up serdev device. */
- + serdev_device_set_baudrate(serdev, uart->default_baud_rate);
- +
- + /* serdev currently only supports RTSCTS flow control. */
- + if (uart->flow_control & (~((u8)ACPI_UART_FLOW_CONTROL_HW))) {
- + dev_warn(&serdev->dev, "setup: unsupported flow control (value: %#04x)\n",
- + uart->flow_control);
- + }
- +
- + /* Set RTSCTS flow control. */
- + flow_control = uart->flow_control & ACPI_UART_FLOW_CONTROL_HW;
- + serdev_device_set_flow_control(serdev, flow_control);
- +
- + /* serdev currently only supports EVEN/ODD parity. */
- + switch (uart->parity) {
- + case ACPI_UART_PARITY_NONE:
- + status = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
- + break;
- + case ACPI_UART_PARITY_EVEN:
- + status = serdev_device_set_parity(serdev, SERDEV_PARITY_EVEN);
- + break;
- + case ACPI_UART_PARITY_ODD:
- + status = serdev_device_set_parity(serdev, SERDEV_PARITY_ODD);
- + break;
- + default:
- + dev_warn(&serdev->dev, "setup: unsupported parity (value: %#04x)\n",
- + uart->parity);
- + break;
- + }
- +
- + if (status) {
- + dev_err(&serdev->dev, "setup: failed to set parity (value: %#04x, error: %d)\n",
- + uart->parity, status);
- + return AE_ERROR;
- + }
- +
- + /* We've found the resource and are done. */
- + return AE_CTRL_TERMINATE;
- +}
- +
- +static acpi_status ssam_serdev_setup_via_acpi(acpi_handle handle,
- + struct serdev_device *serdev)
- +{
- + return acpi_walk_resources(handle, METHOD_NAME__CRS,
- + ssam_serdev_setup_via_acpi_crs, serdev);
- +}
- +
- +
- +/* -- Power management. ----------------------------------------------------- */
- +
- +static void ssam_serial_hub_shutdown(struct device *dev)
- +{
- + struct ssam_controller *c = dev_get_drvdata(dev);
- + int status;
- +
- + /*
- + * Try to disable notifiers, signal display-off and D0-exit, ignore any
- + * errors.
- + *
- + * Note: It has not been established yet if this is actually
- + * necessary/useful for shutdown.
- + */
- +
- + status = ssam_notifier_disable_registered(c);
- + if (status) {
- + ssam_err(c, "pm: failed to disable notifiers for shutdown: %d\n",
- + status);
- + }
- +
- + status = ssam_ctrl_notif_display_off(c);
- + if (status)
- + ssam_err(c, "pm: display-off notification failed: %d\n", status);
- +
- + status = ssam_ctrl_notif_d0_exit(c);
- + if (status)
- + ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
- +}
- +
- +#ifdef CONFIG_PM_SLEEP
- +
- +static int ssam_serial_hub_pm_prepare(struct device *dev)
- +{
- + struct ssam_controller *c = dev_get_drvdata(dev);
- + int status;
- +
- + /*
- + * Try to signal display-off, This will quiesce events.
- + *
- + * Note: Signaling display-off/display-on should normally be done from
- + * some sort of display state notifier. As that is not available,
- + * signal it here.
- + */
- +
- + status = ssam_ctrl_notif_display_off(c);
- + if (status)
- + ssam_err(c, "pm: display-off notification failed: %d\n", status);
- +
- + return status;
- +}
- +
- +static void ssam_serial_hub_pm_complete(struct device *dev)
- +{
- + struct ssam_controller *c = dev_get_drvdata(dev);
- + int status;
- +
- + /*
- + * Try to signal display-on. This will restore events.
- + *
- + * Note: Signaling display-off/display-on should normally be done from
- + * some sort of display state notifier. As that is not available,
- + * signal it here.
- + */
- +
- + status = ssam_ctrl_notif_display_on(c);
- + if (status)
- + ssam_err(c, "pm: display-on notification failed: %d\n", status);
- +}
- +
- +static int ssam_serial_hub_pm_suspend(struct device *dev)
- +{
- + struct ssam_controller *c = dev_get_drvdata(dev);
- + int status;
- +
- + /*
- + * Try to signal D0-exit, enable IRQ wakeup if specified. Abort on
- + * error.
- + */
- +
- + status = ssam_ctrl_notif_d0_exit(c);
- + if (status) {
- + ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
- + goto err_notif;
- + }
- +
- + status = ssam_irq_arm_for_wakeup(c);
- + if (status)
- + goto err_irq;
- +
- + WARN_ON(ssam_controller_suspend(c));
- + return 0;
- +
- +err_irq:
- + ssam_ctrl_notif_d0_entry(c);
- +err_notif:
- + ssam_ctrl_notif_display_on(c);
- + return status;
- +}
- +
- +static int ssam_serial_hub_pm_resume(struct device *dev)
- +{
- + struct ssam_controller *c = dev_get_drvdata(dev);
- + int status;
- +
- + WARN_ON(ssam_controller_resume(c));
- +
- + /*
- + * Try to disable IRQ wakeup (if specified) and signal D0-entry. In
- + * case of errors, log them and try to restore normal operation state
- + * as far as possible.
- + *
- + * Note: Signaling display-off/display-on should normally be done from
- + * some sort of display state notifier. As that is not available,
- + * signal it here.
- + */
- +
- + ssam_irq_disarm_wakeup(c);
- +
- + status = ssam_ctrl_notif_d0_entry(c);
- + if (status)
- + ssam_err(c, "pm: D0-entry notification failed: %d\n", status);
- +
- + return 0;
- +}
- +
- +static int ssam_serial_hub_pm_freeze(struct device *dev)
- +{
- + struct ssam_controller *c = dev_get_drvdata(dev);
- + int status;
- +
- + /*
- + * During hibernation image creation, we only have to ensure that the
- + * EC doesn't send us any events. This is done via the display-off
- + * and D0-exit notifications. Note that this sets up the wakeup IRQ
- + * on the EC side, however, we have disabled it by default on our side
- + * and won't enable it here.
- + *
- + * See ssam_serial_hub_poweroff() for more details on the hibernation
- + * process.
- + */
- +
- + status = ssam_ctrl_notif_d0_exit(c);
- + if (status) {
- + ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
- + ssam_ctrl_notif_display_on(c);
- + return status;
- + }
- +
- + WARN_ON(ssam_controller_suspend(c));
- + return 0;
- +}
- +
- +static int ssam_serial_hub_pm_thaw(struct device *dev)
- +{
- + struct ssam_controller *c = dev_get_drvdata(dev);
- + int status;
- +
- + WARN_ON(ssam_controller_resume(c));
- +
- + status = ssam_ctrl_notif_d0_entry(c);
- + if (status)
- + ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
- +
- + return status;
- +}
- +
- +static int ssam_serial_hub_pm_poweroff(struct device *dev)
- +{
- + struct ssam_controller *c = dev_get_drvdata(dev);
- + int status;
- +
- + /*
- + * When entering hibernation and powering off the system, the EC, at
- + * least on some models, may disable events. Without us taking care of
- + * that, this leads to events not being enabled/restored when the
- + * system resumes from hibernation, resulting SAM-HID subsystem devices
- + * (i.e. keyboard, touchpad) not working, AC-plug/AC-unplug events being
- + * gone, etc.
- + *
- + * To avoid these issues, we disable all registered events here (this is
- + * likely not actually required) and restore them during the drivers PM
- + * restore callback.
- + *
- + * Wakeup from the EC interrupt is not supported during hibernation,
- + * so don't arm the IRQ here.
- + */
- +
- + status = ssam_notifier_disable_registered(c);
- + if (status) {
- + ssam_err(c, "pm: failed to disable notifiers for hibernation: %d\n",
- + status);
- + return status;
- + }
- +
- + status = ssam_ctrl_notif_d0_exit(c);
- + if (status) {
- + ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
- + ssam_notifier_restore_registered(c);
- + return status;
- + }
- +
- + WARN_ON(ssam_controller_suspend(c));
- + return 0;
- +}
- +
- +static int ssam_serial_hub_pm_restore(struct device *dev)
- +{
- + struct ssam_controller *c = dev_get_drvdata(dev);
- + int status;
- +
- + /*
- + * Ignore but log errors, try to restore state as much as possible in
- + * case of failures. See ssam_serial_hub_poweroff() for more details on
- + * the hibernation process.
- + */
- +
- + WARN_ON(ssam_controller_resume(c));
- +
- + status = ssam_ctrl_notif_d0_entry(c);
- + if (status)
- + ssam_err(c, "pm: D0-entry notification failed: %d\n", status);
- +
- + ssam_notifier_restore_registered(c);
- + return 0;
- +}
- +
- +static const struct dev_pm_ops ssam_serial_hub_pm_ops = {
- + .prepare = ssam_serial_hub_pm_prepare,
- + .complete = ssam_serial_hub_pm_complete,
- + .suspend = ssam_serial_hub_pm_suspend,
- + .resume = ssam_serial_hub_pm_resume,
- + .freeze = ssam_serial_hub_pm_freeze,
- + .thaw = ssam_serial_hub_pm_thaw,
- + .poweroff = ssam_serial_hub_pm_poweroff,
- + .restore = ssam_serial_hub_pm_restore,
- +};
- +
- +#else /* CONFIG_PM_SLEEP */
- +
- +static const struct dev_pm_ops ssam_serial_hub_pm_ops = { };
- +
- +#endif /* CONFIG_PM_SLEEP */
- +
- +
- +/* -- Device/driver setup. -------------------------------------------------- */
- +
- +static const struct acpi_gpio_params gpio_ssam_wakeup_int = { 0, 0, false };
- +static const struct acpi_gpio_params gpio_ssam_wakeup = { 1, 0, false };
- +
- +static const struct acpi_gpio_mapping ssam_acpi_gpios[] = {
- + { "ssam_wakeup-int-gpio", &gpio_ssam_wakeup_int, 1 },
- + { "ssam_wakeup-gpio", &gpio_ssam_wakeup, 1 },
- + { },
- +};
- +
- +static int ssam_serial_hub_probe(struct serdev_device *serdev)
- +{
- + struct ssam_controller *ctrl;
- + acpi_handle *ssh = ACPI_HANDLE(&serdev->dev);
- + acpi_status astatus;
- + int status;
- +
- + if (gpiod_count(&serdev->dev, NULL) < 0)
- + return -ENODEV;
- +
- + status = devm_acpi_dev_add_driver_gpios(&serdev->dev, ssam_acpi_gpios);
- + if (status)
- + return status;
- +
- + /* Allocate controller. */
- + ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
- + if (!ctrl)
- + return -ENOMEM;
- +
- + /* Initialize controller. */
- + status = ssam_controller_init(ctrl, serdev);
- + if (status)
- + goto err_ctrl_init;
- +
- + ssam_controller_lock(ctrl);
- +
- + /* Set up serdev device. */
- + serdev_device_set_drvdata(serdev, ctrl);
- + serdev_device_set_client_ops(serdev, &ssam_serdev_ops);
- + status = serdev_device_open(serdev);
- + if (status)
- + goto err_devopen;
- +
- + astatus = ssam_serdev_setup_via_acpi(ssh, serdev);
- + if (ACPI_FAILURE(astatus)) {
- + status = -ENXIO;
- + goto err_devinit;
- + }
- +
- + /* Start controller. */
- + status = ssam_controller_start(ctrl);
- + if (status)
- + goto err_devinit;
- +
- + ssam_controller_unlock(ctrl);
- +
- + /*
- + * Initial SAM requests: Log version and notify default/init power
- + * states.
- + */
- + status = ssam_log_firmware_version(ctrl);
- + if (status)
- + goto err_initrq;
- +
- + status = ssam_ctrl_notif_d0_entry(ctrl);
- + if (status)
- + goto err_initrq;
- +
- + status = ssam_ctrl_notif_display_on(ctrl);
- + if (status)
- + goto err_initrq;
- +
- + status = sysfs_create_group(&serdev->dev.kobj, &ssam_sam_group);
- + if (status)
- + goto err_initrq;
- +
- + /* Set up IRQ. */
- + status = ssam_irq_setup(ctrl);
- + if (status)
- + goto err_irq;
- +
- + /* Finally, set main controller reference. */
- + status = ssam_try_set_controller(ctrl);
- + if (WARN_ON(status)) /* Currently, we're the only provider. */
- + goto err_mainref;
- +
- + /*
- + * TODO: The EC can wake up the system via the associated GPIO interrupt
- + * in multiple situations. One of which is the remaining battery
- + * capacity falling below a certain threshold. Normally, we should
- + * use the device_init_wakeup function, however, the EC also seems
- + * to have other reasons for waking up the system and it seems
- + * that Windows has additional checks whether the system should be
- + * resumed. In short, this causes some spurious unwanted wake-ups.
- + * For now let's thus default power/wakeup to false.
- + */
- + device_set_wakeup_capable(&serdev->dev, true);
- + acpi_walk_dep_device_list(ssh);
- +
- + return 0;
- +
- +err_mainref:
- + ssam_irq_free(ctrl);
- +err_irq:
- + sysfs_remove_group(&serdev->dev.kobj, &ssam_sam_group);
- +err_initrq:
- + ssam_controller_lock(ctrl);
- + ssam_controller_shutdown(ctrl);
- +err_devinit:
- + serdev_device_close(serdev);
- +err_devopen:
- + ssam_controller_destroy(ctrl);
- + ssam_controller_unlock(ctrl);
- +err_ctrl_init:
- + kfree(ctrl);
- + return status;
- +}
- +
- +static void ssam_serial_hub_remove(struct serdev_device *serdev)
- +{
- + struct ssam_controller *ctrl = serdev_device_get_drvdata(serdev);
- + int status;
- +
- + /* Clear static reference so that no one else can get a new one. */
- + ssam_clear_controller();
- +
- + /* Disable and free IRQ. */
- + ssam_irq_free(ctrl);
- +
- + sysfs_remove_group(&serdev->dev.kobj, &ssam_sam_group);
- + ssam_controller_lock(ctrl);
- +
- + /* Act as if suspending to silence events. */
- + status = ssam_ctrl_notif_display_off(ctrl);
- + if (status) {
- + dev_err(&serdev->dev, "display-off notification failed: %d\n",
- + status);
- + }
- +
- + status = ssam_ctrl_notif_d0_exit(ctrl);
- + if (status) {
- + dev_err(&serdev->dev, "D0-exit notification failed: %d\n",
- + status);
- + }
- +
- + /* Shut down controller and remove serdev device reference from it. */
- + ssam_controller_shutdown(ctrl);
- +
- + /* Shut down actual transport. */
- + serdev_device_wait_until_sent(serdev, 0);
- + serdev_device_close(serdev);
- +
- + /* Drop our controller reference. */
- + ssam_controller_unlock(ctrl);
- + ssam_controller_put(ctrl);
- +
- + device_set_wakeup_capable(&serdev->dev, false);
- +}
- +
- +static const struct acpi_device_id ssam_serial_hub_match[] = {
- + { "MSHW0084", 0 },
- + { },
- +};
- +MODULE_DEVICE_TABLE(acpi, ssam_serial_hub_match);
- +
- +static struct serdev_device_driver ssam_serial_hub = {
- + .probe = ssam_serial_hub_probe,
- + .remove = ssam_serial_hub_remove,
- + .driver = {
- + .name = "surface_serial_hub",
- + .acpi_match_table = ssam_serial_hub_match,
- + .pm = &ssam_serial_hub_pm_ops,
- + .shutdown = ssam_serial_hub_shutdown,
- + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- + },
- +};
- +module_serdev_device_driver(ssam_serial_hub);
- +
- +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- +MODULE_DESCRIPTION("Subsystem and Surface Serial Hub driver for Surface System Aggregator Module");
- +MODULE_LICENSE("GPL");
- diff --git a/drivers/platform/surface/aggregator/ssh_msgb.h b/drivers/platform/surface/aggregator/ssh_msgb.h
- new file mode 100644
- index 000000000000..1221f642dda1
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/ssh_msgb.h
- @@ -0,0 +1,205 @@
- +/* SPDX-License-Identifier: GPL-2.0+ */
- +/*
- + * SSH message builder functions.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#ifndef _SURFACE_AGGREGATOR_SSH_MSGB_H
- +#define _SURFACE_AGGREGATOR_SSH_MSGB_H
- +
- +#include <asm/unaligned.h>
- +#include <linux/types.h>
- +
- +#include <linux/surface_aggregator/controller.h>
- +#include <linux/surface_aggregator/serial_hub.h>
- +
- +/**
- + * struct msgbuf - Buffer struct to construct SSH messages.
- + * @begin: Pointer to the beginning of the allocated buffer space.
- + * @end: Pointer to the end (one past last element) of the allocated buffer
- + * space.
- + * @ptr: Pointer to the first free element in the buffer.
- + */
- +struct msgbuf {
- + u8 *begin;
- + u8 *end;
- + u8 *ptr;
- +};
- +
- +/**
- + * msgb_init() - Initialize the given message buffer struct.
- + * @msgb: The buffer struct to initialize
- + * @ptr: Pointer to the underlying memory by which the buffer will be backed.
- + * @cap: Size of the underlying memory.
- + *
- + * Initialize the given message buffer struct using the provided memory as
- + * backing.
- + */
- +static inline void msgb_init(struct msgbuf *msgb, u8 *ptr, size_t cap)
- +{
- + msgb->begin = ptr;
- + msgb->end = ptr + cap;
- + msgb->ptr = ptr;
- +}
- +
- +/**
- + * msgb_bytes_used() - Return the current number of bytes used in the buffer.
- + * @msgb: The message buffer.
- + */
- +static inline size_t msgb_bytes_used(const struct msgbuf *msgb)
- +{
- + return msgb->ptr - msgb->begin;
- +}
- +
- +static inline void __msgb_push_u8(struct msgbuf *msgb, u8 value)
- +{
- + *msgb->ptr = value;
- + msgb->ptr += sizeof(u8);
- +}
- +
- +static inline void __msgb_push_u16(struct msgbuf *msgb, u16 value)
- +{
- + put_unaligned_le16(value, msgb->ptr);
- + msgb->ptr += sizeof(u16);
- +}
- +
- +/**
- + * msgb_push_u16() - Push a u16 value to the buffer.
- + * @msgb: The message buffer.
- + * @value: The value to push to the buffer.
- + */
- +static inline void msgb_push_u16(struct msgbuf *msgb, u16 value)
- +{
- + if (WARN_ON(msgb->ptr + sizeof(u16) > msgb->end))
- + return;
- +
- + __msgb_push_u16(msgb, value);
- +}
- +
- +/**
- + * msgb_push_syn() - Push SSH SYN bytes to the buffer.
- + * @msgb: The message buffer.
- + */
- +static inline void msgb_push_syn(struct msgbuf *msgb)
- +{
- + msgb_push_u16(msgb, SSH_MSG_SYN);
- +}
- +
- +/**
- + * msgb_push_buf() - Push raw data to the buffer.
- + * @msgb: The message buffer.
- + * @buf: The data to push to the buffer.
- + * @len: The length of the data to push to the buffer.
- + */
- +static inline void msgb_push_buf(struct msgbuf *msgb, const u8 *buf, size_t len)
- +{
- + msgb->ptr = memcpy(msgb->ptr, buf, len) + len;
- +}
- +
- +/**
- + * msgb_push_crc() - Compute CRC and push it to the buffer.
- + * @msgb: The message buffer.
- + * @buf: The data for which the CRC should be computed.
- + * @len: The length of the data for which the CRC should be computed.
- + */
- +static inline void msgb_push_crc(struct msgbuf *msgb, const u8 *buf, size_t len)
- +{
- + msgb_push_u16(msgb, ssh_crc(buf, len));
- +}
- +
- +/**
- + * msgb_push_frame() - Push a SSH message frame header to the buffer.
- + * @msgb: The message buffer
- + * @ty: The type of the frame.
- + * @len: The length of the payload of the frame.
- + * @seq: The sequence ID of the frame/packet.
- + */
- +static inline void msgb_push_frame(struct msgbuf *msgb, u8 ty, u16 len, u8 seq)
- +{
- + u8 *const begin = msgb->ptr;
- +
- + if (WARN_ON(msgb->ptr + sizeof(struct ssh_frame) > msgb->end))
- + return;
- +
- + __msgb_push_u8(msgb, ty); /* Frame type. */
- + __msgb_push_u16(msgb, len); /* Frame payload length. */
- + __msgb_push_u8(msgb, seq); /* Frame sequence ID. */
- +
- + msgb_push_crc(msgb, begin, msgb->ptr - begin);
- +}
- +
- +/**
- + * msgb_push_ack() - Push a SSH ACK frame to the buffer.
- + * @msgb: The message buffer
- + * @seq: The sequence ID of the frame/packet to be ACKed.
- + */
- +static inline void msgb_push_ack(struct msgbuf *msgb, u8 seq)
- +{
- + /* SYN. */
- + msgb_push_syn(msgb);
- +
- + /* ACK-type frame + CRC. */
- + msgb_push_frame(msgb, SSH_FRAME_TYPE_ACK, 0x00, seq);
- +
- + /* Payload CRC (ACK-type frames do not have a payload). */
- + msgb_push_crc(msgb, msgb->ptr, 0);
- +}
- +
- +/**
- + * msgb_push_nak() - Push a SSH NAK frame to the buffer.
- + * @msgb: The message buffer
- + */
- +static inline void msgb_push_nak(struct msgbuf *msgb)
- +{
- + /* SYN. */
- + msgb_push_syn(msgb);
- +
- + /* NAK-type frame + CRC. */
- + msgb_push_frame(msgb, SSH_FRAME_TYPE_NAK, 0x00, 0x00);
- +
- + /* Payload CRC (ACK-type frames do not have a payload). */
- + msgb_push_crc(msgb, msgb->ptr, 0);
- +}
- +
- +/**
- + * msgb_push_cmd() - Push a SSH command frame with payload to the buffer.
- + * @msgb: The message buffer.
- + * @seq: The sequence ID (SEQ) of the frame/packet.
- + * @rqid: The request ID (RQID) of the request contained in the frame.
- + * @rqst: The request to wrap in the frame.
- + */
- +static inline void msgb_push_cmd(struct msgbuf *msgb, u8 seq, u16 rqid,
- + const struct ssam_request *rqst)
- +{
- + const u8 type = SSH_FRAME_TYPE_DATA_SEQ;
- + u8 *cmd;
- +
- + /* SYN. */
- + msgb_push_syn(msgb);
- +
- + /* Command frame + CRC. */
- + msgb_push_frame(msgb, type, sizeof(struct ssh_command) + rqst->length, seq);
- +
- + /* Frame payload: Command struct + payload. */
- + if (WARN_ON(msgb->ptr + sizeof(struct ssh_command) > msgb->end))
- + return;
- +
- + cmd = msgb->ptr;
- +
- + __msgb_push_u8(msgb, SSH_PLD_TYPE_CMD); /* Payload type. */
- + __msgb_push_u8(msgb, rqst->target_category); /* Target category. */
- + __msgb_push_u8(msgb, rqst->target_id); /* Target ID (out). */
- + __msgb_push_u8(msgb, 0x00); /* Target ID (in). */
- + __msgb_push_u8(msgb, rqst->instance_id); /* Instance ID. */
- + __msgb_push_u16(msgb, rqid); /* Request ID. */
- + __msgb_push_u8(msgb, rqst->command_id); /* Command ID. */
- +
- + /* Command payload. */
- + msgb_push_buf(msgb, rqst->payload, rqst->length);
- +
- + /* CRC for command struct + payload. */
- + msgb_push_crc(msgb, cmd, msgb->ptr - cmd);
- +}
- +
- +#endif /* _SURFACE_AGGREGATOR_SSH_MSGB_H */
- diff --git a/drivers/platform/surface/aggregator/ssh_packet_layer.c b/drivers/platform/surface/aggregator/ssh_packet_layer.c
- new file mode 100644
- index 000000000000..66e38fdc7963
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/ssh_packet_layer.c
- @@ -0,0 +1,1710 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * SSH packet transport layer.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <asm/unaligned.h>
- +#include <linux/atomic.h>
- +#include <linux/jiffies.h>
- +#include <linux/kfifo.h>
- +#include <linux/kref.h>
- +#include <linux/kthread.h>
- +#include <linux/ktime.h>
- +#include <linux/limits.h>
- +#include <linux/list.h>
- +#include <linux/lockdep.h>
- +#include <linux/serdev.h>
- +#include <linux/slab.h>
- +#include <linux/spinlock.h>
- +#include <linux/workqueue.h>
- +
- +#include <linux/surface_aggregator/serial_hub.h>
- +
- +#include "ssh_msgb.h"
- +#include "ssh_packet_layer.h"
- +#include "ssh_parser.h"
- +
- +/*
- + * To simplify reasoning about the code below, we define a few concepts. The
- + * system below is similar to a state-machine for packets, however, there are
- + * too many states to explicitly write them down. To (somewhat) manage the
- + * states and packets we rely on flags, reference counting, and some simple
- + * concepts. State transitions are triggered by actions.
- + *
- + * >> Actions <<
- + *
- + * - submit
- + * - transmission start (process next item in queue)
- + * - transmission finished (guaranteed to never be parallel to transmission
- + * start)
- + * - ACK received
- + * - NAK received (this is equivalent to issuing re-submit for all pending
- + * packets)
- + * - timeout (this is equivalent to re-issuing a submit or canceling)
- + * - cancel (non-pending and pending)
- + *
- + * >> Data Structures, Packet Ownership, General Overview <<
- + *
- + * The code below employs two main data structures: The packet queue,
- + * containing all packets scheduled for transmission, and the set of pending
- + * packets, containing all packets awaiting an ACK.
- + *
- + * Shared ownership of a packet is controlled via reference counting. Inside
- + * the transport system are a total of five packet owners:
- + *
- + * - the packet queue,
- + * - the pending set,
- + * - the transmitter thread,
- + * - the receiver thread (via ACKing), and
- + * - the timeout work item.
- + *
- + * Normal operation is as follows: The initial reference of the packet is
- + * obtained by submitting the packet and queuing it. The receiver thread takes
- + * packets from the queue. By doing this, it does not increment the refcount
- + * but takes over the reference (removing it from the queue). If the packet is
- + * sequenced (i.e. needs to be ACKed by the client), the transmitter thread
- + * sets-up the timeout and adds the packet to the pending set before starting
- + * to transmit it. As the timeout is handled by a reaper task, no additional
- + * reference for it is needed. After the transmit is done, the reference held
- + * by the transmitter thread is dropped. If the packet is unsequenced (i.e.
- + * does not need an ACK), the packet is completed by the transmitter thread
- + * before dropping that reference.
- + *
- + * On receival of an ACK, the receiver thread removes and obtains the
- + * reference to the packet from the pending set. The receiver thread will then
- + * complete the packet and drop its reference.
- + *
- + * On receival of a NAK, the receiver thread re-submits all currently pending
- + * packets.
- + *
- + * Packet timeouts are detected by the timeout reaper. This is a task,
- + * scheduled depending on the earliest packet timeout expiration date,
- + * checking all currently pending packets if their timeout has expired. If the
- + * timeout of a packet has expired, it is re-submitted and the number of tries
- + * of this packet is incremented. If this number reaches its limit, the packet
- + * will be completed with a failure.
- + *
- + * On transmission failure (such as repeated packet timeouts), the completion
- + * callback is immediately run by on thread on which the error was detected.
- + *
- + * To ensure that a packet eventually leaves the system it is marked as
- + * "locked" directly before it is going to be completed or when it is
- + * canceled. Marking a packet as "locked" has the effect that passing and
- + * creating new references of the packet is disallowed. This means that the
- + * packet cannot be added to the queue, the pending set, and the timeout, or
- + * be picked up by the transmitter thread or receiver thread. To remove a
- + * packet from the system it has to be marked as locked and subsequently all
- + * references from the data structures (queue, pending) have to be removed.
- + * References held by threads will eventually be dropped automatically as
- + * their execution progresses.
- + *
- + * Note that the packet completion callback is, in case of success and for a
- + * sequenced packet, guaranteed to run on the receiver thread, thus providing
- + * a way to reliably identify responses to the packet. The packet completion
- + * callback is only run once and it does not indicate that the packet has
- + * fully left the system (for this, one should rely on the release method,
- + * triggered when the reference count of the packet reaches zero). In case of
- + * re-submission (and with somewhat unlikely timing), it may be possible that
- + * the packet is being re-transmitted while the completion callback runs.
- + * Completion will occur both on success and internal error, as well as when
- + * the packet is canceled.
- + *
- + * >> Flags <<
- + *
- + * Flags are used to indicate the state and progression of a packet. Some flags
- + * have stricter guarantees than other:
- + *
- + * - locked
- + * Indicates if the packet is locked. If the packet is locked, passing and/or
- + * creating additional references to the packet is forbidden. The packet thus
- + * may not be queued, dequeued, or removed or added to the pending set. Note
- + * that the packet state flags may still change (e.g. it may be marked as
- + * ACKed, transmitted, ...).
- + *
- + * - completed
- + * Indicates if the packet completion callback has been executed or is about
- + * to be executed. This flag is used to ensure that the packet completion
- + * callback is only run once.
- + *
- + * - queued
- + * Indicates if a packet is present in the submission queue or not. This flag
- + * must only be modified with the queue lock held, and must be coherent to the
- + * presence of the packet in the queue.
- + *
- + * - pending
- + * Indicates if a packet is present in the set of pending packets or not.
- + * This flag must only be modified with the pending lock held, and must be
- + * coherent to the presence of the packet in the pending set.
- + *
- + * - transmitting
- + * Indicates if the packet is currently transmitting. In case of
- + * re-transmissions, it is only safe to wait on the "transmitted" completion
- + * after this flag has been set. The completion will be set both in success
- + * and error case.
- + *
- + * - transmitted
- + * Indicates if the packet has been transmitted. This flag is not cleared by
- + * the system, thus it indicates the first transmission only.
- + *
- + * - acked
- + * Indicates if the packet has been acknowledged by the client. There are no
- + * other guarantees given. For example, the packet may still be canceled
- + * and/or the completion may be triggered an error even though this bit is
- + * set. Rely on the status provided to the completion callback instead.
- + *
- + * - canceled
- + * Indicates if the packet has been canceled from the outside. There are no
- + * other guarantees given. Specifically, the packet may be completed by
- + * another part of the system before the cancellation attempts to complete it.
- + *
- + * >> General Notes <<
- + *
- + * - To avoid deadlocks, if both queue and pending locks are required, the
- + * pending lock must be acquired before the queue lock.
- + *
- + * - The packet priority must be accessed only while holding the queue lock.
- + *
- + * - The packet timestamp must be accessed only while holding the pending
- + * lock.
- + */
- +
- +/*
- + * SSH_PTL_MAX_PACKET_TRIES - Maximum transmission attempts for packet.
- + *
- + * Maximum number of transmission attempts per sequenced packet in case of
- + * time-outs. Must be smaller than 16. If the packet times out after this
- + * amount of tries, the packet will be completed with %-ETIMEDOUT as status
- + * code.
- + */
- +#define SSH_PTL_MAX_PACKET_TRIES 3
- +
- +/*
- + * SSH_PTL_TX_TIMEOUT - Packet transmission timeout.
- + *
- + * Timeout in jiffies for packet transmission via the underlying serial
- + * device. If transmitting the packet takes longer than this timeout, the
- + * packet will be completed with -ETIMEDOUT. It will not be re-submitted.
- + */
- +#define SSH_PTL_TX_TIMEOUT HZ
- +
- +/*
- + * SSH_PTL_PACKET_TIMEOUT - Packet response timeout.
- + *
- + * Timeout as ktime_t delta for ACKs. If we have not received an ACK in this
- + * time-frame after starting transmission, the packet will be re-submitted.
- + */
- +#define SSH_PTL_PACKET_TIMEOUT ms_to_ktime(1000)
- +
- +/*
- + * SSH_PTL_PACKET_TIMEOUT_RESOLUTION - Packet timeout granularity.
- + *
- + * Time-resolution for timeouts. Should be larger than one jiffy to avoid
- + * direct re-scheduling of reaper work_struct.
- + */
- +#define SSH_PTL_PACKET_TIMEOUT_RESOLUTION ms_to_ktime(max(2000 / HZ, 50))
- +
- +/*
- + * SSH_PTL_MAX_PENDING - Maximum number of pending packets.
- + *
- + * Maximum number of sequenced packets concurrently waiting for an ACK.
- + * Packets marked as blocking will not be transmitted while this limit is
- + * reached.
- + */
- +#define SSH_PTL_MAX_PENDING 1
- +
- +/*
- + * SSH_PTL_RX_BUF_LEN - Evaluation-buffer size in bytes.
- + */
- +#define SSH_PTL_RX_BUF_LEN 4096
- +
- +/*
- + * SSH_PTL_RX_FIFO_LEN - Fifo input-buffer size in bytes.
- + */
- +#define SSH_PTL_RX_FIFO_LEN 4096
- +
- +static void __ssh_ptl_packet_release(struct kref *kref)
- +{
- + struct ssh_packet *p = container_of(kref, struct ssh_packet, refcnt);
- +
- + ptl_dbg_cond(p->ptl, "ptl: releasing packet %p\n", p);
- + p->ops->release(p);
- +}
- +
- +/**
- + * ssh_packet_get() - Increment reference count of packet.
- + * @packet: The packet to increment the reference count of.
- + *
- + * Increments the reference count of the given packet. See ssh_packet_put()
- + * for the counter-part of this function.
- + *
- + * Return: Returns the packet provided as input.
- + */
- +struct ssh_packet *ssh_packet_get(struct ssh_packet *packet)
- +{
- + if (packet)
- + kref_get(&packet->refcnt);
- + return packet;
- +}
- +EXPORT_SYMBOL_GPL(ssh_packet_get);
- +
- +/**
- + * ssh_packet_put() - Decrement reference count of packet.
- + * @packet: The packet to decrement the reference count of.
- + *
- + * If the reference count reaches zero, the ``release`` callback specified in
- + * the packet's &struct ssh_packet_ops, i.e. ``packet->ops->release``, will be
- + * called.
- + *
- + * See ssh_packet_get() for the counter-part of this function.
- + */
- +void ssh_packet_put(struct ssh_packet *packet)
- +{
- + if (packet)
- + kref_put(&packet->refcnt, __ssh_ptl_packet_release);
- +}
- +EXPORT_SYMBOL_GPL(ssh_packet_put);
- +
- +static u8 ssh_packet_get_seq(struct ssh_packet *packet)
- +{
- + return packet->data.ptr[SSH_MSGOFFSET_FRAME(seq)];
- +}
- +
- +/**
- + * ssh_packet_init() - Initialize SSH packet.
- + * @packet: The packet to initialize.
- + * @type: Type-flags of the packet.
- + * @priority: Priority of the packet. See SSH_PACKET_PRIORITY() for details.
- + * @ops: Packet operations.
- + *
- + * Initializes the given SSH packet. Sets the transmission buffer pointer to
- + * %NULL and the transmission buffer length to zero. For data-type packets,
- + * this buffer has to be set separately via ssh_packet_set_data() before
- + * submission, and must contain a valid SSH message, i.e. frame with optional
- + * payload of any type.
- + */
- +void ssh_packet_init(struct ssh_packet *packet, unsigned long type,
- + u8 priority, const struct ssh_packet_ops *ops)
- +{
- + kref_init(&packet->refcnt);
- +
- + packet->ptl = NULL;
- + INIT_LIST_HEAD(&packet->queue_node);
- + INIT_LIST_HEAD(&packet->pending_node);
- +
- + packet->state = type & SSH_PACKET_FLAGS_TY_MASK;
- + packet->priority = priority;
- + packet->timestamp = KTIME_MAX;
- +
- + packet->data.ptr = NULL;
- + packet->data.len = 0;
- +
- + packet->ops = ops;
- +}
- +
- +/**
- + * ssh_ctrl_packet_alloc() - Allocate control packet.
- + * @packet: Where the pointer to the newly allocated packet should be stored.
- + * @buffer: The buffer corresponding to this packet.
- + * @flags: Flags used for allocation.
- + *
- + * Allocates a packet and corresponding transport buffer. Sets the packet's
- + * buffer reference to the allocated buffer. The packet must be freed via
- + * ssh_ctrl_packet_free(), which will also free the corresponding buffer. The
- + * corresponding buffer must not be freed separately. Intended to be used with
- + * %ssh_ptl_ctrl_packet_ops as packet operations.
- + *
- + * Return: Returns zero on success, %-ENOMEM if the allocation failed.
- + */
- +static int ssh_ctrl_packet_alloc(struct ssh_packet **packet,
- + struct ssam_span *buffer, gfp_t flags)
- +{
- + *packet = kzalloc(sizeof(**packet) + SSH_MSG_LEN_CTRL, flags);
- + if (!*packet)
- + return -ENOMEM;
- +
- + buffer->ptr = (u8 *)(*packet + 1);
- + buffer->len = SSH_MSG_LEN_CTRL;
- +
- + return 0;
- +}
- +
- +/**
- + * ssh_ctrl_packet_free() - Free control packet.
- + * @p: The packet to free.
- + */
- +static void ssh_ctrl_packet_free(struct ssh_packet *p)
- +{
- + kfree(p);
- +}
- +
- +static const struct ssh_packet_ops ssh_ptl_ctrl_packet_ops = {
- + .complete = NULL,
- + .release = ssh_ctrl_packet_free,
- +};
- +
- +static void ssh_ptl_timeout_reaper_mod(struct ssh_ptl *ptl, ktime_t now,
- + ktime_t expires)
- +{
- + unsigned long delta = msecs_to_jiffies(ktime_ms_delta(expires, now));
- + ktime_t aexp = ktime_add(expires, SSH_PTL_PACKET_TIMEOUT_RESOLUTION);
- +
- + spin_lock(&ptl->rtx_timeout.lock);
- +
- + /* Re-adjust / schedule reaper only if it is above resolution delta. */
- + if (ktime_before(aexp, ptl->rtx_timeout.expires)) {
- + ptl->rtx_timeout.expires = expires;
- + mod_delayed_work(system_wq, &ptl->rtx_timeout.reaper, delta);
- + }
- +
- + spin_unlock(&ptl->rtx_timeout.lock);
- +}
- +
- +/* Must be called with queue lock held. */
- +static void ssh_packet_next_try(struct ssh_packet *p)
- +{
- + u8 base = ssh_packet_priority_get_base(p->priority);
- + u8 try = ssh_packet_priority_get_try(p->priority);
- +
- + lockdep_assert_held(&p->ptl->queue.lock);
- +
- + p->priority = __SSH_PACKET_PRIORITY(base, try + 1);
- +}
- +
- +/* Must be called with queue lock held. */
- +static struct list_head *__ssh_ptl_queue_find_entrypoint(struct ssh_packet *p)
- +{
- + struct list_head *head;
- + struct ssh_packet *q;
- +
- + lockdep_assert_held(&p->ptl->queue.lock);
- +
- + /*
- + * We generally assume that there are less control (ACK/NAK) packets
- + * and re-submitted data packets as there are normal data packets (at
- + * least in situations in which many packets are queued; if there
- + * aren't many packets queued the decision on how to iterate should be
- + * basically irrelevant; the number of control/data packets is more or
- + * less limited via the maximum number of pending packets). Thus, when
- + * inserting a control or re-submitted data packet, (determined by
- + * their priority), we search from front to back. Normal data packets
- + * are, usually queued directly at the tail of the queue, so for those
- + * search from back to front.
- + */
- +
- + if (p->priority > SSH_PACKET_PRIORITY(DATA, 0)) {
- + list_for_each(head, &p->ptl->queue.head) {
- + q = list_entry(head, struct ssh_packet, queue_node);
- +
- + if (q->priority < p->priority)
- + break;
- + }
- + } else {
- + list_for_each_prev(head, &p->ptl->queue.head) {
- + q = list_entry(head, struct ssh_packet, queue_node);
- +
- + if (q->priority >= p->priority) {
- + head = head->next;
- + break;
- + }
- + }
- + }
- +
- + return head;
- +}
- +
- +/* Must be called with queue lock held. */
- +static int __ssh_ptl_queue_push(struct ssh_packet *packet)
- +{
- + struct ssh_ptl *ptl = packet->ptl;
- + struct list_head *head;
- +
- + lockdep_assert_held(&ptl->queue.lock);
- +
- + if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
- + return -ESHUTDOWN;
- +
- + /* Avoid further transitions when canceling/completing. */
- + if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state))
- + return -EINVAL;
- +
- + /* If this packet has already been queued, do not add it. */
- + if (test_and_set_bit(SSH_PACKET_SF_QUEUED_BIT, &packet->state))
- + return -EALREADY;
- +
- + head = __ssh_ptl_queue_find_entrypoint(packet);
- +
- + list_add_tail(&ssh_packet_get(packet)->queue_node, head);
- + return 0;
- +}
- +
- +static int ssh_ptl_queue_push(struct ssh_packet *packet)
- +{
- + int status;
- +
- + spin_lock(&packet->ptl->queue.lock);
- + status = __ssh_ptl_queue_push(packet);
- + spin_unlock(&packet->ptl->queue.lock);
- +
- + return status;
- +}
- +
- +static void ssh_ptl_queue_remove(struct ssh_packet *packet)
- +{
- + struct ssh_ptl *ptl = packet->ptl;
- +
- + spin_lock(&ptl->queue.lock);
- +
- + if (!test_and_clear_bit(SSH_PACKET_SF_QUEUED_BIT, &packet->state)) {
- + spin_unlock(&ptl->queue.lock);
- + return;
- + }
- +
- + list_del(&packet->queue_node);
- +
- + spin_unlock(&ptl->queue.lock);
- + ssh_packet_put(packet);
- +}
- +
- +static void ssh_ptl_pending_push(struct ssh_packet *p)
- +{
- + struct ssh_ptl *ptl = p->ptl;
- + const ktime_t timestamp = ktime_get_coarse_boottime();
- + const ktime_t timeout = ptl->rtx_timeout.timeout;
- +
- + /*
- + * Note: We can get the time for the timestamp before acquiring the
- + * lock as this is the only place we're setting it and this function
- + * is called only from the transmitter thread. Thus it is not possible
- + * to overwrite the timestamp with an outdated value below.
- + */
- +
- + spin_lock(&ptl->pending.lock);
- +
- + /* If we are canceling/completing this packet, do not add it. */
- + if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state)) {
- + spin_unlock(&ptl->pending.lock);
- + return;
- + }
- +
- + /*
- + * On re-submission, the packet has already been added the pending
- + * set. We still need to update the timestamp as the packet timeout is
- + * reset for each (re-)submission.
- + */
- + p->timestamp = timestamp;
- +
- + /* In case it is already pending (e.g. re-submission), do not add it. */
- + if (!test_and_set_bit(SSH_PACKET_SF_PENDING_BIT, &p->state)) {
- + atomic_inc(&ptl->pending.count);
- + list_add_tail(&ssh_packet_get(p)->pending_node, &ptl->pending.head);
- + }
- +
- + spin_unlock(&ptl->pending.lock);
- +
- + /* Arm/update timeout reaper. */
- + ssh_ptl_timeout_reaper_mod(ptl, timestamp, timestamp + timeout);
- +}
- +
- +static void ssh_ptl_pending_remove(struct ssh_packet *packet)
- +{
- + struct ssh_ptl *ptl = packet->ptl;
- +
- + spin_lock(&ptl->pending.lock);
- +
- + if (!test_and_clear_bit(SSH_PACKET_SF_PENDING_BIT, &packet->state)) {
- + spin_unlock(&ptl->pending.lock);
- + return;
- + }
- +
- + list_del(&packet->pending_node);
- + atomic_dec(&ptl->pending.count);
- +
- + spin_unlock(&ptl->pending.lock);
- +
- + ssh_packet_put(packet);
- +}
- +
- +/* Warning: Does not check/set "completed" bit. */
- +static void __ssh_ptl_complete(struct ssh_packet *p, int status)
- +{
- + struct ssh_ptl *ptl = READ_ONCE(p->ptl);
- +
- + ptl_dbg_cond(ptl, "ptl: completing packet %p (status: %d)\n", p, status);
- +
- + if (p->ops->complete)
- + p->ops->complete(p, status);
- +}
- +
- +static void ssh_ptl_remove_and_complete(struct ssh_packet *p, int status)
- +{
- + /*
- + * A call to this function should in general be preceded by
- + * set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->flags) to avoid re-adding the
- + * packet to the structures it's going to be removed from.
- + *
- + * The set_bit call does not need explicit memory barriers as the
- + * implicit barrier of the test_and_set_bit() call below ensure that the
- + * flag is visible before we actually attempt to remove the packet.
- + */
- +
- + if (test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
- + return;
- +
- + ssh_ptl_queue_remove(p);
- + ssh_ptl_pending_remove(p);
- +
- + __ssh_ptl_complete(p, status);
- +}
- +
- +static bool ssh_ptl_tx_can_process(struct ssh_packet *packet)
- +{
- + struct ssh_ptl *ptl = packet->ptl;
- +
- + if (test_bit(SSH_PACKET_TY_FLUSH_BIT, &packet->state))
- + return !atomic_read(&ptl->pending.count);
- +
- + /* We can always process non-blocking packets. */
- + if (!test_bit(SSH_PACKET_TY_BLOCKING_BIT, &packet->state))
- + return true;
- +
- + /* If we are already waiting for this packet, send it again. */
- + if (test_bit(SSH_PACKET_SF_PENDING_BIT, &packet->state))
- + return true;
- +
- + /* Otherwise: Check if we have the capacity to send. */
- + return atomic_read(&ptl->pending.count) < SSH_PTL_MAX_PENDING;
- +}
- +
- +static struct ssh_packet *ssh_ptl_tx_pop(struct ssh_ptl *ptl)
- +{
- + struct ssh_packet *packet = ERR_PTR(-ENOENT);
- + struct ssh_packet *p, *n;
- +
- + spin_lock(&ptl->queue.lock);
- + list_for_each_entry_safe(p, n, &ptl->queue.head, queue_node) {
- + /*
- + * If we are canceling or completing this packet, ignore it.
- + * It's going to be removed from this queue shortly.
- + */
- + if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
- + continue;
- +
- + /*
- + * Packets should be ordered non-blocking/to-be-resent first.
- + * If we cannot process this packet, assume that we can't
- + * process any following packet either and abort.
- + */
- + if (!ssh_ptl_tx_can_process(p)) {
- + packet = ERR_PTR(-EBUSY);
- + break;
- + }
- +
- + /*
- + * We are allowed to change the state now. Remove it from the
- + * queue and mark it as being transmitted.
- + */
- +
- + list_del(&p->queue_node);
- +
- + set_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &p->state);
- + /* Ensure that state never gets zero. */
- + smp_mb__before_atomic();
- + clear_bit(SSH_PACKET_SF_QUEUED_BIT, &p->state);
- +
- + /*
- + * Update number of tries. This directly influences the
- + * priority in case the packet is re-submitted (e.g. via
- + * timeout/NAK). Note that all reads and writes to the
- + * priority after the first submission are guarded by the
- + * queue lock.
- + */
- + ssh_packet_next_try(p);
- +
- + packet = p;
- + break;
- + }
- + spin_unlock(&ptl->queue.lock);
- +
- + return packet;
- +}
- +
- +static struct ssh_packet *ssh_ptl_tx_next(struct ssh_ptl *ptl)
- +{
- + struct ssh_packet *p;
- +
- + p = ssh_ptl_tx_pop(ptl);
- + if (IS_ERR(p))
- + return p;
- +
- + if (test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &p->state)) {
- + ptl_dbg(ptl, "ptl: transmitting sequenced packet %p\n", p);
- + ssh_ptl_pending_push(p);
- + } else {
- + ptl_dbg(ptl, "ptl: transmitting non-sequenced packet %p\n", p);
- + }
- +
- + return p;
- +}
- +
- +static void ssh_ptl_tx_compl_success(struct ssh_packet *packet)
- +{
- + struct ssh_ptl *ptl = packet->ptl;
- +
- + ptl_dbg(ptl, "ptl: successfully transmitted packet %p\n", packet);
- +
- + /* Transition state to "transmitted". */
- + set_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &packet->state);
- + /* Ensure that state never gets zero. */
- + smp_mb__before_atomic();
- + clear_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &packet->state);
- +
- + /* If the packet is unsequenced, we're done: Lock and complete. */
- + if (!test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &packet->state)) {
- + set_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state);
- + ssh_ptl_remove_and_complete(packet, 0);
- + }
- +
- + /*
- + * Notify that a packet transmission has finished. In general we're only
- + * waiting for one packet (if any), so wake_up_all should be fine.
- + */
- + wake_up_all(&ptl->tx.packet_wq);
- +}
- +
- +static void ssh_ptl_tx_compl_error(struct ssh_packet *packet, int status)
- +{
- + /* Transmission failure: Lock the packet and try to complete it. */
- + set_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state);
- + /* Ensure that state never gets zero. */
- + smp_mb__before_atomic();
- + clear_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &packet->state);
- +
- + ptl_err(packet->ptl, "ptl: transmission error: %d\n", status);
- + ptl_dbg(packet->ptl, "ptl: failed to transmit packet: %p\n", packet);
- +
- + ssh_ptl_remove_and_complete(packet, status);
- +
- + /*
- + * Notify that a packet transmission has finished. In general we're only
- + * waiting for one packet (if any), so wake_up_all should be fine.
- + */
- + wake_up_all(&packet->ptl->tx.packet_wq);
- +}
- +
- +static long ssh_ptl_tx_wait_packet(struct ssh_ptl *ptl)
- +{
- + int status;
- +
- + status = wait_for_completion_interruptible(&ptl->tx.thread_cplt_pkt);
- + reinit_completion(&ptl->tx.thread_cplt_pkt);
- +
- + /*
- + * Ensure completion is cleared before continuing to avoid lost update
- + * problems.
- + */
- + smp_mb__after_atomic();
- +
- + return status;
- +}
- +
- +static long ssh_ptl_tx_wait_transfer(struct ssh_ptl *ptl, long timeout)
- +{
- + long status;
- +
- + status = wait_for_completion_interruptible_timeout(&ptl->tx.thread_cplt_tx,
- + timeout);
- + reinit_completion(&ptl->tx.thread_cplt_tx);
- +
- + /*
- + * Ensure completion is cleared before continuing to avoid lost update
- + * problems.
- + */
- + smp_mb__after_atomic();
- +
- + return status;
- +}
- +
- +static int ssh_ptl_tx_packet(struct ssh_ptl *ptl, struct ssh_packet *packet)
- +{
- + long timeout = SSH_PTL_TX_TIMEOUT;
- + size_t offset = 0;
- +
- + /* Note: Flush-packets don't have any data. */
- + if (unlikely(!packet->data.ptr))
- + return 0;
- +
- + ptl_dbg(ptl, "tx: sending data (length: %zu)\n", packet->data.len);
- + print_hex_dump_debug("tx: ", DUMP_PREFIX_OFFSET, 16, 1,
- + packet->data.ptr, packet->data.len, false);
- +
- + do {
- + ssize_t status, len;
- + u8 *buf;
- +
- + buf = packet->data.ptr + offset;
- + len = packet->data.len - offset;
- +
- + status = serdev_device_write_buf(ptl->serdev, buf, len);
- + if (status < 0)
- + return status;
- +
- + if (status == len)
- + return 0;
- +
- + offset += status;
- +
- + timeout = ssh_ptl_tx_wait_transfer(ptl, timeout);
- + if (kthread_should_stop() || !atomic_read(&ptl->tx.running))
- + return -ESHUTDOWN;
- +
- + if (timeout < 0)
- + return -EINTR;
- +
- + if (timeout == 0)
- + return -ETIMEDOUT;
- + } while (true);
- +}
- +
- +static int ssh_ptl_tx_threadfn(void *data)
- +{
- + struct ssh_ptl *ptl = data;
- +
- + while (!kthread_should_stop() && atomic_read(&ptl->tx.running)) {
- + struct ssh_packet *packet;
- + int status;
- +
- + /* Try to get the next packet. */
- + packet = ssh_ptl_tx_next(ptl);
- +
- + /* If no packet can be processed, we are done. */
- + if (IS_ERR(packet)) {
- + ssh_ptl_tx_wait_packet(ptl);
- + continue;
- + }
- +
- + /* Transfer and complete packet. */
- + status = ssh_ptl_tx_packet(ptl, packet);
- + if (status)
- + ssh_ptl_tx_compl_error(packet, status);
- + else
- + ssh_ptl_tx_compl_success(packet);
- +
- + ssh_packet_put(packet);
- + }
- +
- + return 0;
- +}
- +
- +/**
- + * ssh_ptl_tx_wakeup_packet() - Wake up packet transmitter thread for new
- + * packet.
- + * @ptl: The packet transport layer.
- + *
- + * Wakes up the packet transmitter thread, notifying it that a new packet has
- + * arrived and is ready for transfer. If the packet transport layer has been
- + * shut down, calls to this function will be ignored.
- + */
- +static void ssh_ptl_tx_wakeup_packet(struct ssh_ptl *ptl)
- +{
- + if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
- + return;
- +
- + complete(&ptl->tx.thread_cplt_pkt);
- +}
- +
- +/**
- + * ssh_ptl_tx_start() - Start packet transmitter thread.
- + * @ptl: The packet transport layer.
- + *
- + * Return: Returns zero on success, a negative error code on failure.
- + */
- +int ssh_ptl_tx_start(struct ssh_ptl *ptl)
- +{
- + atomic_set_release(&ptl->tx.running, 1);
- +
- + ptl->tx.thread = kthread_run(ssh_ptl_tx_threadfn, ptl, "ssam_serial_hub-tx");
- + if (IS_ERR(ptl->tx.thread))
- + return PTR_ERR(ptl->tx.thread);
- +
- + return 0;
- +}
- +
- +/**
- + * ssh_ptl_tx_stop() - Stop packet transmitter thread.
- + * @ptl: The packet transport layer.
- + *
- + * Return: Returns zero on success, a negative error code on failure.
- + */
- +int ssh_ptl_tx_stop(struct ssh_ptl *ptl)
- +{
- + int status = 0;
- +
- + if (!IS_ERR_OR_NULL(ptl->tx.thread)) {
- + /* Tell thread to stop. */
- + atomic_set_release(&ptl->tx.running, 0);
- +
- + /*
- + * Wake up thread in case it is paused. Do not use wakeup
- + * helpers as this may be called when the shutdown bit has
- + * already been set.
- + */
- + complete(&ptl->tx.thread_cplt_pkt);
- + complete(&ptl->tx.thread_cplt_tx);
- +
- + /* Finally, wait for thread to stop. */
- + status = kthread_stop(ptl->tx.thread);
- + ptl->tx.thread = NULL;
- + }
- +
- + return status;
- +}
- +
- +static struct ssh_packet *ssh_ptl_ack_pop(struct ssh_ptl *ptl, u8 seq_id)
- +{
- + struct ssh_packet *packet = ERR_PTR(-ENOENT);
- + struct ssh_packet *p, *n;
- +
- + spin_lock(&ptl->pending.lock);
- + list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
- + /*
- + * We generally expect packets to be in order, so first packet
- + * to be added to pending is first to be sent, is first to be
- + * ACKed.
- + */
- + if (unlikely(ssh_packet_get_seq(p) != seq_id))
- + continue;
- +
- + /*
- + * In case we receive an ACK while handling a transmission
- + * error completion. The packet will be removed shortly.
- + */
- + if (unlikely(test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))) {
- + packet = ERR_PTR(-EPERM);
- + break;
- + }
- +
- + /*
- + * Mark the packet as ACKed and remove it from pending by
- + * removing its node and decrementing the pending counter.
- + */
- + set_bit(SSH_PACKET_SF_ACKED_BIT, &p->state);
- + /* Ensure that state never gets zero. */
- + smp_mb__before_atomic();
- + clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
- +
- + atomic_dec(&ptl->pending.count);
- + list_del(&p->pending_node);
- + packet = p;
- +
- + break;
- + }
- + spin_unlock(&ptl->pending.lock);
- +
- + return packet;
- +}
- +
- +static void ssh_ptl_wait_until_transmitted(struct ssh_packet *packet)
- +{
- + wait_event(packet->ptl->tx.packet_wq,
- + test_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &packet->state) ||
- + test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state));
- +}
- +
- +static void ssh_ptl_acknowledge(struct ssh_ptl *ptl, u8 seq)
- +{
- + struct ssh_packet *p;
- +
- + p = ssh_ptl_ack_pop(ptl, seq);
- + if (IS_ERR(p)) {
- + if (PTR_ERR(p) == -ENOENT) {
- + /*
- + * The packet has not been found in the set of pending
- + * packets.
- + */
- + ptl_warn(ptl, "ptl: received ACK for non-pending packet\n");
- + } else {
- + /*
- + * The packet is pending, but we are not allowed to take
- + * it because it has been locked.
- + */
- + WARN_ON(PTR_ERR(p) != -EPERM);
- + }
- + return;
- + }
- +
- + ptl_dbg(ptl, "ptl: received ACK for packet %p\n", p);
- +
- + /*
- + * It is possible that the packet has been transmitted, but the state
- + * has not been updated from "transmitting" to "transmitted" yet.
- + * In that case, we need to wait for this transition to occur in order
- + * to determine between success or failure.
- + *
- + * On transmission failure, the packet will be locked after this call.
- + * On success, the transmitted bit will be set.
- + */
- + ssh_ptl_wait_until_transmitted(p);
- +
- + /*
- + * The packet will already be locked in case of a transmission error or
- + * cancellation. Let the transmitter or cancellation issuer complete the
- + * packet.
- + */
- + if (unlikely(test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))) {
- + if (unlikely(!test_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &p->state)))
- + ptl_err(ptl, "ptl: received ACK before packet had been fully transmitted\n");
- +
- + ssh_packet_put(p);
- + return;
- + }
- +
- + ssh_ptl_remove_and_complete(p, 0);
- + ssh_packet_put(p);
- +
- + if (atomic_read(&ptl->pending.count) < SSH_PTL_MAX_PENDING)
- + ssh_ptl_tx_wakeup_packet(ptl);
- +}
- +
- +/**
- + * ssh_ptl_submit() - Submit a packet to the transport layer.
- + * @ptl: The packet transport layer to submit the packet to.
- + * @p: The packet to submit.
- + *
- + * Submits a new packet to the transport layer, queuing it to be sent. This
- + * function should not be used for re-submission.
- + *
- + * Return: Returns zero on success, %-EINVAL if a packet field is invalid or
- + * the packet has been canceled prior to submission, %-EALREADY if the packet
- + * has already been submitted, or %-ESHUTDOWN if the packet transport layer
- + * has been shut down.
- + */
- +int ssh_ptl_submit(struct ssh_ptl *ptl, struct ssh_packet *p)
- +{
- + struct ssh_ptl *ptl_old;
- + int status;
- +
- + /* Validate packet fields. */
- + if (test_bit(SSH_PACKET_TY_FLUSH_BIT, &p->state)) {
- + if (p->data.ptr || test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &p->state))
- + return -EINVAL;
- + } else if (!p->data.ptr) {
- + return -EINVAL;
- + }
- +
- + /*
- + * The ptl reference only gets set on or before the first submission.
- + * After the first submission, it has to be read-only.
- + *
- + * Note that ptl may already be set from upper-layer request
- + * submission, thus we cannot expect it to be NULL.
- + */
- + ptl_old = READ_ONCE(p->ptl);
- + if (!ptl_old)
- + WRITE_ONCE(p->ptl, ptl);
- + else if (WARN_ON(ptl_old != ptl))
- + return -EALREADY; /* Submitted on different PTL. */
- +
- + status = ssh_ptl_queue_push(p);
- + if (status)
- + return status;
- +
- + if (!test_bit(SSH_PACKET_TY_BLOCKING_BIT, &p->state) ||
- + (atomic_read(&ptl->pending.count) < SSH_PTL_MAX_PENDING))
- + ssh_ptl_tx_wakeup_packet(ptl);
- +
- + return 0;
- +}
- +
- +/*
- + * __ssh_ptl_resubmit() - Re-submit a packet to the transport layer.
- + * @packet: The packet to re-submit.
- + *
- + * Re-submits the given packet: Checks if it can be re-submitted and queues it
- + * if it can, resetting the packet timestamp in the process. Must be called
- + * with the pending lock held.
- + *
- + * Return: Returns %-ECANCELED if the packet has exceeded its number of tries,
- + * %-EINVAL if the packet has been locked, %-EALREADY if the packet is already
- + * on the queue, and %-ESHUTDOWN if the transmission layer has been shut down.
- + */
- +static int __ssh_ptl_resubmit(struct ssh_packet *packet)
- +{
- + int status;
- + u8 try;
- +
- + lockdep_assert_held(&packet->ptl->pending.lock);
- +
- + spin_lock(&packet->ptl->queue.lock);
- +
- + /* Check if the packet is out of tries. */
- + try = ssh_packet_priority_get_try(packet->priority);
- + if (try >= SSH_PTL_MAX_PACKET_TRIES) {
- + spin_unlock(&packet->ptl->queue.lock);
- + return -ECANCELED;
- + }
- +
- + status = __ssh_ptl_queue_push(packet);
- + if (status) {
- + /*
- + * An error here indicates that the packet has either already
- + * been queued, been locked, or the transport layer is being
- + * shut down. In all cases: Ignore the error.
- + */
- + spin_unlock(&packet->ptl->queue.lock);
- + return status;
- + }
- +
- + packet->timestamp = KTIME_MAX;
- +
- + spin_unlock(&packet->ptl->queue.lock);
- + return 0;
- +}
- +
- +static void ssh_ptl_resubmit_pending(struct ssh_ptl *ptl)
- +{
- + struct ssh_packet *p;
- + bool resub = false;
- +
- + /*
- + * Note: We deliberately do not remove/attempt to cancel and complete
- + * packets that are out of tires in this function. The packet will be
- + * eventually canceled and completed by the timeout. Removing the packet
- + * here could lead to overly eager cancellation if the packet has not
- + * been re-transmitted yet but the tries-counter already updated (i.e
- + * ssh_ptl_tx_next() removed the packet from the queue and updated the
- + * counter, but re-transmission for the last try has not actually
- + * started yet).
- + */
- +
- + spin_lock(&ptl->pending.lock);
- +
- + /* Re-queue all pending packets. */
- + list_for_each_entry(p, &ptl->pending.head, pending_node) {
- + /*
- + * Re-submission fails if the packet is out of tries, has been
- + * locked, is already queued, or the layer is being shut down.
- + * No need to re-schedule tx-thread in those cases.
- + */
- + if (!__ssh_ptl_resubmit(p))
- + resub = true;
- + }
- +
- + spin_unlock(&ptl->pending.lock);
- +
- + if (resub)
- + ssh_ptl_tx_wakeup_packet(ptl);
- +}
- +
- +/**
- + * ssh_ptl_cancel() - Cancel a packet.
- + * @p: The packet to cancel.
- + *
- + * Cancels a packet. There are no guarantees on when completion and release
- + * callbacks will be called. This may occur during execution of this function
- + * or may occur at any point later.
- + *
- + * Note that it is not guaranteed that the packet will actually be canceled if
- + * the packet is concurrently completed by another process. The only guarantee
- + * of this function is that the packet will be completed (with success,
- + * failure, or cancellation) and released from the transport layer in a
- + * reasonable time-frame.
- + *
- + * May be called before the packet has been submitted, in which case any later
- + * packet submission fails.
- + */
- +void ssh_ptl_cancel(struct ssh_packet *p)
- +{
- + if (test_and_set_bit(SSH_PACKET_SF_CANCELED_BIT, &p->state))
- + return;
- +
- + /*
- + * Lock packet and commit with memory barrier. If this packet has
- + * already been locked, it's going to be removed and completed by
- + * another party, which should have precedence.
- + */
- + if (test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
- + return;
- +
- + /*
- + * By marking the packet as locked and employing the implicit memory
- + * barrier of test_and_set_bit, we have guaranteed that, at this point,
- + * the packet cannot be added to the queue any more.
- + *
- + * In case the packet has never been submitted, packet->ptl is NULL. If
- + * the packet is currently being submitted, packet->ptl may be NULL or
- + * non-NULL. Due marking the packet as locked above and committing with
- + * the memory barrier, we have guaranteed that, if packet->ptl is NULL,
- + * the packet will never be added to the queue. If packet->ptl is
- + * non-NULL, we don't have any guarantees.
- + */
- +
- + if (READ_ONCE(p->ptl)) {
- + ssh_ptl_remove_and_complete(p, -ECANCELED);
- +
- + if (atomic_read(&p->ptl->pending.count) < SSH_PTL_MAX_PENDING)
- + ssh_ptl_tx_wakeup_packet(p->ptl);
- +
- + } else if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state)) {
- + __ssh_ptl_complete(p, -ECANCELED);
- + }
- +}
- +
- +/* Must be called with pending lock held */
- +static ktime_t ssh_packet_get_expiration(struct ssh_packet *p, ktime_t timeout)
- +{
- + lockdep_assert_held(&p->ptl->pending.lock);
- +
- + if (p->timestamp != KTIME_MAX)
- + return ktime_add(p->timestamp, timeout);
- + else
- + return KTIME_MAX;
- +}
- +
- +static void ssh_ptl_timeout_reap(struct work_struct *work)
- +{
- + struct ssh_ptl *ptl = to_ssh_ptl(work, rtx_timeout.reaper.work);
- + struct ssh_packet *p, *n;
- + LIST_HEAD(claimed);
- + ktime_t now = ktime_get_coarse_boottime();
- + ktime_t timeout = ptl->rtx_timeout.timeout;
- + ktime_t next = KTIME_MAX;
- + bool resub = false;
- + int status;
- +
- + /*
- + * Mark reaper as "not pending". This is done before checking any
- + * packets to avoid lost-update type problems.
- + */
- + spin_lock(&ptl->rtx_timeout.lock);
- + ptl->rtx_timeout.expires = KTIME_MAX;
- + spin_unlock(&ptl->rtx_timeout.lock);
- +
- + spin_lock(&ptl->pending.lock);
- +
- + list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
- + ktime_t expires = ssh_packet_get_expiration(p, timeout);
- +
- + /*
- + * Check if the timeout hasn't expired yet. Find out next
- + * expiration date to be handled after this run.
- + */
- + if (ktime_after(expires, now)) {
- + next = ktime_before(expires, next) ? expires : next;
- + continue;
- + }
- +
- + status = __ssh_ptl_resubmit(p);
- +
- + /*
- + * Re-submission fails if the packet is out of tries, has been
- + * locked, is already queued, or the layer is being shut down.
- + * No need to re-schedule tx-thread in those cases.
- + */
- + if (!status)
- + resub = true;
- +
- + /* Go to next packet if this packet is not out of tries. */
- + if (status != -ECANCELED)
- + continue;
- +
- + /* No more tries left: Cancel the packet. */
- +
- + /*
- + * If someone else has locked the packet already, don't use it
- + * and let the other party complete it.
- + */
- + if (test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
- + continue;
- +
- + /*
- + * We have now marked the packet as locked. Thus it cannot be
- + * added to the pending list again after we've removed it here.
- + * We can therefore re-use the pending_node of this packet
- + * temporarily.
- + */
- +
- + clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
- +
- + atomic_dec(&ptl->pending.count);
- + list_del(&p->pending_node);
- +
- + list_add_tail(&p->pending_node, &claimed);
- + }
- +
- + spin_unlock(&ptl->pending.lock);
- +
- + /* Cancel and complete the packet. */
- + list_for_each_entry_safe(p, n, &claimed, pending_node) {
- + if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state)) {
- + ssh_ptl_queue_remove(p);
- + __ssh_ptl_complete(p, -ETIMEDOUT);
- + }
- +
- + /*
- + * Drop the reference we've obtained by removing it from
- + * the pending set.
- + */
- + list_del(&p->pending_node);
- + ssh_packet_put(p);
- + }
- +
- + /* Ensure that reaper doesn't run again immediately. */
- + next = max(next, ktime_add(now, SSH_PTL_PACKET_TIMEOUT_RESOLUTION));
- + if (next != KTIME_MAX)
- + ssh_ptl_timeout_reaper_mod(ptl, now, next);
- +
- + if (resub)
- + ssh_ptl_tx_wakeup_packet(ptl);
- +}
- +
- +static bool ssh_ptl_rx_retransmit_check(struct ssh_ptl *ptl, u8 seq)
- +{
- + int i;
- +
- + /*
- + * Check if SEQ has been seen recently (i.e. packet was
- + * re-transmitted and we should ignore it).
- + */
- + for (i = 0; i < ARRAY_SIZE(ptl->rx.blocked.seqs); i++) {
- + if (likely(ptl->rx.blocked.seqs[i] != seq))
- + continue;
- +
- + ptl_dbg(ptl, "ptl: ignoring repeated data packet\n");
- + return true;
- + }
- +
- + /* Update list of blocked sequence IDs. */
- + ptl->rx.blocked.seqs[ptl->rx.blocked.offset] = seq;
- + ptl->rx.blocked.offset = (ptl->rx.blocked.offset + 1)
- + % ARRAY_SIZE(ptl->rx.blocked.seqs);
- +
- + return false;
- +}
- +
- +static void ssh_ptl_rx_dataframe(struct ssh_ptl *ptl,
- + const struct ssh_frame *frame,
- + const struct ssam_span *payload)
- +{
- + if (ssh_ptl_rx_retransmit_check(ptl, frame->seq))
- + return;
- +
- + ptl->ops.data_received(ptl, payload);
- +}
- +
- +static void ssh_ptl_send_ack(struct ssh_ptl *ptl, u8 seq)
- +{
- + struct ssh_packet *packet;
- + struct ssam_span buf;
- + struct msgbuf msgb;
- + int status;
- +
- + status = ssh_ctrl_packet_alloc(&packet, &buf, GFP_KERNEL);
- + if (status) {
- + ptl_err(ptl, "ptl: failed to allocate ACK packet\n");
- + return;
- + }
- +
- + ssh_packet_init(packet, 0, SSH_PACKET_PRIORITY(ACK, 0),
- + &ssh_ptl_ctrl_packet_ops);
- +
- + msgb_init(&msgb, buf.ptr, buf.len);
- + msgb_push_ack(&msgb, seq);
- + ssh_packet_set_data(packet, msgb.begin, msgb_bytes_used(&msgb));
- +
- + ssh_ptl_submit(ptl, packet);
- + ssh_packet_put(packet);
- +}
- +
- +static void ssh_ptl_send_nak(struct ssh_ptl *ptl)
- +{
- + struct ssh_packet *packet;
- + struct ssam_span buf;
- + struct msgbuf msgb;
- + int status;
- +
- + status = ssh_ctrl_packet_alloc(&packet, &buf, GFP_KERNEL);
- + if (status) {
- + ptl_err(ptl, "ptl: failed to allocate NAK packet\n");
- + return;
- + }
- +
- + ssh_packet_init(packet, 0, SSH_PACKET_PRIORITY(NAK, 0),
- + &ssh_ptl_ctrl_packet_ops);
- +
- + msgb_init(&msgb, buf.ptr, buf.len);
- + msgb_push_nak(&msgb);
- + ssh_packet_set_data(packet, msgb.begin, msgb_bytes_used(&msgb));
- +
- + ssh_ptl_submit(ptl, packet);
- + ssh_packet_put(packet);
- +}
- +
- +static size_t ssh_ptl_rx_eval(struct ssh_ptl *ptl, struct ssam_span *source)
- +{
- + struct ssh_frame *frame;
- + struct ssam_span payload;
- + struct ssam_span aligned;
- + bool syn_found;
- + int status;
- +
- + /* Find SYN. */
- + syn_found = sshp_find_syn(source, &aligned);
- +
- + if (unlikely(aligned.ptr - source->ptr) > 0) {
- + ptl_warn(ptl, "rx: parser: invalid start of frame, skipping\n");
- +
- + /*
- + * Notes:
- + * - This might send multiple NAKs in case the communication
- + * starts with an invalid SYN and is broken down into multiple
- + * pieces. This should generally be handled fine, we just
- + * might receive duplicate data in this case, which is
- + * detected when handling data frames.
- + * - This path will also be executed on invalid CRCs: When an
- + * invalid CRC is encountered, the code below will skip data
- + * until directly after the SYN. This causes the search for
- + * the next SYN, which is generally not placed directly after
- + * the last one.
- + *
- + * Open question: Should we send this in case of invalid
- + * payload CRCs if the frame-type is non-sequential (current
- + * implementation) or should we drop that frame without
- + * telling the EC?
- + */
- + ssh_ptl_send_nak(ptl);
- + }
- +
- + if (unlikely(!syn_found))
- + return aligned.ptr - source->ptr;
- +
- + /* Parse and validate frame. */
- + status = sshp_parse_frame(&ptl->serdev->dev, &aligned, &frame, &payload,
- + SSH_PTL_RX_BUF_LEN);
- + if (status) /* Invalid frame: skip to next SYN. */
- + return aligned.ptr - source->ptr + sizeof(u16);
- + if (!frame) /* Not enough data. */
- + return aligned.ptr - source->ptr;
- +
- + switch (frame->type) {
- + case SSH_FRAME_TYPE_ACK:
- + ssh_ptl_acknowledge(ptl, frame->seq);
- + break;
- +
- + case SSH_FRAME_TYPE_NAK:
- + ssh_ptl_resubmit_pending(ptl);
- + break;
- +
- + case SSH_FRAME_TYPE_DATA_SEQ:
- + ssh_ptl_send_ack(ptl, frame->seq);
- + fallthrough;
- +
- + case SSH_FRAME_TYPE_DATA_NSQ:
- + ssh_ptl_rx_dataframe(ptl, frame, &payload);
- + break;
- +
- + default:
- + ptl_warn(ptl, "ptl: received frame with unknown type %#04x\n",
- + frame->type);
- + break;
- + }
- +
- + return aligned.ptr - source->ptr + SSH_MESSAGE_LENGTH(frame->len);
- +}
- +
- +static int ssh_ptl_rx_threadfn(void *data)
- +{
- + struct ssh_ptl *ptl = data;
- +
- + while (true) {
- + struct ssam_span span;
- + size_t offs = 0;
- + size_t n;
- +
- + wait_event_interruptible(ptl->rx.wq,
- + !kfifo_is_empty(&ptl->rx.fifo) ||
- + kthread_should_stop());
- + if (kthread_should_stop())
- + break;
- +
- + /* Copy from fifo to evaluation buffer. */
- + n = sshp_buf_read_from_fifo(&ptl->rx.buf, &ptl->rx.fifo);
- +
- + ptl_dbg(ptl, "rx: received data (size: %zu)\n", n);
- + print_hex_dump_debug("rx: ", DUMP_PREFIX_OFFSET, 16, 1,
- + ptl->rx.buf.ptr + ptl->rx.buf.len - n,
- + n, false);
- +
- + /* Parse until we need more bytes or buffer is empty. */
- + while (offs < ptl->rx.buf.len) {
- + sshp_buf_span_from(&ptl->rx.buf, offs, &span);
- + n = ssh_ptl_rx_eval(ptl, &span);
- + if (n == 0)
- + break; /* Need more bytes. */
- +
- + offs += n;
- + }
- +
- + /* Throw away the evaluated parts. */
- + sshp_buf_drop(&ptl->rx.buf, offs);
- + }
- +
- + return 0;
- +}
- +
- +static void ssh_ptl_rx_wakeup(struct ssh_ptl *ptl)
- +{
- + wake_up(&ptl->rx.wq);
- +}
- +
- +/**
- + * ssh_ptl_rx_start() - Start packet transport layer receiver thread.
- + * @ptl: The packet transport layer.
- + *
- + * Return: Returns zero on success, a negative error code on failure.
- + */
- +int ssh_ptl_rx_start(struct ssh_ptl *ptl)
- +{
- + if (ptl->rx.thread)
- + return 0;
- +
- + ptl->rx.thread = kthread_run(ssh_ptl_rx_threadfn, ptl,
- + "ssam_serial_hub-rx");
- + if (IS_ERR(ptl->rx.thread))
- + return PTR_ERR(ptl->rx.thread);
- +
- + return 0;
- +}
- +
- +/**
- + * ssh_ptl_rx_stop() - Stop packet transport layer receiver thread.
- + * @ptl: The packet transport layer.
- + *
- + * Return: Returns zero on success, a negative error code on failure.
- + */
- +int ssh_ptl_rx_stop(struct ssh_ptl *ptl)
- +{
- + int status = 0;
- +
- + if (ptl->rx.thread) {
- + status = kthread_stop(ptl->rx.thread);
- + ptl->rx.thread = NULL;
- + }
- +
- + return status;
- +}
- +
- +/**
- + * ssh_ptl_rx_rcvbuf() - Push data from lower-layer transport to the packet
- + * layer.
- + * @ptl: The packet transport layer.
- + * @buf: Pointer to the data to push to the layer.
- + * @n: Size of the data to push to the layer, in bytes.
- + *
- + * Pushes data from a lower-layer transport to the receiver fifo buffer of the
- + * packet layer and notifies the receiver thread. Calls to this function are
- + * ignored once the packet layer has been shut down.
- + *
- + * Return: Returns the number of bytes transferred (positive or zero) on
- + * success. Returns %-ESHUTDOWN if the packet layer has been shut down.
- + */
- +int ssh_ptl_rx_rcvbuf(struct ssh_ptl *ptl, const u8 *buf, size_t n)
- +{
- + int used;
- +
- + if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
- + return -ESHUTDOWN;
- +
- + used = kfifo_in(&ptl->rx.fifo, buf, n);
- + if (used)
- + ssh_ptl_rx_wakeup(ptl);
- +
- + return used;
- +}
- +
- +/**
- + * ssh_ptl_shutdown() - Shut down the packet transport layer.
- + * @ptl: The packet transport layer.
- + *
- + * Shuts down the packet transport layer, removing and canceling all queued
- + * and pending packets. Packets canceled by this operation will be completed
- + * with %-ESHUTDOWN as status. Receiver and transmitter threads will be
- + * stopped.
- + *
- + * As a result of this function, the transport layer will be marked as shut
- + * down. Submission of packets after the transport layer has been shut down
- + * will fail with %-ESHUTDOWN.
- + */
- +void ssh_ptl_shutdown(struct ssh_ptl *ptl)
- +{
- + LIST_HEAD(complete_q);
- + LIST_HEAD(complete_p);
- + struct ssh_packet *p, *n;
- + int status;
- +
- + /* Ensure that no new packets (including ACK/NAK) can be submitted. */
- + set_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state);
- + /*
- + * Ensure that the layer gets marked as shut-down before actually
- + * stopping it. In combination with the check in ssh_ptl_queue_push(),
- + * this guarantees that no new packets can be added and all already
- + * queued packets are properly canceled. In combination with the check
- + * in ssh_ptl_rx_rcvbuf(), this guarantees that received data is
- + * properly cut off.
- + */
- + smp_mb__after_atomic();
- +
- + status = ssh_ptl_rx_stop(ptl);
- + if (status)
- + ptl_err(ptl, "ptl: failed to stop receiver thread\n");
- +
- + status = ssh_ptl_tx_stop(ptl);
- + if (status)
- + ptl_err(ptl, "ptl: failed to stop transmitter thread\n");
- +
- + cancel_delayed_work_sync(&ptl->rtx_timeout.reaper);
- +
- + /*
- + * At this point, all threads have been stopped. This means that the
- + * only references to packets from inside the system are in the queue
- + * and pending set.
- + *
- + * Note: We still need locks here because someone could still be
- + * canceling packets.
- + *
- + * Note 2: We can re-use queue_node (or pending_node) if we mark the
- + * packet as locked an then remove it from the queue (or pending set
- + * respectively). Marking the packet as locked avoids re-queuing
- + * (which should already be prevented by having stopped the treads...)
- + * and not setting QUEUED_BIT (or PENDING_BIT) prevents removal from a
- + * new list via other threads (e.g. cancellation).
- + *
- + * Note 3: There may be overlap between complete_p and complete_q.
- + * This is handled via test_and_set_bit() on the "completed" flag
- + * (also handles cancellation).
- + */
- +
- + /* Mark queued packets as locked and move them to complete_q. */
- + spin_lock(&ptl->queue.lock);
- + list_for_each_entry_safe(p, n, &ptl->queue.head, queue_node) {
- + set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state);
- + /* Ensure that state does not get zero. */
- + smp_mb__before_atomic();
- + clear_bit(SSH_PACKET_SF_QUEUED_BIT, &p->state);
- +
- + list_del(&p->queue_node);
- + list_add_tail(&p->queue_node, &complete_q);
- + }
- + spin_unlock(&ptl->queue.lock);
- +
- + /* Mark pending packets as locked and move them to complete_p. */
- + spin_lock(&ptl->pending.lock);
- + list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
- + set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state);
- + /* Ensure that state does not get zero. */
- + smp_mb__before_atomic();
- + clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
- +
- + list_del(&p->pending_node);
- + list_add_tail(&p->pending_node, &complete_q);
- + }
- + atomic_set(&ptl->pending.count, 0);
- + spin_unlock(&ptl->pending.lock);
- +
- + /* Complete and drop packets on complete_q. */
- + list_for_each_entry(p, &complete_q, queue_node) {
- + if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
- + __ssh_ptl_complete(p, -ESHUTDOWN);
- +
- + ssh_packet_put(p);
- + }
- +
- + /* Complete and drop packets on complete_p. */
- + list_for_each_entry(p, &complete_p, pending_node) {
- + if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
- + __ssh_ptl_complete(p, -ESHUTDOWN);
- +
- + ssh_packet_put(p);
- + }
- +
- + /*
- + * At this point we have guaranteed that the system doesn't reference
- + * any packets any more.
- + */
- +}
- +
- +/**
- + * ssh_ptl_init() - Initialize packet transport layer.
- + * @ptl: The packet transport layer to initialize.
- + * @serdev: The underlying serial device, i.e. the lower-level transport.
- + * @ops: Packet layer operations.
- + *
- + * Initializes the given packet transport layer. Transmitter and receiver
- + * threads must be started separately via ssh_ptl_tx_start() and
- + * ssh_ptl_rx_start(), after the packet-layer has been initialized and the
- + * lower-level transport layer has been set up.
- + *
- + * Return: Returns zero on success and a nonzero error code on failure.
- + */
- +int ssh_ptl_init(struct ssh_ptl *ptl, struct serdev_device *serdev,
- + struct ssh_ptl_ops *ops)
- +{
- + int i, status;
- +
- + ptl->serdev = serdev;
- + ptl->state = 0;
- +
- + spin_lock_init(&ptl->queue.lock);
- + INIT_LIST_HEAD(&ptl->queue.head);
- +
- + spin_lock_init(&ptl->pending.lock);
- + INIT_LIST_HEAD(&ptl->pending.head);
- + atomic_set_release(&ptl->pending.count, 0);
- +
- + ptl->tx.thread = NULL;
- + atomic_set(&ptl->tx.running, 0);
- + init_completion(&ptl->tx.thread_cplt_pkt);
- + init_completion(&ptl->tx.thread_cplt_tx);
- + init_waitqueue_head(&ptl->tx.packet_wq);
- +
- + ptl->rx.thread = NULL;
- + init_waitqueue_head(&ptl->rx.wq);
- +
- + spin_lock_init(&ptl->rtx_timeout.lock);
- + ptl->rtx_timeout.timeout = SSH_PTL_PACKET_TIMEOUT;
- + ptl->rtx_timeout.expires = KTIME_MAX;
- + INIT_DELAYED_WORK(&ptl->rtx_timeout.reaper, ssh_ptl_timeout_reap);
- +
- + ptl->ops = *ops;
- +
- + /* Initialize list of recent/blocked SEQs with invalid sequence IDs. */
- + for (i = 0; i < ARRAY_SIZE(ptl->rx.blocked.seqs); i++)
- + ptl->rx.blocked.seqs[i] = U16_MAX;
- + ptl->rx.blocked.offset = 0;
- +
- + status = kfifo_alloc(&ptl->rx.fifo, SSH_PTL_RX_FIFO_LEN, GFP_KERNEL);
- + if (status)
- + return status;
- +
- + status = sshp_buf_alloc(&ptl->rx.buf, SSH_PTL_RX_BUF_LEN, GFP_KERNEL);
- + if (status)
- + kfifo_free(&ptl->rx.fifo);
- +
- + return status;
- +}
- +
- +/**
- + * ssh_ptl_destroy() - Deinitialize packet transport layer.
- + * @ptl: The packet transport layer to deinitialize.
- + *
- + * Deinitializes the given packet transport layer and frees resources
- + * associated with it. If receiver and/or transmitter threads have been
- + * started, the layer must first be shut down via ssh_ptl_shutdown() before
- + * this function can be called.
- + */
- +void ssh_ptl_destroy(struct ssh_ptl *ptl)
- +{
- + kfifo_free(&ptl->rx.fifo);
- + sshp_buf_free(&ptl->rx.buf);
- +}
- diff --git a/drivers/platform/surface/aggregator/ssh_packet_layer.h b/drivers/platform/surface/aggregator/ssh_packet_layer.h
- new file mode 100644
- index 000000000000..058f111292ca
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/ssh_packet_layer.h
- @@ -0,0 +1,187 @@
- +/* SPDX-License-Identifier: GPL-2.0+ */
- +/*
- + * SSH packet transport layer.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#ifndef _SURFACE_AGGREGATOR_SSH_PACKET_LAYER_H
- +#define _SURFACE_AGGREGATOR_SSH_PACKET_LAYER_H
- +
- +#include <linux/atomic.h>
- +#include <linux/kfifo.h>
- +#include <linux/ktime.h>
- +#include <linux/list.h>
- +#include <linux/serdev.h>
- +#include <linux/spinlock.h>
- +#include <linux/types.h>
- +#include <linux/wait.h>
- +#include <linux/workqueue.h>
- +
- +#include <linux/surface_aggregator/serial_hub.h>
- +#include "ssh_parser.h"
- +
- +/**
- + * enum ssh_ptl_state_flags - State-flags for &struct ssh_ptl.
- + *
- + * @SSH_PTL_SF_SHUTDOWN_BIT:
- + * Indicates that the packet transport layer has been shut down or is
- + * being shut down and should not accept any new packets/data.
- + */
- +enum ssh_ptl_state_flags {
- + SSH_PTL_SF_SHUTDOWN_BIT,
- +};
- +
- +/**
- + * struct ssh_ptl_ops - Callback operations for packet transport layer.
- + * @data_received: Function called when a data-packet has been received. Both,
- + * the packet layer on which the packet has been received and
- + * the packet's payload data are provided to this function.
- + */
- +struct ssh_ptl_ops {
- + void (*data_received)(struct ssh_ptl *p, const struct ssam_span *data);
- +};
- +
- +/**
- + * struct ssh_ptl - SSH packet transport layer.
- + * @serdev: Serial device providing the underlying data transport.
- + * @state: State(-flags) of the transport layer.
- + * @queue: Packet submission queue.
- + * @queue.lock: Lock for modifying the packet submission queue.
- + * @queue.head: List-head of the packet submission queue.
- + * @pending: Set/list of pending packets.
- + * @pending.lock: Lock for modifying the pending set.
- + * @pending.head: List-head of the pending set/list.
- + * @pending.count: Number of currently pending packets.
- + * @tx: Transmitter subsystem.
- + * @tx.running: Flag indicating (desired) transmitter thread state.
- + * @tx.thread: Transmitter thread.
- + * @tx.thread_cplt_tx: Completion for transmitter thread waiting on transfer.
- + * @tx.thread_cplt_pkt: Completion for transmitter thread waiting on packets.
- + * @tx.packet_wq: Waitqueue-head for packet transmit completion.
- + * @rx: Receiver subsystem.
- + * @rx.thread: Receiver thread.
- + * @rx.wq: Waitqueue-head for receiver thread.
- + * @rx.fifo: Buffer for receiving data/pushing data to receiver thread.
- + * @rx.buf: Buffer for evaluating data on receiver thread.
- + * @rx.blocked: List of recent/blocked sequence IDs to detect retransmission.
- + * @rx.blocked.seqs: Array of blocked sequence IDs.
- + * @rx.blocked.offset: Offset indicating where a new ID should be inserted.
- + * @rtx_timeout: Retransmission timeout subsystem.
- + * @rtx_timeout.lock: Lock for modifying the retransmission timeout reaper.
- + * @rtx_timeout.timeout: Timeout interval for retransmission.
- + * @rtx_timeout.expires: Time specifying when the reaper work is next scheduled.
- + * @rtx_timeout.reaper: Work performing timeout checks and subsequent actions.
- + * @ops: Packet layer operations.
- + */
- +struct ssh_ptl {
- + struct serdev_device *serdev;
- + unsigned long state;
- +
- + struct {
- + spinlock_t lock;
- + struct list_head head;
- + } queue;
- +
- + struct {
- + spinlock_t lock;
- + struct list_head head;
- + atomic_t count;
- + } pending;
- +
- + struct {
- + atomic_t running;
- + struct task_struct *thread;
- + struct completion thread_cplt_tx;
- + struct completion thread_cplt_pkt;
- + struct wait_queue_head packet_wq;
- + } tx;
- +
- + struct {
- + struct task_struct *thread;
- + struct wait_queue_head wq;
- + struct kfifo fifo;
- + struct sshp_buf buf;
- +
- + struct {
- + u16 seqs[8];
- + u16 offset;
- + } blocked;
- + } rx;
- +
- + struct {
- + spinlock_t lock;
- + ktime_t timeout;
- + ktime_t expires;
- + struct delayed_work reaper;
- + } rtx_timeout;
- +
- + struct ssh_ptl_ops ops;
- +};
- +
- +#define __ssam_prcond(func, p, fmt, ...) \
- + do { \
- + typeof(p) __p = (p); \
- + \
- + if (__p) \
- + func(__p, fmt, ##__VA_ARGS__); \
- + } while (0)
- +
- +#define ptl_dbg(p, fmt, ...) dev_dbg(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
- +#define ptl_info(p, fmt, ...) dev_info(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
- +#define ptl_warn(p, fmt, ...) dev_warn(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
- +#define ptl_err(p, fmt, ...) dev_err(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
- +#define ptl_dbg_cond(p, fmt, ...) __ssam_prcond(ptl_dbg, p, fmt, ##__VA_ARGS__)
- +
- +#define to_ssh_ptl(ptr, member) \
- + container_of(ptr, struct ssh_ptl, member)
- +
- +int ssh_ptl_init(struct ssh_ptl *ptl, struct serdev_device *serdev,
- + struct ssh_ptl_ops *ops);
- +
- +void ssh_ptl_destroy(struct ssh_ptl *ptl);
- +
- +/**
- + * ssh_ptl_get_device() - Get device associated with packet transport layer.
- + * @ptl: The packet transport layer.
- + *
- + * Return: Returns the device on which the given packet transport layer builds
- + * upon.
- + */
- +static inline struct device *ssh_ptl_get_device(struct ssh_ptl *ptl)
- +{
- + return ptl->serdev ? &ptl->serdev->dev : NULL;
- +}
- +
- +int ssh_ptl_tx_start(struct ssh_ptl *ptl);
- +int ssh_ptl_tx_stop(struct ssh_ptl *ptl);
- +int ssh_ptl_rx_start(struct ssh_ptl *ptl);
- +int ssh_ptl_rx_stop(struct ssh_ptl *ptl);
- +void ssh_ptl_shutdown(struct ssh_ptl *ptl);
- +
- +int ssh_ptl_submit(struct ssh_ptl *ptl, struct ssh_packet *p);
- +void ssh_ptl_cancel(struct ssh_packet *p);
- +
- +int ssh_ptl_rx_rcvbuf(struct ssh_ptl *ptl, const u8 *buf, size_t n);
- +
- +/**
- + * ssh_ptl_tx_wakeup_transfer() - Wake up packet transmitter thread for
- + * transfer.
- + * @ptl: The packet transport layer.
- + *
- + * Wakes up the packet transmitter thread, notifying it that the underlying
- + * transport has more space for data to be transmitted. If the packet
- + * transport layer has been shut down, calls to this function will be ignored.
- + */
- +static inline void ssh_ptl_tx_wakeup_transfer(struct ssh_ptl *ptl)
- +{
- + if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
- + return;
- +
- + complete(&ptl->tx.thread_cplt_tx);
- +}
- +
- +void ssh_packet_init(struct ssh_packet *packet, unsigned long type,
- + u8 priority, const struct ssh_packet_ops *ops);
- +
- +#endif /* _SURFACE_AGGREGATOR_SSH_PACKET_LAYER_H */
- diff --git a/drivers/platform/surface/aggregator/ssh_parser.c b/drivers/platform/surface/aggregator/ssh_parser.c
- new file mode 100644
- index 000000000000..e2dead8de94a
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/ssh_parser.c
- @@ -0,0 +1,228 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * SSH message parser.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <asm/unaligned.h>
- +#include <linux/compiler.h>
- +#include <linux/device.h>
- +#include <linux/types.h>
- +
- +#include <linux/surface_aggregator/serial_hub.h>
- +#include "ssh_parser.h"
- +
- +/**
- + * sshp_validate_crc() - Validate a CRC in raw message data.
- + * @src: The span of data over which the CRC should be computed.
- + * @crc: The pointer to the expected u16 CRC value.
- + *
- + * Computes the CRC of the provided data span (@src), compares it to the CRC
- + * stored at the given address (@crc), and returns the result of this
- + * comparison, i.e. %true if equal. This function is intended to run on raw
- + * input/message data.
- + *
- + * Return: Returns %true if the computed CRC matches the stored CRC, %false
- + * otherwise.
- + */
- +static bool sshp_validate_crc(const struct ssam_span *src, const u8 *crc)
- +{
- + u16 actual = ssh_crc(src->ptr, src->len);
- + u16 expected = get_unaligned_le16(crc);
- +
- + return actual == expected;
- +}
- +
- +/**
- + * sshp_starts_with_syn() - Check if the given data starts with SSH SYN bytes.
- + * @src: The data span to check the start of.
- + */
- +static bool sshp_starts_with_syn(const struct ssam_span *src)
- +{
- + return src->len >= 2 && get_unaligned_le16(src->ptr) == SSH_MSG_SYN;
- +}
- +
- +/**
- + * sshp_find_syn() - Find SSH SYN bytes in the given data span.
- + * @src: The data span to search in.
- + * @rem: The span (output) indicating the remaining data, starting with SSH
- + * SYN bytes, if found.
- + *
- + * Search for SSH SYN bytes in the given source span. If found, set the @rem
- + * span to the remaining data, starting with the first SYN bytes and capped by
- + * the source span length, and return %true. This function does not copy any
- + * data, but rather only sets pointers to the respective start addresses and
- + * length values.
- + *
- + * If no SSH SYN bytes could be found, set the @rem span to the zero-length
- + * span at the end of the source span and return %false.
- + *
- + * If partial SSH SYN bytes could be found at the end of the source span, set
- + * the @rem span to cover these partial SYN bytes, capped by the end of the
- + * source span, and return %false. This function should then be re-run once
- + * more data is available.
- + *
- + * Return: Returns %true if a complete SSH SYN sequence could be found,
- + * %false otherwise.
- + */
- +bool sshp_find_syn(const struct ssam_span *src, struct ssam_span *rem)
- +{
- + size_t i;
- +
- + for (i = 0; i < src->len - 1; i++) {
- + if (likely(get_unaligned_le16(src->ptr + i) == SSH_MSG_SYN)) {
- + rem->ptr = src->ptr + i;
- + rem->len = src->len - i;
- + return true;
- + }
- + }
- +
- + if (unlikely(src->ptr[src->len - 1] == (SSH_MSG_SYN & 0xff))) {
- + rem->ptr = src->ptr + src->len - 1;
- + rem->len = 1;
- + return false;
- + }
- +
- + rem->ptr = src->ptr + src->len;
- + rem->len = 0;
- + return false;
- +}
- +
- +/**
- + * sshp_parse_frame() - Parse SSH frame.
- + * @dev: The device used for logging.
- + * @source: The source to parse from.
- + * @frame: The parsed frame (output).
- + * @payload: The parsed payload (output).
- + * @maxlen: The maximum supported message length.
- + *
- + * Parses and validates a SSH frame, including its payload, from the given
- + * source. Sets the provided @frame pointer to the start of the frame and
- + * writes the limits of the frame payload to the provided @payload span
- + * pointer.
- + *
- + * This function does not copy any data, but rather only validates the message
- + * data and sets pointers (and length values) to indicate the respective parts.
- + *
- + * If no complete SSH frame could be found, the frame pointer will be set to
- + * the %NULL pointer and the payload span will be set to the null span (start
- + * pointer %NULL, size zero).
- + *
- + * Return: Returns zero on success or if the frame is incomplete, %-ENOMSG if
- + * the start of the message is invalid, %-EBADMSG if any (frame-header or
- + * payload) CRC is invalid, or %-EMSGSIZE if the SSH message is bigger than
- + * the maximum message length specified in the @maxlen parameter.
- + */
- +int sshp_parse_frame(const struct device *dev, const struct ssam_span *source,
- + struct ssh_frame **frame, struct ssam_span *payload,
- + size_t maxlen)
- +{
- + struct ssam_span sf;
- + struct ssam_span sp;
- +
- + /* Initialize output. */
- + *frame = NULL;
- + payload->ptr = NULL;
- + payload->len = 0;
- +
- + if (!sshp_starts_with_syn(source)) {
- + dev_warn(dev, "rx: parser: invalid start of frame\n");
- + return -ENOMSG;
- + }
- +
- + /* Check for minimum packet length. */
- + if (unlikely(source->len < SSH_MESSAGE_LENGTH(0))) {
- + dev_dbg(dev, "rx: parser: not enough data for frame\n");
- + return 0;
- + }
- +
- + /* Pin down frame. */
- + sf.ptr = source->ptr + sizeof(u16);
- + sf.len = sizeof(struct ssh_frame);
- +
- + /* Validate frame CRC. */
- + if (unlikely(!sshp_validate_crc(&sf, sf.ptr + sf.len))) {
- + dev_warn(dev, "rx: parser: invalid frame CRC\n");
- + return -EBADMSG;
- + }
- +
- + /* Ensure packet does not exceed maximum length. */
- + sp.len = get_unaligned_le16(&((struct ssh_frame *)sf.ptr)->len);
- + if (unlikely(SSH_MESSAGE_LENGTH(sp.len) > maxlen)) {
- + dev_warn(dev, "rx: parser: frame too large: %llu bytes\n",
- + SSH_MESSAGE_LENGTH(sp.len));
- + return -EMSGSIZE;
- + }
- +
- + /* Pin down payload. */
- + sp.ptr = sf.ptr + sf.len + sizeof(u16);
- +
- + /* Check for frame + payload length. */
- + if (source->len < SSH_MESSAGE_LENGTH(sp.len)) {
- + dev_dbg(dev, "rx: parser: not enough data for payload\n");
- + return 0;
- + }
- +
- + /* Validate payload CRC. */
- + if (unlikely(!sshp_validate_crc(&sp, sp.ptr + sp.len))) {
- + dev_warn(dev, "rx: parser: invalid payload CRC\n");
- + return -EBADMSG;
- + }
- +
- + *frame = (struct ssh_frame *)sf.ptr;
- + *payload = sp;
- +
- + dev_dbg(dev, "rx: parser: valid frame found (type: %#04x, len: %u)\n",
- + (*frame)->type, (*frame)->len);
- +
- + return 0;
- +}
- +
- +/**
- + * sshp_parse_command() - Parse SSH command frame payload.
- + * @dev: The device used for logging.
- + * @source: The source to parse from.
- + * @command: The parsed command (output).
- + * @command_data: The parsed command data/payload (output).
- + *
- + * Parses and validates a SSH command frame payload. Sets the @command pointer
- + * to the command header and the @command_data span to the command data (i.e.
- + * payload of the command). This will result in a zero-length span if the
- + * command does not have any associated data/payload. This function does not
- + * check the frame-payload-type field, which should be checked by the caller
- + * before calling this function.
- + *
- + * The @source parameter should be the complete frame payload, e.g. returned
- + * by the sshp_parse_frame() command.
- + *
- + * This function does not copy any data, but rather only validates the frame
- + * payload data and sets pointers (and length values) to indicate the
- + * respective parts.
- + *
- + * Return: Returns zero on success or %-ENOMSG if @source does not represent a
- + * valid command-type frame payload, i.e. is too short.
- + */
- +int sshp_parse_command(const struct device *dev, const struct ssam_span *source,
- + struct ssh_command **command,
- + struct ssam_span *command_data)
- +{
- + /* Check for minimum length. */
- + if (unlikely(source->len < sizeof(struct ssh_command))) {
- + *command = NULL;
- + command_data->ptr = NULL;
- + command_data->len = 0;
- +
- + dev_err(dev, "rx: parser: command payload is too short\n");
- + return -ENOMSG;
- + }
- +
- + *command = (struct ssh_command *)source->ptr;
- + command_data->ptr = source->ptr + sizeof(struct ssh_command);
- + command_data->len = source->len - sizeof(struct ssh_command);
- +
- + dev_dbg(dev, "rx: parser: valid command found (tc: %#04x, cid: %#04x)\n",
- + (*command)->tc, (*command)->cid);
- +
- + return 0;
- +}
- diff --git a/drivers/platform/surface/aggregator/ssh_parser.h b/drivers/platform/surface/aggregator/ssh_parser.h
- new file mode 100644
- index 000000000000..63c38d350988
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/ssh_parser.h
- @@ -0,0 +1,154 @@
- +/* SPDX-License-Identifier: GPL-2.0+ */
- +/*
- + * SSH message parser.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#ifndef _SURFACE_AGGREGATOR_SSH_PARSER_H
- +#define _SURFACE_AGGREGATOR_SSH_PARSER_H
- +
- +#include <linux/device.h>
- +#include <linux/kfifo.h>
- +#include <linux/slab.h>
- +#include <linux/types.h>
- +
- +#include <linux/surface_aggregator/serial_hub.h>
- +
- +/**
- + * struct sshp_buf - Parser buffer for SSH messages.
- + * @ptr: Pointer to the beginning of the buffer.
- + * @len: Number of bytes used in the buffer.
- + * @cap: Maximum capacity of the buffer.
- + */
- +struct sshp_buf {
- + u8 *ptr;
- + size_t len;
- + size_t cap;
- +};
- +
- +/**
- + * sshp_buf_init() - Initialize a SSH parser buffer.
- + * @buf: The buffer to initialize.
- + * @ptr: The memory backing the buffer.
- + * @cap: The length of the memory backing the buffer, i.e. its capacity.
- + *
- + * Initializes the buffer with the given memory as backing and set its used
- + * length to zero.
- + */
- +static inline void sshp_buf_init(struct sshp_buf *buf, u8 *ptr, size_t cap)
- +{
- + buf->ptr = ptr;
- + buf->len = 0;
- + buf->cap = cap;
- +}
- +
- +/**
- + * sshp_buf_alloc() - Allocate and initialize a SSH parser buffer.
- + * @buf: The buffer to initialize/allocate to.
- + * @cap: The desired capacity of the buffer.
- + * @flags: The flags used for allocating the memory.
- + *
- + * Allocates @cap bytes and initializes the provided buffer struct with the
- + * allocated memory.
- + *
- + * Return: Returns zero on success and %-ENOMEM if allocation failed.
- + */
- +static inline int sshp_buf_alloc(struct sshp_buf *buf, size_t cap, gfp_t flags)
- +{
- + u8 *ptr;
- +
- + ptr = kzalloc(cap, flags);
- + if (!ptr)
- + return -ENOMEM;
- +
- + sshp_buf_init(buf, ptr, cap);
- + return 0;
- +}
- +
- +/**
- + * sshp_buf_free() - Free a SSH parser buffer.
- + * @buf: The buffer to free.
- + *
- + * Frees a SSH parser buffer by freeing the memory backing it and then
- + * resetting its pointer to %NULL and length and capacity to zero. Intended to
- + * free a buffer previously allocated with sshp_buf_alloc().
- + */
- +static inline void sshp_buf_free(struct sshp_buf *buf)
- +{
- + kfree(buf->ptr);
- + buf->ptr = NULL;
- + buf->len = 0;
- + buf->cap = 0;
- +}
- +
- +/**
- + * sshp_buf_drop() - Drop data from the beginning of the buffer.
- + * @buf: The buffer to drop data from.
- + * @n: The number of bytes to drop.
- + *
- + * Drops the first @n bytes from the buffer. Re-aligns any remaining data to
- + * the beginning of the buffer.
- + */
- +static inline void sshp_buf_drop(struct sshp_buf *buf, size_t n)
- +{
- + memmove(buf->ptr, buf->ptr + n, buf->len - n);
- + buf->len -= n;
- +}
- +
- +/**
- + * sshp_buf_read_from_fifo() - Transfer data from a fifo to the buffer.
- + * @buf: The buffer to write the data into.
- + * @fifo: The fifo to read the data from.
- + *
- + * Transfers the data contained in the fifo to the buffer, removing it from
- + * the fifo. This function will try to transfer as much data as possible,
- + * limited either by the remaining space in the buffer or by the number of
- + * bytes available in the fifo.
- + *
- + * Return: Returns the number of bytes transferred.
- + */
- +static inline size_t sshp_buf_read_from_fifo(struct sshp_buf *buf,
- + struct kfifo *fifo)
- +{
- + size_t n;
- +
- + n = kfifo_out(fifo, buf->ptr + buf->len, buf->cap - buf->len);
- + buf->len += n;
- +
- + return n;
- +}
- +
- +/**
- + * sshp_buf_span_from() - Initialize a span from the given buffer and offset.
- + * @buf: The buffer to create the span from.
- + * @offset: The offset in the buffer at which the span should start.
- + * @span: The span to initialize (output).
- + *
- + * Initializes the provided span to point to the memory at the given offset in
- + * the buffer, with the length of the span being capped by the number of bytes
- + * used in the buffer after the offset (i.e. bytes remaining after the
- + * offset).
- + *
- + * Warning: This function does not validate that @offset is less than or equal
- + * to the number of bytes used in the buffer or the buffer capacity. This must
- + * be guaranteed by the caller.
- + */
- +static inline void sshp_buf_span_from(struct sshp_buf *buf, size_t offset,
- + struct ssam_span *span)
- +{
- + span->ptr = buf->ptr + offset;
- + span->len = buf->len - offset;
- +}
- +
- +bool sshp_find_syn(const struct ssam_span *src, struct ssam_span *rem);
- +
- +int sshp_parse_frame(const struct device *dev, const struct ssam_span *source,
- + struct ssh_frame **frame, struct ssam_span *payload,
- + size_t maxlen);
- +
- +int sshp_parse_command(const struct device *dev, const struct ssam_span *source,
- + struct ssh_command **command,
- + struct ssam_span *command_data);
- +
- +#endif /* _SURFACE_AGGREGATOR_SSH_PARSER_h */
- diff --git a/drivers/platform/surface/aggregator/ssh_request_layer.c b/drivers/platform/surface/aggregator/ssh_request_layer.c
- new file mode 100644
- index 000000000000..66c839a995f3
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/ssh_request_layer.c
- @@ -0,0 +1,1211 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * SSH request transport layer.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <asm/unaligned.h>
- +#include <linux/atomic.h>
- +#include <linux/completion.h>
- +#include <linux/ktime.h>
- +#include <linux/limits.h>
- +#include <linux/list.h>
- +#include <linux/slab.h>
- +#include <linux/spinlock.h>
- +#include <linux/types.h>
- +#include <linux/workqueue.h>
- +
- +#include <linux/surface_aggregator/serial_hub.h>
- +#include <linux/surface_aggregator/controller.h>
- +
- +#include "ssh_packet_layer.h"
- +#include "ssh_request_layer.h"
- +
- +/*
- + * SSH_RTL_REQUEST_TIMEOUT - Request timeout.
- + *
- + * Timeout as ktime_t delta for request responses. If we have not received a
- + * response in this time-frame after finishing the underlying packet
- + * transmission, the request will be completed with %-ETIMEDOUT as status
- + * code.
- + */
- +#define SSH_RTL_REQUEST_TIMEOUT ms_to_ktime(3000)
- +
- +/*
- + * SSH_RTL_REQUEST_TIMEOUT_RESOLUTION - Request timeout granularity.
- + *
- + * Time-resolution for timeouts. Should be larger than one jiffy to avoid
- + * direct re-scheduling of reaper work_struct.
- + */
- +#define SSH_RTL_REQUEST_TIMEOUT_RESOLUTION ms_to_ktime(max(2000 / HZ, 50))
- +
- +/*
- + * SSH_RTL_MAX_PENDING - Maximum number of pending requests.
- + *
- + * Maximum number of requests concurrently waiting to be completed (i.e.
- + * waiting for the corresponding packet transmission to finish if they don't
- + * have a response or waiting for a response if they have one).
- + */
- +#define SSH_RTL_MAX_PENDING 3
- +
- +/*
- + * SSH_RTL_TX_BATCH - Maximum number of requests processed per work execution.
- + * Used to prevent livelocking of the workqueue. Value chosen via educated
- + * guess, may be adjusted.
- + */
- +#define SSH_RTL_TX_BATCH 10
- +
- +static u16 ssh_request_get_rqid(struct ssh_request *rqst)
- +{
- + return get_unaligned_le16(rqst->packet.data.ptr
- + + SSH_MSGOFFSET_COMMAND(rqid));
- +}
- +
- +static u32 ssh_request_get_rqid_safe(struct ssh_request *rqst)
- +{
- + if (!rqst->packet.data.ptr)
- + return U32_MAX;
- +
- + return ssh_request_get_rqid(rqst);
- +}
- +
- +static void ssh_rtl_queue_remove(struct ssh_request *rqst)
- +{
- + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
- +
- + spin_lock(&rtl->queue.lock);
- +
- + if (!test_and_clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &rqst->state)) {
- + spin_unlock(&rtl->queue.lock);
- + return;
- + }
- +
- + list_del(&rqst->node);
- +
- + spin_unlock(&rtl->queue.lock);
- + ssh_request_put(rqst);
- +}
- +
- +static bool ssh_rtl_queue_empty(struct ssh_rtl *rtl)
- +{
- + bool empty;
- +
- + spin_lock(&rtl->queue.lock);
- + empty = list_empty(&rtl->queue.head);
- + spin_unlock(&rtl->queue.lock);
- +
- + return empty;
- +}
- +
- +static void ssh_rtl_pending_remove(struct ssh_request *rqst)
- +{
- + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
- +
- + spin_lock(&rtl->pending.lock);
- +
- + if (!test_and_clear_bit(SSH_REQUEST_SF_PENDING_BIT, &rqst->state)) {
- + spin_unlock(&rtl->pending.lock);
- + return;
- + }
- +
- + atomic_dec(&rtl->pending.count);
- + list_del(&rqst->node);
- +
- + spin_unlock(&rtl->pending.lock);
- +
- + ssh_request_put(rqst);
- +}
- +
- +static int ssh_rtl_tx_pending_push(struct ssh_request *rqst)
- +{
- + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
- +
- + spin_lock(&rtl->pending.lock);
- +
- + if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state)) {
- + spin_unlock(&rtl->pending.lock);
- + return -EINVAL;
- + }
- +
- + if (test_and_set_bit(SSH_REQUEST_SF_PENDING_BIT, &rqst->state)) {
- + spin_unlock(&rtl->pending.lock);
- + return -EALREADY;
- + }
- +
- + atomic_inc(&rtl->pending.count);
- + list_add_tail(&ssh_request_get(rqst)->node, &rtl->pending.head);
- +
- + spin_unlock(&rtl->pending.lock);
- + return 0;
- +}
- +
- +static void ssh_rtl_complete_with_status(struct ssh_request *rqst, int status)
- +{
- + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
- +
- + /* rtl/ptl may not be set if we're canceling before submitting. */
- + rtl_dbg_cond(rtl, "rtl: completing request (rqid: %#06x, status: %d)\n",
- + ssh_request_get_rqid_safe(rqst), status);
- +
- + rqst->ops->complete(rqst, NULL, NULL, status);
- +}
- +
- +static void ssh_rtl_complete_with_rsp(struct ssh_request *rqst,
- + const struct ssh_command *cmd,
- + const struct ssam_span *data)
- +{
- + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
- +
- + rtl_dbg(rtl, "rtl: completing request with response (rqid: %#06x)\n",
- + ssh_request_get_rqid(rqst));
- +
- + rqst->ops->complete(rqst, cmd, data, 0);
- +}
- +
- +static bool ssh_rtl_tx_can_process(struct ssh_request *rqst)
- +{
- + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
- +
- + if (test_bit(SSH_REQUEST_TY_FLUSH_BIT, &rqst->state))
- + return !atomic_read(&rtl->pending.count);
- +
- + return atomic_read(&rtl->pending.count) < SSH_RTL_MAX_PENDING;
- +}
- +
- +static struct ssh_request *ssh_rtl_tx_next(struct ssh_rtl *rtl)
- +{
- + struct ssh_request *rqst = ERR_PTR(-ENOENT);
- + struct ssh_request *p, *n;
- +
- + spin_lock(&rtl->queue.lock);
- +
- + /* Find first non-locked request and remove it. */
- + list_for_each_entry_safe(p, n, &rtl->queue.head, node) {
- + if (unlikely(test_bit(SSH_REQUEST_SF_LOCKED_BIT, &p->state)))
- + continue;
- +
- + if (!ssh_rtl_tx_can_process(p)) {
- + rqst = ERR_PTR(-EBUSY);
- + break;
- + }
- +
- + /* Remove from queue and mark as transmitting. */
- + set_bit(SSH_REQUEST_SF_TRANSMITTING_BIT, &p->state);
- + /* Ensure state never gets zero. */
- + smp_mb__before_atomic();
- + clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &p->state);
- +
- + list_del(&p->node);
- +
- + rqst = p;
- + break;
- + }
- +
- + spin_unlock(&rtl->queue.lock);
- + return rqst;
- +}
- +
- +static int ssh_rtl_tx_try_process_one(struct ssh_rtl *rtl)
- +{
- + struct ssh_request *rqst;
- + int status;
- +
- + /* Get and prepare next request for transmit. */
- + rqst = ssh_rtl_tx_next(rtl);
- + if (IS_ERR(rqst))
- + return PTR_ERR(rqst);
- +
- + /* Add it to/mark it as pending. */
- + status = ssh_rtl_tx_pending_push(rqst);
- + if (status) {
- + ssh_request_put(rqst);
- + return -EAGAIN;
- + }
- +
- + /* Submit packet. */
- + status = ssh_ptl_submit(&rtl->ptl, &rqst->packet);
- + if (status == -ESHUTDOWN) {
- + /*
- + * Packet has been refused due to the packet layer shutting
- + * down. Complete it here.
- + */
- + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state);
- + /*
- + * Note: A barrier is not required here, as there are only two
- + * references in the system at this point: The one that we have,
- + * and the other one that belongs to the pending set. Due to the
- + * request being marked as "transmitting", our process is the
- + * only one allowed to remove the pending node and change the
- + * state. Normally, the task would fall to the packet callback,
- + * but as this is a path where submission failed, this callback
- + * will never be executed.
- + */
- +
- + ssh_rtl_pending_remove(rqst);
- + ssh_rtl_complete_with_status(rqst, -ESHUTDOWN);
- +
- + ssh_request_put(rqst);
- + return -ESHUTDOWN;
- +
- + } else if (status) {
- + /*
- + * If submitting the packet failed and the packet layer isn't
- + * shutting down, the packet has either been submitted/queued
- + * before (-EALREADY, which cannot happen as we have
- + * guaranteed that requests cannot be re-submitted), or the
- + * packet was marked as locked (-EINVAL). To mark the packet
- + * locked at this stage, the request, and thus the packets
- + * itself, had to have been canceled. Simply drop the
- + * reference. Cancellation itself will remove it from the set
- + * of pending requests.
- + */
- +
- + WARN_ON(status != -EINVAL);
- +
- + ssh_request_put(rqst);
- + return -EAGAIN;
- + }
- +
- + ssh_request_put(rqst);
- + return 0;
- +}
- +
- +static bool ssh_rtl_tx_schedule(struct ssh_rtl *rtl)
- +{
- + if (atomic_read(&rtl->pending.count) >= SSH_RTL_MAX_PENDING)
- + return false;
- +
- + if (ssh_rtl_queue_empty(rtl))
- + return false;
- +
- + return schedule_work(&rtl->tx.work);
- +}
- +
- +static void ssh_rtl_tx_work_fn(struct work_struct *work)
- +{
- + struct ssh_rtl *rtl = to_ssh_rtl(work, tx.work);
- + unsigned int iterations = SSH_RTL_TX_BATCH;
- + int status;
- +
- + /*
- + * Try to be nice and not block/live-lock the workqueue: Run a maximum
- + * of 10 tries, then re-submit if necessary. This should not be
- + * necessary for normal execution, but guarantee it anyway.
- + */
- + do {
- + status = ssh_rtl_tx_try_process_one(rtl);
- + if (status == -ENOENT || status == -EBUSY)
- + return; /* No more requests to process. */
- +
- + if (status == -ESHUTDOWN) {
- + /*
- + * Packet system shutting down. No new packets can be
- + * transmitted. Return silently, the party initiating
- + * the shutdown should handle the rest.
- + */
- + return;
- + }
- +
- + WARN_ON(status != 0 && status != -EAGAIN);
- + } while (--iterations);
- +
- + /* Out of tries, reschedule. */
- + ssh_rtl_tx_schedule(rtl);
- +}
- +
- +/**
- + * ssh_rtl_submit() - Submit a request to the transport layer.
- + * @rtl: The request transport layer.
- + * @rqst: The request to submit.
- + *
- + * Submits a request to the transport layer. A single request may not be
- + * submitted multiple times without reinitializing it.
- + *
- + * Return: Returns zero on success, %-EINVAL if the request type is invalid or
- + * the request has been canceled prior to submission, %-EALREADY if the
- + * request has already been submitted, or %-ESHUTDOWN in case the request
- + * transport layer has been shut down.
- + */
- +int ssh_rtl_submit(struct ssh_rtl *rtl, struct ssh_request *rqst)
- +{
- + /*
- + * Ensure that requests expecting a response are sequenced. If this
- + * invariant ever changes, see the comment in ssh_rtl_complete() on what
- + * is required to be changed in the code.
- + */
- + if (test_bit(SSH_REQUEST_TY_HAS_RESPONSE_BIT, &rqst->state))
- + if (!test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &rqst->packet.state))
- + return -EINVAL;
- +
- + spin_lock(&rtl->queue.lock);
- +
- + /*
- + * Try to set ptl and check if this request has already been submitted.
- + *
- + * Must be inside lock as we might run into a lost update problem
- + * otherwise: If this were outside of the lock, cancellation in
- + * ssh_rtl_cancel_nonpending() may run after we've set the ptl
- + * reference but before we enter the lock. In that case, we'd detect
- + * that the request is being added to the queue and would try to remove
- + * it from that, but removal might fail because it hasn't actually been
- + * added yet. By putting this cmpxchg in the critical section, we
- + * ensure that the queuing detection only triggers when we are already
- + * in the critical section and the remove process will wait until the
- + * push operation has been completed (via lock) due to that. Only then,
- + * we can safely try to remove it.
- + */
- + if (cmpxchg(&rqst->packet.ptl, NULL, &rtl->ptl)) {
- + spin_unlock(&rtl->queue.lock);
- + return -EALREADY;
- + }
- +
- + /*
- + * Ensure that we set ptl reference before we continue modifying state.
- + * This is required for non-pending cancellation. This barrier is paired
- + * with the one in ssh_rtl_cancel_nonpending().
- + *
- + * By setting the ptl reference before we test for "locked", we can
- + * check if the "locked" test may have already run. See comments in
- + * ssh_rtl_cancel_nonpending() for more detail.
- + */
- + smp_mb__after_atomic();
- +
- + if (test_bit(SSH_RTL_SF_SHUTDOWN_BIT, &rtl->state)) {
- + spin_unlock(&rtl->queue.lock);
- + return -ESHUTDOWN;
- + }
- +
- + if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state)) {
- + spin_unlock(&rtl->queue.lock);
- + return -EINVAL;
- + }
- +
- + set_bit(SSH_REQUEST_SF_QUEUED_BIT, &rqst->state);
- + list_add_tail(&ssh_request_get(rqst)->node, &rtl->queue.head);
- +
- + spin_unlock(&rtl->queue.lock);
- +
- + ssh_rtl_tx_schedule(rtl);
- + return 0;
- +}
- +
- +static void ssh_rtl_timeout_reaper_mod(struct ssh_rtl *rtl, ktime_t now,
- + ktime_t expires)
- +{
- + unsigned long delta = msecs_to_jiffies(ktime_ms_delta(expires, now));
- + ktime_t aexp = ktime_add(expires, SSH_RTL_REQUEST_TIMEOUT_RESOLUTION);
- +
- + spin_lock(&rtl->rtx_timeout.lock);
- +
- + /* Re-adjust / schedule reaper only if it is above resolution delta. */
- + if (ktime_before(aexp, rtl->rtx_timeout.expires)) {
- + rtl->rtx_timeout.expires = expires;
- + mod_delayed_work(system_wq, &rtl->rtx_timeout.reaper, delta);
- + }
- +
- + spin_unlock(&rtl->rtx_timeout.lock);
- +}
- +
- +static void ssh_rtl_timeout_start(struct ssh_request *rqst)
- +{
- + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
- + ktime_t timestamp = ktime_get_coarse_boottime();
- + ktime_t timeout = rtl->rtx_timeout.timeout;
- +
- + if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state))
- + return;
- +
- + /*
- + * Note: The timestamp gets set only once. This happens on the packet
- + * callback. All other access to it is read-only.
- + */
- + WRITE_ONCE(rqst->timestamp, timestamp);
- + /*
- + * Ensure timestamp is set before starting the reaper. Paired with
- + * implicit barrier following check on ssh_request_get_expiration() in
- + * ssh_rtl_timeout_reap.
- + */
- + smp_mb__after_atomic();
- +
- + ssh_rtl_timeout_reaper_mod(rtl, timestamp, timestamp + timeout);
- +}
- +
- +static void ssh_rtl_complete(struct ssh_rtl *rtl,
- + const struct ssh_command *command,
- + const struct ssam_span *command_data)
- +{
- + struct ssh_request *r = NULL;
- + struct ssh_request *p, *n;
- + u16 rqid = get_unaligned_le16(&command->rqid);
- +
- + /*
- + * Get request from pending based on request ID and mark it as response
- + * received and locked.
- + */
- + spin_lock(&rtl->pending.lock);
- + list_for_each_entry_safe(p, n, &rtl->pending.head, node) {
- + /* We generally expect requests to be processed in order. */
- + if (unlikely(ssh_request_get_rqid(p) != rqid))
- + continue;
- +
- + /*
- + * Mark as "response received" and "locked" as we're going to
- + * complete it.
- + */
- + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &p->state);
- + set_bit(SSH_REQUEST_SF_RSPRCVD_BIT, &p->state);
- + /* Ensure state never gets zero. */
- + smp_mb__before_atomic();
- + clear_bit(SSH_REQUEST_SF_PENDING_BIT, &p->state);
- +
- + atomic_dec(&rtl->pending.count);
- + list_del(&p->node);
- +
- + r = p;
- + break;
- + }
- + spin_unlock(&rtl->pending.lock);
- +
- + if (!r) {
- + rtl_warn(rtl, "rtl: dropping unexpected command message (rqid = %#06x)\n",
- + rqid);
- + return;
- + }
- +
- + /* If the request hasn't been completed yet, we will do this now. */
- + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state)) {
- + ssh_request_put(r);
- + ssh_rtl_tx_schedule(rtl);
- + return;
- + }
- +
- + /*
- + * Make sure the request has been transmitted. In case of a sequenced
- + * request, we are guaranteed that the completion callback will run on
- + * the receiver thread directly when the ACK for the packet has been
- + * received. Similarly, this function is guaranteed to run on the
- + * receiver thread. Thus we are guaranteed that if the packet has been
- + * successfully transmitted and received an ACK, the transmitted flag
- + * has been set and is visible here.
- + *
- + * We are currently not handling unsequenced packets here, as those
- + * should never expect a response as ensured in ssh_rtl_submit. If this
- + * ever changes, one would have to test for
- + *
- + * (r->state & (transmitting | transmitted))
- + *
- + * on unsequenced packets to determine if they could have been
- + * transmitted. There are no synchronization guarantees as in the
- + * sequenced case, since, in this case, the callback function will not
- + * run on the same thread. Thus an exact determination is impossible.
- + */
- + if (!test_bit(SSH_REQUEST_SF_TRANSMITTED_BIT, &r->state)) {
- + rtl_err(rtl, "rtl: received response before ACK for request (rqid = %#06x)\n",
- + rqid);
- +
- + /*
- + * NB: Timeout has already been canceled, request already been
- + * removed from pending and marked as locked and completed. As
- + * we receive a "false" response, the packet might still be
- + * queued though.
- + */
- + ssh_rtl_queue_remove(r);
- +
- + ssh_rtl_complete_with_status(r, -EREMOTEIO);
- + ssh_request_put(r);
- +
- + ssh_rtl_tx_schedule(rtl);
- + return;
- + }
- +
- + /*
- + * NB: Timeout has already been canceled, request already been
- + * removed from pending and marked as locked and completed. The request
- + * can also not be queued any more, as it has been marked as
- + * transmitting and later transmitted. Thus no need to remove it from
- + * anywhere.
- + */
- +
- + ssh_rtl_complete_with_rsp(r, command, command_data);
- + ssh_request_put(r);
- +
- + ssh_rtl_tx_schedule(rtl);
- +}
- +
- +static bool ssh_rtl_cancel_nonpending(struct ssh_request *r)
- +{
- + struct ssh_rtl *rtl;
- + unsigned long flags, fixed;
- + bool remove;
- +
- + /*
- + * Handle unsubmitted request: Try to mark the packet as locked,
- + * expecting the state to be zero (i.e. unsubmitted). Note that, if
- + * setting the state worked, we might still be adding the packet to the
- + * queue in a currently executing submit call. In that case, however,
- + * ptl reference must have been set previously, as locked is checked
- + * after setting ptl. Furthermore, when the ptl reference is set, the
- + * submission process is guaranteed to have entered the critical
- + * section. Thus only if we successfully locked this request and ptl is
- + * NULL, we have successfully removed the request, i.e. we are
- + * guaranteed that, due to the "locked" check in ssh_rtl_submit(), the
- + * packet will never be added. Otherwise, we need to try and grab it
- + * from the queue, where we are now guaranteed that the packet is or has
- + * been due to the critical section.
- + *
- + * Note that if the cmpxchg() fails, we are guaranteed that ptl has
- + * been set and is non-NULL, as states can only be nonzero after this
- + * has been set. Also note that we need to fetch the static (type)
- + * flags to ensure that they don't cause the cmpxchg() to fail.
- + */
- + fixed = READ_ONCE(r->state) & SSH_REQUEST_FLAGS_TY_MASK;
- + flags = cmpxchg(&r->state, fixed, SSH_REQUEST_SF_LOCKED_BIT);
- +
- + /*
- + * Force correct ordering with regards to state and ptl reference access
- + * to safe-guard cancellation to concurrent submission against a
- + * lost-update problem. First try to exchange state, then also check
- + * ptl if that worked. This barrier is paired with the
- + * one in ssh_rtl_submit().
- + */
- + smp_mb__after_atomic();
- +
- + if (flags == fixed && !READ_ONCE(r->packet.ptl)) {
- + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
- + return true;
- +
- + ssh_rtl_complete_with_status(r, -ECANCELED);
- + return true;
- + }
- +
- + rtl = ssh_request_rtl(r);
- + spin_lock(&rtl->queue.lock);
- +
- + /*
- + * Note: 1) Requests cannot be re-submitted. 2) If a request is
- + * queued, it cannot be "transmitting"/"pending" yet. Thus, if we
- + * successfully remove the request here, we have removed all its
- + * occurrences in the system.
- + */
- +
- + remove = test_and_clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &r->state);
- + if (!remove) {
- + spin_unlock(&rtl->queue.lock);
- + return false;
- + }
- +
- + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
- + list_del(&r->node);
- +
- + spin_unlock(&rtl->queue.lock);
- +
- + ssh_request_put(r); /* Drop reference obtained from queue. */
- +
- + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
- + return true;
- +
- + ssh_rtl_complete_with_status(r, -ECANCELED);
- + return true;
- +}
- +
- +static bool ssh_rtl_cancel_pending(struct ssh_request *r)
- +{
- + /* If the packet is already locked, it's going to be removed shortly. */
- + if (test_and_set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state))
- + return true;
- +
- + /*
- + * Now that we have locked the packet, we have guaranteed that it can't
- + * be added to the system any more. If ptl is NULL, the locked
- + * check in ssh_rtl_submit() has not been run and any submission,
- + * currently in progress or called later, won't add the packet. Thus we
- + * can directly complete it.
- + *
- + * The implicit memory barrier of test_and_set_bit() should be enough
- + * to ensure that the correct order (first lock, then check ptl) is
- + * ensured. This is paired with the barrier in ssh_rtl_submit().
- + */
- + if (!READ_ONCE(r->packet.ptl)) {
- + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
- + return true;
- +
- + ssh_rtl_complete_with_status(r, -ECANCELED);
- + return true;
- + }
- +
- + /*
- + * Try to cancel the packet. If the packet has not been completed yet,
- + * this will subsequently (and synchronously) call the completion
- + * callback of the packet, which will complete the request.
- + */
- + ssh_ptl_cancel(&r->packet);
- +
- + /*
- + * If the packet has been completed with success, i.e. has not been
- + * canceled by the above call, the request may not have been completed
- + * yet (may be waiting for a response). Check if we need to do this
- + * here.
- + */
- + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
- + return true;
- +
- + ssh_rtl_queue_remove(r);
- + ssh_rtl_pending_remove(r);
- + ssh_rtl_complete_with_status(r, -ECANCELED);
- +
- + return true;
- +}
- +
- +/**
- + * ssh_rtl_cancel() - Cancel request.
- + * @rqst: The request to cancel.
- + * @pending: Whether to also cancel pending requests.
- + *
- + * Cancels the given request. If @pending is %false, this will not cancel
- + * pending requests, i.e. requests that have already been submitted to the
- + * packet layer but not been completed yet. If @pending is %true, this will
- + * cancel the given request regardless of the state it is in.
- + *
- + * If the request has been canceled by calling this function, both completion
- + * and release callbacks of the request will be executed in a reasonable
- + * time-frame. This may happen during execution of this function, however,
- + * there is no guarantee for this. For example, a request currently
- + * transmitting will be canceled/completed only after transmission has
- + * completed, and the respective callbacks will be executed on the transmitter
- + * thread, which may happen during, but also some time after execution of the
- + * cancel function.
- + *
- + * Return: Returns %true if the given request has been canceled or completed,
- + * either by this function or prior to calling this function, %false
- + * otherwise. If @pending is %true, this function will always return %true.
- + */
- +bool ssh_rtl_cancel(struct ssh_request *rqst, bool pending)
- +{
- + struct ssh_rtl *rtl;
- + bool canceled;
- +
- + if (test_and_set_bit(SSH_REQUEST_SF_CANCELED_BIT, &rqst->state))
- + return true;
- +
- + if (pending)
- + canceled = ssh_rtl_cancel_pending(rqst);
- + else
- + canceled = ssh_rtl_cancel_nonpending(rqst);
- +
- + /* Note: rtl may be NULL if request has not been submitted yet. */
- + rtl = ssh_request_rtl(rqst);
- + if (canceled && rtl)
- + ssh_rtl_tx_schedule(rtl);
- +
- + return canceled;
- +}
- +
- +static void ssh_rtl_packet_callback(struct ssh_packet *p, int status)
- +{
- + struct ssh_request *r = to_ssh_request(p);
- +
- + if (unlikely(status)) {
- + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
- +
- + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
- + return;
- +
- + /*
- + * The packet may get canceled even though it has not been
- + * submitted yet. The request may still be queued. Check the
- + * queue and remove it if necessary. As the timeout would have
- + * been started in this function on success, there's no need
- + * to cancel it here.
- + */
- + ssh_rtl_queue_remove(r);
- + ssh_rtl_pending_remove(r);
- + ssh_rtl_complete_with_status(r, status);
- +
- + ssh_rtl_tx_schedule(ssh_request_rtl(r));
- + return;
- + }
- +
- + /* Update state: Mark as transmitted and clear transmitting. */
- + set_bit(SSH_REQUEST_SF_TRANSMITTED_BIT, &r->state);
- + /* Ensure state never gets zero. */
- + smp_mb__before_atomic();
- + clear_bit(SSH_REQUEST_SF_TRANSMITTING_BIT, &r->state);
- +
- + /* If we expect a response, we just need to start the timeout. */
- + if (test_bit(SSH_REQUEST_TY_HAS_RESPONSE_BIT, &r->state)) {
- + /*
- + * Note: This is the only place where the timestamp gets set,
- + * all other access to it is read-only.
- + */
- + ssh_rtl_timeout_start(r);
- + return;
- + }
- +
- + /*
- + * If we don't expect a response, lock, remove, and complete the
- + * request. Note that, at this point, the request is guaranteed to have
- + * left the queue and no timeout has been started. Thus we only need to
- + * remove it from pending. If the request has already been completed (it
- + * may have been canceled) return.
- + */
- +
- + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
- + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
- + return;
- +
- + ssh_rtl_pending_remove(r);
- + ssh_rtl_complete_with_status(r, 0);
- +
- + ssh_rtl_tx_schedule(ssh_request_rtl(r));
- +}
- +
- +static ktime_t ssh_request_get_expiration(struct ssh_request *r, ktime_t timeout)
- +{
- + ktime_t timestamp = READ_ONCE(r->timestamp);
- +
- + if (timestamp != KTIME_MAX)
- + return ktime_add(timestamp, timeout);
- + else
- + return KTIME_MAX;
- +}
- +
- +static void ssh_rtl_timeout_reap(struct work_struct *work)
- +{
- + struct ssh_rtl *rtl = to_ssh_rtl(work, rtx_timeout.reaper.work);
- + struct ssh_request *r, *n;
- + LIST_HEAD(claimed);
- + ktime_t now = ktime_get_coarse_boottime();
- + ktime_t timeout = rtl->rtx_timeout.timeout;
- + ktime_t next = KTIME_MAX;
- +
- + /*
- + * Mark reaper as "not pending". This is done before checking any
- + * requests to avoid lost-update type problems.
- + */
- + spin_lock(&rtl->rtx_timeout.lock);
- + rtl->rtx_timeout.expires = KTIME_MAX;
- + spin_unlock(&rtl->rtx_timeout.lock);
- +
- + spin_lock(&rtl->pending.lock);
- + list_for_each_entry_safe(r, n, &rtl->pending.head, node) {
- + ktime_t expires = ssh_request_get_expiration(r, timeout);
- +
- + /*
- + * Check if the timeout hasn't expired yet. Find out next
- + * expiration date to be handled after this run.
- + */
- + if (ktime_after(expires, now)) {
- + next = ktime_before(expires, next) ? expires : next;
- + continue;
- + }
- +
- + /* Avoid further transitions if locked. */
- + if (test_and_set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state))
- + continue;
- +
- + /*
- + * We have now marked the packet as locked. Thus it cannot be
- + * added to the pending or queued lists again after we've
- + * removed it here. We can therefore re-use the node of this
- + * packet temporarily.
- + */
- +
- + clear_bit(SSH_REQUEST_SF_PENDING_BIT, &r->state);
- +
- + atomic_dec(&rtl->pending.count);
- + list_del(&r->node);
- +
- + list_add_tail(&r->node, &claimed);
- + }
- + spin_unlock(&rtl->pending.lock);
- +
- + /* Cancel and complete the request. */
- + list_for_each_entry_safe(r, n, &claimed, node) {
- + /*
- + * At this point we've removed the packet from pending. This
- + * means that we've obtained the last (only) reference of the
- + * system to it. Thus we can just complete it.
- + */
- + if (!test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
- + ssh_rtl_complete_with_status(r, -ETIMEDOUT);
- +
- + /*
- + * Drop the reference we've obtained by removing it from the
- + * pending set.
- + */
- + list_del(&r->node);
- + ssh_request_put(r);
- + }
- +
- + /* Ensure that the reaper doesn't run again immediately. */
- + next = max(next, ktime_add(now, SSH_RTL_REQUEST_TIMEOUT_RESOLUTION));
- + if (next != KTIME_MAX)
- + ssh_rtl_timeout_reaper_mod(rtl, now, next);
- +
- + ssh_rtl_tx_schedule(rtl);
- +}
- +
- +static void ssh_rtl_rx_event(struct ssh_rtl *rtl, const struct ssh_command *cmd,
- + const struct ssam_span *data)
- +{
- + rtl_dbg(rtl, "rtl: handling event (rqid: %#06x)\n",
- + get_unaligned_le16(&cmd->rqid));
- +
- + rtl->ops.handle_event(rtl, cmd, data);
- +}
- +
- +static void ssh_rtl_rx_command(struct ssh_ptl *p, const struct ssam_span *data)
- +{
- + struct ssh_rtl *rtl = to_ssh_rtl(p, ptl);
- + struct device *dev = &p->serdev->dev;
- + struct ssh_command *command;
- + struct ssam_span command_data;
- +
- + if (sshp_parse_command(dev, data, &command, &command_data))
- + return;
- +
- + if (ssh_rqid_is_event(get_unaligned_le16(&command->rqid)))
- + ssh_rtl_rx_event(rtl, command, &command_data);
- + else
- + ssh_rtl_complete(rtl, command, &command_data);
- +}
- +
- +static void ssh_rtl_rx_data(struct ssh_ptl *p, const struct ssam_span *data)
- +{
- + if (!data->len) {
- + ptl_err(p, "rtl: rx: no data frame payload\n");
- + return;
- + }
- +
- + switch (data->ptr[0]) {
- + case SSH_PLD_TYPE_CMD:
- + ssh_rtl_rx_command(p, data);
- + break;
- +
- + default:
- + ptl_err(p, "rtl: rx: unknown frame payload type (type: %#04x)\n",
- + data->ptr[0]);
- + break;
- + }
- +}
- +
- +static void ssh_rtl_packet_release(struct ssh_packet *p)
- +{
- + struct ssh_request *rqst;
- +
- + rqst = to_ssh_request(p);
- + rqst->ops->release(rqst);
- +}
- +
- +static const struct ssh_packet_ops ssh_rtl_packet_ops = {
- + .complete = ssh_rtl_packet_callback,
- + .release = ssh_rtl_packet_release,
- +};
- +
- +/**
- + * ssh_request_init() - Initialize SSH request.
- + * @rqst: The request to initialize.
- + * @flags: Request flags, determining the type of the request.
- + * @ops: Request operations.
- + *
- + * Initializes the given SSH request and underlying packet. Sets the message
- + * buffer pointer to %NULL and the message buffer length to zero. This buffer
- + * has to be set separately via ssh_request_set_data() before submission and
- + * must contain a valid SSH request message.
- + *
- + * Return: Returns zero on success or %-EINVAL if the given flags are invalid.
- + */
- +int ssh_request_init(struct ssh_request *rqst, enum ssam_request_flags flags,
- + const struct ssh_request_ops *ops)
- +{
- + unsigned long type = BIT(SSH_PACKET_TY_BLOCKING_BIT);
- +
- + /* Unsequenced requests cannot have a response. */
- + if (flags & SSAM_REQUEST_UNSEQUENCED && flags & SSAM_REQUEST_HAS_RESPONSE)
- + return -EINVAL;
- +
- + if (!(flags & SSAM_REQUEST_UNSEQUENCED))
- + type |= BIT(SSH_PACKET_TY_SEQUENCED_BIT);
- +
- + ssh_packet_init(&rqst->packet, type, SSH_PACKET_PRIORITY(DATA, 0),
- + &ssh_rtl_packet_ops);
- +
- + INIT_LIST_HEAD(&rqst->node);
- +
- + rqst->state = 0;
- + if (flags & SSAM_REQUEST_HAS_RESPONSE)
- + rqst->state |= BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT);
- +
- + rqst->timestamp = KTIME_MAX;
- + rqst->ops = ops;
- +
- + return 0;
- +}
- +
- +/**
- + * ssh_rtl_init() - Initialize request transport layer.
- + * @rtl: The request transport layer to initialize.
- + * @serdev: The underlying serial device, i.e. the lower-level transport.
- + * @ops: Request transport layer operations.
- + *
- + * Initializes the given request transport layer and associated packet
- + * transport layer. Transmitter and receiver threads must be started
- + * separately via ssh_rtl_tx_start() and ssh_rtl_rx_start(), after the
- + * request-layer has been initialized and the lower-level serial device layer
- + * has been set up.
- + *
- + * Return: Returns zero on success and a nonzero error code on failure.
- + */
- +int ssh_rtl_init(struct ssh_rtl *rtl, struct serdev_device *serdev,
- + const struct ssh_rtl_ops *ops)
- +{
- + struct ssh_ptl_ops ptl_ops;
- + int status;
- +
- + ptl_ops.data_received = ssh_rtl_rx_data;
- +
- + status = ssh_ptl_init(&rtl->ptl, serdev, &ptl_ops);
- + if (status)
- + return status;
- +
- + spin_lock_init(&rtl->queue.lock);
- + INIT_LIST_HEAD(&rtl->queue.head);
- +
- + spin_lock_init(&rtl->pending.lock);
- + INIT_LIST_HEAD(&rtl->pending.head);
- + atomic_set_release(&rtl->pending.count, 0);
- +
- + INIT_WORK(&rtl->tx.work, ssh_rtl_tx_work_fn);
- +
- + spin_lock_init(&rtl->rtx_timeout.lock);
- + rtl->rtx_timeout.timeout = SSH_RTL_REQUEST_TIMEOUT;
- + rtl->rtx_timeout.expires = KTIME_MAX;
- + INIT_DELAYED_WORK(&rtl->rtx_timeout.reaper, ssh_rtl_timeout_reap);
- +
- + rtl->ops = *ops;
- +
- + return 0;
- +}
- +
- +/**
- + * ssh_rtl_destroy() - Deinitialize request transport layer.
- + * @rtl: The request transport layer to deinitialize.
- + *
- + * Deinitializes the given request transport layer and frees resources
- + * associated with it. If receiver and/or transmitter threads have been
- + * started, the layer must first be shut down via ssh_rtl_shutdown() before
- + * this function can be called.
- + */
- +void ssh_rtl_destroy(struct ssh_rtl *rtl)
- +{
- + ssh_ptl_destroy(&rtl->ptl);
- +}
- +
- +/**
- + * ssh_rtl_tx_start() - Start request transmitter and receiver.
- + * @rtl: The request transport layer.
- + *
- + * Return: Returns zero on success, a negative error code on failure.
- + */
- +int ssh_rtl_start(struct ssh_rtl *rtl)
- +{
- + int status;
- +
- + status = ssh_ptl_tx_start(&rtl->ptl);
- + if (status)
- + return status;
- +
- + ssh_rtl_tx_schedule(rtl);
- +
- + status = ssh_ptl_rx_start(&rtl->ptl);
- + if (status) {
- + ssh_rtl_flush(rtl, msecs_to_jiffies(5000));
- + ssh_ptl_tx_stop(&rtl->ptl);
- + return status;
- + }
- +
- + return 0;
- +}
- +
- +struct ssh_flush_request {
- + struct ssh_request base;
- + struct completion completion;
- + int status;
- +};
- +
- +static void ssh_rtl_flush_request_complete(struct ssh_request *r,
- + const struct ssh_command *cmd,
- + const struct ssam_span *data,
- + int status)
- +{
- + struct ssh_flush_request *rqst;
- +
- + rqst = container_of(r, struct ssh_flush_request, base);
- + rqst->status = status;
- +}
- +
- +static void ssh_rtl_flush_request_release(struct ssh_request *r)
- +{
- + struct ssh_flush_request *rqst;
- +
- + rqst = container_of(r, struct ssh_flush_request, base);
- + complete_all(&rqst->completion);
- +}
- +
- +static const struct ssh_request_ops ssh_rtl_flush_request_ops = {
- + .complete = ssh_rtl_flush_request_complete,
- + .release = ssh_rtl_flush_request_release,
- +};
- +
- +/**
- + * ssh_rtl_flush() - Flush the request transport layer.
- + * @rtl: request transport layer
- + * @timeout: timeout for the flush operation in jiffies
- + *
- + * Queue a special flush request and wait for its completion. This request
- + * will be completed after all other currently queued and pending requests
- + * have been completed. Instead of a normal data packet, this request submits
- + * a special flush packet, meaning that upon completion, also the underlying
- + * packet transport layer has been flushed.
- + *
- + * Flushing the request layer guarantees that all previously submitted
- + * requests have been fully completed before this call returns. Additionally,
- + * flushing blocks execution of all later submitted requests until the flush
- + * has been completed.
- + *
- + * If the caller ensures that no new requests are submitted after a call to
- + * this function, the request transport layer is guaranteed to have no
- + * remaining requests when this call returns. The same guarantee does not hold
- + * for the packet layer, on which control packets may still be queued after
- + * this call.
- + *
- + * Return: Returns zero on success, %-ETIMEDOUT if the flush timed out and has
- + * been canceled as a result of the timeout, or %-ESHUTDOWN if the packet
- + * and/or request transport layer has been shut down before this call. May
- + * also return %-EINTR if the underlying packet transmission has been
- + * interrupted.
- + */
- +int ssh_rtl_flush(struct ssh_rtl *rtl, unsigned long timeout)
- +{
- + const unsigned int init_flags = SSAM_REQUEST_UNSEQUENCED;
- + struct ssh_flush_request rqst;
- + int status;
- +
- + ssh_request_init(&rqst.base, init_flags, &ssh_rtl_flush_request_ops);
- + rqst.base.packet.state |= BIT(SSH_PACKET_TY_FLUSH_BIT);
- + rqst.base.packet.priority = SSH_PACKET_PRIORITY(FLUSH, 0);
- + rqst.base.state |= BIT(SSH_REQUEST_TY_FLUSH_BIT);
- +
- + init_completion(&rqst.completion);
- +
- + status = ssh_rtl_submit(rtl, &rqst.base);
- + if (status)
- + return status;
- +
- + ssh_request_put(&rqst.base);
- +
- + if (!wait_for_completion_timeout(&rqst.completion, timeout)) {
- + ssh_rtl_cancel(&rqst.base, true);
- + wait_for_completion(&rqst.completion);
- + }
- +
- + WARN_ON(rqst.status != 0 && rqst.status != -ECANCELED &&
- + rqst.status != -ESHUTDOWN && rqst.status != -EINTR);
- +
- + return rqst.status == -ECANCELED ? -ETIMEDOUT : rqst.status;
- +}
- +
- +/**
- + * ssh_rtl_shutdown() - Shut down request transport layer.
- + * @rtl: The request transport layer.
- + *
- + * Shuts down the request transport layer, removing and canceling all queued
- + * and pending requests. Requests canceled by this operation will be completed
- + * with %-ESHUTDOWN as status. Receiver and transmitter threads will be
- + * stopped, the lower-level packet layer will be shutdown.
- + *
- + * As a result of this function, the transport layer will be marked as shut
- + * down. Submission of requests after the transport layer has been shut down
- + * will fail with %-ESHUTDOWN.
- + */
- +void ssh_rtl_shutdown(struct ssh_rtl *rtl)
- +{
- + struct ssh_request *r, *n;
- + LIST_HEAD(claimed);
- + int pending;
- +
- + set_bit(SSH_RTL_SF_SHUTDOWN_BIT, &rtl->state);
- + /*
- + * Ensure that the layer gets marked as shut-down before actually
- + * stopping it. In combination with the check in ssh_rtl_submit(),
- + * this guarantees that no new requests can be added and all already
- + * queued requests are properly canceled.
- + */
- + smp_mb__after_atomic();
- +
- + /* Remove requests from queue. */
- + spin_lock(&rtl->queue.lock);
- + list_for_each_entry_safe(r, n, &rtl->queue.head, node) {
- + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
- + /* Ensure state never gets zero. */
- + smp_mb__before_atomic();
- + clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &r->state);
- +
- + list_del(&r->node);
- + list_add_tail(&r->node, &claimed);
- + }
- + spin_unlock(&rtl->queue.lock);
- +
- + /*
- + * We have now guaranteed that the queue is empty and no more new
- + * requests can be submitted (i.e. it will stay empty). This means that
- + * calling ssh_rtl_tx_schedule() will not schedule tx.work any more. So
- + * we can simply call cancel_work_sync() on tx.work here and when that
- + * returns, we've locked it down. This also means that after this call,
- + * we don't submit any more packets to the underlying packet layer, so
- + * we can also shut that down.
- + */
- +
- + cancel_work_sync(&rtl->tx.work);
- + ssh_ptl_shutdown(&rtl->ptl);
- + cancel_delayed_work_sync(&rtl->rtx_timeout.reaper);
- +
- + /*
- + * Shutting down the packet layer should also have canceled all
- + * requests. Thus the pending set should be empty. Attempt to handle
- + * this gracefully anyways, even though this should be dead code.
- + */
- +
- + pending = atomic_read(&rtl->pending.count);
- + if (WARN_ON(pending)) {
- + spin_lock(&rtl->pending.lock);
- + list_for_each_entry_safe(r, n, &rtl->pending.head, node) {
- + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
- + /* Ensure state never gets zero. */
- + smp_mb__before_atomic();
- + clear_bit(SSH_REQUEST_SF_PENDING_BIT, &r->state);
- +
- + list_del(&r->node);
- + list_add_tail(&r->node, &claimed);
- + }
- + spin_unlock(&rtl->pending.lock);
- + }
- +
- + /* Finally, cancel and complete the requests we claimed before. */
- + list_for_each_entry_safe(r, n, &claimed, node) {
- + /*
- + * We need test_and_set() because we still might compete with
- + * cancellation.
- + */
- + if (!test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
- + ssh_rtl_complete_with_status(r, -ESHUTDOWN);
- +
- + /*
- + * Drop the reference we've obtained by removing it from the
- + * lists.
- + */
- + list_del(&r->node);
- + ssh_request_put(r);
- + }
- +}
- diff --git a/drivers/platform/surface/aggregator/ssh_request_layer.h b/drivers/platform/surface/aggregator/ssh_request_layer.h
- new file mode 100644
- index 000000000000..cb35815858d1
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/ssh_request_layer.h
- @@ -0,0 +1,143 @@
- +/* SPDX-License-Identifier: GPL-2.0+ */
- +/*
- + * SSH request transport layer.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#ifndef _SURFACE_AGGREGATOR_SSH_REQUEST_LAYER_H
- +#define _SURFACE_AGGREGATOR_SSH_REQUEST_LAYER_H
- +
- +#include <linux/atomic.h>
- +#include <linux/ktime.h>
- +#include <linux/list.h>
- +#include <linux/spinlock.h>
- +#include <linux/workqueue.h>
- +
- +#include <linux/surface_aggregator/serial_hub.h>
- +#include <linux/surface_aggregator/controller.h>
- +
- +#include "ssh_packet_layer.h"
- +
- +/**
- + * enum ssh_rtl_state_flags - State-flags for &struct ssh_rtl.
- + *
- + * @SSH_RTL_SF_SHUTDOWN_BIT:
- + * Indicates that the request transport layer has been shut down or is
- + * being shut down and should not accept any new requests.
- + */
- +enum ssh_rtl_state_flags {
- + SSH_RTL_SF_SHUTDOWN_BIT,
- +};
- +
- +/**
- + * struct ssh_rtl_ops - Callback operations for request transport layer.
- + * @handle_event: Function called when a SSH event has been received. The
- + * specified function takes the request layer, received command
- + * struct, and corresponding payload as arguments. If the event
- + * has no payload, the payload span is empty (not %NULL).
- + */
- +struct ssh_rtl_ops {
- + void (*handle_event)(struct ssh_rtl *rtl, const struct ssh_command *cmd,
- + const struct ssam_span *data);
- +};
- +
- +/**
- + * struct ssh_rtl - SSH request transport layer.
- + * @ptl: Underlying packet transport layer.
- + * @state: State(-flags) of the transport layer.
- + * @queue: Request submission queue.
- + * @queue.lock: Lock for modifying the request submission queue.
- + * @queue.head: List-head of the request submission queue.
- + * @pending: Set/list of pending requests.
- + * @pending.lock: Lock for modifying the request set.
- + * @pending.head: List-head of the pending set/list.
- + * @pending.count: Number of currently pending requests.
- + * @tx: Transmitter subsystem.
- + * @tx.work: Transmitter work item.
- + * @rtx_timeout: Retransmission timeout subsystem.
- + * @rtx_timeout.lock: Lock for modifying the retransmission timeout reaper.
- + * @rtx_timeout.timeout: Timeout interval for retransmission.
- + * @rtx_timeout.expires: Time specifying when the reaper work is next scheduled.
- + * @rtx_timeout.reaper: Work performing timeout checks and subsequent actions.
- + * @ops: Request layer operations.
- + */
- +struct ssh_rtl {
- + struct ssh_ptl ptl;
- + unsigned long state;
- +
- + struct {
- + spinlock_t lock;
- + struct list_head head;
- + } queue;
- +
- + struct {
- + spinlock_t lock;
- + struct list_head head;
- + atomic_t count;
- + } pending;
- +
- + struct {
- + struct work_struct work;
- + } tx;
- +
- + struct {
- + spinlock_t lock;
- + ktime_t timeout;
- + ktime_t expires;
- + struct delayed_work reaper;
- + } rtx_timeout;
- +
- + struct ssh_rtl_ops ops;
- +};
- +
- +#define rtl_dbg(r, fmt, ...) ptl_dbg(&(r)->ptl, fmt, ##__VA_ARGS__)
- +#define rtl_info(p, fmt, ...) ptl_info(&(p)->ptl, fmt, ##__VA_ARGS__)
- +#define rtl_warn(r, fmt, ...) ptl_warn(&(r)->ptl, fmt, ##__VA_ARGS__)
- +#define rtl_err(r, fmt, ...) ptl_err(&(r)->ptl, fmt, ##__VA_ARGS__)
- +#define rtl_dbg_cond(r, fmt, ...) __ssam_prcond(rtl_dbg, r, fmt, ##__VA_ARGS__)
- +
- +#define to_ssh_rtl(ptr, member) \
- + container_of(ptr, struct ssh_rtl, member)
- +
- +/**
- + * ssh_rtl_get_device() - Get device associated with request transport layer.
- + * @rtl: The request transport layer.
- + *
- + * Return: Returns the device on which the given request transport layer
- + * builds upon.
- + */
- +static inline struct device *ssh_rtl_get_device(struct ssh_rtl *rtl)
- +{
- + return ssh_ptl_get_device(&rtl->ptl);
- +}
- +
- +/**
- + * ssh_request_rtl() - Get request transport layer associated with request.
- + * @rqst: The request to get the request transport layer reference for.
- + *
- + * Return: Returns the &struct ssh_rtl associated with the given SSH request.
- + */
- +static inline struct ssh_rtl *ssh_request_rtl(struct ssh_request *rqst)
- +{
- + struct ssh_ptl *ptl;
- +
- + ptl = READ_ONCE(rqst->packet.ptl);
- + return likely(ptl) ? to_ssh_rtl(ptl, ptl) : NULL;
- +}
- +
- +int ssh_rtl_submit(struct ssh_rtl *rtl, struct ssh_request *rqst);
- +bool ssh_rtl_cancel(struct ssh_request *rqst, bool pending);
- +
- +int ssh_rtl_init(struct ssh_rtl *rtl, struct serdev_device *serdev,
- + const struct ssh_rtl_ops *ops);
- +
- +int ssh_rtl_start(struct ssh_rtl *rtl);
- +int ssh_rtl_flush(struct ssh_rtl *rtl, unsigned long timeout);
- +void ssh_rtl_shutdown(struct ssh_rtl *rtl);
- +void ssh_rtl_destroy(struct ssh_rtl *rtl);
- +
- +int ssh_request_init(struct ssh_request *rqst, enum ssam_request_flags flags,
- + const struct ssh_request_ops *ops);
- +
- +#endif /* _SURFACE_AGGREGATOR_SSH_REQUEST_LAYER_H */
- diff --git a/include/linux/surface_aggregator/controller.h b/include/linux/surface_aggregator/controller.h
- new file mode 100644
- index 000000000000..f4b1ba887384
- --- /dev/null
- +++ b/include/linux/surface_aggregator/controller.h
- @@ -0,0 +1,824 @@
- +/* SPDX-License-Identifier: GPL-2.0+ */
- +/*
- + * Surface System Aggregator Module (SSAM) controller interface.
- + *
- + * Main communication interface for the SSAM EC. Provides a controller
- + * managing access and communication to and from the SSAM EC, as well as main
- + * communication structures and definitions.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#ifndef _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H
- +#define _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H
- +
- +#include <linux/completion.h>
- +#include <linux/device.h>
- +#include <linux/types.h>
- +
- +#include <linux/surface_aggregator/serial_hub.h>
- +
- +
- +/* -- Main data types and definitions --------------------------------------- */
- +
- +/**
- + * enum ssam_event_flags - Flags for enabling/disabling SAM events
- + * @SSAM_EVENT_SEQUENCED: The event will be sent via a sequenced data frame.
- + */
- +enum ssam_event_flags {
- + SSAM_EVENT_SEQUENCED = BIT(0),
- +};
- +
- +/**
- + * struct ssam_event - SAM event sent from the EC to the host.
- + * @target_category: Target category of the event source. See &enum ssam_ssh_tc.
- + * @target_id: Target ID of the event source.
- + * @command_id: Command ID of the event.
- + * @instance_id: Instance ID of the event source.
- + * @length: Length of the event payload in bytes.
- + * @data: Event payload data.
- + */
- +struct ssam_event {
- + u8 target_category;
- + u8 target_id;
- + u8 command_id;
- + u8 instance_id;
- + u16 length;
- + u8 data[];
- +};
- +
- +/**
- + * enum ssam_request_flags - Flags for SAM requests.
- + *
- + * @SSAM_REQUEST_HAS_RESPONSE:
- + * Specifies that the request expects a response. If not set, the request
- + * will be directly completed after its underlying packet has been
- + * transmitted. If set, the request transport system waits for a response
- + * of the request.
- + *
- + * @SSAM_REQUEST_UNSEQUENCED:
- + * Specifies that the request should be transmitted via an unsequenced
- + * packet. If set, the request must not have a response, meaning that this
- + * flag and the %SSAM_REQUEST_HAS_RESPONSE flag are mutually exclusive.
- + */
- +enum ssam_request_flags {
- + SSAM_REQUEST_HAS_RESPONSE = BIT(0),
- + SSAM_REQUEST_UNSEQUENCED = BIT(1),
- +};
- +
- +/**
- + * struct ssam_request - SAM request description.
- + * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
- + * @target_id: ID of the request's target.
- + * @command_id: Command ID of the request.
- + * @instance_id: Instance ID of the request's target.
- + * @flags: Flags for the request. See &enum ssam_request_flags.
- + * @length: Length of the request payload in bytes.
- + * @payload: Request payload data.
- + *
- + * This struct fully describes a SAM request with payload. It is intended to
- + * help set up the actual transport struct, e.g. &struct ssam_request_sync,
- + * and specifically its raw message data via ssam_request_write_data().
- + */
- +struct ssam_request {
- + u8 target_category;
- + u8 target_id;
- + u8 command_id;
- + u8 instance_id;
- + u16 flags;
- + u16 length;
- + const u8 *payload;
- +};
- +
- +/**
- + * struct ssam_response - Response buffer for SAM request.
- + * @capacity: Capacity of the buffer, in bytes.
- + * @length: Length of the actual data stored in the memory pointed to by
- + * @pointer, in bytes. Set by the transport system.
- + * @pointer: Pointer to the buffer's memory, storing the response payload data.
- + */
- +struct ssam_response {
- + size_t capacity;
- + size_t length;
- + u8 *pointer;
- +};
- +
- +struct ssam_controller;
- +
- +struct ssam_controller *ssam_get_controller(void);
- +struct ssam_controller *ssam_client_bind(struct device *client);
- +int ssam_client_link(struct ssam_controller *ctrl, struct device *client);
- +
- +struct device *ssam_controller_device(struct ssam_controller *c);
- +
- +struct ssam_controller *ssam_controller_get(struct ssam_controller *c);
- +void ssam_controller_put(struct ssam_controller *c);
- +
- +void ssam_controller_statelock(struct ssam_controller *c);
- +void ssam_controller_stateunlock(struct ssam_controller *c);
- +
- +ssize_t ssam_request_write_data(struct ssam_span *buf,
- + struct ssam_controller *ctrl,
- + const struct ssam_request *spec);
- +
- +
- +/* -- Synchronous request interface. ---------------------------------------- */
- +
- +/**
- + * struct ssam_request_sync - Synchronous SAM request struct.
- + * @base: Underlying SSH request.
- + * @comp: Completion used to signal full completion of the request. After the
- + * request has been submitted, this struct may only be modified or
- + * deallocated after the completion has been signaled.
- + * request has been submitted,
- + * @resp: Buffer to store the response.
- + * @status: Status of the request, set after the base request has been
- + * completed or has failed.
- + */
- +struct ssam_request_sync {
- + struct ssh_request base;
- + struct completion comp;
- + struct ssam_response *resp;
- + int status;
- +};
- +
- +int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
- + struct ssam_request_sync **rqst,
- + struct ssam_span *buffer);
- +
- +void ssam_request_sync_free(struct ssam_request_sync *rqst);
- +
- +int ssam_request_sync_init(struct ssam_request_sync *rqst,
- + enum ssam_request_flags flags);
- +
- +/**
- + * ssam_request_sync_set_data - Set message data of a synchronous request.
- + * @rqst: The request.
- + * @ptr: Pointer to the request message data.
- + * @len: Length of the request message data.
- + *
- + * Set the request message data of a synchronous request. The provided buffer
- + * needs to live until the request has been completed.
- + */
- +static inline void ssam_request_sync_set_data(struct ssam_request_sync *rqst,
- + u8 *ptr, size_t len)
- +{
- + ssh_request_set_data(&rqst->base, ptr, len);
- +}
- +
- +/**
- + * ssam_request_sync_set_resp - Set response buffer of a synchronous request.
- + * @rqst: The request.
- + * @resp: The response buffer.
- + *
- + * Sets the response buffer of a synchronous request. This buffer will store
- + * the response of the request after it has been completed. May be %NULL if no
- + * response is expected.
- + */
- +static inline void ssam_request_sync_set_resp(struct ssam_request_sync *rqst,
- + struct ssam_response *resp)
- +{
- + rqst->resp = resp;
- +}
- +
- +int ssam_request_sync_submit(struct ssam_controller *ctrl,
- + struct ssam_request_sync *rqst);
- +
- +/**
- + * ssam_request_sync_wait - Wait for completion of a synchronous request.
- + * @rqst: The request to wait for.
- + *
- + * Wait for completion and release of a synchronous request. After this
- + * function terminates, the request is guaranteed to have left the transport
- + * system. After successful submission of a request, this function must be
- + * called before accessing the response of the request, freeing the request,
- + * or freeing any of the buffers associated with the request.
- + *
- + * This function must not be called if the request has not been submitted yet
- + * and may lead to a deadlock/infinite wait if a subsequent request submission
- + * fails in that case, due to the completion never triggering.
- + *
- + * Return: Returns the status of the given request, which is set on completion
- + * of the packet. This value is zero on success and negative on failure.
- + */
- +static inline int ssam_request_sync_wait(struct ssam_request_sync *rqst)
- +{
- + wait_for_completion(&rqst->comp);
- + return rqst->status;
- +}
- +
- +int ssam_request_sync(struct ssam_controller *ctrl,
- + const struct ssam_request *spec,
- + struct ssam_response *rsp);
- +
- +int ssam_request_sync_with_buffer(struct ssam_controller *ctrl,
- + const struct ssam_request *spec,
- + struct ssam_response *rsp,
- + struct ssam_span *buf);
- +
- +/**
- + * ssam_request_sync_onstack - Execute a synchronous request on the stack.
- + * @ctrl: The controller via which the request is submitted.
- + * @rqst: The request specification.
- + * @rsp: The response buffer.
- + * @payload_len: The (maximum) request payload length.
- + *
- + * Allocates a synchronous request with specified payload length on the stack,
- + * fully initializes it via the provided request specification, submits it,
- + * and finally waits for its completion before returning its status. This
- + * helper macro essentially allocates the request message buffer on the stack
- + * and then calls ssam_request_sync_with_buffer().
- + *
- + * Note: The @payload_len parameter specifies the maximum payload length, used
- + * for buffer allocation. The actual payload length may be smaller.
- + *
- + * Return: Returns the status of the request or any failure during setup, i.e.
- + * zero on success and a negative value on failure.
- + */
- +#define ssam_request_sync_onstack(ctrl, rqst, rsp, payload_len) \
- + ({ \
- + u8 __data[SSH_COMMAND_MESSAGE_LENGTH(payload_len)]; \
- + struct ssam_span __buf = { &__data[0], ARRAY_SIZE(__data) }; \
- + \
- + ssam_request_sync_with_buffer(ctrl, rqst, rsp, &__buf); \
- + })
- +
- +/**
- + * __ssam_retry - Retry request in case of I/O errors or timeouts.
- + * @request: The request function to execute. Must return an integer.
- + * @n: Number of tries.
- + * @args: Arguments for the request function.
- + *
- + * Executes the given request function, i.e. calls @request. In case the
- + * request returns %-EREMOTEIO (indicates I/O error) or %-ETIMEDOUT (request
- + * or underlying packet timed out), @request will be re-executed again, up to
- + * @n times in total.
- + *
- + * Return: Returns the return value of the last execution of @request.
- + */
- +#define __ssam_retry(request, n, args...) \
- + ({ \
- + int __i, __s = 0; \
- + \
- + for (__i = (n); __i > 0; __i--) { \
- + __s = request(args); \
- + if (__s != -ETIMEDOUT && __s != -EREMOTEIO) \
- + break; \
- + } \
- + __s; \
- + })
- +
- +/**
- + * ssam_retry - Retry request in case of I/O errors or timeouts up to three
- + * times in total.
- + * @request: The request function to execute. Must return an integer.
- + * @args: Arguments for the request function.
- + *
- + * Executes the given request function, i.e. calls @request. In case the
- + * request returns %-EREMOTEIO (indicates I/O error) or -%ETIMEDOUT (request
- + * or underlying packet timed out), @request will be re-executed again, up to
- + * three times in total.
- + *
- + * See __ssam_retry() for a more generic macro for this purpose.
- + *
- + * Return: Returns the return value of the last execution of @request.
- + */
- +#define ssam_retry(request, args...) \
- + __ssam_retry(request, 3, args)
- +
- +/**
- + * struct ssam_request_spec - Blue-print specification of SAM request.
- + * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
- + * @target_id: ID of the request's target.
- + * @command_id: Command ID of the request.
- + * @instance_id: Instance ID of the request's target.
- + * @flags: Flags for the request. See &enum ssam_request_flags.
- + *
- + * Blue-print specification for a SAM request. This struct describes the
- + * unique static parameters of a request (i.e. type) without specifying any of
- + * its instance-specific data (e.g. payload). It is intended to be used as base
- + * for defining simple request functions via the
- + * ``SSAM_DEFINE_SYNC_REQUEST_x()`` family of macros.
- + */
- +struct ssam_request_spec {
- + u8 target_category;
- + u8 target_id;
- + u8 command_id;
- + u8 instance_id;
- + u8 flags;
- +};
- +
- +/**
- + * struct ssam_request_spec_md - Blue-print specification for multi-device SAM
- + * request.
- + * @target_category: Category of the request's target. See &enum ssam_ssh_tc.
- + * @command_id: Command ID of the request.
- + * @flags: Flags for the request. See &enum ssam_request_flags.
- + *
- + * Blue-print specification for a multi-device SAM request, i.e. a request
- + * that is applicable to multiple device instances, described by their
- + * individual target and instance IDs. This struct describes the unique static
- + * parameters of a request (i.e. type) without specifying any of its
- + * instance-specific data (e.g. payload) and without specifying any of its
- + * device specific IDs (i.e. target and instance ID). It is intended to be
- + * used as base for defining simple multi-device request functions via the
- + * ``SSAM_DEFINE_SYNC_REQUEST_MD_x()`` and ``SSAM_DEFINE_SYNC_REQUEST_CL_x()``
- + * families of macros.
- + */
- +struct ssam_request_spec_md {
- + u8 target_category;
- + u8 command_id;
- + u8 flags;
- +};
- +
- +/**
- + * SSAM_DEFINE_SYNC_REQUEST_N() - Define synchronous SAM request function
- + * with neither argument nor return value.
- + * @name: Name of the generated function.
- + * @spec: Specification (&struct ssam_request_spec) defining the request.
- + *
- + * Defines a function executing the synchronous SAM request specified by
- + * @spec, with the request having neither argument nor return value. The
- + * generated function takes care of setting up the request struct and buffer
- + * allocation, as well as execution of the request itself, returning once the
- + * request has been fully completed. The required transport buffer will be
- + * allocated on the stack.
- + *
- + * The generated function is defined as ``int name(struct ssam_controller
- + * *ctrl)``, returning the status of the request, which is zero on success and
- + * negative on failure. The ``ctrl`` parameter is the controller via which the
- + * request is being sent.
- + *
- + * Refer to ssam_request_sync_onstack() for more details on the behavior of
- + * the generated function.
- + */
- +#define SSAM_DEFINE_SYNC_REQUEST_N(name, spec...) \
- + int name(struct ssam_controller *ctrl) \
- + { \
- + struct ssam_request_spec s = (struct ssam_request_spec)spec; \
- + struct ssam_request rqst; \
- + \
- + rqst.target_category = s.target_category; \
- + rqst.target_id = s.target_id; \
- + rqst.command_id = s.command_id; \
- + rqst.instance_id = s.instance_id; \
- + rqst.flags = s.flags; \
- + rqst.length = 0; \
- + rqst.payload = NULL; \
- + \
- + return ssam_request_sync_onstack(ctrl, &rqst, NULL, 0); \
- + }
- +
- +/**
- + * SSAM_DEFINE_SYNC_REQUEST_W() - Define synchronous SAM request function with
- + * argument.
- + * @name: Name of the generated function.
- + * @atype: Type of the request's argument.
- + * @spec: Specification (&struct ssam_request_spec) defining the request.
- + *
- + * Defines a function executing the synchronous SAM request specified by
- + * @spec, with the request taking an argument of type @atype and having no
- + * return value. The generated function takes care of setting up the request
- + * struct, buffer allocation, as well as execution of the request itself,
- + * returning once the request has been fully completed. The required transport
- + * buffer will be allocated on the stack.
- + *
- + * The generated function is defined as ``int name(struct ssam_controller
- + * *ctrl, const atype *arg)``, returning the status of the request, which is
- + * zero on success and negative on failure. The ``ctrl`` parameter is the
- + * controller via which the request is sent. The request argument is specified
- + * via the ``arg`` pointer.
- + *
- + * Refer to ssam_request_sync_onstack() for more details on the behavior of
- + * the generated function.
- + */
- +#define SSAM_DEFINE_SYNC_REQUEST_W(name, atype, spec...) \
- + int name(struct ssam_controller *ctrl, const atype *arg) \
- + { \
- + struct ssam_request_spec s = (struct ssam_request_spec)spec; \
- + struct ssam_request rqst; \
- + \
- + rqst.target_category = s.target_category; \
- + rqst.target_id = s.target_id; \
- + rqst.command_id = s.command_id; \
- + rqst.instance_id = s.instance_id; \
- + rqst.flags = s.flags; \
- + rqst.length = sizeof(atype); \
- + rqst.payload = (u8 *)arg; \
- + \
- + return ssam_request_sync_onstack(ctrl, &rqst, NULL, \
- + sizeof(atype)); \
- + }
- +
- +/**
- + * SSAM_DEFINE_SYNC_REQUEST_R() - Define synchronous SAM request function with
- + * return value.
- + * @name: Name of the generated function.
- + * @rtype: Type of the request's return value.
- + * @spec: Specification (&struct ssam_request_spec) defining the request.
- + *
- + * Defines a function executing the synchronous SAM request specified by
- + * @spec, with the request taking no argument but having a return value of
- + * type @rtype. The generated function takes care of setting up the request
- + * and response structs, buffer allocation, as well as execution of the
- + * request itself, returning once the request has been fully completed. The
- + * required transport buffer will be allocated on the stack.
- + *
- + * The generated function is defined as ``int name(struct ssam_controller
- + * *ctrl, rtype *ret)``, returning the status of the request, which is zero on
- + * success and negative on failure. The ``ctrl`` parameter is the controller
- + * via which the request is sent. The request's return value is written to the
- + * memory pointed to by the ``ret`` parameter.
- + *
- + * Refer to ssam_request_sync_onstack() for more details on the behavior of
- + * the generated function.
- + */
- +#define SSAM_DEFINE_SYNC_REQUEST_R(name, rtype, spec...) \
- + int name(struct ssam_controller *ctrl, rtype *ret) \
- + { \
- + struct ssam_request_spec s = (struct ssam_request_spec)spec; \
- + struct ssam_request rqst; \
- + struct ssam_response rsp; \
- + int status; \
- + \
- + rqst.target_category = s.target_category; \
- + rqst.target_id = s.target_id; \
- + rqst.command_id = s.command_id; \
- + rqst.instance_id = s.instance_id; \
- + rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE; \
- + rqst.length = 0; \
- + rqst.payload = NULL; \
- + \
- + rsp.capacity = sizeof(rtype); \
- + rsp.length = 0; \
- + rsp.pointer = (u8 *)ret; \
- + \
- + status = ssam_request_sync_onstack(ctrl, &rqst, &rsp, 0); \
- + if (status) \
- + return status; \
- + \
- + if (rsp.length != sizeof(rtype)) { \
- + struct device *dev = ssam_controller_device(ctrl); \
- + dev_err(dev, \
- + "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
- + sizeof(rtype), rsp.length, rqst.target_category,\
- + rqst.command_id); \
- + return -EIO; \
- + } \
- + \
- + return 0; \
- + }
- +
- +/**
- + * SSAM_DEFINE_SYNC_REQUEST_MD_N() - Define synchronous multi-device SAM
- + * request function with neither argument nor return value.
- + * @name: Name of the generated function.
- + * @spec: Specification (&struct ssam_request_spec_md) defining the request.
- + *
- + * Defines a function executing the synchronous SAM request specified by
- + * @spec, with the request having neither argument nor return value. Device
- + * specifying parameters are not hard-coded, but instead must be provided to
- + * the function. The generated function takes care of setting up the request
- + * struct, buffer allocation, as well as execution of the request itself,
- + * returning once the request has been fully completed. The required transport
- + * buffer will be allocated on the stack.
- + *
- + * The generated function is defined as ``int name(struct ssam_controller
- + * *ctrl, u8 tid, u8 iid)``, returning the status of the request, which is
- + * zero on success and negative on failure. The ``ctrl`` parameter is the
- + * controller via which the request is sent, ``tid`` the target ID for the
- + * request, and ``iid`` the instance ID.
- + *
- + * Refer to ssam_request_sync_onstack() for more details on the behavior of
- + * the generated function.
- + */
- +#define SSAM_DEFINE_SYNC_REQUEST_MD_N(name, spec...) \
- + int name(struct ssam_controller *ctrl, u8 tid, u8 iid) \
- + { \
- + struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
- + struct ssam_request rqst; \
- + \
- + rqst.target_category = s.target_category; \
- + rqst.target_id = tid; \
- + rqst.command_id = s.command_id; \
- + rqst.instance_id = iid; \
- + rqst.flags = s.flags; \
- + rqst.length = 0; \
- + rqst.payload = NULL; \
- + \
- + return ssam_request_sync_onstack(ctrl, &rqst, NULL, 0); \
- + }
- +
- +/**
- + * SSAM_DEFINE_SYNC_REQUEST_MD_W() - Define synchronous multi-device SAM
- + * request function with argument.
- + * @name: Name of the generated function.
- + * @atype: Type of the request's argument.
- + * @spec: Specification (&struct ssam_request_spec_md) defining the request.
- + *
- + * Defines a function executing the synchronous SAM request specified by
- + * @spec, with the request taking an argument of type @atype and having no
- + * return value. Device specifying parameters are not hard-coded, but instead
- + * must be provided to the function. The generated function takes care of
- + * setting up the request struct, buffer allocation, as well as execution of
- + * the request itself, returning once the request has been fully completed.
- + * The required transport buffer will be allocated on the stack.
- + *
- + * The generated function is defined as ``int name(struct ssam_controller
- + * *ctrl, u8 tid, u8 iid, const atype *arg)``, returning the status of the
- + * request, which is zero on success and negative on failure. The ``ctrl``
- + * parameter is the controller via which the request is sent, ``tid`` the
- + * target ID for the request, and ``iid`` the instance ID. The request argument
- + * is specified via the ``arg`` pointer.
- + *
- + * Refer to ssam_request_sync_onstack() for more details on the behavior of
- + * the generated function.
- + */
- +#define SSAM_DEFINE_SYNC_REQUEST_MD_W(name, atype, spec...) \
- + int name(struct ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg)\
- + { \
- + struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
- + struct ssam_request rqst; \
- + \
- + rqst.target_category = s.target_category; \
- + rqst.target_id = tid; \
- + rqst.command_id = s.command_id; \
- + rqst.instance_id = iid; \
- + rqst.flags = s.flags; \
- + rqst.length = sizeof(atype); \
- + rqst.payload = (u8 *)arg; \
- + \
- + return ssam_request_sync_onstack(ctrl, &rqst, NULL, \
- + sizeof(atype)); \
- + }
- +
- +/**
- + * SSAM_DEFINE_SYNC_REQUEST_MD_R() - Define synchronous multi-device SAM
- + * request function with return value.
- + * @name: Name of the generated function.
- + * @rtype: Type of the request's return value.
- + * @spec: Specification (&struct ssam_request_spec_md) defining the request.
- + *
- + * Defines a function executing the synchronous SAM request specified by
- + * @spec, with the request taking no argument but having a return value of
- + * type @rtype. Device specifying parameters are not hard-coded, but instead
- + * must be provided to the function. The generated function takes care of
- + * setting up the request and response structs, buffer allocation, as well as
- + * execution of the request itself, returning once the request has been fully
- + * completed. The required transport buffer will be allocated on the stack.
- + *
- + * The generated function is defined as ``int name(struct ssam_controller
- + * *ctrl, u8 tid, u8 iid, rtype *ret)``, returning the status of the request,
- + * which is zero on success and negative on failure. The ``ctrl`` parameter is
- + * the controller via which the request is sent, ``tid`` the target ID for the
- + * request, and ``iid`` the instance ID. The request's return value is written
- + * to the memory pointed to by the ``ret`` parameter.
- + *
- + * Refer to ssam_request_sync_onstack() for more details on the behavior of
- + * the generated function.
- + */
- +#define SSAM_DEFINE_SYNC_REQUEST_MD_R(name, rtype, spec...) \
- + int name(struct ssam_controller *ctrl, u8 tid, u8 iid, rtype *ret) \
- + { \
- + struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
- + struct ssam_request rqst; \
- + struct ssam_response rsp; \
- + int status; \
- + \
- + rqst.target_category = s.target_category; \
- + rqst.target_id = tid; \
- + rqst.command_id = s.command_id; \
- + rqst.instance_id = iid; \
- + rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE; \
- + rqst.length = 0; \
- + rqst.payload = NULL; \
- + \
- + rsp.capacity = sizeof(rtype); \
- + rsp.length = 0; \
- + rsp.pointer = (u8 *)ret; \
- + \
- + status = ssam_request_sync_onstack(ctrl, &rqst, &rsp, 0); \
- + if (status) \
- + return status; \
- + \
- + if (rsp.length != sizeof(rtype)) { \
- + struct device *dev = ssam_controller_device(ctrl); \
- + dev_err(dev, \
- + "rqst: invalid response length, expected %zu, got %zu (tc: %#04x, cid: %#04x)", \
- + sizeof(rtype), rsp.length, rqst.target_category,\
- + rqst.command_id); \
- + return -EIO; \
- + } \
- + \
- + return 0; \
- + }
- +
- +
- +/* -- Event notifier/callbacks. --------------------------------------------- */
- +
- +#define SSAM_NOTIF_STATE_SHIFT 2
- +#define SSAM_NOTIF_STATE_MASK ((1 << SSAM_NOTIF_STATE_SHIFT) - 1)
- +
- +/**
- + * enum ssam_notif_flags - Flags used in return values from SSAM notifier
- + * callback functions.
- + *
- + * @SSAM_NOTIF_HANDLED:
- + * Indicates that the notification has been handled. This flag should be
- + * set by the handler if the handler can act/has acted upon the event
- + * provided to it. This flag should not be set if the handler is not a
- + * primary handler intended for the provided event.
- + *
- + * If this flag has not been set by any handler after the notifier chain
- + * has been traversed, a warning will be emitted, stating that the event
- + * has not been handled.
- + *
- + * @SSAM_NOTIF_STOP:
- + * Indicates that the notifier traversal should stop. If this flag is
- + * returned from a notifier callback, notifier chain traversal will
- + * immediately stop and any remaining notifiers will not be called. This
- + * flag is automatically set when ssam_notifier_from_errno() is called
- + * with a negative error value.
- + */
- +enum ssam_notif_flags {
- + SSAM_NOTIF_HANDLED = BIT(0),
- + SSAM_NOTIF_STOP = BIT(1),
- +};
- +
- +struct ssam_event_notifier;
- +
- +typedef u32 (*ssam_notifier_fn_t)(struct ssam_event_notifier *nf,
- + const struct ssam_event *event);
- +
- +/**
- + * struct ssam_notifier_block - Base notifier block for SSAM event
- + * notifications.
- + * @node: The node for the list of notifiers.
- + * @fn: The callback function of this notifier. This function takes the
- + * respective notifier block and event as input and should return
- + * a notifier value, which can either be obtained from the flags
- + * provided in &enum ssam_notif_flags, converted from a standard
- + * error value via ssam_notifier_from_errno(), or a combination of
- + * both (e.g. ``ssam_notifier_from_errno(e) | SSAM_NOTIF_HANDLED``).
- + * @priority: Priority value determining the order in which notifier callbacks
- + * will be called. A higher value means higher priority, i.e. the
- + * associated callback will be executed earlier than other (lower
- + * priority) callbacks.
- + */
- +struct ssam_notifier_block {
- + struct list_head node;
- + ssam_notifier_fn_t fn;
- + int priority;
- +};
- +
- +/**
- + * ssam_notifier_from_errno() - Convert standard error value to notifier
- + * return code.
- + * @err: The error code to convert, must be negative (in case of failure) or
- + * zero (in case of success).
- + *
- + * Return: Returns the notifier return value obtained by converting the
- + * specified @err value. In case @err is negative, the %SSAM_NOTIF_STOP flag
- + * will be set, causing notifier call chain traversal to abort.
- + */
- +static inline u32 ssam_notifier_from_errno(int err)
- +{
- + if (WARN_ON(err > 0) || err == 0)
- + return 0;
- + else
- + return ((-err) << SSAM_NOTIF_STATE_SHIFT) | SSAM_NOTIF_STOP;
- +}
- +
- +/**
- + * ssam_notifier_to_errno() - Convert notifier return code to standard error
- + * value.
- + * @ret: The notifier return value to convert.
- + *
- + * Return: Returns the negative error value encoded in @ret or zero if @ret
- + * indicates success.
- + */
- +static inline int ssam_notifier_to_errno(u32 ret)
- +{
- + return -(ret >> SSAM_NOTIF_STATE_SHIFT);
- +}
- +
- +
- +/* -- Event/notification registry. ------------------------------------------ */
- +
- +/**
- + * struct ssam_event_registry - Registry specification used for enabling events.
- + * @target_category: Target category for the event registry requests.
- + * @target_id: Target ID for the event registry requests.
- + * @cid_enable: Command ID for the event-enable request.
- + * @cid_disable: Command ID for the event-disable request.
- + *
- + * This struct describes a SAM event registry via the minimal collection of
- + * SAM IDs specifying the requests to use for enabling and disabling an event.
- + * The individual event to be enabled/disabled itself is specified via &struct
- + * ssam_event_id.
- + */
- +struct ssam_event_registry {
- + u8 target_category;
- + u8 target_id;
- + u8 cid_enable;
- + u8 cid_disable;
- +};
- +
- +/**
- + * struct ssam_event_id - Unique event ID used for enabling events.
- + * @target_category: Target category of the event source.
- + * @instance: Instance ID of the event source.
- + *
- + * This struct specifies the event to be enabled/disabled via an externally
- + * provided registry. It does not specify the registry to be used itself, this
- + * is done via &struct ssam_event_registry.
- + */
- +struct ssam_event_id {
- + u8 target_category;
- + u8 instance;
- +};
- +
- +/**
- + * enum ssam_event_mask - Flags specifying how events are matched to notifiers.
- + *
- + * @SSAM_EVENT_MASK_NONE:
- + * Run the callback for any event with matching target category. Do not
- + * do any additional filtering.
- + *
- + * @SSAM_EVENT_MASK_TARGET:
- + * In addition to filtering by target category, only execute the notifier
- + * callback for events with a target ID matching to the one of the
- + * registry used for enabling/disabling the event.
- + *
- + * @SSAM_EVENT_MASK_INSTANCE:
- + * In addition to filtering by target category, only execute the notifier
- + * callback for events with an instance ID matching to the instance ID
- + * used when enabling the event.
- + *
- + * @SSAM_EVENT_MASK_STRICT:
- + * Do all the filtering above.
- + */
- +enum ssam_event_mask {
- + SSAM_EVENT_MASK_TARGET = BIT(0),
- + SSAM_EVENT_MASK_INSTANCE = BIT(1),
- +
- + SSAM_EVENT_MASK_NONE = 0,
- + SSAM_EVENT_MASK_STRICT =
- + SSAM_EVENT_MASK_TARGET
- + | SSAM_EVENT_MASK_INSTANCE,
- +};
- +
- +/**
- + * SSAM_EVENT_REGISTRY() - Define a new event registry.
- + * @tc: Target category for the event registry requests.
- + * @tid: Target ID for the event registry requests.
- + * @cid_en: Command ID for the event-enable request.
- + * @cid_dis: Command ID for the event-disable request.
- + *
- + * Return: Returns the &struct ssam_event_registry specified by the given
- + * parameters.
- + */
- +#define SSAM_EVENT_REGISTRY(tc, tid, cid_en, cid_dis) \
- + ((struct ssam_event_registry) { \
- + .target_category = (tc), \
- + .target_id = (tid), \
- + .cid_enable = (cid_en), \
- + .cid_disable = (cid_dis), \
- + })
- +
- +#define SSAM_EVENT_REGISTRY_SAM \
- + SSAM_EVENT_REGISTRY(SSAM_SSH_TC_SAM, 0x01, 0x0b, 0x0c)
- +
- +#define SSAM_EVENT_REGISTRY_KIP \
- + SSAM_EVENT_REGISTRY(SSAM_SSH_TC_KIP, 0x02, 0x27, 0x28)
- +
- +#define SSAM_EVENT_REGISTRY_REG \
- + SSAM_EVENT_REGISTRY(SSAM_SSH_TC_REG, 0x02, 0x01, 0x02)
- +
- +/**
- + * struct ssam_event_notifier - Notifier block for SSAM events.
- + * @base: The base notifier block with callback function and priority.
- + * @event: The event for which this block will receive notifications.
- + * @event.reg: Registry via which the event will be enabled/disabled.
- + * @event.id: ID specifying the event.
- + * @event.mask: Flags determining how events are matched to the notifier.
- + * @event.flags: Flags used for enabling the event.
- + */
- +struct ssam_event_notifier {
- + struct ssam_notifier_block base;
- +
- + struct {
- + struct ssam_event_registry reg;
- + struct ssam_event_id id;
- + enum ssam_event_mask mask;
- + u8 flags;
- + } event;
- +};
- +
- +int ssam_notifier_register(struct ssam_controller *ctrl,
- + struct ssam_event_notifier *n);
- +
- +int ssam_notifier_unregister(struct ssam_controller *ctrl,
- + struct ssam_event_notifier *n);
- +
- +#endif /* _LINUX_SURFACE_AGGREGATOR_CONTROLLER_H */
- diff --git a/include/linux/surface_aggregator/serial_hub.h b/include/linux/surface_aggregator/serial_hub.h
- new file mode 100644
- index 000000000000..64276fbfa1d5
- --- /dev/null
- +++ b/include/linux/surface_aggregator/serial_hub.h
- @@ -0,0 +1,672 @@
- +/* SPDX-License-Identifier: GPL-2.0+ */
- +/*
- + * Surface Serial Hub (SSH) protocol and communication interface.
- + *
- + * Lower-level communication layers and SSH protocol definitions for the
- + * Surface System Aggregator Module (SSAM). Provides the interface for basic
- + * packet- and request-based communication with the SSAM EC via SSH.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#ifndef _LINUX_SURFACE_AGGREGATOR_SERIAL_HUB_H
- +#define _LINUX_SURFACE_AGGREGATOR_SERIAL_HUB_H
- +
- +#include <linux/crc-ccitt.h>
- +#include <linux/kref.h>
- +#include <linux/ktime.h>
- +#include <linux/list.h>
- +#include <linux/types.h>
- +
- +
- +/* -- Data structures for SAM-over-SSH communication. ----------------------- */
- +
- +/**
- + * enum ssh_frame_type - Frame types for SSH frames.
- + *
- + * @SSH_FRAME_TYPE_DATA_SEQ:
- + * Indicates a data frame, followed by a payload with the length specified
- + * in the ``struct ssh_frame.len`` field. This frame is sequenced, meaning
- + * that an ACK is required.
- + *
- + * @SSH_FRAME_TYPE_DATA_NSQ:
- + * Same as %SSH_FRAME_TYPE_DATA_SEQ, but unsequenced, meaning that the
- + * message does not have to be ACKed.
- + *
- + * @SSH_FRAME_TYPE_ACK:
- + * Indicates an ACK message.
- + *
- + * @SSH_FRAME_TYPE_NAK:
- + * Indicates an error response for previously sent frame. In general, this
- + * means that the frame and/or payload is malformed, e.g. a CRC is wrong.
- + * For command-type payloads, this can also mean that the command is
- + * invalid.
- + */
- +enum ssh_frame_type {
- + SSH_FRAME_TYPE_DATA_SEQ = 0x80,
- + SSH_FRAME_TYPE_DATA_NSQ = 0x00,
- + SSH_FRAME_TYPE_ACK = 0x40,
- + SSH_FRAME_TYPE_NAK = 0x04,
- +};
- +
- +/**
- + * struct ssh_frame - SSH communication frame.
- + * @type: The type of the frame. See &enum ssh_frame_type.
- + * @len: The length of the frame payload directly following the CRC for this
- + * frame. Does not include the final CRC for that payload.
- + * @seq: The sequence number for this message/exchange.
- + */
- +struct ssh_frame {
- + u8 type;
- + __le16 len;
- + u8 seq;
- +} __packed;
- +
- +static_assert(sizeof(struct ssh_frame) == 4);
- +
- +/*
- + * SSH_FRAME_MAX_PAYLOAD_SIZE - Maximum SSH frame payload length in bytes.
- + *
- + * This is the physical maximum length of the protocol. Implementations may
- + * set a more constrained limit.
- + */
- +#define SSH_FRAME_MAX_PAYLOAD_SIZE U16_MAX
- +
- +/**
- + * enum ssh_payload_type - Type indicator for the SSH payload.
- + * @SSH_PLD_TYPE_CMD: The payload is a command structure with optional command
- + * payload.
- + */
- +enum ssh_payload_type {
- + SSH_PLD_TYPE_CMD = 0x80,
- +};
- +
- +/**
- + * struct ssh_command - Payload of a command-type frame.
- + * @type: The type of the payload. See &enum ssh_payload_type. Should be
- + * SSH_PLD_TYPE_CMD for this struct.
- + * @tc: Command target category.
- + * @tid_out: Output target ID. Should be zero if this an incoming (EC to host)
- + * message.
- + * @tid_in: Input target ID. Should be zero if this is an outgoing (host to
- + * EC) message.
- + * @iid: Instance ID.
- + * @rqid: Request ID. Used to match requests with responses and differentiate
- + * between responses and events.
- + * @cid: Command ID.
- + */
- +struct ssh_command {
- + u8 type;
- + u8 tc;
- + u8 tid_out;
- + u8 tid_in;
- + u8 iid;
- + __le16 rqid;
- + u8 cid;
- +} __packed;
- +
- +static_assert(sizeof(struct ssh_command) == 8);
- +
- +/*
- + * SSH_COMMAND_MAX_PAYLOAD_SIZE - Maximum SSH command payload length in bytes.
- + *
- + * This is the physical maximum length of the protocol. Implementations may
- + * set a more constrained limit.
- + */
- +#define SSH_COMMAND_MAX_PAYLOAD_SIZE \
- + (SSH_FRAME_MAX_PAYLOAD_SIZE - sizeof(struct ssh_command))
- +
- +/*
- + * SSH_MSG_LEN_BASE - Base-length of a SSH message.
- + *
- + * This is the minimum number of bytes required to form a message. The actual
- + * message length is SSH_MSG_LEN_BASE plus the length of the frame payload.
- + */
- +#define SSH_MSG_LEN_BASE (sizeof(struct ssh_frame) + 3ull * sizeof(u16))
- +
- +/*
- + * SSH_MSG_LEN_CTRL - Length of a SSH control message.
- + *
- + * This is the length of a SSH control message, which is equal to a SSH
- + * message without any payload.
- + */
- +#define SSH_MSG_LEN_CTRL SSH_MSG_LEN_BASE
- +
- +/**
- + * SSH_MESSAGE_LENGTH() - Compute length of SSH message.
- + * @payload_size: Length of the payload inside the SSH frame.
- + *
- + * Return: Returns the length of a SSH message with payload of specified size.
- + */
- +#define SSH_MESSAGE_LENGTH(payload_size) (SSH_MSG_LEN_BASE + (payload_size))
- +
- +/**
- + * SSH_COMMAND_MESSAGE_LENGTH() - Compute length of SSH command message.
- + * @payload_size: Length of the command payload.
- + *
- + * Return: Returns the length of a SSH command message with command payload of
- + * specified size.
- + */
- +#define SSH_COMMAND_MESSAGE_LENGTH(payload_size) \
- + SSH_MESSAGE_LENGTH(sizeof(struct ssh_command) + (payload_size))
- +
- +/**
- + * SSH_MSGOFFSET_FRAME() - Compute offset in SSH message to specified field in
- + * frame.
- + * @field: The field for which the offset should be computed.
- + *
- + * Return: Returns the offset of the specified &struct ssh_frame field in the
- + * raw SSH message data as. Takes SYN bytes (u16) preceding the frame into
- + * account.
- + */
- +#define SSH_MSGOFFSET_FRAME(field) \
- + (sizeof(u16) + offsetof(struct ssh_frame, field))
- +
- +/**
- + * SSH_MSGOFFSET_COMMAND() - Compute offset in SSH message to specified field
- + * in command.
- + * @field: The field for which the offset should be computed.
- + *
- + * Return: Returns the offset of the specified &struct ssh_command field in
- + * the raw SSH message data. Takes SYN bytes (u16) preceding the frame and the
- + * frame CRC (u16) between frame and command into account.
- + */
- +#define SSH_MSGOFFSET_COMMAND(field) \
- + (2ull * sizeof(u16) + sizeof(struct ssh_frame) \
- + + offsetof(struct ssh_command, field))
- +
- +/*
- + * SSH_MSG_SYN - SSH message synchronization (SYN) bytes as u16.
- + */
- +#define SSH_MSG_SYN ((u16)0x55aa)
- +
- +/**
- + * ssh_crc() - Compute CRC for SSH messages.
- + * @buf: The pointer pointing to the data for which the CRC should be computed.
- + * @len: The length of the data for which the CRC should be computed.
- + *
- + * Return: Returns the CRC computed on the provided data, as used for SSH
- + * messages.
- + */
- +static inline u16 ssh_crc(const u8 *buf, size_t len)
- +{
- + return crc_ccitt_false(0xffff, buf, len);
- +}
- +
- +/*
- + * SSH_NUM_EVENTS - The number of reserved event IDs.
- + *
- + * The number of reserved event IDs, used for registering an SSH event
- + * handler. Valid event IDs are numbers below or equal to this value, with
- + * exception of zero, which is not an event ID. Thus, this is also the
- + * absolute maximum number of event handlers that can be registered.
- + */
- +#define SSH_NUM_EVENTS 34
- +
- +/*
- + * SSH_NUM_TARGETS - The number of communication targets used in the protocol.
- + */
- +#define SSH_NUM_TARGETS 2
- +
- +/**
- + * ssh_rqid_next_valid() - Return the next valid request ID.
- + * @rqid: The current request ID.
- + *
- + * Return: Returns the next valid request ID, following the current request ID
- + * provided to this function. This function skips any request IDs reserved for
- + * events.
- + */
- +static inline u16 ssh_rqid_next_valid(u16 rqid)
- +{
- + return rqid > 0 ? rqid + 1u : rqid + SSH_NUM_EVENTS + 1u;
- +}
- +
- +/**
- + * ssh_rqid_to_event() - Convert request ID to its corresponding event ID.
- + * @rqid: The request ID to convert.
- + */
- +static inline u16 ssh_rqid_to_event(u16 rqid)
- +{
- + return rqid - 1u;
- +}
- +
- +/**
- + * ssh_rqid_is_event() - Check if given request ID is a valid event ID.
- + * @rqid: The request ID to check.
- + */
- +static inline bool ssh_rqid_is_event(u16 rqid)
- +{
- + return ssh_rqid_to_event(rqid) < SSH_NUM_EVENTS;
- +}
- +
- +/**
- + * ssh_tc_to_rqid() - Convert target category to its corresponding request ID.
- + * @tc: The target category to convert.
- + */
- +static inline u16 ssh_tc_to_rqid(u8 tc)
- +{
- + return tc;
- +}
- +
- +/**
- + * ssh_tid_to_index() - Convert target ID to its corresponding target index.
- + * @tid: The target ID to convert.
- + */
- +static inline u8 ssh_tid_to_index(u8 tid)
- +{
- + return tid - 1u;
- +}
- +
- +/**
- + * ssh_tid_is_valid() - Check if target ID is valid/supported.
- + * @tid: The target ID to check.
- + */
- +static inline bool ssh_tid_is_valid(u8 tid)
- +{
- + return ssh_tid_to_index(tid) < SSH_NUM_TARGETS;
- +}
- +
- +/**
- + * struct ssam_span - Reference to a buffer region.
- + * @ptr: Pointer to the buffer region.
- + * @len: Length of the buffer region.
- + *
- + * A reference to a (non-owned) buffer segment, consisting of pointer and
- + * length. Use of this struct indicates non-owned data, i.e. data of which the
- + * life-time is managed (i.e. it is allocated/freed) via another pointer.
- + */
- +struct ssam_span {
- + u8 *ptr;
- + size_t len;
- +};
- +
- +/*
- + * Known SSH/EC target categories.
- + *
- + * List of currently known target category values; "Known" as in we know they
- + * exist and are valid on at least some device/model. Detailed functionality
- + * or the full category name is only known for some of these categories and
- + * is detailed in the respective comment below.
- + *
- + * These values and abbreviations have been extracted from strings inside the
- + * Windows driver.
- + */
- +enum ssam_ssh_tc {
- + /* Category 0x00 is invalid for EC use. */
- + SSAM_SSH_TC_SAM = 0x01, /* Generic system functionality, real-time clock. */
- + SSAM_SSH_TC_BAT = 0x02, /* Battery/power subsystem. */
- + SSAM_SSH_TC_TMP = 0x03, /* Thermal subsystem. */
- + SSAM_SSH_TC_PMC = 0x04,
- + SSAM_SSH_TC_FAN = 0x05,
- + SSAM_SSH_TC_PoM = 0x06,
- + SSAM_SSH_TC_DBG = 0x07,
- + SSAM_SSH_TC_KBD = 0x08, /* Legacy keyboard (Laptop 1/2). */
- + SSAM_SSH_TC_FWU = 0x09,
- + SSAM_SSH_TC_UNI = 0x0a,
- + SSAM_SSH_TC_LPC = 0x0b,
- + SSAM_SSH_TC_TCL = 0x0c,
- + SSAM_SSH_TC_SFL = 0x0d,
- + SSAM_SSH_TC_KIP = 0x0e,
- + SSAM_SSH_TC_EXT = 0x0f,
- + SSAM_SSH_TC_BLD = 0x10,
- + SSAM_SSH_TC_BAS = 0x11, /* Detachment system (Surface Book 2/3). */
- + SSAM_SSH_TC_SEN = 0x12,
- + SSAM_SSH_TC_SRQ = 0x13,
- + SSAM_SSH_TC_MCU = 0x14,
- + SSAM_SSH_TC_HID = 0x15, /* Generic HID input subsystem. */
- + SSAM_SSH_TC_TCH = 0x16,
- + SSAM_SSH_TC_BKL = 0x17,
- + SSAM_SSH_TC_TAM = 0x18,
- + SSAM_SSH_TC_ACC = 0x19,
- + SSAM_SSH_TC_UFI = 0x1a,
- + SSAM_SSH_TC_USC = 0x1b,
- + SSAM_SSH_TC_PEN = 0x1c,
- + SSAM_SSH_TC_VID = 0x1d,
- + SSAM_SSH_TC_AUD = 0x1e,
- + SSAM_SSH_TC_SMC = 0x1f,
- + SSAM_SSH_TC_KPD = 0x20,
- + SSAM_SSH_TC_REG = 0x21, /* Extended event registry. */
- +};
- +
- +
- +/* -- Packet transport layer (ptl). ----------------------------------------- */
- +
- +/**
- + * enum ssh_packet_base_priority - Base priorities for &struct ssh_packet.
- + * @SSH_PACKET_PRIORITY_FLUSH: Base priority for flush packets.
- + * @SSH_PACKET_PRIORITY_DATA: Base priority for normal data packets.
- + * @SSH_PACKET_PRIORITY_NAK: Base priority for NAK packets.
- + * @SSH_PACKET_PRIORITY_ACK: Base priority for ACK packets.
- + */
- +enum ssh_packet_base_priority {
- + SSH_PACKET_PRIORITY_FLUSH = 0, /* same as DATA to sequence flush */
- + SSH_PACKET_PRIORITY_DATA = 0,
- + SSH_PACKET_PRIORITY_NAK = 1,
- + SSH_PACKET_PRIORITY_ACK = 2,
- +};
- +
- +/*
- + * Same as SSH_PACKET_PRIORITY() below, only with actual values.
- + */
- +#define __SSH_PACKET_PRIORITY(base, try) \
- + (((base) << 4) | ((try) & 0x0f))
- +
- +/**
- + * SSH_PACKET_PRIORITY() - Compute packet priority from base priority and
- + * number of tries.
- + * @base: The base priority as suffix of &enum ssh_packet_base_priority, e.g.
- + * ``FLUSH``, ``DATA``, ``ACK``, or ``NAK``.
- + * @try: The number of tries (must be less than 16).
- + *
- + * Compute the combined packet priority. The combined priority is dominated by
- + * the base priority, whereas the number of (re-)tries decides the precedence
- + * of packets with the same base priority, giving higher priority to packets
- + * that already have more tries.
- + *
- + * Return: Returns the computed priority as value fitting inside a &u8. A
- + * higher number means a higher priority.
- + */
- +#define SSH_PACKET_PRIORITY(base, try) \
- + __SSH_PACKET_PRIORITY(SSH_PACKET_PRIORITY_##base, (try))
- +
- +/**
- + * ssh_packet_priority_get_try() - Get number of tries from packet priority.
- + * @priority: The packet priority.
- + *
- + * Return: Returns the number of tries encoded in the specified packet
- + * priority.
- + */
- +static inline u8 ssh_packet_priority_get_try(u8 priority)
- +{
- + return priority & 0x0f;
- +}
- +
- +/**
- + * ssh_packet_priority_get_base - Get base priority from packet priority.
- + * @priority: The packet priority.
- + *
- + * Return: Returns the base priority encoded in the given packet priority.
- + */
- +static inline u8 ssh_packet_priority_get_base(u8 priority)
- +{
- + return (priority & 0xf0) >> 4;
- +}
- +
- +enum ssh_packet_flags {
- + /* state flags */
- + SSH_PACKET_SF_LOCKED_BIT,
- + SSH_PACKET_SF_QUEUED_BIT,
- + SSH_PACKET_SF_PENDING_BIT,
- + SSH_PACKET_SF_TRANSMITTING_BIT,
- + SSH_PACKET_SF_TRANSMITTED_BIT,
- + SSH_PACKET_SF_ACKED_BIT,
- + SSH_PACKET_SF_CANCELED_BIT,
- + SSH_PACKET_SF_COMPLETED_BIT,
- +
- + /* type flags */
- + SSH_PACKET_TY_FLUSH_BIT,
- + SSH_PACKET_TY_SEQUENCED_BIT,
- + SSH_PACKET_TY_BLOCKING_BIT,
- +
- + /* mask for state flags */
- + SSH_PACKET_FLAGS_SF_MASK =
- + BIT(SSH_PACKET_SF_LOCKED_BIT)
- + | BIT(SSH_PACKET_SF_QUEUED_BIT)
- + | BIT(SSH_PACKET_SF_PENDING_BIT)
- + | BIT(SSH_PACKET_SF_TRANSMITTING_BIT)
- + | BIT(SSH_PACKET_SF_TRANSMITTED_BIT)
- + | BIT(SSH_PACKET_SF_ACKED_BIT)
- + | BIT(SSH_PACKET_SF_CANCELED_BIT)
- + | BIT(SSH_PACKET_SF_COMPLETED_BIT),
- +
- + /* mask for type flags */
- + SSH_PACKET_FLAGS_TY_MASK =
- + BIT(SSH_PACKET_TY_FLUSH_BIT)
- + | BIT(SSH_PACKET_TY_SEQUENCED_BIT)
- + | BIT(SSH_PACKET_TY_BLOCKING_BIT),
- +};
- +
- +struct ssh_ptl;
- +struct ssh_packet;
- +
- +/**
- + * struct ssh_packet_ops - Callback operations for a SSH packet.
- + * @release: Function called when the packet reference count reaches zero.
- + * This callback must be relied upon to ensure that the packet has
- + * left the transport system(s).
- + * @complete: Function called when the packet is completed, either with
- + * success or failure. In case of failure, the reason for the
- + * failure is indicated by the value of the provided status code
- + * argument. This value will be zero in case of success. Note that
- + * a call to this callback does not guarantee that the packet is
- + * not in use by the transport system any more.
- + */
- +struct ssh_packet_ops {
- + void (*release)(struct ssh_packet *p);
- + void (*complete)(struct ssh_packet *p, int status);
- +};
- +
- +/**
- + * struct ssh_packet - SSH transport packet.
- + * @ptl: Pointer to the packet transport layer. May be %NULL if the packet
- + * (or enclosing request) has not been submitted yet.
- + * @refcnt: Reference count of the packet.
- + * @priority: Priority of the packet. Must be computed via
- + * SSH_PACKET_PRIORITY(). Must only be accessed while holding the
- + * queue lock after first submission.
- + * @data: Raw message data.
- + * @data.len: Length of the raw message data.
- + * @data.ptr: Pointer to the raw message data buffer.
- + * @state: State and type flags describing current packet state (dynamic)
- + * and type (static). See &enum ssh_packet_flags for possible
- + * options.
- + * @timestamp: Timestamp specifying when the latest transmission of a
- + * currently pending packet has been started. May be %KTIME_MAX
- + * before or in-between transmission attempts. Used for the packet
- + * timeout implementation. Must only be accessed while holding the
- + * pending lock after first submission.
- + * @queue_node: The list node for the packet queue.
- + * @pending_node: The list node for the set of pending packets.
- + * @ops: Packet operations.
- + */
- +struct ssh_packet {
- + struct ssh_ptl *ptl;
- + struct kref refcnt;
- +
- + u8 priority;
- +
- + struct {
- + size_t len;
- + u8 *ptr;
- + } data;
- +
- + unsigned long state;
- + ktime_t timestamp;
- +
- + struct list_head queue_node;
- + struct list_head pending_node;
- +
- + const struct ssh_packet_ops *ops;
- +};
- +
- +struct ssh_packet *ssh_packet_get(struct ssh_packet *p);
- +void ssh_packet_put(struct ssh_packet *p);
- +
- +/**
- + * ssh_packet_set_data() - Set raw message data of packet.
- + * @p: The packet for which the message data should be set.
- + * @ptr: Pointer to the memory holding the message data.
- + * @len: Length of the message data.
- + *
- + * Sets the raw message data buffer of the packet to the provided memory. The
- + * memory is not copied. Instead, the caller is responsible for management
- + * (i.e. allocation and deallocation) of the memory. The caller must ensure
- + * that the provided memory is valid and contains a valid SSH message,
- + * starting from the time of submission of the packet until the ``release``
- + * callback has been called. During this time, the memory may not be altered
- + * in any way.
- + */
- +static inline void ssh_packet_set_data(struct ssh_packet *p, u8 *ptr, size_t len)
- +{
- + p->data.ptr = ptr;
- + p->data.len = len;
- +}
- +
- +
- +/* -- Request transport layer (rtl). ---------------------------------------- */
- +
- +enum ssh_request_flags {
- + /* state flags */
- + SSH_REQUEST_SF_LOCKED_BIT,
- + SSH_REQUEST_SF_QUEUED_BIT,
- + SSH_REQUEST_SF_PENDING_BIT,
- + SSH_REQUEST_SF_TRANSMITTING_BIT,
- + SSH_REQUEST_SF_TRANSMITTED_BIT,
- + SSH_REQUEST_SF_RSPRCVD_BIT,
- + SSH_REQUEST_SF_CANCELED_BIT,
- + SSH_REQUEST_SF_COMPLETED_BIT,
- +
- + /* type flags */
- + SSH_REQUEST_TY_FLUSH_BIT,
- + SSH_REQUEST_TY_HAS_RESPONSE_BIT,
- +
- + /* mask for state flags */
- + SSH_REQUEST_FLAGS_SF_MASK =
- + BIT(SSH_REQUEST_SF_LOCKED_BIT)
- + | BIT(SSH_REQUEST_SF_QUEUED_BIT)
- + | BIT(SSH_REQUEST_SF_PENDING_BIT)
- + | BIT(SSH_REQUEST_SF_TRANSMITTING_BIT)
- + | BIT(SSH_REQUEST_SF_TRANSMITTED_BIT)
- + | BIT(SSH_REQUEST_SF_RSPRCVD_BIT)
- + | BIT(SSH_REQUEST_SF_CANCELED_BIT)
- + | BIT(SSH_REQUEST_SF_COMPLETED_BIT),
- +
- + /* mask for type flags */
- + SSH_REQUEST_FLAGS_TY_MASK =
- + BIT(SSH_REQUEST_TY_FLUSH_BIT)
- + | BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT),
- +};
- +
- +struct ssh_rtl;
- +struct ssh_request;
- +
- +/**
- + * struct ssh_request_ops - Callback operations for a SSH request.
- + * @release: Function called when the request's reference count reaches zero.
- + * This callback must be relied upon to ensure that the request has
- + * left the transport systems (both, packet an request systems).
- + * @complete: Function called when the request is completed, either with
- + * success or failure. The command data for the request response
- + * is provided via the &struct ssh_command parameter (``cmd``),
- + * the command payload of the request response via the &struct
- + * ssh_span parameter (``data``).
- + *
- + * If the request does not have any response or has not been
- + * completed with success, both ``cmd`` and ``data`` parameters will
- + * be NULL. If the request response does not have any command
- + * payload, the ``data`` span will be an empty (zero-length) span.
- + *
- + * In case of failure, the reason for the failure is indicated by
- + * the value of the provided status code argument (``status``). This
- + * value will be zero in case of success and a regular errno
- + * otherwise.
- + *
- + * Note that a call to this callback does not guarantee that the
- + * request is not in use by the transport systems any more.
- + */
- +struct ssh_request_ops {
- + void (*release)(struct ssh_request *rqst);
- + void (*complete)(struct ssh_request *rqst,
- + const struct ssh_command *cmd,
- + const struct ssam_span *data, int status);
- +};
- +
- +/**
- + * struct ssh_request - SSH transport request.
- + * @packet: The underlying SSH transport packet.
- + * @node: List node for the request queue and pending set.
- + * @state: State and type flags describing current request state (dynamic)
- + * and type (static). See &enum ssh_request_flags for possible
- + * options.
- + * @timestamp: Timestamp specifying when we start waiting on the response of
- + * the request. This is set once the underlying packet has been
- + * completed and may be %KTIME_MAX before that, or when the request
- + * does not expect a response. Used for the request timeout
- + * implementation.
- + * @ops: Request Operations.
- + */
- +struct ssh_request {
- + struct ssh_packet packet;
- + struct list_head node;
- +
- + unsigned long state;
- + ktime_t timestamp;
- +
- + const struct ssh_request_ops *ops;
- +};
- +
- +/**
- + * to_ssh_request() - Cast a SSH packet to its enclosing SSH request.
- + * @p: The packet to cast.
- + *
- + * Casts the given &struct ssh_packet to its enclosing &struct ssh_request.
- + * The caller is responsible for making sure that the packet is actually
- + * wrapped in a &struct ssh_request.
- + *
- + * Return: Returns the &struct ssh_request wrapping the provided packet.
- + */
- +static inline struct ssh_request *to_ssh_request(struct ssh_packet *p)
- +{
- + return container_of(p, struct ssh_request, packet);
- +}
- +
- +/**
- + * ssh_request_get() - Increment reference count of request.
- + * @r: The request to increment the reference count of.
- + *
- + * Increments the reference count of the given request by incrementing the
- + * reference count of the underlying &struct ssh_packet, enclosed in it.
- + *
- + * See also ssh_request_put(), ssh_packet_get().
- + *
- + * Return: Returns the request provided as input.
- + */
- +static inline struct ssh_request *ssh_request_get(struct ssh_request *r)
- +{
- + return r ? to_ssh_request(ssh_packet_get(&r->packet)) : NULL;
- +}
- +
- +/**
- + * ssh_request_put() - Decrement reference count of request.
- + * @r: The request to decrement the reference count of.
- + *
- + * Decrements the reference count of the given request by decrementing the
- + * reference count of the underlying &struct ssh_packet, enclosed in it. If
- + * the reference count reaches zero, the ``release`` callback specified in the
- + * request's &struct ssh_request_ops, i.e. ``r->ops->release``, will be
- + * called.
- + *
- + * See also ssh_request_get(), ssh_packet_put().
- + */
- +static inline void ssh_request_put(struct ssh_request *r)
- +{
- + if (r)
- + ssh_packet_put(&r->packet);
- +}
- +
- +/**
- + * ssh_request_set_data() - Set raw message data of request.
- + * @r: The request for which the message data should be set.
- + * @ptr: Pointer to the memory holding the message data.
- + * @len: Length of the message data.
- + *
- + * Sets the raw message data buffer of the underlying packet to the specified
- + * buffer. Does not copy the actual message data, just sets the buffer pointer
- + * and length. Refer to ssh_packet_set_data() for more details.
- + */
- +static inline void ssh_request_set_data(struct ssh_request *r, u8 *ptr, size_t len)
- +{
- + ssh_packet_set_data(&r->packet, ptr, len);
- +}
- +
- +#endif /* _LINUX_SURFACE_AGGREGATOR_SERIAL_HUB_H */
- --
- 2.31.0
- From 8371b5f32f7ec7a9fedb20d1a7c8e18429e13633 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Mon, 21 Dec 2020 19:39:52 +0100
- Subject: [PATCH] platform/surface: aggregator: Add control packet allocation
- caching
- Surface Serial Hub communication is, in its core, packet based. Each
- sequenced packet requires to be acknowledged, via an ACK-type control
- packet. In case invalid data has been received by the driver, a NAK-type
- (not-acknowledge/negative acknowledge) control packet is sent,
- triggering retransmission.
- Control packets are therefore a core communication primitive and used
- frequently enough (with every sequenced packet transmission sent by the
- embedded controller, including events and request responses) that it may
- warrant caching their allocations to reduce possible memory
- fragmentation.
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Reviewed-by: Hans de Goede <hdegoede@redhat.com>
- Link: https://lore.kernel.org/r/20201221183959.1186143-3-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- drivers/platform/surface/aggregator/core.c | 27 ++++++++++-
- .../surface/aggregator/ssh_packet_layer.c | 47 +++++++++++++++----
- .../surface/aggregator/ssh_packet_layer.h | 3 ++
- 3 files changed, 67 insertions(+), 10 deletions(-)
- diff --git a/drivers/platform/surface/aggregator/core.c b/drivers/platform/surface/aggregator/core.c
- index 18e0e9e34e7b..60d312f71436 100644
- --- a/drivers/platform/surface/aggregator/core.c
- +++ b/drivers/platform/surface/aggregator/core.c
- @@ -780,7 +780,32 @@ static struct serdev_device_driver ssam_serial_hub = {
- .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- },
- };
- -module_serdev_device_driver(ssam_serial_hub);
- +
- +
- +/* -- Module setup. --------------------------------------------------------- */
- +
- +static int __init ssam_core_init(void)
- +{
- + int status;
- +
- + status = ssh_ctrl_packet_cache_init();
- + if (status)
- + return status;
- +
- + status = serdev_device_driver_register(&ssam_serial_hub);
- + if (status)
- + ssh_ctrl_packet_cache_destroy();
- +
- + return status;
- +}
- +module_init(ssam_core_init);
- +
- +static void __exit ssam_core_exit(void)
- +{
- + serdev_device_driver_unregister(&ssam_serial_hub);
- + ssh_ctrl_packet_cache_destroy();
- +}
- +module_exit(ssam_core_exit);
-
- MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- MODULE_DESCRIPTION("Subsystem and Surface Serial Hub driver for Surface System Aggregator Module");
- diff --git a/drivers/platform/surface/aggregator/ssh_packet_layer.c b/drivers/platform/surface/aggregator/ssh_packet_layer.c
- index 66e38fdc7963..23c2e31e7d0e 100644
- --- a/drivers/platform/surface/aggregator/ssh_packet_layer.c
- +++ b/drivers/platform/surface/aggregator/ssh_packet_layer.c
- @@ -303,24 +303,53 @@ void ssh_packet_init(struct ssh_packet *packet, unsigned long type,
- packet->ops = ops;
- }
-
- +static struct kmem_cache *ssh_ctrl_packet_cache;
- +
- +/**
- + * ssh_ctrl_packet_cache_init() - Initialize the control packet cache.
- + */
- +int ssh_ctrl_packet_cache_init(void)
- +{
- + const unsigned int size = sizeof(struct ssh_packet) + SSH_MSG_LEN_CTRL;
- + const unsigned int align = __alignof__(struct ssh_packet);
- + struct kmem_cache *cache;
- +
- + cache = kmem_cache_create("ssam_ctrl_packet", size, align, 0, NULL);
- + if (!cache)
- + return -ENOMEM;
- +
- + ssh_ctrl_packet_cache = cache;
- + return 0;
- +}
- +
- +/**
- + * ssh_ctrl_packet_cache_destroy() - Deinitialize the control packet cache.
- + */
- +void ssh_ctrl_packet_cache_destroy(void)
- +{
- + kmem_cache_destroy(ssh_ctrl_packet_cache);
- + ssh_ctrl_packet_cache = NULL;
- +}
- +
- /**
- - * ssh_ctrl_packet_alloc() - Allocate control packet.
- + * ssh_ctrl_packet_alloc() - Allocate packet from control packet cache.
- * @packet: Where the pointer to the newly allocated packet should be stored.
- * @buffer: The buffer corresponding to this packet.
- * @flags: Flags used for allocation.
- *
- - * Allocates a packet and corresponding transport buffer. Sets the packet's
- - * buffer reference to the allocated buffer. The packet must be freed via
- - * ssh_ctrl_packet_free(), which will also free the corresponding buffer. The
- - * corresponding buffer must not be freed separately. Intended to be used with
- - * %ssh_ptl_ctrl_packet_ops as packet operations.
- + * Allocates a packet and corresponding transport buffer from the control
- + * packet cache. Sets the packet's buffer reference to the allocated buffer.
- + * The packet must be freed via ssh_ctrl_packet_free(), which will also free
- + * the corresponding buffer. The corresponding buffer must not be freed
- + * separately. Intended to be used with %ssh_ptl_ctrl_packet_ops as packet
- + * operations.
- *
- * Return: Returns zero on success, %-ENOMEM if the allocation failed.
- */
- static int ssh_ctrl_packet_alloc(struct ssh_packet **packet,
- struct ssam_span *buffer, gfp_t flags)
- {
- - *packet = kzalloc(sizeof(**packet) + SSH_MSG_LEN_CTRL, flags);
- + *packet = kmem_cache_alloc(ssh_ctrl_packet_cache, flags);
- if (!*packet)
- return -ENOMEM;
-
- @@ -331,12 +360,12 @@ static int ssh_ctrl_packet_alloc(struct ssh_packet **packet,
- }
-
- /**
- - * ssh_ctrl_packet_free() - Free control packet.
- + * ssh_ctrl_packet_free() - Free packet allocated from control packet cache.
- * @p: The packet to free.
- */
- static void ssh_ctrl_packet_free(struct ssh_packet *p)
- {
- - kfree(p);
- + kmem_cache_free(ssh_ctrl_packet_cache, p);
- }
-
- static const struct ssh_packet_ops ssh_ptl_ctrl_packet_ops = {
- diff --git a/drivers/platform/surface/aggregator/ssh_packet_layer.h b/drivers/platform/surface/aggregator/ssh_packet_layer.h
- index 058f111292ca..e8757d03f279 100644
- --- a/drivers/platform/surface/aggregator/ssh_packet_layer.h
- +++ b/drivers/platform/surface/aggregator/ssh_packet_layer.h
- @@ -184,4 +184,7 @@ static inline void ssh_ptl_tx_wakeup_transfer(struct ssh_ptl *ptl)
- void ssh_packet_init(struct ssh_packet *packet, unsigned long type,
- u8 priority, const struct ssh_packet_ops *ops);
-
- +int ssh_ctrl_packet_cache_init(void);
- +void ssh_ctrl_packet_cache_destroy(void);
- +
- #endif /* _SURFACE_AGGREGATOR_SSH_PACKET_LAYER_H */
- --
- 2.31.0
- From dda656831cf11f505423f8edd5dd527f0606377c Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Mon, 21 Dec 2020 19:39:53 +0100
- Subject: [PATCH] platform/surface: aggregator: Add event item allocation
- caching
- Event items are used for completing Surface Aggregator EC events, i.e.
- placing event command data and payload on a workqueue for later
- processing to avoid doing said processing directly on the receiver
- thread. This means that event items are allocated for each incoming
- event, regardless of that event being transmitted via sequenced or
- unsequenced packets.
- On the Surface Book 3 and Surface Laptop 3, touchpad HID input events
- (unsequenced), can constitute a larger amount of traffic, and therefore
- allocation of event items. This warrants caching event items to reduce
- memory fragmentation. The size of the cached objects is specifically
- tuned to accommodate keyboard and touchpad input events and their
- payloads on those devices. As a result, this effectively also covers
- most other event types. In case of a larger event payload, event item
- allocation will fall back to kzalloc().
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Reviewed-by: Hans de Goede <hdegoede@redhat.com>
- Link: https://lore.kernel.org/r/20201221183959.1186143-4-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- .../platform/surface/aggregator/controller.c | 86 +++++++++++++++++--
- .../platform/surface/aggregator/controller.h | 9 ++
- drivers/platform/surface/aggregator/core.c | 16 +++-
- 3 files changed, 101 insertions(+), 10 deletions(-)
- diff --git a/drivers/platform/surface/aggregator/controller.c b/drivers/platform/surface/aggregator/controller.c
- index 488318cf2098..775a4509bece 100644
- --- a/drivers/platform/surface/aggregator/controller.c
- +++ b/drivers/platform/surface/aggregator/controller.c
- @@ -513,14 +513,74 @@ static void ssam_nf_destroy(struct ssam_nf *nf)
- */
- #define SSAM_CPLT_WQ_BATCH 10
-
- +/*
- + * SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN - Maximum payload length for a cached
- + * &struct ssam_event_item.
- + *
- + * This length has been chosen to be accommodate standard touchpad and
- + * keyboard input events. Events with larger payloads will be allocated
- + * separately.
- + */
- +#define SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN 32
- +
- +static struct kmem_cache *ssam_event_item_cache;
- +
- +/**
- + * ssam_event_item_cache_init() - Initialize the event item cache.
- + */
- +int ssam_event_item_cache_init(void)
- +{
- + const unsigned int size = sizeof(struct ssam_event_item)
- + + SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN;
- + const unsigned int align = __alignof__(struct ssam_event_item);
- + struct kmem_cache *cache;
- +
- + cache = kmem_cache_create("ssam_event_item", size, align, 0, NULL);
- + if (!cache)
- + return -ENOMEM;
- +
- + ssam_event_item_cache = cache;
- + return 0;
- +}
- +
- +/**
- + * ssam_event_item_cache_destroy() - Deinitialize the event item cache.
- + */
- +void ssam_event_item_cache_destroy(void)
- +{
- + kmem_cache_destroy(ssam_event_item_cache);
- + ssam_event_item_cache = NULL;
- +}
- +
- +static void __ssam_event_item_free_cached(struct ssam_event_item *item)
- +{
- + kmem_cache_free(ssam_event_item_cache, item);
- +}
- +
- +static void __ssam_event_item_free_generic(struct ssam_event_item *item)
- +{
- + kfree(item);
- +}
- +
- +/**
- + * ssam_event_item_free() - Free the provided event item.
- + * @item: The event item to free.
- + */
- +static void ssam_event_item_free(struct ssam_event_item *item)
- +{
- + item->ops.free(item);
- +}
- +
- /**
- * ssam_event_item_alloc() - Allocate an event item with the given payload size.
- * @len: The event payload length.
- * @flags: The flags used for allocation.
- *
- - * Allocate an event item with the given payload size. Sets the item
- - * operations and payload length values. The item free callback (``ops.free``)
- - * should not be overwritten after this call.
- + * Allocate an event item with the given payload size, preferring allocation
- + * from the event item cache if the payload is small enough (i.e. smaller than
- + * %SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN). Sets the item operations and payload
- + * length values. The item free callback (``ops.free``) should not be
- + * overwritten after this call.
- *
- * Return: Returns the newly allocated event item.
- */
- @@ -528,9 +588,19 @@ static struct ssam_event_item *ssam_event_item_alloc(size_t len, gfp_t flags)
- {
- struct ssam_event_item *item;
-
- - item = kzalloc(struct_size(item, event.data, len), flags);
- - if (!item)
- - return NULL;
- + if (len <= SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN) {
- + item = kmem_cache_alloc(ssam_event_item_cache, flags);
- + if (!item)
- + return NULL;
- +
- + item->ops.free = __ssam_event_item_free_cached;
- + } else {
- + item = kzalloc(struct_size(item, event.data, len), flags);
- + if (!item)
- + return NULL;
- +
- + item->ops.free = __ssam_event_item_free_generic;
- + }
-
- item->event.length = len;
- return item;
- @@ -692,7 +762,7 @@ static void ssam_event_queue_work_fn(struct work_struct *work)
- return;
-
- ssam_nf_call(nf, dev, item->rqid, &item->event);
- - kfree(item);
- + ssam_event_item_free(item);
- } while (--iterations);
-
- if (!ssam_event_queue_is_empty(queue))
- @@ -900,7 +970,7 @@ static void ssam_handle_event(struct ssh_rtl *rtl,
- memcpy(&item->event.data[0], data->ptr, data->len);
-
- if (WARN_ON(ssam_cplt_submit_event(&ctrl->cplt, item)))
- - kfree(item);
- + ssam_event_item_free(item);
- }
-
- static const struct ssh_rtl_ops ssam_rtl_ops = {
- diff --git a/drivers/platform/surface/aggregator/controller.h b/drivers/platform/surface/aggregator/controller.h
- index 5ee9e966f1d7..8297d34e7489 100644
- --- a/drivers/platform/surface/aggregator/controller.h
- +++ b/drivers/platform/surface/aggregator/controller.h
- @@ -80,12 +80,18 @@ struct ssam_cplt;
- * struct ssam_event_item - Struct for event queuing and completion.
- * @node: The node in the queue.
- * @rqid: The request ID of the event.
- + * @ops: Instance specific functions.
- + * @ops.free: Callback for freeing this event item.
- * @event: Actual event data.
- */
- struct ssam_event_item {
- struct list_head node;
- u16 rqid;
-
- + struct {
- + void (*free)(struct ssam_event_item *event);
- + } ops;
- +
- struct ssam_event event; /* must be last */
- };
-
- @@ -273,4 +279,7 @@ int ssam_ctrl_notif_d0_entry(struct ssam_controller *ctrl);
- int ssam_controller_suspend(struct ssam_controller *ctrl);
- int ssam_controller_resume(struct ssam_controller *ctrl);
-
- +int ssam_event_item_cache_init(void);
- +void ssam_event_item_cache_destroy(void);
- +
- #endif /* _SURFACE_AGGREGATOR_CONTROLLER_H */
- diff --git a/drivers/platform/surface/aggregator/core.c b/drivers/platform/surface/aggregator/core.c
- index 60d312f71436..37593234fb31 100644
- --- a/drivers/platform/surface/aggregator/core.c
- +++ b/drivers/platform/surface/aggregator/core.c
- @@ -790,12 +790,23 @@ static int __init ssam_core_init(void)
-
- status = ssh_ctrl_packet_cache_init();
- if (status)
- - return status;
- + goto err_cpkg;
- +
- + status = ssam_event_item_cache_init();
- + if (status)
- + goto err_evitem;
-
- status = serdev_device_driver_register(&ssam_serial_hub);
- if (status)
- - ssh_ctrl_packet_cache_destroy();
- + goto err_register;
-
- + return 0;
- +
- +err_register:
- + ssam_event_item_cache_destroy();
- +err_evitem:
- + ssh_ctrl_packet_cache_destroy();
- +err_cpkg:
- return status;
- }
- module_init(ssam_core_init);
- @@ -803,6 +814,7 @@ module_init(ssam_core_init);
- static void __exit ssam_core_exit(void)
- {
- serdev_device_driver_unregister(&ssam_serial_hub);
- + ssam_event_item_cache_destroy();
- ssh_ctrl_packet_cache_destroy();
- }
- module_exit(ssam_core_exit);
- --
- 2.31.0
- From 7375917efd1285a1f7345d3a1e61374890f139a4 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Mon, 21 Dec 2020 19:39:54 +0100
- Subject: [PATCH] platform/surface: aggregator: Add trace points
- Add trace points to the Surface Aggregator subsystem core. These trace
- points can be used to track packets, requests, and allocations. They are
- further intended for debugging and testing/validation, specifically in
- combination with the error injection capabilities introduced in the
- subsequent commit.
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Reviewed-by: Hans de Goede <hdegoede@redhat.com>
- Acked-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
- Link: https://lore.kernel.org/r/20201221183959.1186143-5-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- drivers/platform/surface/aggregator/Makefile | 3 +
- .../platform/surface/aggregator/controller.c | 5 +
- drivers/platform/surface/aggregator/core.c | 3 +
- .../surface/aggregator/ssh_packet_layer.c | 26 +-
- .../surface/aggregator/ssh_request_layer.c | 18 +
- drivers/platform/surface/aggregator/trace.h | 601 ++++++++++++++++++
- 6 files changed, 655 insertions(+), 1 deletion(-)
- create mode 100644 drivers/platform/surface/aggregator/trace.h
- diff --git a/drivers/platform/surface/aggregator/Makefile b/drivers/platform/surface/aggregator/Makefile
- index faad18d4a7f2..b8b24c8ec310 100644
- --- a/drivers/platform/surface/aggregator/Makefile
- +++ b/drivers/platform/surface/aggregator/Makefile
- @@ -1,6 +1,9 @@
- # SPDX-License-Identifier: GPL-2.0+
- # Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
-
- +# For include/trace/define_trace.h to include trace.h
- +CFLAGS_core.o = -I$(src)
- +
- obj-$(CONFIG_SURFACE_AGGREGATOR) += surface_aggregator.o
-
- surface_aggregator-objs := core.o
- diff --git a/drivers/platform/surface/aggregator/controller.c b/drivers/platform/surface/aggregator/controller.c
- index 775a4509bece..5bcb59ed579d 100644
- --- a/drivers/platform/surface/aggregator/controller.c
- +++ b/drivers/platform/surface/aggregator/controller.c
- @@ -32,6 +32,8 @@
- #include "ssh_msgb.h"
- #include "ssh_request_layer.h"
-
- +#include "trace.h"
- +
-
- /* -- Safe counters. -------------------------------------------------------- */
-
- @@ -568,6 +570,7 @@ static void __ssam_event_item_free_generic(struct ssam_event_item *item)
- */
- static void ssam_event_item_free(struct ssam_event_item *item)
- {
- + trace_ssam_event_item_free(item);
- item->ops.free(item);
- }
-
- @@ -603,6 +606,8 @@ static struct ssam_event_item *ssam_event_item_alloc(size_t len, gfp_t flags)
- }
-
- item->event.length = len;
- +
- + trace_ssam_event_item_alloc(item, len);
- return item;
- }
-
- diff --git a/drivers/platform/surface/aggregator/core.c b/drivers/platform/surface/aggregator/core.c
- index 37593234fb31..b6a9dea53592 100644
- --- a/drivers/platform/surface/aggregator/core.c
- +++ b/drivers/platform/surface/aggregator/core.c
- @@ -24,6 +24,9 @@
- #include <linux/surface_aggregator/controller.h>
- #include "controller.h"
-
- +#define CREATE_TRACE_POINTS
- +#include "trace.h"
- +
-
- /* -- Static controller reference. ------------------------------------------ */
-
- diff --git a/drivers/platform/surface/aggregator/ssh_packet_layer.c b/drivers/platform/surface/aggregator/ssh_packet_layer.c
- index 23c2e31e7d0e..c4f082e57372 100644
- --- a/drivers/platform/surface/aggregator/ssh_packet_layer.c
- +++ b/drivers/platform/surface/aggregator/ssh_packet_layer.c
- @@ -26,6 +26,8 @@
- #include "ssh_packet_layer.h"
- #include "ssh_parser.h"
-
- +#include "trace.h"
- +
- /*
- * To simplify reasoning about the code below, we define a few concepts. The
- * system below is similar to a state-machine for packets, however, there are
- @@ -228,6 +230,8 @@ static void __ssh_ptl_packet_release(struct kref *kref)
- {
- struct ssh_packet *p = container_of(kref, struct ssh_packet, refcnt);
-
- + trace_ssam_packet_release(p);
- +
- ptl_dbg_cond(p->ptl, "ptl: releasing packet %p\n", p);
- p->ops->release(p);
- }
- @@ -356,6 +360,7 @@ static int ssh_ctrl_packet_alloc(struct ssh_packet **packet,
- buffer->ptr = (u8 *)(*packet + 1);
- buffer->len = SSH_MSG_LEN_CTRL;
-
- + trace_ssam_ctrl_packet_alloc(*packet, buffer->len);
- return 0;
- }
-
- @@ -365,6 +370,7 @@ static int ssh_ctrl_packet_alloc(struct ssh_packet **packet,
- */
- static void ssh_ctrl_packet_free(struct ssh_packet *p)
- {
- + trace_ssam_ctrl_packet_free(p);
- kmem_cache_free(ssh_ctrl_packet_cache, p);
- }
-
- @@ -398,7 +404,12 @@ static void ssh_packet_next_try(struct ssh_packet *p)
-
- lockdep_assert_held(&p->ptl->queue.lock);
-
- - p->priority = __SSH_PACKET_PRIORITY(base, try + 1);
- + /*
- + * Ensure that we write the priority in one go via WRITE_ONCE() so we
- + * can access it via READ_ONCE() for tracing. Note that other access
- + * is guarded by the queue lock, so no need to use READ_ONCE() there.
- + */
- + WRITE_ONCE(p->priority, __SSH_PACKET_PRIORITY(base, try + 1));
- }
-
- /* Must be called with queue lock held. */
- @@ -560,6 +571,7 @@ static void __ssh_ptl_complete(struct ssh_packet *p, int status)
- {
- struct ssh_ptl *ptl = READ_ONCE(p->ptl);
-
- + trace_ssam_packet_complete(p, status);
- ptl_dbg_cond(ptl, "ptl: completing packet %p (status: %d)\n", p, status);
-
- if (p->ops->complete)
- @@ -1014,6 +1026,8 @@ int ssh_ptl_submit(struct ssh_ptl *ptl, struct ssh_packet *p)
- struct ssh_ptl *ptl_old;
- int status;
-
- + trace_ssam_packet_submit(p);
- +
- /* Validate packet fields. */
- if (test_bit(SSH_PACKET_TY_FLUSH_BIT, &p->state)) {
- if (p->data.ptr || test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &p->state))
- @@ -1065,6 +1079,8 @@ static int __ssh_ptl_resubmit(struct ssh_packet *packet)
-
- lockdep_assert_held(&packet->ptl->pending.lock);
-
- + trace_ssam_packet_resubmit(packet);
- +
- spin_lock(&packet->ptl->queue.lock);
-
- /* Check if the packet is out of tries. */
- @@ -1148,6 +1164,8 @@ void ssh_ptl_cancel(struct ssh_packet *p)
- if (test_and_set_bit(SSH_PACKET_SF_CANCELED_BIT, &p->state))
- return;
-
- + trace_ssam_packet_cancel(p);
- +
- /*
- * Lock packet and commit with memory barrier. If this packet has
- * already been locked, it's going to be removed and completed by
- @@ -1202,6 +1220,8 @@ static void ssh_ptl_timeout_reap(struct work_struct *work)
- bool resub = false;
- int status;
-
- + trace_ssam_ptl_timeout_reap(atomic_read(&ptl->pending.count));
- +
- /*
- * Mark reaper as "not pending". This is done before checking any
- * packets to avoid lost-update type problems.
- @@ -1224,6 +1244,8 @@ static void ssh_ptl_timeout_reap(struct work_struct *work)
- continue;
- }
-
- + trace_ssam_packet_timeout(p);
- +
- status = __ssh_ptl_resubmit(p);
-
- /*
- @@ -1416,6 +1438,8 @@ static size_t ssh_ptl_rx_eval(struct ssh_ptl *ptl, struct ssam_span *source)
- if (!frame) /* Not enough data. */
- return aligned.ptr - source->ptr;
-
- + trace_ssam_rx_frame_received(frame);
- +
- switch (frame->type) {
- case SSH_FRAME_TYPE_ACK:
- ssh_ptl_acknowledge(ptl, frame->seq);
- diff --git a/drivers/platform/surface/aggregator/ssh_request_layer.c b/drivers/platform/surface/aggregator/ssh_request_layer.c
- index 66c839a995f3..b649d71840fd 100644
- --- a/drivers/platform/surface/aggregator/ssh_request_layer.c
- +++ b/drivers/platform/surface/aggregator/ssh_request_layer.c
- @@ -22,6 +22,8 @@
- #include "ssh_packet_layer.h"
- #include "ssh_request_layer.h"
-
- +#include "trace.h"
- +
- /*
- * SSH_RTL_REQUEST_TIMEOUT - Request timeout.
- *
- @@ -144,6 +146,8 @@ static void ssh_rtl_complete_with_status(struct ssh_request *rqst, int status)
- {
- struct ssh_rtl *rtl = ssh_request_rtl(rqst);
-
- + trace_ssam_request_complete(rqst, status);
- +
- /* rtl/ptl may not be set if we're canceling before submitting. */
- rtl_dbg_cond(rtl, "rtl: completing request (rqid: %#06x, status: %d)\n",
- ssh_request_get_rqid_safe(rqst), status);
- @@ -157,6 +161,8 @@ static void ssh_rtl_complete_with_rsp(struct ssh_request *rqst,
- {
- struct ssh_rtl *rtl = ssh_request_rtl(rqst);
-
- + trace_ssam_request_complete(rqst, 0);
- +
- rtl_dbg(rtl, "rtl: completing request with response (rqid: %#06x)\n",
- ssh_request_get_rqid(rqst));
-
- @@ -329,6 +335,8 @@ static void ssh_rtl_tx_work_fn(struct work_struct *work)
- */
- int ssh_rtl_submit(struct ssh_rtl *rtl, struct ssh_request *rqst)
- {
- + trace_ssam_request_submit(rqst);
- +
- /*
- * Ensure that requests expecting a response are sequenced. If this
- * invariant ever changes, see the comment in ssh_rtl_complete() on what
- @@ -439,6 +447,8 @@ static void ssh_rtl_complete(struct ssh_rtl *rtl,
- struct ssh_request *p, *n;
- u16 rqid = get_unaligned_le16(&command->rqid);
-
- + trace_ssam_rx_response_received(command, command_data->len);
- +
- /*
- * Get request from pending based on request ID and mark it as response
- * received and locked.
- @@ -688,6 +698,8 @@ bool ssh_rtl_cancel(struct ssh_request *rqst, bool pending)
- if (test_and_set_bit(SSH_REQUEST_SF_CANCELED_BIT, &rqst->state))
- return true;
-
- + trace_ssam_request_cancel(rqst);
- +
- if (pending)
- canceled = ssh_rtl_cancel_pending(rqst);
- else
- @@ -779,6 +791,8 @@ static void ssh_rtl_timeout_reap(struct work_struct *work)
- ktime_t timeout = rtl->rtx_timeout.timeout;
- ktime_t next = KTIME_MAX;
-
- + trace_ssam_rtl_timeout_reap(atomic_read(&rtl->pending.count));
- +
- /*
- * Mark reaper as "not pending". This is done before checking any
- * requests to avoid lost-update type problems.
- @@ -822,6 +836,8 @@ static void ssh_rtl_timeout_reap(struct work_struct *work)
-
- /* Cancel and complete the request. */
- list_for_each_entry_safe(r, n, &claimed, node) {
- + trace_ssam_request_timeout(r);
- +
- /*
- * At this point we've removed the packet from pending. This
- * means that we've obtained the last (only) reference of the
- @@ -849,6 +865,8 @@ static void ssh_rtl_timeout_reap(struct work_struct *work)
- static void ssh_rtl_rx_event(struct ssh_rtl *rtl, const struct ssh_command *cmd,
- const struct ssam_span *data)
- {
- + trace_ssam_rx_event_received(cmd, data->len);
- +
- rtl_dbg(rtl, "rtl: handling event (rqid: %#06x)\n",
- get_unaligned_le16(&cmd->rqid));
-
- diff --git a/drivers/platform/surface/aggregator/trace.h b/drivers/platform/surface/aggregator/trace.h
- new file mode 100644
- index 000000000000..dcca8007d876
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/trace.h
- @@ -0,0 +1,601 @@
- +/* SPDX-License-Identifier: GPL-2.0+ */
- +/*
- + * Trace points for SSAM/SSH.
- + *
- + * Copyright (C) 2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#undef TRACE_SYSTEM
- +#define TRACE_SYSTEM surface_aggregator
- +
- +#if !defined(_SURFACE_AGGREGATOR_TRACE_H) || defined(TRACE_HEADER_MULTI_READ)
- +#define _SURFACE_AGGREGATOR_TRACE_H
- +
- +#include <linux/surface_aggregator/serial_hub.h>
- +
- +#include <asm/unaligned.h>
- +#include <linux/tracepoint.h>
- +
- +TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_DATA_SEQ);
- +TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_DATA_NSQ);
- +TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_ACK);
- +TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_NAK);
- +
- +TRACE_DEFINE_ENUM(SSH_PACKET_SF_LOCKED_BIT);
- +TRACE_DEFINE_ENUM(SSH_PACKET_SF_QUEUED_BIT);
- +TRACE_DEFINE_ENUM(SSH_PACKET_SF_PENDING_BIT);
- +TRACE_DEFINE_ENUM(SSH_PACKET_SF_TRANSMITTING_BIT);
- +TRACE_DEFINE_ENUM(SSH_PACKET_SF_TRANSMITTED_BIT);
- +TRACE_DEFINE_ENUM(SSH_PACKET_SF_ACKED_BIT);
- +TRACE_DEFINE_ENUM(SSH_PACKET_SF_CANCELED_BIT);
- +TRACE_DEFINE_ENUM(SSH_PACKET_SF_COMPLETED_BIT);
- +
- +TRACE_DEFINE_ENUM(SSH_PACKET_TY_FLUSH_BIT);
- +TRACE_DEFINE_ENUM(SSH_PACKET_TY_SEQUENCED_BIT);
- +TRACE_DEFINE_ENUM(SSH_PACKET_TY_BLOCKING_BIT);
- +
- +TRACE_DEFINE_ENUM(SSH_PACKET_FLAGS_SF_MASK);
- +TRACE_DEFINE_ENUM(SSH_PACKET_FLAGS_TY_MASK);
- +
- +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_LOCKED_BIT);
- +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_QUEUED_BIT);
- +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_PENDING_BIT);
- +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_TRANSMITTING_BIT);
- +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_TRANSMITTED_BIT);
- +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_RSPRCVD_BIT);
- +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_CANCELED_BIT);
- +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_COMPLETED_BIT);
- +
- +TRACE_DEFINE_ENUM(SSH_REQUEST_TY_FLUSH_BIT);
- +TRACE_DEFINE_ENUM(SSH_REQUEST_TY_HAS_RESPONSE_BIT);
- +
- +TRACE_DEFINE_ENUM(SSH_REQUEST_FLAGS_SF_MASK);
- +TRACE_DEFINE_ENUM(SSH_REQUEST_FLAGS_TY_MASK);
- +
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SAM);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_BAT);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_TMP);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_PMC);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_FAN);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_PoM);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_DBG);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_KBD);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_FWU);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_UNI);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_LPC);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_TCL);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SFL);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_KIP);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_EXT);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_BLD);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_BAS);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SEN);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SRQ);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_MCU);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_HID);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_TCH);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_BKL);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_TAM);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_ACC);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_UFI);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_USC);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_PEN);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_VID);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_AUD);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SMC);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_KPD);
- +TRACE_DEFINE_ENUM(SSAM_SSH_TC_REG);
- +
- +#define SSAM_PTR_UID_LEN 9
- +#define SSAM_U8_FIELD_NOT_APPLICABLE ((u16)-1)
- +#define SSAM_SEQ_NOT_APPLICABLE ((u16)-1)
- +#define SSAM_RQID_NOT_APPLICABLE ((u32)-1)
- +#define SSAM_SSH_TC_NOT_APPLICABLE 0
- +
- +#ifndef _SURFACE_AGGREGATOR_TRACE_HELPERS
- +#define _SURFACE_AGGREGATOR_TRACE_HELPERS
- +
- +/**
- + * ssam_trace_ptr_uid() - Convert the pointer to a non-pointer UID string.
- + * @ptr: The pointer to convert.
- + * @uid_str: A buffer of length SSAM_PTR_UID_LEN where the UID will be stored.
- + *
- + * Converts the given pointer into a UID string that is safe to be shared
- + * with userspace and logs, i.e. doesn't give away the real memory location.
- + */
- +static inline void ssam_trace_ptr_uid(const void *ptr, char *uid_str)
- +{
- + char buf[2 * sizeof(void *) + 1];
- +
- + BUILD_BUG_ON(ARRAY_SIZE(buf) < SSAM_PTR_UID_LEN);
- +
- + snprintf(buf, ARRAY_SIZE(buf), "%p", ptr);
- + memcpy(uid_str, &buf[ARRAY_SIZE(buf) - SSAM_PTR_UID_LEN],
- + SSAM_PTR_UID_LEN);
- +}
- +
- +/**
- + * ssam_trace_get_packet_seq() - Read the packet's sequence ID.
- + * @p: The packet.
- + *
- + * Return: Returns the packet's sequence ID (SEQ) field if present, or
- + * %SSAM_SEQ_NOT_APPLICABLE if not (e.g. flush packet).
- + */
- +static inline u16 ssam_trace_get_packet_seq(const struct ssh_packet *p)
- +{
- + if (!p->data.ptr || p->data.len < SSH_MESSAGE_LENGTH(0))
- + return SSAM_SEQ_NOT_APPLICABLE;
- +
- + return p->data.ptr[SSH_MSGOFFSET_FRAME(seq)];
- +}
- +
- +/**
- + * ssam_trace_get_request_id() - Read the packet's request ID.
- + * @p: The packet.
- + *
- + * Return: Returns the packet's request ID (RQID) field if the packet
- + * represents a request with command data, or %SSAM_RQID_NOT_APPLICABLE if not
- + * (e.g. flush request, control packet).
- + */
- +static inline u32 ssam_trace_get_request_id(const struct ssh_packet *p)
- +{
- + if (!p->data.ptr || p->data.len < SSH_COMMAND_MESSAGE_LENGTH(0))
- + return SSAM_RQID_NOT_APPLICABLE;
- +
- + return get_unaligned_le16(&p->data.ptr[SSH_MSGOFFSET_COMMAND(rqid)]);
- +}
- +
- +/**
- + * ssam_trace_get_request_tc() - Read the packet's request target category.
- + * @p: The packet.
- + *
- + * Return: Returns the packet's request target category (TC) field if the
- + * packet represents a request with command data, or %SSAM_TC_NOT_APPLICABLE
- + * if not (e.g. flush request, control packet).
- + */
- +static inline u32 ssam_trace_get_request_tc(const struct ssh_packet *p)
- +{
- + if (!p->data.ptr || p->data.len < SSH_COMMAND_MESSAGE_LENGTH(0))
- + return SSAM_SSH_TC_NOT_APPLICABLE;
- +
- + return get_unaligned_le16(&p->data.ptr[SSH_MSGOFFSET_COMMAND(tc)]);
- +}
- +
- +#endif /* _SURFACE_AGGREGATOR_TRACE_HELPERS */
- +
- +#define ssam_trace_get_command_field_u8(packet, field) \
- + ((!(packet) || (packet)->data.len < SSH_COMMAND_MESSAGE_LENGTH(0)) \
- + ? 0 : (packet)->data.ptr[SSH_MSGOFFSET_COMMAND(field)])
- +
- +#define ssam_show_generic_u8_field(value) \
- + __print_symbolic(value, \
- + { SSAM_U8_FIELD_NOT_APPLICABLE, "N/A" } \
- + )
- +
- +#define ssam_show_frame_type(ty) \
- + __print_symbolic(ty, \
- + { SSH_FRAME_TYPE_DATA_SEQ, "DSEQ" }, \
- + { SSH_FRAME_TYPE_DATA_NSQ, "DNSQ" }, \
- + { SSH_FRAME_TYPE_ACK, "ACK" }, \
- + { SSH_FRAME_TYPE_NAK, "NAK" } \
- + )
- +
- +#define ssam_show_packet_type(type) \
- + __print_flags(flags & SSH_PACKET_FLAGS_TY_MASK, "", \
- + { BIT(SSH_PACKET_TY_FLUSH_BIT), "F" }, \
- + { BIT(SSH_PACKET_TY_SEQUENCED_BIT), "S" }, \
- + { BIT(SSH_PACKET_TY_BLOCKING_BIT), "B" } \
- + )
- +
- +#define ssam_show_packet_state(state) \
- + __print_flags(flags & SSH_PACKET_FLAGS_SF_MASK, "", \
- + { BIT(SSH_PACKET_SF_LOCKED_BIT), "L" }, \
- + { BIT(SSH_PACKET_SF_QUEUED_BIT), "Q" }, \
- + { BIT(SSH_PACKET_SF_PENDING_BIT), "P" }, \
- + { BIT(SSH_PACKET_SF_TRANSMITTING_BIT), "S" }, \
- + { BIT(SSH_PACKET_SF_TRANSMITTED_BIT), "T" }, \
- + { BIT(SSH_PACKET_SF_ACKED_BIT), "A" }, \
- + { BIT(SSH_PACKET_SF_CANCELED_BIT), "C" }, \
- + { BIT(SSH_PACKET_SF_COMPLETED_BIT), "F" } \
- + )
- +
- +#define ssam_show_packet_seq(seq) \
- + __print_symbolic(seq, \
- + { SSAM_SEQ_NOT_APPLICABLE, "N/A" } \
- + )
- +
- +#define ssam_show_request_type(flags) \
- + __print_flags((flags) & SSH_REQUEST_FLAGS_TY_MASK, "", \
- + { BIT(SSH_REQUEST_TY_FLUSH_BIT), "F" }, \
- + { BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT), "R" } \
- + )
- +
- +#define ssam_show_request_state(flags) \
- + __print_flags((flags) & SSH_REQUEST_FLAGS_SF_MASK, "", \
- + { BIT(SSH_REQUEST_SF_LOCKED_BIT), "L" }, \
- + { BIT(SSH_REQUEST_SF_QUEUED_BIT), "Q" }, \
- + { BIT(SSH_REQUEST_SF_PENDING_BIT), "P" }, \
- + { BIT(SSH_REQUEST_SF_TRANSMITTING_BIT), "S" }, \
- + { BIT(SSH_REQUEST_SF_TRANSMITTED_BIT), "T" }, \
- + { BIT(SSH_REQUEST_SF_RSPRCVD_BIT), "A" }, \
- + { BIT(SSH_REQUEST_SF_CANCELED_BIT), "C" }, \
- + { BIT(SSH_REQUEST_SF_COMPLETED_BIT), "F" } \
- + )
- +
- +#define ssam_show_request_id(rqid) \
- + __print_symbolic(rqid, \
- + { SSAM_RQID_NOT_APPLICABLE, "N/A" } \
- + )
- +
- +#define ssam_show_ssh_tc(rqid) \
- + __print_symbolic(rqid, \
- + { SSAM_SSH_TC_NOT_APPLICABLE, "N/A" }, \
- + { SSAM_SSH_TC_SAM, "SAM" }, \
- + { SSAM_SSH_TC_BAT, "BAT" }, \
- + { SSAM_SSH_TC_TMP, "TMP" }, \
- + { SSAM_SSH_TC_PMC, "PMC" }, \
- + { SSAM_SSH_TC_FAN, "FAN" }, \
- + { SSAM_SSH_TC_PoM, "PoM" }, \
- + { SSAM_SSH_TC_DBG, "DBG" }, \
- + { SSAM_SSH_TC_KBD, "KBD" }, \
- + { SSAM_SSH_TC_FWU, "FWU" }, \
- + { SSAM_SSH_TC_UNI, "UNI" }, \
- + { SSAM_SSH_TC_LPC, "LPC" }, \
- + { SSAM_SSH_TC_TCL, "TCL" }, \
- + { SSAM_SSH_TC_SFL, "SFL" }, \
- + { SSAM_SSH_TC_KIP, "KIP" }, \
- + { SSAM_SSH_TC_EXT, "EXT" }, \
- + { SSAM_SSH_TC_BLD, "BLD" }, \
- + { SSAM_SSH_TC_BAS, "BAS" }, \
- + { SSAM_SSH_TC_SEN, "SEN" }, \
- + { SSAM_SSH_TC_SRQ, "SRQ" }, \
- + { SSAM_SSH_TC_MCU, "MCU" }, \
- + { SSAM_SSH_TC_HID, "HID" }, \
- + { SSAM_SSH_TC_TCH, "TCH" }, \
- + { SSAM_SSH_TC_BKL, "BKL" }, \
- + { SSAM_SSH_TC_TAM, "TAM" }, \
- + { SSAM_SSH_TC_ACC, "ACC" }, \
- + { SSAM_SSH_TC_UFI, "UFI" }, \
- + { SSAM_SSH_TC_USC, "USC" }, \
- + { SSAM_SSH_TC_PEN, "PEN" }, \
- + { SSAM_SSH_TC_VID, "VID" }, \
- + { SSAM_SSH_TC_AUD, "AUD" }, \
- + { SSAM_SSH_TC_SMC, "SMC" }, \
- + { SSAM_SSH_TC_KPD, "KPD" }, \
- + { SSAM_SSH_TC_REG, "REG" } \
- + )
- +
- +DECLARE_EVENT_CLASS(ssam_frame_class,
- + TP_PROTO(const struct ssh_frame *frame),
- +
- + TP_ARGS(frame),
- +
- + TP_STRUCT__entry(
- + __field(u8, type)
- + __field(u8, seq)
- + __field(u16, len)
- + ),
- +
- + TP_fast_assign(
- + __entry->type = frame->type;
- + __entry->seq = frame->seq;
- + __entry->len = get_unaligned_le16(&frame->len);
- + ),
- +
- + TP_printk("ty=%s, seq=%#04x, len=%u",
- + ssam_show_frame_type(__entry->type),
- + __entry->seq,
- + __entry->len
- + )
- +);
- +
- +#define DEFINE_SSAM_FRAME_EVENT(name) \
- + DEFINE_EVENT(ssam_frame_class, ssam_##name, \
- + TP_PROTO(const struct ssh_frame *frame), \
- + TP_ARGS(frame) \
- + )
- +
- +DECLARE_EVENT_CLASS(ssam_command_class,
- + TP_PROTO(const struct ssh_command *cmd, u16 len),
- +
- + TP_ARGS(cmd, len),
- +
- + TP_STRUCT__entry(
- + __field(u16, rqid)
- + __field(u16, len)
- + __field(u8, tc)
- + __field(u8, cid)
- + __field(u8, iid)
- + ),
- +
- + TP_fast_assign(
- + __entry->rqid = get_unaligned_le16(&cmd->rqid);
- + __entry->tc = cmd->tc;
- + __entry->cid = cmd->cid;
- + __entry->iid = cmd->iid;
- + __entry->len = len;
- + ),
- +
- + TP_printk("rqid=%#06x, tc=%s, cid=%#04x, iid=%#04x, len=%u",
- + __entry->rqid,
- + ssam_show_ssh_tc(__entry->tc),
- + __entry->cid,
- + __entry->iid,
- + __entry->len
- + )
- +);
- +
- +#define DEFINE_SSAM_COMMAND_EVENT(name) \
- + DEFINE_EVENT(ssam_command_class, ssam_##name, \
- + TP_PROTO(const struct ssh_command *cmd, u16 len), \
- + TP_ARGS(cmd, len) \
- + )
- +
- +DECLARE_EVENT_CLASS(ssam_packet_class,
- + TP_PROTO(const struct ssh_packet *packet),
- +
- + TP_ARGS(packet),
- +
- + TP_STRUCT__entry(
- + __field(unsigned long, state)
- + __array(char, uid, SSAM_PTR_UID_LEN)
- + __field(u8, priority)
- + __field(u16, length)
- + __field(u16, seq)
- + ),
- +
- + TP_fast_assign(
- + __entry->state = READ_ONCE(packet->state);
- + ssam_trace_ptr_uid(packet, __entry->uid);
- + __entry->priority = READ_ONCE(packet->priority);
- + __entry->length = packet->data.len;
- + __entry->seq = ssam_trace_get_packet_seq(packet);
- + ),
- +
- + TP_printk("uid=%s, seq=%s, ty=%s, pri=%#04x, len=%u, sta=%s",
- + __entry->uid,
- + ssam_show_packet_seq(__entry->seq),
- + ssam_show_packet_type(__entry->state),
- + __entry->priority,
- + __entry->length,
- + ssam_show_packet_state(__entry->state)
- + )
- +);
- +
- +#define DEFINE_SSAM_PACKET_EVENT(name) \
- + DEFINE_EVENT(ssam_packet_class, ssam_##name, \
- + TP_PROTO(const struct ssh_packet *packet), \
- + TP_ARGS(packet) \
- + )
- +
- +DECLARE_EVENT_CLASS(ssam_packet_status_class,
- + TP_PROTO(const struct ssh_packet *packet, int status),
- +
- + TP_ARGS(packet, status),
- +
- + TP_STRUCT__entry(
- + __field(unsigned long, state)
- + __field(int, status)
- + __array(char, uid, SSAM_PTR_UID_LEN)
- + __field(u8, priority)
- + __field(u16, length)
- + __field(u16, seq)
- + ),
- +
- + TP_fast_assign(
- + __entry->state = READ_ONCE(packet->state);
- + __entry->status = status;
- + ssam_trace_ptr_uid(packet, __entry->uid);
- + __entry->priority = READ_ONCE(packet->priority);
- + __entry->length = packet->data.len;
- + __entry->seq = ssam_trace_get_packet_seq(packet);
- + ),
- +
- + TP_printk("uid=%s, seq=%s, ty=%s, pri=%#04x, len=%u, sta=%s, status=%d",
- + __entry->uid,
- + ssam_show_packet_seq(__entry->seq),
- + ssam_show_packet_type(__entry->state),
- + __entry->priority,
- + __entry->length,
- + ssam_show_packet_state(__entry->state),
- + __entry->status
- + )
- +);
- +
- +#define DEFINE_SSAM_PACKET_STATUS_EVENT(name) \
- + DEFINE_EVENT(ssam_packet_status_class, ssam_##name, \
- + TP_PROTO(const struct ssh_packet *packet, int status), \
- + TP_ARGS(packet, status) \
- + )
- +
- +DECLARE_EVENT_CLASS(ssam_request_class,
- + TP_PROTO(const struct ssh_request *request),
- +
- + TP_ARGS(request),
- +
- + TP_STRUCT__entry(
- + __field(unsigned long, state)
- + __field(u32, rqid)
- + __array(char, uid, SSAM_PTR_UID_LEN)
- + __field(u8, tc)
- + __field(u16, cid)
- + __field(u16, iid)
- + ),
- +
- + TP_fast_assign(
- + const struct ssh_packet *p = &request->packet;
- +
- + /* Use packet for UID so we can match requests to packets. */
- + __entry->state = READ_ONCE(request->state);
- + __entry->rqid = ssam_trace_get_request_id(p);
- + ssam_trace_ptr_uid(p, __entry->uid);
- + __entry->tc = ssam_trace_get_request_tc(p);
- + __entry->cid = ssam_trace_get_command_field_u8(p, cid);
- + __entry->iid = ssam_trace_get_command_field_u8(p, iid);
- + ),
- +
- + TP_printk("uid=%s, rqid=%s, ty=%s, sta=%s, tc=%s, cid=%s, iid=%s",
- + __entry->uid,
- + ssam_show_request_id(__entry->rqid),
- + ssam_show_request_type(__entry->state),
- + ssam_show_request_state(__entry->state),
- + ssam_show_ssh_tc(__entry->tc),
- + ssam_show_generic_u8_field(__entry->cid),
- + ssam_show_generic_u8_field(__entry->iid)
- + )
- +);
- +
- +#define DEFINE_SSAM_REQUEST_EVENT(name) \
- + DEFINE_EVENT(ssam_request_class, ssam_##name, \
- + TP_PROTO(const struct ssh_request *request), \
- + TP_ARGS(request) \
- + )
- +
- +DECLARE_EVENT_CLASS(ssam_request_status_class,
- + TP_PROTO(const struct ssh_request *request, int status),
- +
- + TP_ARGS(request, status),
- +
- + TP_STRUCT__entry(
- + __field(unsigned long, state)
- + __field(u32, rqid)
- + __field(int, status)
- + __array(char, uid, SSAM_PTR_UID_LEN)
- + __field(u8, tc)
- + __field(u16, cid)
- + __field(u16, iid)
- + ),
- +
- + TP_fast_assign(
- + const struct ssh_packet *p = &request->packet;
- +
- + /* Use packet for UID so we can match requests to packets. */
- + __entry->state = READ_ONCE(request->state);
- + __entry->rqid = ssam_trace_get_request_id(p);
- + __entry->status = status;
- + ssam_trace_ptr_uid(p, __entry->uid);
- + __entry->tc = ssam_trace_get_request_tc(p);
- + __entry->cid = ssam_trace_get_command_field_u8(p, cid);
- + __entry->iid = ssam_trace_get_command_field_u8(p, iid);
- + ),
- +
- + TP_printk("uid=%s, rqid=%s, ty=%s, sta=%s, tc=%s, cid=%s, iid=%s, status=%d",
- + __entry->uid,
- + ssam_show_request_id(__entry->rqid),
- + ssam_show_request_type(__entry->state),
- + ssam_show_request_state(__entry->state),
- + ssam_show_ssh_tc(__entry->tc),
- + ssam_show_generic_u8_field(__entry->cid),
- + ssam_show_generic_u8_field(__entry->iid),
- + __entry->status
- + )
- +);
- +
- +#define DEFINE_SSAM_REQUEST_STATUS_EVENT(name) \
- + DEFINE_EVENT(ssam_request_status_class, ssam_##name, \
- + TP_PROTO(const struct ssh_request *request, int status),\
- + TP_ARGS(request, status) \
- + )
- +
- +DECLARE_EVENT_CLASS(ssam_alloc_class,
- + TP_PROTO(void *ptr, size_t len),
- +
- + TP_ARGS(ptr, len),
- +
- + TP_STRUCT__entry(
- + __field(size_t, len)
- + __array(char, uid, SSAM_PTR_UID_LEN)
- + ),
- +
- + TP_fast_assign(
- + __entry->len = len;
- + ssam_trace_ptr_uid(ptr, __entry->uid);
- + ),
- +
- + TP_printk("uid=%s, len=%zu", __entry->uid, __entry->len)
- +);
- +
- +#define DEFINE_SSAM_ALLOC_EVENT(name) \
- + DEFINE_EVENT(ssam_alloc_class, ssam_##name, \
- + TP_PROTO(void *ptr, size_t len), \
- + TP_ARGS(ptr, len) \
- + )
- +
- +DECLARE_EVENT_CLASS(ssam_free_class,
- + TP_PROTO(void *ptr),
- +
- + TP_ARGS(ptr),
- +
- + TP_STRUCT__entry(
- + __array(char, uid, SSAM_PTR_UID_LEN)
- + ),
- +
- + TP_fast_assign(
- + ssam_trace_ptr_uid(ptr, __entry->uid);
- + ),
- +
- + TP_printk("uid=%s", __entry->uid)
- +);
- +
- +#define DEFINE_SSAM_FREE_EVENT(name) \
- + DEFINE_EVENT(ssam_free_class, ssam_##name, \
- + TP_PROTO(void *ptr), \
- + TP_ARGS(ptr) \
- + )
- +
- +DECLARE_EVENT_CLASS(ssam_pending_class,
- + TP_PROTO(unsigned int pending),
- +
- + TP_ARGS(pending),
- +
- + TP_STRUCT__entry(
- + __field(unsigned int, pending)
- + ),
- +
- + TP_fast_assign(
- + __entry->pending = pending;
- + ),
- +
- + TP_printk("pending=%u", __entry->pending)
- +);
- +
- +#define DEFINE_SSAM_PENDING_EVENT(name) \
- + DEFINE_EVENT(ssam_pending_class, ssam_##name, \
- + TP_PROTO(unsigned int pending), \
- + TP_ARGS(pending) \
- + )
- +
- +DEFINE_SSAM_FRAME_EVENT(rx_frame_received);
- +DEFINE_SSAM_COMMAND_EVENT(rx_response_received);
- +DEFINE_SSAM_COMMAND_EVENT(rx_event_received);
- +
- +DEFINE_SSAM_PACKET_EVENT(packet_release);
- +DEFINE_SSAM_PACKET_EVENT(packet_submit);
- +DEFINE_SSAM_PACKET_EVENT(packet_resubmit);
- +DEFINE_SSAM_PACKET_EVENT(packet_timeout);
- +DEFINE_SSAM_PACKET_EVENT(packet_cancel);
- +DEFINE_SSAM_PACKET_STATUS_EVENT(packet_complete);
- +DEFINE_SSAM_PENDING_EVENT(ptl_timeout_reap);
- +
- +DEFINE_SSAM_REQUEST_EVENT(request_submit);
- +DEFINE_SSAM_REQUEST_EVENT(request_timeout);
- +DEFINE_SSAM_REQUEST_EVENT(request_cancel);
- +DEFINE_SSAM_REQUEST_STATUS_EVENT(request_complete);
- +DEFINE_SSAM_PENDING_EVENT(rtl_timeout_reap);
- +
- +DEFINE_SSAM_ALLOC_EVENT(ctrl_packet_alloc);
- +DEFINE_SSAM_FREE_EVENT(ctrl_packet_free);
- +
- +DEFINE_SSAM_ALLOC_EVENT(event_item_alloc);
- +DEFINE_SSAM_FREE_EVENT(event_item_free);
- +
- +#endif /* _SURFACE_AGGREGATOR_TRACE_H */
- +
- +/* This part must be outside protection */
- +#undef TRACE_INCLUDE_PATH
- +#undef TRACE_INCLUDE_FILE
- +
- +#define TRACE_INCLUDE_PATH .
- +#define TRACE_INCLUDE_FILE trace
- +
- +#include <trace/define_trace.h>
- --
- 2.31.0
- From 65bd02ca093d1733cb54549a9751638451b52384 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Mon, 21 Dec 2020 19:39:55 +0100
- Subject: [PATCH] platform/surface: aggregator: Add error injection
- capabilities
- This commit adds error injection hooks to the Surface Serial Hub
- communication protocol implementation, to:
- - simulate simple serial transmission errors,
- - drop packets, requests, and responses, simulating communication
- failures and potentially trigger retransmission timeouts, as well as
- - inject invalid data into submitted and received packets.
- Together with the trace points introduced in the previous commit, these
- facilities are intended to aid in testing, validation, and debugging of
- the Surface Aggregator communication layer.
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Reviewed-by: Hans de Goede <hdegoede@redhat.com>
- Acked-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
- Link: https://lore.kernel.org/r/20201221183959.1186143-6-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- drivers/platform/surface/aggregator/Kconfig | 14 +
- .../surface/aggregator/ssh_packet_layer.c | 296 +++++++++++++++++-
- .../surface/aggregator/ssh_request_layer.c | 35 +++
- drivers/platform/surface/aggregator/trace.h | 31 ++
- 4 files changed, 375 insertions(+), 1 deletion(-)
- diff --git a/drivers/platform/surface/aggregator/Kconfig b/drivers/platform/surface/aggregator/Kconfig
- index e9f4ad96e40a..e417bac67088 100644
- --- a/drivers/platform/surface/aggregator/Kconfig
- +++ b/drivers/platform/surface/aggregator/Kconfig
- @@ -40,3 +40,17 @@ menuconfig SURFACE_AGGREGATOR
- Choose m if you want to build the SAM subsystem core and SSH driver as
- module, y if you want to build it into the kernel and n if you don't
- want it at all.
- +
- +config SURFACE_AGGREGATOR_ERROR_INJECTION
- + bool "Surface System Aggregator Module Error Injection Capabilities"
- + depends on SURFACE_AGGREGATOR
- + depends on FUNCTION_ERROR_INJECTION
- + help
- + Provides error-injection capabilities for the Surface System
- + Aggregator Module subsystem and Surface Serial Hub driver.
- +
- + Specifically, exports error injection hooks to be used with the
- + kernel's function error injection capabilities to simulate underlying
- + transport and communication problems, such as invalid data sent to or
- + received from the EC, dropped data, and communication timeouts.
- + Intended for development and debugging.
- diff --git a/drivers/platform/surface/aggregator/ssh_packet_layer.c b/drivers/platform/surface/aggregator/ssh_packet_layer.c
- index c4f082e57372..74f0faaa2b27 100644
- --- a/drivers/platform/surface/aggregator/ssh_packet_layer.c
- +++ b/drivers/platform/surface/aggregator/ssh_packet_layer.c
- @@ -7,6 +7,7 @@
-
- #include <asm/unaligned.h>
- #include <linux/atomic.h>
- +#include <linux/error-injection.h>
- #include <linux/jiffies.h>
- #include <linux/kfifo.h>
- #include <linux/kref.h>
- @@ -226,6 +227,286 @@
- */
- #define SSH_PTL_RX_FIFO_LEN 4096
-
- +#ifdef CONFIG_SURFACE_AGGREGATOR_ERROR_INJECTION
- +
- +/**
- + * ssh_ptl_should_drop_ack_packet() - Error injection hook to drop ACK packets.
- + *
- + * Useful to test detection and handling of automated re-transmits by the EC.
- + * Specifically of packets that the EC considers not-ACKed but the driver
- + * already considers ACKed (due to dropped ACK). In this case, the EC
- + * re-transmits the packet-to-be-ACKed and the driver should detect it as
- + * duplicate/already handled. Note that the driver should still send an ACK
- + * for the re-transmitted packet.
- + */
- +static noinline bool ssh_ptl_should_drop_ack_packet(void)
- +{
- + return false;
- +}
- +ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_ack_packet, TRUE);
- +
- +/**
- + * ssh_ptl_should_drop_nak_packet() - Error injection hook to drop NAK packets.
- + *
- + * Useful to test/force automated (timeout-based) re-transmit by the EC.
- + * Specifically, packets that have not reached the driver completely/with valid
- + * checksums. Only useful in combination with receival of (injected) bad data.
- + */
- +static noinline bool ssh_ptl_should_drop_nak_packet(void)
- +{
- + return false;
- +}
- +ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_nak_packet, TRUE);
- +
- +/**
- + * ssh_ptl_should_drop_dsq_packet() - Error injection hook to drop sequenced
- + * data packet.
- + *
- + * Useful to test re-transmit timeout of the driver. If the data packet has not
- + * been ACKed after a certain time, the driver should re-transmit the packet up
- + * to limited number of times defined in SSH_PTL_MAX_PACKET_TRIES.
- + */
- +static noinline bool ssh_ptl_should_drop_dsq_packet(void)
- +{
- + return false;
- +}
- +ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_dsq_packet, TRUE);
- +
- +/**
- + * ssh_ptl_should_fail_write() - Error injection hook to make
- + * serdev_device_write() fail.
- + *
- + * Hook to simulate errors in serdev_device_write when transmitting packets.
- + */
- +static noinline int ssh_ptl_should_fail_write(void)
- +{
- + return 0;
- +}
- +ALLOW_ERROR_INJECTION(ssh_ptl_should_fail_write, ERRNO);
- +
- +/**
- + * ssh_ptl_should_corrupt_tx_data() - Error injection hook to simulate invalid
- + * data being sent to the EC.
- + *
- + * Hook to simulate corrupt/invalid data being sent from host (driver) to EC.
- + * Causes the packet data to be actively corrupted by overwriting it with
- + * pre-defined values, such that it becomes invalid, causing the EC to respond
- + * with a NAK packet. Useful to test handling of NAK packets received by the
- + * driver.
- + */
- +static noinline bool ssh_ptl_should_corrupt_tx_data(void)
- +{
- + return false;
- +}
- +ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_tx_data, TRUE);
- +
- +/**
- + * ssh_ptl_should_corrupt_rx_syn() - Error injection hook to simulate invalid
- + * data being sent by the EC.
- + *
- + * Hook to simulate invalid SYN bytes, i.e. an invalid start of messages and
- + * test handling thereof in the driver.
- + */
- +static noinline bool ssh_ptl_should_corrupt_rx_syn(void)
- +{
- + return false;
- +}
- +ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_rx_syn, TRUE);
- +
- +/**
- + * ssh_ptl_should_corrupt_rx_data() - Error injection hook to simulate invalid
- + * data being sent by the EC.
- + *
- + * Hook to simulate invalid data/checksum of the message frame and test handling
- + * thereof in the driver.
- + */
- +static noinline bool ssh_ptl_should_corrupt_rx_data(void)
- +{
- + return false;
- +}
- +ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_rx_data, TRUE);
- +
- +static bool __ssh_ptl_should_drop_ack_packet(struct ssh_packet *packet)
- +{
- + if (likely(!ssh_ptl_should_drop_ack_packet()))
- + return false;
- +
- + trace_ssam_ei_tx_drop_ack_packet(packet);
- + ptl_info(packet->ptl, "packet error injection: dropping ACK packet %p\n",
- + packet);
- +
- + return true;
- +}
- +
- +static bool __ssh_ptl_should_drop_nak_packet(struct ssh_packet *packet)
- +{
- + if (likely(!ssh_ptl_should_drop_nak_packet()))
- + return false;
- +
- + trace_ssam_ei_tx_drop_nak_packet(packet);
- + ptl_info(packet->ptl, "packet error injection: dropping NAK packet %p\n",
- + packet);
- +
- + return true;
- +}
- +
- +static bool __ssh_ptl_should_drop_dsq_packet(struct ssh_packet *packet)
- +{
- + if (likely(!ssh_ptl_should_drop_dsq_packet()))
- + return false;
- +
- + trace_ssam_ei_tx_drop_dsq_packet(packet);
- + ptl_info(packet->ptl,
- + "packet error injection: dropping sequenced data packet %p\n",
- + packet);
- +
- + return true;
- +}
- +
- +static bool ssh_ptl_should_drop_packet(struct ssh_packet *packet)
- +{
- + /* Ignore packets that don't carry any data (i.e. flush). */
- + if (!packet->data.ptr || !packet->data.len)
- + return false;
- +
- + switch (packet->data.ptr[SSH_MSGOFFSET_FRAME(type)]) {
- + case SSH_FRAME_TYPE_ACK:
- + return __ssh_ptl_should_drop_ack_packet(packet);
- +
- + case SSH_FRAME_TYPE_NAK:
- + return __ssh_ptl_should_drop_nak_packet(packet);
- +
- + case SSH_FRAME_TYPE_DATA_SEQ:
- + return __ssh_ptl_should_drop_dsq_packet(packet);
- +
- + default:
- + return false;
- + }
- +}
- +
- +static int ssh_ptl_write_buf(struct ssh_ptl *ptl, struct ssh_packet *packet,
- + const unsigned char *buf, size_t count)
- +{
- + int status;
- +
- + status = ssh_ptl_should_fail_write();
- + if (unlikely(status)) {
- + trace_ssam_ei_tx_fail_write(packet, status);
- + ptl_info(packet->ptl,
- + "packet error injection: simulating transmit error %d, packet %p\n",
- + status, packet);
- +
- + return status;
- + }
- +
- + return serdev_device_write_buf(ptl->serdev, buf, count);
- +}
- +
- +static void ssh_ptl_tx_inject_invalid_data(struct ssh_packet *packet)
- +{
- + /* Ignore packets that don't carry any data (i.e. flush). */
- + if (!packet->data.ptr || !packet->data.len)
- + return;
- +
- + /* Only allow sequenced data packets to be modified. */
- + if (packet->data.ptr[SSH_MSGOFFSET_FRAME(type)] != SSH_FRAME_TYPE_DATA_SEQ)
- + return;
- +
- + if (likely(!ssh_ptl_should_corrupt_tx_data()))
- + return;
- +
- + trace_ssam_ei_tx_corrupt_data(packet);
- + ptl_info(packet->ptl,
- + "packet error injection: simulating invalid transmit data on packet %p\n",
- + packet);
- +
- + /*
- + * NB: The value 0xb3 has been chosen more or less randomly so that it
- + * doesn't have any (major) overlap with the SYN bytes (aa 55) and is
- + * non-trivial (i.e. non-zero, non-0xff).
- + */
- + memset(packet->data.ptr, 0xb3, packet->data.len);
- +}
- +
- +static void ssh_ptl_rx_inject_invalid_syn(struct ssh_ptl *ptl,
- + struct ssam_span *data)
- +{
- + struct ssam_span frame;
- +
- + /* Check if there actually is something to corrupt. */
- + if (!sshp_find_syn(data, &frame))
- + return;
- +
- + if (likely(!ssh_ptl_should_corrupt_rx_syn()))
- + return;
- +
- + trace_ssam_ei_rx_corrupt_syn(data->len);
- +
- + data->ptr[1] = 0xb3; /* Set second byte of SYN to "random" value. */
- +}
- +
- +static void ssh_ptl_rx_inject_invalid_data(struct ssh_ptl *ptl,
- + struct ssam_span *frame)
- +{
- + size_t payload_len, message_len;
- + struct ssh_frame *sshf;
- +
- + /* Ignore incomplete messages, will get handled once it's complete. */
- + if (frame->len < SSH_MESSAGE_LENGTH(0))
- + return;
- +
- + /* Ignore incomplete messages, part 2. */
- + payload_len = get_unaligned_le16(&frame->ptr[SSH_MSGOFFSET_FRAME(len)]);
- + message_len = SSH_MESSAGE_LENGTH(payload_len);
- + if (frame->len < message_len)
- + return;
- +
- + if (likely(!ssh_ptl_should_corrupt_rx_data()))
- + return;
- +
- + sshf = (struct ssh_frame *)&frame->ptr[SSH_MSGOFFSET_FRAME(type)];
- + trace_ssam_ei_rx_corrupt_data(sshf);
- +
- + /*
- + * Flip bits in first byte of payload checksum. This is basically
- + * equivalent to a payload/frame data error without us having to worry
- + * about (the, arguably pretty small, probability of) accidental
- + * checksum collisions.
- + */
- + frame->ptr[frame->len - 2] = ~frame->ptr[frame->len - 2];
- +}
- +
- +#else /* CONFIG_SURFACE_AGGREGATOR_ERROR_INJECTION */
- +
- +static inline bool ssh_ptl_should_drop_packet(struct ssh_packet *packet)
- +{
- + return false;
- +}
- +
- +static inline int ssh_ptl_write_buf(struct ssh_ptl *ptl,
- + struct ssh_packet *packet,
- + const unsigned char *buf,
- + size_t count)
- +{
- + return serdev_device_write_buf(ptl->serdev, buf, count);
- +}
- +
- +static inline void ssh_ptl_tx_inject_invalid_data(struct ssh_packet *packet)
- +{
- +}
- +
- +static inline void ssh_ptl_rx_inject_invalid_syn(struct ssh_ptl *ptl,
- + struct ssam_span *data)
- +{
- +}
- +
- +static inline void ssh_ptl_rx_inject_invalid_data(struct ssh_ptl *ptl,
- + struct ssam_span *frame)
- +{
- +}
- +
- +#endif /* CONFIG_SURFACE_AGGREGATOR_ERROR_INJECTION */
- +
- static void __ssh_ptl_packet_release(struct kref *kref)
- {
- struct ssh_packet *p = container_of(kref, struct ssh_packet, refcnt);
- @@ -776,6 +1057,13 @@ static int ssh_ptl_tx_packet(struct ssh_ptl *ptl, struct ssh_packet *packet)
- if (unlikely(!packet->data.ptr))
- return 0;
-
- + /* Error injection: drop packet to simulate transmission problem. */
- + if (ssh_ptl_should_drop_packet(packet))
- + return 0;
- +
- + /* Error injection: simulate invalid packet data. */
- + ssh_ptl_tx_inject_invalid_data(packet);
- +
- ptl_dbg(ptl, "tx: sending data (length: %zu)\n", packet->data.len);
- print_hex_dump_debug("tx: ", DUMP_PREFIX_OFFSET, 16, 1,
- packet->data.ptr, packet->data.len, false);
- @@ -787,7 +1075,7 @@ static int ssh_ptl_tx_packet(struct ssh_ptl *ptl, struct ssh_packet *packet)
- buf = packet->data.ptr + offset;
- len = packet->data.len - offset;
-
- - status = serdev_device_write_buf(ptl->serdev, buf, len);
- + status = ssh_ptl_write_buf(ptl, packet, buf, len);
- if (status < 0)
- return status;
-
- @@ -1400,6 +1688,9 @@ static size_t ssh_ptl_rx_eval(struct ssh_ptl *ptl, struct ssam_span *source)
- bool syn_found;
- int status;
-
- + /* Error injection: Modify data to simulate corrupt SYN bytes. */
- + ssh_ptl_rx_inject_invalid_syn(ptl, source);
- +
- /* Find SYN. */
- syn_found = sshp_find_syn(source, &aligned);
-
- @@ -1430,6 +1721,9 @@ static size_t ssh_ptl_rx_eval(struct ssh_ptl *ptl, struct ssam_span *source)
- if (unlikely(!syn_found))
- return aligned.ptr - source->ptr;
-
- + /* Error injection: Modify data to simulate corruption. */
- + ssh_ptl_rx_inject_invalid_data(ptl, &aligned);
- +
- /* Parse and validate frame. */
- status = sshp_parse_frame(&ptl->serdev->dev, &aligned, &frame, &payload,
- SSH_PTL_RX_BUF_LEN);
- diff --git a/drivers/platform/surface/aggregator/ssh_request_layer.c b/drivers/platform/surface/aggregator/ssh_request_layer.c
- index b649d71840fd..bb1c862411a2 100644
- --- a/drivers/platform/surface/aggregator/ssh_request_layer.c
- +++ b/drivers/platform/surface/aggregator/ssh_request_layer.c
- @@ -8,6 +8,7 @@
- #include <asm/unaligned.h>
- #include <linux/atomic.h>
- #include <linux/completion.h>
- +#include <linux/error-injection.h>
- #include <linux/ktime.h>
- #include <linux/limits.h>
- #include <linux/list.h>
- @@ -58,6 +59,30 @@
- */
- #define SSH_RTL_TX_BATCH 10
-
- +#ifdef CONFIG_SURFACE_AGGREGATOR_ERROR_INJECTION
- +
- +/**
- + * ssh_rtl_should_drop_response() - Error injection hook to drop request
- + * responses.
- + *
- + * Useful to cause request transmission timeouts in the driver by dropping the
- + * response to a request.
- + */
- +static noinline bool ssh_rtl_should_drop_response(void)
- +{
- + return false;
- +}
- +ALLOW_ERROR_INJECTION(ssh_rtl_should_drop_response, TRUE);
- +
- +#else
- +
- +static inline bool ssh_rtl_should_drop_response(void)
- +{
- + return false;
- +}
- +
- +#endif
- +
- static u16 ssh_request_get_rqid(struct ssh_request *rqst)
- {
- return get_unaligned_le16(rqst->packet.data.ptr
- @@ -459,6 +484,16 @@ static void ssh_rtl_complete(struct ssh_rtl *rtl,
- if (unlikely(ssh_request_get_rqid(p) != rqid))
- continue;
-
- + /* Simulate response timeout. */
- + if (ssh_rtl_should_drop_response()) {
- + spin_unlock(&rtl->pending.lock);
- +
- + trace_ssam_ei_rx_drop_response(p);
- + rtl_info(rtl, "request error injection: dropping response for request %p\n",
- + &p->packet);
- + return;
- + }
- +
- /*
- * Mark as "response received" and "locked" as we're going to
- * complete it.
- diff --git a/drivers/platform/surface/aggregator/trace.h b/drivers/platform/surface/aggregator/trace.h
- index dcca8007d876..eb332bb53ae4 100644
- --- a/drivers/platform/surface/aggregator/trace.h
- +++ b/drivers/platform/surface/aggregator/trace.h
- @@ -565,6 +565,28 @@ DECLARE_EVENT_CLASS(ssam_pending_class,
- TP_ARGS(pending) \
- )
-
- +DECLARE_EVENT_CLASS(ssam_data_class,
- + TP_PROTO(size_t length),
- +
- + TP_ARGS(length),
- +
- + TP_STRUCT__entry(
- + __field(size_t, length)
- + ),
- +
- + TP_fast_assign(
- + __entry->length = length;
- + ),
- +
- + TP_printk("length=%zu", __entry->length)
- +);
- +
- +#define DEFINE_SSAM_DATA_EVENT(name) \
- + DEFINE_EVENT(ssam_data_class, ssam_##name, \
- + TP_PROTO(size_t length), \
- + TP_ARGS(length) \
- + )
- +
- DEFINE_SSAM_FRAME_EVENT(rx_frame_received);
- DEFINE_SSAM_COMMAND_EVENT(rx_response_received);
- DEFINE_SSAM_COMMAND_EVENT(rx_event_received);
- @@ -583,6 +605,15 @@ DEFINE_SSAM_REQUEST_EVENT(request_cancel);
- DEFINE_SSAM_REQUEST_STATUS_EVENT(request_complete);
- DEFINE_SSAM_PENDING_EVENT(rtl_timeout_reap);
-
- +DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_ack_packet);
- +DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_nak_packet);
- +DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_dsq_packet);
- +DEFINE_SSAM_PACKET_STATUS_EVENT(ei_tx_fail_write);
- +DEFINE_SSAM_PACKET_EVENT(ei_tx_corrupt_data);
- +DEFINE_SSAM_DATA_EVENT(ei_rx_corrupt_syn);
- +DEFINE_SSAM_FRAME_EVENT(ei_rx_corrupt_data);
- +DEFINE_SSAM_REQUEST_EVENT(ei_rx_drop_response);
- +
- DEFINE_SSAM_ALLOC_EVENT(ctrl_packet_alloc);
- DEFINE_SSAM_FREE_EVENT(ctrl_packet_free);
-
- --
- 2.31.0
- From b13806dafeff14be6cdaad9736d7c9327fe822ac Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Mon, 21 Dec 2020 19:39:56 +0100
- Subject: [PATCH] platform/surface: aggregator: Add dedicated bus and device
- type
- The Surface Aggregator EC provides varying functionality, depending on
- the Surface device. To manage this functionality, we use dedicated
- client devices for each subsystem or virtual device of the EC. While
- some of these clients are described as standard devices in ACPI and the
- corresponding client drivers can be implemented as platform drivers in
- the kernel (making use of the controller API already present), many
- devices, especially on newer Surface models, cannot be found there.
- To simplify management of these devices, we introduce a new bus and
- client device type for the Surface Aggregator subsystem. The new device
- type takes care of managing the controller reference, essentially
- guaranteeing its validity for as long as the client device exists, thus
- alleviating the need to manually establish device links for that purpose
- in the client driver (as has to be done with the platform devices).
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Reviewed-by: Hans de Goede <hdegoede@redhat.com>
- Link: https://lore.kernel.org/r/20201221183959.1186143-7-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- drivers/platform/surface/aggregator/Kconfig | 12 +
- drivers/platform/surface/aggregator/Makefile | 4 +
- drivers/platform/surface/aggregator/bus.c | 415 ++++++++++++++++++
- drivers/platform/surface/aggregator/bus.h | 27 ++
- drivers/platform/surface/aggregator/core.c | 12 +
- include/linux/mod_devicetable.h | 18 +
- include/linux/surface_aggregator/device.h | 423 +++++++++++++++++++
- scripts/mod/devicetable-offsets.c | 8 +
- scripts/mod/file2alias.c | 23 +
- 9 files changed, 942 insertions(+)
- create mode 100644 drivers/platform/surface/aggregator/bus.c
- create mode 100644 drivers/platform/surface/aggregator/bus.h
- create mode 100644 include/linux/surface_aggregator/device.h
- diff --git a/drivers/platform/surface/aggregator/Kconfig b/drivers/platform/surface/aggregator/Kconfig
- index e417bac67088..3aaeea9f0433 100644
- --- a/drivers/platform/surface/aggregator/Kconfig
- +++ b/drivers/platform/surface/aggregator/Kconfig
- @@ -41,6 +41,18 @@ menuconfig SURFACE_AGGREGATOR
- module, y if you want to build it into the kernel and n if you don't
- want it at all.
-
- +config SURFACE_AGGREGATOR_BUS
- + bool "Surface System Aggregator Module Bus"
- + depends on SURFACE_AGGREGATOR
- + default y
- + help
- + Expands the Surface System Aggregator Module (SSAM) core driver by
- + providing a dedicated bus and client-device type.
- +
- + This bus and device type are intended to provide and simplify support
- + for non-platform and non-ACPI SSAM devices, i.e. SSAM devices that are
- + not auto-detectable via the conventional means (e.g. ACPI).
- +
- config SURFACE_AGGREGATOR_ERROR_INJECTION
- bool "Surface System Aggregator Module Error Injection Capabilities"
- depends on SURFACE_AGGREGATOR
- diff --git a/drivers/platform/surface/aggregator/Makefile b/drivers/platform/surface/aggregator/Makefile
- index b8b24c8ec310..c112e2c7112b 100644
- --- a/drivers/platform/surface/aggregator/Makefile
- +++ b/drivers/platform/surface/aggregator/Makefile
- @@ -11,3 +11,7 @@ surface_aggregator-objs += ssh_parser.o
- surface_aggregator-objs += ssh_packet_layer.o
- surface_aggregator-objs += ssh_request_layer.o
- surface_aggregator-objs += controller.o
- +
- +ifeq ($(CONFIG_SURFACE_AGGREGATOR_BUS),y)
- +surface_aggregator-objs += bus.o
- +endif
- diff --git a/drivers/platform/surface/aggregator/bus.c b/drivers/platform/surface/aggregator/bus.c
- new file mode 100644
- index 000000000000..a9b660af0917
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/bus.c
- @@ -0,0 +1,415 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * Surface System Aggregator Module bus and device integration.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <linux/device.h>
- +#include <linux/slab.h>
- +
- +#include <linux/surface_aggregator/controller.h>
- +#include <linux/surface_aggregator/device.h>
- +
- +#include "bus.h"
- +#include "controller.h"
- +
- +static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
- + char *buf)
- +{
- + struct ssam_device *sdev = to_ssam_device(dev);
- +
- + return sysfs_emit(buf, "ssam:d%02Xc%02Xt%02Xi%02Xf%02X\n",
- + sdev->uid.domain, sdev->uid.category, sdev->uid.target,
- + sdev->uid.instance, sdev->uid.function);
- +}
- +static DEVICE_ATTR_RO(modalias);
- +
- +static struct attribute *ssam_device_attrs[] = {
- + &dev_attr_modalias.attr,
- + NULL,
- +};
- +ATTRIBUTE_GROUPS(ssam_device);
- +
- +static int ssam_device_uevent(struct device *dev, struct kobj_uevent_env *env)
- +{
- + struct ssam_device *sdev = to_ssam_device(dev);
- +
- + return add_uevent_var(env, "MODALIAS=ssam:d%02Xc%02Xt%02Xi%02Xf%02X",
- + sdev->uid.domain, sdev->uid.category,
- + sdev->uid.target, sdev->uid.instance,
- + sdev->uid.function);
- +}
- +
- +static void ssam_device_release(struct device *dev)
- +{
- + struct ssam_device *sdev = to_ssam_device(dev);
- +
- + ssam_controller_put(sdev->ctrl);
- + kfree(sdev);
- +}
- +
- +const struct device_type ssam_device_type = {
- + .name = "surface_aggregator_device",
- + .groups = ssam_device_groups,
- + .uevent = ssam_device_uevent,
- + .release = ssam_device_release,
- +};
- +EXPORT_SYMBOL_GPL(ssam_device_type);
- +
- +/**
- + * ssam_device_alloc() - Allocate and initialize a SSAM client device.
- + * @ctrl: The controller under which the device should be added.
- + * @uid: The UID of the device to be added.
- + *
- + * Allocates and initializes a new client device. The parent of the device
- + * will be set to the controller device and the name will be set based on the
- + * UID. Note that the device still has to be added via ssam_device_add().
- + * Refer to that function for more details.
- + *
- + * Return: Returns the newly allocated and initialized SSAM client device, or
- + * %NULL if it could not be allocated.
- + */
- +struct ssam_device *ssam_device_alloc(struct ssam_controller *ctrl,
- + struct ssam_device_uid uid)
- +{
- + struct ssam_device *sdev;
- +
- + sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
- + if (!sdev)
- + return NULL;
- +
- + device_initialize(&sdev->dev);
- + sdev->dev.bus = &ssam_bus_type;
- + sdev->dev.type = &ssam_device_type;
- + sdev->dev.parent = ssam_controller_device(ctrl);
- + sdev->ctrl = ssam_controller_get(ctrl);
- + sdev->uid = uid;
- +
- + dev_set_name(&sdev->dev, "%02x:%02x:%02x:%02x:%02x",
- + sdev->uid.domain, sdev->uid.category, sdev->uid.target,
- + sdev->uid.instance, sdev->uid.function);
- +
- + return sdev;
- +}
- +EXPORT_SYMBOL_GPL(ssam_device_alloc);
- +
- +/**
- + * ssam_device_add() - Add a SSAM client device.
- + * @sdev: The SSAM client device to be added.
- + *
- + * Added client devices must be guaranteed to always have a valid and active
- + * controller. Thus, this function will fail with %-ENODEV if the controller
- + * of the device has not been initialized yet, has been suspended, or has been
- + * shut down.
- + *
- + * The caller of this function should ensure that the corresponding call to
- + * ssam_device_remove() is issued before the controller is shut down. If the
- + * added device is a direct child of the controller device (default), it will
- + * be automatically removed when the controller is shut down.
- + *
- + * By default, the controller device will become the parent of the newly
- + * created client device. The parent may be changed before ssam_device_add is
- + * called, but care must be taken that a) the correct suspend/resume ordering
- + * is guaranteed and b) the client device does not outlive the controller,
- + * i.e. that the device is removed before the controller is being shut down.
- + * In case these guarantees have to be manually enforced, please refer to the
- + * ssam_client_link() and ssam_client_bind() functions, which are intended to
- + * set up device-links for this purpose.
- + *
- + * Return: Returns zero on success, a negative error code on failure.
- + */
- +int ssam_device_add(struct ssam_device *sdev)
- +{
- + int status;
- +
- + /*
- + * Ensure that we can only add new devices to a controller if it has
- + * been started and is not going away soon. This works in combination
- + * with ssam_controller_remove_clients to ensure driver presence for the
- + * controller device, i.e. it ensures that the controller (sdev->ctrl)
- + * is always valid and can be used for requests as long as the client
- + * device we add here is registered as child under it. This essentially
- + * guarantees that the client driver can always expect the preconditions
- + * for functions like ssam_request_sync (controller has to be started
- + * and is not suspended) to hold and thus does not have to check for
- + * them.
- + *
- + * Note that for this to work, the controller has to be a parent device.
- + * If it is not a direct parent, care has to be taken that the device is
- + * removed via ssam_device_remove(), as device_unregister does not
- + * remove child devices recursively.
- + */
- + ssam_controller_statelock(sdev->ctrl);
- +
- + if (sdev->ctrl->state != SSAM_CONTROLLER_STARTED) {
- + ssam_controller_stateunlock(sdev->ctrl);
- + return -ENODEV;
- + }
- +
- + status = device_add(&sdev->dev);
- +
- + ssam_controller_stateunlock(sdev->ctrl);
- + return status;
- +}
- +EXPORT_SYMBOL_GPL(ssam_device_add);
- +
- +/**
- + * ssam_device_remove() - Remove a SSAM client device.
- + * @sdev: The device to remove.
- + *
- + * Removes and unregisters the provided SSAM client device.
- + */
- +void ssam_device_remove(struct ssam_device *sdev)
- +{
- + device_unregister(&sdev->dev);
- +}
- +EXPORT_SYMBOL_GPL(ssam_device_remove);
- +
- +/**
- + * ssam_device_id_compatible() - Check if a device ID matches a UID.
- + * @id: The device ID as potential match.
- + * @uid: The device UID matching against.
- + *
- + * Check if the given ID is a match for the given UID, i.e. if a device with
- + * the provided UID is compatible to the given ID following the match rules
- + * described in its &ssam_device_id.match_flags member.
- + *
- + * Return: Returns %true if the given UID is compatible to the match rule
- + * described by the given ID, %false otherwise.
- + */
- +static bool ssam_device_id_compatible(const struct ssam_device_id *id,
- + struct ssam_device_uid uid)
- +{
- + if (id->domain != uid.domain || id->category != uid.category)
- + return false;
- +
- + if ((id->match_flags & SSAM_MATCH_TARGET) && id->target != uid.target)
- + return false;
- +
- + if ((id->match_flags & SSAM_MATCH_INSTANCE) && id->instance != uid.instance)
- + return false;
- +
- + if ((id->match_flags & SSAM_MATCH_FUNCTION) && id->function != uid.function)
- + return false;
- +
- + return true;
- +}
- +
- +/**
- + * ssam_device_id_is_null() - Check if a device ID is null.
- + * @id: The device ID to check.
- + *
- + * Check if a given device ID is null, i.e. all zeros. Used to check for the
- + * end of ``MODULE_DEVICE_TABLE(ssam, ...)`` or similar lists.
- + *
- + * Return: Returns %true if the given ID represents a null ID, %false
- + * otherwise.
- + */
- +static bool ssam_device_id_is_null(const struct ssam_device_id *id)
- +{
- + return id->match_flags == 0 &&
- + id->domain == 0 &&
- + id->category == 0 &&
- + id->target == 0 &&
- + id->instance == 0 &&
- + id->function == 0 &&
- + id->driver_data == 0;
- +}
- +
- +/**
- + * ssam_device_id_match() - Find the matching ID table entry for the given UID.
- + * @table: The table to search in.
- + * @uid: The UID to matched against the individual table entries.
- + *
- + * Find the first match for the provided device UID in the provided ID table
- + * and return it. Returns %NULL if no match could be found.
- + */
- +const struct ssam_device_id *ssam_device_id_match(const struct ssam_device_id *table,
- + const struct ssam_device_uid uid)
- +{
- + const struct ssam_device_id *id;
- +
- + for (id = table; !ssam_device_id_is_null(id); ++id)
- + if (ssam_device_id_compatible(id, uid))
- + return id;
- +
- + return NULL;
- +}
- +EXPORT_SYMBOL_GPL(ssam_device_id_match);
- +
- +/**
- + * ssam_device_get_match() - Find and return the ID matching the device in the
- + * ID table of the bound driver.
- + * @dev: The device for which to get the matching ID table entry.
- + *
- + * Find the fist match for the UID of the device in the ID table of the
- + * currently bound driver and return it. Returns %NULL if the device does not
- + * have a driver bound to it, the driver does not have match_table (i.e. it is
- + * %NULL), or there is no match in the driver's match_table.
- + *
- + * This function essentially calls ssam_device_id_match() with the ID table of
- + * the bound device driver and the UID of the device.
- + *
- + * Return: Returns the first match for the UID of the device in the device
- + * driver's match table, or %NULL if no such match could be found.
- + */
- +const struct ssam_device_id *ssam_device_get_match(const struct ssam_device *dev)
- +{
- + const struct ssam_device_driver *sdrv;
- +
- + sdrv = to_ssam_device_driver(dev->dev.driver);
- + if (!sdrv)
- + return NULL;
- +
- + if (!sdrv->match_table)
- + return NULL;
- +
- + return ssam_device_id_match(sdrv->match_table, dev->uid);
- +}
- +EXPORT_SYMBOL_GPL(ssam_device_get_match);
- +
- +/**
- + * ssam_device_get_match_data() - Find the ID matching the device in the
- + * ID table of the bound driver and return its ``driver_data`` member.
- + * @dev: The device for which to get the match data.
- + *
- + * Find the fist match for the UID of the device in the ID table of the
- + * corresponding driver and return its driver_data. Returns %NULL if the
- + * device does not have a driver bound to it, the driver does not have
- + * match_table (i.e. it is %NULL), there is no match in the driver's
- + * match_table, or the match does not have any driver_data.
- + *
- + * This function essentially calls ssam_device_get_match() and, if any match
- + * could be found, returns its ``struct ssam_device_id.driver_data`` member.
- + *
- + * Return: Returns the driver data associated with the first match for the UID
- + * of the device in the device driver's match table, or %NULL if no such match
- + * could be found.
- + */
- +const void *ssam_device_get_match_data(const struct ssam_device *dev)
- +{
- + const struct ssam_device_id *id;
- +
- + id = ssam_device_get_match(dev);
- + if (!id)
- + return NULL;
- +
- + return (const void *)id->driver_data;
- +}
- +EXPORT_SYMBOL_GPL(ssam_device_get_match_data);
- +
- +static int ssam_bus_match(struct device *dev, struct device_driver *drv)
- +{
- + struct ssam_device_driver *sdrv = to_ssam_device_driver(drv);
- + struct ssam_device *sdev = to_ssam_device(dev);
- +
- + if (!is_ssam_device(dev))
- + return 0;
- +
- + return !!ssam_device_id_match(sdrv->match_table, sdev->uid);
- +}
- +
- +static int ssam_bus_probe(struct device *dev)
- +{
- + return to_ssam_device_driver(dev->driver)
- + ->probe(to_ssam_device(dev));
- +}
- +
- +static int ssam_bus_remove(struct device *dev)
- +{
- + struct ssam_device_driver *sdrv = to_ssam_device_driver(dev->driver);
- +
- + if (sdrv->remove)
- + sdrv->remove(to_ssam_device(dev));
- +
- + return 0;
- +}
- +
- +struct bus_type ssam_bus_type = {
- + .name = "surface_aggregator",
- + .match = ssam_bus_match,
- + .probe = ssam_bus_probe,
- + .remove = ssam_bus_remove,
- +};
- +EXPORT_SYMBOL_GPL(ssam_bus_type);
- +
- +/**
- + * __ssam_device_driver_register() - Register a SSAM client device driver.
- + * @sdrv: The driver to register.
- + * @owner: The module owning the provided driver.
- + *
- + * Please refer to the ssam_device_driver_register() macro for the normal way
- + * to register a driver from inside its owning module.
- + */
- +int __ssam_device_driver_register(struct ssam_device_driver *sdrv,
- + struct module *owner)
- +{
- + sdrv->driver.owner = owner;
- + sdrv->driver.bus = &ssam_bus_type;
- +
- + /* force drivers to async probe so I/O is possible in probe */
- + sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
- +
- + return driver_register(&sdrv->driver);
- +}
- +EXPORT_SYMBOL_GPL(__ssam_device_driver_register);
- +
- +/**
- + * ssam_device_driver_unregister - Unregister a SSAM device driver.
- + * @sdrv: The driver to unregister.
- + */
- +void ssam_device_driver_unregister(struct ssam_device_driver *sdrv)
- +{
- + driver_unregister(&sdrv->driver);
- +}
- +EXPORT_SYMBOL_GPL(ssam_device_driver_unregister);
- +
- +static int ssam_remove_device(struct device *dev, void *_data)
- +{
- + struct ssam_device *sdev = to_ssam_device(dev);
- +
- + if (is_ssam_device(dev))
- + ssam_device_remove(sdev);
- +
- + return 0;
- +}
- +
- +/**
- + * ssam_controller_remove_clients() - Remove SSAM client devices registered as
- + * direct children under the given controller.
- + * @ctrl: The controller to remove all direct clients for.
- + *
- + * Remove all SSAM client devices registered as direct children under the
- + * given controller. Note that this only accounts for direct children of the
- + * controller device. This does not take care of any client devices where the
- + * parent device has been manually set before calling ssam_device_add. Refer
- + * to ssam_device_add()/ssam_device_remove() for more details on those cases.
- + *
- + * To avoid new devices being added in parallel to this call, the main
- + * controller lock (not statelock) must be held during this (and if
- + * necessary, any subsequent deinitialization) call.
- + */
- +void ssam_controller_remove_clients(struct ssam_controller *ctrl)
- +{
- + struct device *dev;
- +
- + dev = ssam_controller_device(ctrl);
- + device_for_each_child_reverse(dev, NULL, ssam_remove_device);
- +}
- +
- +/**
- + * ssam_bus_register() - Register and set-up the SSAM client device bus.
- + */
- +int ssam_bus_register(void)
- +{
- + return bus_register(&ssam_bus_type);
- +}
- +
- +/**
- + * ssam_bus_unregister() - Unregister the SSAM client device bus.
- + */
- +void ssam_bus_unregister(void)
- +{
- + return bus_unregister(&ssam_bus_type);
- +}
- diff --git a/drivers/platform/surface/aggregator/bus.h b/drivers/platform/surface/aggregator/bus.h
- new file mode 100644
- index 000000000000..7712baaed6a5
- --- /dev/null
- +++ b/drivers/platform/surface/aggregator/bus.h
- @@ -0,0 +1,27 @@
- +/* SPDX-License-Identifier: GPL-2.0+ */
- +/*
- + * Surface System Aggregator Module bus and device integration.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#ifndef _SURFACE_AGGREGATOR_BUS_H
- +#define _SURFACE_AGGREGATOR_BUS_H
- +
- +#include <linux/surface_aggregator/controller.h>
- +
- +#ifdef CONFIG_SURFACE_AGGREGATOR_BUS
- +
- +void ssam_controller_remove_clients(struct ssam_controller *ctrl);
- +
- +int ssam_bus_register(void);
- +void ssam_bus_unregister(void);
- +
- +#else /* CONFIG_SURFACE_AGGREGATOR_BUS */
- +
- +static inline void ssam_controller_remove_clients(struct ssam_controller *ctrl) {}
- +static inline int ssam_bus_register(void) { return 0; }
- +static inline void ssam_bus_unregister(void) {}
- +
- +#endif /* CONFIG_SURFACE_AGGREGATOR_BUS */
- +#endif /* _SURFACE_AGGREGATOR_BUS_H */
- diff --git a/drivers/platform/surface/aggregator/core.c b/drivers/platform/surface/aggregator/core.c
- index b6a9dea53592..8dc2c267bcd6 100644
- --- a/drivers/platform/surface/aggregator/core.c
- +++ b/drivers/platform/surface/aggregator/core.c
- @@ -22,6 +22,8 @@
- #include <linux/sysfs.h>
-
- #include <linux/surface_aggregator/controller.h>
- +
- +#include "bus.h"
- #include "controller.h"
-
- #define CREATE_TRACE_POINTS
- @@ -739,6 +741,9 @@ static void ssam_serial_hub_remove(struct serdev_device *serdev)
- sysfs_remove_group(&serdev->dev.kobj, &ssam_sam_group);
- ssam_controller_lock(ctrl);
-
- + /* Remove all client devices. */
- + ssam_controller_remove_clients(ctrl);
- +
- /* Act as if suspending to silence events. */
- status = ssam_ctrl_notif_display_off(ctrl);
- if (status) {
- @@ -791,6 +796,10 @@ static int __init ssam_core_init(void)
- {
- int status;
-
- + status = ssam_bus_register();
- + if (status)
- + goto err_bus;
- +
- status = ssh_ctrl_packet_cache_init();
- if (status)
- goto err_cpkg;
- @@ -810,6 +819,8 @@ static int __init ssam_core_init(void)
- err_evitem:
- ssh_ctrl_packet_cache_destroy();
- err_cpkg:
- + ssam_bus_unregister();
- +err_bus:
- return status;
- }
- module_init(ssam_core_init);
- @@ -819,6 +830,7 @@ static void __exit ssam_core_exit(void)
- serdev_device_driver_unregister(&ssam_serial_hub);
- ssam_event_item_cache_destroy();
- ssh_ctrl_packet_cache_destroy();
- + ssam_bus_unregister();
- }
- module_exit(ssam_core_exit);
-
- diff --git a/include/linux/mod_devicetable.h b/include/linux/mod_devicetable.h
- index c425290b21e2..935060955152 100644
- --- a/include/linux/mod_devicetable.h
- +++ b/include/linux/mod_devicetable.h
- @@ -846,4 +846,22 @@ struct auxiliary_device_id {
- kernel_ulong_t driver_data;
- };
-
- +/* Surface System Aggregator Module */
- +
- +#define SSAM_MATCH_TARGET 0x1
- +#define SSAM_MATCH_INSTANCE 0x2
- +#define SSAM_MATCH_FUNCTION 0x4
- +
- +struct ssam_device_id {
- + __u8 match_flags;
- +
- + __u8 domain;
- + __u8 category;
- + __u8 target;
- + __u8 instance;
- + __u8 function;
- +
- + kernel_ulong_t driver_data;
- +};
- +
- #endif /* LINUX_MOD_DEVICETABLE_H */
- diff --git a/include/linux/surface_aggregator/device.h b/include/linux/surface_aggregator/device.h
- new file mode 100644
- index 000000000000..02f3e06c0a60
- --- /dev/null
- +++ b/include/linux/surface_aggregator/device.h
- @@ -0,0 +1,423 @@
- +/* SPDX-License-Identifier: GPL-2.0+ */
- +/*
- + * Surface System Aggregator Module (SSAM) bus and client-device subsystem.
- + *
- + * Main interface for the surface-aggregator bus, surface-aggregator client
- + * devices, and respective drivers building on top of the SSAM controller.
- + * Provides support for non-platform/non-ACPI SSAM clients via dedicated
- + * subsystem.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#ifndef _LINUX_SURFACE_AGGREGATOR_DEVICE_H
- +#define _LINUX_SURFACE_AGGREGATOR_DEVICE_H
- +
- +#include <linux/device.h>
- +#include <linux/mod_devicetable.h>
- +#include <linux/types.h>
- +
- +#include <linux/surface_aggregator/controller.h>
- +
- +
- +/* -- Surface System Aggregator Module bus. --------------------------------- */
- +
- +/**
- + * enum ssam_device_domain - SAM device domain.
- + * @SSAM_DOMAIN_VIRTUAL: Virtual device.
- + * @SSAM_DOMAIN_SERIALHUB: Physical device connected via Surface Serial Hub.
- + */
- +enum ssam_device_domain {
- + SSAM_DOMAIN_VIRTUAL = 0x00,
- + SSAM_DOMAIN_SERIALHUB = 0x01,
- +};
- +
- +/**
- + * enum ssam_virtual_tc - Target categories for the virtual SAM domain.
- + * @SSAM_VIRTUAL_TC_HUB: Device hub category.
- + */
- +enum ssam_virtual_tc {
- + SSAM_VIRTUAL_TC_HUB = 0x00,
- +};
- +
- +/**
- + * struct ssam_device_uid - Unique identifier for SSAM device.
- + * @domain: Domain of the device.
- + * @category: Target category of the device.
- + * @target: Target ID of the device.
- + * @instance: Instance ID of the device.
- + * @function: Sub-function of the device. This field can be used to split a
- + * single SAM device into multiple virtual subdevices to separate
- + * different functionality of that device and allow one driver per
- + * such functionality.
- + */
- +struct ssam_device_uid {
- + u8 domain;
- + u8 category;
- + u8 target;
- + u8 instance;
- + u8 function;
- +};
- +
- +/*
- + * Special values for device matching.
- + *
- + * These values are intended to be used with SSAM_DEVICE(), SSAM_VDEV(), and
- + * SSAM_SDEV() exclusively. Specifically, they are used to initialize the
- + * match_flags member of the device ID structure. Do not use them directly
- + * with struct ssam_device_id or struct ssam_device_uid.
- + */
- +#define SSAM_ANY_TID 0xffff
- +#define SSAM_ANY_IID 0xffff
- +#define SSAM_ANY_FUN 0xffff
- +
- +/**
- + * SSAM_DEVICE() - Initialize a &struct ssam_device_id with the given
- + * parameters.
- + * @d: Domain of the device.
- + * @cat: Target category of the device.
- + * @tid: Target ID of the device.
- + * @iid: Instance ID of the device.
- + * @fun: Sub-function of the device.
- + *
- + * Initializes a &struct ssam_device_id with the given parameters. See &struct
- + * ssam_device_uid for details regarding the parameters. The special values
- + * %SSAM_ANY_TID, %SSAM_ANY_IID, and %SSAM_ANY_FUN can be used to specify that
- + * matching should ignore target ID, instance ID, and/or sub-function,
- + * respectively. This macro initializes the ``match_flags`` field based on the
- + * given parameters.
- + *
- + * Note: The parameters @d and @cat must be valid &u8 values, the parameters
- + * @tid, @iid, and @fun must be either valid &u8 values or %SSAM_ANY_TID,
- + * %SSAM_ANY_IID, or %SSAM_ANY_FUN, respectively. Other non-&u8 values are not
- + * allowed.
- + */
- +#define SSAM_DEVICE(d, cat, tid, iid, fun) \
- + .match_flags = (((tid) != SSAM_ANY_TID) ? SSAM_MATCH_TARGET : 0) \
- + | (((iid) != SSAM_ANY_IID) ? SSAM_MATCH_INSTANCE : 0) \
- + | (((fun) != SSAM_ANY_FUN) ? SSAM_MATCH_FUNCTION : 0), \
- + .domain = d, \
- + .category = cat, \
- + .target = ((tid) != SSAM_ANY_TID) ? (tid) : 0, \
- + .instance = ((iid) != SSAM_ANY_IID) ? (iid) : 0, \
- + .function = ((fun) != SSAM_ANY_FUN) ? (fun) : 0 \
- +
- +/**
- + * SSAM_VDEV() - Initialize a &struct ssam_device_id as virtual device with
- + * the given parameters.
- + * @cat: Target category of the device.
- + * @tid: Target ID of the device.
- + * @iid: Instance ID of the device.
- + * @fun: Sub-function of the device.
- + *
- + * Initializes a &struct ssam_device_id with the given parameters in the
- + * virtual domain. See &struct ssam_device_uid for details regarding the
- + * parameters. The special values %SSAM_ANY_TID, %SSAM_ANY_IID, and
- + * %SSAM_ANY_FUN can be used to specify that matching should ignore target ID,
- + * instance ID, and/or sub-function, respectively. This macro initializes the
- + * ``match_flags`` field based on the given parameters.
- + *
- + * Note: The parameter @cat must be a valid &u8 value, the parameters @tid,
- + * @iid, and @fun must be either valid &u8 values or %SSAM_ANY_TID,
- + * %SSAM_ANY_IID, or %SSAM_ANY_FUN, respectively. Other non-&u8 values are not
- + * allowed.
- + */
- +#define SSAM_VDEV(cat, tid, iid, fun) \
- + SSAM_DEVICE(SSAM_DOMAIN_VIRTUAL, SSAM_VIRTUAL_TC_##cat, tid, iid, fun)
- +
- +/**
- + * SSAM_SDEV() - Initialize a &struct ssam_device_id as physical SSH device
- + * with the given parameters.
- + * @cat: Target category of the device.
- + * @tid: Target ID of the device.
- + * @iid: Instance ID of the device.
- + * @fun: Sub-function of the device.
- + *
- + * Initializes a &struct ssam_device_id with the given parameters in the SSH
- + * domain. See &struct ssam_device_uid for details regarding the parameters.
- + * The special values %SSAM_ANY_TID, %SSAM_ANY_IID, and %SSAM_ANY_FUN can be
- + * used to specify that matching should ignore target ID, instance ID, and/or
- + * sub-function, respectively. This macro initializes the ``match_flags``
- + * field based on the given parameters.
- + *
- + * Note: The parameter @cat must be a valid &u8 value, the parameters @tid,
- + * @iid, and @fun must be either valid &u8 values or %SSAM_ANY_TID,
- + * %SSAM_ANY_IID, or %SSAM_ANY_FUN, respectively. Other non-&u8 values are not
- + * allowed.
- + */
- +#define SSAM_SDEV(cat, tid, iid, fun) \
- + SSAM_DEVICE(SSAM_DOMAIN_SERIALHUB, SSAM_SSH_TC_##cat, tid, iid, fun)
- +
- +/**
- + * struct ssam_device - SSAM client device.
- + * @dev: Driver model representation of the device.
- + * @ctrl: SSAM controller managing this device.
- + * @uid: UID identifying the device.
- + */
- +struct ssam_device {
- + struct device dev;
- + struct ssam_controller *ctrl;
- +
- + struct ssam_device_uid uid;
- +};
- +
- +/**
- + * struct ssam_device_driver - SSAM client device driver.
- + * @driver: Base driver model structure.
- + * @match_table: Match table specifying which devices the driver should bind to.
- + * @probe: Called when the driver is being bound to a device.
- + * @remove: Called when the driver is being unbound from the device.
- + */
- +struct ssam_device_driver {
- + struct device_driver driver;
- +
- + const struct ssam_device_id *match_table;
- +
- + int (*probe)(struct ssam_device *sdev);
- + void (*remove)(struct ssam_device *sdev);
- +};
- +
- +extern struct bus_type ssam_bus_type;
- +extern const struct device_type ssam_device_type;
- +
- +/**
- + * is_ssam_device() - Check if the given device is a SSAM client device.
- + * @d: The device to test the type of.
- + *
- + * Return: Returns %true if the specified device is of type &struct
- + * ssam_device, i.e. the device type points to %ssam_device_type, and %false
- + * otherwise.
- + */
- +static inline bool is_ssam_device(struct device *d)
- +{
- + return d->type == &ssam_device_type;
- +}
- +
- +/**
- + * to_ssam_device() - Casts the given device to a SSAM client device.
- + * @d: The device to cast.
- + *
- + * Casts the given &struct device to a &struct ssam_device. The caller has to
- + * ensure that the given device is actually enclosed in a &struct ssam_device,
- + * e.g. by calling is_ssam_device().
- + *
- + * Return: Returns a pointer to the &struct ssam_device wrapping the given
- + * device @d.
- + */
- +static inline struct ssam_device *to_ssam_device(struct device *d)
- +{
- + return container_of(d, struct ssam_device, dev);
- +}
- +
- +/**
- + * to_ssam_device_driver() - Casts the given device driver to a SSAM client
- + * device driver.
- + * @d: The driver to cast.
- + *
- + * Casts the given &struct device_driver to a &struct ssam_device_driver. The
- + * caller has to ensure that the given driver is actually enclosed in a
- + * &struct ssam_device_driver.
- + *
- + * Return: Returns the pointer to the &struct ssam_device_driver wrapping the
- + * given device driver @d.
- + */
- +static inline
- +struct ssam_device_driver *to_ssam_device_driver(struct device_driver *d)
- +{
- + return container_of(d, struct ssam_device_driver, driver);
- +}
- +
- +const struct ssam_device_id *ssam_device_id_match(const struct ssam_device_id *table,
- + const struct ssam_device_uid uid);
- +
- +const struct ssam_device_id *ssam_device_get_match(const struct ssam_device *dev);
- +
- +const void *ssam_device_get_match_data(const struct ssam_device *dev);
- +
- +struct ssam_device *ssam_device_alloc(struct ssam_controller *ctrl,
- + struct ssam_device_uid uid);
- +
- +int ssam_device_add(struct ssam_device *sdev);
- +void ssam_device_remove(struct ssam_device *sdev);
- +
- +/**
- + * ssam_device_get() - Increment reference count of SSAM client device.
- + * @sdev: The device to increment the reference count of.
- + *
- + * Increments the reference count of the given SSAM client device by
- + * incrementing the reference count of the enclosed &struct device via
- + * get_device().
- + *
- + * See ssam_device_put() for the counter-part of this function.
- + *
- + * Return: Returns the device provided as input.
- + */
- +static inline struct ssam_device *ssam_device_get(struct ssam_device *sdev)
- +{
- + return sdev ? to_ssam_device(get_device(&sdev->dev)) : NULL;
- +}
- +
- +/**
- + * ssam_device_put() - Decrement reference count of SSAM client device.
- + * @sdev: The device to decrement the reference count of.
- + *
- + * Decrements the reference count of the given SSAM client device by
- + * decrementing the reference count of the enclosed &struct device via
- + * put_device().
- + *
- + * See ssam_device_get() for the counter-part of this function.
- + */
- +static inline void ssam_device_put(struct ssam_device *sdev)
- +{
- + if (sdev)
- + put_device(&sdev->dev);
- +}
- +
- +/**
- + * ssam_device_get_drvdata() - Get driver-data of SSAM client device.
- + * @sdev: The device to get the driver-data from.
- + *
- + * Return: Returns the driver-data of the given device, previously set via
- + * ssam_device_set_drvdata().
- + */
- +static inline void *ssam_device_get_drvdata(struct ssam_device *sdev)
- +{
- + return dev_get_drvdata(&sdev->dev);
- +}
- +
- +/**
- + * ssam_device_set_drvdata() - Set driver-data of SSAM client device.
- + * @sdev: The device to set the driver-data of.
- + * @data: The data to set the device's driver-data pointer to.
- + */
- +static inline void ssam_device_set_drvdata(struct ssam_device *sdev, void *data)
- +{
- + dev_set_drvdata(&sdev->dev, data);
- +}
- +
- +int __ssam_device_driver_register(struct ssam_device_driver *d, struct module *o);
- +void ssam_device_driver_unregister(struct ssam_device_driver *d);
- +
- +/**
- + * ssam_device_driver_register() - Register a SSAM client device driver.
- + * @drv: The driver to register.
- + */
- +#define ssam_device_driver_register(drv) \
- + __ssam_device_driver_register(drv, THIS_MODULE)
- +
- +/**
- + * module_ssam_device_driver() - Helper macro for SSAM device driver
- + * registration.
- + * @drv: The driver managed by this module.
- + *
- + * Helper macro to register a SSAM device driver via module_init() and
- + * module_exit(). This macro may only be used once per module and replaces the
- + * aforementioned definitions.
- + */
- +#define module_ssam_device_driver(drv) \
- + module_driver(drv, ssam_device_driver_register, \
- + ssam_device_driver_unregister)
- +
- +
- +/* -- Helpers for client-device requests. ----------------------------------- */
- +
- +/**
- + * SSAM_DEFINE_SYNC_REQUEST_CL_N() - Define synchronous client-device SAM
- + * request function with neither argument nor return value.
- + * @name: Name of the generated function.
- + * @spec: Specification (&struct ssam_request_spec_md) defining the request.
- + *
- + * Defines a function executing the synchronous SAM request specified by
- + * @spec, with the request having neither argument nor return value. Device
- + * specifying parameters are not hard-coded, but instead are provided via the
- + * client device, specifically its UID, supplied when calling this function.
- + * The generated function takes care of setting up the request struct, buffer
- + * allocation, as well as execution of the request itself, returning once the
- + * request has been fully completed. The required transport buffer will be
- + * allocated on the stack.
- + *
- + * The generated function is defined as ``int name(struct ssam_device *sdev)``,
- + * returning the status of the request, which is zero on success and negative
- + * on failure. The ``sdev`` parameter specifies both the target device of the
- + * request and by association the controller via which the request is sent.
- + *
- + * Refer to ssam_request_sync_onstack() for more details on the behavior of
- + * the generated function.
- + */
- +#define SSAM_DEFINE_SYNC_REQUEST_CL_N(name, spec...) \
- + SSAM_DEFINE_SYNC_REQUEST_MD_N(__raw_##name, spec) \
- + int name(struct ssam_device *sdev) \
- + { \
- + return __raw_##name(sdev->ctrl, sdev->uid.target, \
- + sdev->uid.instance); \
- + }
- +
- +/**
- + * SSAM_DEFINE_SYNC_REQUEST_CL_W() - Define synchronous client-device SAM
- + * request function with argument.
- + * @name: Name of the generated function.
- + * @atype: Type of the request's argument.
- + * @spec: Specification (&struct ssam_request_spec_md) defining the request.
- + *
- + * Defines a function executing the synchronous SAM request specified by
- + * @spec, with the request taking an argument of type @atype and having no
- + * return value. Device specifying parameters are not hard-coded, but instead
- + * are provided via the client device, specifically its UID, supplied when
- + * calling this function. The generated function takes care of setting up the
- + * request struct, buffer allocation, as well as execution of the request
- + * itself, returning once the request has been fully completed. The required
- + * transport buffer will be allocated on the stack.
- + *
- + * The generated function is defined as ``int name(struct ssam_device *sdev,
- + * const atype *arg)``, returning the status of the request, which is zero on
- + * success and negative on failure. The ``sdev`` parameter specifies both the
- + * target device of the request and by association the controller via which
- + * the request is sent. The request's argument is specified via the ``arg``
- + * pointer.
- + *
- + * Refer to ssam_request_sync_onstack() for more details on the behavior of
- + * the generated function.
- + */
- +#define SSAM_DEFINE_SYNC_REQUEST_CL_W(name, atype, spec...) \
- + SSAM_DEFINE_SYNC_REQUEST_MD_W(__raw_##name, atype, spec) \
- + int name(struct ssam_device *sdev, const atype *arg) \
- + { \
- + return __raw_##name(sdev->ctrl, sdev->uid.target, \
- + sdev->uid.instance, arg); \
- + }
- +
- +/**
- + * SSAM_DEFINE_SYNC_REQUEST_CL_R() - Define synchronous client-device SAM
- + * request function with return value.
- + * @name: Name of the generated function.
- + * @rtype: Type of the request's return value.
- + * @spec: Specification (&struct ssam_request_spec_md) defining the request.
- + *
- + * Defines a function executing the synchronous SAM request specified by
- + * @spec, with the request taking no argument but having a return value of
- + * type @rtype. Device specifying parameters are not hard-coded, but instead
- + * are provided via the client device, specifically its UID, supplied when
- + * calling this function. The generated function takes care of setting up the
- + * request struct, buffer allocation, as well as execution of the request
- + * itself, returning once the request has been fully completed. The required
- + * transport buffer will be allocated on the stack.
- + *
- + * The generated function is defined as ``int name(struct ssam_device *sdev,
- + * rtype *ret)``, returning the status of the request, which is zero on
- + * success and negative on failure. The ``sdev`` parameter specifies both the
- + * target device of the request and by association the controller via which
- + * the request is sent. The request's return value is written to the memory
- + * pointed to by the ``ret`` parameter.
- + *
- + * Refer to ssam_request_sync_onstack() for more details on the behavior of
- + * the generated function.
- + */
- +#define SSAM_DEFINE_SYNC_REQUEST_CL_R(name, rtype, spec...) \
- + SSAM_DEFINE_SYNC_REQUEST_MD_R(__raw_##name, rtype, spec) \
- + int name(struct ssam_device *sdev, rtype *ret) \
- + { \
- + return __raw_##name(sdev->ctrl, sdev->uid.target, \
- + sdev->uid.instance, ret); \
- + }
- +
- +#endif /* _LINUX_SURFACE_AGGREGATOR_DEVICE_H */
- diff --git a/scripts/mod/devicetable-offsets.c b/scripts/mod/devicetable-offsets.c
- index e377f52dbfa3..f078eeb0a961 100644
- --- a/scripts/mod/devicetable-offsets.c
- +++ b/scripts/mod/devicetable-offsets.c
- @@ -246,5 +246,13 @@ int main(void)
- DEVID(auxiliary_device_id);
- DEVID_FIELD(auxiliary_device_id, name);
-
- + DEVID(ssam_device_id);
- + DEVID_FIELD(ssam_device_id, match_flags);
- + DEVID_FIELD(ssam_device_id, domain);
- + DEVID_FIELD(ssam_device_id, category);
- + DEVID_FIELD(ssam_device_id, target);
- + DEVID_FIELD(ssam_device_id, instance);
- + DEVID_FIELD(ssam_device_id, function);
- +
- return 0;
- }
- diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
- index fb4827027536..d21d2871387b 100644
- --- a/scripts/mod/file2alias.c
- +++ b/scripts/mod/file2alias.c
- @@ -1375,6 +1375,28 @@ static int do_auxiliary_entry(const char *filename, void *symval, char *alias)
- return 1;
- }
-
- +/*
- + * Looks like: ssam:dNcNtNiNfN
- + *
- + * N is exactly 2 digits, where each is an upper-case hex digit.
- + */
- +static int do_ssam_entry(const char *filename, void *symval, char *alias)
- +{
- + DEF_FIELD(symval, ssam_device_id, match_flags);
- + DEF_FIELD(symval, ssam_device_id, domain);
- + DEF_FIELD(symval, ssam_device_id, category);
- + DEF_FIELD(symval, ssam_device_id, target);
- + DEF_FIELD(symval, ssam_device_id, instance);
- + DEF_FIELD(symval, ssam_device_id, function);
- +
- + sprintf(alias, "ssam:d%02Xc%02X", domain, category);
- + ADD(alias, "t", match_flags & SSAM_MATCH_TARGET, target);
- + ADD(alias, "i", match_flags & SSAM_MATCH_INSTANCE, instance);
- + ADD(alias, "f", match_flags & SSAM_MATCH_FUNCTION, function);
- +
- + return 1;
- +}
- +
- /* Does namelen bytes of name exactly match the symbol? */
- static bool sym_is(const char *name, unsigned namelen, const char *symbol)
- {
- @@ -1450,6 +1472,7 @@ static const struct devtable devtable[] = {
- {"wmi", SIZE_wmi_device_id, do_wmi_entry},
- {"mhi", SIZE_mhi_device_id, do_mhi_entry},
- {"auxiliary", SIZE_auxiliary_device_id, do_auxiliary_entry},
- + {"ssam", SIZE_ssam_device_id, do_ssam_entry},
- };
-
- /* Create MODULE_ALIAS() statements.
- --
- 2.31.0
- From e17f8d603291fa603359d2ad132ea776dcfc1bfb Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Mon, 21 Dec 2020 19:39:57 +0100
- Subject: [PATCH] docs: driver-api: Add Surface Aggregator subsystem
- documentation
- Add documentation for the Surface Aggregator subsystem and its client
- drivers, giving an overview of the subsystem, its use-cases, its
- internal structure and internal API, as well as its external API for
- writing client drivers.
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Reviewed-by: Hans de Goede <hdegoede@redhat.com>
- Link: https://lore.kernel.org/r/20201221183959.1186143-8-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- Documentation/driver-api/index.rst | 1 +
- .../surface_aggregator/client-api.rst | 38 ++
- .../driver-api/surface_aggregator/client.rst | 393 ++++++++++++
- .../surface_aggregator/clients/index.rst | 10 +
- .../driver-api/surface_aggregator/index.rst | 21 +
- .../surface_aggregator/internal-api.rst | 67 ++
- .../surface_aggregator/internal.rst | 577 ++++++++++++++++++
- .../surface_aggregator/overview.rst | 77 +++
- .../driver-api/surface_aggregator/ssh.rst | 344 +++++++++++
- MAINTAINERS | 1 +
- 10 files changed, 1529 insertions(+)
- create mode 100644 Documentation/driver-api/surface_aggregator/client-api.rst
- create mode 100644 Documentation/driver-api/surface_aggregator/client.rst
- create mode 100644 Documentation/driver-api/surface_aggregator/clients/index.rst
- create mode 100644 Documentation/driver-api/surface_aggregator/index.rst
- create mode 100644 Documentation/driver-api/surface_aggregator/internal-api.rst
- create mode 100644 Documentation/driver-api/surface_aggregator/internal.rst
- create mode 100644 Documentation/driver-api/surface_aggregator/overview.rst
- create mode 100644 Documentation/driver-api/surface_aggregator/ssh.rst
- diff --git a/Documentation/driver-api/index.rst b/Documentation/driver-api/index.rst
- index 2456d0a97ed8..9d9af54d68c5 100644
- --- a/Documentation/driver-api/index.rst
- +++ b/Documentation/driver-api/index.rst
- @@ -99,6 +99,7 @@ available subsections can be seen below.
- rfkill
- serial/index
- sm501
- + surface_aggregator/index
- switchtec
- sync_file
- vfio-mediated-device
- diff --git a/Documentation/driver-api/surface_aggregator/client-api.rst b/Documentation/driver-api/surface_aggregator/client-api.rst
- new file mode 100644
- index 000000000000..8e0b000d0e64
- --- /dev/null
- +++ b/Documentation/driver-api/surface_aggregator/client-api.rst
- @@ -0,0 +1,38 @@
- +.. SPDX-License-Identifier: GPL-2.0+
- +
- +===============================
- +Client Driver API Documentation
- +===============================
- +
- +.. contents::
- + :depth: 2
- +
- +
- +Serial Hub Communication
- +========================
- +
- +.. kernel-doc:: include/linux/surface_aggregator/serial_hub.h
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/ssh_packet_layer.c
- + :export:
- +
- +
- +Controller and Core Interface
- +=============================
- +
- +.. kernel-doc:: include/linux/surface_aggregator/controller.h
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/controller.c
- + :export:
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/core.c
- + :export:
- +
- +
- +Client Bus and Client Device API
- +================================
- +
- +.. kernel-doc:: include/linux/surface_aggregator/device.h
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/bus.c
- + :export:
- diff --git a/Documentation/driver-api/surface_aggregator/client.rst b/Documentation/driver-api/surface_aggregator/client.rst
- new file mode 100644
- index 000000000000..26d13085a117
- --- /dev/null
- +++ b/Documentation/driver-api/surface_aggregator/client.rst
- @@ -0,0 +1,393 @@
- +.. SPDX-License-Identifier: GPL-2.0+
- +
- +.. |ssam_controller| replace:: :c:type:`struct ssam_controller <ssam_controller>`
- +.. |ssam_device| replace:: :c:type:`struct ssam_device <ssam_device>`
- +.. |ssam_device_driver| replace:: :c:type:`struct ssam_device_driver <ssam_device_driver>`
- +.. |ssam_client_bind| replace:: :c:func:`ssam_client_bind`
- +.. |ssam_client_link| replace:: :c:func:`ssam_client_link`
- +.. |ssam_get_controller| replace:: :c:func:`ssam_get_controller`
- +.. |ssam_controller_get| replace:: :c:func:`ssam_controller_get`
- +.. |ssam_controller_put| replace:: :c:func:`ssam_controller_put`
- +.. |ssam_device_alloc| replace:: :c:func:`ssam_device_alloc`
- +.. |ssam_device_add| replace:: :c:func:`ssam_device_add`
- +.. |ssam_device_remove| replace:: :c:func:`ssam_device_remove`
- +.. |ssam_device_driver_register| replace:: :c:func:`ssam_device_driver_register`
- +.. |ssam_device_driver_unregister| replace:: :c:func:`ssam_device_driver_unregister`
- +.. |module_ssam_device_driver| replace:: :c:func:`module_ssam_device_driver`
- +.. |SSAM_DEVICE| replace:: :c:func:`SSAM_DEVICE`
- +.. |ssam_notifier_register| replace:: :c:func:`ssam_notifier_register`
- +.. |ssam_notifier_unregister| replace:: :c:func:`ssam_notifier_unregister`
- +.. |ssam_request_sync| replace:: :c:func:`ssam_request_sync`
- +.. |ssam_event_mask| replace:: :c:type:`enum ssam_event_mask <ssam_event_mask>`
- +
- +
- +======================
- +Writing Client Drivers
- +======================
- +
- +For the API documentation, refer to:
- +
- +.. toctree::
- + :maxdepth: 2
- +
- + client-api
- +
- +
- +Overview
- +========
- +
- +Client drivers can be set up in two main ways, depending on how the
- +corresponding device is made available to the system. We specifically
- +differentiate between devices that are presented to the system via one of
- +the conventional ways, e.g. as platform devices via ACPI, and devices that
- +are non-discoverable and instead need to be explicitly provided by some
- +other mechanism, as discussed further below.
- +
- +
- +Non-SSAM Client Drivers
- +=======================
- +
- +All communication with the SAM EC is handled via the |ssam_controller|
- +representing that EC to the kernel. Drivers targeting a non-SSAM device (and
- +thus not being a |ssam_device_driver|) need to explicitly establish a
- +connection/relation to that controller. This can be done via the
- +|ssam_client_bind| function. Said function returns a reference to the SSAM
- +controller, but, more importantly, also establishes a device link between
- +client device and controller (this can also be done separate via
- +|ssam_client_link|). It is important to do this, as it, first, guarantees
- +that the returned controller is valid for use in the client driver for as
- +long as this driver is bound to its device, i.e. that the driver gets
- +unbound before the controller ever becomes invalid, and, second, as it
- +ensures correct suspend/resume ordering. This setup should be done in the
- +driver's probe function, and may be used to defer probing in case the SSAM
- +subsystem is not ready yet, for example:
- +
- +.. code-block:: c
- +
- + static int client_driver_probe(struct platform_device *pdev)
- + {
- + struct ssam_controller *ctrl;
- +
- + ctrl = ssam_client_bind(&pdev->dev);
- + if (IS_ERR(ctrl))
- + return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
- +
- + // ...
- +
- + return 0;
- + }
- +
- +The controller may be separately obtained via |ssam_get_controller| and its
- +lifetime be guaranteed via |ssam_controller_get| and |ssam_controller_put|.
- +Note that none of these functions, however, guarantee that the controller
- +will not be shut down or suspended. These functions essentially only operate
- +on the reference, i.e. only guarantee a bare minimum of accessibility
- +without any guarantees at all on practical operability.
- +
- +
- +Adding SSAM Devices
- +===================
- +
- +If a device does not already exist/is not already provided via conventional
- +means, it should be provided as |ssam_device| via the SSAM client device
- +hub. New devices can be added to this hub by entering their UID into the
- +corresponding registry. SSAM devices can also be manually allocated via
- +|ssam_device_alloc|, subsequently to which they have to be added via
- +|ssam_device_add| and eventually removed via |ssam_device_remove|. By
- +default, the parent of the device is set to the controller device provided
- +for allocation, however this may be changed before the device is added. Note
- +that, when changing the parent device, care must be taken to ensure that the
- +controller lifetime and suspend/resume ordering guarantees, in the default
- +setup provided through the parent-child relation, are preserved. If
- +necessary, by use of |ssam_client_link| as is done for non-SSAM client
- +drivers and described in more detail above.
- +
- +A client device must always be removed by the party which added the
- +respective device before the controller shuts down. Such removal can be
- +guaranteed by linking the driver providing the SSAM device to the controller
- +via |ssam_client_link|, causing it to unbind before the controller driver
- +unbinds. Client devices registered with the controller as parent are
- +automatically removed when the controller shuts down, but this should not be
- +relied upon, especially as this does not extend to client devices with a
- +different parent.
- +
- +
- +SSAM Client Drivers
- +===================
- +
- +SSAM client device drivers are, in essence, no different than other device
- +driver types. They are represented via |ssam_device_driver| and bind to a
- +|ssam_device| via its UID (:c:type:`struct ssam_device.uid <ssam_device>`)
- +member and the match table
- +(:c:type:`struct ssam_device_driver.match_table <ssam_device_driver>`),
- +which should be set when declaring the driver struct instance. Refer to the
- +|SSAM_DEVICE| macro documentation for more details on how to define members
- +of the driver's match table.
- +
- +The UID for SSAM client devices consists of a ``domain``, a ``category``,
- +a ``target``, an ``instance``, and a ``function``. The ``domain`` is used
- +differentiate between physical SAM devices
- +(:c:type:`SSAM_DOMAIN_SERIALHUB <ssam_device_domain>`), i.e. devices that can
- +be accessed via the Surface Serial Hub, and virtual ones
- +(:c:type:`SSAM_DOMAIN_VIRTUAL <ssam_device_domain>`), such as client-device
- +hubs, that have no real representation on the SAM EC and are solely used on
- +the kernel/driver-side. For physical devices, ``category`` represents the
- +target category, ``target`` the target ID, and ``instance`` the instance ID
- +used to access the physical SAM device. In addition, ``function`` references
- +a specific device functionality, but has no meaning to the SAM EC. The
- +(default) name of a client device is generated based on its UID.
- +
- +A driver instance can be registered via |ssam_device_driver_register| and
- +unregistered via |ssam_device_driver_unregister|. For convenience, the
- +|module_ssam_device_driver| macro may be used to define module init- and
- +exit-functions registering the driver.
- +
- +The controller associated with a SSAM client device can be found in its
- +:c:type:`struct ssam_device.ctrl <ssam_device>` member. This reference is
- +guaranteed to be valid for at least as long as the client driver is bound,
- +but should also be valid for as long as the client device exists. Note,
- +however, that access outside of the bound client driver must ensure that the
- +controller device is not suspended while making any requests or
- +(un-)registering event notifiers (and thus should generally be avoided). This
- +is guaranteed when the controller is accessed from inside the bound client
- +driver.
- +
- +
- +Making Synchronous Requests
- +===========================
- +
- +Synchronous requests are (currently) the main form of host-initiated
- +communication with the EC. There are a couple of ways to define and execute
- +such requests, however, most of them boil down to something similar as shown
- +in the example below. This example defines a write-read request, meaning
- +that the caller provides an argument to the SAM EC and receives a response.
- +The caller needs to know the (maximum) length of the response payload and
- +provide a buffer for it.
- +
- +Care must be taken to ensure that any command payload data passed to the SAM
- +EC is provided in little-endian format and, similarly, any response payload
- +data received from it is converted from little-endian to host endianness.
- +
- +.. code-block:: c
- +
- + int perform_request(struct ssam_controller *ctrl, u32 arg, u32 *ret)
- + {
- + struct ssam_request rqst;
- + struct ssam_response resp;
- + int status;
- +
- + /* Convert request argument to little-endian. */
- + __le32 arg_le = cpu_to_le32(arg);
- + __le32 ret_le = cpu_to_le32(0);
- +
- + /*
- + * Initialize request specification. Replace this with your values.
- + * The rqst.payload field may be NULL if rqst.length is zero,
- + * indicating that the request does not have any argument.
- + *
- + * Note: The request parameters used here are not valid, i.e.
- + * they do not correspond to an actual SAM/EC request.
- + */
- + rqst.target_category = SSAM_SSH_TC_SAM;
- + rqst.target_id = 0x01;
- + rqst.command_id = 0x02;
- + rqst.instance_id = 0x03;
- + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
- + rqst.length = sizeof(arg_le);
- + rqst.payload = (u8 *)&arg_le;
- +
- + /* Initialize request response. */
- + resp.capacity = sizeof(ret_le);
- + resp.length = 0;
- + resp.pointer = (u8 *)&ret_le;
- +
- + /*
- + * Perform actual request. The response pointer may be null in case
- + * the request does not have any response. This must be consistent
- + * with the SSAM_REQUEST_HAS_RESPONSE flag set in the specification
- + * above.
- + */
- + status = ssam_request_sync(ctrl, &rqst, &resp);
- +
- + /*
- + * Alternatively use
- + *
- + * ssam_request_sync_onstack(ctrl, &rqst, &resp, sizeof(arg_le));
- + *
- + * to perform the request, allocating the message buffer directly
- + * on the stack as opposed to allocation via kzalloc().
- + */
- +
- + /*
- + * Convert request response back to native format. Note that in the
- + * error case, this value is not touched by the SSAM core, i.e.
- + * 'ret_le' will be zero as specified in its initialization.
- + */
- + *ret = le32_to_cpu(ret_le);
- +
- + return status;
- + }
- +
- +Note that |ssam_request_sync| in its essence is a wrapper over lower-level
- +request primitives, which may also be used to perform requests. Refer to its
- +implementation and documentation for more details.
- +
- +An arguably more user-friendly way of defining such functions is by using
- +one of the generator macros, for example via:
- +
- +.. code-block:: c
- +
- + SSAM_DEFINE_SYNC_REQUEST_W(__ssam_tmp_perf_mode_set, __le32, {
- + .target_category = SSAM_SSH_TC_TMP,
- + .target_id = 0x01,
- + .command_id = 0x03,
- + .instance_id = 0x00,
- + });
- +
- +This example defines a function
- +
- +.. code-block:: c
- +
- + int __ssam_tmp_perf_mode_set(struct ssam_controller *ctrl, const __le32 *arg);
- +
- +executing the specified request, with the controller passed in when calling
- +said function. In this example, the argument is provided via the ``arg``
- +pointer. Note that the generated function allocates the message buffer on
- +the stack. Thus, if the argument provided via the request is large, these
- +kinds of macros should be avoided. Also note that, in contrast to the
- +previous non-macro example, this function does not do any endianness
- +conversion, which has to be handled by the caller. Apart from those
- +differences the function generated by the macro is similar to the one
- +provided in the non-macro example above.
- +
- +The full list of such function-generating macros is
- +
- +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_N` for requests without return value and
- + without argument.
- +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_R` for requests with return value but no
- + argument.
- +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_W` for requests without return value but
- + with argument.
- +
- +Refer to their respective documentation for more details. For each one of
- +these macros, a special variant is provided, which targets request types
- +applicable to multiple instances of the same device type:
- +
- +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_MD_N`
- +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_MD_R`
- +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_MD_W`
- +
- +The difference of those macros to the previously mentioned versions is, that
- +the device target and instance IDs are not fixed for the generated function,
- +but instead have to be provided by the caller of said function.
- +
- +Additionally, variants for direct use with client devices, i.e.
- +|ssam_device|, are also provided. These can, for example, be used as
- +follows:
- +
- +.. code-block:: c
- +
- + SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_sta, __le32, {
- + .target_category = SSAM_SSH_TC_BAT,
- + .command_id = 0x01,
- + });
- +
- +This invocation of the macro defines a function
- +
- +.. code-block:: c
- +
- + int ssam_bat_get_sta(struct ssam_device *sdev, __le32 *ret);
- +
- +executing the specified request, using the device IDs and controller given
- +in the client device. The full list of such macros for client devices is:
- +
- +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_CL_N`
- +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_CL_R`
- +- :c:func:`SSAM_DEFINE_SYNC_REQUEST_CL_W`
- +
- +
- +Handling Events
- +===============
- +
- +To receive events from the SAM EC, an event notifier must be registered for
- +the desired event via |ssam_notifier_register|. The notifier must be
- +unregistered via |ssam_notifier_unregister| once it is not required any
- +more.
- +
- +Event notifiers are registered by providing (at minimum) a callback to call
- +in case an event has been received, the registry specifying how the event
- +should be enabled, an event ID specifying for which target category and,
- +optionally and depending on the registry used, for which instance ID events
- +should be enabled, and finally, flags describing how the EC will send these
- +events. If the specific registry does not enable events by instance ID, the
- +instance ID must be set to zero. Additionally, a priority for the respective
- +notifier may be specified, which determines its order in relation to any
- +other notifier registered for the same target category.
- +
- +By default, event notifiers will receive all events for the specific target
- +category, regardless of the instance ID specified when registering the
- +notifier. The core may be instructed to only call a notifier if the target
- +ID or instance ID (or both) of the event match the ones implied by the
- +notifier IDs (in case of target ID, the target ID of the registry), by
- +providing an event mask (see |ssam_event_mask|).
- +
- +In general, the target ID of the registry is also the target ID of the
- +enabled event (with the notable exception being keyboard input events on the
- +Surface Laptop 1 and 2, which are enabled via a registry with target ID 1,
- +but provide events with target ID 2).
- +
- +A full example for registering an event notifier and handling received
- +events is provided below:
- +
- +.. code-block:: c
- +
- + u32 notifier_callback(struct ssam_event_notifier *nf,
- + const struct ssam_event *event)
- + {
- + int status = ...
- +
- + /* Handle the event here ... */
- +
- + /* Convert return value and indicate that we handled the event. */
- + return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
- + }
- +
- + int setup_notifier(struct ssam_device *sdev,
- + struct ssam_event_notifier *nf)
- + {
- + /* Set priority wrt. other handlers of same target category. */
- + nf->base.priority = 1;
- +
- + /* Set event/notifier callback. */
- + nf->base.fn = notifier_callback;
- +
- + /* Specify event registry, i.e. how events get enabled/disabled. */
- + nf->event.reg = SSAM_EVENT_REGISTRY_KIP;
- +
- + /* Specify which event to enable/disable */
- + nf->event.id.target_category = sdev->uid.category;
- + nf->event.id.instance = sdev->uid.instance;
- +
- + /*
- + * Specify for which events the notifier callback gets executed.
- + * This essentially tells the core if it can skip notifiers that
- + * don't have target or instance IDs matching those of the event.
- + */
- + nf->event.mask = SSAM_EVENT_MASK_STRICT;
- +
- + /* Specify event flags. */
- + nf->event.flags = SSAM_EVENT_SEQUENCED;
- +
- + return ssam_notifier_register(sdev->ctrl, nf);
- + }
- +
- +Multiple event notifiers can be registered for the same event. The event
- +handler core takes care of enabling and disabling events when notifiers are
- +registered and unregistered, by keeping track of how many notifiers for a
- +specific event (combination of registry, event target category, and event
- +instance ID) are currently registered. This means that a specific event will
- +be enabled when the first notifier for it is being registered and disabled
- +when the last notifier for it is being unregistered. Note that the event
- +flags are therefore only used on the first registered notifier, however, one
- +should take care that notifiers for a specific event are always registered
- +with the same flag and it is considered a bug to do otherwise.
- diff --git a/Documentation/driver-api/surface_aggregator/clients/index.rst b/Documentation/driver-api/surface_aggregator/clients/index.rst
- new file mode 100644
- index 000000000000..31e026d96102
- --- /dev/null
- +++ b/Documentation/driver-api/surface_aggregator/clients/index.rst
- @@ -0,0 +1,10 @@
- +.. SPDX-License-Identifier: GPL-2.0+
- +
- +===========================
- +Client Driver Documentation
- +===========================
- +
- +This is the documentation for client drivers themselves. Refer to
- +:doc:`../client` for documentation on how to write client drivers.
- +
- +.. Place documentation for individual client drivers here.
- diff --git a/Documentation/driver-api/surface_aggregator/index.rst b/Documentation/driver-api/surface_aggregator/index.rst
- new file mode 100644
- index 000000000000..6f3e1094904d
- --- /dev/null
- +++ b/Documentation/driver-api/surface_aggregator/index.rst
- @@ -0,0 +1,21 @@
- +.. SPDX-License-Identifier: GPL-2.0+
- +
- +=======================================
- +Surface System Aggregator Module (SSAM)
- +=======================================
- +
- +.. toctree::
- + :maxdepth: 2
- +
- + overview
- + client
- + clients/index
- + ssh
- + internal
- +
- +.. only:: subproject and html
- +
- + Indices
- + =======
- +
- + * :ref:`genindex`
- diff --git a/Documentation/driver-api/surface_aggregator/internal-api.rst b/Documentation/driver-api/surface_aggregator/internal-api.rst
- new file mode 100644
- index 000000000000..639a67b5a392
- --- /dev/null
- +++ b/Documentation/driver-api/surface_aggregator/internal-api.rst
- @@ -0,0 +1,67 @@
- +.. SPDX-License-Identifier: GPL-2.0+
- +
- +==========================
- +Internal API Documentation
- +==========================
- +
- +.. contents::
- + :depth: 2
- +
- +
- +Packet Transport Layer
- +======================
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/ssh_parser.h
- + :internal:
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/ssh_parser.c
- + :internal:
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/ssh_msgb.h
- + :internal:
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/ssh_packet_layer.h
- + :internal:
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/ssh_packet_layer.c
- + :internal:
- +
- +
- +Request Transport Layer
- +=======================
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/ssh_request_layer.h
- + :internal:
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/ssh_request_layer.c
- + :internal:
- +
- +
- +Controller
- +==========
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/controller.h
- + :internal:
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/controller.c
- + :internal:
- +
- +
- +Client Device Bus
- +=================
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/bus.c
- + :internal:
- +
- +
- +Core
- +====
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/core.c
- + :internal:
- +
- +
- +Trace Helpers
- +=============
- +
- +.. kernel-doc:: drivers/platform/surface/aggregator/trace.h
- diff --git a/Documentation/driver-api/surface_aggregator/internal.rst b/Documentation/driver-api/surface_aggregator/internal.rst
- new file mode 100644
- index 000000000000..72704734982a
- --- /dev/null
- +++ b/Documentation/driver-api/surface_aggregator/internal.rst
- @@ -0,0 +1,577 @@
- +.. SPDX-License-Identifier: GPL-2.0+
- +
- +.. |ssh_ptl| replace:: :c:type:`struct ssh_ptl <ssh_ptl>`
- +.. |ssh_ptl_submit| replace:: :c:func:`ssh_ptl_submit`
- +.. |ssh_ptl_cancel| replace:: :c:func:`ssh_ptl_cancel`
- +.. |ssh_ptl_shutdown| replace:: :c:func:`ssh_ptl_shutdown`
- +.. |ssh_ptl_rx_rcvbuf| replace:: :c:func:`ssh_ptl_rx_rcvbuf`
- +.. |ssh_rtl| replace:: :c:type:`struct ssh_rtl <ssh_rtl>`
- +.. |ssh_rtl_submit| replace:: :c:func:`ssh_rtl_submit`
- +.. |ssh_rtl_cancel| replace:: :c:func:`ssh_rtl_cancel`
- +.. |ssh_rtl_shutdown| replace:: :c:func:`ssh_rtl_shutdown`
- +.. |ssh_packet| replace:: :c:type:`struct ssh_packet <ssh_packet>`
- +.. |ssh_packet_get| replace:: :c:func:`ssh_packet_get`
- +.. |ssh_packet_put| replace:: :c:func:`ssh_packet_put`
- +.. |ssh_packet_ops| replace:: :c:type:`struct ssh_packet_ops <ssh_packet_ops>`
- +.. |ssh_packet_base_priority| replace:: :c:type:`enum ssh_packet_base_priority <ssh_packet_base_priority>`
- +.. |ssh_packet_flags| replace:: :c:type:`enum ssh_packet_flags <ssh_packet_flags>`
- +.. |SSH_PACKET_PRIORITY| replace:: :c:func:`SSH_PACKET_PRIORITY`
- +.. |ssh_frame| replace:: :c:type:`struct ssh_frame <ssh_frame>`
- +.. |ssh_command| replace:: :c:type:`struct ssh_command <ssh_command>`
- +.. |ssh_request| replace:: :c:type:`struct ssh_request <ssh_request>`
- +.. |ssh_request_get| replace:: :c:func:`ssh_request_get`
- +.. |ssh_request_put| replace:: :c:func:`ssh_request_put`
- +.. |ssh_request_ops| replace:: :c:type:`struct ssh_request_ops <ssh_request_ops>`
- +.. |ssh_request_init| replace:: :c:func:`ssh_request_init`
- +.. |ssh_request_flags| replace:: :c:type:`enum ssh_request_flags <ssh_request_flags>`
- +.. |ssam_controller| replace:: :c:type:`struct ssam_controller <ssam_controller>`
- +.. |ssam_device| replace:: :c:type:`struct ssam_device <ssam_device>`
- +.. |ssam_device_driver| replace:: :c:type:`struct ssam_device_driver <ssam_device_driver>`
- +.. |ssam_client_bind| replace:: :c:func:`ssam_client_bind`
- +.. |ssam_client_link| replace:: :c:func:`ssam_client_link`
- +.. |ssam_request_sync| replace:: :c:type:`struct ssam_request_sync <ssam_request_sync>`
- +.. |ssam_event_registry| replace:: :c:type:`struct ssam_event_registry <ssam_event_registry>`
- +.. |ssam_event_id| replace:: :c:type:`struct ssam_event_id <ssam_event_id>`
- +.. |ssam_nf| replace:: :c:type:`struct ssam_nf <ssam_nf>`
- +.. |ssam_nf_refcount_inc| replace:: :c:func:`ssam_nf_refcount_inc`
- +.. |ssam_nf_refcount_dec| replace:: :c:func:`ssam_nf_refcount_dec`
- +.. |ssam_notifier_register| replace:: :c:func:`ssam_notifier_register`
- +.. |ssam_notifier_unregister| replace:: :c:func:`ssam_notifier_unregister`
- +.. |ssam_cplt| replace:: :c:type:`struct ssam_cplt <ssam_cplt>`
- +.. |ssam_event_queue| replace:: :c:type:`struct ssam_event_queue <ssam_event_queue>`
- +.. |ssam_request_sync_submit| replace:: :c:func:`ssam_request_sync_submit`
- +
- +=====================
- +Core Driver Internals
- +=====================
- +
- +Architectural overview of the Surface System Aggregator Module (SSAM) core
- +and Surface Serial Hub (SSH) driver. For the API documentation, refer to:
- +
- +.. toctree::
- + :maxdepth: 2
- +
- + internal-api
- +
- +
- +Overview
- +========
- +
- +The SSAM core implementation is structured in layers, somewhat following the
- +SSH protocol structure:
- +
- +Lower-level packet transport is implemented in the *packet transport layer
- +(PTL)*, directly building on top of the serial device (serdev)
- +infrastructure of the kernel. As the name indicates, this layer deals with
- +the packet transport logic and handles things like packet validation, packet
- +acknowledgment (ACKing), packet (retransmission) timeouts, and relaying
- +packet payloads to higher-level layers.
- +
- +Above this sits the *request transport layer (RTL)*. This layer is centered
- +around command-type packet payloads, i.e. requests (sent from host to EC),
- +responses of the EC to those requests, and events (sent from EC to host).
- +It, specifically, distinguishes events from request responses, matches
- +responses to their corresponding requests, and implements request timeouts.
- +
- +The *controller* layer is building on top of this and essentially decides
- +how request responses and, especially, events are dealt with. It provides an
- +event notifier system, handles event activation/deactivation, provides a
- +workqueue for event and asynchronous request completion, and also manages
- +the message counters required for building command messages (``SEQ``,
- +``RQID``). This layer basically provides a fundamental interface to the SAM
- +EC for use in other kernel drivers.
- +
- +While the controller layer already provides an interface for other kernel
- +drivers, the client *bus* extends this interface to provide support for
- +native SSAM devices, i.e. devices that are not defined in ACPI and not
- +implemented as platform devices, via |ssam_device| and |ssam_device_driver|
- +simplify management of client devices and client drivers.
- +
- +Refer to :doc:`client` for documentation regarding the client device/driver
- +API and interface options for other kernel drivers. It is recommended to
- +familiarize oneself with that chapter and the :doc:`ssh` before continuing
- +with the architectural overview below.
- +
- +
- +Packet Transport Layer
- +======================
- +
- +The packet transport layer is represented via |ssh_ptl| and is structured
- +around the following key concepts:
- +
- +Packets
- +-------
- +
- +Packets are the fundamental transmission unit of the SSH protocol. They are
- +managed by the packet transport layer, which is essentially the lowest layer
- +of the driver and is built upon by other components of the SSAM core.
- +Packets to be transmitted by the SSAM core are represented via |ssh_packet|
- +(in contrast, packets received by the core do not have any specific
- +structure and are managed entirely via the raw |ssh_frame|).
- +
- +This structure contains the required fields to manage the packet inside the
- +transport layer, as well as a reference to the buffer containing the data to
- +be transmitted (i.e. the message wrapped in |ssh_frame|). Most notably, it
- +contains an internal reference count, which is used for managing its
- +lifetime (accessible via |ssh_packet_get| and |ssh_packet_put|). When this
- +counter reaches zero, the ``release()`` callback provided to the packet via
- +its |ssh_packet_ops| reference is executed, which may then deallocate the
- +packet or its enclosing structure (e.g. |ssh_request|).
- +
- +In addition to the ``release`` callback, the |ssh_packet_ops| reference also
- +provides a ``complete()`` callback, which is run once the packet has been
- +completed and provides the status of this completion, i.e. zero on success
- +or a negative errno value in case of an error. Once the packet has been
- +submitted to the packet transport layer, the ``complete()`` callback is
- +always guaranteed to be executed before the ``release()`` callback, i.e. the
- +packet will always be completed, either successfully, with an error, or due
- +to cancellation, before it will be released.
- +
- +The state of a packet is managed via its ``state`` flags
- +(|ssh_packet_flags|), which also contains the packet type. In particular,
- +the following bits are noteworthy:
- +
- +* ``SSH_PACKET_SF_LOCKED_BIT``: This bit is set when completion, either
- + through error or success, is imminent. It indicates that no further
- + references of the packet should be taken and any existing references
- + should be dropped as soon as possible. The process setting this bit is
- + responsible for removing any references to this packet from the packet
- + queue and pending set.
- +
- +* ``SSH_PACKET_SF_COMPLETED_BIT``: This bit is set by the process running the
- + ``complete()`` callback and is used to ensure that this callback only runs
- + once.
- +
- +* ``SSH_PACKET_SF_QUEUED_BIT``: This bit is set when the packet is queued on
- + the packet queue and cleared when it is dequeued.
- +
- +* ``SSH_PACKET_SF_PENDING_BIT``: This bit is set when the packet is added to
- + the pending set and cleared when it is removed from it.
- +
- +Packet Queue
- +------------
- +
- +The packet queue is the first of the two fundamental collections in the
- +packet transport layer. It is a priority queue, with priority of the
- +respective packets based on the packet type (major) and number of tries
- +(minor). See |SSH_PACKET_PRIORITY| for more details on the priority value.
- +
- +All packets to be transmitted by the transport layer must be submitted to
- +this queue via |ssh_ptl_submit|. Note that this includes control packets
- +sent by the transport layer itself. Internally, data packets can be
- +re-submitted to this queue due to timeouts or NAK packets sent by the EC.
- +
- +Pending Set
- +-----------
- +
- +The pending set is the second of the two fundamental collections in the
- +packet transport layer. It stores references to packets that have already
- +been transmitted, but wait for acknowledgment (e.g. the corresponding ACK
- +packet) by the EC.
- +
- +Note that a packet may both be pending and queued if it has been
- +re-submitted due to a packet acknowledgment timeout or NAK. On such a
- +re-submission, packets are not removed from the pending set.
- +
- +Transmitter Thread
- +------------------
- +
- +The transmitter thread is responsible for most of the actual work regarding
- +packet transmission. In each iteration, it (waits for and) checks if the
- +next packet on the queue (if any) can be transmitted and, if so, removes it
- +from the queue and increments its counter for the number of transmission
- +attempts, i.e. tries. If the packet is sequenced, i.e. requires an ACK by
- +the EC, the packet is added to the pending set. Next, the packet's data is
- +submitted to the serdev subsystem. In case of an error or timeout during
- +this submission, the packet is completed by the transmitter thread with the
- +status value of the callback set accordingly. In case the packet is
- +unsequenced, i.e. does not require an ACK by the EC, the packet is completed
- +with success on the transmitter thread.
- +
- +Transmission of sequenced packets is limited by the number of concurrently
- +pending packets, i.e. a limit on how many packets may be waiting for an ACK
- +from the EC in parallel. This limit is currently set to one (see :doc:`ssh`
- +for the reasoning behind this). Control packets (i.e. ACK and NAK) can
- +always be transmitted.
- +
- +Receiver Thread
- +---------------
- +
- +Any data received from the EC is put into a FIFO buffer for further
- +processing. This processing happens on the receiver thread. The receiver
- +thread parses and validates the received message into its |ssh_frame| and
- +corresponding payload. It prepares and submits the necessary ACK (and on
- +validation error or invalid data NAK) packets for the received messages.
- +
- +This thread also handles further processing, such as matching ACK messages
- +to the corresponding pending packet (via sequence ID) and completing it, as
- +well as initiating re-submission of all currently pending packets on
- +receival of a NAK message (re-submission in case of a NAK is similar to
- +re-submission due to timeout, see below for more details on that). Note that
- +the successful completion of a sequenced packet will always run on the
- +receiver thread (whereas any failure-indicating completion will run on the
- +process where the failure occurred).
- +
- +Any payload data is forwarded via a callback to the next upper layer, i.e.
- +the request transport layer.
- +
- +Timeout Reaper
- +--------------
- +
- +The packet acknowledgment timeout is a per-packet timeout for sequenced
- +packets, started when the respective packet begins (re-)transmission (i.e.
- +this timeout is armed once per transmission attempt on the transmitter
- +thread). It is used to trigger re-submission or, when the number of tries
- +has been exceeded, cancellation of the packet in question.
- +
- +This timeout is handled via a dedicated reaper task, which is essentially a
- +work item (re-)scheduled to run when the next packet is set to time out. The
- +work item then checks the set of pending packets for any packets that have
- +exceeded the timeout and, if there are any remaining packets, re-schedules
- +itself to the next appropriate point in time.
- +
- +If a timeout has been detected by the reaper, the packet will either be
- +re-submitted if it still has some remaining tries left, or completed with
- +``-ETIMEDOUT`` as status if not. Note that re-submission, in this case and
- +triggered by receival of a NAK, means that the packet is added to the queue
- +with a now incremented number of tries, yielding a higher priority. The
- +timeout for the packet will be disabled until the next transmission attempt
- +and the packet remains on the pending set.
- +
- +Note that due to transmission and packet acknowledgment timeouts, the packet
- +transport layer is always guaranteed to make progress, if only through
- +timing out packets, and will never fully block.
- +
- +Concurrency and Locking
- +-----------------------
- +
- +There are two main locks in the packet transport layer: One guarding access
- +to the packet queue and one guarding access to the pending set. These
- +collections may only be accessed and modified under the respective lock. If
- +access to both collections is needed, the pending lock must be acquired
- +before the queue lock to avoid deadlocks.
- +
- +In addition to guarding the collections, after initial packet submission
- +certain packet fields may only be accessed under one of the locks.
- +Specifically, the packet priority must only be accessed while holding the
- +queue lock and the packet timestamp must only be accessed while holding the
- +pending lock.
- +
- +Other parts of the packet transport layer are guarded independently. State
- +flags are managed by atomic bit operations and, if necessary, memory
- +barriers. Modifications to the timeout reaper work item and expiration date
- +are guarded by their own lock.
- +
- +The reference of the packet to the packet transport layer (``ptl``) is
- +somewhat special. It is either set when the upper layer request is submitted
- +or, if there is none, when the packet is first submitted. After it is set,
- +it will not change its value. Functions that may run concurrently with
- +submission, i.e. cancellation, can not rely on the ``ptl`` reference to be
- +set. Access to it in these functions is guarded by ``READ_ONCE()``, whereas
- +setting ``ptl`` is equally guarded with ``WRITE_ONCE()`` for symmetry.
- +
- +Some packet fields may be read outside of the respective locks guarding
- +them, specifically priority and state for tracing. In those cases, proper
- +access is ensured by employing ``WRITE_ONCE()`` and ``READ_ONCE()``. Such
- +read-only access is only allowed when stale values are not critical.
- +
- +With respect to the interface for higher layers, packet submission
- +(|ssh_ptl_submit|), packet cancellation (|ssh_ptl_cancel|), data receival
- +(|ssh_ptl_rx_rcvbuf|), and layer shutdown (|ssh_ptl_shutdown|) may always be
- +executed concurrently with respect to each other. Note that packet
- +submission may not run concurrently with itself for the same packet.
- +Equally, shutdown and data receival may also not run concurrently with
- +themselves (but may run concurrently with each other).
- +
- +
- +Request Transport Layer
- +=======================
- +
- +The request transport layer is represented via |ssh_rtl| and builds on top
- +of the packet transport layer. It deals with requests, i.e. SSH packets sent
- +by the host containing a |ssh_command| as frame payload. This layer
- +separates responses to requests from events, which are also sent by the EC
- +via a |ssh_command| payload. While responses are handled in this layer,
- +events are relayed to the next upper layer, i.e. the controller layer, via
- +the corresponding callback. The request transport layer is structured around
- +the following key concepts:
- +
- +Request
- +-------
- +
- +Requests are packets with a command-type payload, sent from host to EC to
- +query data from or trigger an action on it (or both simultaneously). They
- +are represented by |ssh_request|, wrapping the underlying |ssh_packet|
- +storing its message data (i.e. SSH frame with command payload). Note that
- +all top-level representations, e.g. |ssam_request_sync| are built upon this
- +struct.
- +
- +As |ssh_request| extends |ssh_packet|, its lifetime is also managed by the
- +reference counter inside the packet struct (which can be accessed via
- +|ssh_request_get| and |ssh_request_put|). Once the counter reaches zero, the
- +``release()`` callback of the |ssh_request_ops| reference of the request is
- +called.
- +
- +Requests can have an optional response that is equally sent via a SSH
- +message with command-type payload (from EC to host). The party constructing
- +the request must know if a response is expected and mark this in the request
- +flags provided to |ssh_request_init|, so that the request transport layer
- +can wait for this response.
- +
- +Similar to |ssh_packet|, |ssh_request| also has a ``complete()`` callback
- +provided via its request ops reference and is guaranteed to be completed
- +before it is released once it has been submitted to the request transport
- +layer via |ssh_rtl_submit|. For a request without a response, successful
- +completion will occur once the underlying packet has been successfully
- +transmitted by the packet transport layer (i.e. from within the packet
- +completion callback). For a request with response, successful completion
- +will occur once the response has been received and matched to the request
- +via its request ID (which happens on the packet layer's data-received
- +callback running on the receiver thread). If the request is completed with
- +an error, the status value will be set to the corresponding (negative) errno
- +value.
- +
- +The state of a request is again managed via its ``state`` flags
- +(|ssh_request_flags|), which also encode the request type. In particular,
- +the following bits are noteworthy:
- +
- +* ``SSH_REQUEST_SF_LOCKED_BIT``: This bit is set when completion, either
- + through error or success, is imminent. It indicates that no further
- + references of the request should be taken and any existing references
- + should be dropped as soon as possible. The process setting this bit is
- + responsible for removing any references to this request from the request
- + queue and pending set.
- +
- +* ``SSH_REQUEST_SF_COMPLETED_BIT``: This bit is set by the process running the
- + ``complete()`` callback and is used to ensure that this callback only runs
- + once.
- +
- +* ``SSH_REQUEST_SF_QUEUED_BIT``: This bit is set when the request is queued on
- + the request queue and cleared when it is dequeued.
- +
- +* ``SSH_REQUEST_SF_PENDING_BIT``: This bit is set when the request is added to
- + the pending set and cleared when it is removed from it.
- +
- +Request Queue
- +-------------
- +
- +The request queue is the first of the two fundamental collections in the
- +request transport layer. In contrast to the packet queue of the packet
- +transport layer, it is not a priority queue and the simple first come first
- +serve principle applies.
- +
- +All requests to be transmitted by the request transport layer must be
- +submitted to this queue via |ssh_rtl_submit|. Once submitted, requests may
- +not be re-submitted, and will not be re-submitted automatically on timeout.
- +Instead, the request is completed with a timeout error. If desired, the
- +caller can create and submit a new request for another try, but it must not
- +submit the same request again.
- +
- +Pending Set
- +-----------
- +
- +The pending set is the second of the two fundamental collections in the
- +request transport layer. This collection stores references to all pending
- +requests, i.e. requests awaiting a response from the EC (similar to what the
- +pending set of the packet transport layer does for packets).
- +
- +Transmitter Task
- +----------------
- +
- +The transmitter task is scheduled when a new request is available for
- +transmission. It checks if the next request on the request queue can be
- +transmitted and, if so, submits its underlying packet to the packet
- +transport layer. This check ensures that only a limited number of
- +requests can be pending, i.e. waiting for a response, at the same time. If
- +the request requires a response, the request is added to the pending set
- +before its packet is submitted.
- +
- +Packet Completion Callback
- +--------------------------
- +
- +The packet completion callback is executed once the underlying packet of a
- +request has been completed. In case of an error completion, the
- +corresponding request is completed with the error value provided in this
- +callback.
- +
- +On successful packet completion, further processing depends on the request.
- +If the request expects a response, it is marked as transmitted and the
- +request timeout is started. If the request does not expect a response, it is
- +completed with success.
- +
- +Data-Received Callback
- +----------------------
- +
- +The data received callback notifies the request transport layer of data
- +being received by the underlying packet transport layer via a data-type
- +frame. In general, this is expected to be a command-type payload.
- +
- +If the request ID of the command is one of the request IDs reserved for
- +events (one to ``SSH_NUM_EVENTS``, inclusively), it is forwarded to the
- +event callback registered in the request transport layer. If the request ID
- +indicates a response to a request, the respective request is looked up in
- +the pending set and, if found and marked as transmitted, completed with
- +success.
- +
- +Timeout Reaper
- +--------------
- +
- +The request-response-timeout is a per-request timeout for requests expecting
- +a response. It is used to ensure that a request does not wait indefinitely
- +on a response from the EC and is started after the underlying packet has
- +been successfully completed.
- +
- +This timeout is, similar to the packet acknowledgment timeout on the packet
- +transport layer, handled via a dedicated reaper task. This task is
- +essentially a work-item (re-)scheduled to run when the next request is set
- +to time out. The work item then scans the set of pending requests for any
- +requests that have timed out and completes them with ``-ETIMEDOUT`` as
- +status. Requests will not be re-submitted automatically. Instead, the issuer
- +of the request must construct and submit a new request, if so desired.
- +
- +Note that this timeout, in combination with packet transmission and
- +acknowledgment timeouts, guarantees that the request layer will always make
- +progress, even if only through timing out packets, and never fully block.
- +
- +Concurrency and Locking
- +-----------------------
- +
- +Similar to the packet transport layer, there are two main locks in the
- +request transport layer: One guarding access to the request queue and one
- +guarding access to the pending set. These collections may only be accessed
- +and modified under the respective lock.
- +
- +Other parts of the request transport layer are guarded independently. State
- +flags are (again) managed by atomic bit operations and, if necessary, memory
- +barriers. Modifications to the timeout reaper work item and expiration date
- +are guarded by their own lock.
- +
- +Some request fields may be read outside of the respective locks guarding
- +them, specifically the state for tracing. In those cases, proper access is
- +ensured by employing ``WRITE_ONCE()`` and ``READ_ONCE()``. Such read-only
- +access is only allowed when stale values are not critical.
- +
- +With respect to the interface for higher layers, request submission
- +(|ssh_rtl_submit|), request cancellation (|ssh_rtl_cancel|), and layer
- +shutdown (|ssh_rtl_shutdown|) may always be executed concurrently with
- +respect to each other. Note that request submission may not run concurrently
- +with itself for the same request (and also may only be called once per
- +request). Equally, shutdown may also not run concurrently with itself.
- +
- +
- +Controller Layer
- +================
- +
- +The controller layer extends on the request transport layer to provide an
- +easy-to-use interface for client drivers. It is represented by
- +|ssam_controller| and the SSH driver. While the lower level transport layers
- +take care of transmitting and handling packets and requests, the controller
- +layer takes on more of a management role. Specifically, it handles device
- +initialization, power management, and event handling, including event
- +delivery and registration via the (event) completion system (|ssam_cplt|).
- +
- +Event Registration
- +------------------
- +
- +In general, an event (or rather a class of events) has to be explicitly
- +requested by the host before the EC will send it (HID input events seem to
- +be the exception). This is done via an event-enable request (similarly,
- +events should be disabled via an event-disable request once no longer
- +desired).
- +
- +The specific request used to enable (or disable) an event is given via an
- +event registry, i.e. the governing authority of this event (so to speak),
- +represented by |ssam_event_registry|. As parameters to this request, the
- +target category and, depending on the event registry, instance ID of the
- +event to be enabled must be provided. This (optional) instance ID must be
- +zero if the registry does not use it. Together, target category and instance
- +ID form the event ID, represented by |ssam_event_id|. In short, both, event
- +registry and event ID, are required to uniquely identify a respective class
- +of events.
- +
- +Note that a further *request ID* parameter must be provided for the
- +enable-event request. This parameter does not influence the class of events
- +being enabled, but instead is set as the request ID (RQID) on each event of
- +this class sent by the EC. It is used to identify events (as a limited
- +number of request IDs is reserved for use in events only, specifically one
- +to ``SSH_NUM_EVENTS`` inclusively) and also map events to their specific
- +class. Currently, the controller always sets this parameter to the target
- +category specified in |ssam_event_id|.
- +
- +As multiple client drivers may rely on the same (or overlapping) classes of
- +events and enable/disable calls are strictly binary (i.e. on/off), the
- +controller has to manage access to these events. It does so via reference
- +counting, storing the counter inside an RB-tree based mapping with event
- +registry and ID as key (there is no known list of valid event registry and
- +event ID combinations). See |ssam_nf|, |ssam_nf_refcount_inc|, and
- +|ssam_nf_refcount_dec| for details.
- +
- +This management is done together with notifier registration (described in
- +the next section) via the top-level |ssam_notifier_register| and
- +|ssam_notifier_unregister| functions.
- +
- +Event Delivery
- +--------------
- +
- +To receive events, a client driver has to register an event notifier via
- +|ssam_notifier_register|. This increments the reference counter for that
- +specific class of events (as detailed in the previous section), enables the
- +class on the EC (if it has not been enabled already), and installs the
- +provided notifier callback.
- +
- +Notifier callbacks are stored in lists, with one (RCU) list per target
- +category (provided via the event ID; NB: there is a fixed known number of
- +target categories). There is no known association from the combination of
- +event registry and event ID to the command data (target ID, target category,
- +command ID, and instance ID) that can be provided by an event class, apart
- +from target category and instance ID given via the event ID.
- +
- +Note that due to the way notifiers are (or rather have to be) stored, client
- +drivers may receive events that they have not requested and need to account
- +for them. Specifically, they will, by default, receive all events from the
- +same target category. To simplify dealing with this, filtering of events by
- +target ID (provided via the event registry) and instance ID (provided via
- +the event ID) can be requested when registering a notifier. This filtering
- +is applied when iterating over the notifiers at the time they are executed.
- +
- +All notifier callbacks are executed on a dedicated workqueue, the so-called
- +completion workqueue. After an event has been received via the callback
- +installed in the request layer (running on the receiver thread of the packet
- +transport layer), it will be put on its respective event queue
- +(|ssam_event_queue|). From this event queue the completion work item of that
- +queue (running on the completion workqueue) will pick up the event and
- +execute the notifier callback. This is done to avoid blocking on the
- +receiver thread.
- +
- +There is one event queue per combination of target ID and target category.
- +This is done to ensure that notifier callbacks are executed in sequence for
- +events of the same target ID and target category. Callbacks can be executed
- +in parallel for events with a different combination of target ID and target
- +category.
- +
- +Concurrency and Locking
- +-----------------------
- +
- +Most of the concurrency related safety guarantees of the controller are
- +provided by the lower-level request transport layer. In addition to this,
- +event (un-)registration is guarded by its own lock.
- +
- +Access to the controller state is guarded by the state lock. This lock is a
- +read/write semaphore. The reader part can be used to ensure that the state
- +does not change while functions depending on the state to stay the same
- +(e.g. |ssam_notifier_register|, |ssam_notifier_unregister|,
- +|ssam_request_sync_submit|, and derivatives) are executed and this guarantee
- +is not already provided otherwise (e.g. through |ssam_client_bind| or
- +|ssam_client_link|). The writer part guards any transitions that will change
- +the state, i.e. initialization, destruction, suspension, and resumption.
- +
- +The controller state may be accessed (read-only) outside the state lock for
- +smoke-testing against invalid API usage (e.g. in |ssam_request_sync_submit|).
- +Note that such checks are not supposed to (and will not) protect against all
- +invalid usages, but rather aim to help catch them. In those cases, proper
- +variable access is ensured by employing ``WRITE_ONCE()`` and ``READ_ONCE()``.
- +
- +Assuming any preconditions on the state not changing have been satisfied,
- +all non-initialization and non-shutdown functions may run concurrently with
- +each other. This includes |ssam_notifier_register|, |ssam_notifier_unregister|,
- +|ssam_request_sync_submit|, as well as all functions building on top of those.
- diff --git a/Documentation/driver-api/surface_aggregator/overview.rst b/Documentation/driver-api/surface_aggregator/overview.rst
- new file mode 100644
- index 000000000000..1e9d57e50063
- --- /dev/null
- +++ b/Documentation/driver-api/surface_aggregator/overview.rst
- @@ -0,0 +1,77 @@
- +.. SPDX-License-Identifier: GPL-2.0+
- +
- +========
- +Overview
- +========
- +
- +The Surface/System Aggregator Module (SAM, SSAM) is an (arguably *the*)
- +embedded controller (EC) on Microsoft Surface devices. It has been originally
- +introduced on 4th generation devices (Surface Pro 4, Surface Book 1), but
- +its responsibilities and feature-set have since been expanded significantly
- +with the following generations.
- +
- +
- +Features and Integration
- +========================
- +
- +Not much is currently known about SAM on 4th generation devices (Surface Pro
- +4, Surface Book 1), due to the use of a different communication interface
- +between host and EC (as detailed below). On 5th (Surface Pro 2017, Surface
- +Book 2, Surface Laptop 1) and later generation devices, SAM is responsible
- +for providing battery information (both current status and static values,
- +such as maximum capacity etc.), as well as an assortment of temperature
- +sensors (e.g. skin temperature) and cooling/performance-mode setting to the
- +host. On the Surface Book 2, specifically, it additionally provides an
- +interface for properly handling clipboard detachment (i.e. separating the
- +display part from the keyboard part of the device), on the Surface Laptop 1
- +and 2 it is required for keyboard HID input. This HID subsystem has been
- +restructured for 7th generation devices and on those, specifically Surface
- +Laptop 3 and Surface Book 3, is responsible for all major HID input (i.e.
- +keyboard and touchpad).
- +
- +While features have not changed much on a coarse level since the 5th
- +generation, internal interfaces have undergone some rather large changes. On
- +5th and 6th generation devices, both battery and temperature information is
- +exposed to ACPI via a shim driver (referred to as Surface ACPI Notify, or
- +SAN), translating ACPI generic serial bus write-/read-accesses to SAM
- +requests. On 7th generation devices, this additional layer is gone and these
- +devices require a driver hooking directly into the SAM interface. Equally,
- +on newer generations, less devices are declared in ACPI, making them a bit
- +harder to discover and requiring us to hard-code a sort of device registry.
- +Due to this, a SSAM bus and subsystem with client devices
- +(:c:type:`struct ssam_device <ssam_device>`) has been implemented.
- +
- +
- +Communication
- +=============
- +
- +The type of communication interface between host and EC depends on the
- +generation of the Surface device. On 4th generation devices, host and EC
- +communicate via HID, specifically using a HID-over-I2C device, whereas on
- +5th and later generations, communication takes place via a USART serial
- +device. In accordance to the drivers found on other operating systems, we
- +refer to the serial device and its driver as Surface Serial Hub (SSH). When
- +needed, we differentiate between both types of SAM by referring to them as
- +SAM-over-SSH and SAM-over-HID.
- +
- +Currently, this subsystem only supports SAM-over-SSH. The SSH communication
- +interface is described in more detail below. The HID interface has not been
- +reverse engineered yet and it is, at the moment, unclear how many (and
- +which) concepts of the SSH interface detailed below can be transferred to
- +it.
- +
- +Surface Serial Hub
- +------------------
- +
- +As already elaborated above, the Surface Serial Hub (SSH) is the
- +communication interface for SAM on 5th- and all later-generation Surface
- +devices. On the highest level, communication can be separated into two main
- +types: Requests, messages sent from host to EC that may trigger a direct
- +response from the EC (explicitly associated with the request), and events
- +(sometimes also referred to as notifications), sent from EC to host without
- +being a direct response to a previous request. We may also refer to requests
- +without response as commands. In general, events need to be enabled via one
- +of multiple dedicated requests before they are sent by the EC.
- +
- +See :doc:`ssh` for a more technical protocol documentation and
- +:doc:`internal` for an overview of the internal driver architecture.
- diff --git a/Documentation/driver-api/surface_aggregator/ssh.rst b/Documentation/driver-api/surface_aggregator/ssh.rst
- new file mode 100644
- index 000000000000..bf007d6c9873
- --- /dev/null
- +++ b/Documentation/driver-api/surface_aggregator/ssh.rst
- @@ -0,0 +1,344 @@
- +.. SPDX-License-Identifier: GPL-2.0+
- +
- +.. |u8| replace:: :c:type:`u8 <u8>`
- +.. |u16| replace:: :c:type:`u16 <u16>`
- +.. |TYPE| replace:: ``TYPE``
- +.. |LEN| replace:: ``LEN``
- +.. |SEQ| replace:: ``SEQ``
- +.. |SYN| replace:: ``SYN``
- +.. |NAK| replace:: ``NAK``
- +.. |ACK| replace:: ``ACK``
- +.. |DATA| replace:: ``DATA``
- +.. |DATA_SEQ| replace:: ``DATA_SEQ``
- +.. |DATA_NSQ| replace:: ``DATA_NSQ``
- +.. |TC| replace:: ``TC``
- +.. |TID| replace:: ``TID``
- +.. |IID| replace:: ``IID``
- +.. |RQID| replace:: ``RQID``
- +.. |CID| replace:: ``CID``
- +
- +===========================
- +Surface Serial Hub Protocol
- +===========================
- +
- +The Surface Serial Hub (SSH) is the central communication interface for the
- +embedded Surface Aggregator Module controller (SAM or EC), found on newer
- +Surface generations. We will refer to this protocol and interface as
- +SAM-over-SSH, as opposed to SAM-over-HID for the older generations.
- +
- +On Surface devices with SAM-over-SSH, SAM is connected to the host via UART
- +and defined in ACPI as device with ID ``MSHW0084``. On these devices,
- +significant functionality is provided via SAM, including access to battery
- +and power information and events, thermal read-outs and events, and many
- +more. For Surface Laptops, keyboard input is handled via HID directed
- +through SAM, on the Surface Laptop 3 and Surface Book 3 this also includes
- +touchpad input.
- +
- +Note that the standard disclaimer for this subsystem also applies to this
- +document: All of this has been reverse-engineered and may thus be erroneous
- +and/or incomplete.
- +
- +All CRCs used in the following are two-byte ``crc_ccitt_false(0xffff, ...)``.
- +All multi-byte values are little-endian, there is no implicit padding between
- +values.
- +
- +
- +SSH Packet Protocol: Definitions
- +================================
- +
- +The fundamental communication unit of the SSH protocol is a frame
- +(:c:type:`struct ssh_frame <ssh_frame>`). A frame consists of the following
- +fields, packed together and in order:
- +
- +.. flat-table:: SSH Frame
- + :widths: 1 1 4
- + :header-rows: 1
- +
- + * - Field
- + - Type
- + - Description
- +
- + * - |TYPE|
- + - |u8|
- + - Type identifier of the frame.
- +
- + * - |LEN|
- + - |u16|
- + - Length of the payload associated with the frame.
- +
- + * - |SEQ|
- + - |u8|
- + - Sequence ID (see explanation below).
- +
- +Each frame structure is followed by a CRC over this structure. The CRC over
- +the frame structure (|TYPE|, |LEN|, and |SEQ| fields) is placed directly
- +after the frame structure and before the payload. The payload is followed by
- +its own CRC (over all payload bytes). If the payload is not present (i.e.
- +the frame has ``LEN=0``), the CRC of the payload is still present and will
- +evaluate to ``0xffff``. The |LEN| field does not include any of the CRCs, it
- +equals the number of bytes inbetween the CRC of the frame and the CRC of the
- +payload.
- +
- +Additionally, the following fixed two-byte sequences are used:
- +
- +.. flat-table:: SSH Byte Sequences
- + :widths: 1 1 4
- + :header-rows: 1
- +
- + * - Name
- + - Value
- + - Description
- +
- + * - |SYN|
- + - ``[0xAA, 0x55]``
- + - Synchronization bytes.
- +
- +A message consists of |SYN|, followed by the frame (|TYPE|, |LEN|, |SEQ| and
- +CRC) and, if specified in the frame (i.e. ``LEN > 0``), payload bytes,
- +followed finally, regardless if the payload is present, the payload CRC. The
- +messages corresponding to an exchange are, in part, identified by having the
- +same sequence ID (|SEQ|), stored inside the frame (more on this in the next
- +section). The sequence ID is a wrapping counter.
- +
- +A frame can have the following types
- +(:c:type:`enum ssh_frame_type <ssh_frame_type>`):
- +
- +.. flat-table:: SSH Frame Types
- + :widths: 1 1 4
- + :header-rows: 1
- +
- + * - Name
- + - Value
- + - Short Description
- +
- + * - |NAK|
- + - ``0x04``
- + - Sent on error in previously received message.
- +
- + * - |ACK|
- + - ``0x40``
- + - Sent to acknowledge receival of |DATA| frame.
- +
- + * - |DATA_SEQ|
- + - ``0x80``
- + - Sent to transfer data. Sequenced.
- +
- + * - |DATA_NSQ|
- + - ``0x00``
- + - Same as |DATA_SEQ|, but does not need to be ACKed.
- +
- +Both |NAK|- and |ACK|-type frames are used to control flow of messages and
- +thus do not carry a payload. |DATA_SEQ|- and |DATA_NSQ|-type frames on the
- +other hand must carry a payload. The flow sequence and interaction of
- +different frame types will be described in more depth in the next section.
- +
- +
- +SSH Packet Protocol: Flow Sequence
- +==================================
- +
- +Each exchange begins with |SYN|, followed by a |DATA_SEQ|- or
- +|DATA_NSQ|-type frame, followed by its CRC, payload, and payload CRC. In
- +case of a |DATA_NSQ|-type frame, the exchange is then finished. In case of a
- +|DATA_SEQ|-type frame, the receiving party has to acknowledge receival of
- +the frame by responding with a message containing an |ACK|-type frame with
- +the same sequence ID of the |DATA| frame. In other words, the sequence ID of
- +the |ACK| frame specifies the |DATA| frame to be acknowledged. In case of an
- +error, e.g. an invalid CRC, the receiving party responds with a message
- +containing an |NAK|-type frame. As the sequence ID of the previous data
- +frame, for which an error is indicated via the |NAK| frame, cannot be relied
- +upon, the sequence ID of the |NAK| frame should not be used and is set to
- +zero. After receival of an |NAK| frame, the sending party should re-send all
- +outstanding (non-ACKed) messages.
- +
- +Sequence IDs are not synchronized between the two parties, meaning that they
- +are managed independently for each party. Identifying the messages
- +corresponding to a single exchange thus relies on the sequence ID as well as
- +the type of the message, and the context. Specifically, the sequence ID is
- +used to associate an ``ACK`` with its ``DATA_SEQ``-type frame, but not
- +``DATA_SEQ``- or ``DATA_NSQ``-type frames with other ``DATA``- type frames.
- +
- +An example exchange might look like this:
- +
- +::
- +
- + tx: -- SYN FRAME(D) CRC(F) PAYLOAD CRC(P) -----------------------------
- + rx: ------------------------------------- SYN FRAME(A) CRC(F) CRC(P) --
- +
- +where both frames have the same sequence ID (``SEQ``). Here, ``FRAME(D)``
- +indicates a |DATA_SEQ|-type frame, ``FRAME(A)`` an ``ACK``-type frame,
- +``CRC(F)`` the CRC over the previous frame, ``CRC(P)`` the CRC over the
- +previous payload. In case of an error, the exchange would look like this:
- +
- +::
- +
- + tx: -- SYN FRAME(D) CRC(F) PAYLOAD CRC(P) -----------------------------
- + rx: ------------------------------------- SYN FRAME(N) CRC(F) CRC(P) --
- +
- +upon which the sender should re-send the message. ``FRAME(N)`` indicates an
- +|NAK|-type frame. Note that the sequence ID of the |NAK|-type frame is fixed
- +to zero. For |DATA_NSQ|-type frames, both exchanges are the same:
- +
- +::
- +
- + tx: -- SYN FRAME(DATA_NSQ) CRC(F) PAYLOAD CRC(P) ----------------------
- + rx: -------------------------------------------------------------------
- +
- +Here, an error can be detected, but not corrected or indicated to the
- +sending party. These exchanges are symmetric, i.e. switching ``rx`` and
- +``tx`` results again in a valid exchange. Currently, no longer exchanges are
- +known.
- +
- +
- +Commands: Requests, Responses, and Events
- +=========================================
- +
- +Commands are sent as payload inside a data frame. Currently, this is the
- +only known payload type of |DATA| frames, with a payload-type value of
- +``0x80`` (:c:type:`SSH_PLD_TYPE_CMD <ssh_payload_type>`).
- +
- +The command-type payload (:c:type:`struct ssh_command <ssh_command>`)
- +consists of an eight-byte command structure, followed by optional and
- +variable length command data. The length of this optional data is derived
- +from the frame payload length given in the corresponding frame, i.e. it is
- +``frame.len - sizeof(struct ssh_command)``. The command struct contains the
- +following fields, packed together and in order:
- +
- +.. flat-table:: SSH Command
- + :widths: 1 1 4
- + :header-rows: 1
- +
- + * - Field
- + - Type
- + - Description
- +
- + * - |TYPE|
- + - |u8|
- + - Type of the payload. For commands always ``0x80``.
- +
- + * - |TC|
- + - |u8|
- + - Target category.
- +
- + * - |TID| (out)
- + - |u8|
- + - Target ID for outgoing (host to EC) commands.
- +
- + * - |TID| (in)
- + - |u8|
- + - Target ID for incoming (EC to host) commands.
- +
- + * - |IID|
- + - |u8|
- + - Instance ID.
- +
- + * - |RQID|
- + - |u16|
- + - Request ID.
- +
- + * - |CID|
- + - |u8|
- + - Command ID.
- +
- +The command struct and data, in general, does not contain any failure
- +detection mechanism (e.g. CRCs), this is solely done on the frame level.
- +
- +Command-type payloads are used by the host to send commands and requests to
- +the EC as well as by the EC to send responses and events back to the host.
- +We differentiate between requests (sent by the host), responses (sent by the
- +EC in response to a request), and events (sent by the EC without a preceding
- +request).
- +
- +Commands and events are uniquely identified by their target category
- +(``TC``) and command ID (``CID``). The target category specifies a general
- +category for the command (e.g. system in general, vs. battery and AC, vs.
- +temperature, and so on), while the command ID specifies the command inside
- +that category. Only the combination of |TC| + |CID| is unique. Additionally,
- +commands have an instance ID (``IID``), which is used to differentiate
- +between different sub-devices. For example ``TC=3`` ``CID=1`` is a
- +request to get the temperature on a thermal sensor, where |IID| specifies
- +the respective sensor. If the instance ID is not used, it should be set to
- +zero. If instance IDs are used, they, in general, start with a value of one,
- +whereas zero may be used for instance independent queries, if applicable. A
- +response to a request should have the same target category, command ID, and
- +instance ID as the corresponding request.
- +
- +Responses are matched to their corresponding request via the request ID
- +(``RQID``) field. This is a 16 bit wrapping counter similar to the sequence
- +ID on the frames. Note that the sequence ID of the frames for a
- +request-response pair does not match. Only the request ID has to match.
- +Frame-protocol wise these are two separate exchanges, and may even be
- +separated, e.g. by an event being sent after the request but before the
- +response. Not all commands produce a response, and this is not detectable by
- +|TC| + |CID|. It is the responsibility of the issuing party to wait for a
- +response (or signal this to the communication framework, as is done in
- +SAN/ACPI via the ``SNC`` flag).
- +
- +Events are identified by unique and reserved request IDs. These IDs should
- +not be used by the host when sending a new request. They are used on the
- +host to, first, detect events and, second, match them with a registered
- +event handler. Request IDs for events are chosen by the host and directed to
- +the EC when setting up and enabling an event source (via the
- +enable-event-source request). The EC then uses the specified request ID for
- +events sent from the respective source. Note that an event should still be
- +identified by its target category, command ID, and, if applicable, instance
- +ID, as a single event source can send multiple different event types. In
- +general, however, a single target category should map to a single reserved
- +event request ID.
- +
- +Furthermore, requests, responses, and events have an associated target ID
- +(``TID``). This target ID is split into output (host to EC) and input (EC to
- +host) fields, with the respecting other field (e.g. output field on incoming
- +messages) set to zero. Two ``TID`` values are known: Primary (``0x01``) and
- +secondary (``0x02``). In general, the response to a request should have the
- +same ``TID`` value, however, the field (output vs. input) should be used in
- +accordance to the direction in which the response is sent (i.e. on the input
- +field, as responses are generally sent from the EC to the host).
- +
- +Note that, even though requests and events should be uniquely identifiable
- +by target category and command ID alone, the EC may require specific
- +target ID and instance ID values to accept a command. A command that is
- +accepted for ``TID=1``, for example, may not be accepted for ``TID=2``
- +and vice versa.
- +
- +
- +Limitations and Observations
- +============================
- +
- +The protocol can, in theory, handle up to ``U8_MAX`` frames in parallel,
- +with up to ``U16_MAX`` pending requests (neglecting request IDs reserved for
- +events). In practice, however, this is more limited. From our testing
- +(although via a python and thus a user-space program), it seems that the EC
- +can handle up to four requests (mostly) reliably in parallel at a certain
- +time. With five or more requests in parallel, consistent discarding of
- +commands (ACKed frame but no command response) has been observed. For five
- +simultaneous commands, this reproducibly resulted in one command being
- +dropped and four commands being handled.
- +
- +However, it has also been noted that, even with three requests in parallel,
- +occasional frame drops happen. Apart from this, with a limit of three
- +pending requests, no dropped commands (i.e. command being dropped but frame
- +carrying command being ACKed) have been observed. In any case, frames (and
- +possibly also commands) should be re-sent by the host if a certain timeout
- +is exceeded. This is done by the EC for frames with a timeout of one second,
- +up to two re-tries (i.e. three transmissions in total). The limit of
- +re-tries also applies to received NAKs, and, in a worst case scenario, can
- +lead to entire messages being dropped.
- +
- +While this also seems to work fine for pending data frames as long as no
- +transmission failures occur, implementation and handling of these seems to
- +depend on the assumption that there is only one non-acknowledged data frame.
- +In particular, the detection of repeated frames relies on the last sequence
- +number. This means that, if a frame that has been successfully received by
- +the EC is sent again, e.g. due to the host not receiving an |ACK|, the EC
- +will only detect this if it has the sequence ID of the last frame received
- +by the EC. As an example: Sending two frames with ``SEQ=0`` and ``SEQ=1``
- +followed by a repetition of ``SEQ=0`` will not detect the second ``SEQ=0``
- +frame as such, and thus execute the command in this frame each time it has
- +been received, i.e. twice in this example. Sending ``SEQ=0``, ``SEQ=1`` and
- +then repeating ``SEQ=1`` will detect the second ``SEQ=1`` as repetition of
- +the first one and ignore it, thus executing the contained command only once.
- +
- +In conclusion, this suggests a limit of at most one pending un-ACKed frame
- +(per party, effectively leading to synchronous communication regarding
- +frames) and at most three pending commands. The limit to synchronous frame
- +transfers seems to be consistent with behavior observed on Windows.
- diff --git a/MAINTAINERS b/MAINTAINERS
- index 530792c869c4..8e6fe82c1072 100644
- --- a/MAINTAINERS
- +++ b/MAINTAINERS
- @@ -11811,6 +11811,7 @@ M: Maximilian Luz <luzmaximilian@gmail.com>
- S: Maintained
- W: https://github.com/linux-surface/surface-aggregator-module
- C: irc://chat.freenode.net/##linux-surface
- +F: Documentation/driver-api/surface_aggregator/
- F: drivers/platform/surface/aggregator/
- F: include/linux/surface_aggregator/
-
- --
- 2.31.0
- From 461efa8828105b740589ec8673adfe78a403945c Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Mon, 21 Dec 2020 19:39:58 +0100
- Subject: [PATCH] platform/surface: Add Surface Aggregator user-space interface
- Add a misc-device providing user-space access to the Surface Aggregator
- EC, mainly intended for debugging, testing, and reverse-engineering.
- This interface gives user-space applications the ability to send
- requests to the EC and receive the corresponding responses.
- The device-file is managed by a pseudo platform-device and corresponding
- driver to avoid dependence on the dedicated bus, allowing it to be
- loaded in a minimal configuration.
- A python library and scripts to access this device can be found at [1].
- [1]: https://github.com/linux-surface/surface-aggregator-module/tree/master/scripts/ssam
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Link: https://lore.kernel.org/r/20201221183959.1186143-9-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- .../surface_aggregator/clients/cdev.rst | 87 +++++
- .../surface_aggregator/clients/index.rst | 12 +-
- .../userspace-api/ioctl/ioctl-number.rst | 2 +
- MAINTAINERS | 2 +
- drivers/platform/surface/Kconfig | 17 +
- drivers/platform/surface/Makefile | 1 +
- .../surface/surface_aggregator_cdev.c | 303 ++++++++++++++++++
- include/uapi/linux/surface_aggregator/cdev.h | 78 +++++
- 8 files changed, 501 insertions(+), 1 deletion(-)
- create mode 100644 Documentation/driver-api/surface_aggregator/clients/cdev.rst
- create mode 100644 drivers/platform/surface/surface_aggregator_cdev.c
- create mode 100644 include/uapi/linux/surface_aggregator/cdev.h
- diff --git a/Documentation/driver-api/surface_aggregator/clients/cdev.rst b/Documentation/driver-api/surface_aggregator/clients/cdev.rst
- new file mode 100644
- index 000000000000..248c1372d879
- --- /dev/null
- +++ b/Documentation/driver-api/surface_aggregator/clients/cdev.rst
- @@ -0,0 +1,87 @@
- +.. SPDX-License-Identifier: GPL-2.0+
- +
- +.. |u8| replace:: :c:type:`u8 <u8>`
- +.. |u16| replace:: :c:type:`u16 <u16>`
- +.. |ssam_cdev_request| replace:: :c:type:`struct ssam_cdev_request <ssam_cdev_request>`
- +.. |ssam_cdev_request_flags| replace:: :c:type:`enum ssam_cdev_request_flags <ssam_cdev_request_flags>`
- +
- +==============================
- +User-Space EC Interface (cdev)
- +==============================
- +
- +The ``surface_aggregator_cdev`` module provides a misc-device for the SSAM
- +controller to allow for a (more or less) direct connection from user-space to
- +the SAM EC. It is intended to be used for development and debugging, and
- +therefore should not be used or relied upon in any other way. Note that this
- +module is not loaded automatically, but instead must be loaded manually.
- +
- +The provided interface is accessible through the ``/dev/surface/aggregator``
- +device-file. All functionality of this interface is provided via IOCTLs.
- +These IOCTLs and their respective input/output parameter structs are defined in
- +``include/uapi/linux/surface_aggregator/cdev.h``.
- +
- +A small python library and scripts for accessing this interface can be found
- +at https://github.com/linux-surface/surface-aggregator-module/tree/master/scripts/ssam.
- +
- +
- +Controller IOCTLs
- +=================
- +
- +The following IOCTLs are provided:
- +
- +.. flat-table:: Controller IOCTLs
- + :widths: 1 1 1 1 4
- + :header-rows: 1
- +
- + * - Type
- + - Number
- + - Direction
- + - Name
- + - Description
- +
- + * - ``0xA5``
- + - ``1``
- + - ``WR``
- + - ``REQUEST``
- + - Perform synchronous SAM request.
- +
- +
- +``REQUEST``
- +-----------
- +
- +Defined as ``_IOWR(0xA5, 1, struct ssam_cdev_request)``.
- +
- +Executes a synchronous SAM request. The request specification is passed in
- +as argument of type |ssam_cdev_request|, which is then written to/modified
- +by the IOCTL to return status and result of the request.
- +
- +Request payload data must be allocated separately and is passed in via the
- +``payload.data`` and ``payload.length`` members. If a response is required,
- +the response buffer must be allocated by the caller and passed in via the
- +``response.data`` member. The ``response.length`` member must be set to the
- +capacity of this buffer, or if no response is required, zero. Upon
- +completion of the request, the call will write the response to the response
- +buffer (if its capacity allows it) and overwrite the length field with the
- +actual size of the response, in bytes.
- +
- +Additionally, if the request has a response, this must be indicated via the
- +request flags, as is done with in-kernel requests. Request flags can be set
- +via the ``flags`` member and the values correspond to the values found in
- +|ssam_cdev_request_flags|.
- +
- +Finally, the status of the request itself is returned in the ``status``
- +member (a negative errno value indicating failure). Note that failure
- +indication of the IOCTL is separated from failure indication of the request:
- +The IOCTL returns a negative status code if anything failed during setup of
- +the request (``-EFAULT``) or if the provided argument or any of its fields
- +are invalid (``-EINVAL``). In this case, the status value of the request
- +argument may be set, providing more detail on what went wrong (e.g.
- +``-ENOMEM`` for out-of-memory), but this value may also be zero. The IOCTL
- +will return with a zero status code in case the request has been set up,
- +submitted, and completed (i.e. handed back to user-space) successfully from
- +inside the IOCTL, but the request ``status`` member may still be negative in
- +case the actual execution of the request failed after it has been submitted.
- +
- +A full definition of the argument struct is provided below:
- +
- +.. kernel-doc:: include/uapi/linux/surface_aggregator/cdev.h
- diff --git a/Documentation/driver-api/surface_aggregator/clients/index.rst b/Documentation/driver-api/surface_aggregator/clients/index.rst
- index 31e026d96102..ab260ec82cfb 100644
- --- a/Documentation/driver-api/surface_aggregator/clients/index.rst
- +++ b/Documentation/driver-api/surface_aggregator/clients/index.rst
- @@ -7,4 +7,14 @@ Client Driver Documentation
- This is the documentation for client drivers themselves. Refer to
- :doc:`../client` for documentation on how to write client drivers.
-
- -.. Place documentation for individual client drivers here.
- +.. toctree::
- + :maxdepth: 1
- +
- + cdev
- +
- +.. only:: subproject and html
- +
- + Indices
- + =======
- +
- + * :ref:`genindex`
- diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst
- index a4c75a28c839..b5231d7f9200 100644
- --- a/Documentation/userspace-api/ioctl/ioctl-number.rst
- +++ b/Documentation/userspace-api/ioctl/ioctl-number.rst
- @@ -324,6 +324,8 @@ Code Seq# Include File Comments
- 0xA3 90-9F linux/dtlk.h
- 0xA4 00-1F uapi/linux/tee.h Generic TEE subsystem
- 0xA4 00-1F uapi/asm/sgx.h <mailto:linux-sgx@vger.kernel.org>
- +0xA5 01 linux/surface_aggregator/cdev.h Microsoft Surface Platform System Aggregator
- + <mailto:luzmaximilian@gmail.com>
- 0xAA 00-3F linux/uapi/linux/userfaultfd.h
- 0xAB 00-1F linux/nbd.h
- 0xAC 00-1F linux/raw.h
- diff --git a/MAINTAINERS b/MAINTAINERS
- index 8e6fe82c1072..b45df8ec687f 100644
- --- a/MAINTAINERS
- +++ b/MAINTAINERS
- @@ -11813,7 +11813,9 @@ W: https://github.com/linux-surface/surface-aggregator-module
- C: irc://chat.freenode.net/##linux-surface
- F: Documentation/driver-api/surface_aggregator/
- F: drivers/platform/surface/aggregator/
- +F: drivers/platform/surface/surface_aggregator_cdev.c
- F: include/linux/surface_aggregator/
- +F: include/uapi/linux/surface_aggregator/
-
- MICROTEK X6 SCANNER
- M: Oliver Neukum <oliver@neukum.org>
- diff --git a/drivers/platform/surface/Kconfig b/drivers/platform/surface/Kconfig
- index ef6b4051e7c8..82fbcfedc6dc 100644
- --- a/drivers/platform/surface/Kconfig
- +++ b/drivers/platform/surface/Kconfig
- @@ -41,6 +41,23 @@ config SURFACE_3_POWER_OPREGION
- This driver provides support for ACPI operation
- region of the Surface 3 battery platform driver.
-
- +config SURFACE_AGGREGATOR_CDEV
- + tristate "Surface System Aggregator Module User-Space Interface"
- + depends on SURFACE_AGGREGATOR
- + help
- + Provides a misc-device interface to the Surface System Aggregator
- + Module (SSAM) controller.
- +
- + This option provides a module (called surface_aggregator_cdev), that,
- + when loaded, will add a client device (and its respective driver) to
- + the SSAM controller. Said client device manages a misc-device
- + interface (/dev/surface/aggregator), which can be used by user-space
- + tools to directly communicate with the SSAM EC by sending requests and
- + receiving the corresponding responses.
- +
- + The provided interface is intended for debugging and development only,
- + and should not be used otherwise.
- +
- config SURFACE_BOOK1_DGPU_SWITCH
- tristate "Surface Book 1 dGPU Switch Driver"
- depends on SYSFS
- diff --git a/drivers/platform/surface/Makefile b/drivers/platform/surface/Makefile
- index c5392098cfb9..644c7511f64d 100644
- --- a/drivers/platform/surface/Makefile
- +++ b/drivers/platform/surface/Makefile
- @@ -8,6 +8,7 @@ obj-$(CONFIG_SURFACE3_WMI) += surface3-wmi.o
- obj-$(CONFIG_SURFACE_3_BUTTON) += surface3_button.o
- obj-$(CONFIG_SURFACE_3_POWER_OPREGION) += surface3_power.o
- obj-$(CONFIG_SURFACE_AGGREGATOR) += aggregator/
- +obj-$(CONFIG_SURFACE_AGGREGATOR_CDEV) += surface_aggregator_cdev.o
- obj-$(CONFIG_SURFACE_BOOK1_DGPU_SWITCH) += surfacebook1_dgpu_switch.o
- obj-$(CONFIG_SURFACE_GPE) += surface_gpe.o
- obj-$(CONFIG_SURFACE_PRO3_BUTTON) += surfacepro3_button.o
- diff --git a/drivers/platform/surface/surface_aggregator_cdev.c b/drivers/platform/surface/surface_aggregator_cdev.c
- new file mode 100644
- index 000000000000..340d15b148b9
- --- /dev/null
- +++ b/drivers/platform/surface/surface_aggregator_cdev.c
- @@ -0,0 +1,303 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * Provides user-space access to the SSAM EC via the /dev/surface/aggregator
- + * misc device. Intended for debugging and development.
- + *
- + * Copyright (C) 2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <linux/fs.h>
- +#include <linux/kernel.h>
- +#include <linux/kref.h>
- +#include <linux/miscdevice.h>
- +#include <linux/module.h>
- +#include <linux/platform_device.h>
- +#include <linux/rwsem.h>
- +#include <linux/slab.h>
- +#include <linux/uaccess.h>
- +
- +#include <linux/surface_aggregator/cdev.h>
- +#include <linux/surface_aggregator/controller.h>
- +
- +#define SSAM_CDEV_DEVICE_NAME "surface_aggregator_cdev"
- +
- +struct ssam_cdev {
- + struct kref kref;
- + struct rw_semaphore lock;
- + struct ssam_controller *ctrl;
- + struct miscdevice mdev;
- +};
- +
- +static void __ssam_cdev_release(struct kref *kref)
- +{
- + kfree(container_of(kref, struct ssam_cdev, kref));
- +}
- +
- +static struct ssam_cdev *ssam_cdev_get(struct ssam_cdev *cdev)
- +{
- + if (cdev)
- + kref_get(&cdev->kref);
- +
- + return cdev;
- +}
- +
- +static void ssam_cdev_put(struct ssam_cdev *cdev)
- +{
- + if (cdev)
- + kref_put(&cdev->kref, __ssam_cdev_release);
- +}
- +
- +static int ssam_cdev_device_open(struct inode *inode, struct file *filp)
- +{
- + struct miscdevice *mdev = filp->private_data;
- + struct ssam_cdev *cdev = container_of(mdev, struct ssam_cdev, mdev);
- +
- + filp->private_data = ssam_cdev_get(cdev);
- + return stream_open(inode, filp);
- +}
- +
- +static int ssam_cdev_device_release(struct inode *inode, struct file *filp)
- +{
- + ssam_cdev_put(filp->private_data);
- + return 0;
- +}
- +
- +static long ssam_cdev_request(struct ssam_cdev *cdev, unsigned long arg)
- +{
- + struct ssam_cdev_request __user *r;
- + struct ssam_cdev_request rqst;
- + struct ssam_request spec;
- + struct ssam_response rsp;
- + const void __user *plddata;
- + void __user *rspdata;
- + int status = 0, ret = 0, tmp;
- +
- + r = (struct ssam_cdev_request __user *)arg;
- + ret = copy_struct_from_user(&rqst, sizeof(rqst), r, sizeof(*r));
- + if (ret)
- + goto out;
- +
- + plddata = u64_to_user_ptr(rqst.payload.data);
- + rspdata = u64_to_user_ptr(rqst.response.data);
- +
- + /* Setup basic request fields. */
- + spec.target_category = rqst.target_category;
- + spec.target_id = rqst.target_id;
- + spec.command_id = rqst.command_id;
- + spec.instance_id = rqst.instance_id;
- + spec.flags = 0;
- + spec.length = rqst.payload.length;
- + spec.payload = NULL;
- +
- + if (rqst.flags & SSAM_CDEV_REQUEST_HAS_RESPONSE)
- + spec.flags |= SSAM_REQUEST_HAS_RESPONSE;
- +
- + if (rqst.flags & SSAM_CDEV_REQUEST_UNSEQUENCED)
- + spec.flags |= SSAM_REQUEST_UNSEQUENCED;
- +
- + rsp.capacity = rqst.response.length;
- + rsp.length = 0;
- + rsp.pointer = NULL;
- +
- + /* Get request payload from user-space. */
- + if (spec.length) {
- + if (!plddata) {
- + ret = -EINVAL;
- + goto out;
- + }
- +
- + spec.payload = kzalloc(spec.length, GFP_KERNEL);
- + if (!spec.payload) {
- + ret = -ENOMEM;
- + goto out;
- + }
- +
- + if (copy_from_user((void *)spec.payload, plddata, spec.length)) {
- + ret = -EFAULT;
- + goto out;
- + }
- + }
- +
- + /* Allocate response buffer. */
- + if (rsp.capacity) {
- + if (!rspdata) {
- + ret = -EINVAL;
- + goto out;
- + }
- +
- + rsp.pointer = kzalloc(rsp.capacity, GFP_KERNEL);
- + if (!rsp.pointer) {
- + ret = -ENOMEM;
- + goto out;
- + }
- + }
- +
- + /* Perform request. */
- + status = ssam_request_sync(cdev->ctrl, &spec, &rsp);
- + if (status)
- + goto out;
- +
- + /* Copy response to user-space. */
- + if (rsp.length && copy_to_user(rspdata, rsp.pointer, rsp.length))
- + ret = -EFAULT;
- +
- +out:
- + /* Always try to set response-length and status. */
- + tmp = put_user(rsp.length, &r->response.length);
- + if (tmp)
- + ret = tmp;
- +
- + tmp = put_user(status, &r->status);
- + if (tmp)
- + ret = tmp;
- +
- + /* Cleanup. */
- + kfree(spec.payload);
- + kfree(rsp.pointer);
- +
- + return ret;
- +}
- +
- +static long __ssam_cdev_device_ioctl(struct ssam_cdev *cdev, unsigned int cmd,
- + unsigned long arg)
- +{
- + switch (cmd) {
- + case SSAM_CDEV_REQUEST:
- + return ssam_cdev_request(cdev, arg);
- +
- + default:
- + return -ENOTTY;
- + }
- +}
- +
- +static long ssam_cdev_device_ioctl(struct file *file, unsigned int cmd,
- + unsigned long arg)
- +{
- + struct ssam_cdev *cdev = file->private_data;
- + long status;
- +
- + /* Ensure that controller is valid for as long as we need it. */
- + if (down_read_killable(&cdev->lock))
- + return -ERESTARTSYS;
- +
- + if (!cdev->ctrl) {
- + up_read(&cdev->lock);
- + return -ENODEV;
- + }
- +
- + status = __ssam_cdev_device_ioctl(cdev, cmd, arg);
- +
- + up_read(&cdev->lock);
- + return status;
- +}
- +
- +static const struct file_operations ssam_controller_fops = {
- + .owner = THIS_MODULE,
- + .open = ssam_cdev_device_open,
- + .release = ssam_cdev_device_release,
- + .unlocked_ioctl = ssam_cdev_device_ioctl,
- + .compat_ioctl = ssam_cdev_device_ioctl,
- + .llseek = noop_llseek,
- +};
- +
- +static int ssam_dbg_device_probe(struct platform_device *pdev)
- +{
- + struct ssam_controller *ctrl;
- + struct ssam_cdev *cdev;
- + int status;
- +
- + ctrl = ssam_client_bind(&pdev->dev);
- + if (IS_ERR(ctrl))
- + return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
- +
- + cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
- + if (!cdev)
- + return -ENOMEM;
- +
- + kref_init(&cdev->kref);
- + init_rwsem(&cdev->lock);
- + cdev->ctrl = ctrl;
- +
- + cdev->mdev.parent = &pdev->dev;
- + cdev->mdev.minor = MISC_DYNAMIC_MINOR;
- + cdev->mdev.name = "surface_aggregator";
- + cdev->mdev.nodename = "surface/aggregator";
- + cdev->mdev.fops = &ssam_controller_fops;
- +
- + status = misc_register(&cdev->mdev);
- + if (status) {
- + kfree(cdev);
- + return status;
- + }
- +
- + platform_set_drvdata(pdev, cdev);
- + return 0;
- +}
- +
- +static int ssam_dbg_device_remove(struct platform_device *pdev)
- +{
- + struct ssam_cdev *cdev = platform_get_drvdata(pdev);
- +
- + misc_deregister(&cdev->mdev);
- +
- + /*
- + * The controller is only guaranteed to be valid for as long as the
- + * driver is bound. Remove controller so that any lingering open files
- + * cannot access it any more after we're gone.
- + */
- + down_write(&cdev->lock);
- + cdev->ctrl = NULL;
- + up_write(&cdev->lock);
- +
- + ssam_cdev_put(cdev);
- + return 0;
- +}
- +
- +static struct platform_device *ssam_cdev_device;
- +
- +static struct platform_driver ssam_cdev_driver = {
- + .probe = ssam_dbg_device_probe,
- + .remove = ssam_dbg_device_remove,
- + .driver = {
- + .name = SSAM_CDEV_DEVICE_NAME,
- + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- + },
- +};
- +
- +static int __init ssam_debug_init(void)
- +{
- + int status;
- +
- + ssam_cdev_device = platform_device_alloc(SSAM_CDEV_DEVICE_NAME,
- + PLATFORM_DEVID_NONE);
- + if (!ssam_cdev_device)
- + return -ENOMEM;
- +
- + status = platform_device_add(ssam_cdev_device);
- + if (status)
- + goto err_device;
- +
- + status = platform_driver_register(&ssam_cdev_driver);
- + if (status)
- + goto err_driver;
- +
- + return 0;
- +
- +err_driver:
- + platform_device_del(ssam_cdev_device);
- +err_device:
- + platform_device_put(ssam_cdev_device);
- + return status;
- +}
- +module_init(ssam_debug_init);
- +
- +static void __exit ssam_debug_exit(void)
- +{
- + platform_driver_unregister(&ssam_cdev_driver);
- + platform_device_unregister(ssam_cdev_device);
- +}
- +module_exit(ssam_debug_exit);
- +
- +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- +MODULE_DESCRIPTION("User-space interface for Surface System Aggregator Module");
- +MODULE_LICENSE("GPL");
- diff --git a/include/uapi/linux/surface_aggregator/cdev.h b/include/uapi/linux/surface_aggregator/cdev.h
- new file mode 100644
- index 000000000000..fbcce04abfe9
- --- /dev/null
- +++ b/include/uapi/linux/surface_aggregator/cdev.h
- @@ -0,0 +1,78 @@
- +/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
- +/*
- + * Surface System Aggregator Module (SSAM) user-space EC interface.
- + *
- + * Definitions, structs, and IOCTLs for the /dev/surface/aggregator misc
- + * device. This device provides direct user-space access to the SSAM EC.
- + * Intended for debugging and development.
- + *
- + * Copyright (C) 2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#ifndef _UAPI_LINUX_SURFACE_AGGREGATOR_CDEV_H
- +#define _UAPI_LINUX_SURFACE_AGGREGATOR_CDEV_H
- +
- +#include <linux/ioctl.h>
- +#include <linux/types.h>
- +
- +/**
- + * enum ssam_cdev_request_flags - Request flags for SSAM cdev request IOCTL.
- + *
- + * @SSAM_CDEV_REQUEST_HAS_RESPONSE:
- + * Specifies that the request expects a response. If not set, the request
- + * will be directly completed after its underlying packet has been
- + * transmitted. If set, the request transport system waits for a response
- + * of the request.
- + *
- + * @SSAM_CDEV_REQUEST_UNSEQUENCED:
- + * Specifies that the request should be transmitted via an unsequenced
- + * packet. If set, the request must not have a response, meaning that this
- + * flag and the %SSAM_CDEV_REQUEST_HAS_RESPONSE flag are mutually
- + * exclusive.
- + */
- +enum ssam_cdev_request_flags {
- + SSAM_CDEV_REQUEST_HAS_RESPONSE = 0x01,
- + SSAM_CDEV_REQUEST_UNSEQUENCED = 0x02,
- +};
- +
- +/**
- + * struct ssam_cdev_request - Controller request IOCTL argument.
- + * @target_category: Target category of the SAM request.
- + * @target_id: Target ID of the SAM request.
- + * @command_id: Command ID of the SAM request.
- + * @instance_id: Instance ID of the SAM request.
- + * @flags: Request flags (see &enum ssam_cdev_request_flags).
- + * @status: Request status (output).
- + * @payload: Request payload (input data).
- + * @payload.data: Pointer to request payload data.
- + * @payload.length: Length of request payload data (in bytes).
- + * @response: Request response (output data).
- + * @response.data: Pointer to response buffer.
- + * @response.length: On input: Capacity of response buffer (in bytes).
- + * On output: Length of request response (number of bytes
- + * in the buffer that are actually used).
- + */
- +struct ssam_cdev_request {
- + __u8 target_category;
- + __u8 target_id;
- + __u8 command_id;
- + __u8 instance_id;
- + __u16 flags;
- + __s16 status;
- +
- + struct {
- + __u64 data;
- + __u16 length;
- + __u8 __pad[6];
- + } payload;
- +
- + struct {
- + __u64 data;
- + __u16 length;
- + __u8 __pad[6];
- + } response;
- +} __attribute__((__packed__));
- +
- +#define SSAM_CDEV_REQUEST _IOWR(0xA5, 1, struct ssam_cdev_request)
- +
- +#endif /* _UAPI_LINUX_SURFACE_AGGREGATOR_CDEV_H */
- --
- 2.31.0
- From 3a1ade8f72b937629858465ebc82c0e4673bb751 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Mon, 21 Dec 2020 19:39:59 +0100
- Subject: [PATCH] platform/surface: Add Surface ACPI Notify driver
- The Surface ACPI Notify (SAN) device provides an ACPI interface to the
- Surface Aggregator EC, specifically the Surface Serial Hub interface.
- This interface allows EC requests to be made from ACPI code and can
- convert a subset of EC events back to ACPI notifications.
- Specifically, this interface provides a GenericSerialBus operation
- region ACPI code can execute a request by writing the request command
- data and payload to this operation region and reading back the
- corresponding response via a write-then-read operation. Furthermore,
- this interface provides a _DSM method to be called when certain events
- from the EC have been received, essentially turning them into ACPI
- notifications.
- The driver provided in this commit essentially takes care of translating
- the request data written to the operation region, executing the request,
- waiting for it to finish, and finally writing and translating back the
- response (if the request has one). Furthermore, this driver takes care
- of enabling the events handled via ACPI _DSM calls. Lastly, this driver
- also exposes an interface providing discrete GPU (dGPU) power-on
- notifications on the Surface Book 2, which are also received via the
- operation region interface (but not handled by the SAN driver directly),
- making them accessible to other drivers (such as a dGPU hot-plug driver
- that may be added later on).
- On 5th and 6th generation Surface devices (Surface Pro 5/2017, Pro 6,
- Book 2, Laptop 1 and 2), the SAN interface provides full battery and
- thermal subsystem access, as well as other EC based functionality. On
- those models, battery and thermal sensor devices are implemented as
- standard ACPI devices of that type, however, forward ACPI calls to the
- corresponding Surface Aggregator EC request via the SAN interface and
- receive corresponding notifications (e.g. battery information change)
- from it. This interface is therefore required to provide said
- functionality on those devices.
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Reviewed-by: Hans de Goede <hdegoede@redhat.com>
- Link: https://lore.kernel.org/r/20201221183959.1186143-10-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- .../surface_aggregator/clients/index.rst | 1 +
- .../surface_aggregator/clients/san.rst | 44 +
- MAINTAINERS | 2 +
- drivers/platform/surface/Kconfig | 19 +
- drivers/platform/surface/Makefile | 1 +
- .../platform/surface/surface_acpi_notify.c | 886 ++++++++++++++++++
- include/linux/surface_acpi_notify.h | 39 +
- 7 files changed, 992 insertions(+)
- create mode 100644 Documentation/driver-api/surface_aggregator/clients/san.rst
- create mode 100644 drivers/platform/surface/surface_acpi_notify.c
- create mode 100644 include/linux/surface_acpi_notify.h
- diff --git a/Documentation/driver-api/surface_aggregator/clients/index.rst b/Documentation/driver-api/surface_aggregator/clients/index.rst
- index ab260ec82cfb..3ccabce23271 100644
- --- a/Documentation/driver-api/surface_aggregator/clients/index.rst
- +++ b/Documentation/driver-api/surface_aggregator/clients/index.rst
- @@ -11,6 +11,7 @@ This is the documentation for client drivers themselves. Refer to
- :maxdepth: 1
-
- cdev
- + san
-
- .. only:: subproject and html
-
- diff --git a/Documentation/driver-api/surface_aggregator/clients/san.rst b/Documentation/driver-api/surface_aggregator/clients/san.rst
- new file mode 100644
- index 000000000000..38c2580e7758
- --- /dev/null
- +++ b/Documentation/driver-api/surface_aggregator/clients/san.rst
- @@ -0,0 +1,44 @@
- +.. SPDX-License-Identifier: GPL-2.0+
- +
- +.. |san_client_link| replace:: :c:func:`san_client_link`
- +.. |san_dgpu_notifier_register| replace:: :c:func:`san_dgpu_notifier_register`
- +.. |san_dgpu_notifier_unregister| replace:: :c:func:`san_dgpu_notifier_unregister`
- +
- +===================
- +Surface ACPI Notify
- +===================
- +
- +The Surface ACPI Notify (SAN) device provides the bridge between ACPI and
- +SAM controller. Specifically, ACPI code can execute requests and handle
- +battery and thermal events via this interface. In addition to this, events
- +relating to the discrete GPU (dGPU) of the Surface Book 2 can be sent from
- +ACPI code (note: the Surface Book 3 uses a different method for this). The
- +only currently known event sent via this interface is a dGPU power-on
- +notification. While this driver handles the former part internally, it only
- +relays the dGPU events to any other driver interested via its public API and
- +does not handle them.
- +
- +The public interface of this driver is split into two parts: Client
- +registration and notifier-block registration.
- +
- +A client to the SAN interface can be linked as consumer to the SAN device
- +via |san_client_link|. This can be used to ensure that the a client
- +receiving dGPU events does not miss any events due to the SAN interface not
- +being set up as this forces the client driver to unbind once the SAN driver
- +is unbound.
- +
- +Notifier-blocks can be registered by any device for as long as the module is
- +loaded, regardless of being linked as client or not. Registration is done
- +with |san_dgpu_notifier_register|. If the notifier is not needed any more, it
- +should be unregistered via |san_dgpu_notifier_unregister|.
- +
- +Consult the API documentation below for more details.
- +
- +
- +API Documentation
- +=================
- +
- +.. kernel-doc:: include/linux/surface_acpi_notify.h
- +
- +.. kernel-doc:: drivers/platform/surface/surface_acpi_notify.c
- + :export:
- diff --git a/MAINTAINERS b/MAINTAINERS
- index b45df8ec687f..dfe4f4e1da7a 100644
- --- a/MAINTAINERS
- +++ b/MAINTAINERS
- @@ -11813,7 +11813,9 @@ W: https://github.com/linux-surface/surface-aggregator-module
- C: irc://chat.freenode.net/##linux-surface
- F: Documentation/driver-api/surface_aggregator/
- F: drivers/platform/surface/aggregator/
- +F: drivers/platform/surface/surface_acpi_notify.c
- F: drivers/platform/surface/surface_aggregator_cdev.c
- +F: include/linux/surface_acpi_notify.h
- F: include/linux/surface_aggregator/
- F: include/uapi/linux/surface_aggregator/
-
- diff --git a/drivers/platform/surface/Kconfig b/drivers/platform/surface/Kconfig
- index 82fbcfedc6dc..b0b91fa2f6a1 100644
- --- a/drivers/platform/surface/Kconfig
- +++ b/drivers/platform/surface/Kconfig
- @@ -41,6 +41,25 @@ config SURFACE_3_POWER_OPREGION
- This driver provides support for ACPI operation
- region of the Surface 3 battery platform driver.
-
- +config SURFACE_ACPI_NOTIFY
- + tristate "Surface ACPI Notify Driver"
- + depends on SURFACE_AGGREGATOR
- + help
- + Surface ACPI Notify (SAN) driver for Microsoft Surface devices.
- +
- + This driver provides support for the ACPI interface (called SAN) of
- + the Surface System Aggregator Module (SSAM) EC. This interface is used
- + on 5th- and 6th-generation Microsoft Surface devices (including
- + Surface Pro 5 and 6, Surface Book 2, Surface Laptops 1 and 2, and in
- + reduced functionality on the Surface Laptop 3) to execute SSAM
- + requests directly from ACPI code, as well as receive SSAM events and
- + turn them into ACPI notifications. It essentially acts as a
- + translation layer between the SSAM controller and ACPI.
- +
- + Specifically, this driver may be needed for battery status reporting,
- + thermal sensor access, and real-time clock information, depending on
- + the Surface device in question.
- +
- config SURFACE_AGGREGATOR_CDEV
- tristate "Surface System Aggregator Module User-Space Interface"
- depends on SURFACE_AGGREGATOR
- diff --git a/drivers/platform/surface/Makefile b/drivers/platform/surface/Makefile
- index 644c7511f64d..72f4d9fbb6be 100644
- --- a/drivers/platform/surface/Makefile
- +++ b/drivers/platform/surface/Makefile
- @@ -7,6 +7,7 @@
- obj-$(CONFIG_SURFACE3_WMI) += surface3-wmi.o
- obj-$(CONFIG_SURFACE_3_BUTTON) += surface3_button.o
- obj-$(CONFIG_SURFACE_3_POWER_OPREGION) += surface3_power.o
- +obj-$(CONFIG_SURFACE_ACPI_NOTIFY) += surface_acpi_notify.o
- obj-$(CONFIG_SURFACE_AGGREGATOR) += aggregator/
- obj-$(CONFIG_SURFACE_AGGREGATOR_CDEV) += surface_aggregator_cdev.o
- obj-$(CONFIG_SURFACE_BOOK1_DGPU_SWITCH) += surfacebook1_dgpu_switch.o
- diff --git a/drivers/platform/surface/surface_acpi_notify.c b/drivers/platform/surface/surface_acpi_notify.c
- new file mode 100644
- index 000000000000..8cd67a669c86
- --- /dev/null
- +++ b/drivers/platform/surface/surface_acpi_notify.c
- @@ -0,0 +1,886 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * Driver for the Surface ACPI Notify (SAN) interface/shim.
- + *
- + * Translates communication from ACPI to Surface System Aggregator Module
- + * (SSAM/SAM) requests and back, specifically SAM-over-SSH. Translates SSAM
- + * events back to ACPI notifications. Allows handling of discrete GPU
- + * notifications sent from ACPI via the SAN interface by providing them to any
- + * registered external driver.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <asm/unaligned.h>
- +#include <linux/acpi.h>
- +#include <linux/delay.h>
- +#include <linux/jiffies.h>
- +#include <linux/kernel.h>
- +#include <linux/module.h>
- +#include <linux/notifier.h>
- +#include <linux/platform_device.h>
- +#include <linux/rwsem.h>
- +
- +#include <linux/surface_aggregator/controller.h>
- +#include <linux/surface_acpi_notify.h>
- +
- +struct san_data {
- + struct device *dev;
- + struct ssam_controller *ctrl;
- +
- + struct acpi_connection_info info;
- +
- + struct ssam_event_notifier nf_bat;
- + struct ssam_event_notifier nf_tmp;
- +};
- +
- +#define to_san_data(ptr, member) \
- + container_of(ptr, struct san_data, member)
- +
- +
- +/* -- dGPU notifier interface. ---------------------------------------------- */
- +
- +struct san_rqsg_if {
- + struct rw_semaphore lock;
- + struct device *dev;
- + struct blocking_notifier_head nh;
- +};
- +
- +static struct san_rqsg_if san_rqsg_if = {
- + .lock = __RWSEM_INITIALIZER(san_rqsg_if.lock),
- + .dev = NULL,
- + .nh = BLOCKING_NOTIFIER_INIT(san_rqsg_if.nh),
- +};
- +
- +static int san_set_rqsg_interface_device(struct device *dev)
- +{
- + int status = 0;
- +
- + down_write(&san_rqsg_if.lock);
- + if (!san_rqsg_if.dev && dev)
- + san_rqsg_if.dev = dev;
- + else
- + status = -EBUSY;
- + up_write(&san_rqsg_if.lock);
- +
- + return status;
- +}
- +
- +/**
- + * san_client_link() - Link client as consumer to SAN device.
- + * @client: The client to link.
- + *
- + * Sets up a device link between the provided client device as consumer and
- + * the SAN device as provider. This function can be used to ensure that the
- + * SAN interface has been set up and will be set up for as long as the driver
- + * of the client device is bound. This guarantees that, during that time, all
- + * dGPU events will be received by any registered notifier.
- + *
- + * The link will be automatically removed once the client device's driver is
- + * unbound.
- + *
- + * Return: Returns zero on success, %-ENXIO if the SAN interface has not been
- + * set up yet, and %-ENOMEM if device link creation failed.
- + */
- +int san_client_link(struct device *client)
- +{
- + const u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;
- + struct device_link *link;
- +
- + down_read(&san_rqsg_if.lock);
- +
- + if (!san_rqsg_if.dev) {
- + up_read(&san_rqsg_if.lock);
- + return -ENXIO;
- + }
- +
- + link = device_link_add(client, san_rqsg_if.dev, flags);
- + if (!link) {
- + up_read(&san_rqsg_if.lock);
- + return -ENOMEM;
- + }
- +
- + if (READ_ONCE(link->status) == DL_STATE_SUPPLIER_UNBIND) {
- + up_read(&san_rqsg_if.lock);
- + return -ENXIO;
- + }
- +
- + up_read(&san_rqsg_if.lock);
- + return 0;
- +}
- +EXPORT_SYMBOL_GPL(san_client_link);
- +
- +/**
- + * san_dgpu_notifier_register() - Register a SAN dGPU notifier.
- + * @nb: The notifier-block to register.
- + *
- + * Registers a SAN dGPU notifier, receiving any new SAN dGPU events sent from
- + * ACPI. The registered notifier will be called with &struct san_dgpu_event
- + * as notifier data and the command ID of that event as notifier action.
- + */
- +int san_dgpu_notifier_register(struct notifier_block *nb)
- +{
- + return blocking_notifier_chain_register(&san_rqsg_if.nh, nb);
- +}
- +EXPORT_SYMBOL_GPL(san_dgpu_notifier_register);
- +
- +/**
- + * san_dgpu_notifier_unregister() - Unregister a SAN dGPU notifier.
- + * @nb: The notifier-block to unregister.
- + */
- +int san_dgpu_notifier_unregister(struct notifier_block *nb)
- +{
- + return blocking_notifier_chain_unregister(&san_rqsg_if.nh, nb);
- +}
- +EXPORT_SYMBOL_GPL(san_dgpu_notifier_unregister);
- +
- +static int san_dgpu_notifier_call(struct san_dgpu_event *evt)
- +{
- + int ret;
- +
- + ret = blocking_notifier_call_chain(&san_rqsg_if.nh, evt->command, evt);
- + return notifier_to_errno(ret);
- +}
- +
- +
- +/* -- ACPI _DSM event relay. ------------------------------------------------ */
- +
- +#define SAN_DSM_REVISION 0
- +
- +/* 93b666c5-70c6-469f-a215-3d487c91ab3c */
- +static const guid_t SAN_DSM_UUID =
- + GUID_INIT(0x93b666c5, 0x70c6, 0x469f, 0xa2, 0x15, 0x3d,
- + 0x48, 0x7c, 0x91, 0xab, 0x3c);
- +
- +enum san_dsm_event_fn {
- + SAN_DSM_EVENT_FN_BAT1_STAT = 0x03,
- + SAN_DSM_EVENT_FN_BAT1_INFO = 0x04,
- + SAN_DSM_EVENT_FN_ADP1_STAT = 0x05,
- + SAN_DSM_EVENT_FN_ADP1_INFO = 0x06,
- + SAN_DSM_EVENT_FN_BAT2_STAT = 0x07,
- + SAN_DSM_EVENT_FN_BAT2_INFO = 0x08,
- + SAN_DSM_EVENT_FN_THERMAL = 0x09,
- + SAN_DSM_EVENT_FN_DPTF = 0x0a,
- +};
- +
- +enum sam_event_cid_bat {
- + SAM_EVENT_CID_BAT_BIX = 0x15,
- + SAM_EVENT_CID_BAT_BST = 0x16,
- + SAM_EVENT_CID_BAT_ADP = 0x17,
- + SAM_EVENT_CID_BAT_PROT = 0x18,
- + SAM_EVENT_CID_BAT_DPTF = 0x4f,
- +};
- +
- +enum sam_event_cid_tmp {
- + SAM_EVENT_CID_TMP_TRIP = 0x0b,
- +};
- +
- +struct san_event_work {
- + struct delayed_work work;
- + struct device *dev;
- + struct ssam_event event; /* must be last */
- +};
- +
- +static int san_acpi_notify_event(struct device *dev, u64 func,
- + union acpi_object *param)
- +{
- + acpi_handle san = ACPI_HANDLE(dev);
- + union acpi_object *obj;
- + int status = 0;
- +
- + if (!acpi_check_dsm(san, &SAN_DSM_UUID, SAN_DSM_REVISION, 1 << func))
- + return 0;
- +
- + dev_dbg(dev, "notify event %#04llx\n", func);
- +
- + obj = acpi_evaluate_dsm_typed(san, &SAN_DSM_UUID, SAN_DSM_REVISION,
- + func, param, ACPI_TYPE_BUFFER);
- + if (!obj)
- + return -EFAULT;
- +
- + if (obj->buffer.length != 1 || obj->buffer.pointer[0] != 0) {
- + dev_err(dev, "got unexpected result from _DSM\n");
- + status = -EPROTO;
- + }
- +
- + ACPI_FREE(obj);
- + return status;
- +}
- +
- +static int san_evt_bat_adp(struct device *dev, const struct ssam_event *event)
- +{
- + int status;
- +
- + status = san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_ADP1_STAT, NULL);
- + if (status)
- + return status;
- +
- + /*
- + * Ensure that the battery states get updated correctly. When the
- + * battery is fully charged and an adapter is plugged in, it sometimes
- + * is not updated correctly, instead showing it as charging.
- + * Explicitly trigger battery updates to fix this.
- + */
- +
- + status = san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_BAT1_STAT, NULL);
- + if (status)
- + return status;
- +
- + return san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_BAT2_STAT, NULL);
- +}
- +
- +static int san_evt_bat_bix(struct device *dev, const struct ssam_event *event)
- +{
- + enum san_dsm_event_fn fn;
- +
- + if (event->instance_id == 0x02)
- + fn = SAN_DSM_EVENT_FN_BAT2_INFO;
- + else
- + fn = SAN_DSM_EVENT_FN_BAT1_INFO;
- +
- + return san_acpi_notify_event(dev, fn, NULL);
- +}
- +
- +static int san_evt_bat_bst(struct device *dev, const struct ssam_event *event)
- +{
- + enum san_dsm_event_fn fn;
- +
- + if (event->instance_id == 0x02)
- + fn = SAN_DSM_EVENT_FN_BAT2_STAT;
- + else
- + fn = SAN_DSM_EVENT_FN_BAT1_STAT;
- +
- + return san_acpi_notify_event(dev, fn, NULL);
- +}
- +
- +static int san_evt_bat_dptf(struct device *dev, const struct ssam_event *event)
- +{
- + union acpi_object payload;
- +
- + /*
- + * The Surface ACPI expects a buffer and not a package. It specifically
- + * checks for ObjectType (Arg3) == 0x03. This will cause a warning in
- + * acpica/nsarguments.c, but that warning can be safely ignored.
- + */
- + payload.type = ACPI_TYPE_BUFFER;
- + payload.buffer.length = event->length;
- + payload.buffer.pointer = (u8 *)&event->data[0];
- +
- + return san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_DPTF, &payload);
- +}
- +
- +static unsigned long san_evt_bat_delay(u8 cid)
- +{
- + switch (cid) {
- + case SAM_EVENT_CID_BAT_ADP:
- + /*
- + * Wait for battery state to update before signaling adapter
- + * change.
- + */
- + return msecs_to_jiffies(5000);
- +
- + case SAM_EVENT_CID_BAT_BST:
- + /* Ensure we do not miss anything important due to caching. */
- + return msecs_to_jiffies(2000);
- +
- + default:
- + return 0;
- + }
- +}
- +
- +static bool san_evt_bat(const struct ssam_event *event, struct device *dev)
- +{
- + int status;
- +
- + switch (event->command_id) {
- + case SAM_EVENT_CID_BAT_BIX:
- + status = san_evt_bat_bix(dev, event);
- + break;
- +
- + case SAM_EVENT_CID_BAT_BST:
- + status = san_evt_bat_bst(dev, event);
- + break;
- +
- + case SAM_EVENT_CID_BAT_ADP:
- + status = san_evt_bat_adp(dev, event);
- + break;
- +
- + case SAM_EVENT_CID_BAT_PROT:
- + /*
- + * TODO: Implement support for battery protection status change
- + * event.
- + */
- + return true;
- +
- + case SAM_EVENT_CID_BAT_DPTF:
- + status = san_evt_bat_dptf(dev, event);
- + break;
- +
- + default:
- + return false;
- + }
- +
- + if (status) {
- + dev_err(dev, "error handling power event (cid = %#04x)\n",
- + event->command_id);
- + }
- +
- + return true;
- +}
- +
- +static void san_evt_bat_workfn(struct work_struct *work)
- +{
- + struct san_event_work *ev;
- +
- + ev = container_of(work, struct san_event_work, work.work);
- + san_evt_bat(&ev->event, ev->dev);
- + kfree(ev);
- +}
- +
- +static u32 san_evt_bat_nf(struct ssam_event_notifier *nf,
- + const struct ssam_event *event)
- +{
- + struct san_data *d = to_san_data(nf, nf_bat);
- + struct san_event_work *work;
- + unsigned long delay = san_evt_bat_delay(event->command_id);
- +
- + if (delay == 0)
- + return san_evt_bat(event, d->dev) ? SSAM_NOTIF_HANDLED : 0;
- +
- + work = kzalloc(sizeof(*work) + event->length, GFP_KERNEL);
- + if (!work)
- + return ssam_notifier_from_errno(-ENOMEM);
- +
- + INIT_DELAYED_WORK(&work->work, san_evt_bat_workfn);
- + work->dev = d->dev;
- +
- + memcpy(&work->event, event, sizeof(struct ssam_event) + event->length);
- +
- + schedule_delayed_work(&work->work, delay);
- + return SSAM_NOTIF_HANDLED;
- +}
- +
- +static int san_evt_tmp_trip(struct device *dev, const struct ssam_event *event)
- +{
- + union acpi_object param;
- +
- + /*
- + * The Surface ACPI expects an integer and not a package. This will
- + * cause a warning in acpica/nsarguments.c, but that warning can be
- + * safely ignored.
- + */
- + param.type = ACPI_TYPE_INTEGER;
- + param.integer.value = event->instance_id;
- +
- + return san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_THERMAL, ¶m);
- +}
- +
- +static bool san_evt_tmp(const struct ssam_event *event, struct device *dev)
- +{
- + int status;
- +
- + switch (event->command_id) {
- + case SAM_EVENT_CID_TMP_TRIP:
- + status = san_evt_tmp_trip(dev, event);
- + break;
- +
- + default:
- + return false;
- + }
- +
- + if (status) {
- + dev_err(dev, "error handling thermal event (cid = %#04x)\n",
- + event->command_id);
- + }
- +
- + return true;
- +}
- +
- +static u32 san_evt_tmp_nf(struct ssam_event_notifier *nf,
- + const struct ssam_event *event)
- +{
- + struct san_data *d = to_san_data(nf, nf_tmp);
- +
- + return san_evt_tmp(event, d->dev) ? SSAM_NOTIF_HANDLED : 0;
- +}
- +
- +
- +/* -- ACPI GSB OperationRegion handler -------------------------------------- */
- +
- +struct gsb_data_in {
- + u8 cv;
- +} __packed;
- +
- +struct gsb_data_rqsx {
- + u8 cv; /* Command value (san_gsb_request_cv). */
- + u8 tc; /* Target category. */
- + u8 tid; /* Target ID. */
- + u8 iid; /* Instance ID. */
- + u8 snc; /* Expect-response-flag. */
- + u8 cid; /* Command ID. */
- + u16 cdl; /* Payload length. */
- + u8 pld[]; /* Payload. */
- +} __packed;
- +
- +struct gsb_data_etwl {
- + u8 cv; /* Command value (should be 0x02). */
- + u8 etw3; /* Unknown. */
- + u8 etw4; /* Unknown. */
- + u8 msg[]; /* Error message (ASCIIZ). */
- +} __packed;
- +
- +struct gsb_data_out {
- + u8 status; /* _SSH communication status. */
- + u8 len; /* _SSH payload length. */
- + u8 pld[]; /* _SSH payload. */
- +} __packed;
- +
- +union gsb_buffer_data {
- + struct gsb_data_in in; /* Common input. */
- + struct gsb_data_rqsx rqsx; /* RQSX input. */
- + struct gsb_data_etwl etwl; /* ETWL input. */
- + struct gsb_data_out out; /* Output. */
- +};
- +
- +struct gsb_buffer {
- + u8 status; /* GSB AttribRawProcess status. */
- + u8 len; /* GSB AttribRawProcess length. */
- + union gsb_buffer_data data;
- +} __packed;
- +
- +#define SAN_GSB_MAX_RQSX_PAYLOAD (U8_MAX - 2 - sizeof(struct gsb_data_rqsx))
- +#define SAN_GSB_MAX_RESPONSE (U8_MAX - 2 - sizeof(struct gsb_data_out))
- +
- +#define SAN_GSB_COMMAND 0
- +
- +enum san_gsb_request_cv {
- + SAN_GSB_REQUEST_CV_RQST = 0x01,
- + SAN_GSB_REQUEST_CV_ETWL = 0x02,
- + SAN_GSB_REQUEST_CV_RQSG = 0x03,
- +};
- +
- +#define SAN_REQUEST_NUM_TRIES 5
- +
- +static acpi_status san_etwl(struct san_data *d, struct gsb_buffer *b)
- +{
- + struct gsb_data_etwl *etwl = &b->data.etwl;
- +
- + if (b->len < sizeof(struct gsb_data_etwl)) {
- + dev_err(d->dev, "invalid ETWL package (len = %d)\n", b->len);
- + return AE_OK;
- + }
- +
- + dev_err(d->dev, "ETWL(%#04x, %#04x): %.*s\n", etwl->etw3, etwl->etw4,
- + (unsigned int)(b->len - sizeof(struct gsb_data_etwl)),
- + (char *)etwl->msg);
- +
- + /* Indicate success. */
- + b->status = 0x00;
- + b->len = 0x00;
- +
- + return AE_OK;
- +}
- +
- +static
- +struct gsb_data_rqsx *san_validate_rqsx(struct device *dev, const char *type,
- + struct gsb_buffer *b)
- +{
- + struct gsb_data_rqsx *rqsx = &b->data.rqsx;
- +
- + if (b->len < sizeof(struct gsb_data_rqsx)) {
- + dev_err(dev, "invalid %s package (len = %d)\n", type, b->len);
- + return NULL;
- + }
- +
- + if (get_unaligned(&rqsx->cdl) != b->len - sizeof(struct gsb_data_rqsx)) {
- + dev_err(dev, "bogus %s package (len = %d, cdl = %d)\n",
- + type, b->len, get_unaligned(&rqsx->cdl));
- + return NULL;
- + }
- +
- + if (get_unaligned(&rqsx->cdl) > SAN_GSB_MAX_RQSX_PAYLOAD) {
- + dev_err(dev, "payload for %s package too large (cdl = %d)\n",
- + type, get_unaligned(&rqsx->cdl));
- + return NULL;
- + }
- +
- + return rqsx;
- +}
- +
- +static void gsb_rqsx_response_error(struct gsb_buffer *gsb, int status)
- +{
- + gsb->status = 0x00;
- + gsb->len = 0x02;
- + gsb->data.out.status = (u8)(-status);
- + gsb->data.out.len = 0x00;
- +}
- +
- +static void gsb_rqsx_response_success(struct gsb_buffer *gsb, u8 *ptr, size_t len)
- +{
- + gsb->status = 0x00;
- + gsb->len = len + 2;
- + gsb->data.out.status = 0x00;
- + gsb->data.out.len = len;
- +
- + if (len)
- + memcpy(&gsb->data.out.pld[0], ptr, len);
- +}
- +
- +static acpi_status san_rqst_fixup_suspended(struct san_data *d,
- + struct ssam_request *rqst,
- + struct gsb_buffer *gsb)
- +{
- + if (rqst->target_category == SSAM_SSH_TC_BAS && rqst->command_id == 0x0D) {
- + u8 base_state = 1;
- +
- + /* Base state quirk:
- + * The base state may be queried from ACPI when the EC is still
- + * suspended. In this case it will return '-EPERM'. This query
- + * will only be triggered from the ACPI lid GPE interrupt, thus
- + * we are either in laptop or studio mode (base status 0x01 or
- + * 0x02). Furthermore, we will only get here if the device (and
- + * EC) have been suspended.
- + *
- + * We now assume that the device is in laptop mode (0x01). This
- + * has the drawback that it will wake the device when unfolding
- + * it in studio mode, but it also allows us to avoid actively
- + * waiting for the EC to wake up, which may incur a notable
- + * delay.
- + */
- +
- + dev_dbg(d->dev, "rqst: fixup: base-state quirk\n");
- +
- + gsb_rqsx_response_success(gsb, &base_state, sizeof(base_state));
- + return AE_OK;
- + }
- +
- + gsb_rqsx_response_error(gsb, -ENXIO);
- + return AE_OK;
- +}
- +
- +static acpi_status san_rqst(struct san_data *d, struct gsb_buffer *buffer)
- +{
- + u8 rspbuf[SAN_GSB_MAX_RESPONSE];
- + struct gsb_data_rqsx *gsb_rqst;
- + struct ssam_request rqst;
- + struct ssam_response rsp;
- + int status = 0;
- +
- + gsb_rqst = san_validate_rqsx(d->dev, "RQST", buffer);
- + if (!gsb_rqst)
- + return AE_OK;
- +
- + rqst.target_category = gsb_rqst->tc;
- + rqst.target_id = gsb_rqst->tid;
- + rqst.command_id = gsb_rqst->cid;
- + rqst.instance_id = gsb_rqst->iid;
- + rqst.flags = gsb_rqst->snc ? SSAM_REQUEST_HAS_RESPONSE : 0;
- + rqst.length = get_unaligned(&gsb_rqst->cdl);
- + rqst.payload = &gsb_rqst->pld[0];
- +
- + rsp.capacity = ARRAY_SIZE(rspbuf);
- + rsp.length = 0;
- + rsp.pointer = &rspbuf[0];
- +
- + /* Handle suspended device. */
- + if (d->dev->power.is_suspended) {
- + dev_warn(d->dev, "rqst: device is suspended, not executing\n");
- + return san_rqst_fixup_suspended(d, &rqst, buffer);
- + }
- +
- + status = __ssam_retry(ssam_request_sync_onstack, SAN_REQUEST_NUM_TRIES,
- + d->ctrl, &rqst, &rsp, SAN_GSB_MAX_RQSX_PAYLOAD);
- +
- + if (!status) {
- + gsb_rqsx_response_success(buffer, rsp.pointer, rsp.length);
- + } else {
- + dev_err(d->dev, "rqst: failed with error %d\n", status);
- + gsb_rqsx_response_error(buffer, status);
- + }
- +
- + return AE_OK;
- +}
- +
- +static acpi_status san_rqsg(struct san_data *d, struct gsb_buffer *buffer)
- +{
- + struct gsb_data_rqsx *gsb_rqsg;
- + struct san_dgpu_event evt;
- + int status;
- +
- + gsb_rqsg = san_validate_rqsx(d->dev, "RQSG", buffer);
- + if (!gsb_rqsg)
- + return AE_OK;
- +
- + evt.category = gsb_rqsg->tc;
- + evt.target = gsb_rqsg->tid;
- + evt.command = gsb_rqsg->cid;
- + evt.instance = gsb_rqsg->iid;
- + evt.length = get_unaligned(&gsb_rqsg->cdl);
- + evt.payload = &gsb_rqsg->pld[0];
- +
- + status = san_dgpu_notifier_call(&evt);
- + if (!status) {
- + gsb_rqsx_response_success(buffer, NULL, 0);
- + } else {
- + dev_err(d->dev, "rqsg: failed with error %d\n", status);
- + gsb_rqsx_response_error(buffer, status);
- + }
- +
- + return AE_OK;
- +}
- +
- +static acpi_status san_opreg_handler(u32 function, acpi_physical_address command,
- + u32 bits, u64 *value64, void *opreg_context,
- + void *region_context)
- +{
- + struct san_data *d = to_san_data(opreg_context, info);
- + struct gsb_buffer *buffer = (struct gsb_buffer *)value64;
- + int accessor_type = (function & 0xFFFF0000) >> 16;
- +
- + if (command != SAN_GSB_COMMAND) {
- + dev_warn(d->dev, "unsupported command: %#04llx\n", command);
- + return AE_OK;
- + }
- +
- + if (accessor_type != ACPI_GSB_ACCESS_ATTRIB_RAW_PROCESS) {
- + dev_err(d->dev, "invalid access type: %#04x\n", accessor_type);
- + return AE_OK;
- + }
- +
- + /* Buffer must have at least contain the command-value. */
- + if (buffer->len == 0) {
- + dev_err(d->dev, "request-package too small\n");
- + return AE_OK;
- + }
- +
- + switch (buffer->data.in.cv) {
- + case SAN_GSB_REQUEST_CV_RQST:
- + return san_rqst(d, buffer);
- +
- + case SAN_GSB_REQUEST_CV_ETWL:
- + return san_etwl(d, buffer);
- +
- + case SAN_GSB_REQUEST_CV_RQSG:
- + return san_rqsg(d, buffer);
- +
- + default:
- + dev_warn(d->dev, "unsupported SAN0 request (cv: %#04x)\n",
- + buffer->data.in.cv);
- + return AE_OK;
- + }
- +}
- +
- +
- +/* -- Driver setup. --------------------------------------------------------- */
- +
- +static int san_events_register(struct platform_device *pdev)
- +{
- + struct san_data *d = platform_get_drvdata(pdev);
- + int status;
- +
- + d->nf_bat.base.priority = 1;
- + d->nf_bat.base.fn = san_evt_bat_nf;
- + d->nf_bat.event.reg = SSAM_EVENT_REGISTRY_SAM;
- + d->nf_bat.event.id.target_category = SSAM_SSH_TC_BAT;
- + d->nf_bat.event.id.instance = 0;
- + d->nf_bat.event.mask = SSAM_EVENT_MASK_TARGET;
- + d->nf_bat.event.flags = SSAM_EVENT_SEQUENCED;
- +
- + d->nf_tmp.base.priority = 1;
- + d->nf_tmp.base.fn = san_evt_tmp_nf;
- + d->nf_tmp.event.reg = SSAM_EVENT_REGISTRY_SAM;
- + d->nf_tmp.event.id.target_category = SSAM_SSH_TC_TMP;
- + d->nf_tmp.event.id.instance = 0;
- + d->nf_tmp.event.mask = SSAM_EVENT_MASK_TARGET;
- + d->nf_tmp.event.flags = SSAM_EVENT_SEQUENCED;
- +
- + status = ssam_notifier_register(d->ctrl, &d->nf_bat);
- + if (status)
- + return status;
- +
- + status = ssam_notifier_register(d->ctrl, &d->nf_tmp);
- + if (status)
- + ssam_notifier_unregister(d->ctrl, &d->nf_bat);
- +
- + return status;
- +}
- +
- +static void san_events_unregister(struct platform_device *pdev)
- +{
- + struct san_data *d = platform_get_drvdata(pdev);
- +
- + ssam_notifier_unregister(d->ctrl, &d->nf_bat);
- + ssam_notifier_unregister(d->ctrl, &d->nf_tmp);
- +}
- +
- +#define san_consumer_printk(level, dev, handle, fmt, ...) \
- +do { \
- + char *path = "<error getting consumer path>"; \
- + struct acpi_buffer buffer = { \
- + .length = ACPI_ALLOCATE_BUFFER, \
- + .pointer = NULL, \
- + }; \
- + \
- + if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer))) \
- + path = buffer.pointer; \
- + \
- + dev_##level(dev, "[%s]: " fmt, path, ##__VA_ARGS__); \
- + kfree(buffer.pointer); \
- +} while (0)
- +
- +#define san_consumer_dbg(dev, handle, fmt, ...) \
- + san_consumer_printk(dbg, dev, handle, fmt, ##__VA_ARGS__)
- +
- +#define san_consumer_warn(dev, handle, fmt, ...) \
- + san_consumer_printk(warn, dev, handle, fmt, ##__VA_ARGS__)
- +
- +static bool is_san_consumer(struct platform_device *pdev, acpi_handle handle)
- +{
- + struct acpi_handle_list dep_devices;
- + acpi_handle supplier = ACPI_HANDLE(&pdev->dev);
- + acpi_status status;
- + int i;
- +
- + if (!acpi_has_method(handle, "_DEP"))
- + return false;
- +
- + status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
- + if (ACPI_FAILURE(status)) {
- + san_consumer_dbg(&pdev->dev, handle, "failed to evaluate _DEP\n");
- + return false;
- + }
- +
- + for (i = 0; i < dep_devices.count; i++) {
- + if (dep_devices.handles[i] == supplier)
- + return true;
- + }
- +
- + return false;
- +}
- +
- +static acpi_status san_consumer_setup(acpi_handle handle, u32 lvl,
- + void *context, void **rv)
- +{
- + const u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_SUPPLIER;
- + struct platform_device *pdev = context;
- + struct acpi_device *adev;
- + struct device_link *link;
- +
- + if (!is_san_consumer(pdev, handle))
- + return AE_OK;
- +
- + /* Ignore ACPI devices that are not present. */
- + if (acpi_bus_get_device(handle, &adev) != 0)
- + return AE_OK;
- +
- + san_consumer_dbg(&pdev->dev, handle, "creating device link\n");
- +
- + /* Try to set up device links, ignore but log errors. */
- + link = device_link_add(&adev->dev, &pdev->dev, flags);
- + if (!link) {
- + san_consumer_warn(&pdev->dev, handle, "failed to create device link\n");
- + return AE_OK;
- + }
- +
- + return AE_OK;
- +}
- +
- +static int san_consumer_links_setup(struct platform_device *pdev)
- +{
- + acpi_status status;
- +
- + status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
- + ACPI_UINT32_MAX, san_consumer_setup, NULL,
- + pdev, NULL);
- +
- + return status ? -EFAULT : 0;
- +}
- +
- +static int san_probe(struct platform_device *pdev)
- +{
- + acpi_handle san = ACPI_HANDLE(&pdev->dev);
- + struct ssam_controller *ctrl;
- + struct san_data *data;
- + acpi_status astatus;
- + int status;
- +
- + ctrl = ssam_client_bind(&pdev->dev);
- + if (IS_ERR(ctrl))
- + return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
- +
- + status = san_consumer_links_setup(pdev);
- + if (status)
- + return status;
- +
- + data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
- + if (!data)
- + return -ENOMEM;
- +
- + data->dev = &pdev->dev;
- + data->ctrl = ctrl;
- +
- + platform_set_drvdata(pdev, data);
- +
- + astatus = acpi_install_address_space_handler(san, ACPI_ADR_SPACE_GSBUS,
- + &san_opreg_handler, NULL,
- + &data->info);
- + if (ACPI_FAILURE(astatus))
- + return -ENXIO;
- +
- + status = san_events_register(pdev);
- + if (status)
- + goto err_enable_events;
- +
- + status = san_set_rqsg_interface_device(&pdev->dev);
- + if (status)
- + goto err_install_dev;
- +
- + acpi_walk_dep_device_list(san);
- + return 0;
- +
- +err_install_dev:
- + san_events_unregister(pdev);
- +err_enable_events:
- + acpi_remove_address_space_handler(san, ACPI_ADR_SPACE_GSBUS,
- + &san_opreg_handler);
- + return status;
- +}
- +
- +static int san_remove(struct platform_device *pdev)
- +{
- + acpi_handle san = ACPI_HANDLE(&pdev->dev);
- +
- + san_set_rqsg_interface_device(NULL);
- + acpi_remove_address_space_handler(san, ACPI_ADR_SPACE_GSBUS,
- + &san_opreg_handler);
- + san_events_unregister(pdev);
- +
- + /*
- + * We have unregistered our event sources. Now we need to ensure that
- + * all delayed works they may have spawned are run to completion.
- + */
- + flush_scheduled_work();
- +
- + return 0;
- +}
- +
- +static const struct acpi_device_id san_match[] = {
- + { "MSHW0091" },
- + { },
- +};
- +MODULE_DEVICE_TABLE(acpi, san_match);
- +
- +static struct platform_driver surface_acpi_notify = {
- + .probe = san_probe,
- + .remove = san_remove,
- + .driver = {
- + .name = "surface_acpi_notify",
- + .acpi_match_table = san_match,
- + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- + },
- +};
- +module_platform_driver(surface_acpi_notify);
- +
- +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- +MODULE_DESCRIPTION("Surface ACPI Notify driver for Surface System Aggregator Module");
- +MODULE_LICENSE("GPL");
- diff --git a/include/linux/surface_acpi_notify.h b/include/linux/surface_acpi_notify.h
- new file mode 100644
- index 000000000000..8e3e86c7d78c
- --- /dev/null
- +++ b/include/linux/surface_acpi_notify.h
- @@ -0,0 +1,39 @@
- +/* SPDX-License-Identifier: GPL-2.0+ */
- +/*
- + * Interface for Surface ACPI Notify (SAN) driver.
- + *
- + * Provides access to discrete GPU notifications sent from ACPI via the SAN
- + * driver, which are not handled by this driver directly.
- + *
- + * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#ifndef _LINUX_SURFACE_ACPI_NOTIFY_H
- +#define _LINUX_SURFACE_ACPI_NOTIFY_H
- +
- +#include <linux/notifier.h>
- +#include <linux/types.h>
- +
- +/**
- + * struct san_dgpu_event - Discrete GPU ACPI event.
- + * @category: Category of the event.
- + * @target: Target ID of the event source.
- + * @command: Command ID of the event.
- + * @instance: Instance ID of the event source.
- + * @length: Length of the event's payload data (in bytes).
- + * @payload: Pointer to the event's payload data.
- + */
- +struct san_dgpu_event {
- + u8 category;
- + u8 target;
- + u8 command;
- + u8 instance;
- + u16 length;
- + u8 *payload;
- +};
- +
- +int san_client_link(struct device *client);
- +int san_dgpu_notifier_register(struct notifier_block *nb);
- +int san_dgpu_notifier_unregister(struct notifier_block *nb);
- +
- +#endif /* _LINUX_SURFACE_ACPI_NOTIFY_H */
- --
- 2.31.0
- From f4a7514d778da6ff5180ac88e58c53d7573b8ae2 Mon Sep 17 00:00:00 2001
- From: Colin Ian King <colin.king@canonical.com>
- Date: Mon, 11 Jan 2021 14:46:48 +0000
- Subject: [PATCH] platform/surface: fix potential integer overflow on shift of
- a int
- The left shift of int 32 bit integer constant 1 is evaluated using 32 bit
- arithmetic and then passed as a 64 bit function argument. In the case where
- func is 32 or more this can lead to an oveflow. Avoid this by shifting
- using the BIT_ULL macro instead.
- Addresses-Coverity: ("Unintentional integer overflow")
- Fixes: fc00bc8ac1da ("platform/surface: Add Surface ACPI Notify driver")
- Signed-off-by: Colin Ian King <colin.king@canonical.com>
- Reviewed-by: Maximilian Luz <luzmaximilian@gmail.com>
- Link: https://lore.kernel.org/r/20210111144648.20498-1-colin.king@canonical.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- drivers/platform/surface/surface_acpi_notify.c | 2 +-
- 1 file changed, 1 insertion(+), 1 deletion(-)
- diff --git a/drivers/platform/surface/surface_acpi_notify.c b/drivers/platform/surface/surface_acpi_notify.c
- index 8cd67a669c86..ef9c1f8e8336 100644
- --- a/drivers/platform/surface/surface_acpi_notify.c
- +++ b/drivers/platform/surface/surface_acpi_notify.c
- @@ -188,7 +188,7 @@ static int san_acpi_notify_event(struct device *dev, u64 func,
- union acpi_object *obj;
- int status = 0;
-
- - if (!acpi_check_dsm(san, &SAN_DSM_UUID, SAN_DSM_REVISION, 1 << func))
- + if (!acpi_check_dsm(san, &SAN_DSM_UUID, SAN_DSM_REVISION, BIT_ULL(func)))
- return 0;
-
- dev_dbg(dev, "notify event %#04llx\n", func);
- --
- 2.31.0
- From f459f702ea23d66f9e550b3db3435d27a0491a0e Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Mon, 11 Jan 2021 16:48:50 +0100
- Subject: [PATCH] platform/surface: aggregator_cdev: Fix access of
- uninitialized variables
- When copy_struct_from_user() in ssam_cdev_request() fails, we directly
- jump to the 'out' label. In this case, however 'spec' and 'rsp' are not
- initialized, but we still access fields of those variables. Fix this by
- initializing them at the time of their declaration.
- Reported-by: Colin Ian King <colin.king@canonical.com>
- Fixes: 178f6ab77e61 ("platform/surface: Add Surface Aggregator user-space interface")
- Addresses-Coverity: ("Uninitialized pointer read")
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Link: https://lore.kernel.org/r/20210111154851.325404-2-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- drivers/platform/surface/surface_aggregator_cdev.c | 4 ++--
- 1 file changed, 2 insertions(+), 2 deletions(-)
- diff --git a/drivers/platform/surface/surface_aggregator_cdev.c b/drivers/platform/surface/surface_aggregator_cdev.c
- index 340d15b148b9..979340cdd9de 100644
- --- a/drivers/platform/surface/surface_aggregator_cdev.c
- +++ b/drivers/platform/surface/surface_aggregator_cdev.c
- @@ -66,8 +66,8 @@ static long ssam_cdev_request(struct ssam_cdev *cdev, unsigned long arg)
- {
- struct ssam_cdev_request __user *r;
- struct ssam_cdev_request rqst;
- - struct ssam_request spec;
- - struct ssam_response rsp;
- + struct ssam_request spec = {};
- + struct ssam_response rsp = {};
- const void __user *plddata;
- void __user *rspdata;
- int status = 0, ret = 0, tmp;
- --
- 2.31.0
- From 3580c235191512dfffa9b9cdc2ded467c33fe4ce Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Mon, 11 Jan 2021 16:48:51 +0100
- Subject: [PATCH] platform/surface: aggregator_cdev: Add comments regarding
- unchecked allocation size
- CI static analysis complains about the allocation size in payload and
- response buffers being unchecked. In general, these allocations should
- be safe as the user-input is u16 and thus limited to U16_MAX, which is
- only slightly larger than the theoretical maximum imposed by the
- underlying SSH protocol.
- All bounds on these values required by the underlying protocol are
- enforced in ssam_request_sync() (or rather the functions called by it),
- thus bounds here are only relevant for allocation.
- Add comments explaining that this should be safe.
- Reported-by: Colin Ian King <colin.king@canonical.com>
- Fixes: 178f6ab77e61 ("platform/surface: Add Surface Aggregator user-space interface")
- Addresses-Coverity: ("Untrusted allocation size")
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Link: https://lore.kernel.org/r/20210111154851.325404-3-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- .../surface/surface_aggregator_cdev.c | 19 +++++++++++++++++++
- 1 file changed, 19 insertions(+)
- diff --git a/drivers/platform/surface/surface_aggregator_cdev.c b/drivers/platform/surface/surface_aggregator_cdev.c
- index 979340cdd9de..79e28fab7e40 100644
- --- a/drivers/platform/surface/surface_aggregator_cdev.c
- +++ b/drivers/platform/surface/surface_aggregator_cdev.c
- @@ -106,6 +106,15 @@ static long ssam_cdev_request(struct ssam_cdev *cdev, unsigned long arg)
- goto out;
- }
-
- + /*
- + * Note: spec.length is limited to U16_MAX bytes via struct
- + * ssam_cdev_request. This is slightly larger than the
- + * theoretical maximum (SSH_COMMAND_MAX_PAYLOAD_SIZE) of the
- + * underlying protocol (note that nothing remotely this size
- + * should ever be allocated in any normal case). This size is
- + * validated later in ssam_request_sync(), for allocation the
- + * bound imposed by u16 should be enough.
- + */
- spec.payload = kzalloc(spec.length, GFP_KERNEL);
- if (!spec.payload) {
- ret = -ENOMEM;
- @@ -125,6 +134,16 @@ static long ssam_cdev_request(struct ssam_cdev *cdev, unsigned long arg)
- goto out;
- }
-
- + /*
- + * Note: rsp.capacity is limited to U16_MAX bytes via struct
- + * ssam_cdev_request. This is slightly larger than the
- + * theoretical maximum (SSH_COMMAND_MAX_PAYLOAD_SIZE) of the
- + * underlying protocol (note that nothing remotely this size
- + * should ever be allocated in any normal case). In later use,
- + * this capacity does not have to be strictly bounded, as it
- + * is only used as an output buffer to be written to. For
- + * allocation the bound imposed by u16 should be enough.
- + */
- rsp.pointer = kzalloc(rsp.capacity, GFP_KERNEL);
- if (!rsp.pointer) {
- ret = -ENOMEM;
- --
- 2.31.0
- From 08feed5fe75ced960fb3db8193f1fde8597b2d11 Mon Sep 17 00:00:00 2001
- From: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
- Date: Thu, 14 Jan 2021 09:04:52 +0100
- Subject: [PATCH] platform/surface: aggregator: fix a kernel-doc markup
- A function has a different name between their prototype
- and its kernel-doc markup:
- ../drivers/platform/surface/aggregator/ssh_request_layer.c:1065: warning: expecting prototype for ssh_rtl_tx_start(). Prototype was for ssh_rtl_start() instead
- Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
- Reviewed-by: Maximilian Luz <luzmaximilian@gmail.com>
- Link: https://lore.kernel.org/r/4a6bf33cfbd06654d78294127f2b6d354d073089.1610610937.git.mchehab+huawei@kernel.org
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- drivers/platform/surface/aggregator/ssh_request_layer.c | 2 +-
- 1 file changed, 1 insertion(+), 1 deletion(-)
- diff --git a/drivers/platform/surface/aggregator/ssh_request_layer.c b/drivers/platform/surface/aggregator/ssh_request_layer.c
- index bb1c862411a2..25db4d638cfa 100644
- --- a/drivers/platform/surface/aggregator/ssh_request_layer.c
- +++ b/drivers/platform/surface/aggregator/ssh_request_layer.c
- @@ -1056,7 +1056,7 @@ void ssh_rtl_destroy(struct ssh_rtl *rtl)
- }
-
- /**
- - * ssh_rtl_tx_start() - Start request transmitter and receiver.
- + * ssh_rtl_start() - Start request transmitter and receiver.
- * @rtl: The request transport layer.
- *
- * Return: Returns zero on success, a negative error code on failure.
- --
- 2.31.0
- From 58ae3b353f5cf89469766b3f2e2bdc461f145b2c Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Thu, 14 Jan 2021 16:08:26 +0100
- Subject: [PATCH] platform/surface: aggregator: Fix kernel-doc references
- Both, ssh_rtl_rx_start() and ssh_rtl_tx_start() functions, do not exist
- and have been consolidated into ssh_rtl_start(). Nevertheless,
- kernel-doc references the former functions. Replace those references
- with references to ssh_rtl_start().
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Link: https://lore.kernel.org/r/20210114150826.19109-1-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- drivers/platform/surface/aggregator/ssh_request_layer.c | 5 ++---
- 1 file changed, 2 insertions(+), 3 deletions(-)
- diff --git a/drivers/platform/surface/aggregator/ssh_request_layer.c b/drivers/platform/surface/aggregator/ssh_request_layer.c
- index 25db4d638cfa..52a83a8fcf82 100644
- --- a/drivers/platform/surface/aggregator/ssh_request_layer.c
- +++ b/drivers/platform/surface/aggregator/ssh_request_layer.c
- @@ -1004,9 +1004,8 @@ int ssh_request_init(struct ssh_request *rqst, enum ssam_request_flags flags,
- *
- * Initializes the given request transport layer and associated packet
- * transport layer. Transmitter and receiver threads must be started
- - * separately via ssh_rtl_tx_start() and ssh_rtl_rx_start(), after the
- - * request-layer has been initialized and the lower-level serial device layer
- - * has been set up.
- + * separately via ssh_rtl_start(), after the request-layer has been
- + * initialized and the lower-level serial device layer has been set up.
- *
- * Return: Returns zero on success and a nonzero error code on failure.
- */
- --
- 2.31.0
- From 975e2951520f6cda4c23bbb4a0f4ab81c9a7304b Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Tue, 26 Jan 2021 18:22:02 +0100
- Subject: [PATCH] platform/surface: aggregator: Fix braces in if condition with
- unlikely() macro
- The braces of the unlikely() macro inside the if condition only cover
- the subtraction part, not the whole statement. This causes the result of
- the subtraction to be converted to zero or one. While that still works
- in this context, it causes static analysis tools to complain (and is
- just plain wrong).
- Fix the bracket placement and, while at it, simplify the if-condition.
- Also add a comment to the if-condition explaining what we expect the
- result to be and what happens on the failure path, as it seems to have
- caused a bit of confusion.
- This commit should not cause any difference in behavior or generated
- code.
- Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
- Fixes: c167b9c7e3d6 ("platform/surface: Add Surface Aggregator subsystem")
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Link: https://lore.kernel.org/r/20210126172202.1428367-1-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- .../surface/aggregator/ssh_packet_layer.c | 19 ++++++++++++++++++-
- 1 file changed, 18 insertions(+), 1 deletion(-)
- diff --git a/drivers/platform/surface/aggregator/ssh_packet_layer.c b/drivers/platform/surface/aggregator/ssh_packet_layer.c
- index 74f0faaa2b27..583315db8b02 100644
- --- a/drivers/platform/surface/aggregator/ssh_packet_layer.c
- +++ b/drivers/platform/surface/aggregator/ssh_packet_layer.c
- @@ -1694,7 +1694,24 @@ static size_t ssh_ptl_rx_eval(struct ssh_ptl *ptl, struct ssam_span *source)
- /* Find SYN. */
- syn_found = sshp_find_syn(source, &aligned);
-
- - if (unlikely(aligned.ptr - source->ptr) > 0) {
- + if (unlikely(aligned.ptr != source->ptr)) {
- + /*
- + * We expect aligned.ptr == source->ptr. If this is not the
- + * case, then aligned.ptr > source->ptr and we've encountered
- + * some unexpected data where we'd expect the start of a new
- + * message (i.e. the SYN sequence).
- + *
- + * This can happen when a CRC check for the previous message
- + * failed and we start actively searching for the next one
- + * (via the call to sshp_find_syn() above), or the first bytes
- + * of a message got dropped or corrupted.
- + *
- + * In any case, we issue a warning, send a NAK to the EC to
- + * request re-transmission of any data we haven't acknowledged
- + * yet, and finally, skip everything up to the next SYN
- + * sequence.
- + */
- +
- ptl_warn(ptl, "rx: parser: invalid start of frame, skipping\n");
-
- /*
- --
- 2.31.0
- From c34c49f35d59cbe74005ec7a1b4fda147272f2ba Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Thu, 11 Feb 2021 13:41:49 +0100
- Subject: [PATCH] platform/surface: aggregator: Fix access of unaligned value
- The raw message frame length is unaligned and explicitly marked as
- little endian. It should not be accessed without the appropriate
- accessor functions. Fix this.
- Note that payload.len already contains the correct length after parsing
- via sshp_parse_frame(), so we can simply use that instead.
- Reported-by: kernel-test-robot <lkp@intel.com>
- Fixes: c167b9c7e3d6 ("platform/surface: Add Surface Aggregator subsystem")
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Acked-by: Mark Gross <mgross@linux.intel.com>
- Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
- Link: https://lore.kernel.org/r/20210211124149.2439007-1-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- drivers/platform/surface/aggregator/ssh_packet_layer.c | 2 +-
- 1 file changed, 1 insertion(+), 1 deletion(-)
- diff --git a/drivers/platform/surface/aggregator/ssh_packet_layer.c b/drivers/platform/surface/aggregator/ssh_packet_layer.c
- index 583315db8b02..15d96eac6811 100644
- --- a/drivers/platform/surface/aggregator/ssh_packet_layer.c
- +++ b/drivers/platform/surface/aggregator/ssh_packet_layer.c
- @@ -1774,7 +1774,7 @@ static size_t ssh_ptl_rx_eval(struct ssh_ptl *ptl, struct ssam_span *source)
- break;
- }
-
- - return aligned.ptr - source->ptr + SSH_MESSAGE_LENGTH(frame->len);
- + return aligned.ptr - source->ptr + SSH_MESSAGE_LENGTH(payload.len);
- }
-
- static int ssh_ptl_rx_threadfn(void *data)
- --
- 2.31.0
- From 99804626530d972c92ed6670e9f46c901251dba4 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Sun, 7 Feb 2021 03:42:45 +0100
- Subject: [PATCH] platform/surface: Set up Surface Aggregator device registry
- The Surface System Aggregator Module (SSAM) subsystem provides various
- functionalities, which are separated by spreading them across multiple
- devices and corresponding drivers. Parts of that functionality / some of
- those devices, however, can (as far as we currently know) not be
- auto-detected by conventional means. While older (specifically 5th- and
- 6th-)generation models do advertise most of their functionality via
- standard platform devices in ACPI, newer generations do not.
- As we are currently also not aware of any feasible way to query said
- functionalities dynamically, this poses a problem. There is, however, a
- device in ACPI that seems to be used by Windows for identifying
- different Surface models: The Windows Surface Integration Device (WSID).
- This device seems to have a HID corresponding to the overall set of
- functionalities SSAM provides for the associated model.
- This commit introduces a registry providing non-detectable device
- information via software nodes. In addition, a SSAM platform hub driver
- is introduced, which takes care of creating and managing the SSAM
- devices specified in this registry. This approach allows for a
- hierarchical setup akin to ACPI and is easily extendable, e.g. via
- firmware node properties.
- Note that this commit only provides the basis for the platform hub and
- registry, and does not add any content to it. The registry will be
- expanded in subsequent commits.
- Patchset: surface-sam
- ---
- MAINTAINERS | 1 +
- drivers/platform/surface/Kconfig | 27 ++
- drivers/platform/surface/Makefile | 1 +
- .../surface/surface_aggregator_registry.c | 284 ++++++++++++++++++
- 4 files changed, 313 insertions(+)
- create mode 100644 drivers/platform/surface/surface_aggregator_registry.c
- diff --git a/MAINTAINERS b/MAINTAINERS
- index dfe4f4e1da7a..1fd2fd35d5b7 100644
- --- a/MAINTAINERS
- +++ b/MAINTAINERS
- @@ -11815,6 +11815,7 @@ F: Documentation/driver-api/surface_aggregator/
- F: drivers/platform/surface/aggregator/
- F: drivers/platform/surface/surface_acpi_notify.c
- F: drivers/platform/surface/surface_aggregator_cdev.c
- +F: drivers/platform/surface/surface_aggregator_registry.c
- F: include/linux/surface_acpi_notify.h
- F: include/linux/surface_aggregator/
- F: include/uapi/linux/surface_aggregator/
- diff --git a/drivers/platform/surface/Kconfig b/drivers/platform/surface/Kconfig
- index b0b91fa2f6a1..97e08dd35992 100644
- --- a/drivers/platform/surface/Kconfig
- +++ b/drivers/platform/surface/Kconfig
- @@ -77,6 +77,33 @@ config SURFACE_AGGREGATOR_CDEV
- The provided interface is intended for debugging and development only,
- and should not be used otherwise.
-
- +config SURFACE_AGGREGATOR_REGISTRY
- + tristate "Surface System Aggregator Module Device Registry"
- + depends on SURFACE_AGGREGATOR
- + depends on SURFACE_AGGREGATOR_BUS
- + help
- + Device-registry and device-hubs for Surface System Aggregator Module
- + (SSAM) devices.
- +
- + Provides a module and driver which act as a device-registry for SSAM
- + client devices that cannot be detected automatically, e.g. via ACPI.
- + Such devices are instead provided via this registry and attached via
- + device hubs, also provided in this module.
- +
- + Devices provided via this registry are:
- + - Platform profile (performance-/cooling-mode) device (5th- and later
- + generations).
- + - Battery/AC devices (7th-generation).
- + - HID input devices (7th-generation).
- +
- + Select M (recommended) or Y here if you want support for the above
- + mentioned devices on the corresponding Surface models. Without this
- + module, the respective devices will not be instantiated and thus any
- + functionality provided by them will be missing, even when drivers for
- + these devices are present. In other words, this module only provides
- + the respective client devices. Drivers for these devices still need to
- + be selected via the other options.
- +
- config SURFACE_BOOK1_DGPU_SWITCH
- tristate "Surface Book 1 dGPU Switch Driver"
- depends on SYSFS
- diff --git a/drivers/platform/surface/Makefile b/drivers/platform/surface/Makefile
- index 72f4d9fbb6be..30a212aefd35 100644
- --- a/drivers/platform/surface/Makefile
- +++ b/drivers/platform/surface/Makefile
- @@ -10,6 +10,7 @@ obj-$(CONFIG_SURFACE_3_POWER_OPREGION) += surface3_power.o
- obj-$(CONFIG_SURFACE_ACPI_NOTIFY) += surface_acpi_notify.o
- obj-$(CONFIG_SURFACE_AGGREGATOR) += aggregator/
- obj-$(CONFIG_SURFACE_AGGREGATOR_CDEV) += surface_aggregator_cdev.o
- +obj-$(CONFIG_SURFACE_AGGREGATOR_REGISTRY) += surface_aggregator_registry.o
- obj-$(CONFIG_SURFACE_BOOK1_DGPU_SWITCH) += surfacebook1_dgpu_switch.o
- obj-$(CONFIG_SURFACE_GPE) += surface_gpe.o
- obj-$(CONFIG_SURFACE_PRO3_BUTTON) += surfacepro3_button.o
- diff --git a/drivers/platform/surface/surface_aggregator_registry.c b/drivers/platform/surface/surface_aggregator_registry.c
- new file mode 100644
- index 000000000000..a051d941ad96
- --- /dev/null
- +++ b/drivers/platform/surface/surface_aggregator_registry.c
- @@ -0,0 +1,284 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * Surface System Aggregator Module (SSAM) client device registry.
- + *
- + * Registry for non-platform/non-ACPI SSAM client devices, i.e. devices that
- + * cannot be auto-detected. Provides device-hubs and performs instantiation
- + * for these devices.
- + *
- + * Copyright (C) 2020-2021 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <linux/acpi.h>
- +#include <linux/kernel.h>
- +#include <linux/module.h>
- +#include <linux/platform_device.h>
- +#include <linux/property.h>
- +
- +#include <linux/surface_aggregator/controller.h>
- +#include <linux/surface_aggregator/device.h>
- +
- +
- +/* -- Device registry. ------------------------------------------------------ */
- +
- +/*
- + * SSAM device names follow the SSAM module alias, meaning they are prefixed
- + * with 'ssam:', followed by domain, category, target ID, instance ID, and
- + * function, each encoded as two-digit hexadecimal, separated by ':'. In other
- + * words, it follows the scheme
- + *
- + * ssam:dd:cc:tt:ii:ff
- + *
- + * Where, 'dd', 'cc', 'tt', 'ii', and 'ff' are the two-digit hexadecimal
- + * values mentioned above, respectively.
- + */
- +
- +/* Root node. */
- +static const struct software_node ssam_node_root = {
- + .name = "ssam_platform_hub",
- +};
- +
- +/* Devices for Surface Book 2. */
- +static const struct software_node *ssam_node_group_sb2[] = {
- + &ssam_node_root,
- + NULL,
- +};
- +
- +/* Devices for Surface Book 3. */
- +static const struct software_node *ssam_node_group_sb3[] = {
- + &ssam_node_root,
- + NULL,
- +};
- +
- +/* Devices for Surface Laptop 1. */
- +static const struct software_node *ssam_node_group_sl1[] = {
- + &ssam_node_root,
- + NULL,
- +};
- +
- +/* Devices for Surface Laptop 2. */
- +static const struct software_node *ssam_node_group_sl2[] = {
- + &ssam_node_root,
- + NULL,
- +};
- +
- +/* Devices for Surface Laptop 3. */
- +static const struct software_node *ssam_node_group_sl3[] = {
- + &ssam_node_root,
- + NULL,
- +};
- +
- +/* Devices for Surface Laptop Go. */
- +static const struct software_node *ssam_node_group_slg1[] = {
- + &ssam_node_root,
- + NULL,
- +};
- +
- +/* Devices for Surface Pro 5. */
- +static const struct software_node *ssam_node_group_sp5[] = {
- + &ssam_node_root,
- + NULL,
- +};
- +
- +/* Devices for Surface Pro 6. */
- +static const struct software_node *ssam_node_group_sp6[] = {
- + &ssam_node_root,
- + NULL,
- +};
- +
- +/* Devices for Surface Pro 7. */
- +static const struct software_node *ssam_node_group_sp7[] = {
- + &ssam_node_root,
- + NULL,
- +};
- +
- +
- +/* -- Device registry helper functions. ------------------------------------- */
- +
- +static int ssam_uid_from_string(const char *str, struct ssam_device_uid *uid)
- +{
- + u8 d, tc, tid, iid, fn;
- + int n;
- +
- + n = sscanf(str, "ssam:%hhx:%hhx:%hhx:%hhx:%hhx", &d, &tc, &tid, &iid, &fn);
- + if (n != 5)
- + return -EINVAL;
- +
- + uid->domain = d;
- + uid->category = tc;
- + uid->target = tid;
- + uid->instance = iid;
- + uid->function = fn;
- +
- + return 0;
- +}
- +
- +static int ssam_hub_remove_devices_fn(struct device *dev, void *data)
- +{
- + if (!is_ssam_device(dev))
- + return 0;
- +
- + ssam_device_remove(to_ssam_device(dev));
- + return 0;
- +}
- +
- +static void ssam_hub_remove_devices(struct device *parent)
- +{
- + device_for_each_child_reverse(parent, NULL, ssam_hub_remove_devices_fn);
- +}
- +
- +static int ssam_hub_add_device(struct device *parent, struct ssam_controller *ctrl,
- + struct fwnode_handle *node)
- +{
- + struct ssam_device_uid uid;
- + struct ssam_device *sdev;
- + int status;
- +
- + status = ssam_uid_from_string(fwnode_get_name(node), &uid);
- + if (status)
- + return status;
- +
- + sdev = ssam_device_alloc(ctrl, uid);
- + if (!sdev)
- + return -ENOMEM;
- +
- + sdev->dev.parent = parent;
- + sdev->dev.fwnode = node;
- +
- + status = ssam_device_add(sdev);
- + if (status)
- + ssam_device_put(sdev);
- +
- + return status;
- +}
- +
- +static int ssam_hub_add_devices(struct device *parent, struct ssam_controller *ctrl,
- + struct fwnode_handle *node)
- +{
- + struct fwnode_handle *child;
- + int status;
- +
- + fwnode_for_each_child_node(node, child) {
- + /*
- + * Try to add the device specified in the firmware node. If
- + * this fails with -EINVAL, the node does not specify any SSAM
- + * device, so ignore it and continue with the next one.
- + */
- +
- + status = ssam_hub_add_device(parent, ctrl, child);
- + if (status && status != -EINVAL)
- + goto err;
- + }
- +
- + return 0;
- +err:
- + ssam_hub_remove_devices(parent);
- + return status;
- +}
- +
- +
- +/* -- SSAM platform/meta-hub driver. ---------------------------------------- */
- +
- +static const struct acpi_device_id ssam_platform_hub_match[] = {
- + /* Surface Pro 4, 5, and 6 (OMBR < 0x10) */
- + { "MSHW0081", (unsigned long)ssam_node_group_sp5 },
- +
- + /* Surface Pro 6 (OMBR >= 0x10) */
- + { "MSHW0111", (unsigned long)ssam_node_group_sp6 },
- +
- + /* Surface Pro 7 */
- + { "MSHW0116", (unsigned long)ssam_node_group_sp7 },
- +
- + /* Surface Book 2 */
- + { "MSHW0107", (unsigned long)ssam_node_group_sb2 },
- +
- + /* Surface Book 3 */
- + { "MSHW0117", (unsigned long)ssam_node_group_sb3 },
- +
- + /* Surface Laptop 1 */
- + { "MSHW0086", (unsigned long)ssam_node_group_sl1 },
- +
- + /* Surface Laptop 2 */
- + { "MSHW0112", (unsigned long)ssam_node_group_sl2 },
- +
- + /* Surface Laptop 3 (13", Intel) */
- + { "MSHW0114", (unsigned long)ssam_node_group_sl3 },
- +
- + /* Surface Laptop 3 (15", AMD) */
- + { "MSHW0110", (unsigned long)ssam_node_group_sl3 },
- +
- + /* Surface Laptop Go 1 */
- + { "MSHW0118", (unsigned long)ssam_node_group_slg1 },
- +
- + { },
- +};
- +MODULE_DEVICE_TABLE(acpi, ssam_platform_hub_match);
- +
- +static int ssam_platform_hub_probe(struct platform_device *pdev)
- +{
- + const struct software_node **nodes;
- + struct ssam_controller *ctrl;
- + struct fwnode_handle *root;
- + int status;
- +
- + nodes = (const struct software_node **)acpi_device_get_match_data(&pdev->dev);
- + if (!nodes)
- + return -ENODEV;
- +
- + /*
- + * As we're adding the SSAM client devices as children under this device
- + * and not the SSAM controller, we need to add a device link to the
- + * controller to ensure that we remove all of our devices before the
- + * controller is removed. This also guarantees proper ordering for
- + * suspend/resume of the devices on this hub.
- + */
- + ctrl = ssam_client_bind(&pdev->dev);
- + if (IS_ERR(ctrl))
- + return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
- +
- + status = software_node_register_node_group(nodes);
- + if (status)
- + return status;
- +
- + root = software_node_fwnode(&ssam_node_root);
- + if (!root) {
- + software_node_unregister_node_group(nodes);
- + return -ENOENT;
- + }
- +
- + set_secondary_fwnode(&pdev->dev, root);
- +
- + status = ssam_hub_add_devices(&pdev->dev, ctrl, root);
- + if (status) {
- + set_secondary_fwnode(&pdev->dev, NULL);
- + software_node_unregister_node_group(nodes);
- + }
- +
- + platform_set_drvdata(pdev, nodes);
- + return status;
- +}
- +
- +static int ssam_platform_hub_remove(struct platform_device *pdev)
- +{
- + const struct software_node **nodes = platform_get_drvdata(pdev);
- +
- + ssam_hub_remove_devices(&pdev->dev);
- + set_secondary_fwnode(&pdev->dev, NULL);
- + software_node_unregister_node_group(nodes);
- + return 0;
- +}
- +
- +static struct platform_driver ssam_platform_hub_driver = {
- + .probe = ssam_platform_hub_probe,
- + .remove = ssam_platform_hub_remove,
- + .driver = {
- + .name = "surface_aggregator_platform_hub",
- + .acpi_match_table = ssam_platform_hub_match,
- + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- + },
- +};
- +module_platform_driver(ssam_platform_hub_driver);
- +
- +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- +MODULE_DESCRIPTION("Device-registry for Surface System Aggregator Module");
- +MODULE_LICENSE("GPL");
- --
- 2.31.0
- From a72e5aa8093f8325e96e1d997de0b18f09d62746 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Sun, 7 Feb 2021 04:14:35 +0100
- Subject: [PATCH] platform/surface: aggregator_registry: Add base device hub
- The Surface Book 3 has a detachable base part. While the top part
- (so-called clipboard) contains the CPU, touchscreen, and primary
- battery, the base contains, among other things, a keyboard, touchpad,
- and secondary battery.
- Those devices do not react well to being accessed when the base part is
- detached and should thus be removed and added in sync with the base. To
- facilitate this, we introduce a virtual base device hub, which
- automatically removes or adds the devices registered under it.
- Patchset: surface-sam
- ---
- .../surface/surface_aggregator_registry.c | 261 +++++++++++++++++-
- 1 file changed, 260 insertions(+), 1 deletion(-)
- diff --git a/drivers/platform/surface/surface_aggregator_registry.c b/drivers/platform/surface/surface_aggregator_registry.c
- index a051d941ad96..6c23d75a044c 100644
- --- a/drivers/platform/surface/surface_aggregator_registry.c
- +++ b/drivers/platform/surface/surface_aggregator_registry.c
- @@ -11,9 +11,12 @@
-
- #include <linux/acpi.h>
- #include <linux/kernel.h>
- +#include <linux/limits.h>
- #include <linux/module.h>
- +#include <linux/mutex.h>
- #include <linux/platform_device.h>
- #include <linux/property.h>
- +#include <linux/types.h>
-
- #include <linux/surface_aggregator/controller.h>
- #include <linux/surface_aggregator/device.h>
- @@ -38,6 +41,12 @@ static const struct software_node ssam_node_root = {
- .name = "ssam_platform_hub",
- };
-
- +/* Base device hub (devices attached to Surface Book 3 base). */
- +static const struct software_node ssam_node_hub_base = {
- + .name = "ssam:00:00:02:00:00",
- + .parent = &ssam_node_root,
- +};
- +
- /* Devices for Surface Book 2. */
- static const struct software_node *ssam_node_group_sb2[] = {
- &ssam_node_root,
- @@ -47,6 +56,7 @@ static const struct software_node *ssam_node_group_sb2[] = {
- /* Devices for Surface Book 3. */
- static const struct software_node *ssam_node_group_sb3[] = {
- &ssam_node_root,
- + &ssam_node_hub_base,
- NULL,
- };
-
- @@ -177,6 +187,230 @@ static int ssam_hub_add_devices(struct device *parent, struct ssam_controller *c
- }
-
-
- +/* -- SSAM base-hub driver. ------------------------------------------------- */
- +
- +enum ssam_base_hub_state {
- + SSAM_BASE_HUB_UNINITIALIZED,
- + SSAM_BASE_HUB_CONNECTED,
- + SSAM_BASE_HUB_DISCONNECTED,
- +};
- +
- +struct ssam_base_hub {
- + struct ssam_device *sdev;
- +
- + struct mutex lock; /* Guards state update checks and transitions. */
- + enum ssam_base_hub_state state;
- +
- + struct ssam_event_notifier notif;
- +};
- +
- +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_query_opmode, u8, {
- + .target_category = SSAM_SSH_TC_BAS,
- + .target_id = 0x01,
- + .command_id = 0x0d,
- + .instance_id = 0x00,
- +});
- +
- +#define SSAM_BAS_OPMODE_TABLET 0x00
- +#define SSAM_EVENT_BAS_CID_CONNECTION 0x0c
- +
- +static int ssam_base_hub_query_state(struct ssam_base_hub *hub, enum ssam_base_hub_state *state)
- +{
- + u8 opmode;
- + int status;
- +
- + status = ssam_retry(ssam_bas_query_opmode, hub->sdev->ctrl, &opmode);
- + if (status < 0) {
- + dev_err(&hub->sdev->dev, "failed to query base state: %d\n", status);
- + return status;
- + }
- +
- + if (opmode != SSAM_BAS_OPMODE_TABLET)
- + *state = SSAM_BASE_HUB_CONNECTED;
- + else
- + *state = SSAM_BASE_HUB_DISCONNECTED;
- +
- + return 0;
- +}
- +
- +static ssize_t ssam_base_hub_state_show(struct device *dev, struct device_attribute *attr,
- + char *buf)
- +{
- + struct ssam_base_hub *hub = dev_get_drvdata(dev);
- + bool connected;
- +
- + mutex_lock(&hub->lock);
- + connected = hub->state == SSAM_BASE_HUB_CONNECTED;
- + mutex_unlock(&hub->lock);
- +
- + return sysfs_emit(buf, "%d\n", connected);
- +}
- +
- +static struct device_attribute ssam_base_hub_attr_state =
- + __ATTR(state, 0444, ssam_base_hub_state_show, NULL);
- +
- +static struct attribute *ssam_base_hub_attrs[] = {
- + &ssam_base_hub_attr_state.attr,
- + NULL,
- +};
- +
- +const struct attribute_group ssam_base_hub_group = {
- + .attrs = ssam_base_hub_attrs,
- +};
- +
- +static int __ssam_base_hub_update(struct ssam_base_hub *hub, enum ssam_base_hub_state new)
- +{
- + struct fwnode_handle *node = dev_fwnode(&hub->sdev->dev);
- + int status = 0;
- +
- + lockdep_assert_held(&hub->lock);
- +
- + if (hub->state == new)
- + return 0;
- + hub->state = new;
- +
- + if (hub->state == SSAM_BASE_HUB_CONNECTED)
- + status = ssam_hub_add_devices(&hub->sdev->dev, hub->sdev->ctrl, node);
- + else
- + ssam_hub_remove_devices(&hub->sdev->dev);
- +
- + if (status)
- + dev_err(&hub->sdev->dev, "failed to update base-hub devices: %d\n", status);
- +
- + return status;
- +}
- +
- +static int ssam_base_hub_update(struct ssam_base_hub *hub)
- +{
- + enum ssam_base_hub_state state;
- + int status;
- +
- + mutex_lock(&hub->lock);
- +
- + status = ssam_base_hub_query_state(hub, &state);
- + if (!status)
- + status = __ssam_base_hub_update(hub, state);
- +
- + mutex_unlock(&hub->lock);
- + return status;
- +}
- +
- +static u32 ssam_base_hub_notif(struct ssam_event_notifier *nf, const struct ssam_event *event)
- +{
- + struct ssam_base_hub *hub;
- + struct ssam_device *sdev;
- + enum ssam_base_hub_state new;
- +
- + hub = container_of(nf, struct ssam_base_hub, notif);
- + sdev = hub->sdev;
- +
- + if (event->command_id != SSAM_EVENT_BAS_CID_CONNECTION)
- + return 0;
- +
- + if (event->length < 1) {
- + dev_err(&sdev->dev, "unexpected payload size: %u\n",
- + event->length);
- + return 0;
- + }
- +
- + if (event->data[0])
- + new = SSAM_BASE_HUB_CONNECTED;
- + else
- + new = SSAM_BASE_HUB_DISCONNECTED;
- +
- + mutex_lock(&hub->lock);
- + __ssam_base_hub_update(hub, new);
- + mutex_unlock(&hub->lock);
- +
- + /*
- + * Do not return SSAM_NOTIF_HANDLED: The event should be picked up and
- + * consumed by the detachment system driver. We're just a (more or less)
- + * silent observer.
- + */
- + return 0;
- +}
- +
- +static int __maybe_unused ssam_base_hub_resume(struct device *dev)
- +{
- + return ssam_base_hub_update(dev_get_drvdata(dev));
- +}
- +static SIMPLE_DEV_PM_OPS(ssam_base_hub_pm_ops, NULL, ssam_base_hub_resume);
- +
- +static int ssam_base_hub_probe(struct ssam_device *sdev)
- +{
- + struct ssam_base_hub *hub;
- + int status;
- +
- + hub = devm_kzalloc(&sdev->dev, sizeof(*hub), GFP_KERNEL);
- + if (!hub)
- + return -ENOMEM;
- +
- + mutex_init(&hub->lock);
- +
- + hub->sdev = sdev;
- + hub->state = SSAM_BASE_HUB_UNINITIALIZED;
- +
- + hub->notif.base.priority = INT_MAX; /* This notifier should run first. */
- + hub->notif.base.fn = ssam_base_hub_notif;
- + hub->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
- + hub->notif.event.id.target_category = SSAM_SSH_TC_BAS,
- + hub->notif.event.id.instance = 0,
- + hub->notif.event.mask = SSAM_EVENT_MASK_NONE;
- + hub->notif.event.flags = SSAM_EVENT_SEQUENCED;
- +
- + ssam_device_set_drvdata(sdev, hub);
- +
- + status = ssam_notifier_register(sdev->ctrl, &hub->notif);
- + if (status)
- + goto err_register;
- +
- + status = ssam_base_hub_update(hub);
- + if (status)
- + goto err_update;
- +
- + status = sysfs_create_group(&sdev->dev.kobj, &ssam_base_hub_group);
- + if (status)
- + goto err_update;
- +
- + return 0;
- +
- +err_update:
- + ssam_notifier_unregister(sdev->ctrl, &hub->notif);
- + ssam_hub_remove_devices(&sdev->dev);
- +err_register:
- + mutex_destroy(&hub->lock);
- + return status;
- +}
- +
- +static void ssam_base_hub_remove(struct ssam_device *sdev)
- +{
- + struct ssam_base_hub *hub = ssam_device_get_drvdata(sdev);
- +
- + sysfs_remove_group(&sdev->dev.kobj, &ssam_base_hub_group);
- +
- + ssam_notifier_unregister(sdev->ctrl, &hub->notif);
- + ssam_hub_remove_devices(&sdev->dev);
- +
- + mutex_destroy(&hub->lock);
- +}
- +
- +static const struct ssam_device_id ssam_base_hub_match[] = {
- + { SSAM_VDEV(HUB, 0x02, SSAM_ANY_IID, 0x00) },
- + { },
- +};
- +
- +static struct ssam_device_driver ssam_base_hub_driver = {
- + .probe = ssam_base_hub_probe,
- + .remove = ssam_base_hub_remove,
- + .match_table = ssam_base_hub_match,
- + .driver = {
- + .name = "surface_aggregator_base_hub",
- + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- + .pm = &ssam_base_hub_pm_ops,
- + },
- +};
- +
- +
- /* -- SSAM platform/meta-hub driver. ---------------------------------------- */
-
- static const struct acpi_device_id ssam_platform_hub_match[] = {
- @@ -277,7 +511,32 @@ static struct platform_driver ssam_platform_hub_driver = {
- .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- },
- };
- -module_platform_driver(ssam_platform_hub_driver);
- +
- +
- +/* -- Module initialization. ------------------------------------------------ */
- +
- +static int __init ssam_device_hub_init(void)
- +{
- + int status;
- +
- + status = platform_driver_register(&ssam_platform_hub_driver);
- + if (status)
- + return status;
- +
- + status = ssam_device_driver_register(&ssam_base_hub_driver);
- + if (status)
- + platform_driver_unregister(&ssam_platform_hub_driver);
- +
- + return status;
- +}
- +module_init(ssam_device_hub_init);
- +
- +static void __exit ssam_device_hub_exit(void)
- +{
- + ssam_device_driver_unregister(&ssam_base_hub_driver);
- + platform_driver_unregister(&ssam_platform_hub_driver);
- +}
- +module_exit(ssam_device_hub_exit);
-
- MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- MODULE_DESCRIPTION("Device-registry for Surface System Aggregator Module");
- --
- 2.31.0
- From afd3b258a6bdb96e0642babad9707383ae6da152 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Sun, 7 Feb 2021 04:55:09 +0100
- Subject: [PATCH] platform/surface: aggregator_registry: Add battery subsystem
- devices
- Add battery subsystem (TC=0x02) devices (battery and AC) to the SSAM
- device registry. These devices need to be registered for 7th-generation
- Surface models. On 5th- and 6th-generation models, these devices are
- handled via the standard ACPI battery/AC interface, which in turn
- accesses the same SSAM interface via the Surface ACPI Notify (SAN)
- driver.
- Patchset: surface-sam
- ---
- .../surface/surface_aggregator_registry.c | 27 +++++++++++++++++++
- 1 file changed, 27 insertions(+)
- diff --git a/drivers/platform/surface/surface_aggregator_registry.c b/drivers/platform/surface/surface_aggregator_registry.c
- index 6c23d75a044c..cde279692842 100644
- --- a/drivers/platform/surface/surface_aggregator_registry.c
- +++ b/drivers/platform/surface/surface_aggregator_registry.c
- @@ -47,6 +47,24 @@ static const struct software_node ssam_node_hub_base = {
- .parent = &ssam_node_root,
- };
-
- +/* AC adapter. */
- +static const struct software_node ssam_node_bat_ac = {
- + .name = "ssam:01:02:01:01:01",
- + .parent = &ssam_node_root,
- +};
- +
- +/* Primary battery. */
- +static const struct software_node ssam_node_bat_main = {
- + .name = "ssam:01:02:01:01:00",
- + .parent = &ssam_node_root,
- +};
- +
- +/* Secondary battery (Surface Book 3). */
- +static const struct software_node ssam_node_bat_sb3base = {
- + .name = "ssam:01:02:02:01:00",
- + .parent = &ssam_node_hub_base,
- +};
- +
- /* Devices for Surface Book 2. */
- static const struct software_node *ssam_node_group_sb2[] = {
- &ssam_node_root,
- @@ -57,6 +75,9 @@ static const struct software_node *ssam_node_group_sb2[] = {
- static const struct software_node *ssam_node_group_sb3[] = {
- &ssam_node_root,
- &ssam_node_hub_base,
- + &ssam_node_bat_ac,
- + &ssam_node_bat_main,
- + &ssam_node_bat_sb3base,
- NULL,
- };
-
- @@ -75,12 +96,16 @@ static const struct software_node *ssam_node_group_sl2[] = {
- /* Devices for Surface Laptop 3. */
- static const struct software_node *ssam_node_group_sl3[] = {
- &ssam_node_root,
- + &ssam_node_bat_ac,
- + &ssam_node_bat_main,
- NULL,
- };
-
- /* Devices for Surface Laptop Go. */
- static const struct software_node *ssam_node_group_slg1[] = {
- &ssam_node_root,
- + &ssam_node_bat_ac,
- + &ssam_node_bat_main,
- NULL,
- };
-
- @@ -99,6 +124,8 @@ static const struct software_node *ssam_node_group_sp6[] = {
- /* Devices for Surface Pro 7. */
- static const struct software_node *ssam_node_group_sp7[] = {
- &ssam_node_root,
- + &ssam_node_bat_ac,
- + &ssam_node_bat_main,
- NULL,
- };
-
- --
- 2.31.0
- From 0cb17981f08cc3083bc2f84da48f49825f91fb6e Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Sun, 7 Feb 2021 05:01:08 +0100
- Subject: [PATCH] platform/surface: aggregator_registry: Add platform profile
- device
- Add the SSAM platform profile device to the SSAM device registry. This
- device is accessible under the thermal subsystem (TC=0x03) and needs to
- be registered for all Surface models.
- Patchset: surface-sam
- ---
- .../surface/surface_aggregator_registry.c | 15 +++++++++++++++
- 1 file changed, 15 insertions(+)
- diff --git a/drivers/platform/surface/surface_aggregator_registry.c b/drivers/platform/surface/surface_aggregator_registry.c
- index cde279692842..33904613dd4b 100644
- --- a/drivers/platform/surface/surface_aggregator_registry.c
- +++ b/drivers/platform/surface/surface_aggregator_registry.c
- @@ -65,9 +65,16 @@ static const struct software_node ssam_node_bat_sb3base = {
- .parent = &ssam_node_hub_base,
- };
-
- +/* Platform profile / performance-mode device. */
- +static const struct software_node ssam_node_tmp_pprof = {
- + .name = "ssam:01:03:01:00:01",
- + .parent = &ssam_node_root,
- +};
- +
- /* Devices for Surface Book 2. */
- static const struct software_node *ssam_node_group_sb2[] = {
- &ssam_node_root,
- + &ssam_node_tmp_pprof,
- NULL,
- };
-
- @@ -78,18 +85,21 @@ static const struct software_node *ssam_node_group_sb3[] = {
- &ssam_node_bat_ac,
- &ssam_node_bat_main,
- &ssam_node_bat_sb3base,
- + &ssam_node_tmp_pprof,
- NULL,
- };
-
- /* Devices for Surface Laptop 1. */
- static const struct software_node *ssam_node_group_sl1[] = {
- &ssam_node_root,
- + &ssam_node_tmp_pprof,
- NULL,
- };
-
- /* Devices for Surface Laptop 2. */
- static const struct software_node *ssam_node_group_sl2[] = {
- &ssam_node_root,
- + &ssam_node_tmp_pprof,
- NULL,
- };
-
- @@ -98,6 +108,7 @@ static const struct software_node *ssam_node_group_sl3[] = {
- &ssam_node_root,
- &ssam_node_bat_ac,
- &ssam_node_bat_main,
- + &ssam_node_tmp_pprof,
- NULL,
- };
-
- @@ -106,18 +117,21 @@ static const struct software_node *ssam_node_group_slg1[] = {
- &ssam_node_root,
- &ssam_node_bat_ac,
- &ssam_node_bat_main,
- + &ssam_node_tmp_pprof,
- NULL,
- };
-
- /* Devices for Surface Pro 5. */
- static const struct software_node *ssam_node_group_sp5[] = {
- &ssam_node_root,
- + &ssam_node_tmp_pprof,
- NULL,
- };
-
- /* Devices for Surface Pro 6. */
- static const struct software_node *ssam_node_group_sp6[] = {
- &ssam_node_root,
- + &ssam_node_tmp_pprof,
- NULL,
- };
-
- @@ -126,6 +140,7 @@ static const struct software_node *ssam_node_group_sp7[] = {
- &ssam_node_root,
- &ssam_node_bat_ac,
- &ssam_node_bat_main,
- + &ssam_node_tmp_pprof,
- NULL,
- };
-
- --
- 2.31.0
- From c64ca95fc3915aa38881099919d2ef4b65b85ca9 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Sun, 7 Feb 2021 05:06:41 +0100
- Subject: [PATCH] platform/surface: aggregator_registry: Add DTX device
- Add the detachment system (DTX) SSAM device for the Surface Book 3. This
- device is accessible under the base (TC=0x11) subsystem.
- Patchset: surface-sam
- ---
- drivers/platform/surface/surface_aggregator_registry.c | 7 +++++++
- 1 file changed, 7 insertions(+)
- diff --git a/drivers/platform/surface/surface_aggregator_registry.c b/drivers/platform/surface/surface_aggregator_registry.c
- index 33904613dd4b..dc044d06828b 100644
- --- a/drivers/platform/surface/surface_aggregator_registry.c
- +++ b/drivers/platform/surface/surface_aggregator_registry.c
- @@ -71,6 +71,12 @@ static const struct software_node ssam_node_tmp_pprof = {
- .parent = &ssam_node_root,
- };
-
- +/* DTX / detachment-system device (Surface Book 3). */
- +static const struct software_node ssam_node_bas_dtx = {
- + .name = "ssam:01:11:01:00:00",
- + .parent = &ssam_node_root,
- +};
- +
- /* Devices for Surface Book 2. */
- static const struct software_node *ssam_node_group_sb2[] = {
- &ssam_node_root,
- @@ -86,6 +92,7 @@ static const struct software_node *ssam_node_group_sb3[] = {
- &ssam_node_bat_main,
- &ssam_node_bat_sb3base,
- &ssam_node_tmp_pprof,
- + &ssam_node_bas_dtx,
- NULL,
- };
-
- --
- 2.31.0
- From a706c777f1d1632e5c393c8e51aa0f8e3da4c757 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Sun, 7 Feb 2021 05:16:44 +0100
- Subject: [PATCH] platform/surface: aggregator_registry: Add HID subsystem
- devices
- Add HID subsystem (TC=0x15) devices. These devices need to be registered
- for 7th-generation Surface models. On previous generations, these
- devices are either provided as platform devices via ACPI (Surface Laptop
- 1 and 2) or implemented as standard USB device.
- Patchset: surface-sam
- ---
- .../surface/surface_aggregator_registry.c | 49 +++++++++++++++++++
- 1 file changed, 49 insertions(+)
- diff --git a/drivers/platform/surface/surface_aggregator_registry.c b/drivers/platform/surface/surface_aggregator_registry.c
- index dc044d06828b..caee90d135c5 100644
- --- a/drivers/platform/surface/surface_aggregator_registry.c
- +++ b/drivers/platform/surface/surface_aggregator_registry.c
- @@ -77,6 +77,48 @@ static const struct software_node ssam_node_bas_dtx = {
- .parent = &ssam_node_root,
- };
-
- +/* HID keyboard. */
- +static const struct software_node ssam_node_hid_main_keyboard = {
- + .name = "ssam:01:15:02:01:00",
- + .parent = &ssam_node_root,
- +};
- +
- +/* HID touchpad. */
- +static const struct software_node ssam_node_hid_main_touchpad = {
- + .name = "ssam:01:15:02:03:00",
- + .parent = &ssam_node_root,
- +};
- +
- +/* HID device instance 5 (unknown HID device). */
- +static const struct software_node ssam_node_hid_main_iid5 = {
- + .name = "ssam:01:15:02:05:00",
- + .parent = &ssam_node_root,
- +};
- +
- +/* HID keyboard (base hub). */
- +static const struct software_node ssam_node_hid_base_keyboard = {
- + .name = "ssam:01:15:02:01:00",
- + .parent = &ssam_node_hub_base,
- +};
- +
- +/* HID touchpad (base hub). */
- +static const struct software_node ssam_node_hid_base_touchpad = {
- + .name = "ssam:01:15:02:03:00",
- + .parent = &ssam_node_hub_base,
- +};
- +
- +/* HID device instance 5 (unknown HID device, base hub). */
- +static const struct software_node ssam_node_hid_base_iid5 = {
- + .name = "ssam:01:15:02:05:00",
- + .parent = &ssam_node_hub_base,
- +};
- +
- +/* HID device instance 6 (unknown HID device, base hub). */
- +static const struct software_node ssam_node_hid_base_iid6 = {
- + .name = "ssam:01:15:02:06:00",
- + .parent = &ssam_node_hub_base,
- +};
- +
- /* Devices for Surface Book 2. */
- static const struct software_node *ssam_node_group_sb2[] = {
- &ssam_node_root,
- @@ -93,6 +135,10 @@ static const struct software_node *ssam_node_group_sb3[] = {
- &ssam_node_bat_sb3base,
- &ssam_node_tmp_pprof,
- &ssam_node_bas_dtx,
- + &ssam_node_hid_base_keyboard,
- + &ssam_node_hid_base_touchpad,
- + &ssam_node_hid_base_iid5,
- + &ssam_node_hid_base_iid6,
- NULL,
- };
-
- @@ -116,6 +162,9 @@ static const struct software_node *ssam_node_group_sl3[] = {
- &ssam_node_bat_ac,
- &ssam_node_bat_main,
- &ssam_node_tmp_pprof,
- + &ssam_node_hid_main_keyboard,
- + &ssam_node_hid_main_touchpad,
- + &ssam_node_hid_main_iid5,
- NULL,
- };
-
- --
- 2.31.0
- From 8558e1b824cd947857bc04c9a2c96bc3ef2a4475 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Tue, 9 Mar 2021 17:03:15 +0100
- Subject: [PATCH] platform/surface: aggregator_registry: Add support for
- Surface Pro 7+
- The Surface Pro 7+ is essentially a refresh of the Surface Pro 7 with
- updated hardware and a new WSID identifier.
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Patchset: surface-sam
- ---
- drivers/platform/surface/surface_aggregator_registry.c | 5 ++++-
- 1 file changed, 4 insertions(+), 1 deletion(-)
- diff --git a/drivers/platform/surface/surface_aggregator_registry.c b/drivers/platform/surface/surface_aggregator_registry.c
- index caee90d135c5..6de74e893d06 100644
- --- a/drivers/platform/surface/surface_aggregator_registry.c
- +++ b/drivers/platform/surface/surface_aggregator_registry.c
- @@ -191,7 +191,7 @@ static const struct software_node *ssam_node_group_sp6[] = {
- NULL,
- };
-
- -/* Devices for Surface Pro 7. */
- +/* Devices for Surface Pro 7 and Surface Pro 7+. */
- static const struct software_node *ssam_node_group_sp7[] = {
- &ssam_node_root,
- &ssam_node_bat_ac,
- @@ -521,6 +521,9 @@ static const struct acpi_device_id ssam_platform_hub_match[] = {
- /* Surface Pro 7 */
- { "MSHW0116", (unsigned long)ssam_node_group_sp7 },
-
- + /* Surface Pro 7+ */
- + { "MSHW0119", (unsigned long)ssam_node_group_sp7 },
- +
- /* Surface Book 2 */
- { "MSHW0107", (unsigned long)ssam_node_group_sb2 },
-
- --
- 2.31.0
- From 5b6680505958324b1511d5845c51fb880686c2a4 Mon Sep 17 00:00:00 2001
- From: Wei Yongjun <weiyongjun1@huawei.com>
- Date: Tue, 9 Mar 2021 13:15:00 +0000
- Subject: [PATCH] platform/surface: aggregator_registry: Make symbol
- 'ssam_base_hub_group' static
- The sparse tool complains as follows:
- drivers/platform/surface/surface_aggregator_registry.c:355:30: warning:
- symbol 'ssam_base_hub_group' was not declared. Should it be static?
- This symbol is not used outside of surface_aggregator_registry.c, so this
- commit marks it static.
- Fixes: 797e78564634 ("platform/surface: aggregator_registry: Add base device hub")
- Reported-by: Hulk Robot <hulkci@huawei.com>
- Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
- Reviewed-by: Maximilian Luz <luzmaximilian@gmail.com>
- Link: https://lore.kernel.org/r/20210309131500.1885772-1-weiyongjun1@huawei.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- drivers/platform/surface/surface_aggregator_registry.c | 2 +-
- 1 file changed, 1 insertion(+), 1 deletion(-)
- diff --git a/drivers/platform/surface/surface_aggregator_registry.c b/drivers/platform/surface/surface_aggregator_registry.c
- index 6de74e893d06..304d601980ed 100644
- --- a/drivers/platform/surface/surface_aggregator_registry.c
- +++ b/drivers/platform/surface/surface_aggregator_registry.c
- @@ -352,7 +352,7 @@ static struct attribute *ssam_base_hub_attrs[] = {
- NULL,
- };
-
- -const struct attribute_group ssam_base_hub_group = {
- +static const struct attribute_group ssam_base_hub_group = {
- .attrs = ssam_base_hub_attrs,
- };
-
- --
- 2.31.0
- From 329f536e5fe11a7a1c883bad0da1338f7d030c8b Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Tue, 9 Feb 2021 02:46:40 +0100
- Subject: [PATCH] platform/surface: Add DTX driver
- The Microsoft Surface Book series devices consist of a so-called
- clipboard part (containing the CPU, touchscreen, and primary battery)
- and a base part (containing keyboard, secondary battery, and optional
- discrete GPU). These parts can be separated, i.e. the clipboard can be
- detached and used as tablet.
- This detachment process is initiated by pressing a button. On the
- Surface Book 2 and 3 (targeted with this commit), the Surface Aggregator
- Module (i.e. the embedded controller on those devices) attempts to send
- a notification to any listening client driver and waits for further
- instructions (i.e. whether the detachment process should continue or be
- aborted). If it does not receive a response in a certain time-frame, the
- detachment process (by default) continues and the clipboard can be
- physically separated. In other words, (by default and) without a driver,
- the detachment process takes about 10 seconds to complete.
- This commit introduces a driver for this detachment system (called DTX).
- This driver allows a user-space daemon to control and influence the
- detachment behavior. Specifically, it forwards any detachment requests
- to user-space, allows user-space to make such requests itself, and
- allows handling of those requests. Requests can be handled by either
- aborting, continuing/allowing, or delaying (i.e. resetting the timeout
- via a heartbeat commend). The user-space API is implemented via the
- /dev/surface/dtx miscdevice.
- In addition, user-space can change the default behavior on timeout from
- allowing detachment to disallowing it, which is useful if the (optional)
- discrete GPU is in use.
- Furthermore, this driver allows user-space to receive notifications
- about the state of the base, specifically when it is physically removed
- (as opposed to detachment requested), in what manner it is connected
- (i.e. in reverse-/tent-/studio- or laptop-mode), and what type of base
- is connected. Based on this information, the driver also provides a
- simple tablet-mode switch (aliasing all modes without keyboard access,
- i.e. tablet-mode and studio-mode to its reported tablet-mode).
- An implementation of such a user-space daemon, allowing configuration of
- detachment behavior via scripts (e.g. safely unmounting USB devices
- connected to the base before continuing) can be found at [1].
- [1]: https://github.com/linux-surface/surface-dtx-daemon
- Patchset: surface-sam
- ---
- .../userspace-api/ioctl/ioctl-number.rst | 2 +
- MAINTAINERS | 7 +
- drivers/platform/surface/Kconfig | 16 +
- drivers/platform/surface/Makefile | 1 +
- drivers/platform/surface/surface_dtx.c | 1201 +++++++++++++++++
- include/uapi/linux/surface_aggregator/dtx.h | 146 ++
- 6 files changed, 1373 insertions(+)
- create mode 100644 drivers/platform/surface/surface_dtx.c
- create mode 100644 include/uapi/linux/surface_aggregator/dtx.h
- diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst
- index b5231d7f9200..e1dc72a8b62e 100644
- --- a/Documentation/userspace-api/ioctl/ioctl-number.rst
- +++ b/Documentation/userspace-api/ioctl/ioctl-number.rst
- @@ -326,6 +326,8 @@ Code Seq# Include File Comments
- 0xA4 00-1F uapi/asm/sgx.h <mailto:linux-sgx@vger.kernel.org>
- 0xA5 01 linux/surface_aggregator/cdev.h Microsoft Surface Platform System Aggregator
- <mailto:luzmaximilian@gmail.com>
- +0xA5 20-2F linux/surface_aggregator/dtx.h Microsoft Surface DTX driver
- + <mailto:luzmaximilian@gmail.com>
- 0xAA 00-3F linux/uapi/linux/userfaultfd.h
- 0xAB 00-1F linux/nbd.h
- 0xAC 00-1F linux/raw.h
- diff --git a/MAINTAINERS b/MAINTAINERS
- index 1fd2fd35d5b7..1a60e353df38 100644
- --- a/MAINTAINERS
- +++ b/MAINTAINERS
- @@ -11785,6 +11785,13 @@ F: drivers/scsi/smartpqi/smartpqi*.[ch]
- F: include/linux/cciss*.h
- F: include/uapi/linux/cciss*.h
-
- +MICROSOFT SURFACE DTX DRIVER
- +M: Maximilian Luz <luzmaximilian@gmail.com>
- +L: platform-driver-x86@vger.kernel.org
- +S: Maintained
- +F: drivers/platform/surface/surface_dtx.c
- +F: include/uapi/linux/surface_aggregator/dtx.h
- +
- MICROSOFT SURFACE GPE LID SUPPORT DRIVER
- M: Maximilian Luz <luzmaximilian@gmail.com>
- L: platform-driver-x86@vger.kernel.org
- diff --git a/drivers/platform/surface/Kconfig b/drivers/platform/surface/Kconfig
- index 97e08dd35992..745f9d2eb6a7 100644
- --- a/drivers/platform/surface/Kconfig
- +++ b/drivers/platform/surface/Kconfig
- @@ -111,6 +111,22 @@ config SURFACE_BOOK1_DGPU_SWITCH
- This driver provides a sysfs switch to set the power-state of the
- discrete GPU found on the Microsoft Surface Book 1.
-
- +config SURFACE_DTX
- + tristate "Surface DTX (Detachment System) Driver"
- + depends on SURFACE_AGGREGATOR
- + depends on INPUT
- + help
- + Driver for the Surface Book clipboard detachment system (DTX).
- +
- + On the Surface Book series devices, the display part containing the
- + CPU (called the clipboard) can be detached from the base (containing a
- + battery, the keyboard, and, optionally, a discrete GPU) by (if
- + necessary) unlocking and opening the latch connecting both parts.
- +
- + This driver provides a user-space interface that can influence the
- + behavior of this process, which includes the option to abort it in
- + case the base is still in use or speed it up in case it is not.
- +
- config SURFACE_GPE
- tristate "Surface GPE/Lid Support Driver"
- depends on DMI
- diff --git a/drivers/platform/surface/Makefile b/drivers/platform/surface/Makefile
- index 30a212aefd35..19b661e274c3 100644
- --- a/drivers/platform/surface/Makefile
- +++ b/drivers/platform/surface/Makefile
- @@ -12,5 +12,6 @@ obj-$(CONFIG_SURFACE_AGGREGATOR) += aggregator/
- obj-$(CONFIG_SURFACE_AGGREGATOR_CDEV) += surface_aggregator_cdev.o
- obj-$(CONFIG_SURFACE_AGGREGATOR_REGISTRY) += surface_aggregator_registry.o
- obj-$(CONFIG_SURFACE_BOOK1_DGPU_SWITCH) += surfacebook1_dgpu_switch.o
- +obj-$(CONFIG_SURFACE_DTX) += surface_dtx.o
- obj-$(CONFIG_SURFACE_GPE) += surface_gpe.o
- obj-$(CONFIG_SURFACE_PRO3_BUTTON) += surfacepro3_button.o
- diff --git a/drivers/platform/surface/surface_dtx.c b/drivers/platform/surface/surface_dtx.c
- new file mode 100644
- index 000000000000..a95adc1094aa
- --- /dev/null
- +++ b/drivers/platform/surface/surface_dtx.c
- @@ -0,0 +1,1201 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * Surface Book (gen. 2 and later) detachment system (DTX) driver.
- + *
- + * Provides a user-space interface to properly handle clipboard/tablet
- + * (containing screen and processor) detachment from the base of the device
- + * (containing the keyboard and optionally a discrete GPU). Allows to
- + * acknowledge (to speed things up), abort (e.g. in case the dGPU is still in
- + * use), or request detachment via user-space.
- + *
- + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <linux/fs.h>
- +#include <linux/input.h>
- +#include <linux/ioctl.h>
- +#include <linux/kernel.h>
- +#include <linux/kfifo.h>
- +#include <linux/kref.h>
- +#include <linux/miscdevice.h>
- +#include <linux/module.h>
- +#include <linux/mutex.h>
- +#include <linux/platform_device.h>
- +#include <linux/poll.h>
- +#include <linux/rwsem.h>
- +#include <linux/slab.h>
- +#include <linux/workqueue.h>
- +
- +#include <linux/surface_aggregator/controller.h>
- +#include <linux/surface_aggregator/dtx.h>
- +
- +
- +/* -- SSAM interface. ------------------------------------------------------- */
- +
- +enum sam_event_cid_bas {
- + SAM_EVENT_CID_DTX_CONNECTION = 0x0c,
- + SAM_EVENT_CID_DTX_REQUEST = 0x0e,
- + SAM_EVENT_CID_DTX_CANCEL = 0x0f,
- + SAM_EVENT_CID_DTX_LATCH_STATUS = 0x11,
- +};
- +
- +enum ssam_bas_base_state {
- + SSAM_BAS_BASE_STATE_DETACH_SUCCESS = 0x00,
- + SSAM_BAS_BASE_STATE_ATTACHED = 0x01,
- + SSAM_BAS_BASE_STATE_NOT_FEASIBLE = 0x02,
- +};
- +
- +enum ssam_bas_latch_status {
- + SSAM_BAS_LATCH_STATUS_CLOSED = 0x00,
- + SSAM_BAS_LATCH_STATUS_OPENED = 0x01,
- + SSAM_BAS_LATCH_STATUS_FAILED_TO_OPEN = 0x02,
- + SSAM_BAS_LATCH_STATUS_FAILED_TO_REMAIN_OPEN = 0x03,
- + SSAM_BAS_LATCH_STATUS_FAILED_TO_CLOSE = 0x04,
- +};
- +
- +enum ssam_bas_cancel_reason {
- + SSAM_BAS_CANCEL_REASON_NOT_FEASIBLE = 0x00, /* Low battery. */
- + SSAM_BAS_CANCEL_REASON_TIMEOUT = 0x02,
- + SSAM_BAS_CANCEL_REASON_FAILED_TO_OPEN = 0x03,
- + SSAM_BAS_CANCEL_REASON_FAILED_TO_REMAIN_OPEN = 0x04,
- + SSAM_BAS_CANCEL_REASON_FAILED_TO_CLOSE = 0x05,
- +};
- +
- +struct ssam_bas_base_info {
- + u8 state;
- + u8 base_id;
- +} __packed;
- +
- +static_assert(sizeof(struct ssam_bas_base_info) == 2);
- +
- +static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_lock, {
- + .target_category = SSAM_SSH_TC_BAS,
- + .target_id = 0x01,
- + .command_id = 0x06,
- + .instance_id = 0x00,
- +});
- +
- +static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_unlock, {
- + .target_category = SSAM_SSH_TC_BAS,
- + .target_id = 0x01,
- + .command_id = 0x07,
- + .instance_id = 0x00,
- +});
- +
- +static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_request, {
- + .target_category = SSAM_SSH_TC_BAS,
- + .target_id = 0x01,
- + .command_id = 0x08,
- + .instance_id = 0x00,
- +});
- +
- +static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_confirm, {
- + .target_category = SSAM_SSH_TC_BAS,
- + .target_id = 0x01,
- + .command_id = 0x09,
- + .instance_id = 0x00,
- +});
- +
- +static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_heartbeat, {
- + .target_category = SSAM_SSH_TC_BAS,
- + .target_id = 0x01,
- + .command_id = 0x0a,
- + .instance_id = 0x00,
- +});
- +
- +static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_cancel, {
- + .target_category = SSAM_SSH_TC_BAS,
- + .target_id = 0x01,
- + .command_id = 0x0b,
- + .instance_id = 0x00,
- +});
- +
- +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_base, struct ssam_bas_base_info, {
- + .target_category = SSAM_SSH_TC_BAS,
- + .target_id = 0x01,
- + .command_id = 0x0c,
- + .instance_id = 0x00,
- +});
- +
- +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_device_mode, u8, {
- + .target_category = SSAM_SSH_TC_BAS,
- + .target_id = 0x01,
- + .command_id = 0x0d,
- + .instance_id = 0x00,
- +});
- +
- +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_latch_status, u8, {
- + .target_category = SSAM_SSH_TC_BAS,
- + .target_id = 0x01,
- + .command_id = 0x11,
- + .instance_id = 0x00,
- +});
- +
- +
- +/* -- Main structures. ------------------------------------------------------ */
- +
- +enum sdtx_device_state {
- + SDTX_DEVICE_SHUTDOWN_BIT = BIT(0),
- + SDTX_DEVICE_DIRTY_BASE_BIT = BIT(1),
- + SDTX_DEVICE_DIRTY_MODE_BIT = BIT(2),
- + SDTX_DEVICE_DIRTY_LATCH_BIT = BIT(3),
- +};
- +
- +struct sdtx_device {
- + struct kref kref;
- + struct rw_semaphore lock; /* Guards device and controller reference. */
- +
- + struct device *dev;
- + struct ssam_controller *ctrl;
- + unsigned long flags;
- +
- + struct miscdevice mdev;
- + wait_queue_head_t waitq;
- + struct mutex write_lock; /* Guards order of events/notifications. */
- + struct rw_semaphore client_lock; /* Guards client list. */
- + struct list_head client_list;
- +
- + struct delayed_work state_work;
- + struct {
- + struct ssam_bas_base_info base;
- + u8 device_mode;
- + u8 latch_status;
- + } state;
- +
- + struct delayed_work mode_work;
- + struct input_dev *mode_switch;
- +
- + struct ssam_event_notifier notif;
- +};
- +
- +enum sdtx_client_state {
- + SDTX_CLIENT_EVENTS_ENABLED_BIT = BIT(0),
- +};
- +
- +struct sdtx_client {
- + struct sdtx_device *ddev;
- + struct list_head node;
- + unsigned long flags;
- +
- + struct fasync_struct *fasync;
- +
- + struct mutex read_lock; /* Guards FIFO buffer read access. */
- + DECLARE_KFIFO(buffer, u8, 512);
- +};
- +
- +static void __sdtx_device_release(struct kref *kref)
- +{
- + struct sdtx_device *ddev = container_of(kref, struct sdtx_device, kref);
- +
- + mutex_destroy(&ddev->write_lock);
- + kfree(ddev);
- +}
- +
- +static struct sdtx_device *sdtx_device_get(struct sdtx_device *ddev)
- +{
- + if (ddev)
- + kref_get(&ddev->kref);
- +
- + return ddev;
- +}
- +
- +static void sdtx_device_put(struct sdtx_device *ddev)
- +{
- + if (ddev)
- + kref_put(&ddev->kref, __sdtx_device_release);
- +}
- +
- +
- +/* -- Firmware value translations. ------------------------------------------ */
- +
- +static u16 sdtx_translate_base_state(struct sdtx_device *ddev, u8 state)
- +{
- + switch (state) {
- + case SSAM_BAS_BASE_STATE_ATTACHED:
- + return SDTX_BASE_ATTACHED;
- +
- + case SSAM_BAS_BASE_STATE_DETACH_SUCCESS:
- + return SDTX_BASE_DETACHED;
- +
- + case SSAM_BAS_BASE_STATE_NOT_FEASIBLE:
- + return SDTX_DETACH_NOT_FEASIBLE;
- +
- + default:
- + dev_err(ddev->dev, "unknown base state: %#04x\n", state);
- + return SDTX_UNKNOWN(state);
- + }
- +}
- +
- +static u16 sdtx_translate_latch_status(struct sdtx_device *ddev, u8 status)
- +{
- + switch (status) {
- + case SSAM_BAS_LATCH_STATUS_CLOSED:
- + return SDTX_LATCH_CLOSED;
- +
- + case SSAM_BAS_LATCH_STATUS_OPENED:
- + return SDTX_LATCH_OPENED;
- +
- + case SSAM_BAS_LATCH_STATUS_FAILED_TO_OPEN:
- + return SDTX_ERR_FAILED_TO_OPEN;
- +
- + case SSAM_BAS_LATCH_STATUS_FAILED_TO_REMAIN_OPEN:
- + return SDTX_ERR_FAILED_TO_REMAIN_OPEN;
- +
- + case SSAM_BAS_LATCH_STATUS_FAILED_TO_CLOSE:
- + return SDTX_ERR_FAILED_TO_CLOSE;
- +
- + default:
- + dev_err(ddev->dev, "unknown latch status: %#04x\n", status);
- + return SDTX_UNKNOWN(status);
- + }
- +}
- +
- +static u16 sdtx_translate_cancel_reason(struct sdtx_device *ddev, u8 reason)
- +{
- + switch (reason) {
- + case SSAM_BAS_CANCEL_REASON_NOT_FEASIBLE:
- + return SDTX_DETACH_NOT_FEASIBLE;
- +
- + case SSAM_BAS_CANCEL_REASON_TIMEOUT:
- + return SDTX_DETACH_TIMEDOUT;
- +
- + case SSAM_BAS_CANCEL_REASON_FAILED_TO_OPEN:
- + return SDTX_ERR_FAILED_TO_OPEN;
- +
- + case SSAM_BAS_CANCEL_REASON_FAILED_TO_REMAIN_OPEN:
- + return SDTX_ERR_FAILED_TO_REMAIN_OPEN;
- +
- + case SSAM_BAS_CANCEL_REASON_FAILED_TO_CLOSE:
- + return SDTX_ERR_FAILED_TO_CLOSE;
- +
- + default:
- + dev_err(ddev->dev, "unknown cancel reason: %#04x\n", reason);
- + return SDTX_UNKNOWN(reason);
- + }
- +}
- +
- +
- +/* -- IOCTLs. --------------------------------------------------------------- */
- +
- +static int sdtx_ioctl_get_base_info(struct sdtx_device *ddev,
- + struct sdtx_base_info __user *buf)
- +{
- + struct ssam_bas_base_info raw;
- + struct sdtx_base_info info;
- + int status;
- +
- + lockdep_assert_held_read(&ddev->lock);
- +
- + status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &raw);
- + if (status < 0)
- + return status;
- +
- + info.state = sdtx_translate_base_state(ddev, raw.state);
- + info.base_id = SDTX_BASE_TYPE_SSH(raw.base_id);
- +
- + if (copy_to_user(buf, &info, sizeof(info)))
- + return -EFAULT;
- +
- + return 0;
- +}
- +
- +static int sdtx_ioctl_get_device_mode(struct sdtx_device *ddev, u16 __user *buf)
- +{
- + u8 mode;
- + int status;
- +
- + lockdep_assert_held_read(&ddev->lock);
- +
- + status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &mode);
- + if (status < 0)
- + return status;
- +
- + return put_user(mode, buf);
- +}
- +
- +static int sdtx_ioctl_get_latch_status(struct sdtx_device *ddev, u16 __user *buf)
- +{
- + u8 latch;
- + int status;
- +
- + lockdep_assert_held_read(&ddev->lock);
- +
- + status = ssam_retry(ssam_bas_get_latch_status, ddev->ctrl, &latch);
- + if (status < 0)
- + return status;
- +
- + return put_user(sdtx_translate_latch_status(ddev, latch), buf);
- +}
- +
- +static long __surface_dtx_ioctl(struct sdtx_client *client, unsigned int cmd, unsigned long arg)
- +{
- + struct sdtx_device *ddev = client->ddev;
- +
- + lockdep_assert_held_read(&ddev->lock);
- +
- + switch (cmd) {
- + case SDTX_IOCTL_EVENTS_ENABLE:
- + set_bit(SDTX_CLIENT_EVENTS_ENABLED_BIT, &client->flags);
- + return 0;
- +
- + case SDTX_IOCTL_EVENTS_DISABLE:
- + clear_bit(SDTX_CLIENT_EVENTS_ENABLED_BIT, &client->flags);
- + return 0;
- +
- + case SDTX_IOCTL_LATCH_LOCK:
- + return ssam_retry(ssam_bas_latch_lock, ddev->ctrl);
- +
- + case SDTX_IOCTL_LATCH_UNLOCK:
- + return ssam_retry(ssam_bas_latch_unlock, ddev->ctrl);
- +
- + case SDTX_IOCTL_LATCH_REQUEST:
- + return ssam_retry(ssam_bas_latch_request, ddev->ctrl);
- +
- + case SDTX_IOCTL_LATCH_CONFIRM:
- + return ssam_retry(ssam_bas_latch_confirm, ddev->ctrl);
- +
- + case SDTX_IOCTL_LATCH_HEARTBEAT:
- + return ssam_retry(ssam_bas_latch_heartbeat, ddev->ctrl);
- +
- + case SDTX_IOCTL_LATCH_CANCEL:
- + return ssam_retry(ssam_bas_latch_cancel, ddev->ctrl);
- +
- + case SDTX_IOCTL_GET_BASE_INFO:
- + return sdtx_ioctl_get_base_info(ddev, (struct sdtx_base_info __user *)arg);
- +
- + case SDTX_IOCTL_GET_DEVICE_MODE:
- + return sdtx_ioctl_get_device_mode(ddev, (u16 __user *)arg);
- +
- + case SDTX_IOCTL_GET_LATCH_STATUS:
- + return sdtx_ioctl_get_latch_status(ddev, (u16 __user *)arg);
- +
- + default:
- + return -EINVAL;
- + }
- +}
- +
- +static long surface_dtx_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
- +{
- + struct sdtx_client *client = file->private_data;
- + long status;
- +
- + if (down_read_killable(&client->ddev->lock))
- + return -ERESTARTSYS;
- +
- + if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &client->ddev->flags)) {
- + up_read(&client->ddev->lock);
- + return -ENODEV;
- + }
- +
- + status = __surface_dtx_ioctl(client, cmd, arg);
- +
- + up_read(&client->ddev->lock);
- + return status;
- +}
- +
- +
- +/* -- File operations. ------------------------------------------------------ */
- +
- +static int surface_dtx_open(struct inode *inode, struct file *file)
- +{
- + struct sdtx_device *ddev = container_of(file->private_data, struct sdtx_device, mdev);
- + struct sdtx_client *client;
- +
- + /* Initialize client. */
- + client = kzalloc(sizeof(*client), GFP_KERNEL);
- + if (!client)
- + return -ENOMEM;
- +
- + client->ddev = sdtx_device_get(ddev);
- +
- + INIT_LIST_HEAD(&client->node);
- +
- + mutex_init(&client->read_lock);
- + INIT_KFIFO(client->buffer);
- +
- + file->private_data = client;
- +
- + /* Attach client. */
- + down_write(&ddev->client_lock);
- +
- + /*
- + * Do not add a new client if the device has been shut down. Note that
- + * it's enough to hold the client_lock here as, during shutdown, we
- + * only acquire that lock and remove clients after marking the device
- + * as shut down.
- + */
- + if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags)) {
- + up_write(&ddev->client_lock);
- + sdtx_device_put(client->ddev);
- + kfree(client);
- + return -ENODEV;
- + }
- +
- + list_add_tail(&client->node, &ddev->client_list);
- + up_write(&ddev->client_lock);
- +
- + stream_open(inode, file);
- + return 0;
- +}
- +
- +static int surface_dtx_release(struct inode *inode, struct file *file)
- +{
- + struct sdtx_client *client = file->private_data;
- +
- + /* Detach client. */
- + down_write(&client->ddev->client_lock);
- + list_del(&client->node);
- + up_write(&client->ddev->client_lock);
- +
- + /* Free client. */
- + sdtx_device_put(client->ddev);
- + mutex_destroy(&client->read_lock);
- + kfree(client);
- +
- + return 0;
- +}
- +
- +static ssize_t surface_dtx_read(struct file *file, char __user *buf, size_t count, loff_t *offs)
- +{
- + struct sdtx_client *client = file->private_data;
- + struct sdtx_device *ddev = client->ddev;
- + unsigned int copied;
- + int status = 0;
- +
- + if (down_read_killable(&ddev->lock))
- + return -ERESTARTSYS;
- +
- + /* Make sure we're not shut down. */
- + if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags)) {
- + up_read(&ddev->lock);
- + return -ENODEV;
- + }
- +
- + do {
- + /* Check availability, wait if necessary. */
- + if (kfifo_is_empty(&client->buffer)) {
- + up_read(&ddev->lock);
- +
- + if (file->f_flags & O_NONBLOCK)
- + return -EAGAIN;
- +
- + status = wait_event_interruptible(ddev->waitq,
- + !kfifo_is_empty(&client->buffer) ||
- + test_bit(SDTX_DEVICE_SHUTDOWN_BIT,
- + &ddev->flags));
- + if (status < 0)
- + return status;
- +
- + if (down_read_killable(&client->ddev->lock))
- + return -ERESTARTSYS;
- +
- + /* Need to check that we're not shut down again. */
- + if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags)) {
- + up_read(&ddev->lock);
- + return -ENODEV;
- + }
- + }
- +
- + /* Try to read from FIFO. */
- + if (mutex_lock_interruptible(&client->read_lock)) {
- + up_read(&ddev->lock);
- + return -ERESTARTSYS;
- + }
- +
- + status = kfifo_to_user(&client->buffer, buf, count, &copied);
- + mutex_unlock(&client->read_lock);
- +
- + if (status < 0) {
- + up_read(&ddev->lock);
- + return status;
- + }
- +
- + /* We might not have gotten anything, check this here. */
- + if (copied == 0 && (file->f_flags & O_NONBLOCK)) {
- + up_read(&ddev->lock);
- + return -EAGAIN;
- + }
- + } while (copied == 0);
- +
- + up_read(&ddev->lock);
- + return copied;
- +}
- +
- +static __poll_t surface_dtx_poll(struct file *file, struct poll_table_struct *pt)
- +{
- + struct sdtx_client *client = file->private_data;
- + __poll_t events = 0;
- +
- + if (down_read_killable(&client->ddev->lock))
- + return -ERESTARTSYS;
- +
- + if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &client->ddev->flags)) {
- + up_read(&client->ddev->lock);
- + return EPOLLHUP | EPOLLERR;
- + }
- +
- + poll_wait(file, &client->ddev->waitq, pt);
- +
- + if (!kfifo_is_empty(&client->buffer))
- + events |= EPOLLIN | EPOLLRDNORM;
- +
- + up_read(&client->ddev->lock);
- + return events;
- +}
- +
- +static int surface_dtx_fasync(int fd, struct file *file, int on)
- +{
- + struct sdtx_client *client = file->private_data;
- +
- + return fasync_helper(fd, file, on, &client->fasync);
- +}
- +
- +static const struct file_operations surface_dtx_fops = {
- + .owner = THIS_MODULE,
- + .open = surface_dtx_open,
- + .release = surface_dtx_release,
- + .read = surface_dtx_read,
- + .poll = surface_dtx_poll,
- + .fasync = surface_dtx_fasync,
- + .unlocked_ioctl = surface_dtx_ioctl,
- + .compat_ioctl = surface_dtx_ioctl,
- + .llseek = no_llseek,
- +};
- +
- +
- +/* -- Event handling/forwarding. -------------------------------------------- */
- +
- +/*
- + * The device operation mode is not immediately updated on the EC when the
- + * base has been connected, i.e. querying the device mode inside the
- + * connection event callback yields an outdated value. Thus, we can only
- + * determine the new tablet-mode switch and device mode values after some
- + * time.
- + *
- + * These delays have been chosen by experimenting. We first delay on connect
- + * events, then check and validate the device mode against the base state and
- + * if invalid delay again by the "recheck" delay.
- + */
- +#define SDTX_DEVICE_MODE_DELAY_CONNECT msecs_to_jiffies(100)
- +#define SDTX_DEVICE_MODE_DELAY_RECHECK msecs_to_jiffies(100)
- +
- +struct sdtx_status_event {
- + struct sdtx_event e;
- + __u16 v;
- +} __packed;
- +
- +struct sdtx_base_info_event {
- + struct sdtx_event e;
- + struct sdtx_base_info v;
- +} __packed;
- +
- +union sdtx_generic_event {
- + struct sdtx_event common;
- + struct sdtx_status_event status;
- + struct sdtx_base_info_event base;
- +};
- +
- +static void sdtx_update_device_mode(struct sdtx_device *ddev, unsigned long delay);
- +
- +/* Must be executed with ddev->write_lock held. */
- +static void sdtx_push_event(struct sdtx_device *ddev, struct sdtx_event *evt)
- +{
- + const size_t len = sizeof(struct sdtx_event) + evt->length;
- + struct sdtx_client *client;
- +
- + lockdep_assert_held(&ddev->write_lock);
- +
- + down_read(&ddev->client_lock);
- + list_for_each_entry(client, &ddev->client_list, node) {
- + if (!test_bit(SDTX_CLIENT_EVENTS_ENABLED_BIT, &client->flags))
- + continue;
- +
- + if (likely(kfifo_avail(&client->buffer) >= len))
- + kfifo_in(&client->buffer, (const u8 *)evt, len);
- + else
- + dev_warn(ddev->dev, "event buffer overrun\n");
- +
- + kill_fasync(&client->fasync, SIGIO, POLL_IN);
- + }
- + up_read(&ddev->client_lock);
- +
- + wake_up_interruptible(&ddev->waitq);
- +}
- +
- +static u32 sdtx_notifier(struct ssam_event_notifier *nf, const struct ssam_event *in)
- +{
- + struct sdtx_device *ddev = container_of(nf, struct sdtx_device, notif);
- + union sdtx_generic_event event;
- + size_t len;
- +
- + /* Validate event payload length. */
- + switch (in->command_id) {
- + case SAM_EVENT_CID_DTX_CONNECTION:
- + len = 2 * sizeof(u8);
- + break;
- +
- + case SAM_EVENT_CID_DTX_REQUEST:
- + len = 0;
- + break;
- +
- + case SAM_EVENT_CID_DTX_CANCEL:
- + len = sizeof(u8);
- + break;
- +
- + case SAM_EVENT_CID_DTX_LATCH_STATUS:
- + len = sizeof(u8);
- + break;
- +
- + default:
- + return 0;
- + };
- +
- + if (in->length != len) {
- + dev_err(ddev->dev,
- + "unexpected payload size for event %#04x: got %u, expected %zu\n",
- + in->command_id, in->length, len);
- + return 0;
- + }
- +
- + mutex_lock(&ddev->write_lock);
- +
- + /* Translate event. */
- + switch (in->command_id) {
- + case SAM_EVENT_CID_DTX_CONNECTION:
- + clear_bit(SDTX_DEVICE_DIRTY_BASE_BIT, &ddev->flags);
- +
- + /* If state has not changed: do not send new event. */
- + if (ddev->state.base.state == in->data[0] &&
- + ddev->state.base.base_id == in->data[1])
- + goto out;
- +
- + ddev->state.base.state = in->data[0];
- + ddev->state.base.base_id = in->data[1];
- +
- + event.base.e.length = sizeof(struct sdtx_base_info);
- + event.base.e.code = SDTX_EVENT_BASE_CONNECTION;
- + event.base.v.state = sdtx_translate_base_state(ddev, in->data[0]);
- + event.base.v.base_id = SDTX_BASE_TYPE_SSH(in->data[1]);
- + break;
- +
- + case SAM_EVENT_CID_DTX_REQUEST:
- + event.common.code = SDTX_EVENT_REQUEST;
- + event.common.length = 0;
- + break;
- +
- + case SAM_EVENT_CID_DTX_CANCEL:
- + event.status.e.length = sizeof(u16);
- + event.status.e.code = SDTX_EVENT_CANCEL;
- + event.status.v = sdtx_translate_cancel_reason(ddev, in->data[0]);
- + break;
- +
- + case SAM_EVENT_CID_DTX_LATCH_STATUS:
- + clear_bit(SDTX_DEVICE_DIRTY_LATCH_BIT, &ddev->flags);
- +
- + /* If state has not changed: do not send new event. */
- + if (ddev->state.latch_status == in->data[0])
- + goto out;
- +
- + ddev->state.latch_status = in->data[0];
- +
- + event.status.e.length = sizeof(u16);
- + event.status.e.code = SDTX_EVENT_LATCH_STATUS;
- + event.status.v = sdtx_translate_latch_status(ddev, in->data[0]);
- + break;
- + }
- +
- + sdtx_push_event(ddev, &event.common);
- +
- + /* Update device mode on base connection change. */
- + if (in->command_id == SAM_EVENT_CID_DTX_CONNECTION) {
- + unsigned long delay;
- +
- + delay = in->data[0] ? SDTX_DEVICE_MODE_DELAY_CONNECT : 0;
- + sdtx_update_device_mode(ddev, delay);
- + }
- +
- +out:
- + mutex_unlock(&ddev->write_lock);
- + return SSAM_NOTIF_HANDLED;
- +}
- +
- +
- +/* -- State update functions. ----------------------------------------------- */
- +
- +static bool sdtx_device_mode_invalid(u8 mode, u8 base_state)
- +{
- + return ((base_state == SSAM_BAS_BASE_STATE_ATTACHED) &&
- + (mode == SDTX_DEVICE_MODE_TABLET)) ||
- + ((base_state == SSAM_BAS_BASE_STATE_DETACH_SUCCESS) &&
- + (mode != SDTX_DEVICE_MODE_TABLET));
- +}
- +
- +static void sdtx_device_mode_workfn(struct work_struct *work)
- +{
- + struct sdtx_device *ddev = container_of(work, struct sdtx_device, mode_work.work);
- + struct sdtx_status_event event;
- + struct ssam_bas_base_info base;
- + int status, tablet;
- + u8 mode;
- +
- + /* Get operation mode. */
- + status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &mode);
- + if (status) {
- + dev_err(ddev->dev, "failed to get device mode: %d\n", status);
- + return;
- + }
- +
- + /* Get base info. */
- + status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &base);
- + if (status) {
- + dev_err(ddev->dev, "failed to get base info: %d\n", status);
- + return;
- + }
- +
- + /*
- + * In some cases (specifically when attaching the base), the device
- + * mode isn't updated right away. Thus we check if the device mode
- + * makes sense for the given base state and try again later if it
- + * doesn't.
- + */
- + if (sdtx_device_mode_invalid(mode, base.state)) {
- + dev_dbg(ddev->dev, "device mode is invalid, trying again\n");
- + sdtx_update_device_mode(ddev, SDTX_DEVICE_MODE_DELAY_RECHECK);
- + return;
- + }
- +
- + mutex_lock(&ddev->write_lock);
- + clear_bit(SDTX_DEVICE_DIRTY_MODE_BIT, &ddev->flags);
- +
- + /* Avoid sending duplicate device-mode events. */
- + if (ddev->state.device_mode == mode) {
- + mutex_unlock(&ddev->write_lock);
- + return;
- + }
- +
- + ddev->state.device_mode = mode;
- +
- + event.e.length = sizeof(u16);
- + event.e.code = SDTX_EVENT_DEVICE_MODE;
- + event.v = mode;
- +
- + sdtx_push_event(ddev, &event.e);
- +
- + /* Send SW_TABLET_MODE event. */
- + tablet = mode != SDTX_DEVICE_MODE_LAPTOP;
- + input_report_switch(ddev->mode_switch, SW_TABLET_MODE, tablet);
- + input_sync(ddev->mode_switch);
- +
- + mutex_unlock(&ddev->write_lock);
- +}
- +
- +static void sdtx_update_device_mode(struct sdtx_device *ddev, unsigned long delay)
- +{
- + schedule_delayed_work(&ddev->mode_work, delay);
- +}
- +
- +/* Must be executed with ddev->write_lock held. */
- +static void __sdtx_device_state_update_base(struct sdtx_device *ddev,
- + struct ssam_bas_base_info info)
- +{
- + struct sdtx_base_info_event event;
- +
- + lockdep_assert_held(&ddev->write_lock);
- +
- + /* Prevent duplicate events. */
- + if (ddev->state.base.state == info.state &&
- + ddev->state.base.base_id == info.base_id)
- + return;
- +
- + ddev->state.base = info;
- +
- + event.e.length = sizeof(struct sdtx_base_info);
- + event.e.code = SDTX_EVENT_BASE_CONNECTION;
- + event.v.state = sdtx_translate_base_state(ddev, info.state);
- + event.v.base_id = SDTX_BASE_TYPE_SSH(info.base_id);
- +
- + sdtx_push_event(ddev, &event.e);
- +}
- +
- +/* Must be executed with ddev->write_lock held. */
- +static void __sdtx_device_state_update_mode(struct sdtx_device *ddev, u8 mode)
- +{
- + struct sdtx_status_event event;
- + int tablet;
- +
- + /*
- + * Note: This function must be called after updating the base state
- + * via __sdtx_device_state_update_base(), as we rely on the updated
- + * base state value in the validity check below.
- + */
- +
- + lockdep_assert_held(&ddev->write_lock);
- +
- + if (sdtx_device_mode_invalid(mode, ddev->state.base.state)) {
- + dev_dbg(ddev->dev, "device mode is invalid, trying again\n");
- + sdtx_update_device_mode(ddev, SDTX_DEVICE_MODE_DELAY_RECHECK);
- + return;
- + }
- +
- + /* Prevent duplicate events. */
- + if (ddev->state.device_mode == mode)
- + return;
- +
- + ddev->state.device_mode = mode;
- +
- + /* Send event. */
- + event.e.length = sizeof(u16);
- + event.e.code = SDTX_EVENT_DEVICE_MODE;
- + event.v = mode;
- +
- + sdtx_push_event(ddev, &event.e);
- +
- + /* Send SW_TABLET_MODE event. */
- + tablet = mode != SDTX_DEVICE_MODE_LAPTOP;
- + input_report_switch(ddev->mode_switch, SW_TABLET_MODE, tablet);
- + input_sync(ddev->mode_switch);
- +}
- +
- +/* Must be executed with ddev->write_lock held. */
- +static void __sdtx_device_state_update_latch(struct sdtx_device *ddev, u8 status)
- +{
- + struct sdtx_status_event event;
- +
- + lockdep_assert_held(&ddev->write_lock);
- +
- + /* Prevent duplicate events. */
- + if (ddev->state.latch_status == status)
- + return;
- +
- + ddev->state.latch_status = status;
- +
- + event.e.length = sizeof(struct sdtx_base_info);
- + event.e.code = SDTX_EVENT_BASE_CONNECTION;
- + event.v = sdtx_translate_latch_status(ddev, status);
- +
- + sdtx_push_event(ddev, &event.e);
- +}
- +
- +static void sdtx_device_state_workfn(struct work_struct *work)
- +{
- + struct sdtx_device *ddev = container_of(work, struct sdtx_device, state_work.work);
- + struct ssam_bas_base_info base;
- + u8 mode, latch;
- + int status;
- +
- + /* Mark everything as dirty. */
- + set_bit(SDTX_DEVICE_DIRTY_BASE_BIT, &ddev->flags);
- + set_bit(SDTX_DEVICE_DIRTY_MODE_BIT, &ddev->flags);
- + set_bit(SDTX_DEVICE_DIRTY_LATCH_BIT, &ddev->flags);
- +
- + /*
- + * Ensure that the state gets marked as dirty before continuing to
- + * query it. Necessary to ensure that clear_bit() calls in
- + * sdtx_notifier() and sdtx_device_mode_workfn() actually clear these
- + * bits if an event is received while updating the state here.
- + */
- + smp_mb__after_atomic();
- +
- + status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &base);
- + if (status) {
- + dev_err(ddev->dev, "failed to get base state: %d\n", status);
- + return;
- + }
- +
- + status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &mode);
- + if (status) {
- + dev_err(ddev->dev, "failed to get device mode: %d\n", status);
- + return;
- + }
- +
- + status = ssam_retry(ssam_bas_get_latch_status, ddev->ctrl, &latch);
- + if (status) {
- + dev_err(ddev->dev, "failed to get latch status: %d\n", status);
- + return;
- + }
- +
- + mutex_lock(&ddev->write_lock);
- +
- + /*
- + * If the respective dirty-bit has been cleared, an event has been
- + * received, updating this state. The queried state may thus be out of
- + * date. At this point, we can safely assume that the state provided
- + * by the event is either up to date, or we're about to receive
- + * another event updating it.
- + */
- +
- + if (test_and_clear_bit(SDTX_DEVICE_DIRTY_BASE_BIT, &ddev->flags))
- + __sdtx_device_state_update_base(ddev, base);
- +
- + if (test_and_clear_bit(SDTX_DEVICE_DIRTY_MODE_BIT, &ddev->flags))
- + __sdtx_device_state_update_mode(ddev, mode);
- +
- + if (test_and_clear_bit(SDTX_DEVICE_DIRTY_LATCH_BIT, &ddev->flags))
- + __sdtx_device_state_update_latch(ddev, latch);
- +
- + mutex_unlock(&ddev->write_lock);
- +}
- +
- +static void sdtx_update_device_state(struct sdtx_device *ddev, unsigned long delay)
- +{
- + schedule_delayed_work(&ddev->state_work, delay);
- +}
- +
- +
- +/* -- Common device initialization. ----------------------------------------- */
- +
- +static int sdtx_device_init(struct sdtx_device *ddev, struct device *dev,
- + struct ssam_controller *ctrl)
- +{
- + int status, tablet_mode;
- +
- + /* Basic initialization. */
- + kref_init(&ddev->kref);
- + init_rwsem(&ddev->lock);
- + ddev->dev = dev;
- + ddev->ctrl = ctrl;
- +
- + ddev->mdev.minor = MISC_DYNAMIC_MINOR;
- + ddev->mdev.name = "surface_dtx";
- + ddev->mdev.nodename = "surface/dtx";
- + ddev->mdev.fops = &surface_dtx_fops;
- +
- + ddev->notif.base.priority = 1;
- + ddev->notif.base.fn = sdtx_notifier;
- + ddev->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
- + ddev->notif.event.id.target_category = SSAM_SSH_TC_BAS;
- + ddev->notif.event.id.instance = 0;
- + ddev->notif.event.mask = SSAM_EVENT_MASK_NONE;
- + ddev->notif.event.flags = SSAM_EVENT_SEQUENCED;
- +
- + init_waitqueue_head(&ddev->waitq);
- + mutex_init(&ddev->write_lock);
- + init_rwsem(&ddev->client_lock);
- + INIT_LIST_HEAD(&ddev->client_list);
- +
- + INIT_DELAYED_WORK(&ddev->mode_work, sdtx_device_mode_workfn);
- + INIT_DELAYED_WORK(&ddev->state_work, sdtx_device_state_workfn);
- +
- + /*
- + * Get current device state. We want to guarantee that events are only
- + * sent when state actually changes. Thus we cannot use special
- + * "uninitialized" values, as that would cause problems when manually
- + * querying the state in surface_dtx_pm_complete(). I.e. we would not
- + * be able to detect state changes there if no change event has been
- + * received between driver initialization and first device suspension.
- + *
- + * Note that we also need to do this before registering the event
- + * notifier, as that may access the state values.
- + */
- + status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &ddev->state.base);
- + if (status)
- + return status;
- +
- + status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &ddev->state.device_mode);
- + if (status)
- + return status;
- +
- + status = ssam_retry(ssam_bas_get_latch_status, ddev->ctrl, &ddev->state.latch_status);
- + if (status)
- + return status;
- +
- + /* Set up tablet mode switch. */
- + ddev->mode_switch = input_allocate_device();
- + if (!ddev->mode_switch)
- + return -ENOMEM;
- +
- + ddev->mode_switch->name = "Microsoft Surface DTX Device Mode Switch";
- + ddev->mode_switch->phys = "ssam/01:11:01:00:00/input0";
- + ddev->mode_switch->id.bustype = BUS_HOST;
- + ddev->mode_switch->dev.parent = ddev->dev;
- +
- + tablet_mode = (ddev->state.device_mode != SDTX_DEVICE_MODE_LAPTOP);
- + input_set_capability(ddev->mode_switch, EV_SW, SW_TABLET_MODE);
- + input_report_switch(ddev->mode_switch, SW_TABLET_MODE, tablet_mode);
- +
- + status = input_register_device(ddev->mode_switch);
- + if (status) {
- + input_free_device(ddev->mode_switch);
- + return status;
- + }
- +
- + /* Set up event notifier. */
- + status = ssam_notifier_register(ddev->ctrl, &ddev->notif);
- + if (status)
- + goto err_notif;
- +
- + /* Register miscdevice. */
- + status = misc_register(&ddev->mdev);
- + if (status)
- + goto err_mdev;
- +
- + /*
- + * Update device state in case it has changed between getting the
- + * initial mode and registering the event notifier.
- + */
- + sdtx_update_device_state(ddev, 0);
- + return 0;
- +
- +err_notif:
- + ssam_notifier_unregister(ddev->ctrl, &ddev->notif);
- + cancel_delayed_work_sync(&ddev->mode_work);
- +err_mdev:
- + input_unregister_device(ddev->mode_switch);
- + return status;
- +}
- +
- +static struct sdtx_device *sdtx_device_create(struct device *dev, struct ssam_controller *ctrl)
- +{
- + struct sdtx_device *ddev;
- + int status;
- +
- + ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
- + if (!ddev)
- + return ERR_PTR(-ENOMEM);
- +
- + status = sdtx_device_init(ddev, dev, ctrl);
- + if (status) {
- + sdtx_device_put(ddev);
- + return ERR_PTR(status);
- + }
- +
- + return ddev;
- +}
- +
- +static void sdtx_device_destroy(struct sdtx_device *ddev)
- +{
- + struct sdtx_client *client;
- +
- + /*
- + * Mark device as shut-down. Prevent new clients from being added and
- + * new operations from being executed.
- + */
- + set_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags);
- +
- + /* Disable notifiers, prevent new events from arriving. */
- + ssam_notifier_unregister(ddev->ctrl, &ddev->notif);
- +
- + /* Stop mode_work, prevent access to mode_switch. */
- + cancel_delayed_work_sync(&ddev->mode_work);
- +
- + /* Stop state_work. */
- + cancel_delayed_work_sync(&ddev->state_work);
- +
- + /* With mode_work canceled, we can unregister the mode_switch. */
- + input_unregister_device(ddev->mode_switch);
- +
- + /* Wake up async clients. */
- + down_write(&ddev->client_lock);
- + list_for_each_entry(client, &ddev->client_list, node) {
- + kill_fasync(&client->fasync, SIGIO, POLL_HUP);
- + }
- + up_write(&ddev->client_lock);
- +
- + /* Wake up blocking clients. */
- + wake_up_interruptible(&ddev->waitq);
- +
- + /*
- + * Wait for clients to finish their current operation. After this, the
- + * controller and device references are guaranteed to be no longer in
- + * use.
- + */
- + down_write(&ddev->lock);
- + ddev->dev = NULL;
- + ddev->ctrl = NULL;
- + up_write(&ddev->lock);
- +
- + /* Finally remove the misc-device. */
- + misc_deregister(&ddev->mdev);
- +
- + /*
- + * We're now guaranteed that sdtx_device_open() won't be called any
- + * more, so we can now drop out reference.
- + */
- + sdtx_device_put(ddev);
- +}
- +
- +
- +/* -- PM ops. --------------------------------------------------------------- */
- +
- +#ifdef CONFIG_PM_SLEEP
- +
- +static void surface_dtx_pm_complete(struct device *dev)
- +{
- + struct sdtx_device *ddev = dev_get_drvdata(dev);
- +
- + /*
- + * Normally, the EC will store events while suspended (i.e. in
- + * display-off state) and release them when resumed (i.e. transitioned
- + * to display-on state). During hibernation, however, the EC will be
- + * shut down and does not store events. Furthermore, events might be
- + * dropped during prolonged suspension (it is currently unknown how
- + * big this event buffer is and how it behaves on overruns).
- + *
- + * To prevent any problems, we update the device state here. We do
- + * this delayed to ensure that any events sent by the EC directly
- + * after resuming will be handled first. The delay below has been
- + * chosen (experimentally), so that there should be ample time for
- + * these events to be handled, before we check and, if necessary,
- + * update the state.
- + */
- + sdtx_update_device_state(ddev, msecs_to_jiffies(1000));
- +}
- +
- +static const struct dev_pm_ops surface_dtx_pm_ops = {
- + .complete = surface_dtx_pm_complete,
- +};
- +
- +#else /* CONFIG_PM_SLEEP */
- +
- +static const struct dev_pm_ops surface_dtx_pm_ops = {};
- +
- +#endif /* CONFIG_PM_SLEEP */
- +
- +
- +/* -- Platform driver. ------------------------------------------------------ */
- +
- +static int surface_dtx_platform_probe(struct platform_device *pdev)
- +{
- + struct ssam_controller *ctrl;
- + struct sdtx_device *ddev;
- +
- + /* Link to EC. */
- + ctrl = ssam_client_bind(&pdev->dev);
- + if (IS_ERR(ctrl))
- + return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
- +
- + ddev = sdtx_device_create(&pdev->dev, ctrl);
- + if (IS_ERR(ddev))
- + return PTR_ERR(ddev);
- +
- + platform_set_drvdata(pdev, ddev);
- + return 0;
- +}
- +
- +static int surface_dtx_platform_remove(struct platform_device *pdev)
- +{
- + sdtx_device_destroy(platform_get_drvdata(pdev));
- + return 0;
- +}
- +
- +static const struct acpi_device_id surface_dtx_acpi_match[] = {
- + { "MSHW0133", 0 },
- + { },
- +};
- +MODULE_DEVICE_TABLE(acpi, surface_dtx_acpi_match);
- +
- +static struct platform_driver surface_dtx_platform_driver = {
- + .probe = surface_dtx_platform_probe,
- + .remove = surface_dtx_platform_remove,
- + .driver = {
- + .name = "surface_dtx_pltf",
- + .acpi_match_table = surface_dtx_acpi_match,
- + .pm = &surface_dtx_pm_ops,
- + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- + },
- +};
- +module_platform_driver(surface_dtx_platform_driver);
- +
- +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- +MODULE_DESCRIPTION("Detachment-system driver for Surface System Aggregator Module");
- +MODULE_LICENSE("GPL");
- diff --git a/include/uapi/linux/surface_aggregator/dtx.h b/include/uapi/linux/surface_aggregator/dtx.h
- new file mode 100644
- index 000000000000..0833aab0d819
- --- /dev/null
- +++ b/include/uapi/linux/surface_aggregator/dtx.h
- @@ -0,0 +1,146 @@
- +/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
- +/*
- + * Surface DTX (clipboard detachment system driver) user-space interface.
- + *
- + * Definitions, structs, and IOCTLs for the /dev/surface/dtx misc device. This
- + * device allows user-space to control the clipboard detachment process on
- + * Surface Book series devices.
- + *
- + * Copyright (C) 2020-2021 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#ifndef _UAPI_LINUX_SURFACE_AGGREGATOR_DTX_H
- +#define _UAPI_LINUX_SURFACE_AGGREGATOR_DTX_H
- +
- +#include <linux/ioctl.h>
- +#include <linux/types.h>
- +
- +/* Status/error categories */
- +#define SDTX_CATEGORY_STATUS 0x0000
- +#define SDTX_CATEGORY_RUNTIME_ERROR 0x1000
- +#define SDTX_CATEGORY_HARDWARE_ERROR 0x2000
- +#define SDTX_CATEGORY_UNKNOWN 0xf000
- +
- +#define SDTX_CATEGORY_MASK 0xf000
- +#define SDTX_CATEGORY(value) ((value) & SDTX_CATEGORY_MASK)
- +
- +#define SDTX_STATUS(code) ((code) | SDTX_CATEGORY_STATUS)
- +#define SDTX_ERR_RT(code) ((code) | SDTX_CATEGORY_RUNTIME_ERROR)
- +#define SDTX_ERR_HW(code) ((code) | SDTX_CATEGORY_HARDWARE_ERROR)
- +#define SDTX_UNKNOWN(code) ((code) | SDTX_CATEGORY_UNKNOWN)
- +
- +#define SDTX_SUCCESS(value) (SDTX_CATEGORY(value) == SDTX_CATEGORY_STATUS)
- +
- +/* Latch status values */
- +#define SDTX_LATCH_CLOSED SDTX_STATUS(0x00)
- +#define SDTX_LATCH_OPENED SDTX_STATUS(0x01)
- +
- +/* Base state values */
- +#define SDTX_BASE_DETACHED SDTX_STATUS(0x00)
- +#define SDTX_BASE_ATTACHED SDTX_STATUS(0x01)
- +
- +/* Runtime errors (non-critical) */
- +#define SDTX_DETACH_NOT_FEASIBLE SDTX_ERR_RT(0x01)
- +#define SDTX_DETACH_TIMEDOUT SDTX_ERR_RT(0x02)
- +
- +/* Hardware errors (critical) */
- +#define SDTX_ERR_FAILED_TO_OPEN SDTX_ERR_HW(0x01)
- +#define SDTX_ERR_FAILED_TO_REMAIN_OPEN SDTX_ERR_HW(0x02)
- +#define SDTX_ERR_FAILED_TO_CLOSE SDTX_ERR_HW(0x03)
- +
- +/* Base types */
- +#define SDTX_DEVICE_TYPE_HID 0x0100
- +#define SDTX_DEVICE_TYPE_SSH 0x0200
- +
- +#define SDTX_DEVICE_TYPE_MASK 0x0f00
- +#define SDTX_DEVICE_TYPE(value) ((value) & SDTX_DEVICE_TYPE_MASK)
- +
- +#define SDTX_BASE_TYPE_HID(id) ((id) | SDTX_DEVICE_TYPE_HID)
- +#define SDTX_BASE_TYPE_SSH(id) ((id) | SDTX_DEVICE_TYPE_SSH)
- +
- +/**
- + * enum sdtx_device_mode - Mode describing how (and if) the clipboard is
- + * attached to the base of the device.
- + * @SDTX_DEVICE_MODE_TABLET: The clipboard is detached from the base and the
- + * device operates as tablet.
- + * @SDTX_DEVICE_MODE_LAPTOP: The clipboard is attached normally to the base
- + * and the device operates as laptop.
- + * @SDTX_DEVICE_MODE_STUDIO: The clipboard is attached to the base in reverse.
- + * The device operates as tablet with keyboard and
- + * touchpad deactivated, however, the base battery
- + * and, if present in the specific device model, dGPU
- + * are available to the system.
- + */
- +enum sdtx_device_mode {
- + SDTX_DEVICE_MODE_TABLET = 0x00,
- + SDTX_DEVICE_MODE_LAPTOP = 0x01,
- + SDTX_DEVICE_MODE_STUDIO = 0x02,
- +};
- +
- +/**
- + * struct sdtx_event - Event provided by reading from the DTX device file.
- + * @length: Length of the event payload, in bytes.
- + * @code: Event code, detailing what type of event this is.
- + * @data: Payload of the event, containing @length bytes.
- + *
- + * See &enum sdtx_event_code for currently valid event codes.
- + */
- +struct sdtx_event {
- + __u16 length;
- + __u16 code;
- + __u8 data[];
- +} __attribute__((__packed__));
- +
- +/**
- + * enum sdtx_event_code - Code describing the type of an event.
- + * @SDTX_EVENT_REQUEST: Detachment request event type.
- + * @SDTX_EVENT_CANCEL: Cancel detachment process event type.
- + * @SDTX_EVENT_BASE_CONNECTION: Base/clipboard connection change event type.
- + * @SDTX_EVENT_LATCH_STATUS: Latch status change event type.
- + * @SDTX_EVENT_DEVICE_MODE: Device mode change event type.
- + *
- + * Used in &struct sdtx_event to describe the type of the event. Further event
- + * codes are reserved for future use. Any event parser should be able to
- + * gracefully handle unknown events, i.e. by simply skipping them.
- + *
- + * Consult the DTX user-space interface documentation for details regarding
- + * the individual event types.
- + */
- +enum sdtx_event_code {
- + SDTX_EVENT_REQUEST = 1,
- + SDTX_EVENT_CANCEL = 2,
- + SDTX_EVENT_BASE_CONNECTION = 3,
- + SDTX_EVENT_LATCH_STATUS = 4,
- + SDTX_EVENT_DEVICE_MODE = 5,
- +};
- +
- +/**
- + * struct sdtx_base_info - Describes if and what type of base is connected.
- + * @state: The state of the connection. Valid values are %SDTX_BASE_DETACHED,
- + * %SDTX_BASE_ATTACHED, and %SDTX_DETACH_NOT_FEASIBLE (in case a base
- + * is attached but low clipboard battery prevents detachment). Other
- + * values are currently reserved.
- + * @base_id: The type of base connected. Zero if no base is connected.
- + */
- +struct sdtx_base_info {
- + __u16 state;
- + __u16 base_id;
- +} __attribute__((__packed__));
- +
- +/* IOCTLs */
- +#define SDTX_IOCTL_EVENTS_ENABLE _IO(0xa5, 0x21)
- +#define SDTX_IOCTL_EVENTS_DISABLE _IO(0xa5, 0x22)
- +
- +#define SDTX_IOCTL_LATCH_LOCK _IO(0xa5, 0x23)
- +#define SDTX_IOCTL_LATCH_UNLOCK _IO(0xa5, 0x24)
- +
- +#define SDTX_IOCTL_LATCH_REQUEST _IO(0xa5, 0x25)
- +#define SDTX_IOCTL_LATCH_CONFIRM _IO(0xa5, 0x26)
- +#define SDTX_IOCTL_LATCH_HEARTBEAT _IO(0xa5, 0x27)
- +#define SDTX_IOCTL_LATCH_CANCEL _IO(0xa5, 0x28)
- +
- +#define SDTX_IOCTL_GET_BASE_INFO _IOR(0xa5, 0x29, struct sdtx_base_info)
- +#define SDTX_IOCTL_GET_DEVICE_MODE _IOR(0xa5, 0x2a, __u16)
- +#define SDTX_IOCTL_GET_LATCH_STATUS _IOR(0xa5, 0x2b, __u16)
- +
- +#endif /* _UAPI_LINUX_SURFACE_AGGREGATOR_DTX_H */
- --
- 2.31.0
- From fe26e240690665522af694f5a5a7a2985d0f236a Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Tue, 9 Feb 2021 02:50:11 +0100
- Subject: [PATCH] platform/surface: dtx: Add support for native SSAM devices
- Add support for native SSAM devices to the DTX driver. This allows
- support for the Surface Book 3, on which the DTX device is not present
- in ACPI.
- Patchset: surface-sam
- ---
- drivers/platform/surface/Kconfig | 4 ++
- drivers/platform/surface/surface_dtx.c | 90 +++++++++++++++++++++++++-
- 2 files changed, 93 insertions(+), 1 deletion(-)
- diff --git a/drivers/platform/surface/Kconfig b/drivers/platform/surface/Kconfig
- index 745f9d2eb6a7..dea313989b4c 100644
- --- a/drivers/platform/surface/Kconfig
- +++ b/drivers/platform/surface/Kconfig
- @@ -127,6 +127,10 @@ config SURFACE_DTX
- behavior of this process, which includes the option to abort it in
- case the base is still in use or speed it up in case it is not.
-
- + Note that this module can be built without support for the Surface
- + Aggregator Bus (i.e. CONFIG_SURFACE_AGGREGATOR_BUS=n). In that case,
- + some devices, specifically the Surface Book 3, will not be supported.
- +
- config SURFACE_GPE
- tristate "Surface GPE/Lid Support Driver"
- depends on DMI
- diff --git a/drivers/platform/surface/surface_dtx.c b/drivers/platform/surface/surface_dtx.c
- index a95adc1094aa..4bb5d286bf95 100644
- --- a/drivers/platform/surface/surface_dtx.c
- +++ b/drivers/platform/surface/surface_dtx.c
- @@ -27,6 +27,7 @@
- #include <linux/workqueue.h>
-
- #include <linux/surface_aggregator/controller.h>
- +#include <linux/surface_aggregator/device.h>
- #include <linux/surface_aggregator/dtx.h>
-
-
- @@ -1194,7 +1195,94 @@ static struct platform_driver surface_dtx_platform_driver = {
- .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- },
- };
- -module_platform_driver(surface_dtx_platform_driver);
- +
- +
- +/* -- SSAM device driver. --------------------------------------------------- */
- +
- +#ifdef CONFIG_SURFACE_AGGREGATOR_BUS
- +
- +static int surface_dtx_ssam_probe(struct ssam_device *sdev)
- +{
- + struct sdtx_device *ddev;
- +
- + ddev = sdtx_device_create(&sdev->dev, sdev->ctrl);
- + if (IS_ERR(ddev))
- + return PTR_ERR(ddev);
- +
- + ssam_device_set_drvdata(sdev, ddev);
- + return 0;
- +}
- +
- +static void surface_dtx_ssam_remove(struct ssam_device *sdev)
- +{
- + sdtx_device_destroy(ssam_device_get_drvdata(sdev));
- +}
- +
- +static const struct ssam_device_id surface_dtx_ssam_match[] = {
- + { SSAM_SDEV(BAS, 0x01, 0x00, 0x00) },
- + { },
- +};
- +MODULE_DEVICE_TABLE(ssam, surface_dtx_ssam_match);
- +
- +static struct ssam_device_driver surface_dtx_ssam_driver = {
- + .probe = surface_dtx_ssam_probe,
- + .remove = surface_dtx_ssam_remove,
- + .match_table = surface_dtx_ssam_match,
- + .driver = {
- + .name = "surface_dtx",
- + .pm = &surface_dtx_pm_ops,
- + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- + },
- +};
- +
- +static int ssam_dtx_driver_register(void)
- +{
- + return ssam_device_driver_register(&surface_dtx_ssam_driver);
- +}
- +
- +static void ssam_dtx_driver_unregister(void)
- +{
- + ssam_device_driver_unregister(&surface_dtx_ssam_driver);
- +}
- +
- +#else /* CONFIG_SURFACE_AGGREGATOR_BUS */
- +
- +static int ssam_dtx_driver_register(void)
- +{
- + return 0;
- +}
- +
- +static void ssam_dtx_driver_unregister(void)
- +{
- +}
- +
- +#endif /* CONFIG_SURFACE_AGGREGATOR_BUS */
- +
- +
- +/* -- Module setup. --------------------------------------------------------- */
- +
- +static int __init surface_dtx_init(void)
- +{
- + int status;
- +
- + status = ssam_dtx_driver_register();
- + if (status)
- + return status;
- +
- + status = platform_driver_register(&surface_dtx_platform_driver);
- + if (status)
- + ssam_dtx_driver_unregister();
- +
- + return status;
- +}
- +module_init(surface_dtx_init);
- +
- +static void __exit surface_dtx_exit(void)
- +{
- + platform_driver_unregister(&surface_dtx_platform_driver);
- + ssam_dtx_driver_unregister();
- +}
- +module_exit(surface_dtx_exit);
-
- MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- MODULE_DESCRIPTION("Detachment-system driver for Surface System Aggregator Module");
- --
- 2.31.0
- From 5371ba2eedca09094db9dcf6b2b8c60159663543 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Tue, 9 Feb 2021 02:55:31 +0100
- Subject: [PATCH] docs: driver-api: Add Surface DTX driver documentation
- Add documentation for the user-space interface of the Surface DTX
- (detachment system) driver, used on Microsoft Surface Book series
- devices.
- Patchset: surface-sam
- ---
- .../surface_aggregator/clients/dtx.rst | 718 ++++++++++++++++++
- .../surface_aggregator/clients/index.rst | 1 +
- MAINTAINERS | 1 +
- 3 files changed, 720 insertions(+)
- create mode 100644 Documentation/driver-api/surface_aggregator/clients/dtx.rst
- diff --git a/Documentation/driver-api/surface_aggregator/clients/dtx.rst b/Documentation/driver-api/surface_aggregator/clients/dtx.rst
- new file mode 100644
- index 000000000000..e7e7c20007f0
- --- /dev/null
- +++ b/Documentation/driver-api/surface_aggregator/clients/dtx.rst
- @@ -0,0 +1,718 @@
- +.. SPDX-License-Identifier: GPL-2.0+
- +
- +.. |__u16| replace:: :c:type:`__u16 <__u16>`
- +.. |sdtx_event| replace:: :c:type:`struct sdtx_event <sdtx_event>`
- +.. |sdtx_event_code| replace:: :c:type:`enum sdtx_event_code <sdtx_event_code>`
- +.. |sdtx_base_info| replace:: :c:type:`struct sdtx_base_info <sdtx_base_info>`
- +.. |sdtx_device_mode| replace:: :c:type:`struct sdtx_device_mode <sdtx_device_mode>`
- +
- +======================================================
- +User-Space DTX (Clipboard Detachment System) Interface
- +======================================================
- +
- +The ``surface_dtx`` driver is responsible for proper clipboard detachment
- +and re-attachment handling. To this end, it provides the ``/dev/surface/dtx``
- +device file, through which it can interface with a user-space daemon. This
- +daemon is then ultimately responsible for determining and taking necessary
- +actions, such as unmounting devices attached to the base,
- +unloading/reloading the graphics-driver, user-notifications, etc.
- +
- +There are two basic communication principles used in this driver: Commands
- +(in other parts of the documentation also referred to as requests) and
- +events. Commands are sent to the EC and may have a different implications in
- +different contexts. Events are sent by the EC upon some internal state
- +change. Commands are always driver-initiated, whereas events are always
- +initiated by the EC.
- +
- +.. contents::
- +
- +Nomenclature
- +============
- +
- +* **Clipboard:**
- + The detachable upper part of the Surface Book, housing the screen and CPU.
- +
- +* **Base:**
- + The lower part of the Surface Book from which the clipboard can be
- + detached, optionally (model dependent) housing the discrete GPU (dGPU).
- +
- +* **Latch:**
- + The mechanism keeping the clipboard attached to the base in normal
- + operation and allowing it to be detached when requested.
- +
- +* **Silently ignored commands:**
- + The command is accepted by the EC as a valid command and acknowledged
- + (following the standard communication protocol), but the EC does not act
- + upon it, i.e. ignores it.e upper part of the
- +
- +
- +Detachment Process
- +==================
- +
- +Warning: This part of the documentation is based on reverse engineering and
- +testing and thus may contain errors or be incomplete.
- +
- +Latch States
- +------------
- +
- +The latch mechanism has two major states: *open* and *closed*. In the
- +*closed* state (default), the clipboard is secured to the base, whereas in
- +the *open* state, the clipboard can be removed by a user.
- +
- +The latch can additionally be locked and, correspondingly, unlocked, which
- +can influence the detachment procedure. Specifically, this locking mechanism
- +is intended to prevent the dGPU, positioned in the base of the device, from
- +being hot-unplugged while in use. More details can be found in the
- +documentation for the detachment procedure below. By default, the latch is
- +unlocked.
- +
- +Detachment Procedure
- +--------------------
- +
- +Note that the detachment process is governed fully by the EC. The
- +``surface_dtx`` driver only relays events from the EC to user-space and
- +commands from user-space to the EC, i.e. it does not influence this process.
- +
- +The detachment process is started with the user pressing the *detach* button
- +on the base of the device or executing the ``SDTX_IOCTL_LATCH_REQUEST`` IOCTL.
- +Following that:
- +
- +1. The EC turns on the indicator led on the detach-button, sends a
- + *detach-request* event (``SDTX_EVENT_REQUEST``), and awaits further
- + instructions/commands. In case the latch is unlocked, the led will flash
- + green. If the latch has been locked, the led will be solid red
- +
- +2. The event is, via the ``surface_dtx`` driver, relayed to user-space, where
- + an appropriate user-space daemon can handle it and send instructions back
- + to the EC via IOCTLs provided by this driver.
- +
- +3. The EC waits for instructions from user-space and acts according to them.
- + If the EC does not receive any instructions in a given period, it will
- + time out and continue as follows:
- +
- + - If the latch is unlocked, the EC will open the latch and the clipboard
- + can be detached from the base. This is the exact behavior as without
- + this driver or any user-space daemon. See the ``SDTX_IOCTL_LATCH_CONFIRM``
- + description below for more details on the follow-up behavior of the EC.
- +
- + - If the latch is locked, the EC will *not* open the latch, meaning the
- + clipboard cannot be detached from the base. Furthermore, the EC sends
- + an cancel event (``SDTX_EVENT_CANCEL``) detailing this with the cancel
- + reason ``SDTX_DETACH_TIMEDOUT`` (see :ref:`events` for details).
- +
- +Valid responses by a user-space daemon to a detachment request event are:
- +
- +- Execute ``SDTX_IOCTL_LATCH_REQUEST``. This will immediately abort the
- + detachment process. Furthermore, the EC will send a detach-request event,
- + similar to the user pressing the detach-button to cancel said process (see
- + below).
- +
- +- Execute ``SDTX_IOCTL_LATCH_CONFIRM``. This will cause the EC to open the
- + latch, after which the user can separate clipboard and base.
- +
- + As this changes the latch state, a *latch-status* event
- + (``SDTX_EVENT_LATCH_STATUS``) will be sent once the latch has been opened
- + successfully. If the EC fails to open the latch, e.g. due to hardware
- + error or low battery, a latch-cancel event (``SDTX_EVENT_CANCEL``) will be
- + sent with the cancel reason indicating the specific failure.
- +
- + If the latch is currently locked, the latch will automatically be
- + unlocked before it is opened.
- +
- +- Execute ``SDTX_IOCTL_LATCH_HEARTBEAT``. This will reset the internal timeout.
- + No other actions will be performed, i.e. the detachment process will neither
- + be completed nor canceled, and the EC will still be waiting for further
- + responses.
- +
- +- Execute ``SDTX_IOCTL_LATCH_CANCEL``. This will abort the detachment process,
- + similar to ``SDTX_IOCTL_LATCH_REQUEST``, described above, or the button
- + press, described below. A *generic request* event (``SDTX_EVENT_REQUEST``)
- + is send in response to this. In contrast to those, however, this command
- + does not trigger a new detachment process if none is currently in
- + progress.
- +
- +- Do nothing. The detachment process eventually times out as described in
- + point 3.
- +
- +See :ref:`ioctls` for more details on these responses.
- +
- +It is important to note that, if the user presses the detach button at any
- +point when a detachment operation is in progress (i.e. after the EC has sent
- +the initial *detach-request* event (``SDTX_EVENT_REQUEST``) and before it
- +received the corresponding response concluding the process), the detachment
- +process is canceled on the EC-level and an identical event is being sent.
- +Thus a *detach-request* event, by itself, does not signal the start of the
- +detachment process.
- +
- +The detachment process may further be canceled by the EC due to hardware
- +failures or a low clipboard battery. This is done via a cancel event
- +(``SDTX_EVENT_CANCEL``) with the corresponding cancel reason.
- +
- +
- +User-Space Interface Documentation
- +==================================
- +
- +Error Codes and Status Values
- +-----------------------------
- +
- +Error and status codes are divided into different categories, which can be
- +used to determine if the status code is an error, and, if it is, the
- +severity and type of that error. The current categories are:
- +
- +.. flat-table:: Overview of Status/Error Categories.
- + :widths: 2 1 3
- + :header-rows: 1
- +
- + * - Name
- + - Value
- + - Short Description
- +
- + * - ``STATUS``
- + - ``0x0000``
- + - Non-error status codes.
- +
- + * - ``RUNTIME_ERROR``
- + - ``0x1000``
- + - Non-critical runtime errors.
- +
- + * - ``HARDWARE_ERROR``
- + - ``0x2000``
- + - Critical hardware failures.
- +
- + * - ``UNKNOWN``
- + - ``0xF000``
- + - Unknown error codes.
- +
- +Other categories are reserved for future use. The ``SDTX_CATEGORY()`` macro
- +can be used to determine the category of any status value. The
- +``SDTX_SUCCESS()`` macro can be used to check if the status value is a
- +success value (``SDTX_CATEGORY_STATUS``) or if it indicates a failure.
- +
- +Unknown status or error codes sent by the EC are assigned to the ``UNKNOWN``
- +category by the driver and may be implemented via their own code in the
- +future.
- +
- +Currently used error codes are:
- +
- +.. flat-table:: Overview of Error Codes.
- + :widths: 2 1 1 3
- + :header-rows: 1
- +
- + * - Name
- + - Category
- + - Value
- + - Short Description
- +
- + * - ``SDTX_DETACH_NOT_FEASIBLE``
- + - ``RUNTIME``
- + - ``0x1001``
- + - Detachment not feasible due to low clipboard battery.
- +
- + * - ``SDTX_DETACH_TIMEDOUT``
- + - ``RUNTIME``
- + - ``0x1002``
- + - Detachment process timed out while the latch was locked.
- +
- + * - ``SDTX_ERR_FAILED_TO_OPEN``
- + - ``HARDWARE``
- + - ``0x2001``
- + - Failed to open latch.
- +
- + * - ``SDTX_ERR_FAILED_TO_REMAIN_OPEN``
- + - ``HARDWARE``
- + - ``0x2002``
- + - Failed to keep latch open.
- +
- + * - ``SDTX_ERR_FAILED_TO_CLOSE``
- + - ``HARDWARE``
- + - ``0x2003``
- + - Failed to close latch.
- +
- +Other error codes are reserved for future use. Non-error status codes may
- +overlap and are generally only unique within their use-case:
- +
- +.. flat-table:: Latch Status Codes.
- + :widths: 2 1 1 3
- + :header-rows: 1
- +
- + * - Name
- + - Category
- + - Value
- + - Short Description
- +
- + * - ``SDTX_LATCH_CLOSED``
- + - ``STATUS``
- + - ``0x0000``
- + - Latch is closed/has been closed.
- +
- + * - ``SDTX_LATCH_OPENED``
- + - ``STATUS``
- + - ``0x0001``
- + - Latch is open/has been opened.
- +
- +.. flat-table:: Base State Codes.
- + :widths: 2 1 1 3
- + :header-rows: 1
- +
- + * - Name
- + - Category
- + - Value
- + - Short Description
- +
- + * - ``SDTX_BASE_DETACHED``
- + - ``STATUS``
- + - ``0x0000``
- + - Base has been detached/is not present.
- +
- + * - ``SDTX_BASE_ATTACHED``
- + - ``STATUS``
- + - ``0x0001``
- + - Base has been attached/is present.
- +
- +Again, other codes are reserved for future use.
- +
- +.. _events:
- +
- +Events
- +------
- +
- +Events can be received by reading from the device file. They are disabled by
- +default and have to be enabled by executing ``SDTX_IOCTL_EVENTS_ENABLE``
- +first. All events follow the layout prescribed by |sdtx_event|. Specific
- +event types can be identified by their event code, described in
- +|sdtx_event_code|. Note that other event codes are reserved for future use,
- +thus an event parser must be able to handle any unknown/unsupported event
- +types gracefully, by relying on the payload length given in the event header.
- +
- +Currently provided event types are:
- +
- +.. flat-table:: Overview of DTX events.
- + :widths: 2 1 1 3
- + :header-rows: 1
- +
- + * - Name
- + - Code
- + - Payload
- + - Short Description
- +
- + * - ``SDTX_EVENT_REQUEST``
- + - ``1``
- + - ``0`` bytes
- + - Detachment process initiated/aborted.
- +
- + * - ``SDTX_EVENT_CANCEL``
- + - ``2``
- + - ``2`` bytes
- + - EC canceled detachment process.
- +
- + * - ``SDTX_EVENT_BASE_CONNECTION``
- + - ``3``
- + - ``4`` bytes
- + - Base connection state changed.
- +
- + * - ``SDTX_EVENT_LATCH_STATUS``
- + - ``4``
- + - ``2`` bytes
- + - Latch status changed.
- +
- + * - ``SDTX_EVENT_DEVICE_MODE``
- + - ``5``
- + - ``2`` bytes
- + - Device mode changed.
- +
- +Individual events in more detail:
- +
- +``SDTX_EVENT_REQUEST``
- +^^^^^^^^^^^^^^^^^^^^^^
- +
- +Sent when a detachment process is started or, if in progress, aborted by the
- +user, either via a detach button press or a detach request
- +(``SDTX_IOCTL_LATCH_REQUEST``) being sent from user-space.
- +
- +Does not have any payload.
- +
- +``SDTX_EVENT_CANCEL``
- +^^^^^^^^^^^^^^^^^^^^^
- +
- +Sent when a detachment process is canceled by the EC due to unfulfilled
- +preconditions (e.g. clipboard battery too low to detach) or hardware
- +failure. The reason for cancellation is given in the event payload detailed
- +below and can be one of
- +
- +* ``SDTX_DETACH_TIMEDOUT``: Detachment timed out while the latch was locked.
- + The latch has neither been opened nor unlocked.
- +
- +* ``SDTX_DETACH_NOT_FEASIBLE``: Detachment not feasible due to low clipboard
- + battery.
- +
- +* ``SDTX_ERR_FAILED_TO_OPEN``: Could not open the latch (hardware failure).
- +
- +* ``SDTX_ERR_FAILED_TO_REMAIN_OPEN``: Could not keep the latch open (hardware
- + failure).
- +
- +* ``SDTX_ERR_FAILED_TO_CLOSE``: Could not close the latch (hardware failure).
- +
- +Other error codes in this context are reserved for future use.
- +
- +These codes can be classified via the ``SDTX_CATEGORY()`` macro to discern
- +between critical hardware errors (``SDTX_CATEGORY_HARDWARE_ERROR``) or
- +runtime errors (``SDTX_CATEGORY_RUNTIME_ERROR``), the latter of which may
- +happen during normal operation if certain preconditions for detachment are
- +not given.
- +
- +.. flat-table:: Detachment Cancel Event Payload
- + :widths: 1 1 4
- + :header-rows: 1
- +
- + * - Field
- + - Type
- + - Description
- +
- + * - ``reason``
- + - |__u16|
- + - Reason for cancellation.
- +
- +``SDTX_EVENT_BASE_CONNECTION``
- +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- +
- +Sent when the base connection state has changed, i.e. when the base has been
- +attached, detached, or detachment has become infeasible due to low clipboard
- +battery. The new state and, if a base is connected, ID of the base is
- +provided as payload of type |sdtx_base_info| with its layout presented
- +below:
- +
- +.. flat-table:: Base-Connection-Change Event Payload
- + :widths: 1 1 4
- + :header-rows: 1
- +
- + * - Field
- + - Type
- + - Description
- +
- + * - ``state``
- + - |__u16|
- + - Base connection state.
- +
- + * - ``base_id``
- + - |__u16|
- + - Type of base connected (zero if none).
- +
- +Possible values for ``state`` are:
- +
- +* ``SDTX_BASE_DETACHED``,
- +* ``SDTX_BASE_ATTACHED``, and
- +* ``SDTX_DETACH_NOT_FEASIBLE``.
- +
- +Other values are reserved for future use.
- +
- +``SDTX_EVENT_LATCH_STATUS``
- +^^^^^^^^^^^^^^^^^^^^^^^^^^^
- +
- +Sent when the latch status has changed, i.e. when the latch has been opened,
- +closed, or an error occurred. The current status is provided as payload:
- +
- +.. flat-table:: Latch-Status-Change Event Payload
- + :widths: 1 1 4
- + :header-rows: 1
- +
- + * - Field
- + - Type
- + - Description
- +
- + * - ``status``
- + - |__u16|
- + - Latch status.
- +
- +Possible values for ``status`` are:
- +
- +* ``SDTX_LATCH_CLOSED``,
- +* ``SDTX_LATCH_OPENED``,
- +* ``SDTX_ERR_FAILED_TO_OPEN``,
- +* ``SDTX_ERR_FAILED_TO_REMAIN_OPEN``, and
- +* ``SDTX_ERR_FAILED_TO_CLOSE``.
- +
- +Other values are reserved for future use.
- +
- +``SDTX_EVENT_DEVICE_MODE``
- +^^^^^^^^^^^^^^^^^^^^^^^^^^
- +
- +Sent when the device mode has changed. The new device mode is provided as
- +payload:
- +
- +.. flat-table:: Device-Mode-Change Event Payload
- + :widths: 1 1 4
- + :header-rows: 1
- +
- + * - Field
- + - Type
- + - Description
- +
- + * - ``mode``
- + - |__u16|
- + - Device operation mode.
- +
- +Possible values for ``mode`` are:
- +
- +* ``SDTX_DEVICE_MODE_TABLET``,
- +* ``SDTX_DEVICE_MODE_LAPTOP``, and
- +* ``SDTX_DEVICE_MODE_STUDIO``.
- +
- +Other values are reserved for future use.
- +
- +.. _ioctls:
- +
- +IOCTLs
- +------
- +
- +The following IOCTLs are provided:
- +
- +.. flat-table:: Overview of DTX IOCTLs
- + :widths: 1 1 1 1 4
- + :header-rows: 1
- +
- + * - Type
- + - Number
- + - Direction
- + - Name
- + - Description
- +
- + * - ``0xA5``
- + - ``0x21``
- + - ``-``
- + - ``EVENTS_ENABLE``
- + - Enable events for the current file descriptor.
- +
- + * - ``0xA5``
- + - ``0x22``
- + - ``-``
- + - ``EVENTS_DISABLE``
- + - Disable events for the current file descriptor.
- +
- + * - ``0xA5``
- + - ``0x23``
- + - ``-``
- + - ``LATCH_LOCK``
- + - Lock the latch.
- +
- + * - ``0xA5``
- + - ``0x24``
- + - ``-``
- + - ``LATCH_UNLOCK``
- + - Unlock the latch.
- +
- + * - ``0xA5``
- + - ``0x25``
- + - ``-``
- + - ``LATCH_REQUEST``
- + - Request clipboard detachment.
- +
- + * - ``0xA5``
- + - ``0x26``
- + - ``-``
- + - ``LATCH_CONFIRM``
- + - Confirm clipboard detachment request.
- +
- + * - ``0xA5``
- + - ``0x27``
- + - ``-``
- + - ``LATCH_HEARTBEAT``
- + - Send heartbeat signal to EC.
- +
- + * - ``0xA5``
- + - ``0x28``
- + - ``-``
- + - ``LATCH_CANCEL``
- + - Cancel detachment process.
- +
- + * - ``0xA5``
- + - ``0x29``
- + - ``R``
- + - ``GET_BASE_INFO``
- + - Get current base/connection information.
- +
- + * - ``0xA5``
- + - ``0x2A``
- + - ``R``
- + - ``GET_DEVICE_MODE``
- + - Get current device operation mode.
- +
- + * - ``0xA5``
- + - ``0x2B``
- + - ``R``
- + - ``GET_LATCH_STATUS``
- + - Get current device latch status.
- +
- +``SDTX_IOCTL_EVENTS_ENABLE``
- +^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- +
- +Defined as ``_IO(0xA5, 0x22)``.
- +
- +Enable events for the current file descriptor. Events can be obtained by
- +reading from the device, if enabled. Events are disabled by default.
- +
- +``SDTX_IOCTL_EVENTS_DISABLE``
- +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- +
- +Defined as ``_IO(0xA5, 0x22)``.
- +
- +Disable events for the current file descriptor. Events can be obtained by
- +reading from the device, if enabled. Events are disabled by default.
- +
- +``SDTX_IOCTL_LATCH_LOCK``
- +^^^^^^^^^^^^^^^^^^^^^^^^^
- +
- +Defined as ``_IO(0xA5, 0x23)``.
- +
- +Locks the latch, causing the detachment procedure to abort without opening
- +the latch on timeout. The latch is unlocked by default. This command will be
- +silently ignored if the latch is already locked.
- +
- +``SDTX_IOCTL_LATCH_UNLOCK``
- +^^^^^^^^^^^^^^^^^^^^^^^^^^^
- +
- +Defined as ``_IO(0xA5, 0x24)``.
- +
- +Unlocks the latch, causing the detachment procedure to open the latch on
- +timeout. The latch is unlocked by default. This command will not open the
- +latch when sent during an ongoing detachment process. It will be silently
- +ignored if the latch is already unlocked.
- +
- +``SDTX_IOCTL_LATCH_REQUEST``
- +^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- +
- +Defined as ``_IO(0xA5, 0x25)``.
- +
- +Generic latch request. Behavior depends on the context: If no
- +detachment-process is active, detachment is requested. Otherwise the
- +currently active detachment-process will be aborted.
- +
- +If a detachment process is canceled by this operation, a generic detachment
- +request event (``SDTX_EVENT_REQUEST``) will be sent.
- +
- +This essentially behaves the same as a detachment button press.
- +
- +``SDTX_IOCTL_LATCH_CONFIRM``
- +^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- +
- +Defined as ``_IO(0xA5, 0x26)``.
- +
- +Acknowledges and confirms a latch request. If sent during an ongoing
- +detachment process, this command causes the latch to be opened immediately.
- +The latch will also be opened if it has been locked. In this case, the latch
- +lock is reset to the unlocked state.
- +
- +This command will be silently ignored if there is currently no detachment
- +procedure in progress.
- +
- +``SDTX_IOCTL_LATCH_HEARTBEAT``
- +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- +
- +Defined as ``_IO(0xA5, 0x27)``.
- +
- +Sends a heartbeat, essentially resetting the detachment timeout. This
- +command can be used to keep the detachment process alive while work required
- +for the detachment to succeed is still in progress.
- +
- +This command will be silently ignored if there is currently no detachment
- +procedure in progress.
- +
- +``SDTX_IOCTL_LATCH_CANCEL``
- +^^^^^^^^^^^^^^^^^^^^^^^^^^^
- +
- +Defined as ``_IO(0xA5, 0x28)``.
- +
- +Cancels detachment in progress (if any). If a detachment process is canceled
- +by this operation, a generic detachment request event
- +(``SDTX_EVENT_REQUEST``) will be sent.
- +
- +This command will be silently ignored if there is currently no detachment
- +procedure in progress.
- +
- +``SDTX_IOCTL_GET_BASE_INFO``
- +^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- +
- +Defined as ``_IOR(0xA5, 0x29, struct sdtx_base_info)``.
- +
- +Get the current base connection state (i.e. attached/detached) and the type
- +of the base connected to the clipboard. This is command essentially provides
- +a way to query the information provided by the base connection change event
- +(``SDTX_EVENT_BASE_CONNECTION``).
- +
- +Possible values for ``struct sdtx_base_info.state`` are:
- +
- +* ``SDTX_BASE_DETACHED``,
- +* ``SDTX_BASE_ATTACHED``, and
- +* ``SDTX_DETACH_NOT_FEASIBLE``.
- +
- +Other values are reserved for future use.
- +
- +``SDTX_IOCTL_GET_DEVICE_MODE``
- +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- +
- +Defined as ``_IOR(0xA5, 0x2A, __u16)``.
- +
- +Returns the device operation mode, indicating if and how the base is
- +attached to the clipboard. This is command essentially provides a way to
- +query the information provided by the device mode change event
- +(``SDTX_EVENT_DEVICE_MODE``).
- +
- +Returned values are:
- +
- +* ``SDTX_DEVICE_MODE_LAPTOP``
- +* ``SDTX_DEVICE_MODE_TABLET``
- +* ``SDTX_DEVICE_MODE_STUDIO``
- +
- +See |sdtx_device_mode| for details. Other values are reserved for future
- +use.
- +
- +
- +``SDTX_IOCTL_GET_LATCH_STATUS``
- +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- +
- +Defined as ``_IOR(0xA5, 0x2B, __u16)``.
- +
- +Get the current latch status or (presumably) the last error encountered when
- +trying to open/close the latch. This is command essentially provides a way
- +to query the information provided by the latch status change event
- +(``SDTX_EVENT_LATCH_STATUS``).
- +
- +Returned values are:
- +
- +* ``SDTX_LATCH_CLOSED``,
- +* ``SDTX_LATCH_OPENED``,
- +* ``SDTX_ERR_FAILED_TO_OPEN``,
- +* ``SDTX_ERR_FAILED_TO_REMAIN_OPEN``, and
- +* ``SDTX_ERR_FAILED_TO_CLOSE``.
- +
- +Other values are reserved for future use.
- +
- +A Note on Base IDs
- +------------------
- +
- +Base types/IDs provided via ``SDTX_EVENT_BASE_CONNECTION`` or
- +``SDTX_IOCTL_GET_BASE_INFO`` are directly forwarded from the EC in the lower
- +byte of the combined |__u16| value, with the driver storing the EC type from
- +which this ID comes in the high byte (without this, base IDs over different
- +types of ECs may be overlapping).
- +
- +The ``SDTX_DEVICE_TYPE()`` macro can be used to determine the EC device
- +type. This can be one of
- +
- +* ``SDTX_DEVICE_TYPE_HID``, for Surface Aggregator Module over HID, and
- +
- +* ``SDTX_DEVICE_TYPE_SSH``, for Surface Aggregator Module over Surface Serial
- + Hub.
- +
- +Note that currently only the ``SSH`` type EC is supported, however ``HID``
- +type is reserved for future use.
- +
- +Structures and Enums
- +--------------------
- +
- +.. kernel-doc:: include/uapi/linux/surface_aggregator/dtx.h
- +
- +API Users
- +=========
- +
- +A user-space daemon utilizing this API can be found at
- +https://github.com/linux-surface/surface-dtx-daemon.
- diff --git a/Documentation/driver-api/surface_aggregator/clients/index.rst b/Documentation/driver-api/surface_aggregator/clients/index.rst
- index 3ccabce23271..98ea9946b8a2 100644
- --- a/Documentation/driver-api/surface_aggregator/clients/index.rst
- +++ b/Documentation/driver-api/surface_aggregator/clients/index.rst
- @@ -11,6 +11,7 @@ This is the documentation for client drivers themselves. Refer to
- :maxdepth: 1
-
- cdev
- + dtx
- san
-
- .. only:: subproject and html
- diff --git a/MAINTAINERS b/MAINTAINERS
- index 1a60e353df38..a6a4f5afdfa8 100644
- --- a/MAINTAINERS
- +++ b/MAINTAINERS
- @@ -11789,6 +11789,7 @@ MICROSOFT SURFACE DTX DRIVER
- M: Maximilian Luz <luzmaximilian@gmail.com>
- L: platform-driver-x86@vger.kernel.org
- S: Maintained
- +F: Documentation/driver-api/surface_aggregator/clients/dtx.rst
- F: drivers/platform/surface/surface_dtx.c
- F: include/uapi/linux/surface_aggregator/dtx.h
-
- --
- 2.31.0
- From a6c935de36064d4dd03c11201ac1cf1013d26416 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Thu, 11 Feb 2021 20:02:57 +0100
- Subject: [PATCH] input: Add bus ID for Surface Aggregator Module
- Add a bus identifier for the Surface System Aggregator Module, which
- connects various integrated HID devices on Microsoft Surface models via
- a dedicated HID transport protocol.
- Patchset: surface-sam
- ---
- include/uapi/linux/input.h | 1 +
- 1 file changed, 1 insertion(+)
- diff --git a/include/uapi/linux/input.h b/include/uapi/linux/input.h
- index 9a61c28ed3ae..3e81ea3d7df2 100644
- --- a/include/uapi/linux/input.h
- +++ b/include/uapi/linux/input.h
- @@ -271,6 +271,7 @@ struct input_mask {
- #define BUS_RMI 0x1D
- #define BUS_CEC 0x1E
- #define BUS_INTEL_ISHTP 0x1F
- +#define BUS_SURFACE_AGGREGATOR 0x20
-
- /*
- * MT_TOOL types
- --
- 2.31.0
- From 67807eaaf15f2f6b96052861c98c8ba1d77a181d Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Thu, 11 Feb 2021 20:08:50 +0100
- Subject: [PATCH] HID: Add support for Surface Aggregator Module HID transport
- Add a HID transport driver to support integrated HID devices on newer
- Microsoft Surface models (specifically 7th-generation, i.e. Surface
- Laptop 3, Surface Book 3, and later).
- On those models, the internal keyboard and touchpad (as well as some
- other HID devices with currently unknown function) are connected via the
- generic HID subsystem (TC=0x15) of the Surface System Aggregator Module
- (SSAM). This subsystem provides a generic HID transport layer, support
- for which is implemented by this driver.
- Patchset: surface-sam
- ---
- MAINTAINERS | 7 +
- drivers/hid/Kconfig | 2 +
- drivers/hid/Makefile | 2 +
- drivers/hid/surface-hid/Kconfig | 28 +++
- drivers/hid/surface-hid/Makefile | 6 +
- drivers/hid/surface-hid/surface_hid.c | 256 +++++++++++++++++++
- drivers/hid/surface-hid/surface_hid_core.c | 272 +++++++++++++++++++++
- drivers/hid/surface-hid/surface_hid_core.h | 77 ++++++
- 8 files changed, 650 insertions(+)
- create mode 100644 drivers/hid/surface-hid/Kconfig
- create mode 100644 drivers/hid/surface-hid/Makefile
- create mode 100644 drivers/hid/surface-hid/surface_hid.c
- create mode 100644 drivers/hid/surface-hid/surface_hid_core.c
- create mode 100644 drivers/hid/surface-hid/surface_hid_core.h
- diff --git a/MAINTAINERS b/MAINTAINERS
- index a6a4f5afdfa8..2eac975aee50 100644
- --- a/MAINTAINERS
- +++ b/MAINTAINERS
- @@ -11808,6 +11808,13 @@ S: Maintained
- T: git git://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git
- F: drivers/platform/surface/
-
- +MICROSOFT SURFACE HID TRANSPORT DRIVER
- +M: Maximilian Luz <luzmaximilian@gmail.com>
- +L: linux-input@vger.kernel.org
- +L: platform-driver-x86@vger.kernel.org
- +S: Maintained
- +F: drivers/hid/surface-hid/
- +
- MICROSOFT SURFACE PRO 3 BUTTON DRIVER
- M: Chen Yu <yu.c.chen@intel.com>
- L: platform-driver-x86@vger.kernel.org
- diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
- index 09fa75a2b289..e82474897919 100644
- --- a/drivers/hid/Kconfig
- +++ b/drivers/hid/Kconfig
- @@ -1187,4 +1187,6 @@ source "drivers/hid/intel-ish-hid/Kconfig"
-
- source "drivers/hid/amd-sfh-hid/Kconfig"
-
- +source "drivers/hid/surface-hid/Kconfig"
- +
- endmenu
- diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
- index 014d21fe7dac..98d6b9f666ec 100644
- --- a/drivers/hid/Makefile
- +++ b/drivers/hid/Makefile
- @@ -144,3 +144,5 @@ obj-$(CONFIG_INTEL_ISH_HID) += intel-ish-hid/
- obj-$(INTEL_ISH_FIRMWARE_DOWNLOADER) += intel-ish-hid/
-
- obj-$(CONFIG_AMD_SFH_HID) += amd-sfh-hid/
- +
- +obj-$(CONFIG_SURFACE_HID_CORE) += surface-hid/
- diff --git a/drivers/hid/surface-hid/Kconfig b/drivers/hid/surface-hid/Kconfig
- new file mode 100644
- index 000000000000..642c7f0e64fe
- --- /dev/null
- +++ b/drivers/hid/surface-hid/Kconfig
- @@ -0,0 +1,28 @@
- +# SPDX-License-Identifier: GPL-2.0+
- +menu "Surface System Aggregator Module HID support"
- + depends on SURFACE_AGGREGATOR
- + depends on INPUT
- +
- +config SURFACE_HID
- + tristate "HID transport driver for Surface System Aggregator Module"
- + depends on SURFACE_AGGREGATOR_REGISTRY
- + select SURFACE_HID_CORE
- + help
- + Driver to support integrated HID devices on newer Microsoft Surface
- + models.
- +
- + This driver provides support for the HID transport protocol provided
- + by the Surface Aggregator Module (i.e. the embedded controller) on
- + 7th-generation Microsoft Surface devices, i.e. Surface Book 3 and
- + Surface Laptop 3. On those models, it is mainly used to connect the
- + integrated touchpad and keyboard.
- +
- + Say M or Y here, if you want support for integrated HID devices, i.e.
- + integrated touchpad and keyboard, on 7th generation Microsoft Surface
- + models.
- +
- +endmenu
- +
- +config SURFACE_HID_CORE
- + tristate
- + select HID
- diff --git a/drivers/hid/surface-hid/Makefile b/drivers/hid/surface-hid/Makefile
- new file mode 100644
- index 000000000000..62fc04632d3d
- --- /dev/null
- +++ b/drivers/hid/surface-hid/Makefile
- @@ -0,0 +1,6 @@
- +# SPDX-License-Identifier: GPL-2.0+
- +#
- +# Makefile - Surface System Aggregator Module (SSAM) HID transport driver.
- +#
- +obj-$(CONFIG_SURFACE_HID_CORE) += surface_hid_core.o
- +obj-$(CONFIG_SURFACE_HID) += surface_hid.o
- diff --git a/drivers/hid/surface-hid/surface_hid.c b/drivers/hid/surface-hid/surface_hid.c
- new file mode 100644
- index 000000000000..e4477c328536
- --- /dev/null
- +++ b/drivers/hid/surface-hid/surface_hid.c
- @@ -0,0 +1,256 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * Surface System Aggregator Module (SSAM) HID transport driver for the
- + * generic HID interface (HID/TC=0x15 subsystem). Provides support for
- + * integrated HID devices on Surface Laptop 3, Book 3, and later.
- + *
- + * Copyright (C) 2019-2021 Blaž Hrastnik <blaz@mxxn.io>,
- + * Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <asm/unaligned.h>
- +#include <linux/hid.h>
- +#include <linux/kernel.h>
- +#include <linux/module.h>
- +#include <linux/types.h>
- +
- +#include <linux/surface_aggregator/controller.h>
- +#include <linux/surface_aggregator/device.h>
- +
- +#include "surface_hid_core.h"
- +
- +
- +/* -- SAM interface. -------------------------------------------------------- */
- +
- +struct surface_hid_buffer_slice {
- + __u8 entry;
- + __le32 offset;
- + __le32 length;
- + __u8 end;
- + __u8 data[];
- +} __packed;
- +
- +static_assert(sizeof(struct surface_hid_buffer_slice) == 10);
- +
- +enum surface_hid_cid {
- + SURFACE_HID_CID_OUTPUT_REPORT = 0x01,
- + SURFACE_HID_CID_GET_FEATURE_REPORT = 0x02,
- + SURFACE_HID_CID_SET_FEATURE_REPORT = 0x03,
- + SURFACE_HID_CID_GET_DESCRIPTOR = 0x04,
- +};
- +
- +static int ssam_hid_get_descriptor(struct surface_hid_device *shid, u8 entry, u8 *buf, size_t len)
- +{
- + u8 buffer[sizeof(struct surface_hid_buffer_slice) + 0x76];
- + struct surface_hid_buffer_slice *slice;
- + struct ssam_request rqst;
- + struct ssam_response rsp;
- + u32 buffer_len, offset, length;
- + int status;
- +
- + /*
- + * Note: The 0x76 above has been chosen because that's what's used by
- + * the Windows driver. Together with the header, this leads to a 128
- + * byte payload in total.
- + */
- +
- + buffer_len = ARRAY_SIZE(buffer) - sizeof(struct surface_hid_buffer_slice);
- +
- + rqst.target_category = shid->uid.category;
- + rqst.target_id = shid->uid.target;
- + rqst.command_id = SURFACE_HID_CID_GET_DESCRIPTOR;
- + rqst.instance_id = shid->uid.instance;
- + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
- + rqst.length = sizeof(struct surface_hid_buffer_slice);
- + rqst.payload = buffer;
- +
- + rsp.capacity = ARRAY_SIZE(buffer);
- + rsp.pointer = buffer;
- +
- + slice = (struct surface_hid_buffer_slice *)buffer;
- + slice->entry = entry;
- + slice->end = 0;
- +
- + offset = 0;
- + length = buffer_len;
- +
- + while (!slice->end && offset < len) {
- + put_unaligned_le32(offset, &slice->offset);
- + put_unaligned_le32(length, &slice->length);
- +
- + rsp.length = 0;
- +
- + status = ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, &rsp,
- + sizeof(*slice));
- + if (status)
- + return status;
- +
- + offset = get_unaligned_le32(&slice->offset);
- + length = get_unaligned_le32(&slice->length);
- +
- + /* Don't mess stuff up in case we receive garbage. */
- + if (length > buffer_len || offset > len)
- + return -EPROTO;
- +
- + if (offset + length > len)
- + length = len - offset;
- +
- + memcpy(buf + offset, &slice->data[0], length);
- +
- + offset += length;
- + length = buffer_len;
- + }
- +
- + if (offset != len) {
- + dev_err(shid->dev, "unexpected descriptor length: got %u, expected %zu\n",
- + offset, len);
- + return -EPROTO;
- + }
- +
- + return 0;
- +}
- +
- +static int ssam_hid_set_raw_report(struct surface_hid_device *shid, u8 rprt_id, bool feature,
- + u8 *buf, size_t len)
- +{
- + struct ssam_request rqst;
- + u8 cid;
- +
- + if (feature)
- + cid = SURFACE_HID_CID_SET_FEATURE_REPORT;
- + else
- + cid = SURFACE_HID_CID_OUTPUT_REPORT;
- +
- + rqst.target_category = shid->uid.category;
- + rqst.target_id = shid->uid.target;
- + rqst.instance_id = shid->uid.instance;
- + rqst.command_id = cid;
- + rqst.flags = 0;
- + rqst.length = len;
- + rqst.payload = buf;
- +
- + buf[0] = rprt_id;
- +
- + return ssam_retry(ssam_request_sync, shid->ctrl, &rqst, NULL);
- +}
- +
- +static int ssam_hid_get_raw_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
- +{
- + struct ssam_request rqst;
- + struct ssam_response rsp;
- +
- + rqst.target_category = shid->uid.category;
- + rqst.target_id = shid->uid.target;
- + rqst.instance_id = shid->uid.instance;
- + rqst.command_id = SURFACE_HID_CID_GET_FEATURE_REPORT;
- + rqst.flags = 0;
- + rqst.length = sizeof(rprt_id);
- + rqst.payload = &rprt_id;
- +
- + rsp.capacity = len;
- + rsp.length = 0;
- + rsp.pointer = buf;
- +
- + return ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, &rsp, sizeof(rprt_id));
- +}
- +
- +static u32 ssam_hid_event_fn(struct ssam_event_notifier *nf, const struct ssam_event *event)
- +{
- + struct surface_hid_device *shid = container_of(nf, struct surface_hid_device, notif);
- + int status;
- +
- + if (event->command_id != 0x00)
- + return 0;
- +
- + status = hid_input_report(shid->hid, HID_INPUT_REPORT, (u8 *)&event->data[0],
- + event->length, 0);
- +
- + return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
- +}
- +
- +
- +/* -- Transport driver. ----------------------------------------------------- */
- +
- +static int shid_output_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
- +{
- + int status;
- +
- + status = ssam_hid_set_raw_report(shid, rprt_id, false, buf, len);
- + return status >= 0 ? len : status;
- +}
- +
- +static int shid_get_feature_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
- +{
- + int status;
- +
- + status = ssam_hid_get_raw_report(shid, rprt_id, buf, len);
- + return status >= 0 ? len : status;
- +}
- +
- +static int shid_set_feature_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
- +{
- + int status;
- +
- + status = ssam_hid_set_raw_report(shid, rprt_id, true, buf, len);
- + return status >= 0 ? len : status;
- +}
- +
- +
- +/* -- Driver setup. --------------------------------------------------------- */
- +
- +static int surface_hid_probe(struct ssam_device *sdev)
- +{
- + struct surface_hid_device *shid;
- +
- + shid = devm_kzalloc(&sdev->dev, sizeof(*shid), GFP_KERNEL);
- + if (!shid)
- + return -ENOMEM;
- +
- + shid->dev = &sdev->dev;
- + shid->ctrl = sdev->ctrl;
- + shid->uid = sdev->uid;
- +
- + shid->notif.base.priority = 1;
- + shid->notif.base.fn = ssam_hid_event_fn;
- + shid->notif.event.reg = SSAM_EVENT_REGISTRY_REG;
- + shid->notif.event.id.target_category = sdev->uid.category;
- + shid->notif.event.id.instance = sdev->uid.instance;
- + shid->notif.event.mask = SSAM_EVENT_MASK_STRICT;
- + shid->notif.event.flags = 0;
- +
- + shid->ops.get_descriptor = ssam_hid_get_descriptor;
- + shid->ops.output_report = shid_output_report;
- + shid->ops.get_feature_report = shid_get_feature_report;
- + shid->ops.set_feature_report = shid_set_feature_report;
- +
- + ssam_device_set_drvdata(sdev, shid);
- + return surface_hid_device_add(shid);
- +}
- +
- +static void surface_hid_remove(struct ssam_device *sdev)
- +{
- + surface_hid_device_destroy(ssam_device_get_drvdata(sdev));
- +}
- +
- +static const struct ssam_device_id surface_hid_match[] = {
- + { SSAM_SDEV(HID, 0x02, SSAM_ANY_IID, 0x00) },
- + { },
- +};
- +MODULE_DEVICE_TABLE(ssam, surface_hid_match);
- +
- +static struct ssam_device_driver surface_hid_driver = {
- + .probe = surface_hid_probe,
- + .remove = surface_hid_remove,
- + .match_table = surface_hid_match,
- + .driver = {
- + .name = "surface_hid",
- + .pm = &surface_hid_pm_ops,
- + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- + },
- +};
- +module_ssam_device_driver(surface_hid_driver);
- +
- +MODULE_AUTHOR("Blaž Hrastnik <blaz@mxxn.io>");
- +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- +MODULE_DESCRIPTION("HID transport driver for Surface System Aggregator Module");
- +MODULE_LICENSE("GPL");
- diff --git a/drivers/hid/surface-hid/surface_hid_core.c b/drivers/hid/surface-hid/surface_hid_core.c
- new file mode 100644
- index 000000000000..2cadb8013529
- --- /dev/null
- +++ b/drivers/hid/surface-hid/surface_hid_core.c
- @@ -0,0 +1,272 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * Common/core components for the Surface System Aggregator Module (SSAM) HID
- + * transport driver. Provides support for integrated HID devices on Microsoft
- + * Surface models.
- + *
- + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <asm/unaligned.h>
- +#include <linux/hid.h>
- +#include <linux/kernel.h>
- +#include <linux/module.h>
- +#include <linux/types.h>
- +#include <linux/usb/ch9.h>
- +
- +#include <linux/surface_aggregator/controller.h>
- +
- +#include "surface_hid_core.h"
- +
- +
- +/* -- Device descriptor access. --------------------------------------------- */
- +
- +static int surface_hid_load_hid_descriptor(struct surface_hid_device *shid)
- +{
- + int status;
- +
- + status = shid->ops.get_descriptor(shid, SURFACE_HID_DESC_HID,
- + (u8 *)&shid->hid_desc, sizeof(shid->hid_desc));
- + if (status)
- + return status;
- +
- + if (shid->hid_desc.desc_len != sizeof(shid->hid_desc)) {
- + dev_err(shid->dev, "unexpected HID descriptor length: got %u, expected %zu\n",
- + shid->hid_desc.desc_len, sizeof(shid->hid_desc));
- + return -EPROTO;
- + }
- +
- + if (shid->hid_desc.desc_type != HID_DT_HID) {
- + dev_err(shid->dev, "unexpected HID descriptor type: got %#04x, expected %#04x\n",
- + shid->hid_desc.desc_type, HID_DT_HID);
- + return -EPROTO;
- + }
- +
- + if (shid->hid_desc.num_descriptors != 1) {
- + dev_err(shid->dev, "unexpected number of descriptors: got %u, expected 1\n",
- + shid->hid_desc.num_descriptors);
- + return -EPROTO;
- + }
- +
- + if (shid->hid_desc.report_desc_type != HID_DT_REPORT) {
- + dev_err(shid->dev, "unexpected report descriptor type: got %#04x, expected %#04x\n",
- + shid->hid_desc.report_desc_type, HID_DT_REPORT);
- + return -EPROTO;
- + }
- +
- + return 0;
- +}
- +
- +static int surface_hid_load_device_attributes(struct surface_hid_device *shid)
- +{
- + int status;
- +
- + status = shid->ops.get_descriptor(shid, SURFACE_HID_DESC_ATTRS,
- + (u8 *)&shid->attrs, sizeof(shid->attrs));
- + if (status)
- + return status;
- +
- + if (get_unaligned_le32(&shid->attrs.length) != sizeof(shid->attrs)) {
- + dev_err(shid->dev, "unexpected attribute length: got %u, expected %zu\n",
- + get_unaligned_le32(&shid->attrs.length), sizeof(shid->attrs));
- + return -EPROTO;
- + }
- +
- + return 0;
- +}
- +
- +
- +/* -- Transport driver (common). -------------------------------------------- */
- +
- +static int surface_hid_start(struct hid_device *hid)
- +{
- + struct surface_hid_device *shid = hid->driver_data;
- +
- + return ssam_notifier_register(shid->ctrl, &shid->notif);
- +}
- +
- +static void surface_hid_stop(struct hid_device *hid)
- +{
- + struct surface_hid_device *shid = hid->driver_data;
- +
- + /* Note: This call will log errors for us, so ignore them here. */
- + ssam_notifier_unregister(shid->ctrl, &shid->notif);
- +}
- +
- +static int surface_hid_open(struct hid_device *hid)
- +{
- + return 0;
- +}
- +
- +static void surface_hid_close(struct hid_device *hid)
- +{
- +}
- +
- +static int surface_hid_parse(struct hid_device *hid)
- +{
- + struct surface_hid_device *shid = hid->driver_data;
- + size_t len = get_unaligned_le16(&shid->hid_desc.report_desc_len);
- + u8 *buf;
- + int status;
- +
- + buf = kzalloc(len, GFP_KERNEL);
- + if (!buf)
- + return -ENOMEM;
- +
- + status = shid->ops.get_descriptor(shid, SURFACE_HID_DESC_REPORT, buf, len);
- + if (!status)
- + status = hid_parse_report(hid, buf, len);
- +
- + kfree(buf);
- + return status;
- +}
- +
- +static int surface_hid_raw_request(struct hid_device *hid, unsigned char reportnum, u8 *buf,
- + size_t len, unsigned char rtype, int reqtype)
- +{
- + struct surface_hid_device *shid = hid->driver_data;
- +
- + if (rtype == HID_OUTPUT_REPORT && reqtype == HID_REQ_SET_REPORT)
- + return shid->ops.output_report(shid, reportnum, buf, len);
- +
- + else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_GET_REPORT)
- + return shid->ops.get_feature_report(shid, reportnum, buf, len);
- +
- + else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_SET_REPORT)
- + return shid->ops.set_feature_report(shid, reportnum, buf, len);
- +
- + return -EIO;
- +}
- +
- +static struct hid_ll_driver surface_hid_ll_driver = {
- + .start = surface_hid_start,
- + .stop = surface_hid_stop,
- + .open = surface_hid_open,
- + .close = surface_hid_close,
- + .parse = surface_hid_parse,
- + .raw_request = surface_hid_raw_request,
- +};
- +
- +
- +/* -- Common device setup. -------------------------------------------------- */
- +
- +int surface_hid_device_add(struct surface_hid_device *shid)
- +{
- + int status;
- +
- + status = surface_hid_load_hid_descriptor(shid);
- + if (status)
- + return status;
- +
- + status = surface_hid_load_device_attributes(shid);
- + if (status)
- + return status;
- +
- + shid->hid = hid_allocate_device();
- + if (IS_ERR(shid->hid))
- + return PTR_ERR(shid->hid);
- +
- + shid->hid->dev.parent = shid->dev;
- + shid->hid->bus = BUS_SURFACE_AGGREGATOR;
- + shid->hid->vendor = cpu_to_le16(shid->attrs.vendor);
- + shid->hid->product = cpu_to_le16(shid->attrs.product);
- + shid->hid->version = cpu_to_le16(shid->hid_desc.hid_version);
- + shid->hid->country = shid->hid_desc.country_code;
- +
- + snprintf(shid->hid->name, sizeof(shid->hid->name), "Microsoft Surface %04X:%04X",
- + shid->hid->vendor, shid->hid->product);
- +
- + strscpy(shid->hid->phys, dev_name(shid->dev), sizeof(shid->hid->phys));
- +
- + shid->hid->driver_data = shid;
- + shid->hid->ll_driver = &surface_hid_ll_driver;
- +
- + status = hid_add_device(shid->hid);
- + if (status)
- + hid_destroy_device(shid->hid);
- +
- + return status;
- +}
- +EXPORT_SYMBOL_GPL(surface_hid_device_add);
- +
- +void surface_hid_device_destroy(struct surface_hid_device *shid)
- +{
- + hid_destroy_device(shid->hid);
- +}
- +EXPORT_SYMBOL_GPL(surface_hid_device_destroy);
- +
- +
- +/* -- PM ops. --------------------------------------------------------------- */
- +
- +#ifdef CONFIG_PM_SLEEP
- +
- +static int surface_hid_suspend(struct device *dev)
- +{
- + struct surface_hid_device *d = dev_get_drvdata(dev);
- +
- + if (d->hid->driver && d->hid->driver->suspend)
- + return d->hid->driver->suspend(d->hid, PMSG_SUSPEND);
- +
- + return 0;
- +}
- +
- +static int surface_hid_resume(struct device *dev)
- +{
- + struct surface_hid_device *d = dev_get_drvdata(dev);
- +
- + if (d->hid->driver && d->hid->driver->resume)
- + return d->hid->driver->resume(d->hid);
- +
- + return 0;
- +}
- +
- +static int surface_hid_freeze(struct device *dev)
- +{
- + struct surface_hid_device *d = dev_get_drvdata(dev);
- +
- + if (d->hid->driver && d->hid->driver->suspend)
- + return d->hid->driver->suspend(d->hid, PMSG_FREEZE);
- +
- + return 0;
- +}
- +
- +static int surface_hid_poweroff(struct device *dev)
- +{
- + struct surface_hid_device *d = dev_get_drvdata(dev);
- +
- + if (d->hid->driver && d->hid->driver->suspend)
- + return d->hid->driver->suspend(d->hid, PMSG_HIBERNATE);
- +
- + return 0;
- +}
- +
- +static int surface_hid_restore(struct device *dev)
- +{
- + struct surface_hid_device *d = dev_get_drvdata(dev);
- +
- + if (d->hid->driver && d->hid->driver->reset_resume)
- + return d->hid->driver->reset_resume(d->hid);
- +
- + return 0;
- +}
- +
- +const struct dev_pm_ops surface_hid_pm_ops = {
- + .freeze = surface_hid_freeze,
- + .thaw = surface_hid_resume,
- + .suspend = surface_hid_suspend,
- + .resume = surface_hid_resume,
- + .poweroff = surface_hid_poweroff,
- + .restore = surface_hid_restore,
- +};
- +EXPORT_SYMBOL_GPL(surface_hid_pm_ops);
- +
- +#else /* CONFIG_PM_SLEEP */
- +
- +const struct dev_pm_ops surface_hid_pm_ops = { };
- +EXPORT_SYMBOL_GPL(surface_hid_pm_ops);
- +
- +#endif /* CONFIG_PM_SLEEP */
- +
- +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- +MODULE_DESCRIPTION("HID transport driver core for Surface System Aggregator Module");
- +MODULE_LICENSE("GPL");
- diff --git a/drivers/hid/surface-hid/surface_hid_core.h b/drivers/hid/surface-hid/surface_hid_core.h
- new file mode 100644
- index 000000000000..4b1a7b57e035
- --- /dev/null
- +++ b/drivers/hid/surface-hid/surface_hid_core.h
- @@ -0,0 +1,77 @@
- +/* SPDX-License-Identifier: GPL-2.0+ */
- +/*
- + * Common/core components for the Surface System Aggregator Module (SSAM) HID
- + * transport driver. Provides support for integrated HID devices on Microsoft
- + * Surface models.
- + *
- + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#ifndef SURFACE_HID_CORE_H
- +#define SURFACE_HID_CORE_H
- +
- +#include <linux/hid.h>
- +#include <linux/pm.h>
- +#include <linux/types.h>
- +
- +#include <linux/surface_aggregator/controller.h>
- +#include <linux/surface_aggregator/device.h>
- +
- +enum surface_hid_descriptor_entry {
- + SURFACE_HID_DESC_HID = 0,
- + SURFACE_HID_DESC_REPORT = 1,
- + SURFACE_HID_DESC_ATTRS = 2,
- +};
- +
- +struct surface_hid_descriptor {
- + __u8 desc_len; /* = 9 */
- + __u8 desc_type; /* = HID_DT_HID */
- + __le16 hid_version;
- + __u8 country_code;
- + __u8 num_descriptors; /* = 1 */
- +
- + __u8 report_desc_type; /* = HID_DT_REPORT */
- + __le16 report_desc_len;
- +} __packed;
- +
- +static_assert(sizeof(struct surface_hid_descriptor) == 9);
- +
- +struct surface_hid_attributes {
- + __le32 length;
- + __le16 vendor;
- + __le16 product;
- + __le16 version;
- + __u8 _unknown[22];
- +} __packed;
- +
- +static_assert(sizeof(struct surface_hid_attributes) == 32);
- +
- +struct surface_hid_device;
- +
- +struct surface_hid_device_ops {
- + int (*get_descriptor)(struct surface_hid_device *shid, u8 entry, u8 *buf, size_t len);
- + int (*output_report)(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len);
- + int (*get_feature_report)(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len);
- + int (*set_feature_report)(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len);
- +};
- +
- +struct surface_hid_device {
- + struct device *dev;
- + struct ssam_controller *ctrl;
- + struct ssam_device_uid uid;
- +
- + struct surface_hid_descriptor hid_desc;
- + struct surface_hid_attributes attrs;
- +
- + struct ssam_event_notifier notif;
- + struct hid_device *hid;
- +
- + struct surface_hid_device_ops ops;
- +};
- +
- +int surface_hid_device_add(struct surface_hid_device *shid);
- +void surface_hid_device_destroy(struct surface_hid_device *shid);
- +
- +extern const struct dev_pm_ops surface_hid_pm_ops;
- +
- +#endif /* SURFACE_HID_CORE_H */
- --
- 2.31.0
- From 793ac74222c86e9e445aab0eded525edd6b75282 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Thu, 11 Feb 2021 20:10:17 +0100
- Subject: [PATCH] HID: surface-hid: Add support for legacy keyboard interface
- Add support for the legacy keyboard (KBD/TC=0x08) HID transport layer of
- the Surface System Aggregator Module (SSAM) to the Surface HID driver.
- On Surface Laptops 1 and 2, this interface is used to connect the
- integrated keyboard.
- Note that this subsystem interface essentially provides a limited HID
- transport layer. In contras to the generic HID interface (TC=0x15) used
- on newer Surface models, this interface only allows (as far as we know)
- for a single device to be connected and is otherwise severely limited in
- terms of support for feature- and output-reports. Specifically, only
- caps-lock-LED output-reports and a single read-only feature-report are
- supported.
- Patchset: surface-sam
- ---
- drivers/hid/surface-hid/Kconfig | 14 ++
- drivers/hid/surface-hid/Makefile | 1 +
- drivers/hid/surface-hid/surface_hid.c | 7 +-
- drivers/hid/surface-hid/surface_kbd.c | 300 ++++++++++++++++++++++++++
- 4 files changed, 317 insertions(+), 5 deletions(-)
- create mode 100644 drivers/hid/surface-hid/surface_kbd.c
- diff --git a/drivers/hid/surface-hid/Kconfig b/drivers/hid/surface-hid/Kconfig
- index 642c7f0e64fe..7ce9b5d641eb 100644
- --- a/drivers/hid/surface-hid/Kconfig
- +++ b/drivers/hid/surface-hid/Kconfig
- @@ -21,6 +21,20 @@ config SURFACE_HID
- integrated touchpad and keyboard, on 7th generation Microsoft Surface
- models.
-
- +config SURFACE_KBD
- + tristate "HID keyboard transport driver for Surface System Aggregator Module"
- + select SURFACE_HID_CORE
- + help
- + Driver to support HID keyboards on Surface Laptop 1 and 2 devices.
- +
- + This driver provides support for the HID transport protocol provided
- + by the Surface Aggregator Module (i.e. the embedded controller) on
- + Microsoft Surface Laptops 1 and 2. It is used to connect the
- + integrated keyboard on those devices.
- +
- + Say M or Y here, if you want support for the integrated keyboard on
- + Microsoft Surface Laptops 1 and 2.
- +
- endmenu
-
- config SURFACE_HID_CORE
- diff --git a/drivers/hid/surface-hid/Makefile b/drivers/hid/surface-hid/Makefile
- index 62fc04632d3d..4ae11cf09b25 100644
- --- a/drivers/hid/surface-hid/Makefile
- +++ b/drivers/hid/surface-hid/Makefile
- @@ -4,3 +4,4 @@
- #
- obj-$(CONFIG_SURFACE_HID_CORE) += surface_hid_core.o
- obj-$(CONFIG_SURFACE_HID) += surface_hid.o
- +obj-$(CONFIG_SURFACE_KBD) += surface_kbd.o
- diff --git a/drivers/hid/surface-hid/surface_hid.c b/drivers/hid/surface-hid/surface_hid.c
- index e4477c328536..3477b31611ae 100644
- --- a/drivers/hid/surface-hid/surface_hid.c
- +++ b/drivers/hid/surface-hid/surface_hid.c
- @@ -157,15 +157,12 @@ static int ssam_hid_get_raw_report(struct surface_hid_device *shid, u8 rprt_id,
- static u32 ssam_hid_event_fn(struct ssam_event_notifier *nf, const struct ssam_event *event)
- {
- struct surface_hid_device *shid = container_of(nf, struct surface_hid_device, notif);
- - int status;
-
- if (event->command_id != 0x00)
- return 0;
-
- - status = hid_input_report(shid->hid, HID_INPUT_REPORT, (u8 *)&event->data[0],
- - event->length, 0);
- -
- - return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
- + hid_input_report(shid->hid, HID_INPUT_REPORT, (u8 *)&event->data[0], event->length, 0);
- + return SSAM_NOTIF_HANDLED;
- }
-
-
- diff --git a/drivers/hid/surface-hid/surface_kbd.c b/drivers/hid/surface-hid/surface_kbd.c
- new file mode 100644
- index 000000000000..0635341bc517
- --- /dev/null
- +++ b/drivers/hid/surface-hid/surface_kbd.c
- @@ -0,0 +1,300 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * Surface System Aggregator Module (SSAM) HID transport driver for the legacy
- + * keyboard interface (KBD/TC=0x08 subsystem). Provides support for the
- + * integrated HID keyboard on Surface Laptops 1 and 2.
- + *
- + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <asm/unaligned.h>
- +#include <linux/hid.h>
- +#include <linux/kernel.h>
- +#include <linux/module.h>
- +#include <linux/platform_device.h>
- +#include <linux/types.h>
- +
- +#include <linux/surface_aggregator/controller.h>
- +
- +#include "surface_hid_core.h"
- +
- +
- +/* -- SAM interface (KBD). -------------------------------------------------- */
- +
- +#define KBD_FEATURE_REPORT_SIZE 7 /* 6 + report ID */
- +
- +enum surface_kbd_cid {
- + SURFACE_KBD_CID_GET_DESCRIPTOR = 0x00,
- + SURFACE_KBD_CID_SET_CAPSLOCK_LED = 0x01,
- + SURFACE_KBD_CID_EVT_INPUT_GENERIC = 0x03,
- + SURFACE_KBD_CID_EVT_INPUT_HOTKEYS = 0x04,
- + SURFACE_KBD_CID_GET_FEATURE_REPORT = 0x0b,
- +};
- +
- +static int ssam_kbd_get_descriptor(struct surface_hid_device *shid, u8 entry, u8 *buf, size_t len)
- +{
- + struct ssam_request rqst;
- + struct ssam_response rsp;
- + int status;
- +
- + rqst.target_category = shid->uid.category;
- + rqst.target_id = shid->uid.target;
- + rqst.command_id = SURFACE_KBD_CID_GET_DESCRIPTOR;
- + rqst.instance_id = shid->uid.instance;
- + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
- + rqst.length = sizeof(entry);
- + rqst.payload = &entry;
- +
- + rsp.capacity = len;
- + rsp.length = 0;
- + rsp.pointer = buf;
- +
- + status = ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, &rsp, sizeof(entry));
- + if (status)
- + return status;
- +
- + if (rsp.length != len) {
- + dev_err(shid->dev, "invalid descriptor length: got %zu, expected, %zu\n",
- + rsp.length, len);
- + return -EPROTO;
- + }
- +
- + return 0;
- +}
- +
- +static int ssam_kbd_set_caps_led(struct surface_hid_device *shid, bool value)
- +{
- + struct ssam_request rqst;
- + u8 value_u8 = value;
- +
- + rqst.target_category = shid->uid.category;
- + rqst.target_id = shid->uid.target;
- + rqst.command_id = SURFACE_KBD_CID_SET_CAPSLOCK_LED;
- + rqst.instance_id = shid->uid.instance;
- + rqst.flags = 0;
- + rqst.length = sizeof(value_u8);
- + rqst.payload = &value_u8;
- +
- + return ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, NULL, sizeof(value_u8));
- +}
- +
- +static int ssam_kbd_get_feature_report(struct surface_hid_device *shid, u8 *buf, size_t len)
- +{
- + struct ssam_request rqst;
- + struct ssam_response rsp;
- + u8 payload = 0;
- + int status;
- +
- + rqst.target_category = shid->uid.category;
- + rqst.target_id = shid->uid.target;
- + rqst.command_id = SURFACE_KBD_CID_GET_FEATURE_REPORT;
- + rqst.instance_id = shid->uid.instance;
- + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
- + rqst.length = sizeof(payload);
- + rqst.payload = &payload;
- +
- + rsp.capacity = len;
- + rsp.length = 0;
- + rsp.pointer = buf;
- +
- + status = ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, &rsp, sizeof(payload));
- + if (status)
- + return status;
- +
- + if (rsp.length != len) {
- + dev_err(shid->dev, "invalid feature report length: got %zu, expected, %zu\n",
- + rsp.length, len);
- + return -EPROTO;
- + }
- +
- + return 0;
- +}
- +
- +static bool ssam_kbd_is_input_event(const struct ssam_event *event)
- +{
- + if (event->command_id == SURFACE_KBD_CID_EVT_INPUT_GENERIC)
- + return true;
- +
- + if (event->command_id == SURFACE_KBD_CID_EVT_INPUT_HOTKEYS)
- + return true;
- +
- + return false;
- +}
- +
- +static u32 ssam_kbd_event_fn(struct ssam_event_notifier *nf, const struct ssam_event *event)
- +{
- + struct surface_hid_device *shid = container_of(nf, struct surface_hid_device, notif);
- +
- + /*
- + * Check against device UID manually, as registry and device target
- + * category doesn't line up.
- + */
- +
- + if (shid->uid.category != event->target_category)
- + return 0;
- +
- + if (shid->uid.target != event->target_id)
- + return 0;
- +
- + if (shid->uid.instance != event->instance_id)
- + return 0;
- +
- + if (!ssam_kbd_is_input_event(event))
- + return 0;
- +
- + hid_input_report(shid->hid, HID_INPUT_REPORT, (u8 *)&event->data[0], event->length, 0);
- + return SSAM_NOTIF_HANDLED;
- +}
- +
- +
- +/* -- Transport driver (KBD). ----------------------------------------------- */
- +
- +static int skbd_get_caps_led_value(struct hid_device *hid, u8 rprt_id, u8 *buf, size_t len)
- +{
- + struct hid_field *field;
- + unsigned int offset, size;
- + int i;
- +
- + /* Get LED field. */
- + field = hidinput_get_led_field(hid);
- + if (!field)
- + return -ENOENT;
- +
- + /* Check if we got the correct report. */
- + if (len != hid_report_len(field->report))
- + return -ENOENT;
- +
- + if (rprt_id != field->report->id)
- + return -ENOENT;
- +
- + /* Get caps lock LED index. */
- + for (i = 0; i < field->report_count; i++)
- + if ((field->usage[i].hid & 0xffff) == 0x02)
- + break;
- +
- + if (i == field->report_count)
- + return -ENOENT;
- +
- + /* Extract value. */
- + size = field->report_size;
- + offset = field->report_offset + i * size;
- + return !!hid_field_extract(hid, buf + 1, size, offset);
- +}
- +
- +static int skbd_output_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
- +{
- + int caps_led;
- + int status;
- +
- + caps_led = skbd_get_caps_led_value(shid->hid, rprt_id, buf, len);
- + if (caps_led < 0)
- + return -EIO; /* Only caps LED output reports are supported. */
- +
- + status = ssam_kbd_set_caps_led(shid, caps_led);
- + if (status < 0)
- + return status;
- +
- + return len;
- +}
- +
- +static int skbd_get_feature_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
- +{
- + u8 report[KBD_FEATURE_REPORT_SIZE];
- + int status;
- +
- + /*
- + * The keyboard only has a single hard-coded read-only feature report
- + * of size KBD_FEATURE_REPORT_SIZE. Try to load it and compare its
- + * report ID against the requested one.
- + */
- +
- + if (len < ARRAY_SIZE(report))
- + return -ENOSPC;
- +
- + status = ssam_kbd_get_feature_report(shid, report, ARRAY_SIZE(report));
- + if (status < 0)
- + return status;
- +
- + if (rprt_id != report[0])
- + return -ENOENT;
- +
- + memcpy(buf, report, ARRAY_SIZE(report));
- + return len;
- +}
- +
- +static int skbd_set_feature_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
- +{
- + /* Not supported. See skbd_get_feature_report() for details. */
- + return -EIO;
- +}
- +
- +
- +/* -- Driver setup. --------------------------------------------------------- */
- +
- +static int surface_kbd_probe(struct platform_device *pdev)
- +{
- + struct ssam_controller *ctrl;
- + struct surface_hid_device *shid;
- +
- + /* Add device link to EC. */
- + ctrl = ssam_client_bind(&pdev->dev);
- + if (IS_ERR(ctrl))
- + return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
- +
- + shid = devm_kzalloc(&pdev->dev, sizeof(*shid), GFP_KERNEL);
- + if (!shid)
- + return -ENOMEM;
- +
- + shid->dev = &pdev->dev;
- + shid->ctrl = ctrl;
- +
- + shid->uid.domain = SSAM_DOMAIN_SERIALHUB;
- + shid->uid.category = SSAM_SSH_TC_KBD;
- + shid->uid.target = 2;
- + shid->uid.instance = 0;
- + shid->uid.function = 0;
- +
- + shid->notif.base.priority = 1;
- + shid->notif.base.fn = ssam_kbd_event_fn;
- + shid->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
- + shid->notif.event.id.target_category = shid->uid.category;
- + shid->notif.event.id.instance = shid->uid.instance;
- + shid->notif.event.mask = SSAM_EVENT_MASK_NONE;
- + shid->notif.event.flags = 0;
- +
- + shid->ops.get_descriptor = ssam_kbd_get_descriptor;
- + shid->ops.output_report = skbd_output_report;
- + shid->ops.get_feature_report = skbd_get_feature_report;
- + shid->ops.set_feature_report = skbd_set_feature_report;
- +
- + platform_set_drvdata(pdev, shid);
- + return surface_hid_device_add(shid);
- +}
- +
- +static int surface_kbd_remove(struct platform_device *pdev)
- +{
- + surface_hid_device_destroy(platform_get_drvdata(pdev));
- + return 0;
- +}
- +
- +static const struct acpi_device_id surface_kbd_match[] = {
- + { "MSHW0096" },
- + { },
- +};
- +MODULE_DEVICE_TABLE(acpi, surface_kbd_match);
- +
- +static struct platform_driver surface_kbd_driver = {
- + .probe = surface_kbd_probe,
- + .remove = surface_kbd_remove,
- + .driver = {
- + .name = "surface_keyboard",
- + .acpi_match_table = surface_kbd_match,
- + .pm = &surface_hid_pm_ops,
- + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- + },
- +};
- +module_platform_driver(surface_kbd_driver);
- +
- +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- +MODULE_DESCRIPTION("HID legacy transport driver for Surface System Aggregator Module");
- +MODULE_LICENSE("GPL");
- --
- 2.31.0
- From eed7178fa7ca26b93d87546094922b5749763b2d Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Fri, 12 Feb 2021 21:06:12 +0100
- Subject: [PATCH] power: supply: Add battery driver for Surface Aggregator
- Module
- On newer Microsoft Surface models (specifically 7th-generation, i.e.
- Surface Pro 7, Surface Book 3, Surface Laptop 3, and Surface Laptop Go),
- battery and AC status/information is no longer handled via standard ACPI
- devices, but instead directly via the Surface System Aggregator Module
- (SSAM), i.e. the embedded controller on those devices.
- While on previous generation models, battery status is also handled via
- SSAM, an ACPI shim was present to translate the standard ACPI battery
- interface to SSAM requests. The SSAM interface itself, which is modeled
- closely after the ACPI interface, has not changed.
- This commit introduces a new SSAM client device driver to support
- battery status/information via the aforementioned interface on said
- Surface models. It is in parts based on the standard ACPI battery
- driver.
- Patchset: surface-sam
- ---
- MAINTAINERS | 7 +
- drivers/power/supply/Kconfig | 16 +
- drivers/power/supply/Makefile | 1 +
- drivers/power/supply/surface_battery.c | 901 +++++++++++++++++++++++++
- 4 files changed, 925 insertions(+)
- create mode 100644 drivers/power/supply/surface_battery.c
- diff --git a/MAINTAINERS b/MAINTAINERS
- index 2eac975aee50..54a4769114e9 100644
- --- a/MAINTAINERS
- +++ b/MAINTAINERS
- @@ -11785,6 +11785,13 @@ F: drivers/scsi/smartpqi/smartpqi*.[ch]
- F: include/linux/cciss*.h
- F: include/uapi/linux/cciss*.h
-
- +MICROSOFT SURFACE BATTERY AND AC DRIVERS
- +M: Maximilian Luz <luzmaximilian@gmail.com>
- +L: linux-pm@vger.kernel.org
- +L: platform-driver-x86@vger.kernel.org
- +S: Maintained
- +F: drivers/power/supply/surface_battery.c
- +
- MICROSOFT SURFACE DTX DRIVER
- M: Maximilian Luz <luzmaximilian@gmail.com>
- L: platform-driver-x86@vger.kernel.org
- diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
- index 1699b9269a78..b635331daba4 100644
- --- a/drivers/power/supply/Kconfig
- +++ b/drivers/power/supply/Kconfig
- @@ -775,4 +775,20 @@ config RN5T618_POWER
- This driver can also be built as a module. If so, the module will be
- called rn5t618_power.
-
- +config BATTERY_SURFACE
- + tristate "Battery driver for 7th-generation Microsoft Surface devices"
- + depends on SURFACE_AGGREGATOR_REGISTRY
- + help
- + Driver for battery devices connected via/managed by the Surface System
- + Aggregator Module (SSAM).
- +
- + This driver provides battery-information and -status support for
- + Surface devices where said data is not exposed via the standard ACPI
- + devices. On those models (7th-generation), battery-information is
- + instead handled directly via SSAM client devices and this driver.
- +
- + Say M or Y here to include battery status support for 7th-generation
- + Microsoft Surface devices, i.e. Surface Pro 7, Surface Laptop 3,
- + Surface Book 3, and Surface Laptop Go.
- +
- endif # POWER_SUPPLY
- diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
- index dd4b86318cd9..cddc18994119 100644
- --- a/drivers/power/supply/Makefile
- +++ b/drivers/power/supply/Makefile
- @@ -98,3 +98,4 @@ obj-$(CONFIG_CHARGER_BD70528) += bd70528-charger.o
- obj-$(CONFIG_CHARGER_BD99954) += bd99954-charger.o
- obj-$(CONFIG_CHARGER_WILCO) += wilco-charger.o
- obj-$(CONFIG_RN5T618_POWER) += rn5t618_power.o
- +obj-$(CONFIG_BATTERY_SURFACE) += surface_battery.o
- diff --git a/drivers/power/supply/surface_battery.c b/drivers/power/supply/surface_battery.c
- new file mode 100644
- index 000000000000..327fd7af386b
- --- /dev/null
- +++ b/drivers/power/supply/surface_battery.c
- @@ -0,0 +1,901 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * Battery driver for 7th-generation Microsoft Surface devices via Surface
- + * System Aggregator Module (SSAM).
- + *
- + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <asm/unaligned.h>
- +#include <linux/jiffies.h>
- +#include <linux/kernel.h>
- +#include <linux/module.h>
- +#include <linux/mutex.h>
- +#include <linux/power_supply.h>
- +#include <linux/sysfs.h>
- +#include <linux/types.h>
- +#include <linux/workqueue.h>
- +
- +#include <linux/surface_aggregator/device.h>
- +
- +
- +/* -- SAM interface. -------------------------------------------------------- */
- +
- +enum sam_event_cid_bat {
- + SAM_EVENT_CID_BAT_BIX = 0x15,
- + SAM_EVENT_CID_BAT_BST = 0x16,
- + SAM_EVENT_CID_BAT_ADP = 0x17,
- + SAM_EVENT_CID_BAT_PROT = 0x18,
- + SAM_EVENT_CID_BAT_DPTF = 0x53,
- +};
- +
- +enum sam_battery_sta {
- + SAM_BATTERY_STA_OK = 0x0f,
- + SAM_BATTERY_STA_PRESENT = 0x10,
- +};
- +
- +enum sam_battery_state {
- + SAM_BATTERY_STATE_DISCHARGING = BIT(0),
- + SAM_BATTERY_STATE_CHARGING = BIT(1),
- + SAM_BATTERY_STATE_CRITICAL = BIT(2),
- +};
- +
- +enum sam_battery_power_unit {
- + SAM_BATTERY_POWER_UNIT_mW = 0,
- + SAM_BATTERY_POWER_UNIT_mA = 1,
- +};
- +
- +/* Equivalent to data returned in ACPI _BIX method, revision 0. */
- +struct spwr_bix {
- + u8 revision;
- + __le32 power_unit;
- + __le32 design_cap;
- + __le32 last_full_charge_cap;
- + __le32 technology;
- + __le32 design_voltage;
- + __le32 design_cap_warn;
- + __le32 design_cap_low;
- + __le32 cycle_count;
- + __le32 measurement_accuracy;
- + __le32 max_sampling_time;
- + __le32 min_sampling_time;
- + __le32 max_avg_interval;
- + __le32 min_avg_interval;
- + __le32 bat_cap_granularity_1;
- + __le32 bat_cap_granularity_2;
- + __u8 model[21];
- + __u8 serial[11];
- + __u8 type[5];
- + __u8 oem_info[21];
- +} __packed;
- +
- +static_assert(sizeof(struct spwr_bix) == 119);
- +
- +/* Equivalent to data returned in ACPI _BST method. */
- +struct spwr_bst {
- + __le32 state;
- + __le32 present_rate;
- + __le32 remaining_cap;
- + __le32 present_voltage;
- +} __packed;
- +
- +static_assert(sizeof(struct spwr_bst) == 16);
- +
- +#define SPWR_BIX_REVISION 0
- +#define SPWR_BATTERY_VALUE_UNKNOWN 0xffffffff
- +
- +/* Get battery status (_STA) */
- +static SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_sta, __le32, {
- + .target_category = SSAM_SSH_TC_BAT,
- + .command_id = 0x01,
- +});
- +
- +/* Get battery static information (_BIX). */
- +static SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_bix, struct spwr_bix, {
- + .target_category = SSAM_SSH_TC_BAT,
- + .command_id = 0x02,
- +});
- +
- +/* Get battery dynamic information (_BST). */
- +static SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_bst, struct spwr_bst, {
- + .target_category = SSAM_SSH_TC_BAT,
- + .command_id = 0x03,
- +});
- +
- +/* Set battery trip point (_BTP). */
- +static SSAM_DEFINE_SYNC_REQUEST_CL_W(ssam_bat_set_btp, __le32, {
- + .target_category = SSAM_SSH_TC_BAT,
- + .command_id = 0x04,
- +});
- +
- +
- +/* -- Device structures. ---------------------------------------------------- */
- +
- +struct spwr_psy_properties {
- + const char *name;
- + struct ssam_event_registry registry;
- +};
- +
- +struct spwr_battery_device {
- + struct ssam_device *sdev;
- +
- + char name[32];
- + struct power_supply *psy;
- + struct power_supply_desc psy_desc;
- +
- + struct delayed_work update_work;
- +
- + struct ssam_event_notifier notif;
- +
- + struct mutex lock; /* Guards access to state data below. */
- + unsigned long timestamp;
- +
- + __le32 sta;
- + struct spwr_bix bix;
- + struct spwr_bst bst;
- + u32 alarm;
- +};
- +
- +
- +/* -- Module parameters. ---------------------------------------------------- */
- +
- +static unsigned int cache_time = 1000;
- +module_param(cache_time, uint, 0644);
- +MODULE_PARM_DESC(cache_time, "battery state caching time in milliseconds [default: 1000]");
- +
- +
- +/* -- State management. ----------------------------------------------------- */
- +
- +/*
- + * Delay for battery update quirk. See spwr_battery_recheck_adapter() below
- + * for more details.
- + */
- +#define SPWR_AC_BAT_UPDATE_DELAY msecs_to_jiffies(5000)
- +
- +static bool spwr_battery_present(struct spwr_battery_device *bat)
- +{
- + lockdep_assert_held(&bat->lock);
- +
- + return le32_to_cpu(bat->sta) & SAM_BATTERY_STA_PRESENT;
- +}
- +
- +static int spwr_battery_load_sta(struct spwr_battery_device *bat)
- +{
- + lockdep_assert_held(&bat->lock);
- +
- + return ssam_retry(ssam_bat_get_sta, bat->sdev, &bat->sta);
- +}
- +
- +static int spwr_battery_load_bix(struct spwr_battery_device *bat)
- +{
- + int status;
- +
- + lockdep_assert_held(&bat->lock);
- +
- + if (!spwr_battery_present(bat))
- + return 0;
- +
- + status = ssam_retry(ssam_bat_get_bix, bat->sdev, &bat->bix);
- +
- + /* Enforce NULL terminated strings in case anything goes wrong... */
- + bat->bix.model[ARRAY_SIZE(bat->bix.model) - 1] = 0;
- + bat->bix.serial[ARRAY_SIZE(bat->bix.serial) - 1] = 0;
- + bat->bix.type[ARRAY_SIZE(bat->bix.type) - 1] = 0;
- + bat->bix.oem_info[ARRAY_SIZE(bat->bix.oem_info) - 1] = 0;
- +
- + return status;
- +}
- +
- +static int spwr_battery_load_bst(struct spwr_battery_device *bat)
- +{
- + lockdep_assert_held(&bat->lock);
- +
- + if (!spwr_battery_present(bat))
- + return 0;
- +
- + return ssam_retry(ssam_bat_get_bst, bat->sdev, &bat->bst);
- +}
- +
- +static int spwr_battery_set_alarm_unlocked(struct spwr_battery_device *bat, u32 value)
- +{
- + __le32 value_le = cpu_to_le32(value);
- +
- + lockdep_assert_held(&bat->lock);
- +
- + bat->alarm = value;
- + return ssam_retry(ssam_bat_set_btp, bat->sdev, &value_le);
- +}
- +
- +static int spwr_battery_update_bst_unlocked(struct spwr_battery_device *bat, bool cached)
- +{
- + unsigned long cache_deadline = bat->timestamp + msecs_to_jiffies(cache_time);
- + int status;
- +
- + lockdep_assert_held(&bat->lock);
- +
- + if (cached && bat->timestamp && time_is_after_jiffies(cache_deadline))
- + return 0;
- +
- + status = spwr_battery_load_sta(bat);
- + if (status)
- + return status;
- +
- + status = spwr_battery_load_bst(bat);
- + if (status)
- + return status;
- +
- + bat->timestamp = jiffies;
- + return 0;
- +}
- +
- +static int spwr_battery_update_bst(struct spwr_battery_device *bat, bool cached)
- +{
- + int status;
- +
- + mutex_lock(&bat->lock);
- + status = spwr_battery_update_bst_unlocked(bat, cached);
- + mutex_unlock(&bat->lock);
- +
- + return status;
- +}
- +
- +static int spwr_battery_update_bix_unlocked(struct spwr_battery_device *bat)
- +{
- + int status;
- +
- + lockdep_assert_held(&bat->lock);
- +
- + status = spwr_battery_load_sta(bat);
- + if (status)
- + return status;
- +
- + status = spwr_battery_load_bix(bat);
- + if (status)
- + return status;
- +
- + status = spwr_battery_load_bst(bat);
- + if (status)
- + return status;
- +
- + if (bat->bix.revision != SPWR_BIX_REVISION)
- + dev_warn(&bat->sdev->dev, "unsupported battery revision: %u\n", bat->bix.revision);
- +
- + bat->timestamp = jiffies;
- + return 0;
- +}
- +
- +static u32 sprw_battery_get_full_cap_safe(struct spwr_battery_device *bat)
- +{
- + u32 full_cap = get_unaligned_le32(&bat->bix.last_full_charge_cap);
- +
- + lockdep_assert_held(&bat->lock);
- +
- + if (full_cap == 0 || full_cap == SPWR_BATTERY_VALUE_UNKNOWN)
- + full_cap = get_unaligned_le32(&bat->bix.design_cap);
- +
- + return full_cap;
- +}
- +
- +static bool spwr_battery_is_full(struct spwr_battery_device *bat)
- +{
- + u32 state = get_unaligned_le32(&bat->bst.state);
- + u32 full_cap = sprw_battery_get_full_cap_safe(bat);
- + u32 remaining_cap = get_unaligned_le32(&bat->bst.remaining_cap);
- +
- + lockdep_assert_held(&bat->lock);
- +
- + return full_cap != SPWR_BATTERY_VALUE_UNKNOWN && full_cap != 0 &&
- + remaining_cap != SPWR_BATTERY_VALUE_UNKNOWN &&
- + remaining_cap >= full_cap &&
- + state == 0;
- +}
- +
- +static int spwr_battery_recheck_full(struct spwr_battery_device *bat)
- +{
- + bool present;
- + u32 unit;
- + int status;
- +
- + mutex_lock(&bat->lock);
- + unit = get_unaligned_le32(&bat->bix.power_unit);
- + present = spwr_battery_present(bat);
- +
- + status = spwr_battery_update_bix_unlocked(bat);
- + if (status)
- + goto out;
- +
- + /* If battery has been attached, (re-)initialize alarm. */
- + if (!present && spwr_battery_present(bat)) {
- + u32 cap_warn = get_unaligned_le32(&bat->bix.design_cap_warn);
- +
- + status = spwr_battery_set_alarm_unlocked(bat, cap_warn);
- + if (status)
- + goto out;
- + }
- +
- + /*
- + * Warn if the unit has changed. This is something we genuinely don't
- + * expect to happen, so make this a big warning. If it does, we'll
- + * need to add support for it.
- + */
- + WARN_ON(unit != get_unaligned_le32(&bat->bix.power_unit));
- +
- +out:
- + mutex_unlock(&bat->lock);
- +
- + if (!status)
- + power_supply_changed(bat->psy);
- +
- + return status;
- +}
- +
- +static int spwr_battery_recheck_status(struct spwr_battery_device *bat)
- +{
- + int status;
- +
- + status = spwr_battery_update_bst(bat, false);
- + if (!status)
- + power_supply_changed(bat->psy);
- +
- + return status;
- +}
- +
- +static int spwr_battery_recheck_adapter(struct spwr_battery_device *bat)
- +{
- + /*
- + * Handle battery update quirk: When the battery is fully charged (or
- + * charged up to the limit imposed by the UEFI battery limit) and the
- + * adapter is plugged in or removed, the EC does not send a separate
- + * event for the state (charging/discharging) change. Furthermore it
- + * may take some time until the state is updated on the battery.
- + * Schedule an update to solve this.
- + */
- +
- + schedule_delayed_work(&bat->update_work, SPWR_AC_BAT_UPDATE_DELAY);
- + return 0;
- +}
- +
- +static u32 spwr_notify_bat(struct ssam_event_notifier *nf, const struct ssam_event *event)
- +{
- + struct spwr_battery_device *bat = container_of(nf, struct spwr_battery_device, notif);
- + int status;
- +
- + dev_dbg(&bat->sdev->dev, "power event (cid = %#04x, iid = %#04x, tid = %#04x)\n",
- + event->command_id, event->instance_id, event->target_id);
- +
- + /* Handled here, needs to be handled for all targets/instances. */
- + if (event->command_id == SAM_EVENT_CID_BAT_ADP) {
- + status = spwr_battery_recheck_adapter(bat);
- + return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
- + }
- +
- + if (bat->sdev->uid.target != event->target_id)
- + return 0;
- +
- + if (bat->sdev->uid.instance != event->instance_id)
- + return 0;
- +
- + switch (event->command_id) {
- + case SAM_EVENT_CID_BAT_BIX:
- + status = spwr_battery_recheck_full(bat);
- + break;
- +
- + case SAM_EVENT_CID_BAT_BST:
- + status = spwr_battery_recheck_status(bat);
- + break;
- +
- + case SAM_EVENT_CID_BAT_PROT:
- + /*
- + * TODO: Implement support for battery protection status change
- + * event.
- + */
- + status = 0;
- + break;
- +
- + case SAM_EVENT_CID_BAT_DPTF:
- + /*
- + * TODO: Implement support for DPTF event.
- + */
- + status = 0;
- + break;
- +
- + default:
- + return 0;
- + }
- +
- + return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
- +}
- +
- +static void spwr_battery_update_bst_workfn(struct work_struct *work)
- +{
- + struct delayed_work *dwork = to_delayed_work(work);
- + struct spwr_battery_device *bat;
- + int status;
- +
- + bat = container_of(dwork, struct spwr_battery_device, update_work);
- +
- + status = spwr_battery_update_bst(bat, false);
- + if (!status)
- + power_supply_changed(bat->psy);
- +
- + if (status)
- + dev_err(&bat->sdev->dev, "failed to update battery state: %d\n", status);
- +}
- +
- +
- +/* -- Properties. ----------------------------------------------------------- */
- +
- +static enum power_supply_property spwr_battery_props_chg[] = {
- + POWER_SUPPLY_PROP_STATUS,
- + POWER_SUPPLY_PROP_PRESENT,
- + POWER_SUPPLY_PROP_TECHNOLOGY,
- + POWER_SUPPLY_PROP_CYCLE_COUNT,
- + POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
- + POWER_SUPPLY_PROP_VOLTAGE_NOW,
- + POWER_SUPPLY_PROP_CURRENT_NOW,
- + POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
- + POWER_SUPPLY_PROP_CHARGE_FULL,
- + POWER_SUPPLY_PROP_CHARGE_NOW,
- + POWER_SUPPLY_PROP_CAPACITY,
- + POWER_SUPPLY_PROP_CAPACITY_LEVEL,
- + POWER_SUPPLY_PROP_MODEL_NAME,
- + POWER_SUPPLY_PROP_MANUFACTURER,
- + POWER_SUPPLY_PROP_SERIAL_NUMBER,
- +};
- +
- +static enum power_supply_property spwr_battery_props_eng[] = {
- + POWER_SUPPLY_PROP_STATUS,
- + POWER_SUPPLY_PROP_PRESENT,
- + POWER_SUPPLY_PROP_TECHNOLOGY,
- + POWER_SUPPLY_PROP_CYCLE_COUNT,
- + POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
- + POWER_SUPPLY_PROP_VOLTAGE_NOW,
- + POWER_SUPPLY_PROP_POWER_NOW,
- + POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
- + POWER_SUPPLY_PROP_ENERGY_FULL,
- + POWER_SUPPLY_PROP_ENERGY_NOW,
- + POWER_SUPPLY_PROP_CAPACITY,
- + POWER_SUPPLY_PROP_CAPACITY_LEVEL,
- + POWER_SUPPLY_PROP_MODEL_NAME,
- + POWER_SUPPLY_PROP_MANUFACTURER,
- + POWER_SUPPLY_PROP_SERIAL_NUMBER,
- +};
- +
- +static int spwr_battery_prop_status(struct spwr_battery_device *bat)
- +{
- + u32 state = get_unaligned_le32(&bat->bst.state);
- + u32 present_rate = get_unaligned_le32(&bat->bst.present_rate);
- +
- + lockdep_assert_held(&bat->lock);
- +
- + if (state & SAM_BATTERY_STATE_DISCHARGING)
- + return POWER_SUPPLY_STATUS_DISCHARGING;
- +
- + if (state & SAM_BATTERY_STATE_CHARGING)
- + return POWER_SUPPLY_STATUS_CHARGING;
- +
- + if (spwr_battery_is_full(bat))
- + return POWER_SUPPLY_STATUS_FULL;
- +
- + if (present_rate == 0)
- + return POWER_SUPPLY_STATUS_NOT_CHARGING;
- +
- + return POWER_SUPPLY_STATUS_UNKNOWN;
- +}
- +
- +static int spwr_battery_prop_technology(struct spwr_battery_device *bat)
- +{
- + lockdep_assert_held(&bat->lock);
- +
- + if (!strcasecmp("NiCd", bat->bix.type))
- + return POWER_SUPPLY_TECHNOLOGY_NiCd;
- +
- + if (!strcasecmp("NiMH", bat->bix.type))
- + return POWER_SUPPLY_TECHNOLOGY_NiMH;
- +
- + if (!strcasecmp("LION", bat->bix.type))
- + return POWER_SUPPLY_TECHNOLOGY_LION;
- +
- + if (!strncasecmp("LI-ION", bat->bix.type, 6))
- + return POWER_SUPPLY_TECHNOLOGY_LION;
- +
- + if (!strcasecmp("LiP", bat->bix.type))
- + return POWER_SUPPLY_TECHNOLOGY_LIPO;
- +
- + return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
- +}
- +
- +static int spwr_battery_prop_capacity(struct spwr_battery_device *bat)
- +{
- + u32 full_cap = sprw_battery_get_full_cap_safe(bat);
- + u32 remaining_cap = get_unaligned_le32(&bat->bst.remaining_cap);
- +
- + lockdep_assert_held(&bat->lock);
- +
- + if (full_cap == 0 || full_cap == SPWR_BATTERY_VALUE_UNKNOWN)
- + return -ENODEV;
- +
- + if (remaining_cap == SPWR_BATTERY_VALUE_UNKNOWN)
- + return -ENODEV;
- +
- + return remaining_cap * 100 / full_cap;
- +}
- +
- +static int spwr_battery_prop_capacity_level(struct spwr_battery_device *bat)
- +{
- + u32 state = get_unaligned_le32(&bat->bst.state);
- + u32 remaining_cap = get_unaligned_le32(&bat->bst.remaining_cap);
- +
- + lockdep_assert_held(&bat->lock);
- +
- + if (state & SAM_BATTERY_STATE_CRITICAL)
- + return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
- +
- + if (spwr_battery_is_full(bat))
- + return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
- +
- + if (remaining_cap <= bat->alarm)
- + return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
- +
- + return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
- +}
- +
- +static int spwr_battery_get_property(struct power_supply *psy, enum power_supply_property psp,
- + union power_supply_propval *val)
- +{
- + struct spwr_battery_device *bat = power_supply_get_drvdata(psy);
- + u32 value;
- + int status;
- +
- + mutex_lock(&bat->lock);
- +
- + status = spwr_battery_update_bst_unlocked(bat, true);
- + if (status)
- + goto out;
- +
- + /* Abort if battery is not present. */
- + if (!spwr_battery_present(bat) && psp != POWER_SUPPLY_PROP_PRESENT) {
- + status = -ENODEV;
- + goto out;
- + }
- +
- + switch (psp) {
- + case POWER_SUPPLY_PROP_STATUS:
- + val->intval = spwr_battery_prop_status(bat);
- + break;
- +
- + case POWER_SUPPLY_PROP_PRESENT:
- + val->intval = spwr_battery_present(bat);
- + break;
- +
- + case POWER_SUPPLY_PROP_TECHNOLOGY:
- + val->intval = spwr_battery_prop_technology(bat);
- + break;
- +
- + case POWER_SUPPLY_PROP_CYCLE_COUNT:
- + value = get_unaligned_le32(&bat->bix.cycle_count);
- + if (value != SPWR_BATTERY_VALUE_UNKNOWN)
- + val->intval = value;
- + else
- + status = -ENODEV;
- + break;
- +
- + case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
- + value = get_unaligned_le32(&bat->bix.design_voltage);
- + if (value != SPWR_BATTERY_VALUE_UNKNOWN)
- + val->intval = value * 1000;
- + else
- + status = -ENODEV;
- + break;
- +
- + case POWER_SUPPLY_PROP_VOLTAGE_NOW:
- + value = get_unaligned_le32(&bat->bst.present_voltage);
- + if (value != SPWR_BATTERY_VALUE_UNKNOWN)
- + val->intval = value * 1000;
- + else
- + status = -ENODEV;
- + break;
- +
- + case POWER_SUPPLY_PROP_CURRENT_NOW:
- + case POWER_SUPPLY_PROP_POWER_NOW:
- + value = get_unaligned_le32(&bat->bst.present_rate);
- + if (value != SPWR_BATTERY_VALUE_UNKNOWN)
- + val->intval = value * 1000;
- + else
- + status = -ENODEV;
- + break;
- +
- + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
- + case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
- + value = get_unaligned_le32(&bat->bix.design_cap);
- + if (value != SPWR_BATTERY_VALUE_UNKNOWN)
- + val->intval = value * 1000;
- + else
- + status = -ENODEV;
- + break;
- +
- + case POWER_SUPPLY_PROP_CHARGE_FULL:
- + case POWER_SUPPLY_PROP_ENERGY_FULL:
- + value = get_unaligned_le32(&bat->bix.last_full_charge_cap);
- + if (value != SPWR_BATTERY_VALUE_UNKNOWN)
- + val->intval = value * 1000;
- + else
- + status = -ENODEV;
- + break;
- +
- + case POWER_SUPPLY_PROP_CHARGE_NOW:
- + case POWER_SUPPLY_PROP_ENERGY_NOW:
- + value = get_unaligned_le32(&bat->bst.remaining_cap);
- + if (value != SPWR_BATTERY_VALUE_UNKNOWN)
- + val->intval = value * 1000;
- + else
- + status = -ENODEV;
- + break;
- +
- + case POWER_SUPPLY_PROP_CAPACITY:
- + val->intval = spwr_battery_prop_capacity(bat);
- + break;
- +
- + case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
- + val->intval = spwr_battery_prop_capacity_level(bat);
- + break;
- +
- + case POWER_SUPPLY_PROP_MODEL_NAME:
- + val->strval = bat->bix.model;
- + break;
- +
- + case POWER_SUPPLY_PROP_MANUFACTURER:
- + val->strval = bat->bix.oem_info;
- + break;
- +
- + case POWER_SUPPLY_PROP_SERIAL_NUMBER:
- + val->strval = bat->bix.serial;
- + break;
- +
- + default:
- + status = -EINVAL;
- + break;
- + }
- +
- +out:
- + mutex_unlock(&bat->lock);
- + return status;
- +}
- +
- +
- +/* -- Alarm attribute. ------------------------------------------------------ */
- +
- +static ssize_t spwr_battery_alarm_show(struct device *dev, struct device_attribute *attr, char *buf)
- +{
- + struct power_supply *psy = dev_get_drvdata(dev);
- + struct spwr_battery_device *bat = power_supply_get_drvdata(psy);
- + int status;
- +
- + mutex_lock(&bat->lock);
- + status = sysfs_emit(buf, "%d\n", bat->alarm * 1000);
- + mutex_unlock(&bat->lock);
- +
- + return status;
- +}
- +
- +static ssize_t spwr_battery_alarm_store(struct device *dev, struct device_attribute *attr,
- + const char *buf, size_t count)
- +{
- + struct power_supply *psy = dev_get_drvdata(dev);
- + struct spwr_battery_device *bat = power_supply_get_drvdata(psy);
- + unsigned long value;
- + int status;
- +
- + status = kstrtoul(buf, 0, &value);
- + if (status)
- + return status;
- +
- + mutex_lock(&bat->lock);
- +
- + if (!spwr_battery_present(bat)) {
- + mutex_unlock(&bat->lock);
- + return -ENODEV;
- + }
- +
- + status = spwr_battery_set_alarm_unlocked(bat, value / 1000);
- + if (status) {
- + mutex_unlock(&bat->lock);
- + return status;
- + }
- +
- + mutex_unlock(&bat->lock);
- + return count;
- +}
- +
- +static const struct device_attribute alarm_attr = {
- + .attr = {.name = "alarm", .mode = 0644},
- + .show = spwr_battery_alarm_show,
- + .store = spwr_battery_alarm_store,
- +};
- +
- +
- +/* -- Device setup. --------------------------------------------------------- */
- +
- +static void spwr_battery_init(struct spwr_battery_device *bat, struct ssam_device *sdev,
- + struct ssam_event_registry registry, const char *name)
- +{
- + mutex_init(&bat->lock);
- + strncpy(bat->name, name, ARRAY_SIZE(bat->name) - 1);
- +
- + bat->sdev = sdev;
- +
- + bat->notif.base.priority = 1;
- + bat->notif.base.fn = spwr_notify_bat;
- + bat->notif.event.reg = registry;
- + bat->notif.event.id.target_category = sdev->uid.category;
- + bat->notif.event.id.instance = 0;
- + bat->notif.event.mask = SSAM_EVENT_MASK_NONE;
- + bat->notif.event.flags = SSAM_EVENT_SEQUENCED;
- +
- + bat->psy_desc.name = bat->name;
- + bat->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY;
- + bat->psy_desc.get_property = spwr_battery_get_property;
- +
- + INIT_DELAYED_WORK(&bat->update_work, spwr_battery_update_bst_workfn);
- +}
- +
- +static void spwr_battery_destroy(struct spwr_battery_device *bat)
- +{
- + mutex_destroy(&bat->lock);
- +}
- +
- +static int spwr_battery_register(struct spwr_battery_device *bat)
- +{
- + struct power_supply_config psy_cfg = {};
- + __le32 sta;
- + int status;
- +
- + /* Make sure the device is there and functioning properly. */
- + status = ssam_retry(ssam_bat_get_sta, bat->sdev, &sta);
- + if (status)
- + return status;
- +
- + if ((le32_to_cpu(sta) & SAM_BATTERY_STA_OK) != SAM_BATTERY_STA_OK)
- + return -ENODEV;
- +
- + /* Satisfy lockdep although we are in an exclusive context here. */
- + mutex_lock(&bat->lock);
- +
- + status = spwr_battery_update_bix_unlocked(bat);
- + if (status) {
- + mutex_unlock(&bat->lock);
- + return status;
- + }
- +
- + if (spwr_battery_present(bat)) {
- + u32 cap_warn = get_unaligned_le32(&bat->bix.design_cap_warn);
- +
- + status = spwr_battery_set_alarm_unlocked(bat, cap_warn);
- + if (status) {
- + mutex_unlock(&bat->lock);
- + return status;
- + }
- + }
- +
- + mutex_unlock(&bat->lock);
- +
- + switch (get_unaligned_le32(&bat->bix.power_unit)) {
- + case SAM_BATTERY_POWER_UNIT_mW:
- + bat->psy_desc.properties = spwr_battery_props_eng;
- + bat->psy_desc.num_properties = ARRAY_SIZE(spwr_battery_props_eng);
- + break;
- +
- + case SAM_BATTERY_POWER_UNIT_mA:
- + bat->psy_desc.properties = spwr_battery_props_chg;
- + bat->psy_desc.num_properties = ARRAY_SIZE(spwr_battery_props_chg);
- + break;
- +
- + default:
- + dev_err(&bat->sdev->dev, "unsupported battery power unit: %u\n",
- + get_unaligned_le32(&bat->bix.power_unit));
- + return -EINVAL;
- + }
- +
- + psy_cfg.drv_data = bat;
- + bat->psy = power_supply_register(&bat->sdev->dev, &bat->psy_desc, &psy_cfg);
- + if (IS_ERR(bat->psy))
- + return PTR_ERR(bat->psy);
- +
- + status = ssam_notifier_register(bat->sdev->ctrl, &bat->notif);
- + if (status)
- + goto err_notif;
- +
- + status = device_create_file(&bat->psy->dev, &alarm_attr);
- + if (status)
- + goto err_file;
- +
- + return 0;
- +
- +err_file:
- + ssam_notifier_unregister(bat->sdev->ctrl, &bat->notif);
- +err_notif:
- + power_supply_unregister(bat->psy);
- + return status;
- +}
- +
- +static void spwr_battery_unregister(struct spwr_battery_device *bat)
- +{
- + ssam_notifier_unregister(bat->sdev->ctrl, &bat->notif);
- + cancel_delayed_work_sync(&bat->update_work);
- + device_remove_file(&bat->psy->dev, &alarm_attr);
- + power_supply_unregister(bat->psy);
- +}
- +
- +
- +/* -- Driver setup. --------------------------------------------------------- */
- +
- +static int __maybe_unused surface_battery_resume(struct device *dev)
- +{
- + return spwr_battery_recheck_full(dev_get_drvdata(dev));
- +}
- +SIMPLE_DEV_PM_OPS(surface_battery_pm_ops, NULL, surface_battery_resume);
- +
- +static int surface_battery_probe(struct ssam_device *sdev)
- +{
- + const struct spwr_psy_properties *p;
- + struct spwr_battery_device *bat;
- + int status;
- +
- + p = ssam_device_get_match_data(sdev);
- + if (!p)
- + return -ENODEV;
- +
- + bat = devm_kzalloc(&sdev->dev, sizeof(*bat), GFP_KERNEL);
- + if (!bat)
- + return -ENOMEM;
- +
- + spwr_battery_init(bat, sdev, p->registry, p->name);
- + ssam_device_set_drvdata(sdev, bat);
- +
- + status = spwr_battery_register(bat);
- + if (status)
- + spwr_battery_destroy(bat);
- +
- + return status;
- +}
- +
- +static void surface_battery_remove(struct ssam_device *sdev)
- +{
- + struct spwr_battery_device *bat = ssam_device_get_drvdata(sdev);
- +
- + spwr_battery_unregister(bat);
- + spwr_battery_destroy(bat);
- +}
- +
- +static const struct spwr_psy_properties spwr_psy_props_bat1 = {
- + .name = "BAT1",
- + .registry = SSAM_EVENT_REGISTRY_SAM,
- +};
- +
- +static const struct spwr_psy_properties spwr_psy_props_bat2_sb3 = {
- + .name = "BAT2",
- + .registry = SSAM_EVENT_REGISTRY_KIP,
- +};
- +
- +static const struct ssam_device_id surface_battery_match[] = {
- + { SSAM_SDEV(BAT, 0x01, 0x01, 0x00), (unsigned long)&spwr_psy_props_bat1 },
- + { SSAM_SDEV(BAT, 0x02, 0x01, 0x00), (unsigned long)&spwr_psy_props_bat2_sb3 },
- + { },
- +};
- +MODULE_DEVICE_TABLE(ssam, surface_battery_match);
- +
- +static struct ssam_device_driver surface_battery_driver = {
- + .probe = surface_battery_probe,
- + .remove = surface_battery_remove,
- + .match_table = surface_battery_match,
- + .driver = {
- + .name = "surface_battery",
- + .pm = &surface_battery_pm_ops,
- + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- + },
- +};
- +module_ssam_device_driver(surface_battery_driver);
- +
- +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- +MODULE_DESCRIPTION("Battery driver for Surface System Aggregator Module");
- +MODULE_LICENSE("GPL");
- --
- 2.31.0
- From ac720c108673ef509b68ff0c1bb47a818cf98ed2 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Fri, 12 Feb 2021 21:07:17 +0100
- Subject: [PATCH] power: supply: Add AC driver for Surface Aggregator Module
- On newer Microsoft Surface models (specifically 7th-generation, i.e.
- Surface Pro 7, Surface Book 3, Surface Laptop 3, and Surface Laptop Go),
- battery and AC status/information is no longer handled via standard ACPI
- devices, but instead directly via the Surface System Aggregator Module
- (SSAM), i.e. the embedded controller on those devices.
- While on previous generation models, AC status is also handled via SSAM,
- an ACPI shim was present to translate the standard ACPI AC interface to
- SSAM requests. The SSAM interface itself, which is modeled closely after
- the ACPI interface, has not changed.
- This commit introduces a new SSAM client device driver to support AC
- status/information via the aforementioned interface on said Surface
- models.
- Patchset: surface-sam
- ---
- MAINTAINERS | 1 +
- drivers/power/supply/Kconfig | 16 ++
- drivers/power/supply/Makefile | 1 +
- drivers/power/supply/surface_charger.c | 296 +++++++++++++++++++++++++
- 4 files changed, 314 insertions(+)
- create mode 100644 drivers/power/supply/surface_charger.c
- diff --git a/MAINTAINERS b/MAINTAINERS
- index 54a4769114e9..3d57740df499 100644
- --- a/MAINTAINERS
- +++ b/MAINTAINERS
- @@ -11791,6 +11791,7 @@ L: linux-pm@vger.kernel.org
- L: platform-driver-x86@vger.kernel.org
- S: Maintained
- F: drivers/power/supply/surface_battery.c
- +F: drivers/power/supply/surface_charger.c
-
- MICROSOFT SURFACE DTX DRIVER
- M: Maximilian Luz <luzmaximilian@gmail.com>
- diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
- index b635331daba4..d4d756d95778 100644
- --- a/drivers/power/supply/Kconfig
- +++ b/drivers/power/supply/Kconfig
- @@ -791,4 +791,20 @@ config BATTERY_SURFACE
- Microsoft Surface devices, i.e. Surface Pro 7, Surface Laptop 3,
- Surface Book 3, and Surface Laptop Go.
-
- +config CHARGER_SURFACE
- + tristate "AC driver for 7th-generation Microsoft Surface devices"
- + depends on SURFACE_AGGREGATOR_REGISTRY
- + help
- + Driver for AC devices connected via/managed by the Surface System
- + Aggregator Module (SSAM).
- +
- + This driver provides AC-information and -status support for Surface
- + devices where said data is not exposed via the standard ACPI devices.
- + On those models (7th-generation), AC-information is instead handled
- + directly via a SSAM client device and this driver.
- +
- + Say M or Y here to include AC status support for 7th-generation
- + Microsoft Surface devices, i.e. Surface Pro 7, Surface Laptop 3,
- + Surface Book 3, and Surface Laptop Go.
- +
- endif # POWER_SUPPLY
- diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
- index cddc18994119..9fdd34956153 100644
- --- a/drivers/power/supply/Makefile
- +++ b/drivers/power/supply/Makefile
- @@ -99,3 +99,4 @@ obj-$(CONFIG_CHARGER_BD99954) += bd99954-charger.o
- obj-$(CONFIG_CHARGER_WILCO) += wilco-charger.o
- obj-$(CONFIG_RN5T618_POWER) += rn5t618_power.o
- obj-$(CONFIG_BATTERY_SURFACE) += surface_battery.o
- +obj-$(CONFIG_CHARGER_SURFACE) += surface_charger.o
- diff --git a/drivers/power/supply/surface_charger.c b/drivers/power/supply/surface_charger.c
- new file mode 100644
- index 000000000000..982f9b9ef6f5
- --- /dev/null
- +++ b/drivers/power/supply/surface_charger.c
- @@ -0,0 +1,296 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * AC driver for 7th-generation Microsoft Surface devices via Surface System
- + * Aggregator Module (SSAM).
- + *
- + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <asm/unaligned.h>
- +#include <linux/kernel.h>
- +#include <linux/module.h>
- +#include <linux/mutex.h>
- +#include <linux/power_supply.h>
- +#include <linux/types.h>
- +
- +#include <linux/surface_aggregator/device.h>
- +
- +
- +/* -- SAM interface. -------------------------------------------------------- */
- +
- +enum sam_event_cid_bat {
- + SAM_EVENT_CID_BAT_ADP = 0x17,
- +};
- +
- +enum sam_battery_sta {
- + SAM_BATTERY_STA_OK = 0x0f,
- + SAM_BATTERY_STA_PRESENT = 0x10,
- +};
- +
- +/* Get battery status (_STA). */
- +static SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_sta, __le32, {
- + .target_category = SSAM_SSH_TC_BAT,
- + .command_id = 0x01,
- +});
- +
- +/* Get platform power source for battery (_PSR / DPTF PSRC). */
- +static SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_psrc, __le32, {
- + .target_category = SSAM_SSH_TC_BAT,
- + .command_id = 0x0d,
- +});
- +
- +
- +/* -- Device structures. ---------------------------------------------------- */
- +
- +struct spwr_psy_properties {
- + const char *name;
- + struct ssam_event_registry registry;
- +};
- +
- +struct spwr_ac_device {
- + struct ssam_device *sdev;
- +
- + char name[32];
- + struct power_supply *psy;
- + struct power_supply_desc psy_desc;
- +
- + struct ssam_event_notifier notif;
- +
- + struct mutex lock; /* Guards access to state below. */
- +
- + __le32 state;
- +};
- +
- +
- +/* -- State management. ----------------------------------------------------- */
- +
- +static int spwr_ac_update_unlocked(struct spwr_ac_device *ac)
- +{
- + u32 old = ac->state;
- + int status;
- +
- + lockdep_assert_held(&ac->lock);
- +
- + status = ssam_retry(ssam_bat_get_psrc, ac->sdev, &ac->state);
- + if (status < 0)
- + return status;
- +
- + return old != ac->state;
- +}
- +
- +static int spwr_ac_update(struct spwr_ac_device *ac)
- +{
- + int status;
- +
- + mutex_lock(&ac->lock);
- + status = spwr_ac_update_unlocked(ac);
- + mutex_unlock(&ac->lock);
- +
- + return status;
- +}
- +
- +static int spwr_ac_recheck(struct spwr_ac_device *ac)
- +{
- + int status;
- +
- + status = spwr_ac_update(ac);
- + if (status > 0)
- + power_supply_changed(ac->psy);
- +
- + return status >= 0 ? 0 : status;
- +}
- +
- +static u32 spwr_notify_ac(struct ssam_event_notifier *nf, const struct ssam_event *event)
- +{
- + struct spwr_ac_device *ac;
- + int status;
- +
- + ac = container_of(nf, struct spwr_ac_device, notif);
- +
- + dev_dbg(&ac->sdev->dev, "power event (cid = %#04x, iid = %#04x, tid = %#04x)\n",
- + event->command_id, event->instance_id, event->target_id);
- +
- + /*
- + * Allow events of all targets/instances here. Global adapter status
- + * seems to be handled via target=1 and instance=1, but events are
- + * reported on all targets/instances in use.
- + *
- + * While it should be enough to just listen on 1/1, listen everywhere to
- + * make sure we don't miss anything.
- + */
- +
- + switch (event->command_id) {
- + case SAM_EVENT_CID_BAT_ADP:
- + status = spwr_ac_recheck(ac);
- + return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
- +
- + default:
- + return 0;
- + }
- +}
- +
- +
- +/* -- Properties. ----------------------------------------------------------- */
- +
- +static enum power_supply_property spwr_ac_props[] = {
- + POWER_SUPPLY_PROP_ONLINE,
- +};
- +
- +static int spwr_ac_get_property(struct power_supply *psy, enum power_supply_property psp,
- + union power_supply_propval *val)
- +{
- + struct spwr_ac_device *ac = power_supply_get_drvdata(psy);
- + int status;
- +
- + mutex_lock(&ac->lock);
- +
- + status = spwr_ac_update_unlocked(ac);
- + if (status)
- + goto out;
- +
- + switch (psp) {
- + case POWER_SUPPLY_PROP_ONLINE:
- + val->intval = !!le32_to_cpu(ac->state);
- + break;
- +
- + default:
- + status = -EINVAL;
- + goto out;
- + }
- +
- +out:
- + mutex_unlock(&ac->lock);
- + return status;
- +}
- +
- +
- +/* -- Device setup. --------------------------------------------------------- */
- +
- +static void spwr_ac_init(struct spwr_ac_device *ac, struct ssam_device *sdev,
- + struct ssam_event_registry registry, const char *name)
- +{
- + mutex_init(&ac->lock);
- + strncpy(ac->name, name, ARRAY_SIZE(ac->name) - 1);
- +
- + ac->sdev = sdev;
- +
- + ac->notif.base.priority = 1;
- + ac->notif.base.fn = spwr_notify_ac;
- + ac->notif.event.reg = registry;
- + ac->notif.event.id.target_category = sdev->uid.category;
- + ac->notif.event.id.instance = 0;
- + ac->notif.event.mask = SSAM_EVENT_MASK_NONE;
- + ac->notif.event.flags = SSAM_EVENT_SEQUENCED;
- +
- + ac->psy_desc.name = ac->name;
- + ac->psy_desc.type = POWER_SUPPLY_TYPE_MAINS;
- + ac->psy_desc.properties = spwr_ac_props;
- + ac->psy_desc.num_properties = ARRAY_SIZE(spwr_ac_props);
- + ac->psy_desc.get_property = spwr_ac_get_property;
- +}
- +
- +static void spwr_ac_destroy(struct spwr_ac_device *ac)
- +{
- + mutex_destroy(&ac->lock);
- +}
- +
- +static int spwr_ac_register(struct spwr_ac_device *ac)
- +{
- + struct power_supply_config psy_cfg = {};
- + __le32 sta;
- + int status;
- +
- + /* Make sure the device is there and functioning properly. */
- + status = ssam_retry(ssam_bat_get_sta, ac->sdev, &sta);
- + if (status)
- + return status;
- +
- + if ((le32_to_cpu(sta) & SAM_BATTERY_STA_OK) != SAM_BATTERY_STA_OK)
- + return -ENODEV;
- +
- + psy_cfg.drv_data = ac;
- + ac->psy = power_supply_register(&ac->sdev->dev, &ac->psy_desc, &psy_cfg);
- + if (IS_ERR(ac->psy))
- + return PTR_ERR(ac->psy);
- +
- + status = ssam_notifier_register(ac->sdev->ctrl, &ac->notif);
- + if (status)
- + power_supply_unregister(ac->psy);
- +
- + return status;
- +}
- +
- +static int spwr_ac_unregister(struct spwr_ac_device *ac)
- +{
- + ssam_notifier_unregister(ac->sdev->ctrl, &ac->notif);
- + power_supply_unregister(ac->psy);
- + return 0;
- +}
- +
- +
- +/* -- Driver setup. --------------------------------------------------------- */
- +
- +static int __maybe_unused surface_ac_resume(struct device *dev)
- +{
- + return spwr_ac_recheck(dev_get_drvdata(dev));
- +}
- +SIMPLE_DEV_PM_OPS(surface_ac_pm_ops, NULL, surface_ac_resume);
- +
- +static int surface_ac_probe(struct ssam_device *sdev)
- +{
- + const struct spwr_psy_properties *p;
- + struct spwr_ac_device *ac;
- + int status;
- +
- + p = ssam_device_get_match_data(sdev);
- + if (!p)
- + return -ENODEV;
- +
- + ac = devm_kzalloc(&sdev->dev, sizeof(*ac), GFP_KERNEL);
- + if (!ac)
- + return -ENOMEM;
- +
- + spwr_ac_init(ac, sdev, p->registry, p->name);
- + ssam_device_set_drvdata(sdev, ac);
- +
- + status = spwr_ac_register(ac);
- + if (status)
- + spwr_ac_destroy(ac);
- +
- + return status;
- +}
- +
- +static void surface_ac_remove(struct ssam_device *sdev)
- +{
- + struct spwr_ac_device *ac = ssam_device_get_drvdata(sdev);
- +
- + spwr_ac_unregister(ac);
- + spwr_ac_destroy(ac);
- +}
- +
- +static const struct spwr_psy_properties spwr_psy_props_adp1 = {
- + .name = "ADP1",
- + .registry = SSAM_EVENT_REGISTRY_SAM,
- +};
- +
- +static const struct ssam_device_id surface_ac_match[] = {
- + { SSAM_SDEV(BAT, 0x01, 0x01, 0x01), (unsigned long)&spwr_psy_props_adp1 },
- + { },
- +};
- +MODULE_DEVICE_TABLE(ssam, surface_ac_match);
- +
- +static struct ssam_device_driver surface_ac_driver = {
- + .probe = surface_ac_probe,
- + .remove = surface_ac_remove,
- + .match_table = surface_ac_match,
- + .driver = {
- + .name = "surface_ac",
- + .pm = &surface_ac_pm_ops,
- + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- + },
- +};
- +module_ssam_device_driver(surface_ac_driver);
- +
- +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- +MODULE_DESCRIPTION("AC driver for Surface System Aggregator Module");
- +MODULE_LICENSE("GPL");
- --
- 2.31.0
- From 8fc7d4f6d7efcf2a5b53bbd759affba0fa8f2f47 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Sat, 13 Feb 2021 19:58:50 +0100
- Subject: [PATCH] platform/surface: Add performance mode driver
- Add driver to support performance mode on Surface devices with Surface
- Aggregator Module.
- Intended to be replaced by a platform profile driver in 5.12.
- Patchset: surface-sam
- ---
- drivers/platform/surface/Kconfig | 17 +++
- drivers/platform/surface/Makefile | 1 +
- drivers/platform/surface/surface_perfmode.c | 122 ++++++++++++++++++++
- 3 files changed, 140 insertions(+)
- create mode 100644 drivers/platform/surface/surface_perfmode.c
- diff --git a/drivers/platform/surface/Kconfig b/drivers/platform/surface/Kconfig
- index dea313989b4c..3ceeb316d56e 100644
- --- a/drivers/platform/surface/Kconfig
- +++ b/drivers/platform/surface/Kconfig
- @@ -140,6 +140,23 @@ config SURFACE_GPE
- accordingly. It is required on those devices to allow wake-ups from
- suspend by opening the lid.
-
- +config SURFACE_PERFMODE
- + tristate "Surface Performance-Mode Driver"
- + depends on SURFACE_AGGREGATOR_BUS
- + depends on SYSFS
- + help
- + Driver for the performance-/cooling-mode interface of Microsoft
- + Surface devices.
- +
- + Microsoft Surface devices using the Surface System Aggregator Module
- + (SSAM) can be switched between different performance modes. This,
- + depending on the device, can influence their cooling behavior and may
- + influence power limits, allowing users to choose between performance
- + and higher power-draw, or lower power-draw and more silent operation.
- +
- + This driver provides a user-space interface (via sysfs) for
- + controlling said mode via the corresponding client device.
- +
- config SURFACE_PRO3_BUTTON
- tristate "Power/home/volume buttons driver for Microsoft Surface Pro 3/4 tablet"
- depends on INPUT
- diff --git a/drivers/platform/surface/Makefile b/drivers/platform/surface/Makefile
- index 19b661e274c3..31098983decc 100644
- --- a/drivers/platform/surface/Makefile
- +++ b/drivers/platform/surface/Makefile
- @@ -14,4 +14,5 @@ obj-$(CONFIG_SURFACE_AGGREGATOR_REGISTRY) += surface_aggregator_registry.o
- obj-$(CONFIG_SURFACE_BOOK1_DGPU_SWITCH) += surfacebook1_dgpu_switch.o
- obj-$(CONFIG_SURFACE_DTX) += surface_dtx.o
- obj-$(CONFIG_SURFACE_GPE) += surface_gpe.o
- +obj-$(CONFIG_SURFACE_PERFMODE) += surface_perfmode.o
- obj-$(CONFIG_SURFACE_PRO3_BUTTON) += surfacepro3_button.o
- diff --git a/drivers/platform/surface/surface_perfmode.c b/drivers/platform/surface/surface_perfmode.c
- new file mode 100644
- index 000000000000..3b92a43f8606
- --- /dev/null
- +++ b/drivers/platform/surface/surface_perfmode.c
- @@ -0,0 +1,122 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * Surface performance-mode driver.
- + *
- + * Provides a user-space interface for the performance mode control provided
- + * by the Surface System Aggregator Module (SSAM), influencing cooling
- + * behavior of the device and potentially managing power limits.
- + *
- + * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <asm/unaligned.h>
- +#include <linux/kernel.h>
- +#include <linux/module.h>
- +#include <linux/sysfs.h>
- +#include <linux/types.h>
- +
- +#include <linux/surface_aggregator/device.h>
- +
- +enum sam_perf_mode {
- + SAM_PERF_MODE_NORMAL = 1,
- + SAM_PERF_MODE_BATTERY = 2,
- + SAM_PERF_MODE_PERF1 = 3,
- + SAM_PERF_MODE_PERF2 = 4,
- +
- + __SAM_PERF_MODE__MIN = 1,
- + __SAM_PERF_MODE__MAX = 4,
- +};
- +
- +struct ssam_perf_info {
- + __le32 mode;
- + __le16 unknown1;
- + __le16 unknown2;
- +} __packed;
- +
- +static SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_tmp_perf_mode_get, struct ssam_perf_info, {
- + .target_category = SSAM_SSH_TC_TMP,
- + .command_id = 0x02,
- +});
- +
- +static SSAM_DEFINE_SYNC_REQUEST_CL_W(__ssam_tmp_perf_mode_set, __le32, {
- + .target_category = SSAM_SSH_TC_TMP,
- + .command_id = 0x03,
- +});
- +
- +static int ssam_tmp_perf_mode_set(struct ssam_device *sdev, u32 mode)
- +{
- + __le32 mode_le = cpu_to_le32(mode);
- +
- + if (mode < __SAM_PERF_MODE__MIN || mode > __SAM_PERF_MODE__MAX)
- + return -EINVAL;
- +
- + return ssam_retry(__ssam_tmp_perf_mode_set, sdev, &mode_le);
- +}
- +
- +static ssize_t perf_mode_show(struct device *dev, struct device_attribute *attr,
- + char *data)
- +{
- + struct ssam_device *sdev = to_ssam_device(dev);
- + struct ssam_perf_info info;
- + int status;
- +
- + status = ssam_retry(ssam_tmp_perf_mode_get, sdev, &info);
- + if (status) {
- + dev_err(dev, "failed to get current performance mode: %d\n",
- + status);
- + return -EIO;
- + }
- +
- + return sprintf(data, "%d\n", le32_to_cpu(info.mode));
- +}
- +
- +static ssize_t perf_mode_store(struct device *dev, struct device_attribute *attr,
- + const char *data, size_t count)
- +{
- + struct ssam_device *sdev = to_ssam_device(dev);
- + int perf_mode;
- + int status;
- +
- + status = kstrtoint(data, 0, &perf_mode);
- + if (status < 0)
- + return status;
- +
- + status = ssam_tmp_perf_mode_set(sdev, perf_mode);
- + if (status < 0)
- + return status;
- +
- + return count;
- +}
- +
- +static const DEVICE_ATTR_RW(perf_mode);
- +
- +static int surface_sam_sid_perfmode_probe(struct ssam_device *sdev)
- +{
- + return sysfs_create_file(&sdev->dev.kobj, &dev_attr_perf_mode.attr);
- +}
- +
- +static void surface_sam_sid_perfmode_remove(struct ssam_device *sdev)
- +{
- + sysfs_remove_file(&sdev->dev.kobj, &dev_attr_perf_mode.attr);
- +}
- +
- +static const struct ssam_device_id ssam_perfmode_match[] = {
- + { SSAM_SDEV(TMP, 0x01, 0x00, 0x01) },
- + { },
- +};
- +MODULE_DEVICE_TABLE(ssam, ssam_perfmode_match);
- +
- +static struct ssam_device_driver surface_sam_sid_perfmode = {
- + .probe = surface_sam_sid_perfmode_probe,
- + .remove = surface_sam_sid_perfmode_remove,
- + .match_table = ssam_perfmode_match,
- + .driver = {
- + .name = "surface_performance_mode",
- + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- + },
- +};
- +module_ssam_device_driver(surface_sam_sid_perfmode);
- +
- +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- +MODULE_DESCRIPTION("Performance mode interface for Surface System Aggregator Module");
- +MODULE_LICENSE("GPL");
- --
- 2.31.0
- From 784252e94930063aec12fbdd0f4e60f899fbfdbc Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Thu, 4 Mar 2021 20:05:24 +0100
- Subject: [PATCH] platform/surface: aggregator: Make SSAM_DEFINE_SYNC_REQUEST_x
- define static functions
- The SSAM_DEFINE_SYNC_REQUEST_x() macros are intended to reduce
- boiler-plate code for SSAM request definitions by defining a wrapper
- function for the specified request. The client device variants of those
- macros, i.e. SSAM_DEFINE_SYNC_REQUEST_CL_x() in particular rely on the
- multi-device (MD) variants, e.g.:
- #define SSAM_DEFINE_SYNC_REQUEST_CL_R(name, rtype, spec...) \
- SSAM_DEFINE_SYNC_REQUEST_MD_R(__raw_##name, rtype, spec) \
- int name(struct ssam_device *sdev, rtype *ret) \
- { \
- return __raw_##name(sdev->ctrl, sdev->uid.target, \
- sdev->uid.instance, ret); \
- }
- This now creates the problem that it is not possible to declare the
- generated functions static via
- static SSAM_DEFINE_SYNC_REQUEST_CL_R(...)
- as this will only apply to the function defined by the multi-device
- macro, i.e. SSAM_DEFINE_SYNC_REQUEST_MD_R(). Thus compiling with
- `-Wmissing-prototypes' rightfully complains that there is a 'static'
- keyword missing.
- To solve this, make all SSAM_DEFINE_SYNC_REQUEST_x() macros define
- static functions. Non-client-device macros are also changed for
- consistency. In general, we expect those functions to be only used
- locally in the respective drivers for the corresponding interfaces, so
- having to define a wrapper function to be able to export this should be
- the odd case out.
- Reported-by: kernel test robot <lkp@intel.com>
- Fixes: b78b4982d763 ("platform/surface: Add platform profile driver")
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Link: https://lore.kernel.org/r/20210304190524.1172197-1-luzmaximilian@gmail.com
- Signed-off-by: Hans de Goede <hdegoede@redhat.com>
- Patchset: surface-sam
- ---
- .../driver-api/surface_aggregator/client.rst | 4 +-
- .../platform/surface/aggregator/controller.c | 10 +--
- .../surface/surface_aggregator_registry.c | 2 +-
- drivers/platform/surface/surface_dtx.c | 18 ++---
- drivers/platform/surface/surface_perfmode.c | 4 +-
- drivers/power/supply/surface_battery.c | 8 +-
- drivers/power/supply/surface_charger.c | 4 +-
- include/linux/surface_aggregator/controller.h | 74 +++++++++----------
- include/linux/surface_aggregator/device.h | 31 ++++----
- 9 files changed, 78 insertions(+), 77 deletions(-)
- diff --git a/Documentation/driver-api/surface_aggregator/client.rst b/Documentation/driver-api/surface_aggregator/client.rst
- index 26d13085a117..e519d374c378 100644
- --- a/Documentation/driver-api/surface_aggregator/client.rst
- +++ b/Documentation/driver-api/surface_aggregator/client.rst
- @@ -248,7 +248,7 @@ This example defines a function
-
- .. code-block:: c
-
- - int __ssam_tmp_perf_mode_set(struct ssam_controller *ctrl, const __le32 *arg);
- + static int __ssam_tmp_perf_mode_set(struct ssam_controller *ctrl, const __le32 *arg);
-
- executing the specified request, with the controller passed in when calling
- said function. In this example, the argument is provided via the ``arg``
- @@ -296,7 +296,7 @@ This invocation of the macro defines a function
-
- .. code-block:: c
-
- - int ssam_bat_get_sta(struct ssam_device *sdev, __le32 *ret);
- + static int ssam_bat_get_sta(struct ssam_device *sdev, __le32 *ret);
-
- executing the specified request, using the device IDs and controller given
- in the client device. The full list of such macros for client devices is:
- diff --git a/drivers/platform/surface/aggregator/controller.c b/drivers/platform/surface/aggregator/controller.c
- index 5bcb59ed579d..aa6f37b4f46e 100644
- --- a/drivers/platform/surface/aggregator/controller.c
- +++ b/drivers/platform/surface/aggregator/controller.c
- @@ -1750,35 +1750,35 @@ EXPORT_SYMBOL_GPL(ssam_request_sync_with_buffer);
-
- /* -- Internal SAM requests. ------------------------------------------------ */
-
- -static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_get_firmware_version, __le32, {
- +SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_get_firmware_version, __le32, {
- .target_category = SSAM_SSH_TC_SAM,
- .target_id = 0x01,
- .command_id = 0x13,
- .instance_id = 0x00,
- });
-
- -static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_off, u8, {
- +SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_off, u8, {
- .target_category = SSAM_SSH_TC_SAM,
- .target_id = 0x01,
- .command_id = 0x15,
- .instance_id = 0x00,
- });
-
- -static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_on, u8, {
- +SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_on, u8, {
- .target_category = SSAM_SSH_TC_SAM,
- .target_id = 0x01,
- .command_id = 0x16,
- .instance_id = 0x00,
- });
-
- -static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_exit, u8, {
- +SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_exit, u8, {
- .target_category = SSAM_SSH_TC_SAM,
- .target_id = 0x01,
- .command_id = 0x33,
- .instance_id = 0x00,
- });
-
- -static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_entry, u8, {
- +SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_entry, u8, {
- .target_category = SSAM_SSH_TC_SAM,
- .target_id = 0x01,
- .command_id = 0x34,
- diff --git a/drivers/platform/surface/surface_aggregator_registry.c b/drivers/platform/surface/surface_aggregator_registry.c
- index 304d601980ed..eccb9d1007cd 100644
- --- a/drivers/platform/surface/surface_aggregator_registry.c
- +++ b/drivers/platform/surface/surface_aggregator_registry.c
- @@ -302,7 +302,7 @@ struct ssam_base_hub {
- struct ssam_event_notifier notif;
- };
-
- -static SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_query_opmode, u8, {
- +SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_query_opmode, u8, {
- .target_category = SSAM_SSH_TC_BAS,
- .target_id = 0x01,
- .command_id = 0x0d,
- diff --git a/drivers/platform/surface/surface_dtx.c b/drivers/platform/surface/surface_dtx.c
- index 4bb5d286bf95..85451eb94d98 100644
- --- a/drivers/platform/surface/surface_dtx.c
- +++ b/drivers/platform/surface/surface_dtx.c
- @@ -69,63 +69,63 @@ struct ssam_bas_base_info {
-
- static_assert(sizeof(struct ssam_bas_base_info) == 2);
-
- -static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_lock, {
- +SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_lock, {
- .target_category = SSAM_SSH_TC_BAS,
- .target_id = 0x01,
- .command_id = 0x06,
- .instance_id = 0x00,
- });
-
- -static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_unlock, {
- +SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_unlock, {
- .target_category = SSAM_SSH_TC_BAS,
- .target_id = 0x01,
- .command_id = 0x07,
- .instance_id = 0x00,
- });
-
- -static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_request, {
- +SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_request, {
- .target_category = SSAM_SSH_TC_BAS,
- .target_id = 0x01,
- .command_id = 0x08,
- .instance_id = 0x00,
- });
-
- -static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_confirm, {
- +SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_confirm, {
- .target_category = SSAM_SSH_TC_BAS,
- .target_id = 0x01,
- .command_id = 0x09,
- .instance_id = 0x00,
- });
-
- -static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_heartbeat, {
- +SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_heartbeat, {
- .target_category = SSAM_SSH_TC_BAS,
- .target_id = 0x01,
- .command_id = 0x0a,
- .instance_id = 0x00,
- });
-
- -static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_cancel, {
- +SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_cancel, {
- .target_category = SSAM_SSH_TC_BAS,
- .target_id = 0x01,
- .command_id = 0x0b,
- .instance_id = 0x00,
- });
-
- -static SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_base, struct ssam_bas_base_info, {
- +SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_base, struct ssam_bas_base_info, {
- .target_category = SSAM_SSH_TC_BAS,
- .target_id = 0x01,
- .command_id = 0x0c,
- .instance_id = 0x00,
- });
-
- -static SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_device_mode, u8, {
- +SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_device_mode, u8, {
- .target_category = SSAM_SSH_TC_BAS,
- .target_id = 0x01,
- .command_id = 0x0d,
- .instance_id = 0x00,
- });
-
- -static SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_latch_status, u8, {
- +SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_latch_status, u8, {
- .target_category = SSAM_SSH_TC_BAS,
- .target_id = 0x01,
- .command_id = 0x11,
- diff --git a/drivers/platform/surface/surface_perfmode.c b/drivers/platform/surface/surface_perfmode.c
- index 3b92a43f8606..a9114e001d0d 100644
- --- a/drivers/platform/surface/surface_perfmode.c
- +++ b/drivers/platform/surface/surface_perfmode.c
- @@ -33,12 +33,12 @@ struct ssam_perf_info {
- __le16 unknown2;
- } __packed;
-
- -static SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_tmp_perf_mode_get, struct ssam_perf_info, {
- +SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_tmp_perf_mode_get, struct ssam_perf_info, {
- .target_category = SSAM_SSH_TC_TMP,
- .command_id = 0x02,
- });
-
- -static SSAM_DEFINE_SYNC_REQUEST_CL_W(__ssam_tmp_perf_mode_set, __le32, {
- +SSAM_DEFINE_SYNC_REQUEST_CL_W(__ssam_tmp_perf_mode_set, __le32, {
- .target_category = SSAM_SSH_TC_TMP,
- .command_id = 0x03,
- });
- diff --git a/drivers/power/supply/surface_battery.c b/drivers/power/supply/surface_battery.c
- index 327fd7af386b..b93a4f556b5c 100644
- --- a/drivers/power/supply/surface_battery.c
- +++ b/drivers/power/supply/surface_battery.c
- @@ -85,25 +85,25 @@ static_assert(sizeof(struct spwr_bst) == 16);
- #define SPWR_BATTERY_VALUE_UNKNOWN 0xffffffff
-
- /* Get battery status (_STA) */
- -static SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_sta, __le32, {
- +SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_sta, __le32, {
- .target_category = SSAM_SSH_TC_BAT,
- .command_id = 0x01,
- });
-
- /* Get battery static information (_BIX). */
- -static SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_bix, struct spwr_bix, {
- +SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_bix, struct spwr_bix, {
- .target_category = SSAM_SSH_TC_BAT,
- .command_id = 0x02,
- });
-
- /* Get battery dynamic information (_BST). */
- -static SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_bst, struct spwr_bst, {
- +SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_bst, struct spwr_bst, {
- .target_category = SSAM_SSH_TC_BAT,
- .command_id = 0x03,
- });
-
- /* Set battery trip point (_BTP). */
- -static SSAM_DEFINE_SYNC_REQUEST_CL_W(ssam_bat_set_btp, __le32, {
- +SSAM_DEFINE_SYNC_REQUEST_CL_W(ssam_bat_set_btp, __le32, {
- .target_category = SSAM_SSH_TC_BAT,
- .command_id = 0x04,
- });
- diff --git a/drivers/power/supply/surface_charger.c b/drivers/power/supply/surface_charger.c
- index 982f9b9ef6f5..fe484523a2c2 100644
- --- a/drivers/power/supply/surface_charger.c
- +++ b/drivers/power/supply/surface_charger.c
- @@ -28,13 +28,13 @@ enum sam_battery_sta {
- };
-
- /* Get battery status (_STA). */
- -static SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_sta, __le32, {
- +SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_sta, __le32, {
- .target_category = SSAM_SSH_TC_BAT,
- .command_id = 0x01,
- });
-
- /* Get platform power source for battery (_PSR / DPTF PSRC). */
- -static SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_psrc, __le32, {
- +SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_psrc, __le32, {
- .target_category = SSAM_SSH_TC_BAT,
- .command_id = 0x0d,
- });
- diff --git a/include/linux/surface_aggregator/controller.h b/include/linux/surface_aggregator/controller.h
- index f4b1ba887384..0806796eabcb 100644
- --- a/include/linux/surface_aggregator/controller.h
- +++ b/include/linux/surface_aggregator/controller.h
- @@ -344,16 +344,16 @@ struct ssam_request_spec_md {
- * request has been fully completed. The required transport buffer will be
- * allocated on the stack.
- *
- - * The generated function is defined as ``int name(struct ssam_controller
- - * *ctrl)``, returning the status of the request, which is zero on success and
- - * negative on failure. The ``ctrl`` parameter is the controller via which the
- - * request is being sent.
- + * The generated function is defined as ``static int name(struct
- + * ssam_controller *ctrl)``, returning the status of the request, which is
- + * zero on success and negative on failure. The ``ctrl`` parameter is the
- + * controller via which the request is being sent.
- *
- * Refer to ssam_request_sync_onstack() for more details on the behavior of
- * the generated function.
- */
- #define SSAM_DEFINE_SYNC_REQUEST_N(name, spec...) \
- - int name(struct ssam_controller *ctrl) \
- + static int name(struct ssam_controller *ctrl) \
- { \
- struct ssam_request_spec s = (struct ssam_request_spec)spec; \
- struct ssam_request rqst; \
- @@ -383,17 +383,17 @@ struct ssam_request_spec_md {
- * returning once the request has been fully completed. The required transport
- * buffer will be allocated on the stack.
- *
- - * The generated function is defined as ``int name(struct ssam_controller
- - * *ctrl, const atype *arg)``, returning the status of the request, which is
- - * zero on success and negative on failure. The ``ctrl`` parameter is the
- - * controller via which the request is sent. The request argument is specified
- - * via the ``arg`` pointer.
- + * The generated function is defined as ``static int name(struct
- + * ssam_controller *ctrl, const atype *arg)``, returning the status of the
- + * request, which is zero on success and negative on failure. The ``ctrl``
- + * parameter is the controller via which the request is sent. The request
- + * argument is specified via the ``arg`` pointer.
- *
- * Refer to ssam_request_sync_onstack() for more details on the behavior of
- * the generated function.
- */
- #define SSAM_DEFINE_SYNC_REQUEST_W(name, atype, spec...) \
- - int name(struct ssam_controller *ctrl, const atype *arg) \
- + static int name(struct ssam_controller *ctrl, const atype *arg) \
- { \
- struct ssam_request_spec s = (struct ssam_request_spec)spec; \
- struct ssam_request rqst; \
- @@ -424,17 +424,17 @@ struct ssam_request_spec_md {
- * request itself, returning once the request has been fully completed. The
- * required transport buffer will be allocated on the stack.
- *
- - * The generated function is defined as ``int name(struct ssam_controller
- - * *ctrl, rtype *ret)``, returning the status of the request, which is zero on
- - * success and negative on failure. The ``ctrl`` parameter is the controller
- - * via which the request is sent. The request's return value is written to the
- - * memory pointed to by the ``ret`` parameter.
- + * The generated function is defined as ``static int name(struct
- + * ssam_controller *ctrl, rtype *ret)``, returning the status of the request,
- + * which is zero on success and negative on failure. The ``ctrl`` parameter is
- + * the controller via which the request is sent. The request's return value is
- + * written to the memory pointed to by the ``ret`` parameter.
- *
- * Refer to ssam_request_sync_onstack() for more details on the behavior of
- * the generated function.
- */
- #define SSAM_DEFINE_SYNC_REQUEST_R(name, rtype, spec...) \
- - int name(struct ssam_controller *ctrl, rtype *ret) \
- + static int name(struct ssam_controller *ctrl, rtype *ret) \
- { \
- struct ssam_request_spec s = (struct ssam_request_spec)spec; \
- struct ssam_request rqst; \
- @@ -483,17 +483,17 @@ struct ssam_request_spec_md {
- * returning once the request has been fully completed. The required transport
- * buffer will be allocated on the stack.
- *
- - * The generated function is defined as ``int name(struct ssam_controller
- - * *ctrl, u8 tid, u8 iid)``, returning the status of the request, which is
- - * zero on success and negative on failure. The ``ctrl`` parameter is the
- - * controller via which the request is sent, ``tid`` the target ID for the
- - * request, and ``iid`` the instance ID.
- + * The generated function is defined as ``static int name(struct
- + * ssam_controller *ctrl, u8 tid, u8 iid)``, returning the status of the
- + * request, which is zero on success and negative on failure. The ``ctrl``
- + * parameter is the controller via which the request is sent, ``tid`` the
- + * target ID for the request, and ``iid`` the instance ID.
- *
- * Refer to ssam_request_sync_onstack() for more details on the behavior of
- * the generated function.
- */
- #define SSAM_DEFINE_SYNC_REQUEST_MD_N(name, spec...) \
- - int name(struct ssam_controller *ctrl, u8 tid, u8 iid) \
- + static int name(struct ssam_controller *ctrl, u8 tid, u8 iid) \
- { \
- struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
- struct ssam_request rqst; \
- @@ -524,18 +524,18 @@ struct ssam_request_spec_md {
- * the request itself, returning once the request has been fully completed.
- * The required transport buffer will be allocated on the stack.
- *
- - * The generated function is defined as ``int name(struct ssam_controller
- - * *ctrl, u8 tid, u8 iid, const atype *arg)``, returning the status of the
- - * request, which is zero on success and negative on failure. The ``ctrl``
- - * parameter is the controller via which the request is sent, ``tid`` the
- - * target ID for the request, and ``iid`` the instance ID. The request argument
- - * is specified via the ``arg`` pointer.
- + * The generated function is defined as ``static int name(struct
- + * ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg)``, returning the
- + * status of the request, which is zero on success and negative on failure.
- + * The ``ctrl`` parameter is the controller via which the request is sent,
- + * ``tid`` the target ID for the request, and ``iid`` the instance ID. The
- + * request argument is specified via the ``arg`` pointer.
- *
- * Refer to ssam_request_sync_onstack() for more details on the behavior of
- * the generated function.
- */
- #define SSAM_DEFINE_SYNC_REQUEST_MD_W(name, atype, spec...) \
- - int name(struct ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg)\
- + static int name(struct ssam_controller *ctrl, u8 tid, u8 iid, const atype *arg) \
- { \
- struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
- struct ssam_request rqst; \
- @@ -567,18 +567,18 @@ struct ssam_request_spec_md {
- * execution of the request itself, returning once the request has been fully
- * completed. The required transport buffer will be allocated on the stack.
- *
- - * The generated function is defined as ``int name(struct ssam_controller
- - * *ctrl, u8 tid, u8 iid, rtype *ret)``, returning the status of the request,
- - * which is zero on success and negative on failure. The ``ctrl`` parameter is
- - * the controller via which the request is sent, ``tid`` the target ID for the
- - * request, and ``iid`` the instance ID. The request's return value is written
- - * to the memory pointed to by the ``ret`` parameter.
- + * The generated function is defined as ``static int name(struct
- + * ssam_controller *ctrl, u8 tid, u8 iid, rtype *ret)``, returning the status
- + * of the request, which is zero on success and negative on failure. The
- + * ``ctrl`` parameter is the controller via which the request is sent, ``tid``
- + * the target ID for the request, and ``iid`` the instance ID. The request's
- + * return value is written to the memory pointed to by the ``ret`` parameter.
- *
- * Refer to ssam_request_sync_onstack() for more details on the behavior of
- * the generated function.
- */
- #define SSAM_DEFINE_SYNC_REQUEST_MD_R(name, rtype, spec...) \
- - int name(struct ssam_controller *ctrl, u8 tid, u8 iid, rtype *ret) \
- + static int name(struct ssam_controller *ctrl, u8 tid, u8 iid, rtype *ret) \
- { \
- struct ssam_request_spec_md s = (struct ssam_request_spec_md)spec; \
- struct ssam_request rqst; \
- diff --git a/include/linux/surface_aggregator/device.h b/include/linux/surface_aggregator/device.h
- index 02f3e06c0a60..4441ad667c3f 100644
- --- a/include/linux/surface_aggregator/device.h
- +++ b/include/linux/surface_aggregator/device.h
- @@ -336,17 +336,18 @@ void ssam_device_driver_unregister(struct ssam_device_driver *d);
- * request has been fully completed. The required transport buffer will be
- * allocated on the stack.
- *
- - * The generated function is defined as ``int name(struct ssam_device *sdev)``,
- - * returning the status of the request, which is zero on success and negative
- - * on failure. The ``sdev`` parameter specifies both the target device of the
- - * request and by association the controller via which the request is sent.
- + * The generated function is defined as ``static int name(struct ssam_device
- + * *sdev)``, returning the status of the request, which is zero on success and
- + * negative on failure. The ``sdev`` parameter specifies both the target
- + * device of the request and by association the controller via which the
- + * request is sent.
- *
- * Refer to ssam_request_sync_onstack() for more details on the behavior of
- * the generated function.
- */
- #define SSAM_DEFINE_SYNC_REQUEST_CL_N(name, spec...) \
- SSAM_DEFINE_SYNC_REQUEST_MD_N(__raw_##name, spec) \
- - int name(struct ssam_device *sdev) \
- + static int name(struct ssam_device *sdev) \
- { \
- return __raw_##name(sdev->ctrl, sdev->uid.target, \
- sdev->uid.instance); \
- @@ -368,19 +369,19 @@ void ssam_device_driver_unregister(struct ssam_device_driver *d);
- * itself, returning once the request has been fully completed. The required
- * transport buffer will be allocated on the stack.
- *
- - * The generated function is defined as ``int name(struct ssam_device *sdev,
- - * const atype *arg)``, returning the status of the request, which is zero on
- - * success and negative on failure. The ``sdev`` parameter specifies both the
- - * target device of the request and by association the controller via which
- - * the request is sent. The request's argument is specified via the ``arg``
- - * pointer.
- + * The generated function is defined as ``static int name(struct ssam_device
- + * *sdev, const atype *arg)``, returning the status of the request, which is
- + * zero on success and negative on failure. The ``sdev`` parameter specifies
- + * both the target device of the request and by association the controller via
- + * which the request is sent. The request's argument is specified via the
- + * ``arg`` pointer.
- *
- * Refer to ssam_request_sync_onstack() for more details on the behavior of
- * the generated function.
- */
- #define SSAM_DEFINE_SYNC_REQUEST_CL_W(name, atype, spec...) \
- SSAM_DEFINE_SYNC_REQUEST_MD_W(__raw_##name, atype, spec) \
- - int name(struct ssam_device *sdev, const atype *arg) \
- + static int name(struct ssam_device *sdev, const atype *arg) \
- { \
- return __raw_##name(sdev->ctrl, sdev->uid.target, \
- sdev->uid.instance, arg); \
- @@ -402,8 +403,8 @@ void ssam_device_driver_unregister(struct ssam_device_driver *d);
- * itself, returning once the request has been fully completed. The required
- * transport buffer will be allocated on the stack.
- *
- - * The generated function is defined as ``int name(struct ssam_device *sdev,
- - * rtype *ret)``, returning the status of the request, which is zero on
- + * The generated function is defined as ``static int name(struct ssam_device
- + * *sdev, rtype *ret)``, returning the status of the request, which is zero on
- * success and negative on failure. The ``sdev`` parameter specifies both the
- * target device of the request and by association the controller via which
- * the request is sent. The request's return value is written to the memory
- @@ -414,7 +415,7 @@ void ssam_device_driver_unregister(struct ssam_device_driver *d);
- */
- #define SSAM_DEFINE_SYNC_REQUEST_CL_R(name, rtype, spec...) \
- SSAM_DEFINE_SYNC_REQUEST_MD_R(__raw_##name, rtype, spec) \
- - int name(struct ssam_device *sdev, rtype *ret) \
- + static int name(struct ssam_device *sdev, rtype *ret) \
- { \
- return __raw_##name(sdev->ctrl, sdev->uid.target, \
- sdev->uid.instance, ret); \
- --
- 2.31.0
|