core.c 78 KB

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