core.c 79 KB

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