core.c 82 KB

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