core.c 145 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/base/core.c - core driver model code (device registration, etc)
  4. *
  5. * Copyright (c) 2002-3 Patrick Mochel
  6. * Copyright (c) 2002-3 Open Source Development Labs
  7. * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
  8. * Copyright (c) 2006 Novell, Inc.
  9. */
  10. #include <linux/acpi.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/cleanup.h>
  13. #include <linux/cpufreq.h>
  14. #include <linux/device.h>
  15. #include <linux/dma-map-ops.h> /* for dma_default_coherent */
  16. #include <linux/err.h>
  17. #include <linux/fwnode.h>
  18. #include <linux/init.h>
  19. #include <linux/kdev_t.h>
  20. #include <linux/kstrtox.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/notifier.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/sched/mm.h>
  29. #include <linux/sched/signal.h>
  30. #include <linux/slab.h>
  31. #include <linux/string_helpers.h>
  32. #include <linux/swiotlb.h>
  33. #include <linux/sysfs.h>
  34. #include "base.h"
  35. #include "physical_location.h"
  36. #include "power/power.h"
  37. /* Device links support. */
  38. static LIST_HEAD(deferred_sync);
  39. static unsigned int defer_sync_state_count = 1;
  40. static DEFINE_MUTEX(fwnode_link_lock);
  41. static bool fw_devlink_is_permissive(void);
  42. static void __fw_devlink_link_to_consumers(struct device *dev);
  43. static bool fw_devlink_drv_reg_done;
  44. static bool fw_devlink_best_effort;
  45. static struct workqueue_struct *device_link_wq;
  46. /**
  47. * __fwnode_link_add - Create a link between two fwnode_handles.
  48. * @con: Consumer end of the link.
  49. * @sup: Supplier end of the link.
  50. * @flags: Link flags.
  51. *
  52. * Create a fwnode link between fwnode handles @con and @sup. The fwnode link
  53. * represents the detail that the firmware lists @sup fwnode as supplying a
  54. * resource to @con.
  55. *
  56. * The driver core will use the fwnode link to create a device link between the
  57. * two device objects corresponding to @con and @sup when they are created. The
  58. * driver core will automatically delete the fwnode link between @con and @sup
  59. * after doing that.
  60. *
  61. * Attempts to create duplicate links between the same pair of fwnode handles
  62. * are ignored and there is no reference counting.
  63. */
  64. static int __fwnode_link_add(struct fwnode_handle *con,
  65. struct fwnode_handle *sup, u8 flags)
  66. {
  67. struct fwnode_link *link;
  68. list_for_each_entry(link, &sup->consumers, s_hook)
  69. if (link->consumer == con) {
  70. link->flags |= flags;
  71. return 0;
  72. }
  73. link = kzalloc_obj(*link);
  74. if (!link)
  75. return -ENOMEM;
  76. link->supplier = sup;
  77. INIT_LIST_HEAD(&link->s_hook);
  78. link->consumer = con;
  79. INIT_LIST_HEAD(&link->c_hook);
  80. link->flags = flags;
  81. list_add(&link->s_hook, &sup->consumers);
  82. list_add(&link->c_hook, &con->suppliers);
  83. pr_debug("%pfwf Linked as a fwnode consumer to %pfwf\n",
  84. con, sup);
  85. return 0;
  86. }
  87. int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup,
  88. u8 flags)
  89. {
  90. guard(mutex)(&fwnode_link_lock);
  91. return __fwnode_link_add(con, sup, flags);
  92. }
  93. /**
  94. * __fwnode_link_del - Delete a link between two fwnode_handles.
  95. * @link: the fwnode_link to be deleted
  96. *
  97. * The fwnode_link_lock needs to be held when this function is called.
  98. */
  99. static void __fwnode_link_del(struct fwnode_link *link)
  100. {
  101. pr_debug("%pfwf Dropping the fwnode link to %pfwf\n",
  102. link->consumer, link->supplier);
  103. list_del(&link->s_hook);
  104. list_del(&link->c_hook);
  105. kfree(link);
  106. }
  107. /**
  108. * __fwnode_link_cycle - Mark a fwnode link as being part of a cycle.
  109. * @link: the fwnode_link to be marked
  110. *
  111. * The fwnode_link_lock needs to be held when this function is called.
  112. */
  113. static void __fwnode_link_cycle(struct fwnode_link *link)
  114. {
  115. pr_debug("%pfwf: cycle: depends on %pfwf\n",
  116. link->consumer, link->supplier);
  117. link->flags |= FWLINK_FLAG_CYCLE;
  118. }
  119. /**
  120. * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle.
  121. * @fwnode: fwnode whose supplier links need to be deleted
  122. *
  123. * Deletes all supplier links connecting directly to @fwnode.
  124. */
  125. static void fwnode_links_purge_suppliers(struct fwnode_handle *fwnode)
  126. {
  127. struct fwnode_link *link, *tmp;
  128. guard(mutex)(&fwnode_link_lock);
  129. list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook)
  130. __fwnode_link_del(link);
  131. }
  132. /**
  133. * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle.
  134. * @fwnode: fwnode whose consumer links need to be deleted
  135. *
  136. * Deletes all consumer links connecting directly to @fwnode.
  137. */
  138. static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode)
  139. {
  140. struct fwnode_link *link, *tmp;
  141. guard(mutex)(&fwnode_link_lock);
  142. list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook)
  143. __fwnode_link_del(link);
  144. }
  145. /**
  146. * fwnode_links_purge - Delete all links connected to a fwnode_handle.
  147. * @fwnode: fwnode whose links needs to be deleted
  148. *
  149. * Deletes all links connecting directly to a fwnode.
  150. */
  151. void fwnode_links_purge(struct fwnode_handle *fwnode)
  152. {
  153. fwnode_links_purge_suppliers(fwnode);
  154. fwnode_links_purge_consumers(fwnode);
  155. }
  156. void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode)
  157. {
  158. struct fwnode_handle *child;
  159. /* Don't purge consumer links of an added child */
  160. if (fwnode->dev)
  161. return;
  162. fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
  163. fwnode_links_purge_consumers(fwnode);
  164. fwnode_for_each_available_child_node(fwnode, child)
  165. fw_devlink_purge_absent_suppliers(child);
  166. }
  167. EXPORT_SYMBOL_GPL(fw_devlink_purge_absent_suppliers);
  168. /**
  169. * __fwnode_links_move_consumers - Move consumer from @from to @to fwnode_handle
  170. * @from: move consumers away from this fwnode
  171. * @to: move consumers to this fwnode
  172. *
  173. * Move all consumer links from @from fwnode to @to fwnode.
  174. */
  175. static void __fwnode_links_move_consumers(struct fwnode_handle *from,
  176. struct fwnode_handle *to)
  177. {
  178. struct fwnode_link *link, *tmp;
  179. list_for_each_entry_safe(link, tmp, &from->consumers, s_hook) {
  180. __fwnode_link_add(link->consumer, to, link->flags);
  181. __fwnode_link_del(link);
  182. }
  183. }
  184. /**
  185. * __fw_devlink_pickup_dangling_consumers - Pick up dangling consumers
  186. * @fwnode: fwnode from which to pick up dangling consumers
  187. * @new_sup: fwnode of new supplier
  188. *
  189. * If the @fwnode has a corresponding struct device and the device supports
  190. * probing (that is, added to a bus), then we want to let fw_devlink create
  191. * MANAGED device links to this device, so leave @fwnode and its descendant's
  192. * fwnode links alone.
  193. *
  194. * Otherwise, move its consumers to the new supplier @new_sup.
  195. */
  196. static void __fw_devlink_pickup_dangling_consumers(struct fwnode_handle *fwnode,
  197. struct fwnode_handle *new_sup)
  198. {
  199. struct fwnode_handle *child;
  200. if (fwnode->dev && fwnode->dev->bus)
  201. return;
  202. fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
  203. __fwnode_links_move_consumers(fwnode, new_sup);
  204. fwnode_for_each_available_child_node(fwnode, child)
  205. __fw_devlink_pickup_dangling_consumers(child, new_sup);
  206. }
  207. static DEFINE_MUTEX(device_links_lock);
  208. DEFINE_STATIC_SRCU(device_links_srcu);
  209. static inline void device_links_write_lock(void)
  210. {
  211. mutex_lock(&device_links_lock);
  212. }
  213. static inline void device_links_write_unlock(void)
  214. {
  215. mutex_unlock(&device_links_lock);
  216. }
  217. int device_links_read_lock(void) __acquires(&device_links_srcu)
  218. {
  219. return srcu_read_lock(&device_links_srcu);
  220. }
  221. void device_links_read_unlock(int idx) __releases(&device_links_srcu)
  222. {
  223. srcu_read_unlock(&device_links_srcu, idx);
  224. }
  225. int device_links_read_lock_held(void)
  226. {
  227. return srcu_read_lock_held(&device_links_srcu);
  228. }
  229. static void device_link_synchronize_removal(void)
  230. {
  231. synchronize_srcu(&device_links_srcu);
  232. }
  233. static void device_link_remove_from_lists(struct device_link *link)
  234. {
  235. list_del_rcu(&link->s_node);
  236. list_del_rcu(&link->c_node);
  237. }
  238. static bool device_is_ancestor(struct device *dev, struct device *target)
  239. {
  240. while (target->parent) {
  241. target = target->parent;
  242. if (dev == target)
  243. return true;
  244. }
  245. return false;
  246. }
  247. #define DL_MARKER_FLAGS (DL_FLAG_INFERRED | \
  248. DL_FLAG_CYCLE | \
  249. DL_FLAG_MANAGED)
  250. bool device_link_flag_is_sync_state_only(u32 flags)
  251. {
  252. return (flags & ~DL_MARKER_FLAGS) == DL_FLAG_SYNC_STATE_ONLY;
  253. }
  254. /**
  255. * device_is_dependent - Check if one device depends on another one
  256. * @dev: Device to check dependencies for.
  257. * @target: Device to check against.
  258. *
  259. * Check if @target depends on @dev or any device dependent on it (its child or
  260. * its consumer etc). Return 1 if that is the case or 0 otherwise.
  261. */
  262. static int device_is_dependent(struct device *dev, void *target)
  263. {
  264. struct device_link *link;
  265. int ret;
  266. /*
  267. * The "ancestors" check is needed to catch the case when the target
  268. * device has not been completely initialized yet and it is still
  269. * missing from the list of children of its parent device.
  270. */
  271. if (dev == target || device_is_ancestor(dev, target))
  272. return 1;
  273. ret = device_for_each_child(dev, target, device_is_dependent);
  274. if (ret)
  275. return ret;
  276. list_for_each_entry(link, &dev->links.consumers, s_node) {
  277. if (device_link_flag_is_sync_state_only(link->flags))
  278. continue;
  279. if (link->consumer == target)
  280. return 1;
  281. ret = device_is_dependent(link->consumer, target);
  282. if (ret)
  283. break;
  284. }
  285. return ret;
  286. }
  287. static void device_link_init_status(struct device_link *link,
  288. struct device *consumer,
  289. struct device *supplier)
  290. {
  291. switch (supplier->links.status) {
  292. case DL_DEV_PROBING:
  293. switch (consumer->links.status) {
  294. case DL_DEV_PROBING:
  295. /*
  296. * A consumer driver can create a link to a supplier
  297. * that has not completed its probing yet as long as it
  298. * knows that the supplier is already functional (for
  299. * example, it has just acquired some resources from the
  300. * supplier).
  301. */
  302. link->status = DL_STATE_CONSUMER_PROBE;
  303. break;
  304. default:
  305. link->status = DL_STATE_DORMANT;
  306. break;
  307. }
  308. break;
  309. case DL_DEV_DRIVER_BOUND:
  310. switch (consumer->links.status) {
  311. case DL_DEV_PROBING:
  312. link->status = DL_STATE_CONSUMER_PROBE;
  313. break;
  314. case DL_DEV_DRIVER_BOUND:
  315. link->status = DL_STATE_ACTIVE;
  316. break;
  317. default:
  318. link->status = DL_STATE_AVAILABLE;
  319. break;
  320. }
  321. break;
  322. case DL_DEV_UNBINDING:
  323. link->status = DL_STATE_SUPPLIER_UNBIND;
  324. break;
  325. default:
  326. link->status = DL_STATE_DORMANT;
  327. break;
  328. }
  329. }
  330. static int device_reorder_to_tail(struct device *dev, void *not_used)
  331. {
  332. struct device_link *link;
  333. /*
  334. * Devices that have not been registered yet will be put to the ends
  335. * of the lists during the registration, so skip them here.
  336. */
  337. if (device_is_registered(dev))
  338. devices_kset_move_last(dev);
  339. if (device_pm_initialized(dev))
  340. device_pm_move_last(dev);
  341. device_for_each_child(dev, NULL, device_reorder_to_tail);
  342. list_for_each_entry(link, &dev->links.consumers, s_node) {
  343. if (device_link_flag_is_sync_state_only(link->flags))
  344. continue;
  345. device_reorder_to_tail(link->consumer, NULL);
  346. }
  347. return 0;
  348. }
  349. /**
  350. * device_pm_move_to_tail - Move set of devices to the end of device lists
  351. * @dev: Device to move
  352. *
  353. * This is a device_reorder_to_tail() wrapper taking the requisite locks.
  354. *
  355. * It moves the @dev along with all of its children and all of its consumers
  356. * to the ends of the device_kset and dpm_list, recursively.
  357. */
  358. void device_pm_move_to_tail(struct device *dev)
  359. {
  360. int idx;
  361. idx = device_links_read_lock();
  362. device_pm_lock();
  363. device_reorder_to_tail(dev, NULL);
  364. device_pm_unlock();
  365. device_links_read_unlock(idx);
  366. }
  367. #define to_devlink(dev) container_of((dev), struct device_link, link_dev)
  368. static ssize_t status_show(struct device *dev,
  369. struct device_attribute *attr, char *buf)
  370. {
  371. const char *output;
  372. switch (to_devlink(dev)->status) {
  373. case DL_STATE_NONE:
  374. output = "not tracked";
  375. break;
  376. case DL_STATE_DORMANT:
  377. output = "dormant";
  378. break;
  379. case DL_STATE_AVAILABLE:
  380. output = "available";
  381. break;
  382. case DL_STATE_CONSUMER_PROBE:
  383. output = "consumer probing";
  384. break;
  385. case DL_STATE_ACTIVE:
  386. output = "active";
  387. break;
  388. case DL_STATE_SUPPLIER_UNBIND:
  389. output = "supplier unbinding";
  390. break;
  391. default:
  392. output = "unknown";
  393. break;
  394. }
  395. return sysfs_emit(buf, "%s\n", output);
  396. }
  397. static DEVICE_ATTR_RO(status);
  398. static ssize_t auto_remove_on_show(struct device *dev,
  399. struct device_attribute *attr, char *buf)
  400. {
  401. struct device_link *link = to_devlink(dev);
  402. const char *output;
  403. if (device_link_test(link, DL_FLAG_AUTOREMOVE_SUPPLIER))
  404. output = "supplier unbind";
  405. else if (device_link_test(link, DL_FLAG_AUTOREMOVE_CONSUMER))
  406. output = "consumer unbind";
  407. else
  408. output = "never";
  409. return sysfs_emit(buf, "%s\n", output);
  410. }
  411. static DEVICE_ATTR_RO(auto_remove_on);
  412. static ssize_t runtime_pm_show(struct device *dev,
  413. struct device_attribute *attr, char *buf)
  414. {
  415. struct device_link *link = to_devlink(dev);
  416. return sysfs_emit(buf, "%d\n", device_link_test(link, DL_FLAG_PM_RUNTIME));
  417. }
  418. static DEVICE_ATTR_RO(runtime_pm);
  419. static ssize_t sync_state_only_show(struct device *dev,
  420. struct device_attribute *attr, char *buf)
  421. {
  422. struct device_link *link = to_devlink(dev);
  423. return sysfs_emit(buf, "%d\n", device_link_test(link, DL_FLAG_SYNC_STATE_ONLY));
  424. }
  425. static DEVICE_ATTR_RO(sync_state_only);
  426. static struct attribute *devlink_attrs[] = {
  427. &dev_attr_status.attr,
  428. &dev_attr_auto_remove_on.attr,
  429. &dev_attr_runtime_pm.attr,
  430. &dev_attr_sync_state_only.attr,
  431. NULL,
  432. };
  433. ATTRIBUTE_GROUPS(devlink);
  434. static void device_link_release_fn(struct work_struct *work)
  435. {
  436. struct device_link *link = container_of(work, struct device_link, rm_work);
  437. /* Ensure that all references to the link object have been dropped. */
  438. device_link_synchronize_removal();
  439. pm_runtime_release_supplier(link);
  440. /*
  441. * If supplier_preactivated is set, the link has been dropped between
  442. * the pm_runtime_get_suppliers() and pm_runtime_put_suppliers() calls
  443. * in __driver_probe_device(). In that case, drop the supplier's
  444. * PM-runtime usage counter to remove the reference taken by
  445. * pm_runtime_get_suppliers().
  446. */
  447. if (link->supplier_preactivated)
  448. pm_runtime_put_noidle(link->supplier);
  449. pm_request_idle(link->supplier);
  450. put_device(link->consumer);
  451. put_device(link->supplier);
  452. kfree(link);
  453. }
  454. static void devlink_dev_release(struct device *dev)
  455. {
  456. struct device_link *link = to_devlink(dev);
  457. INIT_WORK(&link->rm_work, device_link_release_fn);
  458. /*
  459. * It may take a while to complete this work because of the SRCU
  460. * synchronization in device_link_release_fn() and if the consumer or
  461. * supplier devices get deleted when it runs, so put it into the
  462. * dedicated workqueue.
  463. */
  464. queue_work(device_link_wq, &link->rm_work);
  465. }
  466. /**
  467. * device_link_wait_removal - Wait for ongoing devlink removal jobs to terminate
  468. */
  469. void device_link_wait_removal(void)
  470. {
  471. /*
  472. * devlink removal jobs are queued in the dedicated work queue.
  473. * To be sure that all removal jobs are terminated, ensure that any
  474. * scheduled work has run to completion.
  475. */
  476. flush_workqueue(device_link_wq);
  477. }
  478. EXPORT_SYMBOL_GPL(device_link_wait_removal);
  479. static const struct class devlink_class = {
  480. .name = "devlink",
  481. .dev_groups = devlink_groups,
  482. .dev_release = devlink_dev_release,
  483. };
  484. static int devlink_add_symlinks(struct device *dev)
  485. {
  486. char *buf_con __free(kfree) = NULL, *buf_sup __free(kfree) = NULL;
  487. int ret;
  488. struct device_link *link = to_devlink(dev);
  489. struct device *sup = link->supplier;
  490. struct device *con = link->consumer;
  491. ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
  492. if (ret)
  493. goto out;
  494. ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
  495. if (ret)
  496. goto err_con;
  497. buf_con = kasprintf(GFP_KERNEL, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
  498. if (!buf_con) {
  499. ret = -ENOMEM;
  500. goto err_con_dev;
  501. }
  502. ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf_con);
  503. if (ret)
  504. goto err_con_dev;
  505. buf_sup = kasprintf(GFP_KERNEL, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
  506. if (!buf_sup) {
  507. ret = -ENOMEM;
  508. goto err_sup_dev;
  509. }
  510. ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf_sup);
  511. if (ret)
  512. goto err_sup_dev;
  513. goto out;
  514. err_sup_dev:
  515. sysfs_remove_link(&sup->kobj, buf_con);
  516. err_con_dev:
  517. sysfs_remove_link(&link->link_dev.kobj, "consumer");
  518. err_con:
  519. sysfs_remove_link(&link->link_dev.kobj, "supplier");
  520. out:
  521. return ret;
  522. }
  523. static void devlink_remove_symlinks(struct device *dev)
  524. {
  525. char *buf_con __free(kfree) = NULL, *buf_sup __free(kfree) = NULL;
  526. struct device_link *link = to_devlink(dev);
  527. struct device *sup = link->supplier;
  528. struct device *con = link->consumer;
  529. sysfs_remove_link(&link->link_dev.kobj, "consumer");
  530. sysfs_remove_link(&link->link_dev.kobj, "supplier");
  531. if (device_is_registered(con)) {
  532. buf_sup = kasprintf(GFP_KERNEL, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
  533. if (!buf_sup)
  534. goto out;
  535. sysfs_remove_link(&con->kobj, buf_sup);
  536. }
  537. buf_con = kasprintf(GFP_KERNEL, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
  538. if (!buf_con)
  539. goto out;
  540. sysfs_remove_link(&sup->kobj, buf_con);
  541. return;
  542. out:
  543. WARN(1, "Unable to properly free device link symlinks!\n");
  544. }
  545. static struct class_interface devlink_class_intf = {
  546. .class = &devlink_class,
  547. .add_dev = devlink_add_symlinks,
  548. .remove_dev = devlink_remove_symlinks,
  549. };
  550. static int __init devlink_class_init(void)
  551. {
  552. int ret;
  553. ret = class_register(&devlink_class);
  554. if (ret)
  555. return ret;
  556. ret = class_interface_register(&devlink_class_intf);
  557. if (ret)
  558. class_unregister(&devlink_class);
  559. return ret;
  560. }
  561. postcore_initcall(devlink_class_init);
  562. #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
  563. DL_FLAG_AUTOREMOVE_SUPPLIER | \
  564. DL_FLAG_AUTOPROBE_CONSUMER | \
  565. DL_FLAG_SYNC_STATE_ONLY | \
  566. DL_FLAG_INFERRED | \
  567. DL_FLAG_CYCLE)
  568. #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
  569. DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
  570. /**
  571. * device_link_add - Create a link between two devices.
  572. * @consumer: Consumer end of the link.
  573. * @supplier: Supplier end of the link.
  574. * @flags: Link flags.
  575. *
  576. * Return: On success, a device_link struct will be returned.
  577. * On error or invalid flag settings, NULL will be returned.
  578. *
  579. * The caller is responsible for the proper synchronization of the link creation
  580. * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
  581. * runtime PM framework to take the link into account. Second, if the
  582. * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
  583. * be forced into the active meta state and reference-counted upon the creation
  584. * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
  585. * ignored.
  586. *
  587. * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
  588. * expected to release the link returned by it directly with the help of either
  589. * device_link_del() or device_link_remove().
  590. *
  591. * If that flag is not set, however, the caller of this function is handing the
  592. * management of the link over to the driver core entirely and its return value
  593. * can only be used to check whether or not the link is present. In that case,
  594. * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
  595. * flags can be used to indicate to the driver core when the link can be safely
  596. * deleted. Namely, setting one of them in @flags indicates to the driver core
  597. * that the link is not going to be used (by the given caller of this function)
  598. * after unbinding the consumer or supplier driver, respectively, from its
  599. * device, so the link can be deleted at that point. If none of them is set,
  600. * the link will be maintained until one of the devices pointed to by it (either
  601. * the consumer or the supplier) is unregistered.
  602. *
  603. * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
  604. * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
  605. * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
  606. * be used to request the driver core to automatically probe for a consumer
  607. * driver after successfully binding a driver to the supplier device.
  608. *
  609. * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
  610. * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
  611. * the same time is invalid and will cause NULL to be returned upfront.
  612. * However, if a device link between the given @consumer and @supplier pair
  613. * exists already when this function is called for them, the existing link will
  614. * be returned regardless of its current type and status (the link's flags may
  615. * be modified then). The caller of this function is then expected to treat
  616. * the link as though it has just been created, so (in particular) if
  617. * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
  618. * explicitly when not needed any more (as stated above).
  619. *
  620. * A side effect of the link creation is re-ordering of dpm_list and the
  621. * devices_kset list by moving the consumer device and all devices depending
  622. * on it to the ends of these lists (that does not happen to devices that have
  623. * not been registered when this function is called).
  624. *
  625. * The supplier device is required to be registered when this function is called
  626. * and NULL will be returned if that is not the case. The consumer device need
  627. * not be registered, however.
  628. */
  629. struct device_link *device_link_add(struct device *consumer,
  630. struct device *supplier, u32 flags)
  631. {
  632. struct device_link *link;
  633. if (!consumer || !supplier || consumer == supplier ||
  634. flags & ~DL_ADD_VALID_FLAGS ||
  635. (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
  636. (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
  637. flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
  638. DL_FLAG_AUTOREMOVE_SUPPLIER)))
  639. return NULL;
  640. if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
  641. if (pm_runtime_get_sync(supplier) < 0) {
  642. pm_runtime_put_noidle(supplier);
  643. return NULL;
  644. }
  645. }
  646. if (!(flags & DL_FLAG_STATELESS))
  647. flags |= DL_FLAG_MANAGED;
  648. if (flags & DL_FLAG_SYNC_STATE_ONLY &&
  649. !device_link_flag_is_sync_state_only(flags))
  650. return NULL;
  651. device_links_write_lock();
  652. device_pm_lock();
  653. /*
  654. * If the supplier has not been fully registered yet or there is a
  655. * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
  656. * the supplier already in the graph, return NULL. If the link is a
  657. * SYNC_STATE_ONLY link, we don't check for reverse dependencies
  658. * because it only affects sync_state() callbacks.
  659. */
  660. if (!device_pm_initialized(supplier)
  661. || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
  662. device_is_dependent(consumer, supplier))) {
  663. link = NULL;
  664. goto out;
  665. }
  666. /*
  667. * SYNC_STATE_ONLY links are useless once a consumer device has probed.
  668. * So, only create it if the consumer hasn't probed yet.
  669. */
  670. if (flags & DL_FLAG_SYNC_STATE_ONLY &&
  671. consumer->links.status != DL_DEV_NO_DRIVER &&
  672. consumer->links.status != DL_DEV_PROBING) {
  673. link = NULL;
  674. goto out;
  675. }
  676. /*
  677. * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
  678. * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
  679. * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
  680. */
  681. if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
  682. flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
  683. list_for_each_entry(link, &supplier->links.consumers, s_node) {
  684. if (link->consumer != consumer)
  685. continue;
  686. if (device_link_test(link, DL_FLAG_INFERRED) &&
  687. !(flags & DL_FLAG_INFERRED))
  688. link->flags &= ~DL_FLAG_INFERRED;
  689. if (flags & DL_FLAG_PM_RUNTIME) {
  690. if (!device_link_test(link, DL_FLAG_PM_RUNTIME)) {
  691. pm_runtime_new_link(consumer);
  692. link->flags |= DL_FLAG_PM_RUNTIME;
  693. }
  694. if (flags & DL_FLAG_RPM_ACTIVE)
  695. refcount_inc(&link->rpm_active);
  696. }
  697. if (flags & DL_FLAG_STATELESS) {
  698. kref_get(&link->kref);
  699. if (device_link_test(link, DL_FLAG_SYNC_STATE_ONLY) &&
  700. !device_link_test(link, DL_FLAG_STATELESS)) {
  701. link->flags |= DL_FLAG_STATELESS;
  702. goto reorder;
  703. } else {
  704. link->flags |= DL_FLAG_STATELESS;
  705. goto out;
  706. }
  707. }
  708. /*
  709. * If the life time of the link following from the new flags is
  710. * longer than indicated by the flags of the existing link,
  711. * update the existing link to stay around longer.
  712. */
  713. if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
  714. if (device_link_test(link, DL_FLAG_AUTOREMOVE_CONSUMER)) {
  715. link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
  716. link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
  717. }
  718. } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
  719. link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
  720. DL_FLAG_AUTOREMOVE_SUPPLIER);
  721. }
  722. if (!device_link_test(link, DL_FLAG_MANAGED)) {
  723. kref_get(&link->kref);
  724. link->flags |= DL_FLAG_MANAGED;
  725. device_link_init_status(link, consumer, supplier);
  726. }
  727. if (device_link_test(link, DL_FLAG_SYNC_STATE_ONLY) &&
  728. !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
  729. link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
  730. goto reorder;
  731. }
  732. goto out;
  733. }
  734. link = kzalloc_obj(*link);
  735. if (!link)
  736. goto out;
  737. refcount_set(&link->rpm_active, 1);
  738. get_device(supplier);
  739. link->supplier = supplier;
  740. INIT_LIST_HEAD(&link->s_node);
  741. get_device(consumer);
  742. link->consumer = consumer;
  743. INIT_LIST_HEAD(&link->c_node);
  744. link->flags = flags;
  745. kref_init(&link->kref);
  746. link->link_dev.class = &devlink_class;
  747. device_set_pm_not_required(&link->link_dev);
  748. dev_set_name(&link->link_dev, "%s:%s--%s:%s",
  749. dev_bus_name(supplier), dev_name(supplier),
  750. dev_bus_name(consumer), dev_name(consumer));
  751. if (device_register(&link->link_dev)) {
  752. put_device(&link->link_dev);
  753. link = NULL;
  754. goto out;
  755. }
  756. if (flags & DL_FLAG_PM_RUNTIME) {
  757. if (flags & DL_FLAG_RPM_ACTIVE)
  758. refcount_inc(&link->rpm_active);
  759. pm_runtime_new_link(consumer);
  760. }
  761. /* Determine the initial link state. */
  762. if (flags & DL_FLAG_STATELESS)
  763. link->status = DL_STATE_NONE;
  764. else
  765. device_link_init_status(link, consumer, supplier);
  766. /*
  767. * Some callers expect the link creation during consumer driver probe to
  768. * resume the supplier even without DL_FLAG_RPM_ACTIVE.
  769. */
  770. if (link->status == DL_STATE_CONSUMER_PROBE &&
  771. flags & DL_FLAG_PM_RUNTIME)
  772. pm_runtime_resume(supplier);
  773. list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
  774. list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
  775. if (flags & DL_FLAG_SYNC_STATE_ONLY) {
  776. dev_dbg(consumer,
  777. "Linked as a sync state only consumer to %s\n",
  778. dev_name(supplier));
  779. goto out;
  780. }
  781. reorder:
  782. /*
  783. * Move the consumer and all of the devices depending on it to the end
  784. * of dpm_list and the devices_kset list.
  785. *
  786. * It is necessary to hold dpm_list locked throughout all that or else
  787. * we may end up suspending with a wrong ordering of it.
  788. */
  789. device_reorder_to_tail(consumer, NULL);
  790. dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
  791. out:
  792. device_pm_unlock();
  793. device_links_write_unlock();
  794. if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
  795. pm_runtime_put(supplier);
  796. return link;
  797. }
  798. EXPORT_SYMBOL_GPL(device_link_add);
  799. static void __device_link_del(struct kref *kref)
  800. {
  801. struct device_link *link = container_of(kref, struct device_link, kref);
  802. dev_dbg(link->consumer, "Dropping the link to %s\n",
  803. dev_name(link->supplier));
  804. pm_runtime_drop_link(link);
  805. device_link_remove_from_lists(link);
  806. device_unregister(&link->link_dev);
  807. }
  808. static void device_link_put_kref(struct device_link *link)
  809. {
  810. if (device_link_test(link, DL_FLAG_STATELESS))
  811. kref_put(&link->kref, __device_link_del);
  812. else if (!device_is_registered(link->consumer))
  813. __device_link_del(&link->kref);
  814. else
  815. WARN(1, "Unable to drop a managed device link reference\n");
  816. }
  817. /**
  818. * device_link_del - Delete a stateless link between two devices.
  819. * @link: Device link to delete.
  820. *
  821. * The caller must ensure proper synchronization of this function with runtime
  822. * PM. If the link was added multiple times, it needs to be deleted as often.
  823. * Care is required for hotplugged devices: Their links are purged on removal
  824. * and calling device_link_del() is then no longer allowed.
  825. */
  826. void device_link_del(struct device_link *link)
  827. {
  828. device_links_write_lock();
  829. device_link_put_kref(link);
  830. device_links_write_unlock();
  831. }
  832. EXPORT_SYMBOL_GPL(device_link_del);
  833. /**
  834. * device_link_remove - Delete a stateless link between two devices.
  835. * @consumer: Consumer end of the link.
  836. * @supplier: Supplier end of the link.
  837. *
  838. * The caller must ensure proper synchronization of this function with runtime
  839. * PM.
  840. */
  841. void device_link_remove(void *consumer, struct device *supplier)
  842. {
  843. struct device_link *link;
  844. if (WARN_ON(consumer == supplier))
  845. return;
  846. device_links_write_lock();
  847. list_for_each_entry(link, &supplier->links.consumers, s_node) {
  848. if (link->consumer == consumer) {
  849. device_link_put_kref(link);
  850. break;
  851. }
  852. }
  853. device_links_write_unlock();
  854. }
  855. EXPORT_SYMBOL_GPL(device_link_remove);
  856. static void device_links_missing_supplier(struct device *dev)
  857. {
  858. struct device_link *link;
  859. list_for_each_entry(link, &dev->links.suppliers, c_node) {
  860. if (link->status != DL_STATE_CONSUMER_PROBE)
  861. continue;
  862. if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
  863. WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
  864. } else {
  865. WARN_ON(!device_link_test(link, DL_FLAG_SYNC_STATE_ONLY));
  866. WRITE_ONCE(link->status, DL_STATE_DORMANT);
  867. }
  868. }
  869. }
  870. static bool dev_is_best_effort(struct device *dev)
  871. {
  872. return (fw_devlink_best_effort && dev->can_match) ||
  873. (dev->fwnode && (dev->fwnode->flags & FWNODE_FLAG_BEST_EFFORT));
  874. }
  875. static struct fwnode_handle *fwnode_links_check_suppliers(
  876. struct fwnode_handle *fwnode)
  877. {
  878. struct fwnode_link *link;
  879. if (!fwnode || fw_devlink_is_permissive())
  880. return NULL;
  881. list_for_each_entry(link, &fwnode->suppliers, c_hook)
  882. if (!(link->flags &
  883. (FWLINK_FLAG_CYCLE | FWLINK_FLAG_IGNORE)))
  884. return link->supplier;
  885. return NULL;
  886. }
  887. /**
  888. * device_links_check_suppliers - Check presence of supplier drivers.
  889. * @dev: Consumer device.
  890. *
  891. * Check links from this device to any suppliers. Walk the list of the device's
  892. * links to suppliers and see if all of them are available. If not, simply
  893. * return -EPROBE_DEFER.
  894. *
  895. * We need to guarantee that the supplier will not go away after the check has
  896. * been positive here. It only can go away in __device_release_driver() and
  897. * that function checks the device's links to consumers. This means we need to
  898. * mark the link as "consumer probe in progress" to make the supplier removal
  899. * wait for us to complete (or bad things may happen).
  900. *
  901. * Links without the DL_FLAG_MANAGED flag set are ignored.
  902. */
  903. int device_links_check_suppliers(struct device *dev)
  904. {
  905. struct device_link *link;
  906. int ret = 0, fwnode_ret = 0;
  907. struct fwnode_handle *sup_fw;
  908. /*
  909. * Device waiting for supplier to become available is not allowed to
  910. * probe.
  911. */
  912. scoped_guard(mutex, &fwnode_link_lock) {
  913. sup_fw = fwnode_links_check_suppliers(dev->fwnode);
  914. if (sup_fw) {
  915. if (dev_is_best_effort(dev))
  916. fwnode_ret = -EAGAIN;
  917. else
  918. return dev_err_probe(dev, -EPROBE_DEFER,
  919. "wait for supplier %pfwf\n", sup_fw);
  920. }
  921. }
  922. device_links_write_lock();
  923. list_for_each_entry(link, &dev->links.suppliers, c_node) {
  924. if (!device_link_test(link, DL_FLAG_MANAGED))
  925. continue;
  926. if (link->status != DL_STATE_AVAILABLE &&
  927. !device_link_test(link, DL_FLAG_SYNC_STATE_ONLY)) {
  928. if (dev_is_best_effort(dev) &&
  929. device_link_test(link, DL_FLAG_INFERRED) &&
  930. !link->supplier->can_match) {
  931. ret = -EAGAIN;
  932. continue;
  933. }
  934. device_links_missing_supplier(dev);
  935. ret = dev_err_probe(dev, -EPROBE_DEFER,
  936. "supplier %s not ready\n", dev_name(link->supplier));
  937. break;
  938. }
  939. WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
  940. }
  941. dev->links.status = DL_DEV_PROBING;
  942. device_links_write_unlock();
  943. return ret ? ret : fwnode_ret;
  944. }
  945. /**
  946. * __device_links_queue_sync_state - Queue a device for sync_state() callback
  947. * @dev: Device to call sync_state() on
  948. * @list: List head to queue the @dev on
  949. *
  950. * Queues a device for a sync_state() callback when the device links write lock
  951. * isn't held. This allows the sync_state() execution flow to use device links
  952. * APIs. The caller must ensure this function is called with
  953. * device_links_write_lock() held.
  954. *
  955. * This function does a get_device() to make sure the device is not freed while
  956. * on this list.
  957. *
  958. * So the caller must also ensure that device_links_flush_sync_list() is called
  959. * as soon as the caller releases device_links_write_lock(). This is necessary
  960. * to make sure the sync_state() is called in a timely fashion and the
  961. * put_device() is called on this device.
  962. */
  963. static void __device_links_queue_sync_state(struct device *dev,
  964. struct list_head *list)
  965. {
  966. struct device_link *link;
  967. if (!dev_has_sync_state(dev))
  968. return;
  969. if (dev->state_synced)
  970. return;
  971. list_for_each_entry(link, &dev->links.consumers, s_node) {
  972. if (!device_link_test(link, DL_FLAG_MANAGED))
  973. continue;
  974. if (link->status != DL_STATE_ACTIVE)
  975. return;
  976. }
  977. /*
  978. * Set the flag here to avoid adding the same device to a list more
  979. * than once. This can happen if new consumers get added to the device
  980. * and probed before the list is flushed.
  981. */
  982. dev->state_synced = true;
  983. if (WARN_ON(!list_empty(&dev->links.defer_sync)))
  984. return;
  985. get_device(dev);
  986. list_add_tail(&dev->links.defer_sync, list);
  987. }
  988. /**
  989. * device_links_flush_sync_list - Call sync_state() on a list of devices
  990. * @list: List of devices to call sync_state() on
  991. * @dont_lock_dev: Device for which lock is already held by the caller
  992. *
  993. * Calls sync_state() on all the devices that have been queued for it. This
  994. * function is used in conjunction with __device_links_queue_sync_state(). The
  995. * @dont_lock_dev parameter is useful when this function is called from a
  996. * context where a device lock is already held.
  997. */
  998. static void device_links_flush_sync_list(struct list_head *list,
  999. struct device *dont_lock_dev)
  1000. {
  1001. struct device *dev, *tmp;
  1002. list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
  1003. list_del_init(&dev->links.defer_sync);
  1004. if (dev != dont_lock_dev)
  1005. device_lock(dev);
  1006. dev_sync_state(dev);
  1007. if (dev != dont_lock_dev)
  1008. device_unlock(dev);
  1009. put_device(dev);
  1010. }
  1011. }
  1012. void device_links_supplier_sync_state_pause(void)
  1013. {
  1014. device_links_write_lock();
  1015. defer_sync_state_count++;
  1016. device_links_write_unlock();
  1017. }
  1018. void device_links_supplier_sync_state_resume(void)
  1019. {
  1020. struct device *dev, *tmp;
  1021. LIST_HEAD(sync_list);
  1022. device_links_write_lock();
  1023. if (!defer_sync_state_count) {
  1024. WARN(true, "Unmatched sync_state pause/resume!");
  1025. goto out;
  1026. }
  1027. defer_sync_state_count--;
  1028. if (defer_sync_state_count)
  1029. goto out;
  1030. list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
  1031. /*
  1032. * Delete from deferred_sync list before queuing it to
  1033. * sync_list because defer_sync is used for both lists.
  1034. */
  1035. list_del_init(&dev->links.defer_sync);
  1036. __device_links_queue_sync_state(dev, &sync_list);
  1037. }
  1038. out:
  1039. device_links_write_unlock();
  1040. device_links_flush_sync_list(&sync_list, NULL);
  1041. }
  1042. static int sync_state_resume_initcall(void)
  1043. {
  1044. device_links_supplier_sync_state_resume();
  1045. return 0;
  1046. }
  1047. late_initcall(sync_state_resume_initcall);
  1048. static void __device_links_supplier_defer_sync(struct device *sup)
  1049. {
  1050. if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
  1051. list_add_tail(&sup->links.defer_sync, &deferred_sync);
  1052. }
  1053. static void device_link_drop_managed(struct device_link *link)
  1054. {
  1055. link->flags &= ~DL_FLAG_MANAGED;
  1056. WRITE_ONCE(link->status, DL_STATE_NONE);
  1057. kref_put(&link->kref, __device_link_del);
  1058. }
  1059. static ssize_t waiting_for_supplier_show(struct device *dev,
  1060. struct device_attribute *attr,
  1061. char *buf)
  1062. {
  1063. bool val;
  1064. device_lock(dev);
  1065. scoped_guard(mutex, &fwnode_link_lock)
  1066. val = !!fwnode_links_check_suppliers(dev->fwnode);
  1067. device_unlock(dev);
  1068. return sysfs_emit(buf, "%u\n", val);
  1069. }
  1070. static DEVICE_ATTR_RO(waiting_for_supplier);
  1071. /**
  1072. * device_links_force_bind - Prepares device to be force bound
  1073. * @dev: Consumer device.
  1074. *
  1075. * device_bind_driver() force binds a device to a driver without calling any
  1076. * driver probe functions. So the consumer really isn't going to wait for any
  1077. * supplier before it's bound to the driver. We still want the device link
  1078. * states to be sensible when this happens.
  1079. *
  1080. * In preparation for device_bind_driver(), this function goes through each
  1081. * supplier device links and checks if the supplier is bound. If it is, then
  1082. * the device link status is set to CONSUMER_PROBE. Otherwise, the device link
  1083. * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored.
  1084. */
  1085. void device_links_force_bind(struct device *dev)
  1086. {
  1087. struct device_link *link, *ln;
  1088. device_links_write_lock();
  1089. list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
  1090. if (!device_link_test(link, DL_FLAG_MANAGED))
  1091. continue;
  1092. if (link->status != DL_STATE_AVAILABLE) {
  1093. device_link_drop_managed(link);
  1094. continue;
  1095. }
  1096. WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
  1097. }
  1098. dev->links.status = DL_DEV_PROBING;
  1099. device_links_write_unlock();
  1100. }
  1101. /**
  1102. * device_links_driver_bound - Update device links after probing its driver.
  1103. * @dev: Device to update the links for.
  1104. *
  1105. * The probe has been successful, so update links from this device to any
  1106. * consumers by changing their status to "available".
  1107. *
  1108. * Also change the status of @dev's links to suppliers to "active".
  1109. *
  1110. * Links without the DL_FLAG_MANAGED flag set are ignored.
  1111. */
  1112. void device_links_driver_bound(struct device *dev)
  1113. {
  1114. struct device_link *link, *ln;
  1115. LIST_HEAD(sync_list);
  1116. /*
  1117. * If a device binds successfully, it's expected to have created all
  1118. * the device links it needs to or make new device links as it needs
  1119. * them. So, fw_devlink no longer needs to create device links to any
  1120. * of the device's suppliers.
  1121. *
  1122. * Also, if a child firmware node of this bound device is not added as a
  1123. * device by now, assume it is never going to be added. Make this bound
  1124. * device the fallback supplier to the dangling consumers of the child
  1125. * firmware node because this bound device is probably implementing the
  1126. * child firmware node functionality and we don't want the dangling
  1127. * consumers to defer probe indefinitely waiting for a device for the
  1128. * child firmware node.
  1129. */
  1130. if (dev->fwnode && dev->fwnode->dev == dev) {
  1131. struct fwnode_handle *child;
  1132. fwnode_links_purge_suppliers(dev->fwnode);
  1133. guard(mutex)(&fwnode_link_lock);
  1134. fwnode_for_each_available_child_node(dev->fwnode, child)
  1135. __fw_devlink_pickup_dangling_consumers(child,
  1136. dev->fwnode);
  1137. __fw_devlink_link_to_consumers(dev);
  1138. }
  1139. device_remove_file(dev, &dev_attr_waiting_for_supplier);
  1140. device_links_write_lock();
  1141. list_for_each_entry(link, &dev->links.consumers, s_node) {
  1142. if (!device_link_test(link, DL_FLAG_MANAGED))
  1143. continue;
  1144. /*
  1145. * Links created during consumer probe may be in the "consumer
  1146. * probe" state to start with if the supplier is still probing
  1147. * when they are created and they may become "active" if the
  1148. * consumer probe returns first. Skip them here.
  1149. */
  1150. if (link->status == DL_STATE_CONSUMER_PROBE ||
  1151. link->status == DL_STATE_ACTIVE)
  1152. continue;
  1153. WARN_ON(link->status != DL_STATE_DORMANT);
  1154. WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
  1155. if (device_link_test(link, DL_FLAG_AUTOPROBE_CONSUMER))
  1156. driver_deferred_probe_add(link->consumer);
  1157. }
  1158. if (defer_sync_state_count)
  1159. __device_links_supplier_defer_sync(dev);
  1160. else
  1161. __device_links_queue_sync_state(dev, &sync_list);
  1162. list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
  1163. struct device *supplier;
  1164. if (!device_link_test(link, DL_FLAG_MANAGED))
  1165. continue;
  1166. supplier = link->supplier;
  1167. if (device_link_test(link, DL_FLAG_SYNC_STATE_ONLY)) {
  1168. /*
  1169. * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
  1170. * other DL_MANAGED_LINK_FLAGS have been set. So, it's
  1171. * save to drop the managed link completely.
  1172. */
  1173. device_link_drop_managed(link);
  1174. } else if (dev_is_best_effort(dev) &&
  1175. device_link_test(link, DL_FLAG_INFERRED) &&
  1176. link->status != DL_STATE_CONSUMER_PROBE &&
  1177. !link->supplier->can_match) {
  1178. /*
  1179. * When dev_is_best_effort() is true, we ignore device
  1180. * links to suppliers that don't have a driver. If the
  1181. * consumer device still managed to probe, there's no
  1182. * point in maintaining a device link in a weird state
  1183. * (consumer probed before supplier). So delete it.
  1184. */
  1185. device_link_drop_managed(link);
  1186. } else {
  1187. WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
  1188. WRITE_ONCE(link->status, DL_STATE_ACTIVE);
  1189. }
  1190. /*
  1191. * This needs to be done even for the deleted
  1192. * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
  1193. * device link that was preventing the supplier from getting a
  1194. * sync_state() call.
  1195. */
  1196. if (defer_sync_state_count)
  1197. __device_links_supplier_defer_sync(supplier);
  1198. else
  1199. __device_links_queue_sync_state(supplier, &sync_list);
  1200. }
  1201. dev->links.status = DL_DEV_DRIVER_BOUND;
  1202. device_links_write_unlock();
  1203. device_links_flush_sync_list(&sync_list, dev);
  1204. }
  1205. /**
  1206. * __device_links_no_driver - Update links of a device without a driver.
  1207. * @dev: Device without a drvier.
  1208. *
  1209. * Delete all non-persistent links from this device to any suppliers.
  1210. *
  1211. * Persistent links stay around, but their status is changed to "available",
  1212. * unless they already are in the "supplier unbind in progress" state in which
  1213. * case they need not be updated.
  1214. *
  1215. * Links without the DL_FLAG_MANAGED flag set are ignored.
  1216. */
  1217. static void __device_links_no_driver(struct device *dev)
  1218. {
  1219. struct device_link *link, *ln;
  1220. list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
  1221. if (!device_link_test(link, DL_FLAG_MANAGED))
  1222. continue;
  1223. if (device_link_test(link, DL_FLAG_AUTOREMOVE_CONSUMER)) {
  1224. device_link_drop_managed(link);
  1225. continue;
  1226. }
  1227. if (link->status != DL_STATE_CONSUMER_PROBE &&
  1228. link->status != DL_STATE_ACTIVE)
  1229. continue;
  1230. if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
  1231. WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
  1232. } else {
  1233. WARN_ON(!device_link_test(link, DL_FLAG_SYNC_STATE_ONLY));
  1234. WRITE_ONCE(link->status, DL_STATE_DORMANT);
  1235. }
  1236. }
  1237. dev->links.status = DL_DEV_NO_DRIVER;
  1238. }
  1239. /**
  1240. * device_links_no_driver - Update links after failing driver probe.
  1241. * @dev: Device whose driver has just failed to probe.
  1242. *
  1243. * Clean up leftover links to consumers for @dev and invoke
  1244. * %__device_links_no_driver() to update links to suppliers for it as
  1245. * appropriate.
  1246. *
  1247. * Links without the DL_FLAG_MANAGED flag set are ignored.
  1248. */
  1249. void device_links_no_driver(struct device *dev)
  1250. {
  1251. struct device_link *link;
  1252. device_links_write_lock();
  1253. list_for_each_entry(link, &dev->links.consumers, s_node) {
  1254. if (!device_link_test(link, DL_FLAG_MANAGED))
  1255. continue;
  1256. /*
  1257. * The probe has failed, so if the status of the link is
  1258. * "consumer probe" or "active", it must have been added by
  1259. * a probing consumer while this device was still probing.
  1260. * Change its state to "dormant", as it represents a valid
  1261. * relationship, but it is not functionally meaningful.
  1262. */
  1263. if (link->status == DL_STATE_CONSUMER_PROBE ||
  1264. link->status == DL_STATE_ACTIVE)
  1265. WRITE_ONCE(link->status, DL_STATE_DORMANT);
  1266. }
  1267. __device_links_no_driver(dev);
  1268. device_links_write_unlock();
  1269. }
  1270. /**
  1271. * device_links_driver_cleanup - Update links after driver removal.
  1272. * @dev: Device whose driver has just gone away.
  1273. *
  1274. * Update links to consumers for @dev by changing their status to "dormant" and
  1275. * invoke %__device_links_no_driver() to update links to suppliers for it as
  1276. * appropriate.
  1277. *
  1278. * Links without the DL_FLAG_MANAGED flag set are ignored.
  1279. */
  1280. void device_links_driver_cleanup(struct device *dev)
  1281. {
  1282. struct device_link *link, *ln;
  1283. device_links_write_lock();
  1284. list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
  1285. if (!device_link_test(link, DL_FLAG_MANAGED))
  1286. continue;
  1287. WARN_ON(device_link_test(link, DL_FLAG_AUTOREMOVE_CONSUMER));
  1288. WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
  1289. /*
  1290. * autoremove the links between this @dev and its consumer
  1291. * devices that are not active, i.e. where the link state
  1292. * has moved to DL_STATE_SUPPLIER_UNBIND.
  1293. */
  1294. if (link->status == DL_STATE_SUPPLIER_UNBIND &&
  1295. device_link_test(link, DL_FLAG_AUTOREMOVE_SUPPLIER))
  1296. device_link_drop_managed(link);
  1297. WRITE_ONCE(link->status, DL_STATE_DORMANT);
  1298. }
  1299. list_del_init(&dev->links.defer_sync);
  1300. __device_links_no_driver(dev);
  1301. device_links_write_unlock();
  1302. }
  1303. /**
  1304. * device_links_busy - Check if there are any busy links to consumers.
  1305. * @dev: Device to check.
  1306. *
  1307. * Check each consumer of the device and return 'true' if its link's status
  1308. * is one of "consumer probe" or "active" (meaning that the given consumer is
  1309. * probing right now or its driver is present). Otherwise, change the link
  1310. * state to "supplier unbind" to prevent the consumer from being probed
  1311. * successfully going forward.
  1312. *
  1313. * Return 'false' if there are no probing or active consumers.
  1314. *
  1315. * Links without the DL_FLAG_MANAGED flag set are ignored.
  1316. */
  1317. bool device_links_busy(struct device *dev)
  1318. {
  1319. struct device_link *link;
  1320. bool ret = false;
  1321. device_links_write_lock();
  1322. list_for_each_entry(link, &dev->links.consumers, s_node) {
  1323. if (!device_link_test(link, DL_FLAG_MANAGED))
  1324. continue;
  1325. if (link->status == DL_STATE_CONSUMER_PROBE
  1326. || link->status == DL_STATE_ACTIVE) {
  1327. ret = true;
  1328. break;
  1329. }
  1330. WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
  1331. }
  1332. dev->links.status = DL_DEV_UNBINDING;
  1333. device_links_write_unlock();
  1334. return ret;
  1335. }
  1336. /**
  1337. * device_links_unbind_consumers - Force unbind consumers of the given device.
  1338. * @dev: Device to unbind the consumers of.
  1339. *
  1340. * Walk the list of links to consumers for @dev and if any of them is in the
  1341. * "consumer probe" state, wait for all device probes in progress to complete
  1342. * and start over.
  1343. *
  1344. * If that's not the case, change the status of the link to "supplier unbind"
  1345. * and check if the link was in the "active" state. If so, force the consumer
  1346. * driver to unbind and start over (the consumer will not re-probe as we have
  1347. * changed the state of the link already).
  1348. *
  1349. * Links without the DL_FLAG_MANAGED flag set are ignored.
  1350. */
  1351. void device_links_unbind_consumers(struct device *dev)
  1352. {
  1353. struct device_link *link;
  1354. start:
  1355. device_links_write_lock();
  1356. list_for_each_entry(link, &dev->links.consumers, s_node) {
  1357. enum device_link_state status;
  1358. if (!device_link_test(link, DL_FLAG_MANAGED) ||
  1359. device_link_test(link, DL_FLAG_SYNC_STATE_ONLY))
  1360. continue;
  1361. status = link->status;
  1362. if (status == DL_STATE_CONSUMER_PROBE) {
  1363. device_links_write_unlock();
  1364. wait_for_device_probe();
  1365. goto start;
  1366. }
  1367. WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
  1368. if (status == DL_STATE_ACTIVE) {
  1369. struct device *consumer = link->consumer;
  1370. get_device(consumer);
  1371. device_links_write_unlock();
  1372. device_release_driver_internal(consumer, NULL,
  1373. consumer->parent);
  1374. put_device(consumer);
  1375. goto start;
  1376. }
  1377. }
  1378. device_links_write_unlock();
  1379. }
  1380. /**
  1381. * device_links_purge - Delete existing links to other devices.
  1382. * @dev: Target device.
  1383. */
  1384. static void device_links_purge(struct device *dev)
  1385. {
  1386. struct device_link *link, *ln;
  1387. if (dev->class == &devlink_class)
  1388. return;
  1389. /*
  1390. * Delete all of the remaining links from this device to any other
  1391. * devices (either consumers or suppliers).
  1392. */
  1393. device_links_write_lock();
  1394. list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
  1395. WARN_ON(link->status == DL_STATE_ACTIVE);
  1396. __device_link_del(&link->kref);
  1397. }
  1398. list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
  1399. WARN_ON(link->status != DL_STATE_DORMANT &&
  1400. link->status != DL_STATE_NONE);
  1401. __device_link_del(&link->kref);
  1402. }
  1403. device_links_write_unlock();
  1404. }
  1405. #define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \
  1406. DL_FLAG_SYNC_STATE_ONLY)
  1407. #define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \
  1408. DL_FLAG_AUTOPROBE_CONSUMER)
  1409. #define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \
  1410. DL_FLAG_PM_RUNTIME)
  1411. static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
  1412. static int __init fw_devlink_setup(char *arg)
  1413. {
  1414. if (!arg)
  1415. return -EINVAL;
  1416. if (strcmp(arg, "off") == 0) {
  1417. fw_devlink_flags = 0;
  1418. } else if (strcmp(arg, "permissive") == 0) {
  1419. fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
  1420. } else if (strcmp(arg, "on") == 0) {
  1421. fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
  1422. } else if (strcmp(arg, "rpm") == 0) {
  1423. fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
  1424. }
  1425. return 0;
  1426. }
  1427. early_param("fw_devlink", fw_devlink_setup);
  1428. static bool fw_devlink_strict;
  1429. static int __init fw_devlink_strict_setup(char *arg)
  1430. {
  1431. return kstrtobool(arg, &fw_devlink_strict);
  1432. }
  1433. early_param("fw_devlink.strict", fw_devlink_strict_setup);
  1434. #define FW_DEVLINK_SYNC_STATE_STRICT 0
  1435. #define FW_DEVLINK_SYNC_STATE_TIMEOUT 1
  1436. #ifndef CONFIG_FW_DEVLINK_SYNC_STATE_TIMEOUT
  1437. static int fw_devlink_sync_state;
  1438. #else
  1439. static int fw_devlink_sync_state = FW_DEVLINK_SYNC_STATE_TIMEOUT;
  1440. #endif
  1441. static int __init fw_devlink_sync_state_setup(char *arg)
  1442. {
  1443. if (!arg)
  1444. return -EINVAL;
  1445. if (strcmp(arg, "strict") == 0) {
  1446. fw_devlink_sync_state = FW_DEVLINK_SYNC_STATE_STRICT;
  1447. return 0;
  1448. } else if (strcmp(arg, "timeout") == 0) {
  1449. fw_devlink_sync_state = FW_DEVLINK_SYNC_STATE_TIMEOUT;
  1450. return 0;
  1451. }
  1452. return -EINVAL;
  1453. }
  1454. early_param("fw_devlink.sync_state", fw_devlink_sync_state_setup);
  1455. static inline u32 fw_devlink_get_flags(u8 fwlink_flags)
  1456. {
  1457. if (fwlink_flags & FWLINK_FLAG_CYCLE)
  1458. return FW_DEVLINK_FLAGS_PERMISSIVE | DL_FLAG_CYCLE;
  1459. return fw_devlink_flags;
  1460. }
  1461. static bool fw_devlink_is_permissive(void)
  1462. {
  1463. return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE;
  1464. }
  1465. bool fw_devlink_is_strict(void)
  1466. {
  1467. return fw_devlink_strict && !fw_devlink_is_permissive();
  1468. }
  1469. static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
  1470. {
  1471. if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
  1472. return;
  1473. fwnode_call_int_op(fwnode, add_links);
  1474. fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
  1475. }
  1476. static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
  1477. {
  1478. struct fwnode_handle *child = NULL;
  1479. fw_devlink_parse_fwnode(fwnode);
  1480. while ((child = fwnode_get_next_available_child_node(fwnode, child)))
  1481. fw_devlink_parse_fwtree(child);
  1482. }
  1483. static void fw_devlink_relax_link(struct device_link *link)
  1484. {
  1485. if (!device_link_test(link, DL_FLAG_INFERRED))
  1486. return;
  1487. if (device_link_flag_is_sync_state_only(link->flags))
  1488. return;
  1489. pm_runtime_drop_link(link);
  1490. link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE;
  1491. dev_dbg(link->consumer, "Relaxing link with %s\n",
  1492. dev_name(link->supplier));
  1493. }
  1494. static int fw_devlink_no_driver(struct device *dev, void *data)
  1495. {
  1496. struct device_link *link = to_devlink(dev);
  1497. if (!link->supplier->can_match)
  1498. fw_devlink_relax_link(link);
  1499. return 0;
  1500. }
  1501. void fw_devlink_drivers_done(void)
  1502. {
  1503. fw_devlink_drv_reg_done = true;
  1504. device_links_write_lock();
  1505. class_for_each_device(&devlink_class, NULL, NULL,
  1506. fw_devlink_no_driver);
  1507. device_links_write_unlock();
  1508. }
  1509. static int fw_devlink_dev_sync_state(struct device *dev, void *data)
  1510. {
  1511. struct device_link *link = to_devlink(dev);
  1512. struct device *sup = link->supplier;
  1513. if (!device_link_test(link, DL_FLAG_MANAGED) ||
  1514. link->status == DL_STATE_ACTIVE || sup->state_synced ||
  1515. !dev_has_sync_state(sup))
  1516. return 0;
  1517. if (fw_devlink_sync_state == FW_DEVLINK_SYNC_STATE_STRICT) {
  1518. dev_info(sup, "sync_state() pending due to %s\n",
  1519. dev_name(link->consumer));
  1520. return 0;
  1521. }
  1522. if (!list_empty(&sup->links.defer_sync))
  1523. return 0;
  1524. dev_warn(sup, "Timed out. Forcing sync_state()\n");
  1525. sup->state_synced = true;
  1526. get_device(sup);
  1527. list_add_tail(&sup->links.defer_sync, data);
  1528. return 0;
  1529. }
  1530. void fw_devlink_probing_done(void)
  1531. {
  1532. LIST_HEAD(sync_list);
  1533. device_links_write_lock();
  1534. class_for_each_device(&devlink_class, NULL, &sync_list,
  1535. fw_devlink_dev_sync_state);
  1536. device_links_write_unlock();
  1537. device_links_flush_sync_list(&sync_list, NULL);
  1538. }
  1539. /**
  1540. * wait_for_init_devices_probe - Try to probe any device needed for init
  1541. *
  1542. * Some devices might need to be probed and bound successfully before the kernel
  1543. * boot sequence can finish and move on to init/userspace. For example, a
  1544. * network interface might need to be bound to be able to mount a NFS rootfs.
  1545. *
  1546. * With fw_devlink=on by default, some of these devices might be blocked from
  1547. * probing because they are waiting on a optional supplier that doesn't have a
  1548. * driver. While fw_devlink will eventually identify such devices and unblock
  1549. * the probing automatically, it might be too late by the time it unblocks the
  1550. * probing of devices. For example, the IP4 autoconfig might timeout before
  1551. * fw_devlink unblocks probing of the network interface.
  1552. *
  1553. * This function is available to temporarily try and probe all devices that have
  1554. * a driver even if some of their suppliers haven't been added or don't have
  1555. * drivers.
  1556. *
  1557. * The drivers can then decide which of the suppliers are optional vs mandatory
  1558. * and probe the device if possible. By the time this function returns, all such
  1559. * "best effort" probes are guaranteed to be completed. If a device successfully
  1560. * probes in this mode, we delete all fw_devlink discovered dependencies of that
  1561. * device where the supplier hasn't yet probed successfully because they have to
  1562. * be optional dependencies.
  1563. *
  1564. * Any devices that didn't successfully probe go back to being treated as if
  1565. * this function was never called.
  1566. *
  1567. * This also means that some devices that aren't needed for init and could have
  1568. * waited for their optional supplier to probe (when the supplier's module is
  1569. * loaded later on) would end up probing prematurely with limited functionality.
  1570. * So call this function only when boot would fail without it.
  1571. */
  1572. void __init wait_for_init_devices_probe(void)
  1573. {
  1574. if (!fw_devlink_flags || fw_devlink_is_permissive())
  1575. return;
  1576. /*
  1577. * Wait for all ongoing probes to finish so that the "best effort" is
  1578. * only applied to devices that can't probe otherwise.
  1579. */
  1580. wait_for_device_probe();
  1581. pr_info("Trying to probe devices needed for running init ...\n");
  1582. fw_devlink_best_effort = true;
  1583. driver_deferred_probe_trigger();
  1584. /*
  1585. * Wait for all "best effort" probes to finish before going back to
  1586. * normal enforcement.
  1587. */
  1588. wait_for_device_probe();
  1589. fw_devlink_best_effort = false;
  1590. }
  1591. static void fw_devlink_unblock_consumers(struct device *dev)
  1592. {
  1593. struct device_link *link;
  1594. if (!fw_devlink_flags || fw_devlink_is_permissive())
  1595. return;
  1596. device_links_write_lock();
  1597. list_for_each_entry(link, &dev->links.consumers, s_node)
  1598. fw_devlink_relax_link(link);
  1599. device_links_write_unlock();
  1600. }
  1601. static bool fwnode_init_without_drv(struct fwnode_handle *fwnode)
  1602. {
  1603. struct device *dev;
  1604. bool ret;
  1605. if (!(fwnode->flags & FWNODE_FLAG_INITIALIZED))
  1606. return false;
  1607. dev = get_dev_from_fwnode(fwnode);
  1608. ret = !dev || dev->links.status == DL_DEV_NO_DRIVER;
  1609. put_device(dev);
  1610. return ret;
  1611. }
  1612. static bool fwnode_ancestor_init_without_drv(struct fwnode_handle *fwnode)
  1613. {
  1614. struct fwnode_handle *parent;
  1615. fwnode_for_each_parent_node(fwnode, parent) {
  1616. if (fwnode_init_without_drv(parent)) {
  1617. fwnode_handle_put(parent);
  1618. return true;
  1619. }
  1620. }
  1621. return false;
  1622. }
  1623. /**
  1624. * fwnode_is_ancestor_of - Test if @ancestor is ancestor of @child
  1625. * @ancestor: Firmware which is tested for being an ancestor
  1626. * @child: Firmware which is tested for being the child
  1627. *
  1628. * A node is considered an ancestor of itself too.
  1629. *
  1630. * Return: true if @ancestor is an ancestor of @child. Otherwise, returns false.
  1631. */
  1632. static bool fwnode_is_ancestor_of(const struct fwnode_handle *ancestor,
  1633. const struct fwnode_handle *child)
  1634. {
  1635. struct fwnode_handle *parent;
  1636. if (IS_ERR_OR_NULL(ancestor))
  1637. return false;
  1638. if (child == ancestor)
  1639. return true;
  1640. fwnode_for_each_parent_node(child, parent) {
  1641. if (parent == ancestor) {
  1642. fwnode_handle_put(parent);
  1643. return true;
  1644. }
  1645. }
  1646. return false;
  1647. }
  1648. /**
  1649. * fwnode_get_next_parent_dev - Find device of closest ancestor fwnode
  1650. * @fwnode: firmware node
  1651. *
  1652. * Given a firmware node (@fwnode), this function finds its closest ancestor
  1653. * firmware node that has a corresponding struct device and returns that struct
  1654. * device.
  1655. *
  1656. * The caller is responsible for calling put_device() on the returned device
  1657. * pointer.
  1658. *
  1659. * Return: a pointer to the device of the @fwnode's closest ancestor.
  1660. */
  1661. static struct device *fwnode_get_next_parent_dev(const struct fwnode_handle *fwnode)
  1662. {
  1663. struct fwnode_handle *parent;
  1664. struct device *dev;
  1665. fwnode_for_each_parent_node(fwnode, parent) {
  1666. dev = get_dev_from_fwnode(parent);
  1667. if (dev) {
  1668. fwnode_handle_put(parent);
  1669. return dev;
  1670. }
  1671. }
  1672. return NULL;
  1673. }
  1674. /**
  1675. * __fw_devlink_relax_cycles - Relax and mark dependency cycles.
  1676. * @con_handle: Potential consumer device fwnode.
  1677. * @sup_handle: Potential supplier's fwnode.
  1678. *
  1679. * Needs to be called with fwnode_lock and device link lock held.
  1680. *
  1681. * Check if @sup_handle or any of its ancestors or suppliers direct/indirectly
  1682. * depend on @con. This function can detect multiple cyles between @sup_handle
  1683. * and @con. When such dependency cycles are found, convert all device links
  1684. * created solely by fw_devlink into SYNC_STATE_ONLY device links. Also, mark
  1685. * all fwnode links in the cycle with FWLINK_FLAG_CYCLE so that when they are
  1686. * converted into a device link in the future, they are created as
  1687. * SYNC_STATE_ONLY device links. This is the equivalent of doing
  1688. * fw_devlink=permissive just between the devices in the cycle. We need to do
  1689. * this because, at this point, fw_devlink can't tell which of these
  1690. * dependencies is not a real dependency.
  1691. *
  1692. * Return true if one or more cycles were found. Otherwise, return false.
  1693. */
  1694. static bool __fw_devlink_relax_cycles(struct fwnode_handle *con_handle,
  1695. struct fwnode_handle *sup_handle)
  1696. {
  1697. struct device *sup_dev = NULL, *par_dev = NULL, *con_dev = NULL;
  1698. struct fwnode_link *link;
  1699. struct device_link *dev_link;
  1700. bool ret = false;
  1701. if (!sup_handle)
  1702. return false;
  1703. /*
  1704. * We aren't trying to find all cycles. Just a cycle between con and
  1705. * sup_handle.
  1706. */
  1707. if (sup_handle->flags & FWNODE_FLAG_VISITED)
  1708. return false;
  1709. sup_handle->flags |= FWNODE_FLAG_VISITED;
  1710. /* Termination condition. */
  1711. if (sup_handle == con_handle) {
  1712. pr_debug("----- cycle: start -----\n");
  1713. ret = true;
  1714. goto out;
  1715. }
  1716. sup_dev = get_dev_from_fwnode(sup_handle);
  1717. con_dev = get_dev_from_fwnode(con_handle);
  1718. /*
  1719. * If sup_dev is bound to a driver and @con hasn't started binding to a
  1720. * driver, sup_dev can't be a consumer of @con. So, no need to check
  1721. * further.
  1722. */
  1723. if (sup_dev && sup_dev->links.status == DL_DEV_DRIVER_BOUND &&
  1724. con_dev && con_dev->links.status == DL_DEV_NO_DRIVER) {
  1725. ret = false;
  1726. goto out;
  1727. }
  1728. list_for_each_entry(link, &sup_handle->suppliers, c_hook) {
  1729. if (link->flags & FWLINK_FLAG_IGNORE)
  1730. continue;
  1731. if (__fw_devlink_relax_cycles(con_handle, link->supplier)) {
  1732. __fwnode_link_cycle(link);
  1733. ret = true;
  1734. }
  1735. }
  1736. /*
  1737. * Give priority to device parent over fwnode parent to account for any
  1738. * quirks in how fwnodes are converted to devices.
  1739. */
  1740. if (sup_dev)
  1741. par_dev = get_device(sup_dev->parent);
  1742. else
  1743. par_dev = fwnode_get_next_parent_dev(sup_handle);
  1744. if (par_dev && __fw_devlink_relax_cycles(con_handle, par_dev->fwnode)) {
  1745. pr_debug("%pfwf: cycle: child of %pfwf\n", sup_handle,
  1746. par_dev->fwnode);
  1747. ret = true;
  1748. }
  1749. if (!sup_dev)
  1750. goto out;
  1751. list_for_each_entry(dev_link, &sup_dev->links.suppliers, c_node) {
  1752. /*
  1753. * Ignore a SYNC_STATE_ONLY flag only if it wasn't marked as
  1754. * such due to a cycle.
  1755. */
  1756. if (device_link_flag_is_sync_state_only(dev_link->flags) &&
  1757. !device_link_test(dev_link, DL_FLAG_CYCLE))
  1758. continue;
  1759. if (__fw_devlink_relax_cycles(con_handle,
  1760. dev_link->supplier->fwnode)) {
  1761. pr_debug("%pfwf: cycle: depends on %pfwf\n", sup_handle,
  1762. dev_link->supplier->fwnode);
  1763. fw_devlink_relax_link(dev_link);
  1764. dev_link->flags |= DL_FLAG_CYCLE;
  1765. ret = true;
  1766. }
  1767. }
  1768. out:
  1769. sup_handle->flags &= ~FWNODE_FLAG_VISITED;
  1770. put_device(sup_dev);
  1771. put_device(con_dev);
  1772. put_device(par_dev);
  1773. return ret;
  1774. }
  1775. /**
  1776. * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
  1777. * @con: consumer device for the device link
  1778. * @sup_handle: fwnode handle of supplier
  1779. * @link: fwnode link that's being converted to a device link
  1780. *
  1781. * This function will try to create a device link between the consumer device
  1782. * @con and the supplier device represented by @sup_handle.
  1783. *
  1784. * The supplier has to be provided as a fwnode because incorrect cycles in
  1785. * fwnode links can sometimes cause the supplier device to never be created.
  1786. * This function detects such cases and returns an error if it cannot create a
  1787. * device link from the consumer to a missing supplier.
  1788. *
  1789. * Returns,
  1790. * 0 on successfully creating a device link
  1791. * -EINVAL if the device link cannot be created as expected
  1792. * -EAGAIN if the device link cannot be created right now, but it may be
  1793. * possible to do that in the future
  1794. */
  1795. static int fw_devlink_create_devlink(struct device *con,
  1796. struct fwnode_handle *sup_handle,
  1797. struct fwnode_link *link)
  1798. {
  1799. struct device *sup_dev;
  1800. int ret = 0;
  1801. u32 flags;
  1802. if (link->flags & FWLINK_FLAG_IGNORE)
  1803. return 0;
  1804. /*
  1805. * In some cases, a device P might also be a supplier to its child node
  1806. * C. However, this would defer the probe of C until the probe of P
  1807. * completes successfully. This is perfectly fine in the device driver
  1808. * model. device_add() doesn't guarantee probe completion of the device
  1809. * by the time it returns.
  1810. *
  1811. * However, there are a few drivers that assume C will finish probing
  1812. * as soon as it's added and before P finishes probing. So, we provide
  1813. * a flag to let fw_devlink know not to delay the probe of C until the
  1814. * probe of P completes successfully.
  1815. *
  1816. * When such a flag is set, we can't create device links where P is the
  1817. * supplier of C as that would delay the probe of C.
  1818. */
  1819. if (sup_handle->flags & FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD &&
  1820. fwnode_is_ancestor_of(sup_handle, con->fwnode))
  1821. return -EINVAL;
  1822. /*
  1823. * Don't try to optimize by not calling the cycle detection logic under
  1824. * certain conditions. There's always some corner case that won't get
  1825. * detected.
  1826. */
  1827. device_links_write_lock();
  1828. if (__fw_devlink_relax_cycles(link->consumer, sup_handle)) {
  1829. __fwnode_link_cycle(link);
  1830. pr_debug("----- cycle: end -----\n");
  1831. pr_info("%pfwf: Fixed dependency cycle(s) with %pfwf\n",
  1832. link->consumer, sup_handle);
  1833. }
  1834. device_links_write_unlock();
  1835. if (con->fwnode == link->consumer)
  1836. flags = fw_devlink_get_flags(link->flags);
  1837. else
  1838. flags = FW_DEVLINK_FLAGS_PERMISSIVE;
  1839. if (sup_handle->flags & FWNODE_FLAG_NOT_DEVICE)
  1840. sup_dev = fwnode_get_next_parent_dev(sup_handle);
  1841. else
  1842. sup_dev = get_dev_from_fwnode(sup_handle);
  1843. if (sup_dev) {
  1844. /*
  1845. * If it's one of those drivers that don't actually bind to
  1846. * their device using driver core, then don't wait on this
  1847. * supplier device indefinitely.
  1848. */
  1849. if (sup_dev->links.status == DL_DEV_NO_DRIVER &&
  1850. sup_handle->flags & FWNODE_FLAG_INITIALIZED) {
  1851. dev_dbg(con,
  1852. "Not linking %pfwf - dev might never probe\n",
  1853. sup_handle);
  1854. ret = -EINVAL;
  1855. goto out;
  1856. }
  1857. if (con != sup_dev && !device_link_add(con, sup_dev, flags)) {
  1858. dev_err(con, "Failed to create device link (0x%x) with supplier %s for %pfwf\n",
  1859. flags, dev_name(sup_dev), link->consumer);
  1860. ret = -EINVAL;
  1861. }
  1862. goto out;
  1863. }
  1864. /*
  1865. * Supplier or supplier's ancestor already initialized without a struct
  1866. * device or being probed by a driver.
  1867. */
  1868. if (fwnode_init_without_drv(sup_handle) ||
  1869. fwnode_ancestor_init_without_drv(sup_handle)) {
  1870. dev_dbg(con, "Not linking %pfwf - might never become dev\n",
  1871. sup_handle);
  1872. return -EINVAL;
  1873. }
  1874. ret = -EAGAIN;
  1875. out:
  1876. put_device(sup_dev);
  1877. return ret;
  1878. }
  1879. /**
  1880. * __fw_devlink_link_to_consumers - Create device links to consumers of a device
  1881. * @dev: Device that needs to be linked to its consumers
  1882. *
  1883. * This function looks at all the consumer fwnodes of @dev and creates device
  1884. * links between the consumer device and @dev (supplier).
  1885. *
  1886. * If the consumer device has not been added yet, then this function creates a
  1887. * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
  1888. * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
  1889. * sync_state() callback before the real consumer device gets to be added and
  1890. * then probed.
  1891. *
  1892. * Once device links are created from the real consumer to @dev (supplier), the
  1893. * fwnode links are deleted.
  1894. */
  1895. static void __fw_devlink_link_to_consumers(struct device *dev)
  1896. {
  1897. struct fwnode_handle *fwnode = dev->fwnode;
  1898. struct fwnode_link *link, *tmp;
  1899. list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
  1900. struct device *con_dev;
  1901. bool own_link = true;
  1902. int ret;
  1903. con_dev = get_dev_from_fwnode(link->consumer);
  1904. /*
  1905. * If consumer device is not available yet, make a "proxy"
  1906. * SYNC_STATE_ONLY link from the consumer's parent device to
  1907. * the supplier device. This is necessary to make sure the
  1908. * supplier doesn't get a sync_state() callback before the real
  1909. * consumer can create a device link to the supplier.
  1910. *
  1911. * This proxy link step is needed to handle the case where the
  1912. * consumer's parent device is added before the supplier.
  1913. */
  1914. if (!con_dev) {
  1915. con_dev = fwnode_get_next_parent_dev(link->consumer);
  1916. /*
  1917. * However, if the consumer's parent device is also the
  1918. * parent of the supplier, don't create a
  1919. * consumer-supplier link from the parent to its child
  1920. * device. Such a dependency is impossible.
  1921. */
  1922. if (con_dev &&
  1923. fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
  1924. put_device(con_dev);
  1925. con_dev = NULL;
  1926. } else {
  1927. own_link = false;
  1928. }
  1929. }
  1930. if (!con_dev)
  1931. continue;
  1932. ret = fw_devlink_create_devlink(con_dev, fwnode, link);
  1933. put_device(con_dev);
  1934. if (!own_link || ret == -EAGAIN)
  1935. continue;
  1936. __fwnode_link_del(link);
  1937. }
  1938. }
  1939. /**
  1940. * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
  1941. * @dev: The consumer device that needs to be linked to its suppliers
  1942. * @fwnode: Root of the fwnode tree that is used to create device links
  1943. *
  1944. * This function looks at all the supplier fwnodes of fwnode tree rooted at
  1945. * @fwnode and creates device links between @dev (consumer) and all the
  1946. * supplier devices of the entire fwnode tree at @fwnode.
  1947. *
  1948. * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
  1949. * and the real suppliers of @dev. Once these device links are created, the
  1950. * fwnode links are deleted.
  1951. *
  1952. * In addition, it also looks at all the suppliers of the entire fwnode tree
  1953. * because some of the child devices of @dev that have not been added yet
  1954. * (because @dev hasn't probed) might already have their suppliers added to
  1955. * driver core. So, this function creates SYNC_STATE_ONLY device links between
  1956. * @dev (consumer) and these suppliers to make sure they don't execute their
  1957. * sync_state() callbacks before these child devices have a chance to create
  1958. * their device links. The fwnode links that correspond to the child devices
  1959. * aren't delete because they are needed later to create the device links
  1960. * between the real consumer and supplier devices.
  1961. */
  1962. static void __fw_devlink_link_to_suppliers(struct device *dev,
  1963. struct fwnode_handle *fwnode)
  1964. {
  1965. bool own_link = (dev->fwnode == fwnode);
  1966. struct fwnode_link *link, *tmp;
  1967. struct fwnode_handle *child = NULL;
  1968. list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
  1969. int ret;
  1970. struct fwnode_handle *sup = link->supplier;
  1971. ret = fw_devlink_create_devlink(dev, sup, link);
  1972. if (!own_link || ret == -EAGAIN)
  1973. continue;
  1974. __fwnode_link_del(link);
  1975. }
  1976. /*
  1977. * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
  1978. * all the descendants. This proxy link step is needed to handle the
  1979. * case where the supplier is added before the consumer's parent device
  1980. * (@dev).
  1981. */
  1982. while ((child = fwnode_get_next_available_child_node(fwnode, child)))
  1983. __fw_devlink_link_to_suppliers(dev, child);
  1984. }
  1985. static void fw_devlink_link_device(struct device *dev)
  1986. {
  1987. struct fwnode_handle *fwnode = dev->fwnode;
  1988. if (!fw_devlink_flags)
  1989. return;
  1990. fw_devlink_parse_fwtree(fwnode);
  1991. guard(mutex)(&fwnode_link_lock);
  1992. __fw_devlink_link_to_consumers(dev);
  1993. __fw_devlink_link_to_suppliers(dev, fwnode);
  1994. }
  1995. /* Device links support end. */
  1996. static struct kobject *dev_kobj;
  1997. /* /sys/dev/char */
  1998. static struct kobject *sysfs_dev_char_kobj;
  1999. /* /sys/dev/block */
  2000. static struct kobject *sysfs_dev_block_kobj;
  2001. static DEFINE_MUTEX(device_hotplug_lock);
  2002. void lock_device_hotplug(void)
  2003. {
  2004. mutex_lock(&device_hotplug_lock);
  2005. }
  2006. void unlock_device_hotplug(void)
  2007. {
  2008. mutex_unlock(&device_hotplug_lock);
  2009. }
  2010. int lock_device_hotplug_sysfs(void)
  2011. {
  2012. if (mutex_trylock(&device_hotplug_lock))
  2013. return 0;
  2014. /* Avoid busy looping (5 ms of sleep should do). */
  2015. msleep(5);
  2016. return restart_syscall();
  2017. }
  2018. #ifdef CONFIG_BLOCK
  2019. static inline int device_is_not_partition(struct device *dev)
  2020. {
  2021. return !(dev->type == &part_type);
  2022. }
  2023. #else
  2024. static inline int device_is_not_partition(struct device *dev)
  2025. {
  2026. return 1;
  2027. }
  2028. #endif
  2029. static void device_platform_notify(struct device *dev)
  2030. {
  2031. acpi_device_notify(dev);
  2032. software_node_notify(dev);
  2033. }
  2034. static void device_platform_notify_remove(struct device *dev)
  2035. {
  2036. software_node_notify_remove(dev);
  2037. acpi_device_notify_remove(dev);
  2038. }
  2039. /**
  2040. * dev_driver_string - Return a device's driver name, if at all possible
  2041. * @dev: struct device to get the name of
  2042. *
  2043. * Will return the device's driver's name if it is bound to a device. If
  2044. * the device is not bound to a driver, it will return the name of the bus
  2045. * it is attached to. If it is not attached to a bus either, an empty
  2046. * string will be returned.
  2047. */
  2048. const char *dev_driver_string(const struct device *dev)
  2049. {
  2050. struct device_driver *drv;
  2051. /* dev->driver can change to NULL underneath us because of unbinding,
  2052. * so be careful about accessing it. dev->bus and dev->class should
  2053. * never change once they are set, so they don't need special care.
  2054. */
  2055. drv = READ_ONCE(dev->driver);
  2056. return drv ? drv->name : dev_bus_name(dev);
  2057. }
  2058. EXPORT_SYMBOL(dev_driver_string);
  2059. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  2060. static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
  2061. char *buf)
  2062. {
  2063. struct device_attribute *dev_attr = to_dev_attr(attr);
  2064. struct device *dev = kobj_to_dev(kobj);
  2065. ssize_t ret = -EIO;
  2066. if (dev_attr->show)
  2067. ret = dev_attr->show(dev, dev_attr, buf);
  2068. if (ret >= (ssize_t)PAGE_SIZE) {
  2069. printk("dev_attr_show: %pS returned bad count\n",
  2070. dev_attr->show);
  2071. }
  2072. return ret;
  2073. }
  2074. static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
  2075. const char *buf, size_t count)
  2076. {
  2077. struct device_attribute *dev_attr = to_dev_attr(attr);
  2078. struct device *dev = kobj_to_dev(kobj);
  2079. ssize_t ret = -EIO;
  2080. if (dev_attr->store)
  2081. ret = dev_attr->store(dev, dev_attr, buf, count);
  2082. return ret;
  2083. }
  2084. static const struct sysfs_ops dev_sysfs_ops = {
  2085. .show = dev_attr_show,
  2086. .store = dev_attr_store,
  2087. };
  2088. #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
  2089. ssize_t device_store_ulong(struct device *dev,
  2090. struct device_attribute *attr,
  2091. const char *buf, size_t size)
  2092. {
  2093. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2094. int ret;
  2095. unsigned long new;
  2096. ret = kstrtoul(buf, 0, &new);
  2097. if (ret)
  2098. return ret;
  2099. *(unsigned long *)(ea->var) = new;
  2100. /* Always return full write size even if we didn't consume all */
  2101. return size;
  2102. }
  2103. EXPORT_SYMBOL_GPL(device_store_ulong);
  2104. ssize_t device_show_ulong(struct device *dev,
  2105. struct device_attribute *attr,
  2106. char *buf)
  2107. {
  2108. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2109. return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
  2110. }
  2111. EXPORT_SYMBOL_GPL(device_show_ulong);
  2112. ssize_t device_store_int(struct device *dev,
  2113. struct device_attribute *attr,
  2114. const char *buf, size_t size)
  2115. {
  2116. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2117. int ret;
  2118. long new;
  2119. ret = kstrtol(buf, 0, &new);
  2120. if (ret)
  2121. return ret;
  2122. if (new > INT_MAX || new < INT_MIN)
  2123. return -EINVAL;
  2124. *(int *)(ea->var) = new;
  2125. /* Always return full write size even if we didn't consume all */
  2126. return size;
  2127. }
  2128. EXPORT_SYMBOL_GPL(device_store_int);
  2129. ssize_t device_show_int(struct device *dev,
  2130. struct device_attribute *attr,
  2131. char *buf)
  2132. {
  2133. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2134. return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
  2135. }
  2136. EXPORT_SYMBOL_GPL(device_show_int);
  2137. ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
  2138. const char *buf, size_t size)
  2139. {
  2140. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2141. if (kstrtobool(buf, ea->var) < 0)
  2142. return -EINVAL;
  2143. return size;
  2144. }
  2145. EXPORT_SYMBOL_GPL(device_store_bool);
  2146. ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
  2147. char *buf)
  2148. {
  2149. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2150. return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
  2151. }
  2152. EXPORT_SYMBOL_GPL(device_show_bool);
  2153. ssize_t device_show_string(struct device *dev,
  2154. struct device_attribute *attr, char *buf)
  2155. {
  2156. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2157. return sysfs_emit(buf, "%s\n", (char *)ea->var);
  2158. }
  2159. EXPORT_SYMBOL_GPL(device_show_string);
  2160. /**
  2161. * device_release - free device structure.
  2162. * @kobj: device's kobject.
  2163. *
  2164. * This is called once the reference count for the object
  2165. * reaches 0. We forward the call to the device's release
  2166. * method, which should handle actually freeing the structure.
  2167. */
  2168. static void device_release(struct kobject *kobj)
  2169. {
  2170. struct device *dev = kobj_to_dev(kobj);
  2171. struct device_private *p = dev->p;
  2172. /*
  2173. * Some platform devices are driven without driver attached
  2174. * and managed resources may have been acquired. Make sure
  2175. * all resources are released.
  2176. *
  2177. * Drivers still can add resources into device after device
  2178. * is deleted but alive, so release devres here to avoid
  2179. * possible memory leak.
  2180. */
  2181. devres_release_all(dev);
  2182. kfree(dev->dma_range_map);
  2183. kfree(dev->driver_override.name);
  2184. if (dev->release)
  2185. dev->release(dev);
  2186. else if (dev->type && dev->type->release)
  2187. dev->type->release(dev);
  2188. else if (dev->class && dev->class->dev_release)
  2189. dev->class->dev_release(dev);
  2190. else
  2191. WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",
  2192. dev_name(dev));
  2193. kfree(p);
  2194. }
  2195. static const struct ns_common *device_namespace(const struct kobject *kobj)
  2196. {
  2197. const struct device *dev = kobj_to_dev(kobj);
  2198. if (dev->class && dev->class->namespace)
  2199. return dev->class->namespace(dev);
  2200. return NULL;
  2201. }
  2202. static void device_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
  2203. {
  2204. const struct device *dev = kobj_to_dev(kobj);
  2205. if (dev->class && dev->class->get_ownership)
  2206. dev->class->get_ownership(dev, uid, gid);
  2207. }
  2208. static const struct kobj_type device_ktype = {
  2209. .release = device_release,
  2210. .sysfs_ops = &dev_sysfs_ops,
  2211. .namespace = device_namespace,
  2212. .get_ownership = device_get_ownership,
  2213. };
  2214. static int dev_uevent_filter(const struct kobject *kobj)
  2215. {
  2216. const struct kobj_type *ktype = get_ktype(kobj);
  2217. if (ktype == &device_ktype) {
  2218. const struct device *dev = kobj_to_dev(kobj);
  2219. if (dev->bus)
  2220. return 1;
  2221. if (dev->class)
  2222. return 1;
  2223. }
  2224. return 0;
  2225. }
  2226. static const char *dev_uevent_name(const struct kobject *kobj)
  2227. {
  2228. const struct device *dev = kobj_to_dev(kobj);
  2229. if (dev->bus)
  2230. return dev->bus->name;
  2231. if (dev->class)
  2232. return dev->class->name;
  2233. return NULL;
  2234. }
  2235. /*
  2236. * Try filling "DRIVER=<name>" uevent variable for a device. Because this
  2237. * function may race with binding and unbinding the device from a driver,
  2238. * we need to be careful. Binding is generally safe, at worst we miss the
  2239. * fact that the device is already bound to a driver (but the driver
  2240. * information that is delivered through uevents is best-effort, it may
  2241. * become obsolete as soon as it is generated anyways). Unbinding is more
  2242. * risky as driver pointer is transitioning to NULL, so READ_ONCE() should
  2243. * be used to make sure we are dealing with the same pointer, and to
  2244. * ensure that driver structure is not going to disappear from under us
  2245. * we take bus' drivers klist lock. The assumption that only registered
  2246. * driver can be bound to a device, and to unregister a driver bus code
  2247. * will take the same lock.
  2248. */
  2249. static void dev_driver_uevent(const struct device *dev, struct kobj_uevent_env *env)
  2250. {
  2251. struct subsys_private *sp = bus_to_subsys(dev->bus);
  2252. if (sp) {
  2253. scoped_guard(spinlock, &sp->klist_drivers.k_lock) {
  2254. struct device_driver *drv = READ_ONCE(dev->driver);
  2255. if (drv)
  2256. add_uevent_var(env, "DRIVER=%s", drv->name);
  2257. }
  2258. subsys_put(sp);
  2259. }
  2260. }
  2261. static int dev_uevent(const struct kobject *kobj, struct kobj_uevent_env *env)
  2262. {
  2263. const struct device *dev = kobj_to_dev(kobj);
  2264. int retval = 0;
  2265. /* add device node properties if present */
  2266. if (MAJOR(dev->devt)) {
  2267. const char *tmp;
  2268. const char *name;
  2269. umode_t mode = 0;
  2270. kuid_t uid = GLOBAL_ROOT_UID;
  2271. kgid_t gid = GLOBAL_ROOT_GID;
  2272. add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
  2273. add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
  2274. name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
  2275. if (name) {
  2276. add_uevent_var(env, "DEVNAME=%s", name);
  2277. if (mode)
  2278. add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
  2279. if (!uid_eq(uid, GLOBAL_ROOT_UID))
  2280. add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
  2281. if (!gid_eq(gid, GLOBAL_ROOT_GID))
  2282. add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
  2283. kfree(tmp);
  2284. }
  2285. }
  2286. if (dev->type && dev->type->name)
  2287. add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
  2288. /* Add "DRIVER=%s" variable if the device is bound to a driver */
  2289. dev_driver_uevent(dev, env);
  2290. /* Add common DT information about the device */
  2291. of_device_uevent(dev, env);
  2292. /* have the bus specific function add its stuff */
  2293. if (dev->bus && dev->bus->uevent) {
  2294. retval = dev->bus->uevent(dev, env);
  2295. if (retval)
  2296. pr_debug("device: '%s': %s: bus uevent() returned %d\n",
  2297. dev_name(dev), __func__, retval);
  2298. }
  2299. /* have the class specific function add its stuff */
  2300. if (dev->class && dev->class->dev_uevent) {
  2301. retval = dev->class->dev_uevent(dev, env);
  2302. if (retval)
  2303. pr_debug("device: '%s': %s: class uevent() "
  2304. "returned %d\n", dev_name(dev),
  2305. __func__, retval);
  2306. }
  2307. /* have the device type specific function add its stuff */
  2308. if (dev->type && dev->type->uevent) {
  2309. retval = dev->type->uevent(dev, env);
  2310. if (retval)
  2311. pr_debug("device: '%s': %s: dev_type uevent() "
  2312. "returned %d\n", dev_name(dev),
  2313. __func__, retval);
  2314. }
  2315. return retval;
  2316. }
  2317. static const struct kset_uevent_ops device_uevent_ops = {
  2318. .filter = dev_uevent_filter,
  2319. .name = dev_uevent_name,
  2320. .uevent = dev_uevent,
  2321. };
  2322. static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
  2323. char *buf)
  2324. {
  2325. struct kobject *top_kobj;
  2326. struct kset *kset;
  2327. struct kobj_uevent_env *env = NULL;
  2328. int i;
  2329. int len = 0;
  2330. int retval;
  2331. /* search the kset, the device belongs to */
  2332. top_kobj = &dev->kobj;
  2333. while (!top_kobj->kset && top_kobj->parent)
  2334. top_kobj = top_kobj->parent;
  2335. if (!top_kobj->kset)
  2336. goto out;
  2337. kset = top_kobj->kset;
  2338. if (!kset->uevent_ops || !kset->uevent_ops->uevent)
  2339. goto out;
  2340. /* respect filter */
  2341. if (kset->uevent_ops && kset->uevent_ops->filter)
  2342. if (!kset->uevent_ops->filter(&dev->kobj))
  2343. goto out;
  2344. env = kzalloc_obj(struct kobj_uevent_env);
  2345. if (!env)
  2346. return -ENOMEM;
  2347. /* let the kset specific function add its keys */
  2348. retval = kset->uevent_ops->uevent(&dev->kobj, env);
  2349. if (retval)
  2350. goto out;
  2351. /* copy keys to file */
  2352. for (i = 0; i < env->envp_idx; i++)
  2353. len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
  2354. out:
  2355. kfree(env);
  2356. return len;
  2357. }
  2358. static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
  2359. const char *buf, size_t count)
  2360. {
  2361. int rc;
  2362. rc = kobject_synth_uevent(&dev->kobj, buf, count);
  2363. if (rc) {
  2364. dev_err(dev, "uevent: failed to send synthetic uevent: %d\n", rc);
  2365. return rc;
  2366. }
  2367. return count;
  2368. }
  2369. static DEVICE_ATTR_RW(uevent);
  2370. static ssize_t online_show(struct device *dev, struct device_attribute *attr,
  2371. char *buf)
  2372. {
  2373. bool val;
  2374. device_lock(dev);
  2375. val = !dev->offline;
  2376. device_unlock(dev);
  2377. return sysfs_emit(buf, "%u\n", val);
  2378. }
  2379. static ssize_t online_store(struct device *dev, struct device_attribute *attr,
  2380. const char *buf, size_t count)
  2381. {
  2382. bool val;
  2383. int ret;
  2384. ret = kstrtobool(buf, &val);
  2385. if (ret < 0)
  2386. return ret;
  2387. ret = lock_device_hotplug_sysfs();
  2388. if (ret)
  2389. return ret;
  2390. ret = val ? device_online(dev) : device_offline(dev);
  2391. unlock_device_hotplug();
  2392. return ret < 0 ? ret : count;
  2393. }
  2394. static DEVICE_ATTR_RW(online);
  2395. static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
  2396. char *buf)
  2397. {
  2398. const char *loc;
  2399. switch (dev->removable) {
  2400. case DEVICE_REMOVABLE:
  2401. loc = "removable";
  2402. break;
  2403. case DEVICE_FIXED:
  2404. loc = "fixed";
  2405. break;
  2406. default:
  2407. loc = "unknown";
  2408. }
  2409. return sysfs_emit(buf, "%s\n", loc);
  2410. }
  2411. static DEVICE_ATTR_RO(removable);
  2412. int device_add_groups(struct device *dev, const struct attribute_group **groups)
  2413. {
  2414. return sysfs_create_groups(&dev->kobj, groups);
  2415. }
  2416. EXPORT_SYMBOL_GPL(device_add_groups);
  2417. void device_remove_groups(struct device *dev,
  2418. const struct attribute_group **groups)
  2419. {
  2420. sysfs_remove_groups(&dev->kobj, groups);
  2421. }
  2422. EXPORT_SYMBOL_GPL(device_remove_groups);
  2423. union device_attr_group_devres {
  2424. const struct attribute_group *group;
  2425. const struct attribute_group **groups;
  2426. };
  2427. static void devm_attr_group_remove(struct device *dev, void *res)
  2428. {
  2429. union device_attr_group_devres *devres = res;
  2430. const struct attribute_group *group = devres->group;
  2431. dev_dbg(dev, "%s: removing group %p\n", __func__, group);
  2432. sysfs_remove_group(&dev->kobj, group);
  2433. }
  2434. /**
  2435. * devm_device_add_group - given a device, create a managed attribute group
  2436. * @dev: The device to create the group for
  2437. * @grp: The attribute group to create
  2438. *
  2439. * This function creates a group for the first time. It will explicitly
  2440. * warn and error if any of the attribute files being created already exist.
  2441. *
  2442. * Returns 0 on success or error code on failure.
  2443. */
  2444. int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
  2445. {
  2446. union device_attr_group_devres *devres;
  2447. int error;
  2448. devres = devres_alloc(devm_attr_group_remove,
  2449. sizeof(*devres), GFP_KERNEL);
  2450. if (!devres)
  2451. return -ENOMEM;
  2452. error = sysfs_create_group(&dev->kobj, grp);
  2453. if (error) {
  2454. devres_free(devres);
  2455. return error;
  2456. }
  2457. devres->group = grp;
  2458. devres_add(dev, devres);
  2459. return 0;
  2460. }
  2461. EXPORT_SYMBOL_GPL(devm_device_add_group);
  2462. static int device_add_attrs(struct device *dev)
  2463. {
  2464. const struct class *class = dev->class;
  2465. const struct device_type *type = dev->type;
  2466. int error;
  2467. if (class) {
  2468. error = device_add_groups(dev, class->dev_groups);
  2469. if (error)
  2470. return error;
  2471. }
  2472. if (type) {
  2473. error = device_add_groups(dev, type->groups);
  2474. if (error)
  2475. goto err_remove_class_groups;
  2476. }
  2477. error = device_add_groups(dev, dev->groups);
  2478. if (error)
  2479. goto err_remove_type_groups;
  2480. if (device_supports_offline(dev) && !dev->offline_disabled) {
  2481. error = device_create_file(dev, &dev_attr_online);
  2482. if (error)
  2483. goto err_remove_dev_groups;
  2484. }
  2485. if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
  2486. error = device_create_file(dev, &dev_attr_waiting_for_supplier);
  2487. if (error)
  2488. goto err_remove_dev_online;
  2489. }
  2490. if (dev_removable_is_valid(dev)) {
  2491. error = device_create_file(dev, &dev_attr_removable);
  2492. if (error)
  2493. goto err_remove_dev_waiting_for_supplier;
  2494. }
  2495. if (dev_add_physical_location(dev)) {
  2496. error = device_add_group(dev,
  2497. &dev_attr_physical_location_group);
  2498. if (error)
  2499. goto err_remove_dev_removable;
  2500. }
  2501. return 0;
  2502. err_remove_dev_removable:
  2503. device_remove_file(dev, &dev_attr_removable);
  2504. err_remove_dev_waiting_for_supplier:
  2505. device_remove_file(dev, &dev_attr_waiting_for_supplier);
  2506. err_remove_dev_online:
  2507. device_remove_file(dev, &dev_attr_online);
  2508. err_remove_dev_groups:
  2509. device_remove_groups(dev, dev->groups);
  2510. err_remove_type_groups:
  2511. if (type)
  2512. device_remove_groups(dev, type->groups);
  2513. err_remove_class_groups:
  2514. if (class)
  2515. device_remove_groups(dev, class->dev_groups);
  2516. return error;
  2517. }
  2518. static void device_remove_attrs(struct device *dev)
  2519. {
  2520. const struct class *class = dev->class;
  2521. const struct device_type *type = dev->type;
  2522. if (dev->physical_location) {
  2523. device_remove_group(dev, &dev_attr_physical_location_group);
  2524. kfree(dev->physical_location);
  2525. }
  2526. device_remove_file(dev, &dev_attr_removable);
  2527. device_remove_file(dev, &dev_attr_waiting_for_supplier);
  2528. device_remove_file(dev, &dev_attr_online);
  2529. device_remove_groups(dev, dev->groups);
  2530. if (type)
  2531. device_remove_groups(dev, type->groups);
  2532. if (class)
  2533. device_remove_groups(dev, class->dev_groups);
  2534. }
  2535. static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
  2536. char *buf)
  2537. {
  2538. return print_dev_t(buf, dev->devt);
  2539. }
  2540. static DEVICE_ATTR_RO(dev);
  2541. /* /sys/devices/ */
  2542. struct kset *devices_kset;
  2543. /**
  2544. * devices_kset_move_before - Move device in the devices_kset's list.
  2545. * @deva: Device to move.
  2546. * @devb: Device @deva should come before.
  2547. */
  2548. static void devices_kset_move_before(struct device *deva, struct device *devb)
  2549. {
  2550. if (!devices_kset)
  2551. return;
  2552. pr_debug("devices_kset: Moving %s before %s\n",
  2553. dev_name(deva), dev_name(devb));
  2554. spin_lock(&devices_kset->list_lock);
  2555. list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
  2556. spin_unlock(&devices_kset->list_lock);
  2557. }
  2558. /**
  2559. * devices_kset_move_after - Move device in the devices_kset's list.
  2560. * @deva: Device to move
  2561. * @devb: Device @deva should come after.
  2562. */
  2563. static void devices_kset_move_after(struct device *deva, struct device *devb)
  2564. {
  2565. if (!devices_kset)
  2566. return;
  2567. pr_debug("devices_kset: Moving %s after %s\n",
  2568. dev_name(deva), dev_name(devb));
  2569. spin_lock(&devices_kset->list_lock);
  2570. list_move(&deva->kobj.entry, &devb->kobj.entry);
  2571. spin_unlock(&devices_kset->list_lock);
  2572. }
  2573. /**
  2574. * devices_kset_move_last - move the device to the end of devices_kset's list.
  2575. * @dev: device to move
  2576. */
  2577. void devices_kset_move_last(struct device *dev)
  2578. {
  2579. if (!devices_kset)
  2580. return;
  2581. pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
  2582. spin_lock(&devices_kset->list_lock);
  2583. list_move_tail(&dev->kobj.entry, &devices_kset->list);
  2584. spin_unlock(&devices_kset->list_lock);
  2585. }
  2586. /**
  2587. * device_create_file - create sysfs attribute file for device.
  2588. * @dev: device.
  2589. * @attr: device attribute descriptor.
  2590. */
  2591. int device_create_file(struct device *dev,
  2592. const struct device_attribute *attr)
  2593. {
  2594. int error = 0;
  2595. if (dev) {
  2596. WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
  2597. "Attribute %s: write permission without 'store'\n",
  2598. attr->attr.name);
  2599. WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
  2600. "Attribute %s: read permission without 'show'\n",
  2601. attr->attr.name);
  2602. error = sysfs_create_file(&dev->kobj, &attr->attr);
  2603. }
  2604. return error;
  2605. }
  2606. EXPORT_SYMBOL_GPL(device_create_file);
  2607. /**
  2608. * device_remove_file - remove sysfs attribute file.
  2609. * @dev: device.
  2610. * @attr: device attribute descriptor.
  2611. */
  2612. void device_remove_file(struct device *dev,
  2613. const struct device_attribute *attr)
  2614. {
  2615. if (dev)
  2616. sysfs_remove_file(&dev->kobj, &attr->attr);
  2617. }
  2618. EXPORT_SYMBOL_GPL(device_remove_file);
  2619. /**
  2620. * device_remove_file_self - remove sysfs attribute file from its own method.
  2621. * @dev: device.
  2622. * @attr: device attribute descriptor.
  2623. *
  2624. * See kernfs_remove_self() for details.
  2625. */
  2626. bool device_remove_file_self(struct device *dev,
  2627. const struct device_attribute *attr)
  2628. {
  2629. if (dev)
  2630. return sysfs_remove_file_self(&dev->kobj, &attr->attr);
  2631. else
  2632. return false;
  2633. }
  2634. EXPORT_SYMBOL_GPL(device_remove_file_self);
  2635. /**
  2636. * device_create_bin_file - create sysfs binary attribute file for device.
  2637. * @dev: device.
  2638. * @attr: device binary attribute descriptor.
  2639. */
  2640. int device_create_bin_file(struct device *dev,
  2641. const struct bin_attribute *attr)
  2642. {
  2643. int error = -EINVAL;
  2644. if (dev)
  2645. error = sysfs_create_bin_file(&dev->kobj, attr);
  2646. return error;
  2647. }
  2648. EXPORT_SYMBOL_GPL(device_create_bin_file);
  2649. /**
  2650. * device_remove_bin_file - remove sysfs binary attribute file
  2651. * @dev: device.
  2652. * @attr: device binary attribute descriptor.
  2653. */
  2654. void device_remove_bin_file(struct device *dev,
  2655. const struct bin_attribute *attr)
  2656. {
  2657. if (dev)
  2658. sysfs_remove_bin_file(&dev->kobj, attr);
  2659. }
  2660. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  2661. static void klist_children_get(struct klist_node *n)
  2662. {
  2663. struct device_private *p = to_device_private_parent(n);
  2664. struct device *dev = p->device;
  2665. get_device(dev);
  2666. }
  2667. static void klist_children_put(struct klist_node *n)
  2668. {
  2669. struct device_private *p = to_device_private_parent(n);
  2670. struct device *dev = p->device;
  2671. put_device(dev);
  2672. }
  2673. /**
  2674. * device_initialize - init device structure.
  2675. * @dev: device.
  2676. *
  2677. * This prepares the device for use by other layers by initializing
  2678. * its fields.
  2679. * It is the first half of device_register(), if called by
  2680. * that function, though it can also be called separately, so one
  2681. * may use @dev's fields. In particular, get_device()/put_device()
  2682. * may be used for reference counting of @dev after calling this
  2683. * function.
  2684. *
  2685. * All fields in @dev must be initialized by the caller to 0, except
  2686. * for those explicitly set to some other value. The simplest
  2687. * approach is to use kzalloc() to allocate the structure containing
  2688. * @dev.
  2689. *
  2690. * NOTE: Use put_device() to give up your reference instead of freeing
  2691. * @dev directly once you have called this function.
  2692. */
  2693. void device_initialize(struct device *dev)
  2694. {
  2695. dev->kobj.kset = devices_kset;
  2696. kobject_init(&dev->kobj, &device_ktype);
  2697. INIT_LIST_HEAD(&dev->dma_pools);
  2698. mutex_init(&dev->mutex);
  2699. spin_lock_init(&dev->driver_override.lock);
  2700. lockdep_set_novalidate_class(&dev->mutex);
  2701. spin_lock_init(&dev->devres_lock);
  2702. INIT_LIST_HEAD(&dev->devres_head);
  2703. device_pm_init(dev);
  2704. set_dev_node(dev, NUMA_NO_NODE);
  2705. INIT_LIST_HEAD(&dev->links.consumers);
  2706. INIT_LIST_HEAD(&dev->links.suppliers);
  2707. INIT_LIST_HEAD(&dev->links.defer_sync);
  2708. dev->links.status = DL_DEV_NO_DRIVER;
  2709. #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
  2710. defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
  2711. defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
  2712. dev->dma_coherent = dma_default_coherent;
  2713. #endif
  2714. swiotlb_dev_init(dev);
  2715. }
  2716. EXPORT_SYMBOL_GPL(device_initialize);
  2717. struct kobject *virtual_device_parent(void)
  2718. {
  2719. static struct kobject *virtual_dir = NULL;
  2720. if (!virtual_dir)
  2721. virtual_dir = kobject_create_and_add("virtual",
  2722. &devices_kset->kobj);
  2723. return virtual_dir;
  2724. }
  2725. struct class_dir {
  2726. struct kobject kobj;
  2727. const struct class *class;
  2728. };
  2729. #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
  2730. static void class_dir_release(struct kobject *kobj)
  2731. {
  2732. struct class_dir *dir = to_class_dir(kobj);
  2733. kfree(dir);
  2734. }
  2735. static const
  2736. struct kobj_ns_type_operations *class_dir_child_ns_type(const struct kobject *kobj)
  2737. {
  2738. const struct class_dir *dir = to_class_dir(kobj);
  2739. return dir->class->ns_type;
  2740. }
  2741. static const struct kobj_type class_dir_ktype = {
  2742. .release = class_dir_release,
  2743. .sysfs_ops = &kobj_sysfs_ops,
  2744. .child_ns_type = class_dir_child_ns_type
  2745. };
  2746. static struct kobject *class_dir_create_and_add(struct subsys_private *sp,
  2747. struct kobject *parent_kobj)
  2748. {
  2749. struct class_dir *dir;
  2750. int retval;
  2751. dir = kzalloc_obj(*dir);
  2752. if (!dir)
  2753. return ERR_PTR(-ENOMEM);
  2754. dir->class = sp->class;
  2755. kobject_init(&dir->kobj, &class_dir_ktype);
  2756. dir->kobj.kset = &sp->glue_dirs;
  2757. retval = kobject_add(&dir->kobj, parent_kobj, "%s", sp->class->name);
  2758. if (retval < 0) {
  2759. kobject_put(&dir->kobj);
  2760. return ERR_PTR(retval);
  2761. }
  2762. return &dir->kobj;
  2763. }
  2764. static DEFINE_MUTEX(gdp_mutex);
  2765. static struct kobject *get_device_parent(struct device *dev,
  2766. struct device *parent)
  2767. {
  2768. struct subsys_private *sp = class_to_subsys(dev->class);
  2769. struct kobject *kobj = NULL;
  2770. if (sp) {
  2771. struct kobject *parent_kobj;
  2772. struct kobject *k;
  2773. /*
  2774. * If we have no parent, we live in "virtual".
  2775. * Class-devices with a non class-device as parent, live
  2776. * in a "glue" directory to prevent namespace collisions.
  2777. */
  2778. if (parent == NULL)
  2779. parent_kobj = virtual_device_parent();
  2780. else if (parent->class && !dev->class->ns_type) {
  2781. subsys_put(sp);
  2782. return &parent->kobj;
  2783. } else {
  2784. parent_kobj = &parent->kobj;
  2785. }
  2786. mutex_lock(&gdp_mutex);
  2787. /* find our class-directory at the parent and reference it */
  2788. spin_lock(&sp->glue_dirs.list_lock);
  2789. list_for_each_entry(k, &sp->glue_dirs.list, entry)
  2790. if (k->parent == parent_kobj) {
  2791. kobj = kobject_get(k);
  2792. break;
  2793. }
  2794. spin_unlock(&sp->glue_dirs.list_lock);
  2795. if (kobj) {
  2796. mutex_unlock(&gdp_mutex);
  2797. subsys_put(sp);
  2798. return kobj;
  2799. }
  2800. /* or create a new class-directory at the parent device */
  2801. k = class_dir_create_and_add(sp, parent_kobj);
  2802. /* do not emit an uevent for this simple "glue" directory */
  2803. mutex_unlock(&gdp_mutex);
  2804. subsys_put(sp);
  2805. return k;
  2806. }
  2807. /* subsystems can specify a default root directory for their devices */
  2808. if (!parent && dev->bus) {
  2809. struct device *dev_root = bus_get_dev_root(dev->bus);
  2810. if (dev_root) {
  2811. kobj = &dev_root->kobj;
  2812. put_device(dev_root);
  2813. return kobj;
  2814. }
  2815. }
  2816. if (parent)
  2817. return &parent->kobj;
  2818. return NULL;
  2819. }
  2820. static inline bool live_in_glue_dir(struct kobject *kobj,
  2821. struct device *dev)
  2822. {
  2823. struct subsys_private *sp;
  2824. bool retval;
  2825. if (!kobj || !dev->class)
  2826. return false;
  2827. sp = class_to_subsys(dev->class);
  2828. if (!sp)
  2829. return false;
  2830. if (kobj->kset == &sp->glue_dirs)
  2831. retval = true;
  2832. else
  2833. retval = false;
  2834. subsys_put(sp);
  2835. return retval;
  2836. }
  2837. static inline struct kobject *get_glue_dir(struct device *dev)
  2838. {
  2839. return dev->kobj.parent;
  2840. }
  2841. /**
  2842. * kobject_has_children - Returns whether a kobject has children.
  2843. * @kobj: the object to test
  2844. *
  2845. * This will return whether a kobject has other kobjects as children.
  2846. *
  2847. * It does NOT account for the presence of attribute files, only sub
  2848. * directories. It also assumes there is no concurrent addition or
  2849. * removal of such children, and thus relies on external locking.
  2850. */
  2851. static inline bool kobject_has_children(struct kobject *kobj)
  2852. {
  2853. WARN_ON_ONCE(kref_read(&kobj->kref) == 0);
  2854. return kobj->sd && kobj->sd->dir.subdirs;
  2855. }
  2856. /*
  2857. * make sure cleaning up dir as the last step, we need to make
  2858. * sure .release handler of kobject is run with holding the
  2859. * global lock
  2860. */
  2861. static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
  2862. {
  2863. unsigned int ref;
  2864. /* see if we live in a "glue" directory */
  2865. if (!live_in_glue_dir(glue_dir, dev))
  2866. return;
  2867. mutex_lock(&gdp_mutex);
  2868. /**
  2869. * There is a race condition between removing glue directory
  2870. * and adding a new device under the glue directory.
  2871. *
  2872. * CPU1: CPU2:
  2873. *
  2874. * device_add()
  2875. * get_device_parent()
  2876. * class_dir_create_and_add()
  2877. * kobject_add_internal()
  2878. * create_dir() // create glue_dir
  2879. *
  2880. * device_add()
  2881. * get_device_parent()
  2882. * kobject_get() // get glue_dir
  2883. *
  2884. * device_del()
  2885. * cleanup_glue_dir()
  2886. * kobject_del(glue_dir)
  2887. *
  2888. * kobject_add()
  2889. * kobject_add_internal()
  2890. * create_dir() // in glue_dir
  2891. * sysfs_create_dir_ns()
  2892. * kernfs_create_dir_ns(sd)
  2893. *
  2894. * sysfs_remove_dir() // glue_dir->sd=NULL
  2895. * sysfs_put() // free glue_dir->sd
  2896. *
  2897. * // sd is freed
  2898. * kernfs_new_node(sd)
  2899. * kernfs_get(glue_dir)
  2900. * kernfs_add_one()
  2901. * kernfs_put()
  2902. *
  2903. * Before CPU1 remove last child device under glue dir, if CPU2 add
  2904. * a new device under glue dir, the glue_dir kobject reference count
  2905. * will be increase to 2 in kobject_get(k). And CPU2 has been called
  2906. * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
  2907. * and sysfs_put(). This result in glue_dir->sd is freed.
  2908. *
  2909. * Then the CPU2 will see a stale "empty" but still potentially used
  2910. * glue dir around in kernfs_new_node().
  2911. *
  2912. * In order to avoid this happening, we also should make sure that
  2913. * kernfs_node for glue_dir is released in CPU1 only when refcount
  2914. * for glue_dir kobj is 1.
  2915. */
  2916. ref = kref_read(&glue_dir->kref);
  2917. if (!kobject_has_children(glue_dir) && !--ref)
  2918. kobject_del(glue_dir);
  2919. kobject_put(glue_dir);
  2920. mutex_unlock(&gdp_mutex);
  2921. }
  2922. static int device_add_class_symlinks(struct device *dev)
  2923. {
  2924. struct device_node *of_node = dev_of_node(dev);
  2925. struct subsys_private *sp;
  2926. int error;
  2927. if (of_node) {
  2928. error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
  2929. if (error)
  2930. dev_warn(dev, "Error %d creating of_node link\n",error);
  2931. /* An error here doesn't warrant bringing down the device */
  2932. }
  2933. sp = class_to_subsys(dev->class);
  2934. if (!sp)
  2935. return 0;
  2936. error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem");
  2937. if (error)
  2938. goto out_devnode;
  2939. if (dev->parent && device_is_not_partition(dev)) {
  2940. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  2941. "device");
  2942. if (error)
  2943. goto out_subsys;
  2944. }
  2945. /* link in the class directory pointing to the device */
  2946. error = sysfs_create_link(&sp->subsys.kobj, &dev->kobj, dev_name(dev));
  2947. if (error)
  2948. goto out_device;
  2949. goto exit;
  2950. out_device:
  2951. sysfs_remove_link(&dev->kobj, "device");
  2952. out_subsys:
  2953. sysfs_remove_link(&dev->kobj, "subsystem");
  2954. out_devnode:
  2955. sysfs_remove_link(&dev->kobj, "of_node");
  2956. exit:
  2957. subsys_put(sp);
  2958. return error;
  2959. }
  2960. static void device_remove_class_symlinks(struct device *dev)
  2961. {
  2962. struct subsys_private *sp = class_to_subsys(dev->class);
  2963. if (dev_of_node(dev))
  2964. sysfs_remove_link(&dev->kobj, "of_node");
  2965. if (!sp)
  2966. return;
  2967. if (dev->parent && device_is_not_partition(dev))
  2968. sysfs_remove_link(&dev->kobj, "device");
  2969. sysfs_remove_link(&dev->kobj, "subsystem");
  2970. sysfs_delete_link(&sp->subsys.kobj, &dev->kobj, dev_name(dev));
  2971. subsys_put(sp);
  2972. }
  2973. /**
  2974. * dev_set_name - set a device name
  2975. * @dev: device
  2976. * @fmt: format string for the device's name
  2977. */
  2978. int dev_set_name(struct device *dev, const char *fmt, ...)
  2979. {
  2980. va_list vargs;
  2981. int err;
  2982. va_start(vargs, fmt);
  2983. err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
  2984. va_end(vargs);
  2985. return err;
  2986. }
  2987. EXPORT_SYMBOL_GPL(dev_set_name);
  2988. /* select a /sys/dev/ directory for the device */
  2989. static struct kobject *device_to_dev_kobj(struct device *dev)
  2990. {
  2991. if (is_blockdev(dev))
  2992. return sysfs_dev_block_kobj;
  2993. else
  2994. return sysfs_dev_char_kobj;
  2995. }
  2996. static int device_create_sys_dev_entry(struct device *dev)
  2997. {
  2998. struct kobject *kobj = device_to_dev_kobj(dev);
  2999. int error = 0;
  3000. char devt_str[15];
  3001. if (kobj) {
  3002. format_dev_t(devt_str, dev->devt);
  3003. error = sysfs_create_link(kobj, &dev->kobj, devt_str);
  3004. }
  3005. return error;
  3006. }
  3007. static void device_remove_sys_dev_entry(struct device *dev)
  3008. {
  3009. struct kobject *kobj = device_to_dev_kobj(dev);
  3010. char devt_str[15];
  3011. if (kobj) {
  3012. format_dev_t(devt_str, dev->devt);
  3013. sysfs_remove_link(kobj, devt_str);
  3014. }
  3015. }
  3016. static int device_private_init(struct device *dev)
  3017. {
  3018. dev->p = kzalloc_obj(*dev->p);
  3019. if (!dev->p)
  3020. return -ENOMEM;
  3021. dev->p->device = dev;
  3022. klist_init(&dev->p->klist_children, klist_children_get,
  3023. klist_children_put);
  3024. INIT_LIST_HEAD(&dev->p->deferred_probe);
  3025. return 0;
  3026. }
  3027. /**
  3028. * device_add - add device to device hierarchy.
  3029. * @dev: device.
  3030. *
  3031. * This is part 2 of device_register(), though may be called
  3032. * separately _iff_ device_initialize() has been called separately.
  3033. *
  3034. * This adds @dev to the kobject hierarchy via kobject_add(), adds it
  3035. * to the global and sibling lists for the device, then
  3036. * adds it to the other relevant subsystems of the driver model.
  3037. *
  3038. * Do not call this routine or device_register() more than once for
  3039. * any device structure. The driver model core is not designed to work
  3040. * with devices that get unregistered and then spring back to life.
  3041. * (Among other things, it's very hard to guarantee that all references
  3042. * to the previous incarnation of @dev have been dropped.) Allocate
  3043. * and register a fresh new struct device instead.
  3044. *
  3045. * NOTE: _Never_ directly free @dev after calling this function, even
  3046. * if it returned an error! Always use put_device() to give up your
  3047. * reference instead.
  3048. *
  3049. * Rule of thumb is: if device_add() succeeds, you should call
  3050. * device_del() when you want to get rid of it. If device_add() has
  3051. * *not* succeeded, use *only* put_device() to drop the reference
  3052. * count.
  3053. */
  3054. int device_add(struct device *dev)
  3055. {
  3056. struct subsys_private *sp;
  3057. struct device *parent;
  3058. struct kobject *kobj;
  3059. struct class_interface *class_intf;
  3060. int error = -EINVAL;
  3061. struct kobject *glue_dir = NULL;
  3062. dev = get_device(dev);
  3063. if (!dev)
  3064. goto done;
  3065. if (!dev->p) {
  3066. error = device_private_init(dev);
  3067. if (error)
  3068. goto done;
  3069. }
  3070. /*
  3071. * for statically allocated devices, which should all be converted
  3072. * some day, we need to initialize the name. We prevent reading back
  3073. * the name, and force the use of dev_name()
  3074. */
  3075. if (dev->init_name) {
  3076. error = dev_set_name(dev, "%s", dev->init_name);
  3077. dev->init_name = NULL;
  3078. }
  3079. if (dev_name(dev))
  3080. error = 0;
  3081. /* subsystems can specify simple device enumeration */
  3082. else if (dev->bus && dev->bus->dev_name)
  3083. error = dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
  3084. else
  3085. error = -EINVAL;
  3086. if (error)
  3087. goto name_error;
  3088. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  3089. parent = get_device(dev->parent);
  3090. kobj = get_device_parent(dev, parent);
  3091. if (IS_ERR(kobj)) {
  3092. error = PTR_ERR(kobj);
  3093. goto parent_error;
  3094. }
  3095. if (kobj)
  3096. dev->kobj.parent = kobj;
  3097. /* use parent numa_node */
  3098. if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
  3099. set_dev_node(dev, dev_to_node(parent));
  3100. /* first, register with generic layer. */
  3101. /* we require the name to be set before, and pass NULL */
  3102. error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
  3103. if (error) {
  3104. glue_dir = kobj;
  3105. goto Error;
  3106. }
  3107. /* notify platform of device entry */
  3108. device_platform_notify(dev);
  3109. error = device_create_file(dev, &dev_attr_uevent);
  3110. if (error)
  3111. goto attrError;
  3112. error = device_add_class_symlinks(dev);
  3113. if (error)
  3114. goto SymlinkError;
  3115. error = device_add_attrs(dev);
  3116. if (error)
  3117. goto AttrsError;
  3118. error = bus_add_device(dev);
  3119. if (error)
  3120. goto BusError;
  3121. error = dpm_sysfs_add(dev);
  3122. if (error)
  3123. goto DPMError;
  3124. device_pm_add(dev);
  3125. if (MAJOR(dev->devt)) {
  3126. error = device_create_file(dev, &dev_attr_dev);
  3127. if (error)
  3128. goto DevAttrError;
  3129. error = device_create_sys_dev_entry(dev);
  3130. if (error)
  3131. goto SysEntryError;
  3132. devtmpfs_create_node(dev);
  3133. }
  3134. /* Notify clients of device addition. This call must come
  3135. * after dpm_sysfs_add() and before kobject_uevent().
  3136. */
  3137. bus_notify(dev, BUS_NOTIFY_ADD_DEVICE);
  3138. kobject_uevent(&dev->kobj, KOBJ_ADD);
  3139. /*
  3140. * Check if any of the other devices (consumers) have been waiting for
  3141. * this device (supplier) to be added so that they can create a device
  3142. * link to it.
  3143. *
  3144. * This needs to happen after device_pm_add() because device_link_add()
  3145. * requires the supplier be registered before it's called.
  3146. *
  3147. * But this also needs to happen before bus_probe_device() to make sure
  3148. * waiting consumers can link to it before the driver is bound to the
  3149. * device and the driver sync_state callback is called for this device.
  3150. */
  3151. if (dev->fwnode && !dev->fwnode->dev) {
  3152. dev->fwnode->dev = dev;
  3153. fw_devlink_link_device(dev);
  3154. }
  3155. bus_probe_device(dev);
  3156. /*
  3157. * If all driver registration is done and a newly added device doesn't
  3158. * match with any driver, don't block its consumers from probing in
  3159. * case the consumer device is able to operate without this supplier.
  3160. */
  3161. if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)
  3162. fw_devlink_unblock_consumers(dev);
  3163. if (parent)
  3164. klist_add_tail(&dev->p->knode_parent,
  3165. &parent->p->klist_children);
  3166. sp = class_to_subsys(dev->class);
  3167. if (sp) {
  3168. mutex_lock(&sp->mutex);
  3169. /* tie the class to the device */
  3170. klist_add_tail(&dev->p->knode_class, &sp->klist_devices);
  3171. /* notify any interfaces that the device is here */
  3172. list_for_each_entry(class_intf, &sp->interfaces, node)
  3173. if (class_intf->add_dev)
  3174. class_intf->add_dev(dev);
  3175. mutex_unlock(&sp->mutex);
  3176. subsys_put(sp);
  3177. }
  3178. done:
  3179. put_device(dev);
  3180. return error;
  3181. SysEntryError:
  3182. if (MAJOR(dev->devt))
  3183. device_remove_file(dev, &dev_attr_dev);
  3184. DevAttrError:
  3185. device_pm_remove(dev);
  3186. dpm_sysfs_remove(dev);
  3187. DPMError:
  3188. device_set_driver(dev, NULL);
  3189. bus_remove_device(dev);
  3190. BusError:
  3191. device_remove_attrs(dev);
  3192. AttrsError:
  3193. device_remove_class_symlinks(dev);
  3194. SymlinkError:
  3195. device_remove_file(dev, &dev_attr_uevent);
  3196. attrError:
  3197. device_platform_notify_remove(dev);
  3198. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  3199. glue_dir = get_glue_dir(dev);
  3200. kobject_del(&dev->kobj);
  3201. Error:
  3202. cleanup_glue_dir(dev, glue_dir);
  3203. parent_error:
  3204. put_device(parent);
  3205. name_error:
  3206. kfree(dev->p);
  3207. dev->p = NULL;
  3208. goto done;
  3209. }
  3210. EXPORT_SYMBOL_GPL(device_add);
  3211. /**
  3212. * device_register - register a device with the system.
  3213. * @dev: pointer to the device structure
  3214. *
  3215. * This happens in two clean steps - initialize the device
  3216. * and add it to the system. The two steps can be called
  3217. * separately, but this is the easiest and most common.
  3218. * I.e. you should only call the two helpers separately if
  3219. * have a clearly defined need to use and refcount the device
  3220. * before it is added to the hierarchy.
  3221. *
  3222. * For more information, see the kerneldoc for device_initialize()
  3223. * and device_add().
  3224. *
  3225. * NOTE: _Never_ directly free @dev after calling this function, even
  3226. * if it returned an error! Always use put_device() to give up the
  3227. * reference initialized in this function instead.
  3228. */
  3229. int device_register(struct device *dev)
  3230. {
  3231. device_initialize(dev);
  3232. return device_add(dev);
  3233. }
  3234. EXPORT_SYMBOL_GPL(device_register);
  3235. /**
  3236. * get_device - increment reference count for device.
  3237. * @dev: device.
  3238. *
  3239. * This simply forwards the call to kobject_get(), though
  3240. * we do take care to provide for the case that we get a NULL
  3241. * pointer passed in.
  3242. */
  3243. struct device *get_device(struct device *dev)
  3244. {
  3245. return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
  3246. }
  3247. EXPORT_SYMBOL_GPL(get_device);
  3248. /**
  3249. * put_device - decrement reference count.
  3250. * @dev: device in question.
  3251. */
  3252. void put_device(struct device *dev)
  3253. {
  3254. /* might_sleep(); */
  3255. if (dev)
  3256. kobject_put(&dev->kobj);
  3257. }
  3258. EXPORT_SYMBOL_GPL(put_device);
  3259. bool kill_device(struct device *dev)
  3260. {
  3261. /*
  3262. * Require the device lock and set the "dead" flag to guarantee that
  3263. * the update behavior is consistent with the other bitfields near
  3264. * it and that we cannot have an asynchronous probe routine trying
  3265. * to run while we are tearing out the bus/class/sysfs from
  3266. * underneath the device.
  3267. */
  3268. device_lock_assert(dev);
  3269. if (dev->p->dead)
  3270. return false;
  3271. dev->p->dead = true;
  3272. return true;
  3273. }
  3274. EXPORT_SYMBOL_GPL(kill_device);
  3275. /**
  3276. * device_del - delete device from system.
  3277. * @dev: device.
  3278. *
  3279. * This is the first part of the device unregistration
  3280. * sequence. This removes the device from the lists we control
  3281. * from here, has it removed from the other driver model
  3282. * subsystems it was added to in device_add(), and removes it
  3283. * from the kobject hierarchy.
  3284. *
  3285. * NOTE: this should be called manually _iff_ device_add() was
  3286. * also called manually.
  3287. */
  3288. void device_del(struct device *dev)
  3289. {
  3290. struct subsys_private *sp;
  3291. struct device *parent = dev->parent;
  3292. struct kobject *glue_dir = NULL;
  3293. struct class_interface *class_intf;
  3294. unsigned int noio_flag;
  3295. device_lock(dev);
  3296. kill_device(dev);
  3297. device_unlock(dev);
  3298. if (dev->fwnode && dev->fwnode->dev == dev)
  3299. dev->fwnode->dev = NULL;
  3300. /* Notify clients of device removal. This call must come
  3301. * before dpm_sysfs_remove().
  3302. */
  3303. noio_flag = memalloc_noio_save();
  3304. bus_notify(dev, BUS_NOTIFY_DEL_DEVICE);
  3305. dpm_sysfs_remove(dev);
  3306. if (parent)
  3307. klist_del(&dev->p->knode_parent);
  3308. if (MAJOR(dev->devt)) {
  3309. devtmpfs_delete_node(dev);
  3310. device_remove_sys_dev_entry(dev);
  3311. device_remove_file(dev, &dev_attr_dev);
  3312. }
  3313. sp = class_to_subsys(dev->class);
  3314. if (sp) {
  3315. device_remove_class_symlinks(dev);
  3316. mutex_lock(&sp->mutex);
  3317. /* notify any interfaces that the device is now gone */
  3318. list_for_each_entry(class_intf, &sp->interfaces, node)
  3319. if (class_intf->remove_dev)
  3320. class_intf->remove_dev(dev);
  3321. /* remove the device from the class list */
  3322. klist_del(&dev->p->knode_class);
  3323. mutex_unlock(&sp->mutex);
  3324. subsys_put(sp);
  3325. }
  3326. device_remove_file(dev, &dev_attr_uevent);
  3327. device_remove_attrs(dev);
  3328. bus_remove_device(dev);
  3329. device_pm_remove(dev);
  3330. driver_deferred_probe_del(dev);
  3331. device_platform_notify_remove(dev);
  3332. device_links_purge(dev);
  3333. /*
  3334. * If a device does not have a driver attached, we need to clean
  3335. * up any managed resources. We do this in device_release(), but
  3336. * it's never called (and we leak the device) if a managed
  3337. * resource holds a reference to the device. So release all
  3338. * managed resources here, like we do in driver_detach(). We
  3339. * still need to do so again in device_release() in case someone
  3340. * adds a new resource after this point, though.
  3341. */
  3342. devres_release_all(dev);
  3343. bus_notify(dev, BUS_NOTIFY_REMOVED_DEVICE);
  3344. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  3345. glue_dir = get_glue_dir(dev);
  3346. kobject_del(&dev->kobj);
  3347. cleanup_glue_dir(dev, glue_dir);
  3348. memalloc_noio_restore(noio_flag);
  3349. put_device(parent);
  3350. }
  3351. EXPORT_SYMBOL_GPL(device_del);
  3352. /**
  3353. * device_unregister - unregister device from system.
  3354. * @dev: device going away.
  3355. *
  3356. * We do this in two parts, like we do device_register(). First,
  3357. * we remove it from all the subsystems with device_del(), then
  3358. * we decrement the reference count via put_device(). If that
  3359. * is the final reference count, the device will be cleaned up
  3360. * via device_release() above. Otherwise, the structure will
  3361. * stick around until the final reference to the device is dropped.
  3362. */
  3363. void device_unregister(struct device *dev)
  3364. {
  3365. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  3366. device_del(dev);
  3367. put_device(dev);
  3368. }
  3369. EXPORT_SYMBOL_GPL(device_unregister);
  3370. static struct device *prev_device(struct klist_iter *i)
  3371. {
  3372. struct klist_node *n = klist_prev(i);
  3373. struct device *dev = NULL;
  3374. struct device_private *p;
  3375. if (n) {
  3376. p = to_device_private_parent(n);
  3377. dev = p->device;
  3378. }
  3379. return dev;
  3380. }
  3381. static struct device *next_device(struct klist_iter *i)
  3382. {
  3383. struct klist_node *n = klist_next(i);
  3384. struct device *dev = NULL;
  3385. struct device_private *p;
  3386. if (n) {
  3387. p = to_device_private_parent(n);
  3388. dev = p->device;
  3389. }
  3390. return dev;
  3391. }
  3392. /**
  3393. * device_get_devnode - path of device node file
  3394. * @dev: device
  3395. * @mode: returned file access mode
  3396. * @uid: returned file owner
  3397. * @gid: returned file group
  3398. * @tmp: possibly allocated string
  3399. *
  3400. * Return the relative path of a possible device node.
  3401. * Non-default names may need to allocate a memory to compose
  3402. * a name. This memory is returned in tmp and needs to be
  3403. * freed by the caller.
  3404. */
  3405. const char *device_get_devnode(const struct device *dev,
  3406. umode_t *mode, kuid_t *uid, kgid_t *gid,
  3407. const char **tmp)
  3408. {
  3409. char *s;
  3410. *tmp = NULL;
  3411. /* the device type may provide a specific name */
  3412. if (dev->type && dev->type->devnode)
  3413. *tmp = dev->type->devnode(dev, mode, uid, gid);
  3414. if (*tmp)
  3415. return *tmp;
  3416. /* the class may provide a specific name */
  3417. if (dev->class && dev->class->devnode)
  3418. *tmp = dev->class->devnode(dev, mode);
  3419. if (*tmp)
  3420. return *tmp;
  3421. /* return name without allocation, tmp == NULL */
  3422. if (strchr(dev_name(dev), '!') == NULL)
  3423. return dev_name(dev);
  3424. /* replace '!' in the name with '/' */
  3425. s = kstrdup_and_replace(dev_name(dev), '!', '/', GFP_KERNEL);
  3426. if (!s)
  3427. return NULL;
  3428. return *tmp = s;
  3429. }
  3430. /**
  3431. * device_for_each_child - device child iterator.
  3432. * @parent: parent struct device.
  3433. * @data: data for the callback.
  3434. * @fn: function to be called for each device.
  3435. *
  3436. * Iterate over @parent's child devices, and call @fn for each,
  3437. * passing it @data.
  3438. *
  3439. * We check the return of @fn each time. If it returns anything
  3440. * other than 0, we break out and return that value.
  3441. */
  3442. int device_for_each_child(struct device *parent, void *data,
  3443. device_iter_t fn)
  3444. {
  3445. struct klist_iter i;
  3446. struct device *child;
  3447. int error = 0;
  3448. if (!parent || !parent->p)
  3449. return 0;
  3450. klist_iter_init(&parent->p->klist_children, &i);
  3451. while (!error && (child = next_device(&i)))
  3452. error = fn(child, data);
  3453. klist_iter_exit(&i);
  3454. return error;
  3455. }
  3456. EXPORT_SYMBOL_GPL(device_for_each_child);
  3457. /**
  3458. * device_for_each_child_reverse - device child iterator in reversed order.
  3459. * @parent: parent struct device.
  3460. * @data: data for the callback.
  3461. * @fn: function to be called for each device.
  3462. *
  3463. * Iterate over @parent's child devices, and call @fn for each,
  3464. * passing it @data.
  3465. *
  3466. * We check the return of @fn each time. If it returns anything
  3467. * other than 0, we break out and return that value.
  3468. */
  3469. int device_for_each_child_reverse(struct device *parent, void *data,
  3470. device_iter_t fn)
  3471. {
  3472. struct klist_iter i;
  3473. struct device *child;
  3474. int error = 0;
  3475. if (!parent || !parent->p)
  3476. return 0;
  3477. klist_iter_init(&parent->p->klist_children, &i);
  3478. while ((child = prev_device(&i)) && !error)
  3479. error = fn(child, data);
  3480. klist_iter_exit(&i);
  3481. return error;
  3482. }
  3483. EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
  3484. /**
  3485. * device_for_each_child_reverse_from - device child iterator in reversed order.
  3486. * @parent: parent struct device.
  3487. * @from: optional starting point in child list
  3488. * @data: data for the callback.
  3489. * @fn: function to be called for each device.
  3490. *
  3491. * Iterate over @parent's child devices, starting at @from, and call @fn
  3492. * for each, passing it @data. This helper is identical to
  3493. * device_for_each_child_reverse() when @from is NULL.
  3494. *
  3495. * @fn is checked each iteration. If it returns anything other than 0,
  3496. * iteration stop and that value is returned to the caller of
  3497. * device_for_each_child_reverse_from();
  3498. */
  3499. int device_for_each_child_reverse_from(struct device *parent,
  3500. struct device *from, void *data,
  3501. device_iter_t fn)
  3502. {
  3503. struct klist_iter i;
  3504. struct device *child;
  3505. int error = 0;
  3506. if (!parent || !parent->p)
  3507. return 0;
  3508. klist_iter_init_node(&parent->p->klist_children, &i,
  3509. (from ? &from->p->knode_parent : NULL));
  3510. while ((child = prev_device(&i)) && !error)
  3511. error = fn(child, data);
  3512. klist_iter_exit(&i);
  3513. return error;
  3514. }
  3515. EXPORT_SYMBOL_GPL(device_for_each_child_reverse_from);
  3516. /**
  3517. * device_find_child - device iterator for locating a particular device.
  3518. * @parent: parent struct device
  3519. * @data: Data to pass to match function
  3520. * @match: Callback function to check device
  3521. *
  3522. * This is similar to the device_for_each_child() function above, but it
  3523. * returns a reference to a device that is 'found' for later use, as
  3524. * determined by the @match callback.
  3525. *
  3526. * The callback should return 0 if the device doesn't match and non-zero
  3527. * if it does. If the callback returns non-zero and a reference to the
  3528. * current device can be obtained, this function will return to the caller
  3529. * and not iterate over any more devices.
  3530. *
  3531. * NOTE: you will need to drop the reference with put_device() after use.
  3532. */
  3533. struct device *device_find_child(struct device *parent, const void *data,
  3534. device_match_t match)
  3535. {
  3536. struct klist_iter i;
  3537. struct device *child;
  3538. if (!parent || !parent->p)
  3539. return NULL;
  3540. klist_iter_init(&parent->p->klist_children, &i);
  3541. while ((child = next_device(&i))) {
  3542. if (match(child, data)) {
  3543. get_device(child);
  3544. break;
  3545. }
  3546. }
  3547. klist_iter_exit(&i);
  3548. return child;
  3549. }
  3550. EXPORT_SYMBOL_GPL(device_find_child);
  3551. int __init devices_init(void)
  3552. {
  3553. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  3554. if (!devices_kset)
  3555. return -ENOMEM;
  3556. dev_kobj = kobject_create_and_add("dev", NULL);
  3557. if (!dev_kobj)
  3558. goto dev_kobj_err;
  3559. sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  3560. if (!sysfs_dev_block_kobj)
  3561. goto block_kobj_err;
  3562. sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  3563. if (!sysfs_dev_char_kobj)
  3564. goto char_kobj_err;
  3565. device_link_wq = alloc_workqueue("device_link_wq", WQ_PERCPU, 0);
  3566. if (!device_link_wq)
  3567. goto wq_err;
  3568. return 0;
  3569. wq_err:
  3570. kobject_put(sysfs_dev_char_kobj);
  3571. char_kobj_err:
  3572. kobject_put(sysfs_dev_block_kobj);
  3573. block_kobj_err:
  3574. kobject_put(dev_kobj);
  3575. dev_kobj_err:
  3576. kset_unregister(devices_kset);
  3577. return -ENOMEM;
  3578. }
  3579. static int device_check_offline(struct device *dev, void *not_used)
  3580. {
  3581. int ret;
  3582. ret = device_for_each_child(dev, NULL, device_check_offline);
  3583. if (ret)
  3584. return ret;
  3585. return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
  3586. }
  3587. /**
  3588. * device_offline - Prepare the device for hot-removal.
  3589. * @dev: Device to be put offline.
  3590. *
  3591. * Execute the device bus type's .offline() callback, if present, to prepare
  3592. * the device for a subsequent hot-removal. If that succeeds, the device must
  3593. * not be used until either it is removed or its bus type's .online() callback
  3594. * is executed.
  3595. *
  3596. * Call under device_hotplug_lock.
  3597. */
  3598. int device_offline(struct device *dev)
  3599. {
  3600. int ret;
  3601. if (dev->offline_disabled)
  3602. return -EPERM;
  3603. ret = device_for_each_child(dev, NULL, device_check_offline);
  3604. if (ret)
  3605. return ret;
  3606. device_lock(dev);
  3607. if (device_supports_offline(dev)) {
  3608. if (dev->offline) {
  3609. ret = 1;
  3610. } else {
  3611. ret = dev->bus->offline(dev);
  3612. if (!ret) {
  3613. kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  3614. dev->offline = true;
  3615. }
  3616. }
  3617. }
  3618. device_unlock(dev);
  3619. return ret;
  3620. }
  3621. /**
  3622. * device_online - Put the device back online after successful device_offline().
  3623. * @dev: Device to be put back online.
  3624. *
  3625. * If device_offline() has been successfully executed for @dev, but the device
  3626. * has not been removed subsequently, execute its bus type's .online() callback
  3627. * to indicate that the device can be used again.
  3628. *
  3629. * Call under device_hotplug_lock.
  3630. */
  3631. int device_online(struct device *dev)
  3632. {
  3633. int ret = 0;
  3634. device_lock(dev);
  3635. if (device_supports_offline(dev)) {
  3636. if (dev->offline) {
  3637. ret = dev->bus->online(dev);
  3638. if (!ret) {
  3639. kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  3640. dev->offline = false;
  3641. }
  3642. } else {
  3643. ret = 1;
  3644. }
  3645. }
  3646. device_unlock(dev);
  3647. return ret;
  3648. }
  3649. struct root_device {
  3650. struct device dev;
  3651. struct module *owner;
  3652. };
  3653. static inline struct root_device *to_root_device(struct device *d)
  3654. {
  3655. return container_of(d, struct root_device, dev);
  3656. }
  3657. static void root_device_release(struct device *dev)
  3658. {
  3659. kfree(to_root_device(dev));
  3660. }
  3661. /**
  3662. * __root_device_register - allocate and register a root device
  3663. * @name: root device name
  3664. * @owner: owner module of the root device, usually THIS_MODULE
  3665. *
  3666. * This function allocates a root device and registers it
  3667. * using device_register(). In order to free the returned
  3668. * device, use root_device_unregister().
  3669. *
  3670. * Root devices are dummy devices which allow other devices
  3671. * to be grouped under /sys/devices. Use this function to
  3672. * allocate a root device and then use it as the parent of
  3673. * any device which should appear under /sys/devices/{name}
  3674. *
  3675. * The /sys/devices/{name} directory will also contain a
  3676. * 'module' symlink which points to the @owner directory
  3677. * in sysfs.
  3678. *
  3679. * Returns &struct device pointer on success, or ERR_PTR() on error.
  3680. *
  3681. * Note: You probably want to use root_device_register().
  3682. */
  3683. struct device *__root_device_register(const char *name, struct module *owner)
  3684. {
  3685. struct root_device *root;
  3686. int err = -ENOMEM;
  3687. root = kzalloc_obj(struct root_device);
  3688. if (!root)
  3689. return ERR_PTR(err);
  3690. err = dev_set_name(&root->dev, "%s", name);
  3691. if (err) {
  3692. kfree(root);
  3693. return ERR_PTR(err);
  3694. }
  3695. root->dev.release = root_device_release;
  3696. err = device_register(&root->dev);
  3697. if (err) {
  3698. put_device(&root->dev);
  3699. return ERR_PTR(err);
  3700. }
  3701. #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
  3702. if (owner) {
  3703. struct module_kobject *mk = &owner->mkobj;
  3704. err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
  3705. if (err) {
  3706. device_unregister(&root->dev);
  3707. return ERR_PTR(err);
  3708. }
  3709. root->owner = owner;
  3710. }
  3711. #endif
  3712. return &root->dev;
  3713. }
  3714. EXPORT_SYMBOL_GPL(__root_device_register);
  3715. /**
  3716. * root_device_unregister - unregister and free a root device
  3717. * @dev: device going away
  3718. *
  3719. * This function unregisters and cleans up a device that was created by
  3720. * root_device_register().
  3721. */
  3722. void root_device_unregister(struct device *dev)
  3723. {
  3724. struct root_device *root = to_root_device(dev);
  3725. if (root->owner)
  3726. sysfs_remove_link(&root->dev.kobj, "module");
  3727. device_unregister(dev);
  3728. }
  3729. EXPORT_SYMBOL_GPL(root_device_unregister);
  3730. static void device_create_release(struct device *dev)
  3731. {
  3732. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  3733. kfree(dev);
  3734. }
  3735. static __printf(6, 0) struct device *
  3736. device_create_groups_vargs(const struct class *class, struct device *parent,
  3737. dev_t devt, void *drvdata,
  3738. const struct attribute_group **groups,
  3739. const char *fmt, va_list args)
  3740. {
  3741. struct device *dev = NULL;
  3742. int retval = -ENODEV;
  3743. if (IS_ERR_OR_NULL(class))
  3744. goto error;
  3745. dev = kzalloc_obj(*dev);
  3746. if (!dev) {
  3747. retval = -ENOMEM;
  3748. goto error;
  3749. }
  3750. device_initialize(dev);
  3751. dev->devt = devt;
  3752. dev->class = class;
  3753. dev->parent = parent;
  3754. dev->groups = groups;
  3755. dev->release = device_create_release;
  3756. dev_set_drvdata(dev, drvdata);
  3757. retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
  3758. if (retval)
  3759. goto error;
  3760. retval = device_add(dev);
  3761. if (retval)
  3762. goto error;
  3763. return dev;
  3764. error:
  3765. put_device(dev);
  3766. return ERR_PTR(retval);
  3767. }
  3768. /**
  3769. * device_create - creates a device and registers it with sysfs
  3770. * @class: pointer to the struct class that this device should be registered to
  3771. * @parent: pointer to the parent struct device of this new device, if any
  3772. * @devt: the dev_t for the char device to be added
  3773. * @drvdata: the data to be added to the device for callbacks
  3774. * @fmt: string for the device's name
  3775. *
  3776. * This function can be used by char device classes. A struct device
  3777. * will be created in sysfs, registered to the specified class.
  3778. *
  3779. * A "dev" file will be created, showing the dev_t for the device, if
  3780. * the dev_t is not 0,0.
  3781. * If a pointer to a parent struct device is passed in, the newly created
  3782. * struct device will be a child of that device in sysfs.
  3783. * The pointer to the struct device will be returned from the call.
  3784. * Any further sysfs files that might be required can be created using this
  3785. * pointer.
  3786. *
  3787. * Returns &struct device pointer on success, or ERR_PTR() on error.
  3788. */
  3789. struct device *device_create(const struct class *class, struct device *parent,
  3790. dev_t devt, void *drvdata, const char *fmt, ...)
  3791. {
  3792. va_list vargs;
  3793. struct device *dev;
  3794. va_start(vargs, fmt);
  3795. dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
  3796. fmt, vargs);
  3797. va_end(vargs);
  3798. return dev;
  3799. }
  3800. EXPORT_SYMBOL_GPL(device_create);
  3801. /**
  3802. * device_create_with_groups - creates a device and registers it with sysfs
  3803. * @class: pointer to the struct class that this device should be registered to
  3804. * @parent: pointer to the parent struct device of this new device, if any
  3805. * @devt: the dev_t for the char device to be added
  3806. * @drvdata: the data to be added to the device for callbacks
  3807. * @groups: NULL-terminated list of attribute groups to be created
  3808. * @fmt: string for the device's name
  3809. *
  3810. * This function can be used by char device classes. A struct device
  3811. * will be created in sysfs, registered to the specified class.
  3812. * Additional attributes specified in the groups parameter will also
  3813. * be created automatically.
  3814. *
  3815. * A "dev" file will be created, showing the dev_t for the device, if
  3816. * the dev_t is not 0,0.
  3817. * If a pointer to a parent struct device is passed in, the newly created
  3818. * struct device will be a child of that device in sysfs.
  3819. * The pointer to the struct device will be returned from the call.
  3820. * Any further sysfs files that might be required can be created using this
  3821. * pointer.
  3822. *
  3823. * Returns &struct device pointer on success, or ERR_PTR() on error.
  3824. */
  3825. struct device *device_create_with_groups(const struct class *class,
  3826. struct device *parent, dev_t devt,
  3827. void *drvdata,
  3828. const struct attribute_group **groups,
  3829. const char *fmt, ...)
  3830. {
  3831. va_list vargs;
  3832. struct device *dev;
  3833. va_start(vargs, fmt);
  3834. dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
  3835. fmt, vargs);
  3836. va_end(vargs);
  3837. return dev;
  3838. }
  3839. EXPORT_SYMBOL_GPL(device_create_with_groups);
  3840. /**
  3841. * device_destroy - removes a device that was created with device_create()
  3842. * @class: pointer to the struct class that this device was registered with
  3843. * @devt: the dev_t of the device that was previously registered
  3844. *
  3845. * This call unregisters and cleans up a device that was created with a
  3846. * call to device_create().
  3847. */
  3848. void device_destroy(const struct class *class, dev_t devt)
  3849. {
  3850. struct device *dev;
  3851. dev = class_find_device_by_devt(class, devt);
  3852. if (dev) {
  3853. put_device(dev);
  3854. device_unregister(dev);
  3855. }
  3856. }
  3857. EXPORT_SYMBOL_GPL(device_destroy);
  3858. /**
  3859. * device_rename - renames a device
  3860. * @dev: the pointer to the struct device to be renamed
  3861. * @new_name: the new name of the device
  3862. *
  3863. * It is the responsibility of the caller to provide mutual
  3864. * exclusion between two different calls of device_rename
  3865. * on the same device to ensure that new_name is valid and
  3866. * won't conflict with other devices.
  3867. *
  3868. * Note: given that some subsystems (networking and infiniband) use this
  3869. * function, with no immediate plans for this to change, we cannot assume or
  3870. * require that this function not be called at all.
  3871. *
  3872. * However, if you're writing new code, do not call this function. The following
  3873. * text from Kay Sievers offers some insight:
  3874. *
  3875. * Renaming devices is racy at many levels, symlinks and other stuff are not
  3876. * replaced atomically, and you get a "move" uevent, but it's not easy to
  3877. * connect the event to the old and new device. Device nodes are not renamed at
  3878. * all, there isn't even support for that in the kernel now.
  3879. *
  3880. * In the meantime, during renaming, your target name might be taken by another
  3881. * driver, creating conflicts. Or the old name is taken directly after you
  3882. * renamed it -- then you get events for the same DEVPATH, before you even see
  3883. * the "move" event. It's just a mess, and nothing new should ever rely on
  3884. * kernel device renaming. Besides that, it's not even implemented now for
  3885. * other things than (driver-core wise very simple) network devices.
  3886. *
  3887. * Make up a "real" name in the driver before you register anything, or add
  3888. * some other attributes for userspace to find the device, or use udev to add
  3889. * symlinks -- but never rename kernel devices later, it's a complete mess. We
  3890. * don't even want to get into that and try to implement the missing pieces in
  3891. * the core. We really have other pieces to fix in the driver core mess. :)
  3892. */
  3893. int device_rename(struct device *dev, const char *new_name)
  3894. {
  3895. struct subsys_private *sp = NULL;
  3896. struct kobject *kobj = &dev->kobj;
  3897. char *old_device_name = NULL;
  3898. int error;
  3899. bool is_link_renamed = false;
  3900. dev = get_device(dev);
  3901. if (!dev)
  3902. return -EINVAL;
  3903. dev_dbg(dev, "renaming to %s\n", new_name);
  3904. old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
  3905. if (!old_device_name) {
  3906. error = -ENOMEM;
  3907. goto out;
  3908. }
  3909. if (dev->class) {
  3910. sp = class_to_subsys(dev->class);
  3911. if (!sp) {
  3912. error = -EINVAL;
  3913. goto out;
  3914. }
  3915. error = sysfs_rename_link_ns(&sp->subsys.kobj, kobj, old_device_name,
  3916. new_name, kobject_namespace(kobj));
  3917. if (error)
  3918. goto out;
  3919. is_link_renamed = true;
  3920. }
  3921. error = kobject_rename(kobj, new_name);
  3922. out:
  3923. if (error && is_link_renamed)
  3924. sysfs_rename_link_ns(&sp->subsys.kobj, kobj, new_name,
  3925. old_device_name, kobject_namespace(kobj));
  3926. subsys_put(sp);
  3927. put_device(dev);
  3928. kfree(old_device_name);
  3929. return error;
  3930. }
  3931. EXPORT_SYMBOL_GPL(device_rename);
  3932. static int device_move_class_links(struct device *dev,
  3933. struct device *old_parent,
  3934. struct device *new_parent)
  3935. {
  3936. int error = 0;
  3937. if (old_parent)
  3938. sysfs_remove_link(&dev->kobj, "device");
  3939. if (new_parent)
  3940. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  3941. "device");
  3942. return error;
  3943. }
  3944. /**
  3945. * device_move - moves a device to a new parent
  3946. * @dev: the pointer to the struct device to be moved
  3947. * @new_parent: the new parent of the device (can be NULL)
  3948. * @dpm_order: how to reorder the dpm_list
  3949. */
  3950. int device_move(struct device *dev, struct device *new_parent,
  3951. enum dpm_order dpm_order)
  3952. {
  3953. int error;
  3954. struct device *old_parent;
  3955. struct kobject *new_parent_kobj;
  3956. dev = get_device(dev);
  3957. if (!dev)
  3958. return -EINVAL;
  3959. device_pm_lock();
  3960. new_parent = get_device(new_parent);
  3961. new_parent_kobj = get_device_parent(dev, new_parent);
  3962. if (IS_ERR(new_parent_kobj)) {
  3963. error = PTR_ERR(new_parent_kobj);
  3964. put_device(new_parent);
  3965. goto out;
  3966. }
  3967. pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
  3968. __func__, new_parent ? dev_name(new_parent) : "<NULL>");
  3969. error = kobject_move(&dev->kobj, new_parent_kobj);
  3970. if (error) {
  3971. cleanup_glue_dir(dev, new_parent_kobj);
  3972. put_device(new_parent);
  3973. goto out;
  3974. }
  3975. old_parent = dev->parent;
  3976. dev->parent = new_parent;
  3977. if (old_parent)
  3978. klist_remove(&dev->p->knode_parent);
  3979. if (new_parent) {
  3980. klist_add_tail(&dev->p->knode_parent,
  3981. &new_parent->p->klist_children);
  3982. set_dev_node(dev, dev_to_node(new_parent));
  3983. }
  3984. if (dev->class) {
  3985. error = device_move_class_links(dev, old_parent, new_parent);
  3986. if (error) {
  3987. /* We ignore errors on cleanup since we're hosed anyway... */
  3988. device_move_class_links(dev, new_parent, old_parent);
  3989. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  3990. if (new_parent)
  3991. klist_remove(&dev->p->knode_parent);
  3992. dev->parent = old_parent;
  3993. if (old_parent) {
  3994. klist_add_tail(&dev->p->knode_parent,
  3995. &old_parent->p->klist_children);
  3996. set_dev_node(dev, dev_to_node(old_parent));
  3997. }
  3998. }
  3999. cleanup_glue_dir(dev, new_parent_kobj);
  4000. put_device(new_parent);
  4001. goto out;
  4002. }
  4003. }
  4004. switch (dpm_order) {
  4005. case DPM_ORDER_NONE:
  4006. break;
  4007. case DPM_ORDER_DEV_AFTER_PARENT:
  4008. device_pm_move_after(dev, new_parent);
  4009. devices_kset_move_after(dev, new_parent);
  4010. break;
  4011. case DPM_ORDER_PARENT_BEFORE_DEV:
  4012. device_pm_move_before(new_parent, dev);
  4013. devices_kset_move_before(new_parent, dev);
  4014. break;
  4015. case DPM_ORDER_DEV_LAST:
  4016. device_pm_move_last(dev);
  4017. devices_kset_move_last(dev);
  4018. break;
  4019. }
  4020. put_device(old_parent);
  4021. out:
  4022. device_pm_unlock();
  4023. put_device(dev);
  4024. return error;
  4025. }
  4026. EXPORT_SYMBOL_GPL(device_move);
  4027. static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
  4028. kgid_t kgid)
  4029. {
  4030. struct kobject *kobj = &dev->kobj;
  4031. const struct class *class = dev->class;
  4032. const struct device_type *type = dev->type;
  4033. int error;
  4034. if (class) {
  4035. /*
  4036. * Change the device groups of the device class for @dev to
  4037. * @kuid/@kgid.
  4038. */
  4039. error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
  4040. kgid);
  4041. if (error)
  4042. return error;
  4043. }
  4044. if (type) {
  4045. /*
  4046. * Change the device groups of the device type for @dev to
  4047. * @kuid/@kgid.
  4048. */
  4049. error = sysfs_groups_change_owner(kobj, type->groups, kuid,
  4050. kgid);
  4051. if (error)
  4052. return error;
  4053. }
  4054. /* Change the device groups of @dev to @kuid/@kgid. */
  4055. error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
  4056. if (error)
  4057. return error;
  4058. if (device_supports_offline(dev) && !dev->offline_disabled) {
  4059. /* Change online device attributes of @dev to @kuid/@kgid. */
  4060. error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
  4061. kuid, kgid);
  4062. if (error)
  4063. return error;
  4064. }
  4065. return 0;
  4066. }
  4067. /**
  4068. * device_change_owner - change the owner of an existing device.
  4069. * @dev: device.
  4070. * @kuid: new owner's kuid
  4071. * @kgid: new owner's kgid
  4072. *
  4073. * This changes the owner of @dev and its corresponding sysfs entries to
  4074. * @kuid/@kgid. This function closely mirrors how @dev was added via driver
  4075. * core.
  4076. *
  4077. * Returns 0 on success or error code on failure.
  4078. */
  4079. int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
  4080. {
  4081. int error;
  4082. struct kobject *kobj = &dev->kobj;
  4083. struct subsys_private *sp;
  4084. dev = get_device(dev);
  4085. if (!dev)
  4086. return -EINVAL;
  4087. /*
  4088. * Change the kobject and the default attributes and groups of the
  4089. * ktype associated with it to @kuid/@kgid.
  4090. */
  4091. error = sysfs_change_owner(kobj, kuid, kgid);
  4092. if (error)
  4093. goto out;
  4094. /*
  4095. * Change the uevent file for @dev to the new owner. The uevent file
  4096. * was created in a separate step when @dev got added and we mirror
  4097. * that step here.
  4098. */
  4099. error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
  4100. kgid);
  4101. if (error)
  4102. goto out;
  4103. /*
  4104. * Change the device groups, the device groups associated with the
  4105. * device class, and the groups associated with the device type of @dev
  4106. * to @kuid/@kgid.
  4107. */
  4108. error = device_attrs_change_owner(dev, kuid, kgid);
  4109. if (error)
  4110. goto out;
  4111. error = dpm_sysfs_change_owner(dev, kuid, kgid);
  4112. if (error)
  4113. goto out;
  4114. /*
  4115. * Change the owner of the symlink located in the class directory of
  4116. * the device class associated with @dev which points to the actual
  4117. * directory entry for @dev to @kuid/@kgid. This ensures that the
  4118. * symlink shows the same permissions as its target.
  4119. */
  4120. sp = class_to_subsys(dev->class);
  4121. if (!sp) {
  4122. error = -EINVAL;
  4123. goto out;
  4124. }
  4125. error = sysfs_link_change_owner(&sp->subsys.kobj, &dev->kobj, dev_name(dev), kuid, kgid);
  4126. subsys_put(sp);
  4127. out:
  4128. put_device(dev);
  4129. return error;
  4130. }
  4131. /**
  4132. * device_shutdown - call ->shutdown() on each device to shutdown.
  4133. */
  4134. void device_shutdown(void)
  4135. {
  4136. struct device *dev, *parent;
  4137. wait_for_device_probe();
  4138. device_block_probing();
  4139. cpufreq_suspend();
  4140. spin_lock(&devices_kset->list_lock);
  4141. /*
  4142. * Walk the devices list backward, shutting down each in turn.
  4143. * Beware that device unplug events may also start pulling
  4144. * devices offline, even as the system is shutting down.
  4145. */
  4146. while (!list_empty(&devices_kset->list)) {
  4147. dev = list_entry(devices_kset->list.prev, struct device,
  4148. kobj.entry);
  4149. /*
  4150. * hold reference count of device's parent to
  4151. * prevent it from being freed because parent's
  4152. * lock is to be held
  4153. */
  4154. parent = get_device(dev->parent);
  4155. get_device(dev);
  4156. /*
  4157. * Make sure the device is off the kset list, in the
  4158. * event that dev->*->shutdown() doesn't remove it.
  4159. */
  4160. list_del_init(&dev->kobj.entry);
  4161. spin_unlock(&devices_kset->list_lock);
  4162. /* hold lock to avoid race with probe/release */
  4163. if (parent)
  4164. device_lock(parent);
  4165. device_lock(dev);
  4166. /* Don't allow any more runtime suspends */
  4167. pm_runtime_get_noresume(dev);
  4168. pm_runtime_barrier(dev);
  4169. if (dev->class && dev->class->shutdown_pre) {
  4170. if (initcall_debug)
  4171. dev_info(dev, "shutdown_pre\n");
  4172. dev->class->shutdown_pre(dev);
  4173. }
  4174. if (dev->bus && dev->bus->shutdown) {
  4175. if (initcall_debug)
  4176. dev_info(dev, "shutdown\n");
  4177. dev->bus->shutdown(dev);
  4178. } else if (dev->driver && dev->driver->shutdown) {
  4179. if (initcall_debug)
  4180. dev_info(dev, "shutdown\n");
  4181. dev->driver->shutdown(dev);
  4182. }
  4183. device_unlock(dev);
  4184. if (parent)
  4185. device_unlock(parent);
  4186. put_device(dev);
  4187. put_device(parent);
  4188. spin_lock(&devices_kset->list_lock);
  4189. }
  4190. spin_unlock(&devices_kset->list_lock);
  4191. }
  4192. /*
  4193. * Device logging functions
  4194. */
  4195. #ifdef CONFIG_PRINTK
  4196. static void
  4197. set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
  4198. {
  4199. const char *subsys;
  4200. memset(dev_info, 0, sizeof(*dev_info));
  4201. if (dev->class)
  4202. subsys = dev->class->name;
  4203. else if (dev->bus)
  4204. subsys = dev->bus->name;
  4205. else
  4206. return;
  4207. strscpy(dev_info->subsystem, subsys);
  4208. /*
  4209. * Add device identifier DEVICE=:
  4210. * b12:8 block dev_t
  4211. * c127:3 char dev_t
  4212. * n8 netdev ifindex
  4213. * +sound:card0 subsystem:devname
  4214. */
  4215. if (MAJOR(dev->devt)) {
  4216. char c;
  4217. if (strcmp(subsys, "block") == 0)
  4218. c = 'b';
  4219. else
  4220. c = 'c';
  4221. snprintf(dev_info->device, sizeof(dev_info->device),
  4222. "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
  4223. } else if (strcmp(subsys, "net") == 0) {
  4224. struct net_device *net = to_net_dev(dev);
  4225. snprintf(dev_info->device, sizeof(dev_info->device),
  4226. "n%u", net->ifindex);
  4227. } else {
  4228. snprintf(dev_info->device, sizeof(dev_info->device),
  4229. "+%s:%s", subsys, dev_name(dev));
  4230. }
  4231. }
  4232. int dev_vprintk_emit(int level, const struct device *dev,
  4233. const char *fmt, va_list args)
  4234. {
  4235. struct dev_printk_info dev_info;
  4236. set_dev_info(dev, &dev_info);
  4237. return vprintk_emit(0, level, &dev_info, fmt, args);
  4238. }
  4239. EXPORT_SYMBOL(dev_vprintk_emit);
  4240. int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
  4241. {
  4242. va_list args;
  4243. int r;
  4244. va_start(args, fmt);
  4245. r = dev_vprintk_emit(level, dev, fmt, args);
  4246. va_end(args);
  4247. return r;
  4248. }
  4249. EXPORT_SYMBOL(dev_printk_emit);
  4250. static void __dev_printk(const char *level, const struct device *dev,
  4251. struct va_format *vaf)
  4252. {
  4253. if (dev)
  4254. dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
  4255. dev_driver_string(dev), dev_name(dev), vaf);
  4256. else
  4257. printk("%s(NULL device *): %pV", level, vaf);
  4258. }
  4259. void _dev_printk(const char *level, const struct device *dev,
  4260. const char *fmt, ...)
  4261. {
  4262. struct va_format vaf;
  4263. va_list args;
  4264. va_start(args, fmt);
  4265. vaf.fmt = fmt;
  4266. vaf.va = &args;
  4267. __dev_printk(level, dev, &vaf);
  4268. va_end(args);
  4269. }
  4270. EXPORT_SYMBOL(_dev_printk);
  4271. #define define_dev_printk_level(func, kern_level) \
  4272. void func(const struct device *dev, const char *fmt, ...) \
  4273. { \
  4274. struct va_format vaf; \
  4275. va_list args; \
  4276. \
  4277. va_start(args, fmt); \
  4278. \
  4279. vaf.fmt = fmt; \
  4280. vaf.va = &args; \
  4281. \
  4282. __dev_printk(kern_level, dev, &vaf); \
  4283. \
  4284. va_end(args); \
  4285. } \
  4286. EXPORT_SYMBOL(func);
  4287. define_dev_printk_level(_dev_emerg, KERN_EMERG);
  4288. define_dev_printk_level(_dev_alert, KERN_ALERT);
  4289. define_dev_printk_level(_dev_crit, KERN_CRIT);
  4290. define_dev_printk_level(_dev_err, KERN_ERR);
  4291. define_dev_printk_level(_dev_warn, KERN_WARNING);
  4292. define_dev_printk_level(_dev_notice, KERN_NOTICE);
  4293. define_dev_printk_level(_dev_info, KERN_INFO);
  4294. #endif
  4295. static void __dev_probe_failed(const struct device *dev, int err, bool fatal,
  4296. const char *fmt, va_list vargsp)
  4297. {
  4298. struct va_format vaf;
  4299. va_list vargs;
  4300. /*
  4301. * On x86_64 and possibly on other architectures, va_list is actually a
  4302. * size-1 array containing a structure. As a result, function parameter
  4303. * vargsp decays from T[1] to T*, and &vargsp has type T** rather than
  4304. * T(*)[1], which is expected by its assignment to vaf.va below.
  4305. *
  4306. * One standard way to solve this mess is by creating a copy in a local
  4307. * variable of type va_list and then using a pointer to that local copy
  4308. * instead, which is the approach employed here.
  4309. */
  4310. va_copy(vargs, vargsp);
  4311. vaf.fmt = fmt;
  4312. vaf.va = &vargs;
  4313. switch (err) {
  4314. case -EPROBE_DEFER:
  4315. device_set_deferred_probe_reason(dev, &vaf);
  4316. dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
  4317. break;
  4318. case -ENOMEM:
  4319. /* Don't print anything on -ENOMEM, there's already enough output */
  4320. break;
  4321. default:
  4322. /* Log fatal final failures as errors, otherwise produce warnings */
  4323. if (fatal)
  4324. dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
  4325. else
  4326. dev_warn(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
  4327. break;
  4328. }
  4329. va_end(vargs);
  4330. }
  4331. /**
  4332. * dev_err_probe - probe error check and log helper
  4333. * @dev: the pointer to the struct device
  4334. * @err: error value to test
  4335. * @fmt: printf-style format string
  4336. * @...: arguments as specified in the format string
  4337. *
  4338. * This helper implements common pattern present in probe functions for error
  4339. * checking: print debug or error message depending if the error value is
  4340. * -EPROBE_DEFER and propagate error upwards.
  4341. * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
  4342. * checked later by reading devices_deferred debugfs attribute.
  4343. * It replaces the following code sequence::
  4344. *
  4345. * if (err != -EPROBE_DEFER)
  4346. * dev_err(dev, ...);
  4347. * else
  4348. * dev_dbg(dev, ...);
  4349. * return err;
  4350. *
  4351. * with::
  4352. *
  4353. * return dev_err_probe(dev, err, ...);
  4354. *
  4355. * Using this helper in your probe function is totally fine even if @err
  4356. * is known to never be -EPROBE_DEFER.
  4357. * The benefit compared to a normal dev_err() is the standardized format
  4358. * of the error code, which is emitted symbolically (i.e. you get "EAGAIN"
  4359. * instead of "-35"), and having the error code returned allows more
  4360. * compact error paths.
  4361. *
  4362. * Returns @err.
  4363. */
  4364. int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
  4365. {
  4366. va_list vargs;
  4367. va_start(vargs, fmt);
  4368. /* Use dev_err() for logging when err doesn't equal -EPROBE_DEFER */
  4369. __dev_probe_failed(dev, err, true, fmt, vargs);
  4370. va_end(vargs);
  4371. return err;
  4372. }
  4373. EXPORT_SYMBOL_GPL(dev_err_probe);
  4374. /**
  4375. * dev_warn_probe - probe error check and log helper
  4376. * @dev: the pointer to the struct device
  4377. * @err: error value to test
  4378. * @fmt: printf-style format string
  4379. * @...: arguments as specified in the format string
  4380. *
  4381. * This helper implements common pattern present in probe functions for error
  4382. * checking: print debug or warning message depending if the error value is
  4383. * -EPROBE_DEFER and propagate error upwards.
  4384. * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
  4385. * checked later by reading devices_deferred debugfs attribute.
  4386. * It replaces the following code sequence::
  4387. *
  4388. * if (err != -EPROBE_DEFER)
  4389. * dev_warn(dev, ...);
  4390. * else
  4391. * dev_dbg(dev, ...);
  4392. * return err;
  4393. *
  4394. * with::
  4395. *
  4396. * return dev_warn_probe(dev, err, ...);
  4397. *
  4398. * Using this helper in your probe function is totally fine even if @err
  4399. * is known to never be -EPROBE_DEFER.
  4400. * The benefit compared to a normal dev_warn() is the standardized format
  4401. * of the error code, which is emitted symbolically (i.e. you get "EAGAIN"
  4402. * instead of "-35"), and having the error code returned allows more
  4403. * compact error paths.
  4404. *
  4405. * Returns @err.
  4406. */
  4407. int dev_warn_probe(const struct device *dev, int err, const char *fmt, ...)
  4408. {
  4409. va_list vargs;
  4410. va_start(vargs, fmt);
  4411. /* Use dev_warn() for logging when err doesn't equal -EPROBE_DEFER */
  4412. __dev_probe_failed(dev, err, false, fmt, vargs);
  4413. va_end(vargs);
  4414. return err;
  4415. }
  4416. EXPORT_SYMBOL_GPL(dev_warn_probe);
  4417. static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
  4418. {
  4419. return fwnode && !IS_ERR(fwnode->secondary);
  4420. }
  4421. /**
  4422. * set_primary_fwnode - Change the primary firmware node of a given device.
  4423. * @dev: Device to handle.
  4424. * @fwnode: New primary firmware node of the device.
  4425. *
  4426. * Set the device's firmware node pointer to @fwnode, but if a secondary
  4427. * firmware node of the device is present, preserve it.
  4428. *
  4429. * Valid fwnode cases are:
  4430. * - primary --> secondary --> -ENODEV
  4431. * - primary --> NULL
  4432. * - secondary --> -ENODEV
  4433. * - NULL
  4434. */
  4435. void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
  4436. {
  4437. struct device *parent = dev->parent;
  4438. struct fwnode_handle *fn = dev->fwnode;
  4439. if (fwnode) {
  4440. if (fwnode_is_primary(fn))
  4441. fn = fn->secondary;
  4442. if (fn) {
  4443. WARN_ON(fwnode->secondary);
  4444. fwnode->secondary = fn;
  4445. }
  4446. dev->fwnode = fwnode;
  4447. } else {
  4448. if (fwnode_is_primary(fn)) {
  4449. dev->fwnode = fn->secondary;
  4450. /* Skip nullifying fn->secondary if the primary is shared */
  4451. if (parent && fn == parent->fwnode)
  4452. return;
  4453. /* Set fn->secondary = NULL, so fn remains the primary fwnode */
  4454. fn->secondary = NULL;
  4455. } else {
  4456. dev->fwnode = NULL;
  4457. }
  4458. }
  4459. }
  4460. EXPORT_SYMBOL_GPL(set_primary_fwnode);
  4461. /**
  4462. * set_secondary_fwnode - Change the secondary firmware node of a given device.
  4463. * @dev: Device to handle.
  4464. * @fwnode: New secondary firmware node of the device.
  4465. *
  4466. * If a primary firmware node of the device is present, set its secondary
  4467. * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
  4468. * @fwnode.
  4469. */
  4470. void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
  4471. {
  4472. if (fwnode)
  4473. fwnode->secondary = ERR_PTR(-ENODEV);
  4474. if (fwnode_is_primary(dev->fwnode))
  4475. dev->fwnode->secondary = fwnode;
  4476. else
  4477. dev->fwnode = fwnode;
  4478. }
  4479. EXPORT_SYMBOL_GPL(set_secondary_fwnode);
  4480. /**
  4481. * device_remove_of_node - Remove an of_node from a device
  4482. * @dev: device whose device tree node is being removed
  4483. */
  4484. void device_remove_of_node(struct device *dev)
  4485. {
  4486. dev = get_device(dev);
  4487. if (!dev)
  4488. return;
  4489. if (!dev->of_node)
  4490. goto end;
  4491. if (dev->fwnode == of_fwnode_handle(dev->of_node))
  4492. dev->fwnode = NULL;
  4493. of_node_put(dev->of_node);
  4494. dev->of_node = NULL;
  4495. end:
  4496. put_device(dev);
  4497. }
  4498. EXPORT_SYMBOL_GPL(device_remove_of_node);
  4499. /**
  4500. * device_add_of_node - Add an of_node to an existing device
  4501. * @dev: device whose device tree node is being added
  4502. * @of_node: of_node to add
  4503. *
  4504. * Return: 0 on success or error code on failure.
  4505. */
  4506. int device_add_of_node(struct device *dev, struct device_node *of_node)
  4507. {
  4508. int ret;
  4509. if (!of_node)
  4510. return -EINVAL;
  4511. dev = get_device(dev);
  4512. if (!dev)
  4513. return -EINVAL;
  4514. if (dev->of_node) {
  4515. dev_err(dev, "Cannot replace node %pOF with %pOF\n",
  4516. dev->of_node, of_node);
  4517. ret = -EBUSY;
  4518. goto end;
  4519. }
  4520. dev->of_node = of_node_get(of_node);
  4521. if (!dev->fwnode)
  4522. dev->fwnode = of_fwnode_handle(of_node);
  4523. ret = 0;
  4524. end:
  4525. put_device(dev);
  4526. return ret;
  4527. }
  4528. EXPORT_SYMBOL_GPL(device_add_of_node);
  4529. /**
  4530. * device_set_of_node_from_dev - reuse device-tree node of another device
  4531. * @dev: device whose device-tree node is being set
  4532. * @dev2: device whose device-tree node is being reused
  4533. *
  4534. * Takes another reference to the new device-tree node after first dropping
  4535. * any reference held to the old node.
  4536. */
  4537. void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
  4538. {
  4539. of_node_put(dev->of_node);
  4540. dev->of_node = of_node_get(dev2->of_node);
  4541. dev->of_node_reused = true;
  4542. }
  4543. EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
  4544. void device_set_node(struct device *dev, struct fwnode_handle *fwnode)
  4545. {
  4546. dev->fwnode = fwnode;
  4547. dev->of_node = to_of_node(fwnode);
  4548. }
  4549. EXPORT_SYMBOL_GPL(device_set_node);
  4550. /**
  4551. * get_dev_from_fwnode - Obtain a reference count of the struct device the
  4552. * struct fwnode_handle is associated with.
  4553. * @fwnode: The pointer to the struct fwnode_handle to obtain the struct device
  4554. * reference count of.
  4555. *
  4556. * This function obtains a reference count of the device the device pointer
  4557. * embedded in the struct fwnode_handle points to.
  4558. *
  4559. * Note that the struct device pointer embedded in struct fwnode_handle does
  4560. * *not* have a reference count of the struct device itself.
  4561. *
  4562. * Hence, it is a UAF (and thus a bug) to call this function if the caller can't
  4563. * guarantee that the last reference count of the corresponding struct device is
  4564. * not dropped concurrently.
  4565. *
  4566. * This is possible since struct fwnode_handle has its own reference count and
  4567. * hence can out-live the struct device it is associated with.
  4568. */
  4569. struct device *get_dev_from_fwnode(struct fwnode_handle *fwnode)
  4570. {
  4571. return get_device((fwnode)->dev);
  4572. }
  4573. EXPORT_SYMBOL_GPL(get_dev_from_fwnode);
  4574. int device_match_name(struct device *dev, const void *name)
  4575. {
  4576. return sysfs_streq(dev_name(dev), name);
  4577. }
  4578. EXPORT_SYMBOL_GPL(device_match_name);
  4579. int device_match_type(struct device *dev, const void *type)
  4580. {
  4581. return dev->type == type;
  4582. }
  4583. EXPORT_SYMBOL_GPL(device_match_type);
  4584. int device_match_of_node(struct device *dev, const void *np)
  4585. {
  4586. return np && dev->of_node == np;
  4587. }
  4588. EXPORT_SYMBOL_GPL(device_match_of_node);
  4589. int device_match_fwnode(struct device *dev, const void *fwnode)
  4590. {
  4591. return fwnode && dev_fwnode(dev) == fwnode;
  4592. }
  4593. EXPORT_SYMBOL_GPL(device_match_fwnode);
  4594. int device_match_devt(struct device *dev, const void *pdevt)
  4595. {
  4596. return dev->devt == *(dev_t *)pdevt;
  4597. }
  4598. EXPORT_SYMBOL_GPL(device_match_devt);
  4599. int device_match_acpi_dev(struct device *dev, const void *adev)
  4600. {
  4601. return adev && ACPI_COMPANION(dev) == adev;
  4602. }
  4603. EXPORT_SYMBOL(device_match_acpi_dev);
  4604. int device_match_acpi_handle(struct device *dev, const void *handle)
  4605. {
  4606. return handle && ACPI_HANDLE(dev) == handle;
  4607. }
  4608. EXPORT_SYMBOL(device_match_acpi_handle);
  4609. int device_match_any(struct device *dev, const void *unused)
  4610. {
  4611. return 1;
  4612. }
  4613. EXPORT_SYMBOL_GPL(device_match_any);