core.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/base/core.c - core driver model code (device registration, etc)
  4. *
  5. * Copyright (c) 2002-3 Patrick Mochel
  6. * Copyright (c) 2002-3 Open Source Development Labs
  7. * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
  8. * Copyright (c) 2006 Novell, Inc.
  9. */
  10. #include <linux/cpufreq.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/fwnode.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/kdev_t.h>
  19. #include <linux/notifier.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/genhd.h>
  23. #include <linux/mutex.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/sched/signal.h>
  27. #include <linux/sysfs.h>
  28. #include "base.h"
  29. #include "power/power.h"
  30. #ifdef CONFIG_SYSFS_DEPRECATED
  31. #ifdef CONFIG_SYSFS_DEPRECATED_V2
  32. long sysfs_deprecated = 1;
  33. #else
  34. long sysfs_deprecated = 0;
  35. #endif
  36. static int __init sysfs_deprecated_setup(char *arg)
  37. {
  38. return kstrtol(arg, 10, &sysfs_deprecated);
  39. }
  40. early_param("sysfs.deprecated", sysfs_deprecated_setup);
  41. #endif
  42. /* Device links support. */
  43. #ifdef CONFIG_SRCU
  44. static DEFINE_MUTEX(device_links_lock);
  45. DEFINE_STATIC_SRCU(device_links_srcu);
  46. static inline void device_links_write_lock(void)
  47. {
  48. mutex_lock(&device_links_lock);
  49. }
  50. static inline void device_links_write_unlock(void)
  51. {
  52. mutex_unlock(&device_links_lock);
  53. }
  54. int device_links_read_lock(void)
  55. {
  56. return srcu_read_lock(&device_links_srcu);
  57. }
  58. void device_links_read_unlock(int idx)
  59. {
  60. srcu_read_unlock(&device_links_srcu, idx);
  61. }
  62. #else /* !CONFIG_SRCU */
  63. static DECLARE_RWSEM(device_links_lock);
  64. static inline void device_links_write_lock(void)
  65. {
  66. down_write(&device_links_lock);
  67. }
  68. static inline void device_links_write_unlock(void)
  69. {
  70. up_write(&device_links_lock);
  71. }
  72. int device_links_read_lock(void)
  73. {
  74. down_read(&device_links_lock);
  75. return 0;
  76. }
  77. void device_links_read_unlock(int not_used)
  78. {
  79. up_read(&device_links_lock);
  80. }
  81. #endif /* !CONFIG_SRCU */
  82. /**
  83. * device_is_dependent - Check if one device depends on another one
  84. * @dev: Device to check dependencies for.
  85. * @target: Device to check against.
  86. *
  87. * Check if @target depends on @dev or any device dependent on it (its child or
  88. * its consumer etc). Return 1 if that is the case or 0 otherwise.
  89. */
  90. static int device_is_dependent(struct device *dev, void *target)
  91. {
  92. struct device_link *link;
  93. int ret;
  94. if (dev == target)
  95. return 1;
  96. ret = device_for_each_child(dev, target, device_is_dependent);
  97. if (ret)
  98. return ret;
  99. list_for_each_entry(link, &dev->links.consumers, s_node) {
  100. if (link->consumer == target)
  101. return 1;
  102. ret = device_is_dependent(link->consumer, target);
  103. if (ret)
  104. break;
  105. }
  106. return ret;
  107. }
  108. static void device_link_init_status(struct device_link *link,
  109. struct device *consumer,
  110. struct device *supplier)
  111. {
  112. switch (supplier->links.status) {
  113. case DL_DEV_PROBING:
  114. switch (consumer->links.status) {
  115. case DL_DEV_PROBING:
  116. /*
  117. * A consumer driver can create a link to a supplier
  118. * that has not completed its probing yet as long as it
  119. * knows that the supplier is already functional (for
  120. * example, it has just acquired some resources from the
  121. * supplier).
  122. */
  123. link->status = DL_STATE_CONSUMER_PROBE;
  124. break;
  125. default:
  126. link->status = DL_STATE_DORMANT;
  127. break;
  128. }
  129. break;
  130. case DL_DEV_DRIVER_BOUND:
  131. switch (consumer->links.status) {
  132. case DL_DEV_PROBING:
  133. link->status = DL_STATE_CONSUMER_PROBE;
  134. break;
  135. case DL_DEV_DRIVER_BOUND:
  136. link->status = DL_STATE_ACTIVE;
  137. break;
  138. default:
  139. link->status = DL_STATE_AVAILABLE;
  140. break;
  141. }
  142. break;
  143. case DL_DEV_UNBINDING:
  144. link->status = DL_STATE_SUPPLIER_UNBIND;
  145. break;
  146. default:
  147. link->status = DL_STATE_DORMANT;
  148. break;
  149. }
  150. }
  151. static int device_reorder_to_tail(struct device *dev, void *not_used)
  152. {
  153. struct device_link *link;
  154. /*
  155. * Devices that have not been registered yet will be put to the ends
  156. * of the lists during the registration, so skip them here.
  157. */
  158. if (device_is_registered(dev))
  159. devices_kset_move_last(dev);
  160. if (device_pm_initialized(dev))
  161. device_pm_move_last(dev);
  162. device_for_each_child(dev, NULL, device_reorder_to_tail);
  163. list_for_each_entry(link, &dev->links.consumers, s_node)
  164. device_reorder_to_tail(link->consumer, NULL);
  165. return 0;
  166. }
  167. /**
  168. * device_pm_move_to_tail - Move set of devices to the end of device lists
  169. * @dev: Device to move
  170. *
  171. * This is a device_reorder_to_tail() wrapper taking the requisite locks.
  172. *
  173. * It moves the @dev along with all of its children and all of its consumers
  174. * to the ends of the device_kset and dpm_list, recursively.
  175. */
  176. void device_pm_move_to_tail(struct device *dev)
  177. {
  178. int idx;
  179. idx = device_links_read_lock();
  180. device_pm_lock();
  181. device_reorder_to_tail(dev, NULL);
  182. device_pm_unlock();
  183. device_links_read_unlock(idx);
  184. }
  185. #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
  186. DL_FLAG_AUTOREMOVE_SUPPLIER | \
  187. DL_FLAG_AUTOPROBE_CONSUMER)
  188. #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
  189. DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
  190. /**
  191. * device_link_add - Create a link between two devices.
  192. * @consumer: Consumer end of the link.
  193. * @supplier: Supplier end of the link.
  194. * @flags: Link flags.
  195. *
  196. * The caller is responsible for the proper synchronization of the link creation
  197. * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
  198. * runtime PM framework to take the link into account. Second, if the
  199. * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
  200. * be forced into the active metastate and reference-counted upon the creation
  201. * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
  202. * ignored.
  203. *
  204. * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
  205. * expected to release the link returned by it directly with the help of either
  206. * device_link_del() or device_link_remove().
  207. *
  208. * If that flag is not set, however, the caller of this function is handing the
  209. * management of the link over to the driver core entirely and its return value
  210. * can only be used to check whether or not the link is present. In that case,
  211. * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
  212. * flags can be used to indicate to the driver core when the link can be safely
  213. * deleted. Namely, setting one of them in @flags indicates to the driver core
  214. * that the link is not going to be used (by the given caller of this function)
  215. * after unbinding the consumer or supplier driver, respectively, from its
  216. * device, so the link can be deleted at that point. If none of them is set,
  217. * the link will be maintained until one of the devices pointed to by it (either
  218. * the consumer or the supplier) is unregistered.
  219. *
  220. * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
  221. * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
  222. * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
  223. * be used to request the driver core to automaticall probe for a consmer
  224. * driver after successfully binding a driver to the supplier device.
  225. *
  226. * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
  227. * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
  228. * the same time is invalid and will cause NULL to be returned upfront.
  229. * However, if a device link between the given @consumer and @supplier pair
  230. * exists already when this function is called for them, the existing link will
  231. * be returned regardless of its current type and status (the link's flags may
  232. * be modified then). The caller of this function is then expected to treat
  233. * the link as though it has just been created, so (in particular) if
  234. * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
  235. * explicitly when not needed any more (as stated above).
  236. *
  237. * A side effect of the link creation is re-ordering of dpm_list and the
  238. * devices_kset list by moving the consumer device and all devices depending
  239. * on it to the ends of these lists (that does not happen to devices that have
  240. * not been registered when this function is called).
  241. *
  242. * The supplier device is required to be registered when this function is called
  243. * and NULL will be returned if that is not the case. The consumer device need
  244. * not be registered, however.
  245. */
  246. struct device_link *device_link_add(struct device *consumer,
  247. struct device *supplier, u32 flags)
  248. {
  249. struct device_link *link;
  250. if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
  251. (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
  252. (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
  253. flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
  254. DL_FLAG_AUTOREMOVE_SUPPLIER)))
  255. return NULL;
  256. if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
  257. if (pm_runtime_get_sync(supplier) < 0) {
  258. pm_runtime_put_noidle(supplier);
  259. return NULL;
  260. }
  261. }
  262. if (!(flags & DL_FLAG_STATELESS))
  263. flags |= DL_FLAG_MANAGED;
  264. device_links_write_lock();
  265. device_pm_lock();
  266. /*
  267. * If the supplier has not been fully registered yet or there is a
  268. * reverse dependency between the consumer and the supplier already in
  269. * the graph, return NULL.
  270. */
  271. if (!device_pm_initialized(supplier)
  272. || device_is_dependent(consumer, supplier)) {
  273. link = NULL;
  274. goto out;
  275. }
  276. /*
  277. * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
  278. * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
  279. * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
  280. */
  281. if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
  282. flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
  283. list_for_each_entry(link, &supplier->links.consumers, s_node) {
  284. if (link->consumer != consumer)
  285. continue;
  286. if (flags & DL_FLAG_PM_RUNTIME) {
  287. if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
  288. pm_runtime_new_link(consumer);
  289. link->flags |= DL_FLAG_PM_RUNTIME;
  290. }
  291. if (flags & DL_FLAG_RPM_ACTIVE)
  292. refcount_inc(&link->rpm_active);
  293. }
  294. if (flags & DL_FLAG_STATELESS) {
  295. link->flags |= DL_FLAG_STATELESS;
  296. kref_get(&link->kref);
  297. goto out;
  298. }
  299. /*
  300. * If the life time of the link following from the new flags is
  301. * longer than indicated by the flags of the existing link,
  302. * update the existing link to stay around longer.
  303. */
  304. if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
  305. if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
  306. link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
  307. link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
  308. }
  309. } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
  310. link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
  311. DL_FLAG_AUTOREMOVE_SUPPLIER);
  312. }
  313. if (!(link->flags & DL_FLAG_MANAGED)) {
  314. kref_get(&link->kref);
  315. link->flags |= DL_FLAG_MANAGED;
  316. device_link_init_status(link, consumer, supplier);
  317. }
  318. goto out;
  319. }
  320. link = kzalloc(sizeof(*link), GFP_KERNEL);
  321. if (!link)
  322. goto out;
  323. refcount_set(&link->rpm_active, 1);
  324. if (flags & DL_FLAG_PM_RUNTIME) {
  325. if (flags & DL_FLAG_RPM_ACTIVE)
  326. refcount_inc(&link->rpm_active);
  327. pm_runtime_new_link(consumer);
  328. }
  329. get_device(supplier);
  330. link->supplier = supplier;
  331. INIT_LIST_HEAD(&link->s_node);
  332. get_device(consumer);
  333. link->consumer = consumer;
  334. INIT_LIST_HEAD(&link->c_node);
  335. link->flags = flags;
  336. kref_init(&link->kref);
  337. /* Determine the initial link state. */
  338. if (flags & DL_FLAG_STATELESS)
  339. link->status = DL_STATE_NONE;
  340. else
  341. device_link_init_status(link, consumer, supplier);
  342. /*
  343. * Some callers expect the link creation during consumer driver probe to
  344. * resume the supplier even without DL_FLAG_RPM_ACTIVE.
  345. */
  346. if (link->status == DL_STATE_CONSUMER_PROBE &&
  347. flags & DL_FLAG_PM_RUNTIME)
  348. pm_runtime_resume(supplier);
  349. /*
  350. * Move the consumer and all of the devices depending on it to the end
  351. * of dpm_list and the devices_kset list.
  352. *
  353. * It is necessary to hold dpm_list locked throughout all that or else
  354. * we may end up suspending with a wrong ordering of it.
  355. */
  356. device_reorder_to_tail(consumer, NULL);
  357. list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
  358. list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
  359. dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
  360. out:
  361. device_pm_unlock();
  362. device_links_write_unlock();
  363. if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
  364. pm_runtime_put(supplier);
  365. return link;
  366. }
  367. EXPORT_SYMBOL_GPL(device_link_add);
  368. static void device_link_free(struct device_link *link)
  369. {
  370. while (refcount_dec_not_one(&link->rpm_active))
  371. pm_runtime_put(link->supplier);
  372. put_device(link->consumer);
  373. put_device(link->supplier);
  374. kfree(link);
  375. }
  376. #ifdef CONFIG_SRCU
  377. static void __device_link_free_srcu(struct rcu_head *rhead)
  378. {
  379. device_link_free(container_of(rhead, struct device_link, rcu_head));
  380. }
  381. static void __device_link_del(struct kref *kref)
  382. {
  383. struct device_link *link = container_of(kref, struct device_link, kref);
  384. dev_info(link->consumer, "Dropping the link to %s\n",
  385. dev_name(link->supplier));
  386. if (link->flags & DL_FLAG_PM_RUNTIME)
  387. pm_runtime_drop_link(link->consumer);
  388. list_del_rcu(&link->s_node);
  389. list_del_rcu(&link->c_node);
  390. call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
  391. }
  392. #else /* !CONFIG_SRCU */
  393. static void __device_link_del(struct kref *kref)
  394. {
  395. struct device_link *link = container_of(kref, struct device_link, kref);
  396. dev_info(link->consumer, "Dropping the link to %s\n",
  397. dev_name(link->supplier));
  398. if (link->flags & DL_FLAG_PM_RUNTIME)
  399. pm_runtime_drop_link(link->consumer);
  400. list_del(&link->s_node);
  401. list_del(&link->c_node);
  402. device_link_free(link);
  403. }
  404. #endif /* !CONFIG_SRCU */
  405. static void device_link_put_kref(struct device_link *link)
  406. {
  407. if (link->flags & DL_FLAG_STATELESS)
  408. kref_put(&link->kref, __device_link_del);
  409. else
  410. WARN(1, "Unable to drop a managed device link reference\n");
  411. }
  412. /**
  413. * device_link_del - Delete a stateless link between two devices.
  414. * @link: Device link to delete.
  415. *
  416. * The caller must ensure proper synchronization of this function with runtime
  417. * PM. If the link was added multiple times, it needs to be deleted as often.
  418. * Care is required for hotplugged devices: Their links are purged on removal
  419. * and calling device_link_del() is then no longer allowed.
  420. */
  421. void device_link_del(struct device_link *link)
  422. {
  423. device_links_write_lock();
  424. device_pm_lock();
  425. device_link_put_kref(link);
  426. device_pm_unlock();
  427. device_links_write_unlock();
  428. }
  429. EXPORT_SYMBOL_GPL(device_link_del);
  430. /**
  431. * device_link_remove - Delete a stateless link between two devices.
  432. * @consumer: Consumer end of the link.
  433. * @supplier: Supplier end of the link.
  434. *
  435. * The caller must ensure proper synchronization of this function with runtime
  436. * PM.
  437. */
  438. void device_link_remove(void *consumer, struct device *supplier)
  439. {
  440. struct device_link *link;
  441. if (WARN_ON(consumer == supplier))
  442. return;
  443. device_links_write_lock();
  444. device_pm_lock();
  445. list_for_each_entry(link, &supplier->links.consumers, s_node) {
  446. if (link->consumer == consumer) {
  447. device_link_put_kref(link);
  448. break;
  449. }
  450. }
  451. device_pm_unlock();
  452. device_links_write_unlock();
  453. }
  454. EXPORT_SYMBOL_GPL(device_link_remove);
  455. static void device_links_missing_supplier(struct device *dev)
  456. {
  457. struct device_link *link;
  458. list_for_each_entry(link, &dev->links.suppliers, c_node)
  459. if (link->status == DL_STATE_CONSUMER_PROBE)
  460. WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
  461. }
  462. /**
  463. * device_links_check_suppliers - Check presence of supplier drivers.
  464. * @dev: Consumer device.
  465. *
  466. * Check links from this device to any suppliers. Walk the list of the device's
  467. * links to suppliers and see if all of them are available. If not, simply
  468. * return -EPROBE_DEFER.
  469. *
  470. * We need to guarantee that the supplier will not go away after the check has
  471. * been positive here. It only can go away in __device_release_driver() and
  472. * that function checks the device's links to consumers. This means we need to
  473. * mark the link as "consumer probe in progress" to make the supplier removal
  474. * wait for us to complete (or bad things may happen).
  475. *
  476. * Links without the DL_FLAG_MANAGED flag set are ignored.
  477. */
  478. int device_links_check_suppliers(struct device *dev)
  479. {
  480. struct device_link *link;
  481. int ret = 0;
  482. device_links_write_lock();
  483. list_for_each_entry(link, &dev->links.suppliers, c_node) {
  484. if (!(link->flags & DL_FLAG_MANAGED))
  485. continue;
  486. if (link->status != DL_STATE_AVAILABLE) {
  487. device_links_missing_supplier(dev);
  488. ret = -EPROBE_DEFER;
  489. break;
  490. }
  491. WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
  492. }
  493. dev->links.status = DL_DEV_PROBING;
  494. device_links_write_unlock();
  495. return ret;
  496. }
  497. /**
  498. * device_links_driver_bound - Update device links after probing its driver.
  499. * @dev: Device to update the links for.
  500. *
  501. * The probe has been successful, so update links from this device to any
  502. * consumers by changing their status to "available".
  503. *
  504. * Also change the status of @dev's links to suppliers to "active".
  505. *
  506. * Links without the DL_FLAG_MANAGED flag set are ignored.
  507. */
  508. void device_links_driver_bound(struct device *dev)
  509. {
  510. struct device_link *link;
  511. device_links_write_lock();
  512. list_for_each_entry(link, &dev->links.consumers, s_node) {
  513. if (!(link->flags & DL_FLAG_MANAGED))
  514. continue;
  515. /*
  516. * Links created during consumer probe may be in the "consumer
  517. * probe" state to start with if the supplier is still probing
  518. * when they are created and they may become "active" if the
  519. * consumer probe returns first. Skip them here.
  520. */
  521. if (link->status == DL_STATE_CONSUMER_PROBE ||
  522. link->status == DL_STATE_ACTIVE)
  523. continue;
  524. WARN_ON(link->status != DL_STATE_DORMANT);
  525. WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
  526. if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
  527. driver_deferred_probe_add(link->consumer);
  528. }
  529. list_for_each_entry(link, &dev->links.suppliers, c_node) {
  530. if (!(link->flags & DL_FLAG_MANAGED))
  531. continue;
  532. WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
  533. WRITE_ONCE(link->status, DL_STATE_ACTIVE);
  534. }
  535. dev->links.status = DL_DEV_DRIVER_BOUND;
  536. device_links_write_unlock();
  537. }
  538. static void device_link_drop_managed(struct device_link *link)
  539. {
  540. link->flags &= ~DL_FLAG_MANAGED;
  541. WRITE_ONCE(link->status, DL_STATE_NONE);
  542. kref_put(&link->kref, __device_link_del);
  543. }
  544. /**
  545. * __device_links_no_driver - Update links of a device without a driver.
  546. * @dev: Device without a drvier.
  547. *
  548. * Delete all non-persistent links from this device to any suppliers.
  549. *
  550. * Persistent links stay around, but their status is changed to "available",
  551. * unless they already are in the "supplier unbind in progress" state in which
  552. * case they need not be updated.
  553. *
  554. * Links without the DL_FLAG_MANAGED flag set are ignored.
  555. */
  556. static void __device_links_no_driver(struct device *dev)
  557. {
  558. struct device_link *link, *ln;
  559. list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
  560. if (!(link->flags & DL_FLAG_MANAGED))
  561. continue;
  562. if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
  563. device_link_drop_managed(link);
  564. else if (link->status == DL_STATE_CONSUMER_PROBE ||
  565. link->status == DL_STATE_ACTIVE)
  566. WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
  567. }
  568. dev->links.status = DL_DEV_NO_DRIVER;
  569. }
  570. /**
  571. * device_links_no_driver - Update links after failing driver probe.
  572. * @dev: Device whose driver has just failed to probe.
  573. *
  574. * Clean up leftover links to consumers for @dev and invoke
  575. * %__device_links_no_driver() to update links to suppliers for it as
  576. * appropriate.
  577. *
  578. * Links without the DL_FLAG_MANAGED flag set are ignored.
  579. */
  580. void device_links_no_driver(struct device *dev)
  581. {
  582. struct device_link *link;
  583. device_links_write_lock();
  584. list_for_each_entry(link, &dev->links.consumers, s_node) {
  585. if (!(link->flags & DL_FLAG_MANAGED))
  586. continue;
  587. /*
  588. * The probe has failed, so if the status of the link is
  589. * "consumer probe" or "active", it must have been added by
  590. * a probing consumer while this device was still probing.
  591. * Change its state to "dormant", as it represents a valid
  592. * relationship, but it is not functionally meaningful.
  593. */
  594. if (link->status == DL_STATE_CONSUMER_PROBE ||
  595. link->status == DL_STATE_ACTIVE)
  596. WRITE_ONCE(link->status, DL_STATE_DORMANT);
  597. }
  598. __device_links_no_driver(dev);
  599. device_links_write_unlock();
  600. }
  601. /**
  602. * device_links_driver_cleanup - Update links after driver removal.
  603. * @dev: Device whose driver has just gone away.
  604. *
  605. * Update links to consumers for @dev by changing their status to "dormant" and
  606. * invoke %__device_links_no_driver() to update links to suppliers for it as
  607. * appropriate.
  608. *
  609. * Links without the DL_FLAG_MANAGED flag set are ignored.
  610. */
  611. void device_links_driver_cleanup(struct device *dev)
  612. {
  613. struct device_link *link, *ln;
  614. device_links_write_lock();
  615. list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
  616. if (!(link->flags & DL_FLAG_MANAGED))
  617. continue;
  618. WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
  619. WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
  620. /*
  621. * autoremove the links between this @dev and its consumer
  622. * devices that are not active, i.e. where the link state
  623. * has moved to DL_STATE_SUPPLIER_UNBIND.
  624. */
  625. if (link->status == DL_STATE_SUPPLIER_UNBIND &&
  626. link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
  627. device_link_drop_managed(link);
  628. WRITE_ONCE(link->status, DL_STATE_DORMANT);
  629. }
  630. __device_links_no_driver(dev);
  631. device_links_write_unlock();
  632. }
  633. /**
  634. * device_links_busy - Check if there are any busy links to consumers.
  635. * @dev: Device to check.
  636. *
  637. * Check each consumer of the device and return 'true' if its link's status
  638. * is one of "consumer probe" or "active" (meaning that the given consumer is
  639. * probing right now or its driver is present). Otherwise, change the link
  640. * state to "supplier unbind" to prevent the consumer from being probed
  641. * successfully going forward.
  642. *
  643. * Return 'false' if there are no probing or active consumers.
  644. *
  645. * Links without the DL_FLAG_MANAGED flag set are ignored.
  646. */
  647. bool device_links_busy(struct device *dev)
  648. {
  649. struct device_link *link;
  650. bool ret = false;
  651. device_links_write_lock();
  652. list_for_each_entry(link, &dev->links.consumers, s_node) {
  653. if (!(link->flags & DL_FLAG_MANAGED))
  654. continue;
  655. if (link->status == DL_STATE_CONSUMER_PROBE
  656. || link->status == DL_STATE_ACTIVE) {
  657. ret = true;
  658. break;
  659. }
  660. WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
  661. }
  662. dev->links.status = DL_DEV_UNBINDING;
  663. device_links_write_unlock();
  664. return ret;
  665. }
  666. /**
  667. * device_links_unbind_consumers - Force unbind consumers of the given device.
  668. * @dev: Device to unbind the consumers of.
  669. *
  670. * Walk the list of links to consumers for @dev and if any of them is in the
  671. * "consumer probe" state, wait for all device probes in progress to complete
  672. * and start over.
  673. *
  674. * If that's not the case, change the status of the link to "supplier unbind"
  675. * and check if the link was in the "active" state. If so, force the consumer
  676. * driver to unbind and start over (the consumer will not re-probe as we have
  677. * changed the state of the link already).
  678. *
  679. * Links without the DL_FLAG_MANAGED flag set are ignored.
  680. */
  681. void device_links_unbind_consumers(struct device *dev)
  682. {
  683. struct device_link *link;
  684. start:
  685. device_links_write_lock();
  686. list_for_each_entry(link, &dev->links.consumers, s_node) {
  687. enum device_link_state status;
  688. if (!(link->flags & DL_FLAG_MANAGED))
  689. continue;
  690. status = link->status;
  691. if (status == DL_STATE_CONSUMER_PROBE) {
  692. device_links_write_unlock();
  693. wait_for_device_probe();
  694. goto start;
  695. }
  696. WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
  697. if (status == DL_STATE_ACTIVE) {
  698. struct device *consumer = link->consumer;
  699. get_device(consumer);
  700. device_links_write_unlock();
  701. device_release_driver_internal(consumer, NULL,
  702. consumer->parent);
  703. put_device(consumer);
  704. goto start;
  705. }
  706. }
  707. device_links_write_unlock();
  708. }
  709. /**
  710. * device_links_purge - Delete existing links to other devices.
  711. * @dev: Target device.
  712. */
  713. static void device_links_purge(struct device *dev)
  714. {
  715. struct device_link *link, *ln;
  716. /*
  717. * Delete all of the remaining links from this device to any other
  718. * devices (either consumers or suppliers).
  719. */
  720. device_links_write_lock();
  721. list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
  722. WARN_ON(link->status == DL_STATE_ACTIVE);
  723. __device_link_del(&link->kref);
  724. }
  725. list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
  726. WARN_ON(link->status != DL_STATE_DORMANT &&
  727. link->status != DL_STATE_NONE);
  728. __device_link_del(&link->kref);
  729. }
  730. device_links_write_unlock();
  731. }
  732. /* Device links support end. */
  733. int (*platform_notify)(struct device *dev) = NULL;
  734. int (*platform_notify_remove)(struct device *dev) = NULL;
  735. static struct kobject *dev_kobj;
  736. struct kobject *sysfs_dev_char_kobj;
  737. struct kobject *sysfs_dev_block_kobj;
  738. static DEFINE_MUTEX(device_hotplug_lock);
  739. void lock_device_hotplug(void)
  740. {
  741. mutex_lock(&device_hotplug_lock);
  742. }
  743. void unlock_device_hotplug(void)
  744. {
  745. mutex_unlock(&device_hotplug_lock);
  746. }
  747. int lock_device_hotplug_sysfs(void)
  748. {
  749. if (mutex_trylock(&device_hotplug_lock))
  750. return 0;
  751. /* Avoid busy looping (5 ms of sleep should do). */
  752. msleep(5);
  753. return restart_syscall();
  754. }
  755. #ifdef CONFIG_BLOCK
  756. static inline int device_is_not_partition(struct device *dev)
  757. {
  758. return !(dev->type == &part_type);
  759. }
  760. #else
  761. static inline int device_is_not_partition(struct device *dev)
  762. {
  763. return 1;
  764. }
  765. #endif
  766. /**
  767. * dev_driver_string - Return a device's driver name, if at all possible
  768. * @dev: struct device to get the name of
  769. *
  770. * Will return the device's driver's name if it is bound to a device. If
  771. * the device is not bound to a driver, it will return the name of the bus
  772. * it is attached to. If it is not attached to a bus either, an empty
  773. * string will be returned.
  774. */
  775. const char *dev_driver_string(const struct device *dev)
  776. {
  777. struct device_driver *drv;
  778. /* dev->driver can change to NULL underneath us because of unbinding,
  779. * so be careful about accessing it. dev->bus and dev->class should
  780. * never change once they are set, so they don't need special care.
  781. */
  782. drv = READ_ONCE(dev->driver);
  783. return drv ? drv->name :
  784. (dev->bus ? dev->bus->name :
  785. (dev->class ? dev->class->name : ""));
  786. }
  787. EXPORT_SYMBOL(dev_driver_string);
  788. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  789. static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
  790. char *buf)
  791. {
  792. struct device_attribute *dev_attr = to_dev_attr(attr);
  793. struct device *dev = kobj_to_dev(kobj);
  794. ssize_t ret = -EIO;
  795. if (dev_attr->show)
  796. ret = dev_attr->show(dev, dev_attr, buf);
  797. if (ret >= (ssize_t)PAGE_SIZE) {
  798. printk("dev_attr_show: %pS returned bad count\n",
  799. dev_attr->show);
  800. }
  801. return ret;
  802. }
  803. static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
  804. const char *buf, size_t count)
  805. {
  806. struct device_attribute *dev_attr = to_dev_attr(attr);
  807. struct device *dev = kobj_to_dev(kobj);
  808. ssize_t ret = -EIO;
  809. if (dev_attr->store)
  810. ret = dev_attr->store(dev, dev_attr, buf, count);
  811. return ret;
  812. }
  813. static const struct sysfs_ops dev_sysfs_ops = {
  814. .show = dev_attr_show,
  815. .store = dev_attr_store,
  816. };
  817. #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
  818. ssize_t device_store_ulong(struct device *dev,
  819. struct device_attribute *attr,
  820. const char *buf, size_t size)
  821. {
  822. struct dev_ext_attribute *ea = to_ext_attr(attr);
  823. char *end;
  824. unsigned long new = simple_strtoul(buf, &end, 0);
  825. if (end == buf)
  826. return -EINVAL;
  827. *(unsigned long *)(ea->var) = new;
  828. /* Always return full write size even if we didn't consume all */
  829. return size;
  830. }
  831. EXPORT_SYMBOL_GPL(device_store_ulong);
  832. ssize_t device_show_ulong(struct device *dev,
  833. struct device_attribute *attr,
  834. char *buf)
  835. {
  836. struct dev_ext_attribute *ea = to_ext_attr(attr);
  837. return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
  838. }
  839. EXPORT_SYMBOL_GPL(device_show_ulong);
  840. ssize_t device_store_int(struct device *dev,
  841. struct device_attribute *attr,
  842. const char *buf, size_t size)
  843. {
  844. struct dev_ext_attribute *ea = to_ext_attr(attr);
  845. char *end;
  846. long new = simple_strtol(buf, &end, 0);
  847. if (end == buf || new > INT_MAX || new < INT_MIN)
  848. return -EINVAL;
  849. *(int *)(ea->var) = new;
  850. /* Always return full write size even if we didn't consume all */
  851. return size;
  852. }
  853. EXPORT_SYMBOL_GPL(device_store_int);
  854. ssize_t device_show_int(struct device *dev,
  855. struct device_attribute *attr,
  856. char *buf)
  857. {
  858. struct dev_ext_attribute *ea = to_ext_attr(attr);
  859. return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
  860. }
  861. EXPORT_SYMBOL_GPL(device_show_int);
  862. ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
  863. const char *buf, size_t size)
  864. {
  865. struct dev_ext_attribute *ea = to_ext_attr(attr);
  866. if (strtobool(buf, ea->var) < 0)
  867. return -EINVAL;
  868. return size;
  869. }
  870. EXPORT_SYMBOL_GPL(device_store_bool);
  871. ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
  872. char *buf)
  873. {
  874. struct dev_ext_attribute *ea = to_ext_attr(attr);
  875. return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
  876. }
  877. EXPORT_SYMBOL_GPL(device_show_bool);
  878. /**
  879. * device_release - free device structure.
  880. * @kobj: device's kobject.
  881. *
  882. * This is called once the reference count for the object
  883. * reaches 0. We forward the call to the device's release
  884. * method, which should handle actually freeing the structure.
  885. */
  886. static void device_release(struct kobject *kobj)
  887. {
  888. struct device *dev = kobj_to_dev(kobj);
  889. struct device_private *p = dev->p;
  890. /*
  891. * Some platform devices are driven without driver attached
  892. * and managed resources may have been acquired. Make sure
  893. * all resources are released.
  894. *
  895. * Drivers still can add resources into device after device
  896. * is deleted but alive, so release devres here to avoid
  897. * possible memory leak.
  898. */
  899. devres_release_all(dev);
  900. if (dev->release)
  901. dev->release(dev);
  902. else if (dev->type && dev->type->release)
  903. dev->type->release(dev);
  904. else if (dev->class && dev->class->dev_release)
  905. dev->class->dev_release(dev);
  906. else
  907. WARN(1, KERN_ERR "Device '%s' does not have a release() "
  908. "function, it is broken and must be fixed.\n",
  909. dev_name(dev));
  910. kfree(p);
  911. }
  912. static const void *device_namespace(struct kobject *kobj)
  913. {
  914. struct device *dev = kobj_to_dev(kobj);
  915. const void *ns = NULL;
  916. if (dev->class && dev->class->ns_type)
  917. ns = dev->class->namespace(dev);
  918. return ns;
  919. }
  920. static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
  921. {
  922. struct device *dev = kobj_to_dev(kobj);
  923. if (dev->class && dev->class->get_ownership)
  924. dev->class->get_ownership(dev, uid, gid);
  925. }
  926. static struct kobj_type device_ktype = {
  927. .release = device_release,
  928. .sysfs_ops = &dev_sysfs_ops,
  929. .namespace = device_namespace,
  930. .get_ownership = device_get_ownership,
  931. };
  932. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  933. {
  934. struct kobj_type *ktype = get_ktype(kobj);
  935. if (ktype == &device_ktype) {
  936. struct device *dev = kobj_to_dev(kobj);
  937. if (dev->bus)
  938. return 1;
  939. if (dev->class)
  940. return 1;
  941. }
  942. return 0;
  943. }
  944. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  945. {
  946. struct device *dev = kobj_to_dev(kobj);
  947. if (dev->bus)
  948. return dev->bus->name;
  949. if (dev->class)
  950. return dev->class->name;
  951. return NULL;
  952. }
  953. static int dev_uevent(struct kset *kset, struct kobject *kobj,
  954. struct kobj_uevent_env *env)
  955. {
  956. struct device *dev = kobj_to_dev(kobj);
  957. int retval = 0;
  958. /* add device node properties if present */
  959. if (MAJOR(dev->devt)) {
  960. const char *tmp;
  961. const char *name;
  962. umode_t mode = 0;
  963. kuid_t uid = GLOBAL_ROOT_UID;
  964. kgid_t gid = GLOBAL_ROOT_GID;
  965. add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
  966. add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
  967. name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
  968. if (name) {
  969. add_uevent_var(env, "DEVNAME=%s", name);
  970. if (mode)
  971. add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
  972. if (!uid_eq(uid, GLOBAL_ROOT_UID))
  973. add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
  974. if (!gid_eq(gid, GLOBAL_ROOT_GID))
  975. add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
  976. kfree(tmp);
  977. }
  978. }
  979. if (dev->type && dev->type->name)
  980. add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
  981. if (dev->driver)
  982. add_uevent_var(env, "DRIVER=%s", dev->driver->name);
  983. /* Add common DT information about the device */
  984. of_device_uevent(dev, env);
  985. /* have the bus specific function add its stuff */
  986. if (dev->bus && dev->bus->uevent) {
  987. retval = dev->bus->uevent(dev, env);
  988. if (retval)
  989. pr_debug("device: '%s': %s: bus uevent() returned %d\n",
  990. dev_name(dev), __func__, retval);
  991. }
  992. /* have the class specific function add its stuff */
  993. if (dev->class && dev->class->dev_uevent) {
  994. retval = dev->class->dev_uevent(dev, env);
  995. if (retval)
  996. pr_debug("device: '%s': %s: class uevent() "
  997. "returned %d\n", dev_name(dev),
  998. __func__, retval);
  999. }
  1000. /* have the device type specific function add its stuff */
  1001. if (dev->type && dev->type->uevent) {
  1002. retval = dev->type->uevent(dev, env);
  1003. if (retval)
  1004. pr_debug("device: '%s': %s: dev_type uevent() "
  1005. "returned %d\n", dev_name(dev),
  1006. __func__, retval);
  1007. }
  1008. return retval;
  1009. }
  1010. static const struct kset_uevent_ops device_uevent_ops = {
  1011. .filter = dev_uevent_filter,
  1012. .name = dev_uevent_name,
  1013. .uevent = dev_uevent,
  1014. };
  1015. static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
  1016. char *buf)
  1017. {
  1018. struct kobject *top_kobj;
  1019. struct kset *kset;
  1020. struct kobj_uevent_env *env = NULL;
  1021. int i;
  1022. size_t count = 0;
  1023. int retval;
  1024. /* search the kset, the device belongs to */
  1025. top_kobj = &dev->kobj;
  1026. while (!top_kobj->kset && top_kobj->parent)
  1027. top_kobj = top_kobj->parent;
  1028. if (!top_kobj->kset)
  1029. goto out;
  1030. kset = top_kobj->kset;
  1031. if (!kset->uevent_ops || !kset->uevent_ops->uevent)
  1032. goto out;
  1033. /* respect filter */
  1034. if (kset->uevent_ops && kset->uevent_ops->filter)
  1035. if (!kset->uevent_ops->filter(kset, &dev->kobj))
  1036. goto out;
  1037. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  1038. if (!env)
  1039. return -ENOMEM;
  1040. /* let the kset specific function add its keys */
  1041. retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
  1042. if (retval)
  1043. goto out;
  1044. /* copy keys to file */
  1045. for (i = 0; i < env->envp_idx; i++)
  1046. count += sprintf(&buf[count], "%s\n", env->envp[i]);
  1047. out:
  1048. kfree(env);
  1049. return count;
  1050. }
  1051. static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
  1052. const char *buf, size_t count)
  1053. {
  1054. int rc;
  1055. rc = kobject_synth_uevent(&dev->kobj, buf, count);
  1056. if (rc) {
  1057. dev_err(dev, "uevent: failed to send synthetic uevent\n");
  1058. return rc;
  1059. }
  1060. return count;
  1061. }
  1062. static DEVICE_ATTR_RW(uevent);
  1063. static ssize_t online_show(struct device *dev, struct device_attribute *attr,
  1064. char *buf)
  1065. {
  1066. bool val;
  1067. device_lock(dev);
  1068. val = !dev->offline;
  1069. device_unlock(dev);
  1070. return sprintf(buf, "%u\n", val);
  1071. }
  1072. static ssize_t online_store(struct device *dev, struct device_attribute *attr,
  1073. const char *buf, size_t count)
  1074. {
  1075. bool val;
  1076. int ret;
  1077. ret = strtobool(buf, &val);
  1078. if (ret < 0)
  1079. return ret;
  1080. ret = lock_device_hotplug_sysfs();
  1081. if (ret)
  1082. return ret;
  1083. ret = val ? device_online(dev) : device_offline(dev);
  1084. unlock_device_hotplug();
  1085. return ret < 0 ? ret : count;
  1086. }
  1087. static DEVICE_ATTR_RW(online);
  1088. int device_add_groups(struct device *dev, const struct attribute_group **groups)
  1089. {
  1090. return sysfs_create_groups(&dev->kobj, groups);
  1091. }
  1092. EXPORT_SYMBOL_GPL(device_add_groups);
  1093. void device_remove_groups(struct device *dev,
  1094. const struct attribute_group **groups)
  1095. {
  1096. sysfs_remove_groups(&dev->kobj, groups);
  1097. }
  1098. EXPORT_SYMBOL_GPL(device_remove_groups);
  1099. union device_attr_group_devres {
  1100. const struct attribute_group *group;
  1101. const struct attribute_group **groups;
  1102. };
  1103. static int devm_attr_group_match(struct device *dev, void *res, void *data)
  1104. {
  1105. return ((union device_attr_group_devres *)res)->group == data;
  1106. }
  1107. static void devm_attr_group_remove(struct device *dev, void *res)
  1108. {
  1109. union device_attr_group_devres *devres = res;
  1110. const struct attribute_group *group = devres->group;
  1111. dev_dbg(dev, "%s: removing group %p\n", __func__, group);
  1112. sysfs_remove_group(&dev->kobj, group);
  1113. }
  1114. static void devm_attr_groups_remove(struct device *dev, void *res)
  1115. {
  1116. union device_attr_group_devres *devres = res;
  1117. const struct attribute_group **groups = devres->groups;
  1118. dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
  1119. sysfs_remove_groups(&dev->kobj, groups);
  1120. }
  1121. /**
  1122. * devm_device_add_group - given a device, create a managed attribute group
  1123. * @dev: The device to create the group for
  1124. * @grp: The attribute group to create
  1125. *
  1126. * This function creates a group for the first time. It will explicitly
  1127. * warn and error if any of the attribute files being created already exist.
  1128. *
  1129. * Returns 0 on success or error code on failure.
  1130. */
  1131. int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
  1132. {
  1133. union device_attr_group_devres *devres;
  1134. int error;
  1135. devres = devres_alloc(devm_attr_group_remove,
  1136. sizeof(*devres), GFP_KERNEL);
  1137. if (!devres)
  1138. return -ENOMEM;
  1139. error = sysfs_create_group(&dev->kobj, grp);
  1140. if (error) {
  1141. devres_free(devres);
  1142. return error;
  1143. }
  1144. devres->group = grp;
  1145. devres_add(dev, devres);
  1146. return 0;
  1147. }
  1148. EXPORT_SYMBOL_GPL(devm_device_add_group);
  1149. /**
  1150. * devm_device_remove_group: remove a managed group from a device
  1151. * @dev: device to remove the group from
  1152. * @grp: group to remove
  1153. *
  1154. * This function removes a group of attributes from a device. The attributes
  1155. * previously have to have been created for this group, otherwise it will fail.
  1156. */
  1157. void devm_device_remove_group(struct device *dev,
  1158. const struct attribute_group *grp)
  1159. {
  1160. WARN_ON(devres_release(dev, devm_attr_group_remove,
  1161. devm_attr_group_match,
  1162. /* cast away const */ (void *)grp));
  1163. }
  1164. EXPORT_SYMBOL_GPL(devm_device_remove_group);
  1165. /**
  1166. * devm_device_add_groups - create a bunch of managed attribute groups
  1167. * @dev: The device to create the group for
  1168. * @groups: The attribute groups to create, NULL terminated
  1169. *
  1170. * This function creates a bunch of managed attribute groups. If an error
  1171. * occurs when creating a group, all previously created groups will be
  1172. * removed, unwinding everything back to the original state when this
  1173. * function was called. It will explicitly warn and error if any of the
  1174. * attribute files being created already exist.
  1175. *
  1176. * Returns 0 on success or error code from sysfs_create_group on failure.
  1177. */
  1178. int devm_device_add_groups(struct device *dev,
  1179. const struct attribute_group **groups)
  1180. {
  1181. union device_attr_group_devres *devres;
  1182. int error;
  1183. devres = devres_alloc(devm_attr_groups_remove,
  1184. sizeof(*devres), GFP_KERNEL);
  1185. if (!devres)
  1186. return -ENOMEM;
  1187. error = sysfs_create_groups(&dev->kobj, groups);
  1188. if (error) {
  1189. devres_free(devres);
  1190. return error;
  1191. }
  1192. devres->groups = groups;
  1193. devres_add(dev, devres);
  1194. return 0;
  1195. }
  1196. EXPORT_SYMBOL_GPL(devm_device_add_groups);
  1197. /**
  1198. * devm_device_remove_groups - remove a list of managed groups
  1199. *
  1200. * @dev: The device for the groups to be removed from
  1201. * @groups: NULL terminated list of groups to be removed
  1202. *
  1203. * If groups is not NULL, remove the specified groups from the device.
  1204. */
  1205. void devm_device_remove_groups(struct device *dev,
  1206. const struct attribute_group **groups)
  1207. {
  1208. WARN_ON(devres_release(dev, devm_attr_groups_remove,
  1209. devm_attr_group_match,
  1210. /* cast away const */ (void *)groups));
  1211. }
  1212. EXPORT_SYMBOL_GPL(devm_device_remove_groups);
  1213. static int device_add_attrs(struct device *dev)
  1214. {
  1215. struct class *class = dev->class;
  1216. const struct device_type *type = dev->type;
  1217. int error;
  1218. if (class) {
  1219. error = device_add_groups(dev, class->dev_groups);
  1220. if (error)
  1221. return error;
  1222. }
  1223. if (type) {
  1224. error = device_add_groups(dev, type->groups);
  1225. if (error)
  1226. goto err_remove_class_groups;
  1227. }
  1228. error = device_add_groups(dev, dev->groups);
  1229. if (error)
  1230. goto err_remove_type_groups;
  1231. if (device_supports_offline(dev) && !dev->offline_disabled) {
  1232. error = device_create_file(dev, &dev_attr_online);
  1233. if (error)
  1234. goto err_remove_dev_groups;
  1235. }
  1236. return 0;
  1237. err_remove_dev_groups:
  1238. device_remove_groups(dev, dev->groups);
  1239. err_remove_type_groups:
  1240. if (type)
  1241. device_remove_groups(dev, type->groups);
  1242. err_remove_class_groups:
  1243. if (class)
  1244. device_remove_groups(dev, class->dev_groups);
  1245. return error;
  1246. }
  1247. static void device_remove_attrs(struct device *dev)
  1248. {
  1249. struct class *class = dev->class;
  1250. const struct device_type *type = dev->type;
  1251. device_remove_file(dev, &dev_attr_online);
  1252. device_remove_groups(dev, dev->groups);
  1253. if (type)
  1254. device_remove_groups(dev, type->groups);
  1255. if (class)
  1256. device_remove_groups(dev, class->dev_groups);
  1257. }
  1258. static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
  1259. char *buf)
  1260. {
  1261. return print_dev_t(buf, dev->devt);
  1262. }
  1263. static DEVICE_ATTR_RO(dev);
  1264. /* /sys/devices/ */
  1265. struct kset *devices_kset;
  1266. /**
  1267. * devices_kset_move_before - Move device in the devices_kset's list.
  1268. * @deva: Device to move.
  1269. * @devb: Device @deva should come before.
  1270. */
  1271. static void devices_kset_move_before(struct device *deva, struct device *devb)
  1272. {
  1273. if (!devices_kset)
  1274. return;
  1275. pr_debug("devices_kset: Moving %s before %s\n",
  1276. dev_name(deva), dev_name(devb));
  1277. spin_lock(&devices_kset->list_lock);
  1278. list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
  1279. spin_unlock(&devices_kset->list_lock);
  1280. }
  1281. /**
  1282. * devices_kset_move_after - Move device in the devices_kset's list.
  1283. * @deva: Device to move
  1284. * @devb: Device @deva should come after.
  1285. */
  1286. static void devices_kset_move_after(struct device *deva, struct device *devb)
  1287. {
  1288. if (!devices_kset)
  1289. return;
  1290. pr_debug("devices_kset: Moving %s after %s\n",
  1291. dev_name(deva), dev_name(devb));
  1292. spin_lock(&devices_kset->list_lock);
  1293. list_move(&deva->kobj.entry, &devb->kobj.entry);
  1294. spin_unlock(&devices_kset->list_lock);
  1295. }
  1296. /**
  1297. * devices_kset_move_last - move the device to the end of devices_kset's list.
  1298. * @dev: device to move
  1299. */
  1300. void devices_kset_move_last(struct device *dev)
  1301. {
  1302. if (!devices_kset)
  1303. return;
  1304. pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
  1305. spin_lock(&devices_kset->list_lock);
  1306. list_move_tail(&dev->kobj.entry, &devices_kset->list);
  1307. spin_unlock(&devices_kset->list_lock);
  1308. }
  1309. /**
  1310. * device_create_file - create sysfs attribute file for device.
  1311. * @dev: device.
  1312. * @attr: device attribute descriptor.
  1313. */
  1314. int device_create_file(struct device *dev,
  1315. const struct device_attribute *attr)
  1316. {
  1317. int error = 0;
  1318. if (dev) {
  1319. WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
  1320. "Attribute %s: write permission without 'store'\n",
  1321. attr->attr.name);
  1322. WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
  1323. "Attribute %s: read permission without 'show'\n",
  1324. attr->attr.name);
  1325. error = sysfs_create_file(&dev->kobj, &attr->attr);
  1326. }
  1327. return error;
  1328. }
  1329. EXPORT_SYMBOL_GPL(device_create_file);
  1330. /**
  1331. * device_remove_file - remove sysfs attribute file.
  1332. * @dev: device.
  1333. * @attr: device attribute descriptor.
  1334. */
  1335. void device_remove_file(struct device *dev,
  1336. const struct device_attribute *attr)
  1337. {
  1338. if (dev)
  1339. sysfs_remove_file(&dev->kobj, &attr->attr);
  1340. }
  1341. EXPORT_SYMBOL_GPL(device_remove_file);
  1342. /**
  1343. * device_remove_file_self - remove sysfs attribute file from its own method.
  1344. * @dev: device.
  1345. * @attr: device attribute descriptor.
  1346. *
  1347. * See kernfs_remove_self() for details.
  1348. */
  1349. bool device_remove_file_self(struct device *dev,
  1350. const struct device_attribute *attr)
  1351. {
  1352. if (dev)
  1353. return sysfs_remove_file_self(&dev->kobj, &attr->attr);
  1354. else
  1355. return false;
  1356. }
  1357. EXPORT_SYMBOL_GPL(device_remove_file_self);
  1358. /**
  1359. * device_create_bin_file - create sysfs binary attribute file for device.
  1360. * @dev: device.
  1361. * @attr: device binary attribute descriptor.
  1362. */
  1363. int device_create_bin_file(struct device *dev,
  1364. const struct bin_attribute *attr)
  1365. {
  1366. int error = -EINVAL;
  1367. if (dev)
  1368. error = sysfs_create_bin_file(&dev->kobj, attr);
  1369. return error;
  1370. }
  1371. EXPORT_SYMBOL_GPL(device_create_bin_file);
  1372. /**
  1373. * device_remove_bin_file - remove sysfs binary attribute file
  1374. * @dev: device.
  1375. * @attr: device binary attribute descriptor.
  1376. */
  1377. void device_remove_bin_file(struct device *dev,
  1378. const struct bin_attribute *attr)
  1379. {
  1380. if (dev)
  1381. sysfs_remove_bin_file(&dev->kobj, attr);
  1382. }
  1383. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  1384. static void klist_children_get(struct klist_node *n)
  1385. {
  1386. struct device_private *p = to_device_private_parent(n);
  1387. struct device *dev = p->device;
  1388. get_device(dev);
  1389. }
  1390. static void klist_children_put(struct klist_node *n)
  1391. {
  1392. struct device_private *p = to_device_private_parent(n);
  1393. struct device *dev = p->device;
  1394. put_device(dev);
  1395. }
  1396. /**
  1397. * device_initialize - init device structure.
  1398. * @dev: device.
  1399. *
  1400. * This prepares the device for use by other layers by initializing
  1401. * its fields.
  1402. * It is the first half of device_register(), if called by
  1403. * that function, though it can also be called separately, so one
  1404. * may use @dev's fields. In particular, get_device()/put_device()
  1405. * may be used for reference counting of @dev after calling this
  1406. * function.
  1407. *
  1408. * All fields in @dev must be initialized by the caller to 0, except
  1409. * for those explicitly set to some other value. The simplest
  1410. * approach is to use kzalloc() to allocate the structure containing
  1411. * @dev.
  1412. *
  1413. * NOTE: Use put_device() to give up your reference instead of freeing
  1414. * @dev directly once you have called this function.
  1415. */
  1416. void device_initialize(struct device *dev)
  1417. {
  1418. dev->kobj.kset = devices_kset;
  1419. kobject_init(&dev->kobj, &device_ktype);
  1420. INIT_LIST_HEAD(&dev->dma_pools);
  1421. mutex_init(&dev->mutex);
  1422. lockdep_set_novalidate_class(&dev->mutex);
  1423. spin_lock_init(&dev->devres_lock);
  1424. INIT_LIST_HEAD(&dev->devres_head);
  1425. device_pm_init(dev);
  1426. set_dev_node(dev, -1);
  1427. #ifdef CONFIG_GENERIC_MSI_IRQ
  1428. INIT_LIST_HEAD(&dev->msi_list);
  1429. #endif
  1430. INIT_LIST_HEAD(&dev->links.consumers);
  1431. INIT_LIST_HEAD(&dev->links.suppliers);
  1432. dev->links.status = DL_DEV_NO_DRIVER;
  1433. }
  1434. EXPORT_SYMBOL_GPL(device_initialize);
  1435. struct kobject *virtual_device_parent(struct device *dev)
  1436. {
  1437. static struct kobject *virtual_dir = NULL;
  1438. if (!virtual_dir)
  1439. virtual_dir = kobject_create_and_add("virtual",
  1440. &devices_kset->kobj);
  1441. return virtual_dir;
  1442. }
  1443. struct class_dir {
  1444. struct kobject kobj;
  1445. struct class *class;
  1446. };
  1447. #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
  1448. static void class_dir_release(struct kobject *kobj)
  1449. {
  1450. struct class_dir *dir = to_class_dir(kobj);
  1451. kfree(dir);
  1452. }
  1453. static const
  1454. struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
  1455. {
  1456. struct class_dir *dir = to_class_dir(kobj);
  1457. return dir->class->ns_type;
  1458. }
  1459. static struct kobj_type class_dir_ktype = {
  1460. .release = class_dir_release,
  1461. .sysfs_ops = &kobj_sysfs_ops,
  1462. .child_ns_type = class_dir_child_ns_type
  1463. };
  1464. static struct kobject *
  1465. class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
  1466. {
  1467. struct class_dir *dir;
  1468. int retval;
  1469. dir = kzalloc(sizeof(*dir), GFP_KERNEL);
  1470. if (!dir)
  1471. return ERR_PTR(-ENOMEM);
  1472. dir->class = class;
  1473. kobject_init(&dir->kobj, &class_dir_ktype);
  1474. dir->kobj.kset = &class->p->glue_dirs;
  1475. retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
  1476. if (retval < 0) {
  1477. kobject_put(&dir->kobj);
  1478. return ERR_PTR(retval);
  1479. }
  1480. return &dir->kobj;
  1481. }
  1482. static DEFINE_MUTEX(gdp_mutex);
  1483. static struct kobject *get_device_parent(struct device *dev,
  1484. struct device *parent)
  1485. {
  1486. if (dev->class) {
  1487. struct kobject *kobj = NULL;
  1488. struct kobject *parent_kobj;
  1489. struct kobject *k;
  1490. #ifdef CONFIG_BLOCK
  1491. /* block disks show up in /sys/block */
  1492. if (sysfs_deprecated && dev->class == &block_class) {
  1493. if (parent && parent->class == &block_class)
  1494. return &parent->kobj;
  1495. return &block_class.p->subsys.kobj;
  1496. }
  1497. #endif
  1498. /*
  1499. * If we have no parent, we live in "virtual".
  1500. * Class-devices with a non class-device as parent, live
  1501. * in a "glue" directory to prevent namespace collisions.
  1502. */
  1503. if (parent == NULL)
  1504. parent_kobj = virtual_device_parent(dev);
  1505. else if (parent->class && !dev->class->ns_type)
  1506. return &parent->kobj;
  1507. else
  1508. parent_kobj = &parent->kobj;
  1509. mutex_lock(&gdp_mutex);
  1510. /* find our class-directory at the parent and reference it */
  1511. spin_lock(&dev->class->p->glue_dirs.list_lock);
  1512. list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
  1513. if (k->parent == parent_kobj) {
  1514. kobj = kobject_get(k);
  1515. break;
  1516. }
  1517. spin_unlock(&dev->class->p->glue_dirs.list_lock);
  1518. if (kobj) {
  1519. mutex_unlock(&gdp_mutex);
  1520. return kobj;
  1521. }
  1522. /* or create a new class-directory at the parent device */
  1523. k = class_dir_create_and_add(dev->class, parent_kobj);
  1524. /* do not emit an uevent for this simple "glue" directory */
  1525. mutex_unlock(&gdp_mutex);
  1526. return k;
  1527. }
  1528. /* subsystems can specify a default root directory for their devices */
  1529. if (!parent && dev->bus && dev->bus->dev_root)
  1530. return &dev->bus->dev_root->kobj;
  1531. if (parent)
  1532. return &parent->kobj;
  1533. return NULL;
  1534. }
  1535. static inline bool live_in_glue_dir(struct kobject *kobj,
  1536. struct device *dev)
  1537. {
  1538. if (!kobj || !dev->class ||
  1539. kobj->kset != &dev->class->p->glue_dirs)
  1540. return false;
  1541. return true;
  1542. }
  1543. static inline struct kobject *get_glue_dir(struct device *dev)
  1544. {
  1545. return dev->kobj.parent;
  1546. }
  1547. /*
  1548. * make sure cleaning up dir as the last step, we need to make
  1549. * sure .release handler of kobject is run with holding the
  1550. * global lock
  1551. */
  1552. static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
  1553. {
  1554. unsigned int ref;
  1555. /* see if we live in a "glue" directory */
  1556. if (!live_in_glue_dir(glue_dir, dev))
  1557. return;
  1558. mutex_lock(&gdp_mutex);
  1559. /**
  1560. * There is a race condition between removing glue directory
  1561. * and adding a new device under the glue directory.
  1562. *
  1563. * CPU1: CPU2:
  1564. *
  1565. * device_add()
  1566. * get_device_parent()
  1567. * class_dir_create_and_add()
  1568. * kobject_add_internal()
  1569. * create_dir() // create glue_dir
  1570. *
  1571. * device_add()
  1572. * get_device_parent()
  1573. * kobject_get() // get glue_dir
  1574. *
  1575. * device_del()
  1576. * cleanup_glue_dir()
  1577. * kobject_del(glue_dir)
  1578. *
  1579. * kobject_add()
  1580. * kobject_add_internal()
  1581. * create_dir() // in glue_dir
  1582. * sysfs_create_dir_ns()
  1583. * kernfs_create_dir_ns(sd)
  1584. *
  1585. * sysfs_remove_dir() // glue_dir->sd=NULL
  1586. * sysfs_put() // free glue_dir->sd
  1587. *
  1588. * // sd is freed
  1589. * kernfs_new_node(sd)
  1590. * kernfs_get(glue_dir)
  1591. * kernfs_add_one()
  1592. * kernfs_put()
  1593. *
  1594. * Before CPU1 remove last child device under glue dir, if CPU2 add
  1595. * a new device under glue dir, the glue_dir kobject reference count
  1596. * will be increase to 2 in kobject_get(k). And CPU2 has been called
  1597. * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
  1598. * and sysfs_put(). This result in glue_dir->sd is freed.
  1599. *
  1600. * Then the CPU2 will see a stale "empty" but still potentially used
  1601. * glue dir around in kernfs_new_node().
  1602. *
  1603. * In order to avoid this happening, we also should make sure that
  1604. * kernfs_node for glue_dir is released in CPU1 only when refcount
  1605. * for glue_dir kobj is 1.
  1606. */
  1607. ref = kref_read(&glue_dir->kref);
  1608. if (!kobject_has_children(glue_dir) && !--ref)
  1609. kobject_del(glue_dir);
  1610. kobject_put(glue_dir);
  1611. mutex_unlock(&gdp_mutex);
  1612. }
  1613. static int device_add_class_symlinks(struct device *dev)
  1614. {
  1615. struct device_node *of_node = dev_of_node(dev);
  1616. int error;
  1617. if (of_node) {
  1618. error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
  1619. if (error)
  1620. dev_warn(dev, "Error %d creating of_node link\n",error);
  1621. /* An error here doesn't warrant bringing down the device */
  1622. }
  1623. if (!dev->class)
  1624. return 0;
  1625. error = sysfs_create_link(&dev->kobj,
  1626. &dev->class->p->subsys.kobj,
  1627. "subsystem");
  1628. if (error)
  1629. goto out_devnode;
  1630. if (dev->parent && device_is_not_partition(dev)) {
  1631. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  1632. "device");
  1633. if (error)
  1634. goto out_subsys;
  1635. }
  1636. #ifdef CONFIG_BLOCK
  1637. /* /sys/block has directories and does not need symlinks */
  1638. if (sysfs_deprecated && dev->class == &block_class)
  1639. return 0;
  1640. #endif
  1641. /* link in the class directory pointing to the device */
  1642. error = sysfs_create_link(&dev->class->p->subsys.kobj,
  1643. &dev->kobj, dev_name(dev));
  1644. if (error)
  1645. goto out_device;
  1646. return 0;
  1647. out_device:
  1648. sysfs_remove_link(&dev->kobj, "device");
  1649. out_subsys:
  1650. sysfs_remove_link(&dev->kobj, "subsystem");
  1651. out_devnode:
  1652. sysfs_remove_link(&dev->kobj, "of_node");
  1653. return error;
  1654. }
  1655. static void device_remove_class_symlinks(struct device *dev)
  1656. {
  1657. if (dev_of_node(dev))
  1658. sysfs_remove_link(&dev->kobj, "of_node");
  1659. if (!dev->class)
  1660. return;
  1661. if (dev->parent && device_is_not_partition(dev))
  1662. sysfs_remove_link(&dev->kobj, "device");
  1663. sysfs_remove_link(&dev->kobj, "subsystem");
  1664. #ifdef CONFIG_BLOCK
  1665. if (sysfs_deprecated && dev->class == &block_class)
  1666. return;
  1667. #endif
  1668. sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
  1669. }
  1670. /**
  1671. * dev_set_name - set a device name
  1672. * @dev: device
  1673. * @fmt: format string for the device's name
  1674. */
  1675. int dev_set_name(struct device *dev, const char *fmt, ...)
  1676. {
  1677. va_list vargs;
  1678. int err;
  1679. va_start(vargs, fmt);
  1680. err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
  1681. va_end(vargs);
  1682. return err;
  1683. }
  1684. EXPORT_SYMBOL_GPL(dev_set_name);
  1685. /**
  1686. * device_to_dev_kobj - select a /sys/dev/ directory for the device
  1687. * @dev: device
  1688. *
  1689. * By default we select char/ for new entries. Setting class->dev_obj
  1690. * to NULL prevents an entry from being created. class->dev_kobj must
  1691. * be set (or cleared) before any devices are registered to the class
  1692. * otherwise device_create_sys_dev_entry() and
  1693. * device_remove_sys_dev_entry() will disagree about the presence of
  1694. * the link.
  1695. */
  1696. static struct kobject *device_to_dev_kobj(struct device *dev)
  1697. {
  1698. struct kobject *kobj;
  1699. if (dev->class)
  1700. kobj = dev->class->dev_kobj;
  1701. else
  1702. kobj = sysfs_dev_char_kobj;
  1703. return kobj;
  1704. }
  1705. static int device_create_sys_dev_entry(struct device *dev)
  1706. {
  1707. struct kobject *kobj = device_to_dev_kobj(dev);
  1708. int error = 0;
  1709. char devt_str[15];
  1710. if (kobj) {
  1711. format_dev_t(devt_str, dev->devt);
  1712. error = sysfs_create_link(kobj, &dev->kobj, devt_str);
  1713. }
  1714. return error;
  1715. }
  1716. static void device_remove_sys_dev_entry(struct device *dev)
  1717. {
  1718. struct kobject *kobj = device_to_dev_kobj(dev);
  1719. char devt_str[15];
  1720. if (kobj) {
  1721. format_dev_t(devt_str, dev->devt);
  1722. sysfs_remove_link(kobj, devt_str);
  1723. }
  1724. }
  1725. static int device_private_init(struct device *dev)
  1726. {
  1727. dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
  1728. if (!dev->p)
  1729. return -ENOMEM;
  1730. dev->p->device = dev;
  1731. klist_init(&dev->p->klist_children, klist_children_get,
  1732. klist_children_put);
  1733. INIT_LIST_HEAD(&dev->p->deferred_probe);
  1734. return 0;
  1735. }
  1736. /**
  1737. * device_add - add device to device hierarchy.
  1738. * @dev: device.
  1739. *
  1740. * This is part 2 of device_register(), though may be called
  1741. * separately _iff_ device_initialize() has been called separately.
  1742. *
  1743. * This adds @dev to the kobject hierarchy via kobject_add(), adds it
  1744. * to the global and sibling lists for the device, then
  1745. * adds it to the other relevant subsystems of the driver model.
  1746. *
  1747. * Do not call this routine or device_register() more than once for
  1748. * any device structure. The driver model core is not designed to work
  1749. * with devices that get unregistered and then spring back to life.
  1750. * (Among other things, it's very hard to guarantee that all references
  1751. * to the previous incarnation of @dev have been dropped.) Allocate
  1752. * and register a fresh new struct device instead.
  1753. *
  1754. * NOTE: _Never_ directly free @dev after calling this function, even
  1755. * if it returned an error! Always use put_device() to give up your
  1756. * reference instead.
  1757. */
  1758. int device_add(struct device *dev)
  1759. {
  1760. struct device *parent;
  1761. struct kobject *kobj;
  1762. struct class_interface *class_intf;
  1763. int error = -EINVAL;
  1764. struct kobject *glue_dir = NULL;
  1765. dev = get_device(dev);
  1766. if (!dev)
  1767. goto done;
  1768. if (!dev->p) {
  1769. error = device_private_init(dev);
  1770. if (error)
  1771. goto done;
  1772. }
  1773. /*
  1774. * for statically allocated devices, which should all be converted
  1775. * some day, we need to initialize the name. We prevent reading back
  1776. * the name, and force the use of dev_name()
  1777. */
  1778. if (dev->init_name) {
  1779. dev_set_name(dev, "%s", dev->init_name);
  1780. dev->init_name = NULL;
  1781. }
  1782. /* subsystems can specify simple device enumeration */
  1783. if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
  1784. dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
  1785. if (!dev_name(dev)) {
  1786. error = -EINVAL;
  1787. goto name_error;
  1788. }
  1789. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1790. parent = get_device(dev->parent);
  1791. kobj = get_device_parent(dev, parent);
  1792. if (IS_ERR(kobj)) {
  1793. error = PTR_ERR(kobj);
  1794. goto parent_error;
  1795. }
  1796. if (kobj)
  1797. dev->kobj.parent = kobj;
  1798. /* use parent numa_node */
  1799. if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
  1800. set_dev_node(dev, dev_to_node(parent));
  1801. /* first, register with generic layer. */
  1802. /* we require the name to be set before, and pass NULL */
  1803. error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
  1804. if (error) {
  1805. glue_dir = get_glue_dir(dev);
  1806. goto Error;
  1807. }
  1808. /* notify platform of device entry */
  1809. if (platform_notify)
  1810. platform_notify(dev);
  1811. error = device_create_file(dev, &dev_attr_uevent);
  1812. if (error)
  1813. goto attrError;
  1814. error = device_add_class_symlinks(dev);
  1815. if (error)
  1816. goto SymlinkError;
  1817. error = device_add_attrs(dev);
  1818. if (error)
  1819. goto AttrsError;
  1820. error = bus_add_device(dev);
  1821. if (error)
  1822. goto BusError;
  1823. error = dpm_sysfs_add(dev);
  1824. if (error)
  1825. goto DPMError;
  1826. device_pm_add(dev);
  1827. if (MAJOR(dev->devt)) {
  1828. error = device_create_file(dev, &dev_attr_dev);
  1829. if (error)
  1830. goto DevAttrError;
  1831. error = device_create_sys_dev_entry(dev);
  1832. if (error)
  1833. goto SysEntryError;
  1834. devtmpfs_create_node(dev);
  1835. }
  1836. /* Notify clients of device addition. This call must come
  1837. * after dpm_sysfs_add() and before kobject_uevent().
  1838. */
  1839. if (dev->bus)
  1840. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  1841. BUS_NOTIFY_ADD_DEVICE, dev);
  1842. kobject_uevent(&dev->kobj, KOBJ_ADD);
  1843. bus_probe_device(dev);
  1844. if (parent)
  1845. klist_add_tail(&dev->p->knode_parent,
  1846. &parent->p->klist_children);
  1847. if (dev->class) {
  1848. mutex_lock(&dev->class->p->mutex);
  1849. /* tie the class to the device */
  1850. klist_add_tail(&dev->knode_class,
  1851. &dev->class->p->klist_devices);
  1852. /* notify any interfaces that the device is here */
  1853. list_for_each_entry(class_intf,
  1854. &dev->class->p->interfaces, node)
  1855. if (class_intf->add_dev)
  1856. class_intf->add_dev(dev, class_intf);
  1857. mutex_unlock(&dev->class->p->mutex);
  1858. }
  1859. done:
  1860. put_device(dev);
  1861. return error;
  1862. SysEntryError:
  1863. if (MAJOR(dev->devt))
  1864. device_remove_file(dev, &dev_attr_dev);
  1865. DevAttrError:
  1866. device_pm_remove(dev);
  1867. dpm_sysfs_remove(dev);
  1868. DPMError:
  1869. bus_remove_device(dev);
  1870. BusError:
  1871. device_remove_attrs(dev);
  1872. AttrsError:
  1873. device_remove_class_symlinks(dev);
  1874. SymlinkError:
  1875. device_remove_file(dev, &dev_attr_uevent);
  1876. attrError:
  1877. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  1878. glue_dir = get_glue_dir(dev);
  1879. kobject_del(&dev->kobj);
  1880. Error:
  1881. cleanup_glue_dir(dev, glue_dir);
  1882. parent_error:
  1883. put_device(parent);
  1884. name_error:
  1885. kfree(dev->p);
  1886. dev->p = NULL;
  1887. goto done;
  1888. }
  1889. EXPORT_SYMBOL_GPL(device_add);
  1890. /**
  1891. * device_register - register a device with the system.
  1892. * @dev: pointer to the device structure
  1893. *
  1894. * This happens in two clean steps - initialize the device
  1895. * and add it to the system. The two steps can be called
  1896. * separately, but this is the easiest and most common.
  1897. * I.e. you should only call the two helpers separately if
  1898. * have a clearly defined need to use and refcount the device
  1899. * before it is added to the hierarchy.
  1900. *
  1901. * For more information, see the kerneldoc for device_initialize()
  1902. * and device_add().
  1903. *
  1904. * NOTE: _Never_ directly free @dev after calling this function, even
  1905. * if it returned an error! Always use put_device() to give up the
  1906. * reference initialized in this function instead.
  1907. */
  1908. int device_register(struct device *dev)
  1909. {
  1910. device_initialize(dev);
  1911. return device_add(dev);
  1912. }
  1913. EXPORT_SYMBOL_GPL(device_register);
  1914. /**
  1915. * get_device - increment reference count for device.
  1916. * @dev: device.
  1917. *
  1918. * This simply forwards the call to kobject_get(), though
  1919. * we do take care to provide for the case that we get a NULL
  1920. * pointer passed in.
  1921. */
  1922. struct device *get_device(struct device *dev)
  1923. {
  1924. return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
  1925. }
  1926. EXPORT_SYMBOL_GPL(get_device);
  1927. /**
  1928. * put_device - decrement reference count.
  1929. * @dev: device in question.
  1930. */
  1931. void put_device(struct device *dev)
  1932. {
  1933. /* might_sleep(); */
  1934. if (dev)
  1935. kobject_put(&dev->kobj);
  1936. }
  1937. EXPORT_SYMBOL_GPL(put_device);
  1938. bool kill_device(struct device *dev)
  1939. {
  1940. /*
  1941. * Require the device lock and set the "dead" flag to guarantee that
  1942. * the update behavior is consistent with the other bitfields near
  1943. * it and that we cannot have an asynchronous probe routine trying
  1944. * to run while we are tearing out the bus/class/sysfs from
  1945. * underneath the device.
  1946. */
  1947. lockdep_assert_held(&dev->mutex);
  1948. if (dev->p->dead)
  1949. return false;
  1950. dev->p->dead = true;
  1951. return true;
  1952. }
  1953. EXPORT_SYMBOL_GPL(kill_device);
  1954. /**
  1955. * device_del - delete device from system.
  1956. * @dev: device.
  1957. *
  1958. * This is the first part of the device unregistration
  1959. * sequence. This removes the device from the lists we control
  1960. * from here, has it removed from the other driver model
  1961. * subsystems it was added to in device_add(), and removes it
  1962. * from the kobject hierarchy.
  1963. *
  1964. * NOTE: this should be called manually _iff_ device_add() was
  1965. * also called manually.
  1966. */
  1967. void device_del(struct device *dev)
  1968. {
  1969. struct device *parent = dev->parent;
  1970. struct kobject *glue_dir = NULL;
  1971. struct class_interface *class_intf;
  1972. device_lock(dev);
  1973. kill_device(dev);
  1974. device_unlock(dev);
  1975. /* Notify clients of device removal. This call must come
  1976. * before dpm_sysfs_remove().
  1977. */
  1978. if (dev->bus)
  1979. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  1980. BUS_NOTIFY_DEL_DEVICE, dev);
  1981. dpm_sysfs_remove(dev);
  1982. if (parent)
  1983. klist_del(&dev->p->knode_parent);
  1984. if (MAJOR(dev->devt)) {
  1985. devtmpfs_delete_node(dev);
  1986. device_remove_sys_dev_entry(dev);
  1987. device_remove_file(dev, &dev_attr_dev);
  1988. }
  1989. if (dev->class) {
  1990. device_remove_class_symlinks(dev);
  1991. mutex_lock(&dev->class->p->mutex);
  1992. /* notify any interfaces that the device is now gone */
  1993. list_for_each_entry(class_intf,
  1994. &dev->class->p->interfaces, node)
  1995. if (class_intf->remove_dev)
  1996. class_intf->remove_dev(dev, class_intf);
  1997. /* remove the device from the class list */
  1998. klist_del(&dev->knode_class);
  1999. mutex_unlock(&dev->class->p->mutex);
  2000. }
  2001. device_remove_file(dev, &dev_attr_uevent);
  2002. device_remove_attrs(dev);
  2003. bus_remove_device(dev);
  2004. device_pm_remove(dev);
  2005. driver_deferred_probe_del(dev);
  2006. device_remove_properties(dev);
  2007. device_links_purge(dev);
  2008. /* Notify the platform of the removal, in case they
  2009. * need to do anything...
  2010. */
  2011. if (platform_notify_remove)
  2012. platform_notify_remove(dev);
  2013. if (dev->bus)
  2014. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  2015. BUS_NOTIFY_REMOVED_DEVICE, dev);
  2016. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  2017. glue_dir = get_glue_dir(dev);
  2018. kobject_del(&dev->kobj);
  2019. cleanup_glue_dir(dev, glue_dir);
  2020. put_device(parent);
  2021. }
  2022. EXPORT_SYMBOL_GPL(device_del);
  2023. /**
  2024. * device_unregister - unregister device from system.
  2025. * @dev: device going away.
  2026. *
  2027. * We do this in two parts, like we do device_register(). First,
  2028. * we remove it from all the subsystems with device_del(), then
  2029. * we decrement the reference count via put_device(). If that
  2030. * is the final reference count, the device will be cleaned up
  2031. * via device_release() above. Otherwise, the structure will
  2032. * stick around until the final reference to the device is dropped.
  2033. */
  2034. void device_unregister(struct device *dev)
  2035. {
  2036. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  2037. device_del(dev);
  2038. put_device(dev);
  2039. }
  2040. EXPORT_SYMBOL_GPL(device_unregister);
  2041. static struct device *prev_device(struct klist_iter *i)
  2042. {
  2043. struct klist_node *n = klist_prev(i);
  2044. struct device *dev = NULL;
  2045. struct device_private *p;
  2046. if (n) {
  2047. p = to_device_private_parent(n);
  2048. dev = p->device;
  2049. }
  2050. return dev;
  2051. }
  2052. static struct device *next_device(struct klist_iter *i)
  2053. {
  2054. struct klist_node *n = klist_next(i);
  2055. struct device *dev = NULL;
  2056. struct device_private *p;
  2057. if (n) {
  2058. p = to_device_private_parent(n);
  2059. dev = p->device;
  2060. }
  2061. return dev;
  2062. }
  2063. /**
  2064. * device_get_devnode - path of device node file
  2065. * @dev: device
  2066. * @mode: returned file access mode
  2067. * @uid: returned file owner
  2068. * @gid: returned file group
  2069. * @tmp: possibly allocated string
  2070. *
  2071. * Return the relative path of a possible device node.
  2072. * Non-default names may need to allocate a memory to compose
  2073. * a name. This memory is returned in tmp and needs to be
  2074. * freed by the caller.
  2075. */
  2076. const char *device_get_devnode(struct device *dev,
  2077. umode_t *mode, kuid_t *uid, kgid_t *gid,
  2078. const char **tmp)
  2079. {
  2080. char *s;
  2081. *tmp = NULL;
  2082. /* the device type may provide a specific name */
  2083. if (dev->type && dev->type->devnode)
  2084. *tmp = dev->type->devnode(dev, mode, uid, gid);
  2085. if (*tmp)
  2086. return *tmp;
  2087. /* the class may provide a specific name */
  2088. if (dev->class && dev->class->devnode)
  2089. *tmp = dev->class->devnode(dev, mode);
  2090. if (*tmp)
  2091. return *tmp;
  2092. /* return name without allocation, tmp == NULL */
  2093. if (strchr(dev_name(dev), '!') == NULL)
  2094. return dev_name(dev);
  2095. /* replace '!' in the name with '/' */
  2096. s = kstrdup(dev_name(dev), GFP_KERNEL);
  2097. if (!s)
  2098. return NULL;
  2099. strreplace(s, '!', '/');
  2100. return *tmp = s;
  2101. }
  2102. /**
  2103. * device_for_each_child - device child iterator.
  2104. * @parent: parent struct device.
  2105. * @fn: function to be called for each device.
  2106. * @data: data for the callback.
  2107. *
  2108. * Iterate over @parent's child devices, and call @fn for each,
  2109. * passing it @data.
  2110. *
  2111. * We check the return of @fn each time. If it returns anything
  2112. * other than 0, we break out and return that value.
  2113. */
  2114. int device_for_each_child(struct device *parent, void *data,
  2115. int (*fn)(struct device *dev, void *data))
  2116. {
  2117. struct klist_iter i;
  2118. struct device *child;
  2119. int error = 0;
  2120. if (!parent->p)
  2121. return 0;
  2122. klist_iter_init(&parent->p->klist_children, &i);
  2123. while (!error && (child = next_device(&i)))
  2124. error = fn(child, data);
  2125. klist_iter_exit(&i);
  2126. return error;
  2127. }
  2128. EXPORT_SYMBOL_GPL(device_for_each_child);
  2129. /**
  2130. * device_for_each_child_reverse - device child iterator in reversed order.
  2131. * @parent: parent struct device.
  2132. * @fn: function to be called for each device.
  2133. * @data: data for the callback.
  2134. *
  2135. * Iterate over @parent's child devices, and call @fn for each,
  2136. * passing it @data.
  2137. *
  2138. * We check the return of @fn each time. If it returns anything
  2139. * other than 0, we break out and return that value.
  2140. */
  2141. int device_for_each_child_reverse(struct device *parent, void *data,
  2142. int (*fn)(struct device *dev, void *data))
  2143. {
  2144. struct klist_iter i;
  2145. struct device *child;
  2146. int error = 0;
  2147. if (!parent->p)
  2148. return 0;
  2149. klist_iter_init(&parent->p->klist_children, &i);
  2150. while ((child = prev_device(&i)) && !error)
  2151. error = fn(child, data);
  2152. klist_iter_exit(&i);
  2153. return error;
  2154. }
  2155. EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
  2156. /**
  2157. * device_find_child - device iterator for locating a particular device.
  2158. * @parent: parent struct device
  2159. * @match: Callback function to check device
  2160. * @data: Data to pass to match function
  2161. *
  2162. * This is similar to the device_for_each_child() function above, but it
  2163. * returns a reference to a device that is 'found' for later use, as
  2164. * determined by the @match callback.
  2165. *
  2166. * The callback should return 0 if the device doesn't match and non-zero
  2167. * if it does. If the callback returns non-zero and a reference to the
  2168. * current device can be obtained, this function will return to the caller
  2169. * and not iterate over any more devices.
  2170. *
  2171. * NOTE: you will need to drop the reference with put_device() after use.
  2172. */
  2173. struct device *device_find_child(struct device *parent, void *data,
  2174. int (*match)(struct device *dev, void *data))
  2175. {
  2176. struct klist_iter i;
  2177. struct device *child;
  2178. if (!parent)
  2179. return NULL;
  2180. klist_iter_init(&parent->p->klist_children, &i);
  2181. while ((child = next_device(&i)))
  2182. if (match(child, data) && get_device(child))
  2183. break;
  2184. klist_iter_exit(&i);
  2185. return child;
  2186. }
  2187. EXPORT_SYMBOL_GPL(device_find_child);
  2188. int __init devices_init(void)
  2189. {
  2190. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  2191. if (!devices_kset)
  2192. return -ENOMEM;
  2193. dev_kobj = kobject_create_and_add("dev", NULL);
  2194. if (!dev_kobj)
  2195. goto dev_kobj_err;
  2196. sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  2197. if (!sysfs_dev_block_kobj)
  2198. goto block_kobj_err;
  2199. sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  2200. if (!sysfs_dev_char_kobj)
  2201. goto char_kobj_err;
  2202. return 0;
  2203. char_kobj_err:
  2204. kobject_put(sysfs_dev_block_kobj);
  2205. block_kobj_err:
  2206. kobject_put(dev_kobj);
  2207. dev_kobj_err:
  2208. kset_unregister(devices_kset);
  2209. return -ENOMEM;
  2210. }
  2211. static int device_check_offline(struct device *dev, void *not_used)
  2212. {
  2213. int ret;
  2214. ret = device_for_each_child(dev, NULL, device_check_offline);
  2215. if (ret)
  2216. return ret;
  2217. return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
  2218. }
  2219. /**
  2220. * device_offline - Prepare the device for hot-removal.
  2221. * @dev: Device to be put offline.
  2222. *
  2223. * Execute the device bus type's .offline() callback, if present, to prepare
  2224. * the device for a subsequent hot-removal. If that succeeds, the device must
  2225. * not be used until either it is removed or its bus type's .online() callback
  2226. * is executed.
  2227. *
  2228. * Call under device_hotplug_lock.
  2229. */
  2230. int device_offline(struct device *dev)
  2231. {
  2232. int ret;
  2233. if (dev->offline_disabled)
  2234. return -EPERM;
  2235. ret = device_for_each_child(dev, NULL, device_check_offline);
  2236. if (ret)
  2237. return ret;
  2238. device_lock(dev);
  2239. if (device_supports_offline(dev)) {
  2240. if (dev->offline) {
  2241. ret = 1;
  2242. } else {
  2243. ret = dev->bus->offline(dev);
  2244. if (!ret) {
  2245. kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  2246. dev->offline = true;
  2247. }
  2248. }
  2249. }
  2250. device_unlock(dev);
  2251. return ret;
  2252. }
  2253. /**
  2254. * device_online - Put the device back online after successful device_offline().
  2255. * @dev: Device to be put back online.
  2256. *
  2257. * If device_offline() has been successfully executed for @dev, but the device
  2258. * has not been removed subsequently, execute its bus type's .online() callback
  2259. * to indicate that the device can be used again.
  2260. *
  2261. * Call under device_hotplug_lock.
  2262. */
  2263. int device_online(struct device *dev)
  2264. {
  2265. int ret = 0;
  2266. device_lock(dev);
  2267. if (device_supports_offline(dev)) {
  2268. if (dev->offline) {
  2269. ret = dev->bus->online(dev);
  2270. if (!ret) {
  2271. kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  2272. dev->offline = false;
  2273. }
  2274. } else {
  2275. ret = 1;
  2276. }
  2277. }
  2278. device_unlock(dev);
  2279. return ret;
  2280. }
  2281. struct root_device {
  2282. struct device dev;
  2283. struct module *owner;
  2284. };
  2285. static inline struct root_device *to_root_device(struct device *d)
  2286. {
  2287. return container_of(d, struct root_device, dev);
  2288. }
  2289. static void root_device_release(struct device *dev)
  2290. {
  2291. kfree(to_root_device(dev));
  2292. }
  2293. /**
  2294. * __root_device_register - allocate and register a root device
  2295. * @name: root device name
  2296. * @owner: owner module of the root device, usually THIS_MODULE
  2297. *
  2298. * This function allocates a root device and registers it
  2299. * using device_register(). In order to free the returned
  2300. * device, use root_device_unregister().
  2301. *
  2302. * Root devices are dummy devices which allow other devices
  2303. * to be grouped under /sys/devices. Use this function to
  2304. * allocate a root device and then use it as the parent of
  2305. * any device which should appear under /sys/devices/{name}
  2306. *
  2307. * The /sys/devices/{name} directory will also contain a
  2308. * 'module' symlink which points to the @owner directory
  2309. * in sysfs.
  2310. *
  2311. * Returns &struct device pointer on success, or ERR_PTR() on error.
  2312. *
  2313. * Note: You probably want to use root_device_register().
  2314. */
  2315. struct device *__root_device_register(const char *name, struct module *owner)
  2316. {
  2317. struct root_device *root;
  2318. int err = -ENOMEM;
  2319. root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
  2320. if (!root)
  2321. return ERR_PTR(err);
  2322. err = dev_set_name(&root->dev, "%s", name);
  2323. if (err) {
  2324. kfree(root);
  2325. return ERR_PTR(err);
  2326. }
  2327. root->dev.release = root_device_release;
  2328. err = device_register(&root->dev);
  2329. if (err) {
  2330. put_device(&root->dev);
  2331. return ERR_PTR(err);
  2332. }
  2333. #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
  2334. if (owner) {
  2335. struct module_kobject *mk = &owner->mkobj;
  2336. err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
  2337. if (err) {
  2338. device_unregister(&root->dev);
  2339. return ERR_PTR(err);
  2340. }
  2341. root->owner = owner;
  2342. }
  2343. #endif
  2344. return &root->dev;
  2345. }
  2346. EXPORT_SYMBOL_GPL(__root_device_register);
  2347. /**
  2348. * root_device_unregister - unregister and free a root device
  2349. * @dev: device going away
  2350. *
  2351. * This function unregisters and cleans up a device that was created by
  2352. * root_device_register().
  2353. */
  2354. void root_device_unregister(struct device *dev)
  2355. {
  2356. struct root_device *root = to_root_device(dev);
  2357. if (root->owner)
  2358. sysfs_remove_link(&root->dev.kobj, "module");
  2359. device_unregister(dev);
  2360. }
  2361. EXPORT_SYMBOL_GPL(root_device_unregister);
  2362. static void device_create_release(struct device *dev)
  2363. {
  2364. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  2365. kfree(dev);
  2366. }
  2367. static __printf(6, 0) struct device *
  2368. device_create_groups_vargs(struct class *class, struct device *parent,
  2369. dev_t devt, void *drvdata,
  2370. const struct attribute_group **groups,
  2371. const char *fmt, va_list args)
  2372. {
  2373. struct device *dev = NULL;
  2374. int retval = -ENODEV;
  2375. if (class == NULL || IS_ERR(class))
  2376. goto error;
  2377. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2378. if (!dev) {
  2379. retval = -ENOMEM;
  2380. goto error;
  2381. }
  2382. device_initialize(dev);
  2383. dev->devt = devt;
  2384. dev->class = class;
  2385. dev->parent = parent;
  2386. dev->groups = groups;
  2387. dev->release = device_create_release;
  2388. dev_set_drvdata(dev, drvdata);
  2389. retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
  2390. if (retval)
  2391. goto error;
  2392. retval = device_add(dev);
  2393. if (retval)
  2394. goto error;
  2395. return dev;
  2396. error:
  2397. put_device(dev);
  2398. return ERR_PTR(retval);
  2399. }
  2400. /**
  2401. * device_create_vargs - creates a device and registers it with sysfs
  2402. * @class: pointer to the struct class that this device should be registered to
  2403. * @parent: pointer to the parent struct device of this new device, if any
  2404. * @devt: the dev_t for the char device to be added
  2405. * @drvdata: the data to be added to the device for callbacks
  2406. * @fmt: string for the device's name
  2407. * @args: va_list for the device's name
  2408. *
  2409. * This function can be used by char device classes. A struct device
  2410. * will be created in sysfs, registered to the specified class.
  2411. *
  2412. * A "dev" file will be created, showing the dev_t for the device, if
  2413. * the dev_t is not 0,0.
  2414. * If a pointer to a parent struct device is passed in, the newly created
  2415. * struct device will be a child of that device in sysfs.
  2416. * The pointer to the struct device will be returned from the call.
  2417. * Any further sysfs files that might be required can be created using this
  2418. * pointer.
  2419. *
  2420. * Returns &struct device pointer on success, or ERR_PTR() on error.
  2421. *
  2422. * Note: the struct class passed to this function must have previously
  2423. * been created with a call to class_create().
  2424. */
  2425. struct device *device_create_vargs(struct class *class, struct device *parent,
  2426. dev_t devt, void *drvdata, const char *fmt,
  2427. va_list args)
  2428. {
  2429. return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
  2430. fmt, args);
  2431. }
  2432. EXPORT_SYMBOL_GPL(device_create_vargs);
  2433. /**
  2434. * device_create - creates a device and registers it with sysfs
  2435. * @class: pointer to the struct class that this device should be registered to
  2436. * @parent: pointer to the parent struct device of this new device, if any
  2437. * @devt: the dev_t for the char device to be added
  2438. * @drvdata: the data to be added to the device for callbacks
  2439. * @fmt: string for the device's name
  2440. *
  2441. * This function can be used by char device classes. A struct device
  2442. * will be created in sysfs, registered to the specified class.
  2443. *
  2444. * A "dev" file will be created, showing the dev_t for the device, if
  2445. * the dev_t is not 0,0.
  2446. * If a pointer to a parent struct device is passed in, the newly created
  2447. * struct device will be a child of that device in sysfs.
  2448. * The pointer to the struct device will be returned from the call.
  2449. * Any further sysfs files that might be required can be created using this
  2450. * pointer.
  2451. *
  2452. * Returns &struct device pointer on success, or ERR_PTR() on error.
  2453. *
  2454. * Note: the struct class passed to this function must have previously
  2455. * been created with a call to class_create().
  2456. */
  2457. struct device *device_create(struct class *class, struct device *parent,
  2458. dev_t devt, void *drvdata, const char *fmt, ...)
  2459. {
  2460. va_list vargs;
  2461. struct device *dev;
  2462. va_start(vargs, fmt);
  2463. dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
  2464. va_end(vargs);
  2465. return dev;
  2466. }
  2467. EXPORT_SYMBOL_GPL(device_create);
  2468. /**
  2469. * device_create_with_groups - creates a device and registers it with sysfs
  2470. * @class: pointer to the struct class that this device should be registered to
  2471. * @parent: pointer to the parent struct device of this new device, if any
  2472. * @devt: the dev_t for the char device to be added
  2473. * @drvdata: the data to be added to the device for callbacks
  2474. * @groups: NULL-terminated list of attribute groups to be created
  2475. * @fmt: string for the device's name
  2476. *
  2477. * This function can be used by char device classes. A struct device
  2478. * will be created in sysfs, registered to the specified class.
  2479. * Additional attributes specified in the groups parameter will also
  2480. * be created automatically.
  2481. *
  2482. * A "dev" file will be created, showing the dev_t for the device, if
  2483. * the dev_t is not 0,0.
  2484. * If a pointer to a parent struct device is passed in, the newly created
  2485. * struct device will be a child of that device in sysfs.
  2486. * The pointer to the struct device will be returned from the call.
  2487. * Any further sysfs files that might be required can be created using this
  2488. * pointer.
  2489. *
  2490. * Returns &struct device pointer on success, or ERR_PTR() on error.
  2491. *
  2492. * Note: the struct class passed to this function must have previously
  2493. * been created with a call to class_create().
  2494. */
  2495. struct device *device_create_with_groups(struct class *class,
  2496. struct device *parent, dev_t devt,
  2497. void *drvdata,
  2498. const struct attribute_group **groups,
  2499. const char *fmt, ...)
  2500. {
  2501. va_list vargs;
  2502. struct device *dev;
  2503. va_start(vargs, fmt);
  2504. dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
  2505. fmt, vargs);
  2506. va_end(vargs);
  2507. return dev;
  2508. }
  2509. EXPORT_SYMBOL_GPL(device_create_with_groups);
  2510. static int __match_devt(struct device *dev, const void *data)
  2511. {
  2512. const dev_t *devt = data;
  2513. return dev->devt == *devt;
  2514. }
  2515. /**
  2516. * device_destroy - removes a device that was created with device_create()
  2517. * @class: pointer to the struct class that this device was registered with
  2518. * @devt: the dev_t of the device that was previously registered
  2519. *
  2520. * This call unregisters and cleans up a device that was created with a
  2521. * call to device_create().
  2522. */
  2523. void device_destroy(struct class *class, dev_t devt)
  2524. {
  2525. struct device *dev;
  2526. dev = class_find_device(class, NULL, &devt, __match_devt);
  2527. if (dev) {
  2528. put_device(dev);
  2529. device_unregister(dev);
  2530. }
  2531. }
  2532. EXPORT_SYMBOL_GPL(device_destroy);
  2533. /**
  2534. * device_rename - renames a device
  2535. * @dev: the pointer to the struct device to be renamed
  2536. * @new_name: the new name of the device
  2537. *
  2538. * It is the responsibility of the caller to provide mutual
  2539. * exclusion between two different calls of device_rename
  2540. * on the same device to ensure that new_name is valid and
  2541. * won't conflict with other devices.
  2542. *
  2543. * Note: Don't call this function. Currently, the networking layer calls this
  2544. * function, but that will change. The following text from Kay Sievers offers
  2545. * some insight:
  2546. *
  2547. * Renaming devices is racy at many levels, symlinks and other stuff are not
  2548. * replaced atomically, and you get a "move" uevent, but it's not easy to
  2549. * connect the event to the old and new device. Device nodes are not renamed at
  2550. * all, there isn't even support for that in the kernel now.
  2551. *
  2552. * In the meantime, during renaming, your target name might be taken by another
  2553. * driver, creating conflicts. Or the old name is taken directly after you
  2554. * renamed it -- then you get events for the same DEVPATH, before you even see
  2555. * the "move" event. It's just a mess, and nothing new should ever rely on
  2556. * kernel device renaming. Besides that, it's not even implemented now for
  2557. * other things than (driver-core wise very simple) network devices.
  2558. *
  2559. * We are currently about to change network renaming in udev to completely
  2560. * disallow renaming of devices in the same namespace as the kernel uses,
  2561. * because we can't solve the problems properly, that arise with swapping names
  2562. * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
  2563. * be allowed to some other name than eth[0-9]*, for the aforementioned
  2564. * reasons.
  2565. *
  2566. * Make up a "real" name in the driver before you register anything, or add
  2567. * some other attributes for userspace to find the device, or use udev to add
  2568. * symlinks -- but never rename kernel devices later, it's a complete mess. We
  2569. * don't even want to get into that and try to implement the missing pieces in
  2570. * the core. We really have other pieces to fix in the driver core mess. :)
  2571. */
  2572. int device_rename(struct device *dev, const char *new_name)
  2573. {
  2574. struct kobject *kobj = &dev->kobj;
  2575. char *old_device_name = NULL;
  2576. int error;
  2577. dev = get_device(dev);
  2578. if (!dev)
  2579. return -EINVAL;
  2580. dev_dbg(dev, "renaming to %s\n", new_name);
  2581. old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
  2582. if (!old_device_name) {
  2583. error = -ENOMEM;
  2584. goto out;
  2585. }
  2586. if (dev->class) {
  2587. error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
  2588. kobj, old_device_name,
  2589. new_name, kobject_namespace(kobj));
  2590. if (error)
  2591. goto out;
  2592. }
  2593. error = kobject_rename(kobj, new_name);
  2594. if (error)
  2595. goto out;
  2596. out:
  2597. put_device(dev);
  2598. kfree(old_device_name);
  2599. return error;
  2600. }
  2601. EXPORT_SYMBOL_GPL(device_rename);
  2602. static int device_move_class_links(struct device *dev,
  2603. struct device *old_parent,
  2604. struct device *new_parent)
  2605. {
  2606. int error = 0;
  2607. if (old_parent)
  2608. sysfs_remove_link(&dev->kobj, "device");
  2609. if (new_parent)
  2610. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  2611. "device");
  2612. return error;
  2613. }
  2614. /**
  2615. * device_move - moves a device to a new parent
  2616. * @dev: the pointer to the struct device to be moved
  2617. * @new_parent: the new parent of the device (can be NULL)
  2618. * @dpm_order: how to reorder the dpm_list
  2619. */
  2620. int device_move(struct device *dev, struct device *new_parent,
  2621. enum dpm_order dpm_order)
  2622. {
  2623. int error;
  2624. struct device *old_parent;
  2625. struct kobject *new_parent_kobj;
  2626. dev = get_device(dev);
  2627. if (!dev)
  2628. return -EINVAL;
  2629. device_pm_lock();
  2630. new_parent = get_device(new_parent);
  2631. new_parent_kobj = get_device_parent(dev, new_parent);
  2632. if (IS_ERR(new_parent_kobj)) {
  2633. error = PTR_ERR(new_parent_kobj);
  2634. put_device(new_parent);
  2635. goto out;
  2636. }
  2637. pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
  2638. __func__, new_parent ? dev_name(new_parent) : "<NULL>");
  2639. error = kobject_move(&dev->kobj, new_parent_kobj);
  2640. if (error) {
  2641. cleanup_glue_dir(dev, new_parent_kobj);
  2642. put_device(new_parent);
  2643. goto out;
  2644. }
  2645. old_parent = dev->parent;
  2646. dev->parent = new_parent;
  2647. if (old_parent)
  2648. klist_remove(&dev->p->knode_parent);
  2649. if (new_parent) {
  2650. klist_add_tail(&dev->p->knode_parent,
  2651. &new_parent->p->klist_children);
  2652. set_dev_node(dev, dev_to_node(new_parent));
  2653. }
  2654. if (dev->class) {
  2655. error = device_move_class_links(dev, old_parent, new_parent);
  2656. if (error) {
  2657. /* We ignore errors on cleanup since we're hosed anyway... */
  2658. device_move_class_links(dev, new_parent, old_parent);
  2659. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  2660. if (new_parent)
  2661. klist_remove(&dev->p->knode_parent);
  2662. dev->parent = old_parent;
  2663. if (old_parent) {
  2664. klist_add_tail(&dev->p->knode_parent,
  2665. &old_parent->p->klist_children);
  2666. set_dev_node(dev, dev_to_node(old_parent));
  2667. }
  2668. }
  2669. cleanup_glue_dir(dev, new_parent_kobj);
  2670. put_device(new_parent);
  2671. goto out;
  2672. }
  2673. }
  2674. switch (dpm_order) {
  2675. case DPM_ORDER_NONE:
  2676. break;
  2677. case DPM_ORDER_DEV_AFTER_PARENT:
  2678. device_pm_move_after(dev, new_parent);
  2679. devices_kset_move_after(dev, new_parent);
  2680. break;
  2681. case DPM_ORDER_PARENT_BEFORE_DEV:
  2682. device_pm_move_before(new_parent, dev);
  2683. devices_kset_move_before(new_parent, dev);
  2684. break;
  2685. case DPM_ORDER_DEV_LAST:
  2686. device_pm_move_last(dev);
  2687. devices_kset_move_last(dev);
  2688. break;
  2689. }
  2690. put_device(old_parent);
  2691. out:
  2692. device_pm_unlock();
  2693. put_device(dev);
  2694. return error;
  2695. }
  2696. EXPORT_SYMBOL_GPL(device_move);
  2697. /**
  2698. * device_shutdown - call ->shutdown() on each device to shutdown.
  2699. */
  2700. void device_shutdown(void)
  2701. {
  2702. struct device *dev, *parent;
  2703. wait_for_device_probe();
  2704. device_block_probing();
  2705. cpufreq_suspend();
  2706. spin_lock(&devices_kset->list_lock);
  2707. /*
  2708. * Walk the devices list backward, shutting down each in turn.
  2709. * Beware that device unplug events may also start pulling
  2710. * devices offline, even as the system is shutting down.
  2711. */
  2712. while (!list_empty(&devices_kset->list)) {
  2713. dev = list_entry(devices_kset->list.prev, struct device,
  2714. kobj.entry);
  2715. /*
  2716. * hold reference count of device's parent to
  2717. * prevent it from being freed because parent's
  2718. * lock is to be held
  2719. */
  2720. parent = get_device(dev->parent);
  2721. get_device(dev);
  2722. /*
  2723. * Make sure the device is off the kset list, in the
  2724. * event that dev->*->shutdown() doesn't remove it.
  2725. */
  2726. list_del_init(&dev->kobj.entry);
  2727. spin_unlock(&devices_kset->list_lock);
  2728. /* hold lock to avoid race with probe/release */
  2729. if (parent)
  2730. device_lock(parent);
  2731. device_lock(dev);
  2732. /* Don't allow any more runtime suspends */
  2733. pm_runtime_get_noresume(dev);
  2734. pm_runtime_barrier(dev);
  2735. if (dev->class && dev->class->shutdown_pre) {
  2736. if (initcall_debug)
  2737. dev_info(dev, "shutdown_pre\n");
  2738. dev->class->shutdown_pre(dev);
  2739. }
  2740. if (dev->bus && dev->bus->shutdown) {
  2741. if (initcall_debug)
  2742. dev_info(dev, "shutdown\n");
  2743. dev->bus->shutdown(dev);
  2744. } else if (dev->driver && dev->driver->shutdown) {
  2745. if (initcall_debug)
  2746. dev_info(dev, "shutdown\n");
  2747. dev->driver->shutdown(dev);
  2748. }
  2749. device_unlock(dev);
  2750. if (parent)
  2751. device_unlock(parent);
  2752. put_device(dev);
  2753. put_device(parent);
  2754. spin_lock(&devices_kset->list_lock);
  2755. }
  2756. spin_unlock(&devices_kset->list_lock);
  2757. }
  2758. /*
  2759. * Device logging functions
  2760. */
  2761. #ifdef CONFIG_PRINTK
  2762. static int
  2763. create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
  2764. {
  2765. const char *subsys;
  2766. size_t pos = 0;
  2767. if (dev->class)
  2768. subsys = dev->class->name;
  2769. else if (dev->bus)
  2770. subsys = dev->bus->name;
  2771. else
  2772. return 0;
  2773. pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
  2774. if (pos >= hdrlen)
  2775. goto overflow;
  2776. /*
  2777. * Add device identifier DEVICE=:
  2778. * b12:8 block dev_t
  2779. * c127:3 char dev_t
  2780. * n8 netdev ifindex
  2781. * +sound:card0 subsystem:devname
  2782. */
  2783. if (MAJOR(dev->devt)) {
  2784. char c;
  2785. if (strcmp(subsys, "block") == 0)
  2786. c = 'b';
  2787. else
  2788. c = 'c';
  2789. pos++;
  2790. pos += snprintf(hdr + pos, hdrlen - pos,
  2791. "DEVICE=%c%u:%u",
  2792. c, MAJOR(dev->devt), MINOR(dev->devt));
  2793. } else if (strcmp(subsys, "net") == 0) {
  2794. struct net_device *net = to_net_dev(dev);
  2795. pos++;
  2796. pos += snprintf(hdr + pos, hdrlen - pos,
  2797. "DEVICE=n%u", net->ifindex);
  2798. } else {
  2799. pos++;
  2800. pos += snprintf(hdr + pos, hdrlen - pos,
  2801. "DEVICE=+%s:%s", subsys, dev_name(dev));
  2802. }
  2803. if (pos >= hdrlen)
  2804. goto overflow;
  2805. return pos;
  2806. overflow:
  2807. dev_WARN(dev, "device/subsystem name too long");
  2808. return 0;
  2809. }
  2810. int dev_vprintk_emit(int level, const struct device *dev,
  2811. const char *fmt, va_list args)
  2812. {
  2813. char hdr[128];
  2814. size_t hdrlen;
  2815. hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
  2816. return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
  2817. }
  2818. EXPORT_SYMBOL(dev_vprintk_emit);
  2819. int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
  2820. {
  2821. va_list args;
  2822. int r;
  2823. va_start(args, fmt);
  2824. r = dev_vprintk_emit(level, dev, fmt, args);
  2825. va_end(args);
  2826. return r;
  2827. }
  2828. EXPORT_SYMBOL(dev_printk_emit);
  2829. static void __dev_printk(const char *level, const struct device *dev,
  2830. struct va_format *vaf)
  2831. {
  2832. if (dev)
  2833. dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
  2834. dev_driver_string(dev), dev_name(dev), vaf);
  2835. else
  2836. printk("%s(NULL device *): %pV", level, vaf);
  2837. }
  2838. void dev_printk(const char *level, const struct device *dev,
  2839. const char *fmt, ...)
  2840. {
  2841. struct va_format vaf;
  2842. va_list args;
  2843. va_start(args, fmt);
  2844. vaf.fmt = fmt;
  2845. vaf.va = &args;
  2846. __dev_printk(level, dev, &vaf);
  2847. va_end(args);
  2848. }
  2849. EXPORT_SYMBOL(dev_printk);
  2850. #define define_dev_printk_level(func, kern_level) \
  2851. void func(const struct device *dev, const char *fmt, ...) \
  2852. { \
  2853. struct va_format vaf; \
  2854. va_list args; \
  2855. \
  2856. va_start(args, fmt); \
  2857. \
  2858. vaf.fmt = fmt; \
  2859. vaf.va = &args; \
  2860. \
  2861. __dev_printk(kern_level, dev, &vaf); \
  2862. \
  2863. va_end(args); \
  2864. } \
  2865. EXPORT_SYMBOL(func);
  2866. define_dev_printk_level(_dev_emerg, KERN_EMERG);
  2867. define_dev_printk_level(_dev_alert, KERN_ALERT);
  2868. define_dev_printk_level(_dev_crit, KERN_CRIT);
  2869. define_dev_printk_level(_dev_err, KERN_ERR);
  2870. define_dev_printk_level(_dev_warn, KERN_WARNING);
  2871. define_dev_printk_level(_dev_notice, KERN_NOTICE);
  2872. define_dev_printk_level(_dev_info, KERN_INFO);
  2873. #endif
  2874. static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
  2875. {
  2876. return fwnode && !IS_ERR(fwnode->secondary);
  2877. }
  2878. /**
  2879. * set_primary_fwnode - Change the primary firmware node of a given device.
  2880. * @dev: Device to handle.
  2881. * @fwnode: New primary firmware node of the device.
  2882. *
  2883. * Set the device's firmware node pointer to @fwnode, but if a secondary
  2884. * firmware node of the device is present, preserve it.
  2885. */
  2886. void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
  2887. {
  2888. if (fwnode) {
  2889. struct fwnode_handle *fn = dev->fwnode;
  2890. if (fwnode_is_primary(fn))
  2891. fn = fn->secondary;
  2892. if (fn) {
  2893. WARN_ON(fwnode->secondary);
  2894. fwnode->secondary = fn;
  2895. }
  2896. dev->fwnode = fwnode;
  2897. } else {
  2898. dev->fwnode = fwnode_is_primary(dev->fwnode) ?
  2899. dev->fwnode->secondary : NULL;
  2900. }
  2901. }
  2902. EXPORT_SYMBOL_GPL(set_primary_fwnode);
  2903. /**
  2904. * set_secondary_fwnode - Change the secondary firmware node of a given device.
  2905. * @dev: Device to handle.
  2906. * @fwnode: New secondary firmware node of the device.
  2907. *
  2908. * If a primary firmware node of the device is present, set its secondary
  2909. * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
  2910. * @fwnode.
  2911. */
  2912. void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
  2913. {
  2914. if (fwnode)
  2915. fwnode->secondary = ERR_PTR(-ENODEV);
  2916. if (fwnode_is_primary(dev->fwnode))
  2917. dev->fwnode->secondary = fwnode;
  2918. else
  2919. dev->fwnode = fwnode;
  2920. }
  2921. /**
  2922. * device_set_of_node_from_dev - reuse device-tree node of another device
  2923. * @dev: device whose device-tree node is being set
  2924. * @dev2: device whose device-tree node is being reused
  2925. *
  2926. * Takes another reference to the new device-tree node after first dropping
  2927. * any reference held to the old node.
  2928. */
  2929. void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
  2930. {
  2931. of_node_put(dev->of_node);
  2932. dev->of_node = of_node_get(dev2->of_node);
  2933. dev->of_node_reused = true;
  2934. }
  2935. EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);