core.c 141 KB

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