core.c 74 KB

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