core.c 74 KB

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