core.c 80 KB

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