core.c 82 KB

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