core.c 74 KB

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