core.c 74 KB

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