core.c 75 KB

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