core.c 79 KB

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