core.c 141 KB

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